python - 在python中合成音频音高

标签 python audio

关闭。这个问题需要更多 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/

相关文章:

audio - 转码音频和视频

ios - 噪声过滤器可消除ios中输入音频流中的噪声

jquery - 如何重播整个音频

python - 使用python将多个文本文件合并为一个文本文件

python - ctypes:公开 C 中 malloc 的结构数组

python - 属性错误: 'tuple' object has no attribute 'replace' For brackets

c# - 使用 FFT 确定低频信号的幅度

flash - 使用 SampleDataEvent 为 NetStream 中的音频实时修改声音

python - Python Nose 测试会显示错误的值吗?

python - 如果多个大型文本文件太大而无法单独转换,如何将它们转换为一个 CSV 文件?