6

我使用 Python 2.7.3、Mac OS 10.8.2 和 Xcode 4.5.1

我正在尝试按照http://people.csail.mit.edu/hubert/pyaudio/中的说明使用 PyAudio 录制声音

并使用该程序

"""PyAudio example: Record a few seconds of audio and save to a WAVE file."""

import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
            channels=CHANNELS,
            rate=RATE,
            input=True,
            frames_per_buffer=CHUNK)

print("* recording")

frames = []

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

print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

它适用于RATE = 44100. 但我想用RATE = 16000和记录CHANNELS = 1

更改值会给我一个错误在此处输入图像描述

我将如何录制RATE = 16000

4

1 回答 1

1

我遇到了同样的问题。该问题已在 portaudio 中修复,但 brew 为我安装了旧版本。我升级了

brew install portaudio --HEAD

然后能够使用 CHANNELS=1 和 RATE=16000 运行您的代码

于 2014-03-27T00:46:56.007 回答