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:
- Decode the first file . mp3
- store decoded audio in an N Vector positions
- Decode the next file . mp3
- Concatenate the new decoded vector at the end of the other vector
- do this and loop until finished reading all the files of your . txt
- 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!
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 ...
– ederwander
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.
– Luiz Vieira
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.
– Luiz Vieira