【发布时间】:2017-03-09 10:49:21
【问题描述】:
我正在尝试使用 python 使用我的 rpi3 从 USB 麦克风以 8Khz 录制语音。我使用了 pyaudio、sounddevice 和 soundfile 库,但它们只能让我以 44100Hz 或 48000Hz 采样。当我尝试以 8KHz 采样时,出现以下错误:
“PortAudioError: Error opening InputStream: Invalid sample rate”。
另一方面,当我使用命令时:
"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
我检查过,我知道我使用了正确的设备,但正如您所见,默认采样率没有改变,我仍然得到同样的错误。
【问题讨论】:
-
那么你的问题是什么?
-
如何使用python进行8KHz采样?
-
你能展示你用来录制音频的 Python 代码吗?
标签: python raspberry-pi