我正在尝试在 ubuntu 12.04 中使用pyaudio播放 wav 文件并选择输出设备索引 - USB Advanced Audio Device: USB Audio (hw:1,0) 。
如果我选择帧速率 48000.000000 wav 文件并选择输出设备索引 - USB 音频它工作正常,我可以听到。但是如果我选择帧速率 16000.000000 wav 文件并选择输出设备索引 - USB 音频它工作正常,我听不见,它显示以下错误:
Expression 'SetApproximateSampleRate( pcm, hwParams, sr )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1968
Expression 'PaAlsaStreamComponent_InitialConfigure( &self->playback, outParams, self->primeBuffers, hwParamsPlayback, &realSr )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2646
Expression 'PaAlsaStream_Configure( stream, inputParameters, outputParameters, sampleRate, framesPerBuffer, &inputLatency, &outputLatency, &hostBufferSizeMode )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2767
Traceback (most recent call last):
File "speech.py", line 34, in <module>
output=True)
File "/usr/lib/pymodules/python2.7/pyaudio.py", line 714, in open
stream = Stream(self, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/pyaudio.py", line 396, in __init__
self._stream = pa.open(**arguments)
IOError: [Errno Invalid sample rate] -9997
如果我选择“默认”设备索引,则相同的采样率 16 k 它工作正常。我的代码很容易获取音频设备输出索引,并且我正在传递输出设备索引 ID
import pyaudio
import wave
import sys
if len(sys.argv) < 2:
print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
sys.exit(-1)
"""wf = wave.open('C:/Users/arunku2x/Desktop/bar_opening.wav', 'rb')"""
wf = wave.open(sys.argv[1], 'rb')
p = pyaudio.PyAudio()
count = p.get_device_count()
devices = []
for i in range(count):
devices.append(p.get_device_info_by_index(i))
for i, dev in enumerate(devices):
print "%d - %s" % (i, dev['name'])
print "Sample Width %f" % wf.getsampwidth()
print "Number of channels %d" % wf.getnchannels()
print "Frame rate %f" % wf.getframerate()
#x = p.is_format_supported(rate = wf.getframerate(),output_device = 4 ,output_channels = wf.getnchannels(),output_format= p.get_format_from_width(wf.getsampwidth()))
#print "format .............x %d" % x
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output_device_index =4,#here 4 means - USB Audio device
output=True)
data = wf.readframes(1024)
while data != '':
stream.write(data)
data=wf.readframes(1024)
使用 PyAudio 播放声音。如果您有任何想法使用带有 USB 音频设备的 PyAudio 来处理 16k 采样率,请告诉我。