Mac os El Capitan recording sound with python
<p>I'm trying to record sound from mic. Firstly as used PyAudio then sounddevice but both failed.</p>

<p>Here is code for PyAudio:</p>

<pre><code>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
</code></pre>

<p>It gives me a warning:
<code>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.
</code> and nothing is ever recorded.</p>

<p>As I understand it's something with El Capitan and not solved yet. But maybe I'm wrong? </p>

<p>So I decided to switch library to sounddevice:</p>

<pre><code>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
</code></pre>

<p>And it says:</p>

<pre><code>||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
</code></pre>

<p>And again nothing is recorded. What I'm doing wrong?</p>

<p>Yesterday I ran into similar problem. It seems that it's caused by using multiprocessing with sounddevice. When I do <code>import sounddevice</code> at the top of the module I get <code>||PaMacCore (AUHAL)|| Warning on line 530: err=''who?'', msg=Audio Hardware: Unknown Property</code> and then the app just hangs while creating <code>sounddevice.RawInputStream</code>. When I import sounddevice in my <code>run()</code> method (I am creating a new class based on <code>multiprocessing.Process</code>) it works fine. For me it seems that sounddevice does something to initialise itself right after being imported and this must happen in the same process that will use it.
<strong>Edit:</strong> instead of <code>Multiprocessing</code> use <code>Threading</code>.</p>

python osx osx-elcapitan pyaudio python-sounddevice