How to mix / overlay two mp3 audio file into one mp3 file (not concatenate)

14,206

First of all, in order to mix two audio files you need to manipulate their raw representation; since an MP3 file is compressed, you don't have a direct access to the signal's raw representation. You need to decode the compressed MP3 stream in order to "understand" the wave form of your audio signals and then you will be able to mix them.

Thus, in order to mix two compressed audio file into a single compressed audio file, the following steps are required:

  1. decode the compressed file using a decoder to obtain the raw data (NO PUBLIC SYSTEM API available for this, you need to do it manually!).
  2. mix the two raw uncompressed data streams (applying audio clipping if necessary). For this, you need to consider the raw data format obtained with your decoder (PCM)
  3. encode the raw mixed data into a compressed MP3 file (as per the decoder, you need to do it manually using an encoder)

More info aboud MP3 decoders can be found here.

Share:
14,206

Related videos on Youtube

sandeep
Author by

sandeep

Updated on July 03, 2022

Comments

  • sandeep
    sandeep almost 2 years

    I want to merge two mp3 files into one mp3 file.for example if 1st file is 1min and 2nd file is 30 sec then the output should be one min. In that one min it should play both the files.

  • Advait Saravade
    Advait Saravade over 8 years
    it would be great if you could add some code to your answer.
  • bonnyz
    bonnyz over 8 years
    @AdvaitS This is a lot of work involved, no easy answer for this tasks. You should try to implement it first, and then come back to ask clarifications
  • Advait Saravade
    Advait Saravade over 8 years
    Is there any way to share some of my code with you?
  • bonnyz
    bonnyz over 8 years
    @AdvaitS You should post your code together with an educated explanation of what you've done and what you need to do as a new question. I think this is the best way to find help.
  • Vanilla Boy
    Vanilla Boy over 8 years
    @bonnyz but your solution is incomplete... :)
  • Advait Saravade
    Advait Saravade over 8 years
    I cannot possibly do that. The question would be a duplicate. Your solution, although providing guidance, is still incomplete. I would love to award the bounty for your answer if you provided some code.
  • Advait Saravade
    Advait Saravade over 8 years
    I don't think Java Sound was ported to Android back then. So the MixingAudioInputStream constructor won't work.
  • Advait Saravade
    Advait Saravade over 8 years
    Yes I've tried this. The sound API hasn't been ported over to Android. There's even a discussion about it here - groups.google.com/forum/m/#!topic/android-developers/…
  • Sagar Hatekar
    Sagar Hatekar over 7 years
    @Advait S People took time out to post their solutions here. The least you could do is try them out and post your findings or at least post why the solutions don't work. I know its hard to part with your bounty but no one is going to do your homework for you here :)