How to join audio files with Node js

Asked

Viewed 314 times

1

I need to concatenate 2 audio files in wav format with Node. I have 5-second "audio 1" and "5-second audio 2" I need to generate the file "audio 3" 5 seconds long, which contains the contents of audio 1 and audio 2 playing simultaneously.

How can I do that?

1 answer

0

You don’t want to concatenate you want to mix ...

The simplest way I know is to use the web-audio-api

Install with:

npm install web-audio-api

Decode both files and add the values of the two vectors:

    var AudioContext = require('web-audio-api').AudioContext
    context = new AudioContext


    var samplerate;
    var pcmdata1 = [] ;
    var pcmdata2 = [] ;
    var mix = [];

    var soundfile1 = "sounds/sound1.wav"
    var soundfile2 = "sounds/sound2.wav"



    decodeSoundFile1(soundfile1);
    decodeSoundFile2(soundfile2);
    mixer();
    playSound(mix)




    //decodificando os dois arquivos

    function decodeSoundFile1(soundfile){
      console.log("decoding file ", soundfile, " ..... ")
      fs.readFile(soundfile, function(err, buf) {
        if (err) throw err
        context.decodeAudioData(buf, function(audioBuffer) {
          console.log(audioBuffer.numberOfChannels, audioBuffer.length, audioBuffer.sampleRate, audioBuffer.duration);
          pcmdata1 = (audioBuffer.getChannelData(0)) ;
          samplerate=audioBuffer.sampleRate;
        }, function(err) { throw err })
      })
    }


   function decodeSoundFile2(soundfile){
      console.log("decoding file ", soundfile, " ..... ")
      fs.readFile(soundfile, function(err, buf) {
        if (err) throw err
        context.decodeAudioData(buf, function(audioBuffer) {
          console.log(audioBuffer.numberOfChannels, audioBuffer.length, audioBuffer.sampleRate, audioBuffer.duration);
          pcmdata2 = (audioBuffer.getChannelData(0)) ;
        }, function(err) { throw err })
      })
    }



  //mixando os arquivos


  function mixer() {
      for (var i = 0; i < pcmdata1.length; i++) 
           mix[i] = pcmdata1[i] + pcmdata2[i]

}

  //tocando o áudio mixado no browser

  function playSound(arr) {
    var buf = new Float32Array(arr.length)
    for (var i = 0; i < arr.length; i++) buf[i] = arr[i]
    var buffer = context.createBuffer(1, buf.length, samplerate)
    buffer.copyToChannel(buf, 0)
    var source = context.createBufferSource();
    source.buffer = buffer;
    source.connect(context.destination);
    source.start(0);
}

of course you can replace the function that plays the audio by another that encodes the audio in a wav or mp3 file, I quickly made the code and did not treat many things that can be important, both audio files have to be exactly the same size, you can treat in code if you want later, both decoded files also have to possess the same sample rate ...

I’ll explain here as this is done in java, the logic and the steps are the same for any language!

Browser other questions tagged

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