unserver-unify
Version:
94 lines (79 loc) • 2.86 kB
JavaScript
var audio_context,
recorder,
volume,
volumeLevel = 0,
currentEditedSoundIndex;
var _stream;
function startUserMedia(stream,callback) {
//console.log(stream);
_stream=stream;
var input = audio_context.createMediaStreamSource(stream);
console.log('Media stream created.');
volume = audio_context.createGain();
volume.gain.value = volumeLevel;
input.connect(volume);
volume.connect(audio_context.destination);
console.log('Input connected to audio context destination.');
recorder = new Recorder(input);
console.log('Recorder initialised.');
callback();
}
function startRecording(button) {
var audioRef = document.getElementById('audiosStatus');
audioRef.innerHTML="Waiting for Permission ....";
init(function(){
recorder && recorder.record();
audioRef.innerHTML="Recording ....";
button.disabled = true;
button.nextElementSibling.disabled = false;
console.log('Recording...');
});
}
function stopRecording(button) {
recorder && recorder.stop();
_stream.getTracks()[0].stop();
button.disabled = true;
button.previousElementSibling.disabled = false;
console.log('Stopped recording.');
var audioRef = document.getElementById('audiosStatus');
audioRef.innerHTML="Converting ....";
// create WAV download link using audio data blob
createDownloadLink();
recorder.clear();
}
function createDownloadLink() {
currentEditedSoundIndex = -1;
recorder && recorder.exportWAV(handleWAV.bind(this));
}
function handleWAV(blob) {
// var tableRef = document.getElementById('recordingslist');
/*var audioRef = document.getElementById('audioslist');
while (audioRef.hasChildNodes()) {
audioRef.removeChild(audioRef.lastChild);
}
var url = URL.createObjectURL(blob);
var audioElement = document.createElement('audio');
audioElement.controls = true;
audioElement.src = url;
var li = document.createElement('div');
li.appendChild(audioElement);
audioRef.appendChild(li);*/
}
function init(callback) {
try {
// webkit shim
window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
window.URL = window.URL || window.webkitURL || window.mozURL;
audio_context = new AudioContext();
console.log('Audio context set up.');
console.log('navigator.getUserMedia ' + (navigator.getUserMedia ? 'available.' : 'not present!'));
} catch (e) {
console.warn('No web audio support in this browser!');
}
navigator.getUserMedia({audio: true}, function(stream){
startUserMedia(stream,callback);
}, function(e) {
console.warn('No live audio input: ' + e);
});
};