noisereduce
noisereduce copied to clipboard
Checking signal for mono / noisereduce on multichannel audio
Several people have asked for noisereduce to work for multiple channels.
Just a stupid idea since i just stumbled over this issue.
But what if you do each channel seperately?
untested pseudo code:
import noisereduce as nr
import numpy as np
def _interleave(left, right):
"""Given two separate arrays, return a new interleaved array
This function is useful for converting separate left/right audio
streams into one stereo audio stream. Input arrays and returned
array are Numpy arrays.
"""
return np.ravel(np.vstack((left, right)), order='F')
audio_chunk = None # your audio data
input_channels = 2 # for stereo audio
sample_rate = 16000 # your audio sample rate
audio_data_dtype = np.int16 # your audio data type
if isinstance(audio_chunk, bytes):
audio_chunk = np.frombuffer(audio_chunk, dtype=audio_data_dtype)
# reshaped to (-1, input_channels)
audio_data = audio_chunk.reshape(-1, input_channels)
# Process the channels
clean_channels = []
for i in range(input_channels):
channel_data = audio_data[:, i]
# reduce noise on channel
channel_data = nr.reduce_noise(y=channel_data, sr=sample_rate)
clean_channels.append(channel_data)
# Combine the channels again
final_audio_data = _interleave(*clean_channels)
# Convert back to bytes
np.asarray(final_audio_data, dtype=audio_data_dtype).tobytes()