简体   繁体   中英

Merging several MP3 tracks in to one track

Well frankly, I don't know where to begin with this one. I have to have a program that merges a few mp3 tracks in to one track (not concatenate, but actually merge them, sorta like playing a few audiotracks all at once). Is there a library available or another basic program that will automatically do this? I also need to change the volume of each track, and I don't know where to start with that too.

Speed and efficiency is a major factor in this, so I can't just do the lazy thing and play them and capture it.

I will try to draw some general steps that you can follow:

  1. You will need to decompress your mp3 files into raw audio. Depending on what kind of application you are developing you could use madlib (GPL) or ffmpeg (LGPL) for example.

  2. You will need to normalize all your tracks into the same frequency (re-sampling), and decide on an output bit-depth .

  3. Now you can mix all your tracks. Assuming that you have 2 channels, the basic idea is to compute an average of all samples of the right channel of your tracks, then do the same with the left channel. There are many approaches for this and here you will have to decide on a trade-off in performance/quality for your results. For example some developers will prefer to do the mix operation on float point, in order to deal with clipping (d)effects , but others may advise against it because it affects performance. There is a good post about integer-based mixing here (it has been referenced several times in SO). Also before (or during) this step you could implement some audio normalization process by using a multiplier on the samples of the channel that you want to normalize.

  4. Finally you can compress your mixed audio again. You could use lame for example on this task, or ffmpeg one more time.

There are many DSP packages for linux out there that might do several (or all) of these steps for you. One GPL project that comes to my mind is VLC (VideoLan Client). You could try to call it as an external process or schedule it with VLM (VideoLan Manager). From the documentation :

Scheduled broadcasting:

new my_media broadcast enabled
setup my_media input my_video.mpeg input my_other_movie.mpeg 
setup my_media output #rtp{mux=ts,dst=239.255.1.1,sdp=sap://,name="My Media"}

Naturally, the output stream in VLC can also be a local file.

Edit: I also found a question on SO where the accepted answer can be useful to you, it recommends using Sox . From the homepage:

SoX is a cross-platform (Windows, Linux, MacOS X, etc.) command line utility that can convert various formats of computer audio files in to other formats. It can also apply various effects to these sound files(...)

You could mix them as separate tracks in audacity. Is it a requirement that you write the code yourself?

when you have the data streams in raw form, you can treat the midpoint of possible sample values as zero.. (8bit samples.. 256 values possible.. 127 is zero.. samples at 0 or 255 are at max volume).

to adjust volume, shift, multiply to increase volume, divide to decrease (be mindful of clipping)

to mix, just add values of each stream, one sample at a time (mindful of clipping).

Here is a good link too: http://www.4front-tech.com/pguide/audio.html

This can be done real-time, given enough processor performance. You need to be able to read the mp3 files, some real-time software mp3 decoder blocks (similar to what one would use to make your own mp3 player/visualizer) that are fast enough to run N copies/threads in real-time, possibly some DSP sample rate conversion blocks, and a DSP mixer block. All software. Tie all the processing threads together with queues and data fifos.

There my be library code available for some of these blocks (decoders, resamplers), but beware potential licensing restrictions.

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