Search code examples
javascriptaudio-streamingplaybackmicrophone

Realtime microphone input mixing with music playback


I am trying to build an Internet Radio platform and I have battled a lot with the problem that is mentioned on the title.

To explain myself further, what I am trying to achieve is, 1) while recording input from the broadcaster's microphone, to mix it with audio from music playback and 2) at the same time be able to lower or raise the volume of the music playback (also realtime through the UI) so that the broadcaster's voice can blend with the music.

This is to imitate a usual radio broadcaster's behavior where music volume lowers when the person wants to speak and raises back again when he finishes talking! The 2nd feature definitely comes after the 1st but I guess mentioning it helps explain both.

To conclude, I have already managed to write code that receives and reproduces microphone input (though it doesn't work perfectly!). At this point I need to know if there is code or libraries that can help me do exactly what I am trying to do. All this is done in hope I won't need to use IceCast etc.

Below is my code for getting microphone input:

// getting microphone input and sending it to our server

var recordedChunks = [];
var mediaRecorder = null;
let slice = 100;                        // how frequently we capture sound
const slices = 20;                      // 20 * => after 2 sec
let sendfreq = slice * slices;          // how frequently we send it

/* get microphone button handle */
var microphoneButton = document.getElementById('console-toggle-microphone');
microphoneButton.setAttribute('on', 'no');

/* initialise mic streaming capability */
navigator.mediaDevices.getUserMedia({ audio: true, video: false }).then(stream => {
    _stream = stream;
})
.catch(function(err) {
    show_error('Error: Microphone access has been denied probably!', err);
});

function toggle_mic() {
    if (microphoneButton.getAttribute('on') == 'yes')
    {
        clearInterval();
        microphoneButton.setAttribute('on', 'no');
        microphoneButton.innerHTML = 'start mic';
    }
    else if (microphoneButton.getAttribute('on') == 'no') 
    {
        microphoneButton.setAttribute('on', 'yes');
        microphoneButton.innerHTML = 'stop mic';

        function record_and_send() {
            const recorder = new MediaRecorder(_stream);
            const chunks = [];
            recorder.ondataavailable = e => chunks.push(e.data);
            recorder.onstop = e => socket.emit('console-mic-chunks', chunks);
            setTimeout(()=> recorder.stop(), sendfreq); // we'll have a 5s media file
            recorder.start();
        }
        // generate a new file every 5s
        setInterval(record_and_send, sendfreq); 
    }
}

Thanks alot!


Solution

  • In case when your audio track from the microphone doesn't need to be synchronized with audio playback (as for me I do not see any reason for this), then you can just play two separate audio instances and change the volume of the one underway (audio playback in your case).

    Shortly speaking, you don't have to mix audio tracks and do complex stuff to solve this task.

    Draft example:

    <input type="range" id="myRange" value="20" oninput="changeVol(this.value)" onchange="changeVol(this.value)">
    
    // Audio playback
    const audioPlayback  = new Audio();
    const audioPlaybackSrc  = document.createElement("source");
    audioPlaybackSrc.type = "audio/mpeg";
    audioPlaybackSrc.src  = "path/to/audio.mp3";
    audioPlayback.appendChild(audioPlaybackSrc);
    audioPlayback.play();
    
    // Change volume for audio playback on the fly
    function changeVol(newVolumeValue) {
       audioPlayback.volume = newVolumeValue;
    }
    
    // Dealing with the microphone
    navigator.mediaDevices.getUserMedia({
          audio: true
       })
       .then(stream => {
          // Start recording the audio
          const mediaRecorder = new MediaRecorder(stream);
          mediaRecorder.start();
    
          // While recording, store the audio data chunks
          const audioChunks = [];
          mediaRecorder.addEventListener("dataavailable", event => {
             audioChunks.push(event.data);
          });
    
          // Play the audio after stop
          mediaRecorder.addEventListener("stop", () => {
             const audioBlob = new Blob(audioChunks);
             const audioUrl = URL.createObjectURL(audioBlob);
             const audio = new Audio(audioUrl);
             audio.play();
          });
    
          // Stop recording the audio
          setTimeout(() => {
             mediaRecorder.stop();
          }, 3000);
       });
    

    Play multiple audio files simultaneously

    Change audio volume with JS

    How to record and play audio in JavaScript