Libraryless. Click here for Pure Java version (3297L/19K).
1 | sS hSpeechRecognition(S jsOnSpeech, bool autoStart) {
|
2 | ret hSpeechRecognition(jsOnSpeech, autoStart, "de-DE", true); |
3 | } |
4 | |
5 | // requires JQuery |
6 | sS hSpeechRecognition(S jsOnSpeech, bool autoStart, S language, bool autoTurnOn, O... _) {
|
7 | optPar S noWebKit = "No webkit"; |
8 | optPar S speechUI = ""; |
9 | ret hjavascript(([[ |
10 | var bigOn = #BIGON#, srPause = false, listening = false; |
11 | var recognition; |
12 | var firstStart = true; |
13 | var srUpdatesBlocked = false; |
14 | |
15 | function resultHTML(html) {
|
16 | if ($("#speechResults")) $("#speechResults").html(html);
|
17 | } |
18 | |
19 | function srUpdate() {
|
20 | if (srUpdatesBlocked) return; |
21 | var btn = $("#speechOnBtn, .speechOnBtn");
|
22 | //console.log("srUpdate: btn=" + btn.length);
|
23 | btn.prop('disabled', false);
|
24 | btn.html(bigOn ? "Stop listening" : "Listen"); |
25 | //console.log("srUpdate: bigOn=" + bigOn + ", srPause=" + srPause);
|
26 | |
27 | var should = bigOn && !srPause; |
28 | $(".listenStatus").css("visibility", should ? "visible" : "hidden");
|
29 | if (should && !listening) startRecognition(); |
30 | else if (!should && listening) stopRecognition(); |
31 | } |
32 | |
33 | function stopRecognition() {
|
34 | listening = false; |
35 | recognition.stop(); |
36 | srUpdate(); |
37 | } |
38 | |
39 | function isMobile() {
|
40 | return /mobile/i.test(navigator.userAgent); |
41 | } |
42 | |
43 | function startRecognition() {
|
44 | listening = true; |
45 | recognition.start(); |
46 | if (firstStart) {
|
47 | firstStart = false; |
48 | resultHTML(isMobile() ? "Please talk after the blip" : "You can talk now"); |
49 | } |
50 | srUpdate(); |
51 | } |
52 | |
53 | function startSpeechRecog() {
|
54 | resultHTML("Starting");
|
55 | console.log("startSpeechRecog");
|
56 | |
57 | if (!('webkitSpeechRecognition' in window)) {
|
58 | console.log("No webkit");
|
59 | document.writeln(]] + jsQuote(noWebKit) + [[); |
60 | return; |
61 | } |
62 | |
63 | console.log("Have webkit");
|
64 | document.writeln(]] + jsQuote(speechUI) + [[); |
65 | |
66 | recognition = new webkitSpeechRecognition(); |
67 | resultHTML("Speech recognition available.");
|
68 | recognition.lang = "#LANGUAGE#"; |
69 | |
70 | recognition.onerror = function(event) {
|
71 | var s = " "; |
72 | if (event.error != "no-speech") s = "Error: " + event.error; |
73 | resultHTML(s); |
74 | } |
75 | |
76 | recognition.onresult = function(event) {
|
77 | var result = event.results[0]; |
78 | var transcript = result[0].transcript; |
79 | resultHTML("You said: \"" + transcript + "\"");
|
80 | #ONSPEECH# |
81 | if (transcript == "stop listening") {
|
82 | bigOn = false; |
83 | srUpdate(); |
84 | } |
85 | } |
86 | |
87 | recognition.onnomatch = function(event) {
|
88 | resultHTML("-");
|
89 | } |
90 | |
91 | recognition.onend = function(event) {
|
92 | listening = false; |
93 | srUpdatesBlocked = true; // block for a short time |
94 | setTimeout(function() {
|
95 | srUpdatesBlocked = false; |
96 | srUpdate(); |
97 | }, 100); |
98 | } |
99 | |
100 | srUpdate(); |
101 | } |
102 | |
103 | // legacy |
104 | function startOrStop() { startOrStop(); }
|
105 | |
106 | function startOrStopSpeechRecog() {
|
107 | bigOn = !bigOn; |
108 | srUpdate(); |
109 | } |
110 | |
111 | window.srUpdate = srUpdate; |
112 | ]] + (autoStart ? "startSpeechRecog();" : "")) |
113 | .replace("#BIGON#", str(autoTurnOn))
|
114 | .replace("#LANGUAGE#", language)
|
115 | .replace("#ONSPEECH#", jsOnSpeech));
|
116 | } |
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
| Snippet ID: | #1014011 |
| Snippet name: | hSpeechRecognition - HTML continues speech recognition using Google |
| Eternal ID of this version: | #1014011/43 |
| Text MD5: | 6aad98163b18457985bb409ea9610d21 |
| Transpilation MD5: | 91799d1a347a9d6502e81580b776e2ef |
| Author: | stefan |
| Category: | javax / html |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2021-07-24 00:53:18 |
| Source code size: | 3446 bytes / 116 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 818 / 1078 |
| Version history: | 42 change(s) |
| Referenced in: | [show references] |