Matt
Matt

Reputation: 3652

How can I convert a WAV from stereo to mono in Python?

I don't want to use any other apps (like sox) - I want to do this in pure Python. Installing needed Python libs is fine.

Upvotes: 29

Views: 48138

Answers (3)

Ladislav Vašina
Ladislav Vašina

Reputation: 71

The simplest way I can think of is using the PyTorch mean function as in the example below.

import torch
import torchaudio

def stereo_to_mono_convertor(signal):
    # If there is more than 1 channel in your audio
    if signal.shape[0] > 1:
        # Do a mean of all channels and keep it in one channel
        signal = torch.mean(signal, dim=0, keepdim=True)
    return signal

# Load audio as tensor
waveform, sr = torchaudio.load('audio.wav')
# Convert it to mono channel
waveform = stereo_to_mono_convertor(waveform)

Upvotes: 2

Jiaaro
Jiaaro

Reputation: 76958

I maintain an open source library, pydub, which make this pretty simple

from pydub import AudioSegment
sound = AudioSegment.from_wav("/path/to/file.wav")
sound = sound.set_channels(1)
sound.export("/output/path.wav", format="wav")

One caveat: it uses ffmpeg to handle audio format conversions, but if you only use wav it can be pure python.

Upvotes: 61

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799310

If the WAV file is PCM-encoded then you can use wave. Open the source and destination files, read samples, average the channels, and write them out.

Upvotes: 12

Related Questions