UNPKG

unserver-unify

Version:

307 lines (289 loc) 8.72 kB
angular.module('bamboo.course').controller('AudioExpChapterCtrl', function($scope, $stateParams, ApiService, CommonService, $timeout) { this.cid = $stateParams.cid; console.log("--- EWordChapterCtrl"); var self = this; var resUrl = this.resUrl = ApiService.SHOST + '/lcourse/' + this.cid + '/resources/'; this.index = 0; this.currentPPT; this.slidePages = []; this.init = function(chapter) { self.chapter = chapter; resUrl = this.resUrl = ApiService.SHOST + '/lcourse/' + self.chapter.cid + '/resources/'; self.slidePages.push({ ntype: 'title', }); console.log(self.chapter); if(self.chapter.convertFlag&&self.chapter.options&&self.chapter.options.country&&self.chapter.options.country.val) { self.selectLang = self.chapter.options.country; } else if(self.chapter.convertFlag&&self.chapter.options) { self.selectLang = self.chapter.options.language; } console.log(self.selectLang); angular.forEach(self.chapter.ppts, function(ppt, index) { ppt.ntype = "word"; ppt.index = index; self.slidePages.push(ppt); }); self.show(0); } this.code; var myAudio; this.playAudio = function(file) { if (!file) { return; } var filename = resUrl + file; try { if(!myAudio){ myAudio = new Audio(filename); }else{ myAudio.pause(); myAudio.src=filename; myAudio.play(); } } catch (e) {} } this.nextPage = function() { self.index++; if (self.index >= self.slidePages.length) { self.index = 0; } self.show(self.index); } this.lastPage = function() { self.index--; if (self.index < 0) { self.index = self.slidePages.length - 1; } self.show(self.index); } this.show = function(index) { self.index = index; self.currentPPT = self.slidePages[index]; console.log(self.currentPPT); self.clear(); self.playAudio(self.currentPPT.audio); } //.......... Audio Record ................. this.final_transcript=""; this.interim_transcript=""; this.final_transcript_desc = []; var start_timestamp; this.recognizing = false; var recognition = new webkitSpeechRecognition(); recognition.continuous = true; recognition.interimResults = true; recognition.onstart = function() { self.recognizing = true; $scope.$apply(); // CommonService.showInfo('Started'); console.log("started"); // start_img.src = '/intl/en/chrome/assets/common/images/content/mic-animate.gif'; }; recognition.onerror = function(event) { if (event.error == 'no-speech') { CommonService.showError('Speech function is not supported by your browser!') } if (event.error == 'audio-capture') { CommonService.showError('Can not get your mic!') } if (event.error == 'not-allowed') { if (event.timeStamp - start_timestamp < 100) { CommonService.showError('Mic is blocked'); } else { CommonService.showError('Mic is Denied'); } } }; recognition.onend = function() { self.recognizing = false; $scope.$apply(); console.log(); if (!self.final_transcript&&!self.forceStop) { CommonService.showInfo('Nothing decoded'); return; } // CommonService.showInfo('Stoped'); }; recognition.onresult = function(event) { // var interim_transcript = ''; // console.log(event.results); if (typeof(event.results) == 'undefined') { recognition.onend = null; recognition.stop(); CommonService.showError('Sth wrong!'); return; } for (var i = event.resultIndex; i < event.results.length; ++i) { if (event.results[i].isFinal) { console.log(event.results[i][0]); if(!self.currentPPT.desc) { self.final_transcript += event.results[i][0].transcript +" "+ parseInt(event.results[i][0].confidence*100)+'% ' } else { self.final_transcript_desc.push({txt:event.results[i][0].transcript,accuracy:parseInt(event.results[i][0].confidence*100)}); } } else { self.interim_transcript = event.results[i][0].transcript; } } $timeout(function() { console.log(self.final_transcript); console.log(self.currentPPT.word); console.log(self.interim_transcript); if(!self.currentPPT.desc) { if(self.interim_transcript.toLowerCase()==self.currentPPT.word.toLowerCase()) { self.final_result = "Right"; } else { self.final_result = "Wrong"; } if(self.interim_transcript){ self.forceStop = true; recognition.stop(); } } else { calResult(); } }, 10); /* final_transcript = capitalize(final_transcript); if (final_transcript || interim_transcript) { showButtons('inline-block'); }*/ }; this.start = function(event) { if(self.finishDesc){ self.clear(); } if (!self.selectLang) { CommonService.showError('No Language selected!') return; } self.final_transcript = ''; self.final_result = false; recognition.lang = self.selectLang.val; recognition.start(); start_timestamp = new Date(); console.log("Started"); } this.stop=function(){ if (self.recognizing) { self.forceStop = false; recognition.stop(); return; } } this.clear = function() { self.stop(); self.final_result = false; self.final_transcript=""; self.interim_transcript=""; self.final_transcript_desc=[]; self.forceStop = true; self.finishDesc = false; } function calResult(){ var txt = ""; var acc = 0; angular.forEach(self.final_transcript_desc,function(val){ txt += val.txt +" "; acc += val.accuracy; }); console.log(self.final_transcript_desc); self.final_transcript = txt; self.accuracy = ((acc/(self.final_transcript_desc.length*100))*100)+"%"; } this.finish = function() { calResult(); console.log(self.final_transcript_desc); self.finishDesc = true; self.final_transcript_desc=[]; } this.mode; function shootAudio(callback) { var audioObj = { audio: true, // video: true, } if (myAudio) { myAudio.pause(); self.audioStatus='idle'; } var errBack = function(error) { return callback(false, error); }; navigator.getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia); if (navigator.getUserMedia) { // Standard navigator.getUserMedia(audioObj, function(stream) { console.log("audio detected"); callback(stream); }, errBack); } else { console.log("--- not supported---"); return callback(false, "Not Supported Browser"); } } var _stream; this.recordAudio = function() { self.mode = "recording"; shootAudio(function(stream) { _stream = stream; $timeout(function() { window.audioVideoRecorder = window.RecordRTC(stream, { type: 'audio' }); window.audioVideoRecorder.startRecording(); self.currentPPT.mode = "recording"; }, 200) }); } this.playbacksrc; this.stopRecording = function() { self.currentPPT.mode = 'idle'; self.mode = null; window.audioVideoRecorder.stopRecording(function(url) { // console.log(_stream); // _stream.stop(); $timeout(function() { self.currentPPT.playbacksrc = url; }, 500); }); } this.playUserAudio = function() { if (!self.currentPPT.playbacksrc) { return; } try { if(!myAudio){ myAudio = new Audio(self.currentPPT.playbacksrc); }else{ myAudio.pause(); myAudio.src=self.currentPPT.playbacksrc; } myAudio.play(); console.log('dsfsdfsdf'); self.audioStatus='playrec'; } catch (e) {} } this.toggoleRec=function(){ if(self.audioStatus!='playrec'){ self.playUserAudio(); }else{ self.stopPlay(); } } this.stopPlay=function(){ try { if (myAudio) { myAudio.pause(); self.audioStatus="idle"; } } catch (e) {} } $scope.$on("$destroy", function() { if (myAudio) { myAudio.pause(); } }); });