Working but broken communication with nodejs server

This commit is contained in:
Rekai Musuka
2018-09-04 11:18:39 +01:00
parent 7922a55e14
commit 4cc4fb91bb
4 changed files with 234 additions and 3 deletions

View File

@@ -0,0 +1,77 @@
import io from 'socket.io-client';
/**
* The Class Responsible for Talking with Polyglot-Node
* @param {string} url - URL of NodeJS Server.
*/
export default class NodeSocket {
constructor(url) {
this.io = io(url);
this.listen();
}
/**
* Create socket.io event listeners.
*/
listen() {
this.io.on('disconnect', (err) => {
console.error("Disconnected from Server.");
})
this.io.on('connect', (data) => {
this.io.emit("join", "Hi There NodeJS!");
console.log("Connected to NodeJS Server");
});
}
/**
* This Class Grabs the Audio from the User.
*
* DEPRECAION WARNING. This method makes vital use of createScriptProcessor which will be scrapped at some point
* in the future. TODO: Switch from createScriptProcessor to AudioWorklet (No documentation as of 2018-08-30)
* @deprecated
* @param {MediaStream} stream - Global MediaStream
* @async
*/
async grabAudio(stream) {
this.io.emit('startRecognition', "en-US", "fr");
AudioContext = window.AudioContext || window.webkitAudioContext;
const ctx = new AudioContext();
const processor = ctx.createScriptProcessor(2048, 1, 1);
processor.connect(ctx.destination);
processor.onaudioprocess = e => this.handleBuffer(e);
// const stream = await navigator.mediaDevices.getUserMedia({ audio: true, video: false });
ctx.createMediaStreamSource(stream).connect(processor);
ctx.resume();
}
/**
* Takes the Float32Array Array from the ScriptProcessor Event
* and converts it to Int16 for Google Speech to Text use.
* @param {AudioProcessingEvent} e Chunk of Audio w/ Audio F32Array
*/
handleBuffer(e) {
let l = e.inputBuffer.getChannelData(0);
this.io.emit('binary', convertF32ToI16(l));
/**
* 32Khz to 16Khz Audio
* Converts Float32 Buffer to Int16Buffer
* @param {Float32Array} buffer - Float32 Buffer;
* @returns {ArrayBuffer}
*/
function convertF32ToI16(buffer) {
let l = buffer.length;
let buf = new Int16Array(l/3);
while (l--) {
if (l % 3 == 0) {
buf[l / 3] = buffer[l] * 0xFFFF;
}
}
return buf.buffer;
}
}
}

View File

@@ -1,3 +1,4 @@
import NodeSocket from './nodeserver';
// Broadcast Types
const JOIN_ROOM = "JOIN_ROOM";
@@ -12,6 +13,7 @@ let remoteVideoContainer;
// Objects
let pcPeers = {}; // peer connection
let localstream;
let nodeSocket = new NodeSocket("http://localhost:1337");
window.onload = () => {
currentUser = document.getElementById("current-user").innerHTML;
@@ -33,7 +35,9 @@ document.onreadystatechange = async () => {
localstream = stream;
localVideo.srcObject = stream
nodeSocket.grabAudio(stream);
localVideo.muted = true
} catch (e) { console.error(e); }
}
};