Warning: session_start(): open(/var/lib/php/sessions/sess_oiseu4rpmn39p0sdknd6ofja4g, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
!7
sbool autoClose = true; // close JavaScript window on server lost
sbool autoTurnOn = true; // turn on recognition on program start
static int initialDelay = 0, autoCloseDelay = 5000;
static double firstWindowDelay = 0;
sbool infoBox;
static int port;
static O onUtterance; // voidfunc(S)
static L webSockets = synchroList();
sbool startRecognition;
static java.util.Timer stopper;
sS language = "en-US";
sS myURL;
static JButton btn;
sbool hadAnyWebSockets; // Does Chrome work at all?
p-subst {
load('language);
if (isMainProgram())
infoBox = true;
print("Logging speech to: " + speechRecognitionLog());
NanoHTTPD.SOCKET_READ_TIMEOUT = 24*3600*1000; // not long enough - TODO: Fix in NanoHTTPD
port = serveHttpWithWebSockets(findFreePortAbove(9999), func(NanoHTTPD.IHTTPSession handshake) {
print("New WebSocket.");
WebSocket ws = new WebSocket(handshake) {
protected void onPong(WebSocketFrame pongFrame) { print("pong"); }
protected void onMessage(WebSocketFrame messageFrame) ctex {
fS s = messageFrame.getTextPayload();
//print("WebSocket message: " + s);
cancelTimeout();
infoBoxOrPrint(/*"User said: " +*/ s, infoBox);
//send("stop");
//print("Sending start."); send("start");
logQuotedWithTime(speechRecognitionLog(), "[Chrome Speech] " + s);
thread {
pcallF(onUtterance, s);
}
hotCommands(s);
}
protected void onClose(WebSocketFrame.CloseCode code, String reason, boolean initiatedByRemote) {
print("WebSocket closed.");
webSockets.remove(this);
}
protected void onException(IOException e) { printStackTrace(e); }
};
if (startRecognition) {
startRecognition = false;
ws.send("big on");
}
// close any other recognizers
for (WebSocket ws2 : cloneList(webSockets)) {
print("Closing websocket.");
pcall { ws2.close(WebSocketFrame.CloseCode.NormalClosure, ""); }
webSockets.remove(ws2);
}
hadAnyWebSockets = true;
ret addAndReturn(webSockets, ws);
});
myURL = print("http://localhost:" + port + "/popup");
makeBot("Chrome Speech.");
thread { sleepSeconds(firstWindowDelay); startChromeApp(myURL); }
showControls(jcenteredline(btn = jbutton("Open Speech Recognizer", r {
startChromeApp(myURL)
})));
awtEvery(btn, 500, r { setEnabled(btn, empty(webSockets)) });
/*thread "Chrome Re-Starter" {
sleepSeconds(20);
repeat with sleep 5 {
if (hadAnyWebSockets && empty(webSockets)) {
startChromeApp(myURL);
sleepSeconds(15);
}
}
});*/
//if (autoTurnOn) startRecognition();
}
html {
if (neq(uri, "/popup"))
ret hbody("Opening popup..." + hjavascript([[
window.open('/popup', 'speech_recognizer', 'width=300,height=300,location=no');
setTimeout(function() { window.close(); }, 10000);
]]));
ret hhtml(hhead(htitle("Continuous Speech Recognizer")) + hbody(div(
h3("Continuous Speech Recognizer")
+ loadJQuery()
+ hdiv("Language: " + language, id := 'lang, style := "font-size: 10px")
+ hdiv("Results come here", id := 'results, style := "margin: 10px")
+ hjavascript([[
var websocket;
var bigOn = #BIGON#, pause = false, listening = false;
//var stopUntil = 0;
function update() {
if (bigOn) {
$("#btn").html("Turn off");
document.title = (pause ? "Paused" : "On") + " - Speech Recognizer";
} else {
$("#btn").html("Turn on");
document.title = "Speech Recognizer";
}
var should = bigOn && !pause;
if (should && !listening) startRecognition();
else if (!should && listening) stopRecognition();
}
function stopRecognition() {
listening = false;
recognition.stop();
update();
}
function startRecognition() {
listening = true;
//if (Date.now() < stopUntil) return;
recognition.start();
update();
}
function openWebSocket() {
websocket = new WebSocket("ws://localhost:#PORT#/");
websocket.onopen = function(event) {
$("#btn").prop('disabled', false);
$("#results").html(bigOn ? "Listening." : "Click to turn me on.");
if (bigOn)
startRecognition();
};
websocket.onmessage = function(event) {
if (event.data == 'big on') { bigOn = true; update(); }
if (event.data == 'big off') { bigOn = false; update(); }
if (event.data == 'pause') { pause = true; update(); }
if (event.data == 'unpause') { pause = false; update(); }
if (event.data.substring(0, 9) == 'language ') {
var l = event.data.substring(9);
recognition.lang = l;
$("#lang").html("Language: " + l);
}
};
websocket.onclose = function(event) {
$("#results").html("WebSocket closed");
if (#AUTOCLOSE#) setTimeout(function() { window.close(); }, autoCloseDelay);
};
}
setTimeout(openWebSocket, #INITIALDELAY#);
var recognition = new webkitSpeechRecognition();
recognition.lang = "#LANGUAGE#";
recognition.onerror = function(event) {
var s = " ";
if (event.error != "no-speech") s = "Error: " + event.error;
$("#results").html(s);
//stopRecognition(); // do we get onEnd later?
//setTimeout(startRecognition, 1000); // safety delay
}
recognition.onresult = function(event) {
var result = event.results[0];
var transcript = result[0].transcript;
$("#results").html("Transcript: " + transcript);
websocket.send(transcript);
//stopUntil = Date.now()+200;
//stopRecognition(); setTimeout(startRecognition, 100);
}
recognition.onnomatch = function(event) {
$("#results").html("-");
//stopRecognition(); setTimeout(startRecognition, 100);
}
recognition.onend = function(event) {
//$("#results").html("-end-");
//stopRecognition();
listening = false; setTimeout(update, 100);
}
function startOrStop() {
bigOn = !bigOn;
update();
}
window.resizeTo(300, 300);
]]) // end of JavaScript, variables follow
.replace("#BIGON#", str(autoTurnOn))
.replace("#PORT#", str(port))
.replace("#AUTOCLOSE#", autoClose ? "true" : "false")
.replace("#INITIALDELAY#", str(initialDelay))
.replace("#LANGUAGE#", language)
.replace("autoCloseDelay", str(autoCloseDelay))
+ tag('button, "...", onclick := "startOrStop()", type := 'button, id := 'btn, disabled := 'disabled)
+ hdiv("", id := 'msgs, style := "margin: 10px; font-size: 10px")
//+ p(ahref("#", "Popup", onClick := "window.open('/', 'speech_recognizer', 'width=300,height=300,location=no'); return false;"));
, style := "text-align: center"));
}
svoid startRecognition {
L l = cloneList(webSockets);
if (empty(l)) startRecognition = true;
else {
//print("Starting recognition." + (l(l) > 1 ? "Weird: Have " + l(l) + " websockets" : ""));
pcall {
first(l).send("big on");
}
}
}
svoid stopRecognition {
if (startRecognition) startRecognition = false;
if (nempty(webSockets)) pcall {
first(webSockets).send("big off");
}
}
sS hotCommands(S s) {
if "english|englisch" ret answer("language " + quote("en-US"));
if "german|deutsch" ret answer("language " + quote("de-DE"));
if "stop recognition" { stopRecognition(); ret "OK"; }
null;
}
answer {
try answer hotCommands(s);
if "start recognition timeout *" {
final int seconds = parseInt($1);
startRecognition();
stopper = timerOnce(toMS(seconds), f stopRecognition);
ret "OK";
}
if "start recognition" { startRecognition(); ret "OK"; }
if "language *" {
setAndSave('language, $1);
if (eq(language, "de-DE")) william("Switching to German");
else william("Switching to English");
pcall { if (nempty(webSockets)) first(webSockets).send("language " + $1); }
stopRecognition();
sleep(500);
startRecognition();
ret "OK";
}
}
svoid cancelTimeout {
if (stopper != null) { stopper.cancel(); stopper = null; }
}