gpt4 book ai didi

python - 在python中合成音频音高

转载 作者:行者123 更新时间:2023-12-03 00:46:03 30 4
gpt4 key购买 nike

关闭。这个问题需要更多 focused .它目前不接受答案。












想改进这个问题?更新问题,使其仅关注一个问题 editing this post .

7年前关闭。




Improve this question




我想知道是否有办法设置音频音高。一定的语气是基础。我想知道如何使音高上升或下降。谢谢。

还有你如何播放音频。另外,如果您知道执行此操作的任何模块,我想知道它们,谢谢。

我的目标是创造一个盲人可以玩的乒乓球游戏。球越高,球场越高。球越低,球场越低。最好在python中。提前致谢

最佳答案

如果你想尝试 pyaudio 库,那么你可以使用我前几天创建的这段函数代码!

import pyaudio
import struct
import math

SHRT_MAX=32767 # short uses 16 bits in complement 2

def my_sin(t,frequency):
radians = t * frequency * 2.0 * math.pi
pulse = math.sin(radians)
return pulse

#pulse_function creates numbers in [-1,1] interval
def generate(duration = 5,pulse_function = (lambda t: my_sin(t,1000))):
sample_width=2
sample_rate = 44100
sample_duration = 1.0/sample_rate
total_samples = int(sample_rate * duration)
p = pyaudio.PyAudio()
pformat = p.get_format_from_width(sample_width)
stream = p.open(format=pformat,channels=1,rate=sample_rate,output=True)
for n in range(total_samples):
t = n*sample_duration
pulse = int(SHRT_MAX*pulse_function(t))
data=struct.pack("h",pulse)
stream.write(data)

#example of a function I took from wikipedia.
major_chord = f = lambda t: (my_sin(t,440)+my_sin(t,550)+my_sin(t,660))/3

#choose any frequency you want
#choose amplitude from 0 to 1
def create_pulse_function(frequency=1000,amplitude=1):
return lambda t: amplitude * my_sin(t,frequency)

if __name__=="__main__":
# play fundamental sound at 1000Hz for 5 seconds at maximum intensity
f = create_pulse_function(1000,1)
generate(pulse_function=f)
# play fundamental sound at 500Hz for 5 seconds at maximum intensity
f = create_pulse_function(500,1)
generate(pulse_function=f)
# play fundamental sound at 500Hz for 5 seconds at 50% intensity
f = create_pulse_function(500,0.5)
generate(pulse_function=f)

关于python - 在python中合成音频音高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26761055/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com