简体   繁体   中英

How to write multi channel wav file in python

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

you can make a multichannel audiosegment from muliple mono audio segments:

from pydub import AudioSegment

# load individual channels...
mutli_channel = AudioSegment.from_mono_audiosegments(channel1, channel2, ..., channel_n)

more info in the pydub docs

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM