我尝试了多种播放固定频率声音(例如 1000Hz)的方法,但都没有效果。
我已经下载了“哔哔”声,但它没有发出任何声音。
我尝试与 pyao 交互,但是没有效果。
我尝试与 audiere 交互,并出现运行时错误,表明无法找到该库,尽管是从软件中心安装的。
任何有关安装适当库和相关代码的指导都将非常感激。
我无法为此生成 .mp3/ .wav 文件,但需要在运行时生成音调。
非常感谢你
答案1
import math
#sudo apt-get install python-pyaudio
from pyaudio import PyAudio
#See http://en.wikipedia.org/wiki/Bit_rate#Audio
BITRATE = 16000 #number of frames per second/frameset.
#See http://www.phy.mtu.edu/~suits/notefreqs.html
FREQUENCY = 261.63 #Hz, waves per second, 261.63=C4-note.
LENGTH = 1.2232 #seconds to play sound
NUMBEROFFRAMES = int(BITRATE * LENGTH)
RESTFRAMES = NUMBEROFFRAMES % BITRATE
WAVEDATA = ''
for x in xrange(NUMBEROFFRAMES):
WAVEDATA += chr(int(math.sin(x / ((BITRATE / FREQUENCY) / math.pi)) * 127 + 128))
#fill remainder of frameset with silence
for x in xrange(RESTFRAMES):
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()
p.terminate()
答案2
您可以使用以下方法执行此操作python-pyaudio
(至少在 12.04 中可用):
from __future__ import division #Avoid division problems in Python 2
import math
import pyaudio
import sys
PyAudio = pyaudio.PyAudio
RATE = 16000
WAVE = 1000
data = ''.join([chr(int(math.sin(x/((RATE/WAVE)/math.pi))*127+128)) for x in xrange(RATE)])
p = PyAudio()
stream = p.open(format =
p.get_format_from_width(1),
channels = 1,
rate = RATE,
output = True)
for DISCARD in xrange(5):
stream.write(data)
stream.stop_stream()
stream.close()
p.terminate()