Skip to content

Instantly share code, notes, and snippets.

@getflourish
Last active May 9, 2018 20:37
Show Gist options
  • Save getflourish/96b663af0e4418b11e76 to your computer and use it in GitHub Desktop.
Save getflourish/96b663af0e4418b11e76 to your computer and use it in GitHub Desktop.
Speech to Text for Processing using WebSockets and Google Chrome.
/*
Simple WebSocketServer example that can receive voice transcripts from Chrome
Requires WebSockets Library: https://github.com/alexandrainst/processing_websockets
*/
import websockets.*;
WebsocketServer socket;
void setup() {
socket = new WebsocketServer(this, 1337, "/p5websocket");
}
void draw() {
background(0);
}
void webSocketServerEvent(String msg){
println(msg);
}
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
// We need to check if the browser supports WebSockets
if ("WebSocket" in window) {
// Before we can connect to the WebSocket, we need to start it in Processing.
var ws = new WebSocket("ws://localhost:1337/p5websocket");
} else {
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
// Now we can start the speech recognition
// Supported only in Chrome
// Once started, you need to allow Chrome to use the microphone
var recognition = new webkitSpeechRecognition();
// Be default, Chrome will only return a single result.
// By enabling "continuous", Chrome will keep the microphone active.
recognition.continuous = true;
recognition.onresult = function(event) {
// Get the current result from the results object
var transcript = event.results[event.results.length-1][0].transcript;
// Send the result string via WebSocket to the running Processing Sketch
ws.send(transcript);
}
// Start the recognition
recognition.start();
// Restart the recognition on timeout
recognition.onend = function(){
recognition.start();
}
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment