Created
January 7, 2016 17:33
-
-
Save MagnusThor/00f98d53843aeb635672 to your computer and use it in GitHub Desktop.
Create transcripts using webkitSpeechRecognition by attaching a mediaStream audioTrack
This file contains hidden or 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
var AudioTrackSpeechLog = (function () { | |
"use strict"; | |
var ctor = function (track, completed, interim, lang) { | |
var self = this; | |
this.recognizing = false; | |
this.finalTranscript = ""; | |
if (('webkitSpeechRecognition' in window)) { | |
this.recognition = new webkitSpeechRecognition(); | |
this.recognition.continuous = true; | |
this.recognition.interimResults = true; | |
this.recognition.lang = lang || navigator.language; | |
this.recognition.onstart = function() { | |
console.log("staring"); | |
}; | |
this.recognition.onend = function () { | |
console.log("ending"); | |
// self.start(); | |
}; | |
this.recognition.onresult = function (event) { | |
var interimTranscript = ''; | |
for (var i = event.resultIndex; i < event.results.length; ++i) { | |
if (event.results[i].isFinal) { | |
self.finalTranscript += event.results[i][0].transcript; | |
completed(event.results[i][0].transcript, | |
event.results[i][0].confidence, | |
self.trackId, new Date()); | |
} else { | |
interimTranscript += event.results[i][0].transcript; | |
interim(interimTranscript,self.trackId, new Date()); | |
} | |
} | |
}; | |
this.trackId = track.id; | |
} | |
}; | |
ctor.prototype.start = function () { | |
this.recognition.start(); | |
this.recognizing = true; | |
return this; | |
}; | |
ctor.prototype.stop = function () { | |
this.recognition.stop(); | |
this.recognizing = false; | |
return this; | |
}; | |
return ctor; | |
})(); | |
// track is the mediaStram track | |
//i.e track = stream.getAudioTracks()[0] | |
var audioLogger = new AudioTrackSpeechLog(track, | |
function (completeText, confidence, trackId, ts) { | |
// complete text is the final Result | |
}, function (text, trackId, ts) { | |
// text is interim result | |
}, "sv-SE"); // last arg optinal | |
audioLogger.start(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment