Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //The function create the file with the transcription of the audio
- function writeTranscription(text)
- {
- var textFile = null;
- var data = new Blob([text], {type: 'text/plain'});
- if(textFile !== null)
- {
- window.URL.revokeObjectURL(textFile);
- }
- textFile = window.URL.createObjectURL(data);
- return textFile;
- }
- var ASR = new webkitSpeechRecognition();
- var transcription = "";
- var recognizing = false;
- var autoRestart = false;
- var lastStartedAt = 0;
- ASR.lang='it-IT';
- ASR.continuous = false;
- ASR.maxAlternatives=5;
- var media = new Audio("audio_files/ballaro_parte2.mp3");
- media.addEventListener('ended', function()
- {
- autoRestart = false;
- ASR.stop();
- }, true);
- var isPlaying = false;
- function onStartASR(event)
- {
- ASR.start();
- console.log('onStartASR Pressed to start recognition');
- }
- function onEndASR(event)
- {
- autoRestart = false;
- ASR.stop();
- }
- ASR.onstart = function()
- {
- console.log('onstart'); //debug
- lastStartedAt = new Date().getTime();
- autoRestart = true;
- recognizing = true;
- setTimeout(function() {media.play();}, 500);
- };
- ASR.onend = function()
- {
- console.log('onend'); //debug
- media.pause();
- recognizing = false;
- if(autoRestart)
- {
- setTimeout(function() {ASR.start();}, 1000);
- // var timeSinceLastStart = new Date().getTime() - lastStartedAt;
- // if(timeSinceLastStart < 1000)
- // {
- // setTimeout(function() {ASR.start();}, 1000 - timeSinceLastStart);
- // }
- // else
- // {
- // recognizing = false;
- // ASR.start();
- // }
- } else
- {
- if(transcription)
- {
- var link = document.getElementById('link');
- link.href = writeTranscription(transcription);
- link.style.display = 'block';
- }
- }
- };
- ASR.onresult = function(event)
- {
- console.log('onresult'); //debug
- console.log(event);
- confidence = event.results[0][0].confidence;
- transcript = event.results[0][0].transcript;
- console.log('trans:' + transcript + ' conf:' + confidence);
- if(event.results[0].isFinal)
- {
- $("#ASRDiv").html(transcript);
- transcription += transcript + " ";
- }
- };
- ASR.onerror = function(event)
- {
- console.log('onerror: ' + event.error); //debug
- switch(event.error)
- {
- case 'network':
- recognizing = true;
- break;
- case 'not allowed':
- case 'service-not-allowed':
- autoRestart = false;
- recognizing = false;
- break;
- case 'no-speech':
- recognition = true;
- break;
- case 'language-not-supported':
- window.alert('Language not supported!');
- autoRestart = false;
- recognizing = false;
- break;
- case 'bad-grammar':
- window.alert('Bad grammar!');
- autoRestart = false;
- recognizing = false;
- break;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement