Skip to content

Commit

Permalink
stream works
Browse files Browse the repository at this point in the history
  • Loading branch information
espidev committed Sep 14, 2019
1 parent 191488b commit 599d952
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
51 changes: 48 additions & 3 deletions src/assets/html/session.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<head>


<link rel="stylesheet", type="text/css", href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.2/animate.min.css">
<link href="https://fonts.googleapis.com/css?family=Nunito&display=swap" rel="stylesheet">
Expand Down Expand Up @@ -192,6 +191,9 @@

<script type="text/javascript">

let socket = new WebSocket("ws://localhost:3001/stream-audio");
// this is great everything is great

let bufferSize = 2048,
AudioContext,
context,
Expand All @@ -203,6 +205,20 @@
function toggleMic() {
if (mic) {
document.getElementById("mic-button").innerHTML = "<i class=\"material-icons\">mic_off</i>"

// stop recording
let track = globalStream.getTracks()[0];
track.stop();

input.disconnect(processor);
processor.disconnect(context.destination);
context.close().then(function () {
input = null;
processor = null;
context = null;
AudioContext = null;
});

} else {
document.getElementById("mic-button").innerHTML = "<i class=\"material-icons\">mic</i>"

Expand Down Expand Up @@ -237,10 +253,39 @@

function microphoneProcess(e) {
let left = e.inputBuffer.getChannelData(0);
let left16 = downsampleBuffer(left, 44100, 16000)
socket.emit('binaryData', left16);
let left16 = downsampleBuffer(left, 44100, 16000);
socket.send(left16);
}

let downsampleBuffer = function (buffer, sampleRate, outSampleRate) {
if (outSampleRate == sampleRate) {
return buffer;
}
if (outSampleRate > sampleRate) {
throw "downsampling rate show be smaller than original sample rate";
}
let sampleRateRatio = sampleRate / outSampleRate;
let newLength = Math.round(buffer.length / sampleRateRatio);
let result = new Int16Array(newLength);
let offsetResult = 0;
let offsetBuffer = 0;
while (offsetResult < result.length) {
let nextOffsetBuffer = Math.round((offsetResult + 1) * sampleRateRatio);
let accum = 0, count = 0;
for (let i = offsetBuffer; i < nextOffsetBuffer && i < buffer.length; i++) {
accum += buffer[i];
count++;
}

result[offsetResult] = Math.min(1, accum / count) * 0x7FFF;
offsetResult++;
offsetBuffer = nextOffsetBuffer;
}
return result.buffer;
};

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function httpGet(theUrl) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theUrl, false); // false for synchronous request
Expand Down
8 changes: 4 additions & 4 deletions src/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"net/http"
"os"
"os/signal"
"strconv"
"time"

speech "cloud.google.com/go/speech/apiv1"
Expand Down Expand Up @@ -98,6 +97,7 @@ func main() {

}

/*
func speechToText(user string, b []byte) {
log.Println("Sending to google...") // TODO
Expand Down Expand Up @@ -191,7 +191,7 @@ func audioReceive(w http.ResponseWriter, hr *http.Request) {
go speechToText("dude", r)
}
}
}*/

func newAudioReceive(w http.ResponseWriter, hr *http.Request) {
wsupgrader.CheckOrigin = func(r *http.Request) bool {return true}
Expand Down Expand Up @@ -250,7 +250,7 @@ func newAudioReceive(w http.ResponseWriter, hr *http.Request) {
return
}

log.Println("Sending to google...")
// log.Println("Sending to google...")

if err := stream.Send(&speechpb.StreamingRecognizeRequest{
StreamingRequest: &speechpb.StreamingRecognizeRequest_AudioContent{
Expand All @@ -259,7 +259,7 @@ func newAudioReceive(w http.ResponseWriter, hr *http.Request) {
}); err != nil {
log.Printf("Could not send audio: %v", err)
}
log.Println("Sent to google!")
// log.Println("Sent to google!")
}
}()

Expand Down

0 comments on commit 599d952

Please sign in to comment.