Skip to content
This repository has been archived by the owner on Apr 23, 2021. It is now read-only.

enhancements patch #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions webspeechdemo/webspeechdemo.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
background-color:transparent;
padding: 0;
}
#final_span[contenteditable]:focus { outline: 0px solid transparent; }
</style>
<h1 class="center" id="headline">
<a href="http://dvcs.w3.org/hg/speech-api/raw-file/tip/speechapi.html">
Expand Down Expand Up @@ -106,9 +107,8 @@ <h1 class="center" id="headline">
<img id="start_img" src="mic.gif" alt="Start"></button>
</div>
<div id="results">
<span id="final_span" class="final"></span>
<span id="final_span" class="final" contenteditable="true"></span>
<span id="interim_span" class="interim"></span>
<p>
</div>
<div class="center">
<div class="sidebyside" style="text-align:right">
Expand Down Expand Up @@ -197,7 +197,6 @@ <h1 class="center" id="headline">
['yue-Hant-HK', '粵語 (香港)']],
['日本語', ['ja-JP']],
['Lingua latīna', ['la']]];

for (var i = 0; i < langs.length; i++) {
select_language.options[i] = new Option(langs[i][0], i);
}
Expand All @@ -211,12 +210,20 @@ <h1 class="center" id="headline">
select_dialect.remove(i);
}
var list = langs[select_language.selectedIndex];
for (var i = 1; i < list.length; i++) {
select_dialect.options.add(new Option(list[i][1], list[i][0]));
for (var j = 1; j < list.length; j++) {
select_dialect.options.add(new Option(list[j][1], list[j][0]));
}
select_dialect.style.visibility = list[1].length == 1 ? 'hidden' : 'visible';
if(recognizing){
ignore_onend = true;
recognition.stop();
setTimeout(function(){
recognition.lang = select_dialect.value;
ignore_onend = false;
recognition.start();
},500);
}
}

var create_email = false;
var final_transcript = '';
var recognizing = false;
Expand All @@ -229,13 +236,11 @@ <h1 class="center" id="headline">
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';
Expand All @@ -256,7 +261,6 @@ <h1 class="center" id="headline">
ignore_onend = true;
}
};

recognition.onend = function() {
recognizing = false;
if (ignore_onend) {
Expand All @@ -279,41 +283,38 @@ <h1 class="center" id="headline">
createEmail();
}
};

recognition.onresult = function(event) {
var interim_transcript = '';
var new_final_transcript = '';
for (var i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
final_transcript += event.results[i][0].transcript;
new_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);
//console.log(new_final_transcript);
final_transcript = capitalize(new_final_transcript);
final_span.innerHTML = final_span.innerHTML + linebreak(final_transcript);
interim_span.innerHTML = linebreak(interim_transcript);
if (final_transcript || interim_transcript) {
showButtons('inline-block');
}
};
}

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 createEmail() {
var n = final_transcript.indexOf('\n');
if (n < 0 || n >= 80) {
Expand All @@ -323,7 +324,6 @@ <h1 class="center" id="headline">
var body = encodeURI(final_transcript.substring(n + 1));
window.location.href = 'mailto:?subject=' + subject + '&body=' + body;
}

function copyButton() {
if (recognizing) {
recognizing = false;
Expand All @@ -333,7 +333,6 @@ <h1 class="center" id="headline">
copy_info.style.display = 'inline-block';
showInfo('');
}

function emailButton() {
if (recognizing) {
create_email = true;
Expand All @@ -346,7 +345,6 @@ <h1 class="center" id="headline">
email_info.style.display = 'inline-block';
showInfo('');
}

function startButton(event) {
if (recognizing) {
recognition.stop();
Expand All @@ -356,14 +354,13 @@ <h1 class="center" id="headline">
recognition.lang = select_dialect.value;
recognition.start();
ignore_onend = false;
final_span.innerHTML = '';
//final_span.innerHTML = '';
interim_span.innerHTML = '';
start_img.src = 'mic-slash.gif';
showInfo('info_allow');
showButtons('none');
start_timestamp = event.timeStamp;
}

function showInfo(s) {
if (s) {
for (var child = info.firstChild; child; child = child.nextSibling) {
Expand All @@ -376,7 +373,6 @@ <h1 class="center" id="headline">
info.style.visibility = 'hidden';
}
}

var current_style;
function showButtons(style) {
if (style == current_style) {
Expand All @@ -388,4 +384,4 @@ <h1 class="center" id="headline">
copy_info.style.display = 'none';
email_info.style.display = 'none';
}
</script>
</script>