【问题标题】:Using 8khz sample rate in python on raspberry pi3在覆盆子pi3上的python中使用8khz采样率
【发布时间】: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


【解决方案1】:

您可能在 Python 脚本中打开了错误的设备。可用的采样率由您的硬件决定,在 PyAudio 中打开的采样率不支持 8kHz,而您使用arecord 打开的采样率显然支持。 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 索引的音频设备列表(并因此选择正确的设备),您可以使用this 答案中的代码。

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 2019-02-18
    • 2015-02-07
    • 2017-11-07
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多