用python生成/合成声音?

是否有可能让python产生像正弦波这样的简单声音?

是否有可用的模块?如果没有,您将如何创建自己的?

另外,您是否需要某种主机环境让python运行才能播放声音,还是可以仅通过从终端拨打电话来实现?

如果答案取决于操作系统,我使用的是 mac。

已邀请:
我一直在寻找同样的东西,最后,我写了这段运行良好的代码。

import math #import needed modules
import pyaudio #sudo apt-get install python-pyaudio

PyAudio = pyaudio.PyAudio #initialize pyaudio

#See https://en.wikipedia.org/wiki/Bit_rate#Audio
BITRATE = 16000 #number of frames per second/frameset.

FREQUENCY = 500 #Hz, waves per second, 261.63=C4-note.
LENGTH = 1 #seconds to play sound

if FREQUENCY > BITRATE:
BITRATE = FREQUENCY+100

NUMBEROFFRAMES = int(BITRATE * LENGTH)
RESTFRAMES = NUMBEROFFRAMES % BITRATE
WAVEDATA = ''

#generating wawes
for x in xrange(NUMBEROFFRAMES):
WAVEDATA = WAVEDATA+chr(int(math.sin(x/((BITRATE/FREQUENCY)/math.pi))*127+128))

for x in xrange(RESTFRAMES):
WAVEDATA = WAVEDATA+chr(128)

p = PyAudio()
stream = p.open(format = p.get_format_from_width(1),
channels = 1,
rate = BITRATE,
output = True)

stream.write(WAVEDATA)
stream.stop_stream()
stream.close()

要回复问题请先登录注册