使用 Pyaudio 在 Python 中录制音频,错误 ||PaMacCore (AUHAL)|| ... msg=音频单元:在当前上下文中无法执行

Posted

技术标签:

【中文标题】使用 Pyaudio 在 Python 中录制音频,错误 ||PaMacCore (AUHAL)|| ... msg=音频单元:在当前上下文中无法执行【英文标题】:Audio recording in Python with Pyaudio, error ||PaMacCore (AUHAL)|| ... msg=Audio Unit: cannot do in current context 【发布时间】:2021-12-02 20:53:35 【问题描述】:

我正在使用 pyaudio 在我的 Mac BigSur 11.6 (20G165) 上录制声音。具体来说,我使用 BlackHole 将声音从应用程序重定向到输入,效果很好。

它通常可以正常工作,但有时我会在终端中收到此错误:

||PaMacCore (AUHAL)||第 2500 行出错:err='-10863', msg=Audio Unit: cannot do in current context

知道为什么或如何防止它发生(例如,等到 PaMacCore 准备好再次录制或其他什么)?

我已经尝试过重新安装,但没有帮助

brew install portaudio

brew install portaudio --HEAD

这是我的代码:

import pyaudio
import wave


class Recorder(object):
    def __init__(self, fname, mode, channels,
                 rate, frames_per_buffer):
        self.fname = fname
        self.mode = mode
        self.channels = channels
        self.rate = rate
        self.frames_per_buffer = frames_per_buffer
        self._pa = pyaudio.PyAudio()
        self.wavefile = self._prepare_file(self.fname, self.mode)
        self._stream = None

    def __enter__(self):
        return self

    def __exit__(self, exception, value, traceback):
        self.close()

    def record(self, duration):
        # Use a stream with no callback function in blocking mode
        self._stream = self._pa.open(format=pyaudio.paInt16,
                                     channels=self.channels,
                                     rate=self.rate,
                                     input=True,
                                     frames_per_buffer=self.frames_per_buffer)
        for _ in range(int(self.rate / self.frames_per_buffer * duration)):
            audio = self._stream.read(self.frames_per_buffer)
            self.wavefile.writeframes(audio)
        return None

    def start_recording(self):
        # Use a stream with a callback in non-blocking mode
        self._stream = self._pa.open(format=pyaudio.paInt16,
                                     channels=self.channels,
                                     rate=self.rate,
                                     input=True,
                                     frames_per_buffer=self.frames_per_buffer,
                                     stream_callback=self.get_callback())
        self._stream.start_stream()
        return self

    def stop_recording(self):
        self._stream.stop_stream()
        return self

    def get_callback(self):
        def callback(in_data, frame_count, time_info, status):
            self.wavefile.writeframes(in_data)
            return in_data, pyaudio.paContinue

        return callback

    def close(self):
        self._stream.close()
        self._pa.terminate()
        self.wavefile.close()

    def _prepare_file(self, fname, mode='wb'):
        wavefile = wave.open(fname, mode)
        wavefile.setnchannels(self.channels)
        wavefile.setsampwidth(self._pa.get_sample_size(pyaudio.paInt16))
        wavefile.setframerate(self.rate)
        return wavefile

使用示例:

recfile = Recorder(filename, 'wb', 2, 44100, 1024)
recfile.start_recording()
...
recfile.stop_recording()

【问题讨论】:

【参考方案1】:

这是我的代码,它适用于我。

def recording_by_pyaudio():
    chunk = 1024  # Record in chunks of 1024 samples
    sample_format = pyaudio.paInt32  # 16 bits per sample
    channels = 2
    fs = 44100  # Record at 44100 samples per second
    seconds = 4
    output = os.path.join(BASE_DIR, "recording.wav")

    p = pyaudio.PyAudio()  # Create an interface to PortAudio

    print('Recording')

    stream = p.open(format=sample_format,
                    channels=channels,
                    rate=fs,
                    frames_per_buffer=chunk,
                    input=True)
    
    frames = []  # Initialize array to store frames
    # Store data in chunks for 3 seconds
    for i in range(0, int(fs / chunk * seconds)):
        data = stream.read(chunk)
        frames.append(data)

    # Stop and close the stream 
    stream.stop_stream()
    stream.close()
    # Terminate the PortAudio interface
    p.terminate()

    print('Finished recording')

    # Save the recorded data as a WAV file
    wf = wave.open(output, 'wb')
    wf.setnchannels(channels)
    wf.setsampwidth(p.get_sample_size(sample_format))
    wf.setframerate(fs)
    wf.writeframes(b''.join(frames))
    wf.close()
    return output

【讨论】:

【参考方案2】:

显然问题出在 BlackHole 的聚合输出设备中的比特率不匹配。我正在汇总 Blackhole 的输出 (44,1kHz) 和 Mac 扬声器 (48kHz)。这不会导致任何一致的不良行为,但有时会导致这些错误。

【讨论】:

以上是关于使用 Pyaudio 在 Python 中录制音频,错误 ||PaMacCore (AUHAL)|| ... msg=音频单元:在当前上下文中无法执行的主要内容,如果未能解决你的问题,请参考以下文章

使用 opencv 和 pyaudio 从网络摄像头录制视频和音频

pyaudio音频录制及基础音频知识理解

pyaudio录音报错问题

有啥方法可以在python2中录制音频? (苹果电脑)

使用PYaudio录制音频和视频(自己)

使用 PyAudio 将多个图层添加到录音中