在raspberry pi3上使用python中的8khz采样率

在raspberry pi3上使用python中的8khz采样率,python,raspberry-pi,Python,Raspberry Pi,我正试图用我的rpi3用python以8Khz的频率从USB麦克风录制语音。我使用了pyaudio、sounddevice和soundfile库,但它们只允许我以44100Hz或48000Hz采样。当我尝试以8KHz采样时,我得到了以下错误: “PortAudioError:打开InputStream时出错:采样率无效” 另一方面,当我使用命令时: "arecord -D plughw:1,0 -f S16_LE -r 8000 -d 2 test.wav" 在命令行中,一切都很好 这是我使

我正试图用我的rpi3用python以8Khz的频率从USB麦克风录制语音。我使用了pyaudio、sounddevice和soundfile库,但它们只允许我以44100Hz或48000Hz采样。当我尝试以8KHz采样时,我得到了以下错误:

“PortAudioError:打开InputStream时出错:采样率无效”

另一方面,当我使用命令时:

"arecord -D plughw:1,0 -f S16_LE -r 8000 -d 2 test.wav" 
在命令行中,一切都很好

这是我使用的代码:

import pyaudio
import wave

FORMAT = pyaudio.paInt16




CHANNELS = 1
RATE = 8000
CHUNK = 4000
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "test1.wav"

audio = pyaudio.PyAudio()
print audio.get_default_input_device_info()


# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,rate=RATE, input=True,frames_per_buffer=CHUNK,input_device_index=1)
print ("recording...")
frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data) 
print ("finished recording")


# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()



waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
结果是:

{'defaultSampleRate': 44100.0, 'defaultLowOutputLatency': -1.0, 'defaultLowInputLatency': 0.008684807256235827, 'maxInputChannels': 1L, 'structVersion': 2L, 'hostApi': 0L, 'index': 1L, 'defaultHighOutputLatency': -1.0, 'maxOutputChannels': 0L, 'name': u'USB PnP Sound Device: Audio (hw:1,0)', 'defaultHighInputLatency': 0.034829931972789115}

Traceback (most recent call last):
  File "/home/pi/wave1.py", line 20, in <module>
    stream = audio.open(format=FORMAT, channels=CHANNELS,rate=RATE, input=True,frames_per_buffer=CHUNK,input_device_index=1)
  File "build/bdist.linux-armv7l/egg/pyaudio.py", line 750, in open
    stream = Stream(self, *args, **kwargs)
  File "build/bdist.linux-armv7l/egg/pyaudio.py", line 441, in __init__
    self._stream = pa.open(**arguments)
IOError: [Errno -9997] Invalid sample rate
{'defaultSampleRate':44100.0,'defaultLowOutputLatency':-1.0,'defaultLowInputLatency':0.00868480725623582527,'MaxInputChannel':1L,'structVersion':2L,'hostApi':0L,'index':1L,'defaultHighOutputLatency':-1.0,'MaxOutputChannel':0L,'name':u'USB PnP声音设备:音频(hw:1,0)'defaultHighInputLatency':0.0348299319792789115}
回溯(最近一次呼叫最后一次):
文件“/home/pi/wave1.py”,第20行,在
流=音频。打开(格式=格式,通道=通道,速率=速率,输入=真,每缓冲区帧数=块,输入设备索引=1)
文件“build/bdist.linux-armv7l/egg/pyaudio.py”,第750行,打开
流=流(自,*args,**kwargs)
文件“build/bdist.linux-armv7l/egg/pyaudio.py”,第441行,在__
self.\u stream=pa.open(**参数)
IOError:[Errno-9997]采样率无效

我检查了一下,我知道我使用的是正确的设备,但正如你所看到的,默认采样率没有改变,我仍然得到相同的错误

您可能在Python脚本中打开了错误的设备。可用的采样率由您的硬件决定,在PyAudio中打开的采样率不支持8kHz,而您使用
arecord
打开的采样率显然支持8kHz。Linux下可用的不同API似乎对硬件进行了不同的索引,这可能会非常混乱(我确实发现了这一点)

在我的Pi上,USB麦克风是设备2,我以44.1kHz的频率采样,因此我有:

import pyaudio

DEVICE = 2 #USB Audio Device
CHANNELS = 1
FRAME_RATE = 44100
FORMAT = pyaudio.paInt16

def init_stream():
    stream = p.open(format = FORMAT,
                    channels = CHANNELS, 
                    rate = FRAME_RATE, 
                    input = True,
                    output = False,
                    input_device_index = DEVICE,
                    frames_per_buffer = 8192,
                    stream_callback = callback)
    return (stream)

要获得PyAudio索引的音频设备列表(并选择正确的设备),您可以使用代码进行回答。

那么您的问题是什么?如何使用python进行8KHz采样?您可以显示用于录制音频的python代码吗?