OSX上的Pyaudio类型错误

时间:2016-03-14 04:47:31

标签: python audio microphone portaudio pyaudio

尝试在OSX上使用pyaudio进行基本麦克风录音时出现类型错误。我刚刚用brew安装了portaudio,并用pip安装了最新的pyaudio。他们似乎安装得很好。但是在运行我的代码时,我得到了:

Traceback (most recent call last):
  File "micTest.py", line 37, in <module>
    data = stream.read(CHUNK)
  File "/usr/local/lib/python2.7/site-packages/pyaudio.py", line 608, in read
    return pa.read_stream(self._stream, num_frames, exception_on_overflow)
TypeError: function takes exactly 2 arguments (3 given)

有问题的代码是:

import audioop
import pyaudio
import wave

FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "file.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)
print "recording..."
frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):

    data = stream.read(CHUNK)
    mx = audioop.max(data, 2)
    print mx
    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()

default_audio_device_info如下所示:

{
 'defaultSampleRate': 44100.0,
 'defaultLowOutputLatency': 0.01,
 'defaultLowInputLatency': 0.00199546485260771,
 'maxInputChannels': 2L,
 'structVersion': 2L,
 'hostApi': 0L,
 'index': 0L,
 'defaultHighOutputLatency': 0.1,
 'maxOutputChannels': 0L,
 'name': u'Built-in Microph',
 'defaultHighInputLatency': 0.012154195011337868
}

不知道这里可能出现什么问题,或者为什么读取方法中的'self'被算作参数。

1 个答案:

答案 0 :(得分:-2)

我不知道PyAudio有什么问题,但您可能想尝试sounddevice模块?

import sounddevice as sd
fs = 44100
myrec = sd.rec(RECORD_SECONDS * fs, fs, channels=2)

要将myrec存储为文件,您可以使用soundfile模块。