Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f567dea
Showing
5 changed files
with
219 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
<!DOCTYPE html> | ||
<meta charset="utf-8"> | ||
<title>forty.to demo</title> | ||
<style> | ||
* { | ||
font-family: Verdana, Arial, sans-serif; | ||
} | ||
a:link { | ||
color:#000; | ||
text-decoration: none; | ||
} | ||
a:visited { | ||
color:#000; | ||
} | ||
a:hover { | ||
color:#33F; | ||
} | ||
.button { | ||
background: -webkit-linear-gradient(top,#008dfd 0,#0370ea 100%); | ||
border: 1px solid #076bd2; | ||
border-radius: 3px; | ||
color: #fff; | ||
display: none; | ||
font-size: 13px; | ||
font-weight: bold; | ||
line-height: 1.3; | ||
padding: 8px 25px; | ||
text-align: center; | ||
text-shadow: 1px 1px 1px #076bd2; | ||
letter-spacing: normal; | ||
} | ||
.center { | ||
padding: 10px; | ||
text-align: center; | ||
} | ||
.final { | ||
color: black; | ||
padding-right: 3px; | ||
} | ||
.interim { | ||
color: gray; | ||
} | ||
.info { | ||
font-size: 14px; | ||
text-align: center; | ||
color: #777; | ||
display: none; | ||
} | ||
.right { | ||
float: right; | ||
} | ||
#headline { | ||
font-size: 40px; | ||
font-weight: 300; | ||
} | ||
#info { | ||
font-size: 20px; | ||
text-align: center; | ||
color: #777; | ||
visibility: hidden; | ||
max-height: 20px; | ||
} | ||
#results { | ||
font-size: 14px; | ||
padding: 15px; | ||
text-align: center; | ||
min-height: 14px; | ||
} | ||
#start_button { | ||
border: 0; | ||
background-color:transparent; | ||
padding: 0; | ||
} | ||
</style> | ||
<body onload="startButton()"> | ||
<h1 class="center" id="headline"><a href="http://forty.to"><img id="logo_img" src="logo.png" alt="forty.to demo"></a></h1> | ||
<div id="info"> | ||
<p id="info_speak_now">Speak now.</p> | ||
<p id="info_no_speech">No speech was detected. You may need to adjust your<a href="//support.google.com/chrome/bin/answer.py?hl=en&answer=1407892"> microphone settings</a>.</p> | ||
<p id="info_no_microphone" style="display:none">No microphone was found. Ensure that a microphone is installed and that <a href="//support.google.com/chrome/bin/answer.py?hl=en&answer=1407892"> microphone settings</a> are configured correctly.</p> | ||
<p id="info_allow">Click the "Allow" button above to enable your microphone.</p> | ||
<p id="info_denied">Permission to use microphone was denied.</p> | ||
<p id="info_blocked">Permission to use microphone is blocked. To change, go to chrome://settings/contentExceptions#media-stream</p> | ||
<p id="info_upgrade">Web Speech API is not supported by this browser. Upgrade to <a href="//www.google.com/chrome">Chrome</a> version 25 or later.</p> | ||
</div> | ||
<div class="center"> | ||
<button id="start_button"><img id="start_img" src="mic.gif" alt="Start"></button> | ||
</div> | ||
<div id="results"> | ||
<form id="form" method="get" action="http://127.0.0.1/cgi-bin/demo.fcgi"><input id="question" type="hidden" name="question"></form> | ||
<span id="final_span" class="final"></span><span id="interim_span" class="interim"></span><p> | ||
</div> | ||
</body> | ||
<script> | ||
var final_transcript = ''; | ||
var recognizing = false; | ||
var ignore_onend; | ||
if (!('webkitSpeechRecognition' in window)) { | ||
upgrade(); | ||
} else { | ||
start_button.style.display = 'inline-block'; | ||
var recognition = new webkitSpeechRecognition(); | ||
recognition.continuous = true; | ||
recognition.interimResults = true; | ||
|
||
recognition.onstart = function() { | ||
recognizing = true; | ||
showInfo('info_speak_now'); | ||
start_img.src = 'mic-animate.gif'; | ||
}; | ||
|
||
recognition.onerror = function(event) { | ||
if (event.error == 'no-speech') { | ||
start_img.src = 'mic.gif'; | ||
showInfo('info_no_speech'); | ||
ignore_onend = true; | ||
} | ||
if (event.error == 'audio-capture') { | ||
start_img.src = 'mic.gif'; | ||
showInfo('info_no_microphone'); | ||
ignore_onend = true; | ||
} | ||
if (event.error == 'not-allowed') { | ||
showInfo('info_denied'); | ||
ignore_onend = true; | ||
} | ||
}; | ||
|
||
recognition.onend = function() { | ||
recognizing = false; | ||
if (ignore_onend) { | ||
return; | ||
} | ||
start_img.src = 'mic.gif'; | ||
if (!final_transcript) { | ||
showInfo('info_start'); | ||
return; | ||
} | ||
showInfo(''); | ||
if (window.getSelection) { | ||
window.getSelection().removeAllRanges(); | ||
var range = document.createRange(); | ||
range.selectNode(document.getElementById('final_span')); | ||
window.getSelection().addRange(range); | ||
} | ||
}; | ||
|
||
recognition.onresult = function(event) { | ||
var interim_transcript = ''; | ||
for (var i = event.resultIndex; i < event.results.length; ++i) { | ||
if (event.results[i].isFinal) { | ||
final_transcript += event.results[i][0].transcript; | ||
} else { | ||
interim_transcript += event.results[i][0].transcript; | ||
} | ||
} | ||
final_transcript = capitalize(final_transcript); | ||
final_span.innerHTML = linebreak(final_transcript); | ||
interim_span.innerHTML = linebreak(interim_transcript); | ||
if('' == interim_transcript) { | ||
final_transcript = questionize(final_transcript); | ||
final_span.innerHTML = linebreak(final_transcript); | ||
var formObject = document.forms['form']; | ||
formObject.elements["question"].value = linebreak(final_transcript); | ||
recognizing = false; | ||
recognition.stop(); | ||
formObject.submit(); | ||
} | ||
}; | ||
} | ||
|
||
function upgrade() { | ||
start_button.style.visibility = 'hidden'; | ||
showInfo('info_upgrade'); | ||
} | ||
|
||
var two_line = /\n\n/g; | ||
var one_line = /\n/g; | ||
function linebreak(s) { | ||
return s.replace(two_line, '<p></p>').replace(one_line, '<br>'); | ||
} | ||
|
||
var first_char = /\S/; | ||
function capitalize(s) { | ||
return s.replace(first_char, function(m) { return m.toUpperCase(); }); | ||
} | ||
|
||
function questionize(s) { | ||
return s + '?'; | ||
} | ||
|
||
function startButton() { | ||
if (recognizing) { | ||
recognition.stop(); | ||
return; | ||
} | ||
final_transcript = ''; | ||
recognition.lang = 'en-US'; | ||
recognition.start(); | ||
ignore_onend = false; | ||
final_span.innerHTML = ''; | ||
interim_span.innerHTML = ''; | ||
start_img.src = 'mic-slash.gif'; | ||
showInfo('info_allow'); | ||
} | ||
|
||
function showInfo(s) { | ||
if (s) { | ||
for (var child = info.firstChild; child; child = child.nextSibling) { | ||
if (child.style) { | ||
child.style.display = child.id == s ? 'inline' : 'none'; | ||
} | ||
} | ||
info.style.visibility = 'visible'; | ||
} else { | ||
info.style.visibility = 'hidden'; | ||
} | ||
} | ||
</script> |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.