How to write multi channel wav file in python

Viewed 2854

I am reading multi channel audio using pydub and I am doing some manipulation to change the audio's loudness. Now I want to write this multi channel audio as a .wav file?

I don't know how to do this. The pydub doesn't support this action.

Could anyone please help me with this?

Kind regards Denis

2 Answers

I recommend using soundfile's write function. It expects a numpy matrix of shape (N, C), where N is the audio duration in samples and C is the number of channels.

Installation

pip install soundfile

Usage

import soundfile
import numpy as np

sampling_rate = 16000
duration_in_seconds = 1
num_channels = 2

# Create a white noise signal of two channels
audio_signal = np.random.randn(
    sampling_rate*duration_in_seconds,
    num_channels
)

soundfile.write("output.wav", audio_signal, sampling_rate)
Related