Search code examples
javascriptaudiohtml5-audio

Having An Audio Recorded In An Specified BPM


I am using the recorder.js for recording couple of audio elements(output sound) that are playing in the page. I wonder how I would be able to set a number, as the BPM(Beats Per Minute) of the recorded audio.

//connectRecorder Function
function connectRecorder() {
    if (audioSources == '') {
        audioContext = new AudioContext();
        gainNode = audioContext.createGain();
        for (var i = 0; i < audiosClass.length; i++) {
            audioSources.push(audioContext.createMediaElementSource(audiosClass[i]));
            audioSources[i].connect(gainNode);
        }
        gainNode.connect(audioContext.destination); 
    }
}

//recordButton Click Handler
function recordButtonClickHandler() {
    if (recordButton.value == 'Start Recording') {
        recorder = new Recorder(gainNode);
        recorder.record();
        recordButton.value = 'Stop Recording';
    } else {
        recorder.stop();
        recorder.exportWAV(function (blob) {
            hiddenRecordExportLink.href = URL.createObjectURL(blob);
            if (recordSelect.value == 'wav') {
                hiddenRecordExportLink.download = 'record.wav';
            } else {
                hiddenRecordExportLink.download = 'record.mp3';
            }
            hiddenRecordExportLink.click();
        });
        recordButton.value = 'Start Recording';
    }
}

Solution

  • You can't set the BPM, specifically, but you can set the playback rate. If you know your source track is 120 BPM, and you want to bring it up to 130 BPM, you would set the playback rate like this:

    audio.playbackRate = 130 / 120; // 1.083333