noisereduce icon indicating copy to clipboard operation
noisereduce copied to clipboard

Checking signal for mono / noisereduce on multichannel audio

Open timsainb opened this issue 6 years ago • 1 comments

Several people have asked for noisereduce to work for multiple channels.

timsainb avatar Jan 20 '20 18:01 timsainb

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()

Sharrnah avatar Jun 28 '24 17:06 Sharrnah