pyaudio没有使用16000采样率使用USB音频设备索引和获取错误" Errno无效采样率] -9997"在ubuntu 12.04 LTS

时间:2014-04-09 00:15:49

标签: python-2.7 audio usb ubuntu-12.04 pyaudio

我正在尝试使用 pyaudio 播放wav文件并选择输出设备索引 - USB高级音频设备:USB音频(hw:1,0)。

如果我选择帧速率48000.000000 wav文件并选择输出设备索引 - USB Audio它工作正常,我能听到.BUT 但如果我选择帧速率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,如果我选择&#34;默认&#34;设备索引它工作正常。我的代码很容易得到音频设备输出索引,我传递输出设备索引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采样率,请告诉我。

0 个答案:

没有答案