How to imporve audio sound quality from 2 input devices using Pyaudio?

21 Views Asked by At

The sound recorded using 2 input devices (1 is Microphone, 2 is Stereo Mix) has:

  1. Echo sound for sound from Stereo Mix
  2. Continous noises for sound from Microphone. Those problems are not seem just from a single auido source.
import pyaudio
import wave
import numpy as np

import time
import threading
from typing import List

class MultiStream:
    def __init__(self):
        self.accumulated_buffers: List = []
        self.save_path: str = "output_ms.wav"
        self.p = pyaudio.PyAudio()
        self.streams = [self.p.open(
            format=FORMAT,
            channels=CHANNELS,
            rate=RATE,
            input=True,
            input_device_index=device_index,
            frames_per_buffer=CHUNK
        ) for device_index in DEVICE_INDICES]

    def __enter__(self):
        print(f"{BLUE_START}Recording Started with MultiStream... {RESET}")
        self.start_record()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.stop_record()
        print(f"{BLUE_START}Recording stopped. Converting to WAV file... {RESET}")

    def start_record(self):
        self.accumulated_buffers = []

        def record_thread():
            while True:
                buffers = [stream.read(CHUNK) for stream in self.streams]
                buffers_np = [np.frombuffer(buffer, dtype=np.int16) for buffer in buffers]
                combined_buffer = np.sum(buffers_np, axis=0)
                combined_buffer_bytes = combined_buffer.astype(np.int16).tobytes()
                self.accumulated_buffers.append(combined_buffer_bytes)

        threading._start_new_thread(record_thread, ())

    def stop_record(self):
        with wave.open(self.save_path, 'wb') as wf:
            wf.setnchannels(CHANNELS)
            wf.setsampwidth(pyaudio.get_sample_size(FORMAT))
            wf.setframerate(RATE)
            wf.writeframes(b''.join(self.accumulated_buffers))
        
        for stream in self.streams:
            stream.stop_stream()
            stream.close()
            self.p.terminate()

Any suggestion for how to avoid those issues?

0

There are 0 best solutions below