Skip to content

Commit

Permalink
Create a hacky test page for TTS
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosuav committed Jan 5, 2025
1 parent bb8f6d5 commit fe743f1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
18 changes: 18 additions & 0 deletions httpstatic/tts_hack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {choc, set_content, DOM, on} from "https://rosuav.github.io/choc/factory.js";
const {OPTGROUP, OPTION} = choc; //autoimport

export function render(data) {
set_content("#tts_voice", data.voices.map(([label, voices]) =>
OPTGROUP({label}, voices.map(v => OPTION({value: v.selector}, v.desc)))
));
}

export function sockmsg_speak(msg) {
DOM("#player").src = msg.tts;
DOM("#player").play();
}

on("submit", "#send", e => {
e.preventDefault();
ws_sync.send({cmd: "speak", text: DOM("#stuff").value, voice: DOM("#tts_voice").value});
});
34 changes: 34 additions & 0 deletions modules/http/tts_hack.pike
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
inherit http_websocket;
inherit annotated;

constant markdown = #"# TTS hack
<audio controls id=player></audio>
<label>Voice: <select id=tts_voice></select></label>
<form id=send><label>Enter stuff: <input size=80 id=stuff></label> <button>Send</button></form>
";

mapping(string:mixed) http_request(Protocols.HTTP.Server.Request req) {
return render(req, (["vars": (["ws_group": req->variables->key || ""])]));
}

@retain: multiset tts_hack_valid_keys = (<>);
string websocket_validate(mapping(string:mixed) conn, mapping(string:mixed) msg) {
if (!tts_hack_valid_keys[msg->group]) return "Nope";
}

mapping get_state(string group) {
return (["voices": G->G->tts_config->avail_voices || ({ })]);
}

__async__ mapping|zero websocket_cmd_speak(mapping(string:mixed) conn, mapping(string:mixed) msg) {
string text = msg->text;
if (!text || text == "") return 0;
object alertbox = G->G->websocket_types->chan_alertbox;
text = await(alertbox->filter_bad_words(text, "replace"));
string tts = await(alertbox->text_to_speech(text, msg->voice || "en-GB/en-GB-Standard-A/FEMALE", "tts_hack"));
return (["cmd": "speak", "text": text, "tts": tts]);
}

protected void create(string name) {::create(name);}

0 comments on commit fe743f1

Please sign in to comment.