Advertisement
Guest User

Untitled

a guest
May 10th, 2015
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //The function create the file with the transcription of the audio
  2. function writeTranscription(text)
  3. {
  4.     var textFile = null;
  5.     var data = new Blob([text], {type: 'text/plain'});
  6.    
  7.     if(textFile !== null)
  8.     {
  9.         window.URL.revokeObjectURL(textFile);
  10.     }
  11.  
  12.     textFile = window.URL.createObjectURL(data);
  13.  
  14.     return textFile;
  15. }
  16.  
  17. var ASR = new webkitSpeechRecognition();
  18. var transcription = "";
  19.  
  20. var recognizing = false;
  21. var autoRestart = false;
  22. var lastStartedAt = 0;
  23.  
  24. ASR.lang='it-IT';
  25. ASR.continuous = false;
  26. ASR.maxAlternatives=5;
  27.  
  28. var media = new Audio("audio_files/ballaro_parte2.mp3");
  29. media.addEventListener('ended', function()
  30.     {
  31.         autoRestart = false;
  32.         ASR.stop();
  33.     }, true);
  34. var isPlaying = false;
  35.  
  36. function onStartASR(event)
  37. {
  38.     ASR.start();
  39.     console.log('onStartASR Pressed to start recognition');
  40. }
  41.  
  42. function onEndASR(event)
  43. {
  44.     autoRestart = false;
  45.     ASR.stop();
  46. }
  47.  
  48. ASR.onstart = function()
  49. {
  50.     console.log('onstart'); //debug
  51.     lastStartedAt = new Date().getTime();
  52.     autoRestart = true;
  53.     recognizing = true;
  54.     setTimeout(function() {media.play();}, 500);
  55. };
  56.  
  57. ASR.onend = function()
  58. {
  59.     console.log('onend'); //debug
  60.     media.pause();
  61.     recognizing = false;
  62.     if(autoRestart)
  63.     {
  64.         setTimeout(function() {ASR.start();}, 1000);
  65.  
  66.         // var timeSinceLastStart = new Date().getTime() - lastStartedAt;
  67.         // if(timeSinceLastStart < 1000)
  68.         // {
  69.         //  setTimeout(function() {ASR.start();}, 1000 - timeSinceLastStart);
  70.         // }
  71.         // else
  72.         // {
  73.         //  recognizing = false;
  74.         //  ASR.start();
  75.         // }
  76.     } else
  77.     {
  78.         if(transcription)
  79.         {
  80.             var link = document.getElementById('link');
  81.             link.href = writeTranscription(transcription);
  82.             link.style.display = 'block';
  83.         }
  84.     }
  85. };
  86.  
  87. ASR.onresult = function(event)
  88. {
  89.     console.log('onresult'); //debug
  90.     console.log(event);
  91.     confidence = event.results[0][0].confidence;
  92.     transcript = event.results[0][0].transcript;
  93.     console.log('trans:' + transcript + ' conf:' + confidence);
  94.     if(event.results[0].isFinal)
  95.     {
  96.         $("#ASRDiv").html(transcript);
  97.         transcription += transcript + " ";
  98.     }
  99. };
  100.  
  101. ASR.onerror = function(event)
  102. {
  103.     console.log('onerror: ' + event.error); //debug
  104.     switch(event.error)
  105.     {
  106.         case 'network':
  107.             recognizing = true;
  108.             break;
  109.         case 'not allowed':
  110.         case 'service-not-allowed':
  111.             autoRestart = false;
  112.             recognizing = false;
  113.             break;
  114.         case 'no-speech':
  115.             recognition = true;
  116.             break;
  117.         case 'language-not-supported':
  118.             window.alert('Language not supported!');
  119.             autoRestart = false;
  120.             recognizing = false;
  121.             break;
  122.         case 'bad-grammar':
  123.             window.alert('Bad grammar!');
  124.             autoRestart = false;
  125.             recognizing = false;
  126.             break;
  127.     }
  128. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement