简体   繁体   English

在树莓派pi3上的python中使用8khz采样率

[英]Using 8khz sample rate in python on raspberry pi3

I'm trying to record voice from USB microphone in 8Khz with my rpi3 using python. 我正在尝试使用python使用rpi3从8Khz的USB麦克风录制语音。 I used pyaudio, sounddevice, and soundfile libraries but they only let me sample in 44100Hz or 48000Hz. 我使用了pyaudio,sounddevice和soundfile库,但它们只允许我以44100Hz或48000Hz采样。 When I tried to sample in 8KHz I got the following error: 当我尝试以8KHz采样时,出现以下错误:

"PortAudioError: Error opening InputStream: Invalid sample rate". “ PortAudioError:打开InputStream时出错:无效的采样率”。

On the other hand when I used the command: 另一方面,当我使用命令时:

"arecord -D plughw:1,0 -f S16_LE -r 8000 -d 2 test.wav" 

in the command-line everything was fine. 在命令行中,一切都很好。

This is the code I used: 这是我使用的代码:

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()

This is the result: 结果如下:

{'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

I checked and i know that I'm using the right device, but as you can see the default sample rate didn't change and I still get the same error. 经过检查,我知道我使用的是正确的设备,但是如您所见,默认采样率没有变化,并且仍然出现相同的错误。

You could be opening the wrong device in your Python script. 您可能在Python脚本中打开了错误的设备。 The available sample rates are determined by your hardware, and the one being opened in PyAudio does not support 8kHz, whereas the one you are opening using arecord clearly does. 可用的采样率由您的硬件决定,在PyAudio中打开的采样率不支持8kHz,而使用arecord打开的采样率显然可以支持8kHz。 The different API's available under Linux seem to index the hardware differently and it can be very confusing (I've certainly found it so). Linux下可用的不同API似乎对硬件建立了不同的索引,这可能非常令人困惑(我当然是这样发现的)。

On my Pi, the USB mic is device 2, and I am sampling at 44.1kHz, so I have: 在我的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)

To get a list of the audio devices, as indexed by PyAudio (and so select the correct device), you can use the code in this answer. 要获取由PyAudio索引的音频设备列表(并选择正确的设备),您可以在答案中使用代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM