Converting PortAudio to Webrtc

@whitphx I am trying to convert my code from pyaudio to webrtc.

This is how I am currently consuming the audio input from Microphone. I see that Webrtc uses pydub instead of pyaudio. How can I convert this to webrtc?

RATE = 16000
CHUNK = int(RATE / 10)  # 100ms
with MicrophoneStream(RATE, CHUNK, device=args.input_device) as stream:
        audio_generator = stream.generator()
class MicrophoneStream(object):
    """Opens a recording stream as a generator yielding the audio chunks."""

    def __init__(self, rate, chunk, device=None):
        self._rate = rate
        self._chunk = chunk
        self._device = device
        # Create a thread-safe buffer of audio data
        self._buff = queue.Queue()
        self.closed = True

    def __enter__(self):
        self._audio_interface = pyaudio.PyAudio()
        self._audio_stream = self._audio_interface.open(
            format=pyaudio.paInt16,
            input_device_index=self._device,
            channels=1,
            rate=self._rate,
            input=True,
            frames_per_buffer=self._chunk,
            stream_callback=self._fill_buffer,
        )
        self.closed = False
        return self

    def __exit__(self, type=None, value=None, traceback=None):
        self._audio_stream.stop_stream()
        self._audio_stream.close()
        self.closed = True
        # Signal the generator to terminate so that the client's
        # streaming_recognize method will not block the process termination.
        self._buff.put(None)
        self._audio_interface.terminate()

    def _fill_buffer(self, in_data, frame_count, time_info, status_flags):
        """Continuously collect data from the audio stream, into the buffer."""
        self._buff.put(in_data)
        return None, pyaudio.paContinue

    def generator(self):
        while not self.closed:
            chunk = self._buff.get()
            if chunk is None:
                return
            data = [chunk]
            while True:
                try:
                    chunk = self._buff.get(block=False)
                    if chunk is None:
                        return
                    data.append(chunk)
                except queue.Empty:
                    break
            yield b''.join(data)

    def stop(self):
        self.__exit__()

I canโ€™t help because Iโ€™m not familiar with PyAudio.
If there are anyone who have experiences with PyAudio, please answer this question.

What I can say is that pydub is not mandatory to deal with audio streams with streamlit-webrtc. I just used it in the sample app as an example, but itโ€™s independent from WebRTC stuff.


Crossposted to

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.