3

我正在尝试从麦克风录制声音。首先使用 PyAudio 然后 sounddevice 但都失败了。

这是 PyAudio 的代码:

import pyaudio


def _recording_loop(samples_queue, running, stream, chunk_size):
    stream.start_stream()

    while running.is_set():
        samples_queue.put(stream.read(chunk_size))

    stream.stop_stream()


class Recoder:

    def __init__(self, frame_rate, period):
        self.proc = None
        self.running = Event()
        self.samples_queue = Queue()
        self.frame_rate = frame_rate
        self.chunk_size = (frame_rate*period) / 1000
        self.channels = 1

        self._pa = pyaudio.PyAudio()
        self._stream = None

    def start(self):
        if self.proc is None:
            self._stream = self._pa.open(format=pyaudio.paInt8,
                                         channels=self.channels,
                                         rate=self.frame_rate,
                                         input=True,
                                         frames_per_buffer=self.chunk_size)

            self.running.set()
            self.proc = Process(target=_recording_loop, args=[self.samples_queue, self.running, self._stream,
                                                              self.chunk_size])
            self.proc.start()

    def stop(self):
        if self.proc is not None:
            self.running.clear()
            self.proc.join()

        self._stream.close()
        self._pa.terminate()

    def empty(self):
        return self.samples_queue.empty()

    def read(self):
        res = []
        while not self.samples_queue.empty():
            res.append(self.samples_queue.get())
    return res

它给了我一个警告: Python[21648:645093] 13:42:01.242 WARNING: 140: This application, or a library it uses, is using the deprecated Carbon Component Manager for hosting Audio Units. Support for this will be removed in a future release. Also, this makes the host incompatible with version 3 audio units. Please transition to the API's in AudioComponent.h. 没有任何记录。

据我了解,这是 El Capitan 的问题,尚未解决。但也许我错了?

所以我决定将库切换到 sounddevice:

from multiprocessing import Process, Queue, Event
import sounddevice as sd


def _recording_loop(samples_queue, running, frame_rate, chunk_size):
    while running.is_set():
        samples_queue.put(sd.rec(chunk_size, samplerate=frame_rate, channels=1,
                                 dtype='int8', blocking=True))


class Recoder:

    def __init__(self, frame_rate, period):

        self.proc = None
        self.running = Event()
        self.samples_queue = Queue()
        self.frame_rate = frame_rate
        self.period = period

        self.chunk_size = (frame_rate * period) / 1000

    def start(self):
        if self.proc is None:
            self.running.set()
            self.proc = Process(target=_recording_loop, args=[self.samples_queue, self.running, self.frame_rate,
                                                              self.chunk_size])
            self.proc.start()

    def stop(self):
        if self.proc is not None:
            self.running.clear()
            self.proc.join()

    def empty(self):
        return self.samples_queue.empty()

    def read(self):
        res = []
        while not self.samples_queue.empty():
            res.append(self.samples_queue.get())

        return res

它说:

||PaMacCore (AUHAL)|| Warning on line 530: err=''who?'', msg=Audio Hardware: Unknown Property
||PaMacCore (AUHAL)|| Warning on line 534: err=''who?'', msg=Audio Hardware: Unknown Property
||PaMacCore (AUHAL)|| Warning on line 445: err=''who?'', msg=Audio Hardware: Unknown Property

再一次没有记录。我做错了什么?

4

2 回答 2

0

sounddevice.rec()不应该这样使用。您只需使用要记录的帧数调用它即可(请参阅文档中的示例):

import sounddevice as sd

fs = 44100
duration = 10  # seconds
myrecording = sd.rec(duration * fs, samplerate=fs, channels=2,
                     blocking=True)

而已。您不需要半页代码来录制一些声音。

顺便说一句,您现在可以忽略警告,请参阅https://github.com/spatialaudio/python-sounddevice/issues/10

于 2016-09-02T19:30:23.367 回答
0

昨天我遇到了类似的问题。这似乎是由于使用带有声音设备的多处理引起的。当我import sounddevice在模块顶部执行时,我得到||PaMacCore (AUHAL)|| Warning on line 530: err=''who?'', msg=Audio Hardware: Unknown Property然后应用程序在创建时挂起sounddevice.RawInputStream。当我在我的run()方法中导入 sounddevice 时(我正在创建一个基于 的新类multiprocessing.Process),它工作正常。对我来说,sounddevice 似乎在导入后立即进行了初始化,这必须发生在将使用它的同一进程中。 编辑:而不是Multiprocessing使用Threading.

于 2016-10-01T09:44:14.967 回答