Last active
May 9, 2018 18:36
-
-
Save alexcohn/6ca519b31af56b9324e2729bd64611d5 to your computer and use it in GitHub Desktop.
experiment around audio duration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Audio Recorder</title> | |
<style> | |
audio { | |
display: block; | |
} | |
</style> | |
</head> | |
<body> | |
<button type="button" onclick="mediaRecorder.start(1000)">Start</button> | |
<button type="button" onclick="mediaRecorder.stop()">Stop</button> | |
<script type="text/javascript"> | |
var mediaRecorder = null, | |
audio = null, | |
chunks = [], | |
startTs = 0, | |
timeout = null, | |
max_duration = 4;// in seconds. | |
function onSuccess( stream ) { | |
mediaRecorder = new MediaRecorder( stream ); | |
mediaRecorder.ondataavailable = function( event ) { | |
document.body.insertAdjacentHTML( 'beforeend', 'add chunk #' + chunks.length + ' at ' + (Date.now()-startTs) + ' ' + event.data.size + ' bytes <br/>' ); | |
chunks.push( event.data ) | |
if ( chunks.length >= max_duration && mediaRecorder.state === 'recording' ) { | |
document.body.insertAdjacentHTML( 'beforeend', 'stop ' + (Date.now()-startTs) + '<br/>' ) | |
mediaRecorder.stop() | |
} | |
} | |
mediaRecorder.onstart = function() { | |
startTs = Date.now() | |
document.body.insertAdjacentHTML( 'beforeend', '<br/>start <br/>' ) | |
setTimeout(function() { | |
if ( mediaRecorder.state === 'recording' ) { | |
document.body.insertAdjacentHTML( 'beforeend', 'timeout ' + (Date.now()-startTs) + '<br/>' ) | |
stream.getAudioTracks()[0].enabled = false | |
} | |
}, max_duration*1000) | |
}; | |
mediaRecorder.onstop = function() { | |
audio = document.createElement('audio') | |
audio_blob = new Blob(chunks, { | |
'type' : 'audio/mpeg' | |
}) | |
audio.controls = 'controls' | |
audio.autoplay = 'autoplay' | |
audio.src = URL.createObjectURL( audio_blob ) | |
setTimeout(function() { | |
if (audio.parent == null ) { | |
document.body.appendChild(audio) | |
document.body.insertAdjacentHTML( 'beforeend', ' <br/>' ) | |
} | |
}, 100); | |
startTs = Date.now() | |
timeout = setInterval(function() { | |
if (Number.isFinite(audio.duration)) { | |
document.body.insertAdjacentHTML( 'beforeend', 'duration ' + audio.duration + ' calculated in ' + (Date.now()-startTs) + ' for ' + chunks.length + ' chunks <br/>' ) | |
if ( Math.floor(audio.duration) > max_duration && audio.parent == null ) { | |
chunks = chunks.slice(0, chunks.length-1) | |
shorter_audio_blob = new Blob(chunks, { | |
'type' : 'audio/mpeg' | |
}) | |
audio.src = URL.createObjectURL( shorter_audio_blob ) | |
} | |
else { | |
clearInterval(timeout) | |
document.body.appendChild(audio); | |
} | |
} | |
}, 10) // it takes some time for duration to become valid | |
} | |
} | |
var onError = function(err) { | |
console.log('Error: ' + err) | |
} | |
navigator.mediaDevices.getUserMedia({ audio: true }).then(onSuccess, onError) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We mute the audio track when reached max_duration.
In Chrome, slices have correct duration, but it takes a long time, almost 2 sec, to calculate. In Firefox, audio.duration is available after some 40 ms, but the duration is off by almost a second. We then cut the last chunk to keep the duration between max_duration and max_duration+1.