pydub icon indicating copy to clipboard operation
pydub copied to clipboard

How to turn a 2 channel audio file into a 4 channel one?

Open Fear-MK opened this issue 4 years ago • 4 comments

This isn't much so an issue, but rather a question. How can I make a 4 channel (and above (6, 8 channels)) music file without the 3rd and 4th channel being blank?

def speed_change(speed): for file in filename_tracks: audio_input = filename_tracks[file]+"_amp.wav" audio_output = filename_tracks[file]+"_f.wav" sound = AudioSegment.from_file(audio_input) sound_with_altered_frame_rate = sound._spawn(sound.raw_data, overrides={ "frame_rate": int(sound.frame_rate * speed) }) speed_up = sound_with_altered_frame_rate.set_frame_rate(sound.frame_rate) if "STRM" in filename_tracks[file]: speed_up.export(audio_output, format="wav", parameters=["-ac", "4"]) else: speed_up.export(audio_output, format="wav")

This is the code being included at the moment, and currently it leaves channels 3 and 4 blank.

Fear-MK avatar Nov 05 '21 19:11 Fear-MK

So far, I have never seen multichannel audio being used in PyDub. In fact, my very crude and basic eq function relies on the fact that all files processed are Stereo or Mono and not multichannel. If multichannel audio is supported, I would have to change the design and pattern of that function a little.

Can anyone please confirm this? @jiaaro @ross @cghawthorne @shadchin @emyller

anubhav-narayan avatar Nov 06 '21 09:11 anubhav-narayan

I knew you could mix down to 2 channels (from reading the readme), which gave me the impression you could somehow do the opposite, thought I may be wrong. If there was another library that made multichannel files that would be great too, but my lack of googling skills means I haven't found anything thus far.

Fear-MK avatar Nov 06 '21 09:11 Fear-MK

Could you load them all as mono and combine from there?

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

https://stackoverflow.com/questions/44920182/how-to-write-multi-channel-wav-file-in-python

mjlabe avatar Apr 11 '22 18:04 mjlabe

The module cutcutcodec is able to deal with a lot of audio channels. To turn n channels into m channels with m < n, conventions exist. some classic matrices are implemented here. But with m > n, you have to invent your own rule! For example, to go from stereo to 5.1 you can do:

from cutcutcodec.core.io import read, write
from cutcutcodec.core.filter.audio.equation import FilterAudioEquation
with read("audio_stereo.mp3") as container:
    s2 = container.out_streams[0]
    s6 = FilterAudioEquation([s2], "fl_0", "fr_0", "(fl_0+fr_0)/2", "(fl_0+fr_0)/4", "fl_0", "fr_0").out_streams[0]
    write([s6], "audio_5.1.opus", [{"encodec": "libvorbis", "rate": 44100}])

robinechuca avatar Dec 20 '24 15:12 robinechuca