Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Noitidart
Created May 31, 2017 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noitidart/8d57dcd7194fb95a96627a73099b38e1 to your computer and use it in GitHub Desktop.
Save Noitidart/8d57dcd7194fb95a96627a73099b38e1 to your computer and use it in GitHub Desktop.
async authorize() {
const { dispatch } = this.props;
if (!this.stream) {
try {
this.stream = await navigator.mediaDevices.getUserMedia({ audio:true, video:false });
} catch(ex) {
// TODO: add detailed possible explanations for the error returned
// if no mic plugged in
// if mic in use by another application
return dispatch(errorListen(`Failed to get permission to use your microphone. Error returned was: "${ex.message}"`));
}
this.recorder = new MediaRecorder(this.stream);
this.recorder.addEventListener('stop', this.handleRecordingStop, false);
this.recorder.addEventListener('dataavailable', this.handleRecordingData, false);
}
console.log('ok authorized');
this.start();
}
startRecording() {
console.log('will start watson who will start recording');
this.chunks = [];
this.startSTT();
}
stopRecording() {
console.log('will now stop');
this.recorder.stop();
}
is_stt_listening = false
handleRecordingData = e => {
// console.log('data available, e:', e);
const chunk = e.data;
this.chunks.push(chunk);
this.sendChunks();
}
handleRecordingStop = e => {
console.log('stopped, e:', e);
const { chunks } = this;
this.chunks = null;
console.log('chunks:', chunks);
}
///// watson stuff
websocket = null
content_type = 'audio/ogg'
sendChunks = () => {
if (this.is_stt_listening && this.chunks.length) {
const chunks = this.chunks;
const blob = new Blob([chunks], { type:this.content_type });
if (blob.size >= 100) {
// if not 100 i get this error in onmessage:
// watson websocket got message, e: message { target: WebSocket, isTrusted: true, data: "{ "error": "Stream was 13 bytes but needs to be at least 100 bytes." }", origin: "wss://stream.watsonplatform.net", lastEventId: "", ports: Object, currentTarget: WebSocket, eventPhase: 2, bubbles: false, cancelable: false, defaultPrevented: false }
this.chunks = []; // create a new array for chunks, as i sent the current ones
console.log('blob:', blob.size, blob);
console.log('will now send blob to watson');
this.is_stt_listening = false;
this.websocket.send(blob);
console.log('sending stop message to watson');
this.websocket.send(JSON.stringify({ 'action':'stop' }));
}
}
}
startSTT = async () => {
this.is_stt_listening = false;
console.log('will now start watson')
const token = await getToken();
this.websocket = new WebSocket(`wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?watson-token=${token}`);
this.websocket.onopen = this.handleSTTOpen;
this.websocket.onclose = this.handleSTTClose;
this.websocket.onmessage = this.handleSTTMessage;
this.websocket.onerror = this.handleSTTError;
console.log('started watson');
this.recorder.start(1000);
console.log('started recording');
}
handleSTTOpen = e => {
console.log('watson websocket opened, e:', e);
// watson websocket opened, e: open { target: WebSocket, isTrusted: true, currentTarget: WebSocket, eventPhase: 2, bubbles: false, cancelable: false, defaultPrevented: false, composed: false, timeStamp: 2447.905853517878, cancelBubble: false, originalTarget: WebSocket }
this.websocket.send(JSON.stringify({
action: 'start',
'content-type': this.content_type
}));
}
handleSTTClose = e => {
console.log('watson websocket closed, e:', e);
// watson websocket closed, e: close { target: WebSocket, isTrusted: true, wasClean: true, code: 1011, reason: "see the previous message for the error details.", currentTarget: WebSocket, eventPhase: 2, bubbles: false, cancelable: false, defaultPrevented: false, composed: false }
this.websocket = null;
}
handleSTTMessage = e => {
console.log('watson websocket got message, e:', e);
const { dispatch } = this.props;
const data = JSON.parse(e.data);
if (data.error) {
this.stop();
dispatch(errorListen(`Server failed during speech to text process. Error returned was: "${data.error}"`));
} else if (data.state && data.state === 'listening') {
this.is_stt_listening = true;
this.sendChunks();
}
// watson websocket got message, e: message { target: WebSocket, isTrusted: true, data: "{"state": "listening" }", origin: "wss://stream.watsonplatform.net", lastEventId: "", ports: Object, currentTarget: WebSocket, eventPhase: 2, bubbles: false, cancelable: false, defaultPrevented: false }
// watson websocket got message, e: message { target: WebSocket, isTrusted: true, data: "{"error": "Session timed out." }", origin: "wss://stream.watsonplatform.net", lastEventId: "", ports: Object, currentTarget: WebSocket, eventPhase: 2, bubbles: false, cancelable: false, defaultPrevented: false }
}
handleSTTError = e => {
console.warn('watson websocket got error, e:', e);
}
stopSTT = () => {
console.log('will now stop watson');
this.websocket.close();
console.log('stopped watson');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment