-
-
Notifications
You must be signed in to change notification settings - Fork 557
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: load fxgl-intelligence locally, keep consistent package str…
…ucture
- Loading branch information
Showing
10 changed files
with
155 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
fxgl-intelligence/src/main/resources/com/almasb/fxgl/intelligence/rpc-common.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const SEPARATOR = "*,,*"; | ||
const FUNCTION_CALL_TAG = "F_CALL:"; | ||
const FUNCTION_RETURN_TAG = "F_RETURN:"; | ||
|
||
function rpcRun(funcName, ...args) { | ||
let argsString = ""; | ||
|
||
for (const arg of args) { | ||
argsString += arg + SEPARATOR; | ||
} | ||
|
||
let message = `${FUNCTION_CALL_TAG}${funcName}${SEPARATOR}${argsString}`; | ||
|
||
socket.send(message); | ||
} | ||
|
||
function rpcReturn(funcName) { | ||
// TODO: unique id? | ||
//socket.send(`${FUNCTION_RETURN_TAG}${funcName}.F_RESULT:${names}`); | ||
} |
24 changes: 24 additions & 0 deletions
24
fxgl-intelligence/src/main/resources/com/almasb/fxgl/intelligence/tts/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
<title>Text-to-speech</title> | ||
</head> | ||
|
||
<body> | ||
<h2>Text-to-speech service</h2> | ||
|
||
<form id="myForm"> | ||
<input id="txt" type="text" class="txt" value="" /> | ||
|
||
<div class="controls"> | ||
<button id="play" type="submit">Play</button> | ||
</div> | ||
</form> | ||
|
||
<script src="../rpc-common.js"></script> | ||
<script src="script.js"></script> | ||
</body> | ||
</html> |
82 changes: 82 additions & 0 deletions
82
fxgl-intelligence/src/main/resources/com/almasb/fxgl/intelligence/tts/script.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
const synth = window.speechSynthesis; | ||
let voices = []; | ||
let selectedVoice = null; | ||
|
||
const inputForm = document.querySelector("form"); | ||
const inputTxt = document.querySelector(".txt"); | ||
|
||
const socket = new WebSocket('ws://localhost:55550'); | ||
|
||
socket.addEventListener('open', function (event) { | ||
voices = synth.getVoices(); | ||
|
||
if (voices.length == 0) { | ||
// voices are loaded async | ||
window.speechSynthesis.onvoiceschanged = function() { | ||
voices = synth.getVoices(); | ||
initVoices(); | ||
}; | ||
} else { | ||
initVoices(); | ||
} | ||
}); | ||
|
||
socket.addEventListener('message', function (event) { | ||
let message = event.data; | ||
|
||
if (message.startsWith(FUNCTION_CALL_TAG)) { | ||
let func = message.substring(FUNCTION_CALL_TAG.length); | ||
let tokens = func.split('*,,*'); | ||
let funcName = tokens[0]; | ||
|
||
if (funcName === "speak") { | ||
// TODO: check length? | ||
let voiceName = tokens[1]; | ||
let voiceText = tokens[2]; | ||
|
||
for (let i = 0; i < voices.length; i++) { | ||
if (voices[i].name === voiceName) { | ||
selectedVoice = voices[i]; | ||
break; | ||
} | ||
} | ||
|
||
speak(voiceText); | ||
} | ||
} | ||
}); | ||
|
||
function initVoices() { | ||
let names = voices.map((v) => v.name); | ||
|
||
rpcRun("initVoices", names); | ||
} | ||
|
||
function speak(text) { | ||
if (synth.speaking || text === "") | ||
return; | ||
|
||
const speech = new SpeechSynthesisUtterance(text); | ||
|
||
speech.onerror = function (event) { | ||
// TODO: | ||
//socket.send(`Speech synthesis error: ${event.error}`); | ||
}; | ||
|
||
if (selectedVoice !== null) { | ||
speech.voice = selectedVoice; | ||
} | ||
|
||
speech.pitch = 1.0; | ||
speech.rate = 1.0; | ||
|
||
synth.speak(speech); | ||
} | ||
|
||
inputForm.onsubmit = function (event) { | ||
event.preventDefault(); | ||
|
||
speak(inputTxt.value); | ||
|
||
inputTxt.blur(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters