我发现很难找到一个允许程序以任意音高播放音符的 python 合成器模块。最好的音符应该不仅仅是一个纯正弦波,并且应该包括至少一些谐波 - 它应该不仅仅是一个哔哔声。
这个想法是能够写出类似的东西
the_module.play(frequency, loudness, duration)
或者
my_synth = the_module.newsynth()
my_synth.play(frequency, loudness, duration)
其中频率以 Hz 为单位指定,并从用户的扬声器播放合成音调。有用于执行此操作的 JavaScript 模块,例如 Tone.js ,但是有人知道使用 Python 的方法吗?
最佳答案
如果在 windows 上,你可以使用内置的 winsound.Beep
.
如果在 Linux 上,则需要直接写入 /dev/audio
, 喜欢建议 here :
def beep(frequency, amplitude, duration):
sample = 8000
half_period = int(sample/frequency/2)
beep = chr(amplitude)*half_period+chr(0)*half_period
beep *= int(duration*frequency)
audio = file('/dev/audio', 'wb')
audio.write(beep)
audio.close()
关于python - 在python中以任意音高合成声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57059223/