Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

423
LINES

< > BotCompany Repo | #1002938 // Pivo Bot Continuous Test

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (778L/9K/28K).

1  
!752
2  
3  
static S rawLink(S pageName) {
4  
  ret "/" + parseSnippetID(getProgramID()) + "/raw/" + pageName;
5  
}
6  
7  
static S html(S uri, Map<S, S> params) {
8  
  if (eq(uri, "/has-speech")) {
9  
    S s = params.get("q");
10  
    S text = s;
11  
    
12  
    /*
13  
    ((ThreadLocal<Boolean>) get(getMainBot(), "attn")).set(true);
14  
    pivoMode. set(true);
15  
    S text = //askSelf(s);
16  
      callStaticAnswerMethod(mc(), s);
17  
    if (empty(text)) ret "";
18  
    S soundURL = getSoundURLCereproc(text, voice);
19  
    S html = [[
20  
    <audio controls autoplay>
21  
      <source src="SRC" type="audio/mpeg">
22  
      ! Kein Ton im Browser !
23  
    </audio>
24  
    ]];
25  
    html = html.replace("SRC", htmlencode(soundURL));
26  
    */
27  
    
28  
    ret "Speech: " + htmlencode(text) /* + "<br>" + html*/;
29  
  }
30  
  
31  
  if (eq(uri, "/empty")) {
32  
    ret "";
33  
  }
34  
  
35  
  if (eq(uri, "/upper")) {
36  
 S html = [=[
37  
<!DOCTYPE html>
38  
<meta charset="utf-8">
39  
<title>TinyBrain Speech Demo</title>
40  
<style>
41  
  * {
42  
    font-family: Verdana, Arial, sans-serif;
43  
  }
44  
  a:link {
45  
    color:#000;
46  
    text-decoration: none;
47  
  }
48  
  a:visited {
49  
    color:#000;
50  
  }
51  
  a:hover {
52  
    color:#33F;
53  
  }
54  
  .button {
55  
    background: -webkit-linear-gradient(top,#008dfd 0,#0370ea 100%);
56  
    border: 1px solid #076bd2;
57  
    border-radius: 3px;
58  
    color: #fff;
59  
    display: none;
60  
    font-size: 13px;
61  
    font-weight: bold;
62  
    line-height: 1.3;
63  
    padding: 8px 25px;
64  
    text-align: center;
65  
    text-shadow: 1px 1px 1px #076bd2;
66  
    letter-spacing: normal;
67  
  }
68  
  .center {
69  
    padding: 10px;
70  
    text-align: center;
71  
  }
72  
  .final {
73  
    color: black;
74  
    padding-right: 3px; 
75  
  }
76  
  .interim {
77  
    color: gray;
78  
  }
79  
  .info {
80  
    font-size: 34px;
81  
    text-align: center;
82  
    color: #777;
83  
    display: none;
84  
  }
85  
  .right {
86  
    float: right;
87  
  }
88  
  .sidebyside {
89  
    display: inline-block;
90  
    width: 45%;
91  
    min-height: 40px;
92  
    text-align: left;
93  
    vertical-align: top;
94  
  }
95  
  #headline {
96  
    font-size: 80px;
97  
    font-weight: 300;
98  
  }
99  
  #info {
100  
    font-size: 50px;
101  
    text-align: center;
102  
    color: #777;
103  
    visibility: hidden;
104  
  }
105  
  #results {
106  
    font-size: 55px;
107  
    font-weight: bold;
108  
    border: 1px solid #ddd;
109  
    padding: 15px;
110  
    text-align: left;
111  
    min-height: 50px;
112  
  }
113  
  #start_button {
114  
    border: 0;    background-color:transparent;
115  
    padding: 0;
116  
  }
117  
</style>
118  
119  
<div id="info">
120  
  <p id="info_start">Click on the microphone icon and begin speaking.</p>
121  
  <p id="info_speak_now">Speak after the bleep.</p>
122  
  <p id="info_no_speech">No speech was detected</p>
123  
  <p id="info_no_microphone" style="display:none">
124  
    No microphone was found. Ensure that a microphone is installed and that
125  
    <a href="//support.google.com/chrome/bin/answer.py?hl=en&amp;answer=1407892">
126  
    microphone settings</a> are configured correctly.</p>
127  
  <p id="info_allow">Click the "Allow" button.</p>
128  
  <p id="info_denied">Permission to use microphone was denied.</p>
129  
  <p id="info_blocked">Permission to use microphone is blocked. To change,
130  
    go to chrome://settings/contentExceptions#media-stream</p>
131  
  <p id="info_upgrade">Web Speech API is not supported by this browser.
132  
     Upgrade to <a href="//www.google.com/chrome">Chrome</a>
133  
     version 25 or later.</p>
134  
</div>
135  
<div class="right">
136  
  <button id="start_button" onclick="startButton(event)">
137  
    <img id="start_img" src="http://tinybrain.de:8080/speech/mic.gif" width="120" alt="Start"></button>
138  
</div>
139  
<div id="results">
140  
  <span id="final_span" class="final"></span>
141  
  <span id="interim_span" class="interim"></span>
142  
  <p>
143  
</div>
144  
<div class="center">
145  
  <p>
146  
  <div id="div_language">
147  
    <?= $mode ?>
148  
    &nbsp;&nbsp;
149  
    <select id="select_language" onchange="updateCountry()"></select>
150  
    &nbsp;&nbsp;
151  
    <select id="select_dialect"></select>
152  
  </div>
153  
</div>
154  
<script>
155  
var langs =
156  
[['Afrikaans',       ['af-ZA']],
157  
 ['Bahasa Indonesia',['id-ID']],
158  
 ['Bahasa Melayu',   ['ms-MY']],
159  
 ['Català',          ['ca-ES']],
160  
 ['?eština',         ['cs-CZ']],
161  
 ['Deutsch',         ['de-DE']],
162  
 ['English',         ['en-AU', 'Australia'],
163  
                     ['en-CA', 'Canada'],
164  
                     ['en-IN', 'India'],
165  
                     ['en-NZ', 'New Zealand'],
166  
                     ['en-ZA', 'South Africa'],
167  
                     ['en-GB', 'United Kingdom'],
168  
                     ['en-US', 'United States']],
169  
 ['Español',         ['es-AR', 'Argentina'],
170  
                     ['es-BO', 'Bolivia'],
171  
                     ['es-CL', 'Chile'],
172  
                     ['es-CO', 'Colombia'],
173  
                     ['es-CR', 'Costa Rica'],
174  
                     ['es-EC', 'Ecuador'],
175  
                     ['es-SV', 'El Salvador'],
176  
                     ['es-ES', 'España'],
177  
                     ['es-US', 'Estados Unidos'],
178  
                     ['es-GT', 'Guatemala'],
179  
                     ['es-HN', 'Honduras'],
180  
                     ['es-MX', 'México'],
181  
                     ['es-NI', 'Nicaragua'],
182  
                     ['es-PA', 'Panamá'],
183  
                     ['es-PY', 'Paraguay'],
184  
                     ['es-PE', 'Perú'],
185  
                     ['es-PR', 'Puerto Rico'],
186  
                     ['es-DO', 'República Dominicana'],
187  
                     ['es-UY', 'Uruguay'],
188  
                     ['es-VE', 'Venezuela']],
189  
 ['Euskara',         ['eu-ES']],
190  
 ['Français',        ['fr-FR']],
191  
 ['Galego',          ['gl-ES']],
192  
 ['Hrvatski',        ['hr_HR']],
193  
 ['IsiZulu',         ['zu-ZA']],
194  
 ['Íslenska',        ['is-IS']],
195  
 ['Italiano',        ['it-IT', 'Italia'],
196  
                     ['it-CH', 'Svizzera']],
197  
 ['Magyar',          ['hu-HU']],
198  
 ['Nederlands',      ['nl-NL']],
199  
 ['Norsk bokmål',    ['nb-NO']],
200  
 ['Polski',          ['pl-PL']],
201  
 ['Português',       ['pt-BR', 'Brasil'],
202  
                     ['pt-PT', 'Portugal']],
203  
 ['Român?',          ['ro-RO']],
204  
 ['Sloven?ina',      ['sk-SK']],
205  
 ['Suomi',           ['fi-FI']],
206  
 ['Svenska',         ['sv-SE']],
207  
 ['Türkçe',          ['tr-TR']],
208  
 ['?????????',       ['bg-BG']],
209  
 ['P??????',         ['ru-RU']],
210  
 ['??????',          ['sr-RS']],
211  
 ['???',            ['ko-KR']],
212  
 ['??',             ['cmn-Hans-CN', '??? (????)'],
213  
                     ['cmn-Hans-HK', '??? (??)'],
214  
                     ['cmn-Hant-TW', '?? (??)'],
215  
                     ['yue-Hant-HK', '?? (??)']],
216  
 ['???',           ['ja-JP']],
217  
 ['Lingua lat?na',   ['la']]];
218  
219  
for (var i = 0; i < langs.length; i++) {
220  
  select_language.options[i] = new Option(langs[i][0], i);
221  
}
222  
select_language.selectedIndex = 5;
223  
updateCountry();
224  
select_dialect.selectedIndex = 6;
225  
showInfo('info_start');
226  
227  
function updateCountry() {
228  
  for (var i = select_dialect.options.length - 1; i >= 0; i--) {
229  
    select_dialect.remove(i);
230  
  }
231  
  var list = langs[select_language.selectedIndex];
232  
  for (var i = 1; i < list.length; i++) {
233  
    select_dialect.options.add(new Option(list[i][1], list[i][0]));
234  
  }
235  
  select_dialect.style.visibility = list[1].length == 1 ? 'hidden' : 'visible';
236  
}
237  
238  
var final_transcript = '';
239  
var recognizing = false;
240  
var ignore_onend;
241  
var ignore_result;
242  
var start_timestamp;
243  
if (!('webkitSpeechRecognition' in window)) {
244  
  upgrade();
245  
} else {
246  
  var contentFrame = parent.contentframe;
247  
  var lastText = "";
248  
249  
  start_button.style.display = 'inline-block';
250  
  var recognition = new webkitSpeechRecognition();
251  
  recognition.continuous = true;
252  
  recognition.interimResults = false;
253  
254  
  recognition.onstart = function() {
255  
    recognizing = true;
256  
    showInfo('info_speak_now');
257  
    start_img.src = 'http://tinybrain.de:8080/speech/mic-animate.gif';
258  
  };
259  
260  
  recognition.onerror = function(event) {
261  
    if (event.error == 'no-speech') {
262  
      start_img.src = 'http://tinybrain.de:8080/speech/mic.gif';
263  
      showInfo('info_no_speech');
264  
      ignore_onend = true;
265  
    }
266  
    if (event.error == 'audio-capture') {
267  
      start_img.src = 'http://tinybrain.de:8080/speech/mic.gif';
268  
      showInfo('info_no_microphone');
269  
      ignore_onend = true;
270  
    }
271  
    if (event.error == 'not-allowed') {
272  
      if (event.timeStamp - start_timestamp < 100) {
273  
        showInfo('info_blocked');
274  
      } else {
275  
        showInfo('info_denied');
276  
      }
277  
      ignore_onend = true;
278  
    }
279  
  };
280  
281  
  recognition.onend = function() {
282  
    recognizing = false;
283  
    if (ignore_onend) {
284  
      return;
285  
    }
286  
    start_img.src = 'http://tinybrain.de:8080/speech/mic.gif';
287  
    
288  
    //final_transcript = ""; // DEH HACK
289  
290  
    if (!final_transcript) {
291  
      showInfo('info_start');
292  
      return;
293  
    }
294  
    showInfo('');
295  
    if (window.getSelection) {
296  
      window.getSelection().removeAllRanges();
297  
      var range = document.createRange();
298  
      range.selectNode(document.getElementById('final_span'));
299  
      window.getSelection().addRange(range);
300  
    }
301  
  };
302  
303  
  recognition.onresult = function(event) {
304  
    var interim_transcript = '';
305  
    if (ignore_result) {
306  
      ignore_result = false;
307  
      return;
308  
    }
309  
    for (var i = event.resultIndex; i < event.results.length; ++i) {
310  
      if (event.results[i].isFinal) {
311  
        var newData = event.results[i][0].transcript;
312  
        //final_transcript += newData;
313  
        final_transcript = newData;
314  
        var frameURL = "LINK/has-speech?q=" + encodeURIComponent(newData);
315  
        if (contentFrame && lastText != newData) {
316  
          lastText = newData;
317  
          contentFrame.location.href = frameURL;
318  
        }
319  
      } else {
320  
        interim_transcript += event.results[i][0].transcript;
321  
      }
322  
    }
323  
    final_transcript = capitalize(final_transcript);
324  
    final_span.innerHTML = linebreak(final_transcript);
325  
    interim_span.innerHTML = linebreak(interim_transcript);
326  
    if (/*final_transcript ||*/ interim_transcript) {
327  
      showButtons('inline-block');
328  
    }
329  
  };
330  
  
331  
  startRecognition();
332  
}
333  
334  
function upgrade() {
335  
  start_button.style.visibility = 'hidden';
336  
  showInfo('info_upgrade');
337  
}
338  
339  
var two_line = /\n\n/g;
340  
var one_line = /\n/g;
341  
function linebreak(s) {
342  
  return s.replace(two_line, '<p></p>').replace(one_line, '<br>');
343  
}
344  
345  
var first_char = /\S/;
346  
function capitalize(s) {
347  
  return s.replace(first_char, function(m) { return m.toUpperCase(); });
348  
}
349  
350  
function copyButton() {
351  
  if (recognizing) {
352  
    recognizing = false;
353  
    recognition.stop();
354  
  }
355  
  copy_button.style.display = 'none';
356  
  copy_info.style.display = 'inline-block';
357  
  showInfo('');
358  
}
359  
360  
function startButton(event) {
361  
  startRecognition();
362  
}
363  
364  
function startRecognition() {
365  
  if (recognizing) {
366  
    recognition.stop();
367  
    ignore_result = true;
368  
    return;
369  
  }
370  
  final_transcript = '';
371  
  recognition.lang = select_dialect.value;
372  
  recognition.start();
373  
  ignore_onend = false;
374  
  final_span.innerHTML = '';
375  
  interim_span.innerHTML = '';
376  
  start_img.src = 'http://tinybrain.de:8080/speech/mic-slash.gif';
377  
  showInfo('info_allow');
378  
  showButtons('none');
379  
  start_timestamp = event.timeStamp;
380  
}
381  
382  
function showInfo(s) {
383  
  if (s) {
384  
    for (var child = info.firstChild; child; child = child.nextSibling) {
385  
      if (child.style) {
386  
        child.style.display = child.id == s ? 'inline' : 'none';
387  
      }
388  
    }
389  
    info.style.visibility = 'visible';
390  
  } else {
391  
    info.style.visibility = 'hidden';
392  
  }
393  
}
394  
395  
var current_style;
396  
function showButtons(style) {
397  
  if (style == current_style) {
398  
    return;
399  
  }
400  
  current_style = style;
401  
  copy_button.style.display = style;
402  
  copy_info.style.display = 'none';
403  
}
404  
</script>
405  
]=];
406  
    ret render(html);
407  
  }
408  
  
409  
  // frame set
410  
  
411  
  ret render([[
412  
<title>Pivo 2 Continuous</title>
413  
414  
<frameset rows="280,*">
415  
  <frame src="LINK/upper"></frame>
416  
  <frame name="contentframe" src="LINK/empty"></frame>
417  
</frameset>
418  
]]);
419  
}
420  
421  
static S render(S html) {
422  
  ret html.replace("LINK/", rawLink(""));
423  
}

Author comment

Began life as a copy of #1002922

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002938
Snippet name: Pivo Bot Continuous Test
Eternal ID of this version: #1002938/1
Text MD5: 9f883d7c358d6ac701ee2c7e4825cf24
Transpilation MD5: 9309b2b60b5a4f21418d383058d76b82
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-04-10 14:43:32
Source code size: 11845 bytes / 423 lines
Pitched / IR pitched: No / No
Views / Downloads: 771 / 1007
Referenced in: [show references]