Join multiple mp3 files into a single mp3 file

Asked

Viewed 395 times

0

Hello! I would like to create a program (in C language or other capable) that receives as input a text and return me as output a song.

For this I would need a text file (.txt) where I would need to read it and several audio files (.mp3) each with a musical note. Let’s say the letter A represents the note C.mp3 the letter B presents the note A.mp3 the sequence of the letters A and C the note D.mp3.

Reading the file to me would not be a problem my difficulty is in joining these files . mp3 according to the sequence of the text and turn them into a single file . mp3

I just don’t know where to start. I’ve talked to some teachers but no one knows how to do it. So I ask you:

How do I join multiple mp3 files into a single mp3 file? You can show me some way (sites, books, courses, etc)?

  • 1

    I gave you an answer with the path of the stones, it is not simple to use pre-recorded samples for what you want, decoding and encoding mp3 files is a bitch, if your project accepts samples . wav would be a facilitator, particularly I would go another way, I find it more interesting to synthesize all the notes according to the entries of his . txt, vc would enter the notes(frequencies) and the duration of each, vc could synthesize the inputs using sine or cosine ...

  • Your question really is wide open. You already have an excellent response from colleague @ederwander (although it’s not in one of the languages you requested), but I still suggest you edit your question to make it more focused. You can start by defining the language (C and C++ are distinct - after all you want in C or C++?), and you can also let go of the whole question of reading the notes in the file. Your problem seems to be just like joining two audio files in mp3 format.

  • Still, if you want to "compose" music using musical notes, there are other possible ways. You don’t need to read a compressed audio file with each of the notes and join them, you can simply issue such notes. Depending on the library or OS you use, there are functions like the Beep which allows you to define the frequency and duration one-note. Or, having the mp3/wave, just play them in sequence, without joining.

1 answer

3


It is possible to do it in any language, but it is not so simple :-(, you will need to decode the audio and store it neatly within a vector or matrix, the logical process is:

  1. Decode the first file . mp3
  2. store decoded audio in an N Vector positions
  3. Decode the next file . mp3
  4. Concatenate the new decoded vector at the end of the other vector
  5. do this and loop until finished reading all the files of your . txt
  6. Encode vector for . mp3

Decode. mp3 is very complex, if you insist on using C I recommend that you link your project with a specific lib capable of doing Decode, I’ve used enough o libmad to do similar things in C, it may be easier for you to do the same process using . wav because to decode and encode is simpler ...

In Python2.7 it can be relatively simple to decode . mp3 using the Pymedia

import pymedia.audio.acodec as acodec
import pymedia.muxer as muxer

name='Sia - Chandelier.mp3';


name1= str.split(name,'.')


if name1[ -1 ].lower()  == 'mp3':


    dm= muxer.Demuxer( name1[ -1 ].lower() )
    f=open(name, 'rb' )
    dec= None
    s= " "
    sinal=[]
    while len( s ):
        s= f.read( 4096 )
        if len( s ):
            frames= dm.parse( s )
            for fr in frames:
                if dec== None:
                    dec= acodec.Decoder( dm.streams[ 0 ] )
                r= dec.decode( fr[ 1 ] )
                if r and r.data:
                    din = np.fromstring(r.data, dtype=np.int16)
                    sinal.append(din)

The vector/list sinal will contain the audio Sia - Chandelier.mp3 fully decoded, all you need to do now is concatenate each read file at the end of the vector sinal, finally vc must encode the signal vector for file . mp3, vc need something like this to effect the Encode:

# Open muxer and encoder

          if enc== None:
            params= { 'id': acodec.getCodecID(type),
                      'bitrate': bitrate,
                      'sample_rate': r.sample_rate,
                      'channels': r.channels }
            print 'Encoder params:', params
            mx= muxer.Muxer( type )
            stId= mx.addStream( muxer.CODEC_TYPE_AUDIO, params )
            enc= acodec.Encoder( params )
            fw= open(fOutput, 'wb')
            ss= mx.start()
            fw.write(ss)

          enc_frames= enc.encode( pack("%dh"%len(sinal), *(sinal)) )
          if enc_frames:
            for efr in enc_frames:
              ss= mx.write( stId, efr )
              if ss:
                fw.write(ss)

If it was totally in . wav this could be done in a few lines using python, hopefully this will give you a north!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.