mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-22 20:37:58 +08:00
153
webspeechapi/webspeechapi-tests.ts
Normal file
153
webspeechapi/webspeechapi-tests.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
///<reference path="webspeechapi.d.ts" />
|
||||
|
||||
/*
|
||||
Examples from the spec:
|
||||
https://dvcs.w3.org/hg/speech-api/raw-file/tip/speechapi.html#examples-recognition
|
||||
*/
|
||||
|
||||
// 6.1 Speech Recognition Examples
|
||||
|
||||
// Example 1
|
||||
declare var q: HTMLInputElement;
|
||||
() => {
|
||||
var recognition = new SpeechRecognition();
|
||||
recognition.onresult = function (event) {
|
||||
if (event.results.length > 0) {
|
||||
q.value = event.results[0][0].transcript;
|
||||
q.form.submit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Example 2
|
||||
declare var select: HTMLSelectElement;
|
||||
() => {
|
||||
var recognition = new SpeechRecognition();
|
||||
recognition.maxAlternatives = 10;
|
||||
recognition.onresult = function (event) {
|
||||
if (event.results.length > 0) {
|
||||
var result = event.results[0];
|
||||
for (var i = 0; i < result.length; ++i) {
|
||||
var text = result[i].transcript;
|
||||
select.options[i] = new Option(text, text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function start() {
|
||||
select.options.length = 0;
|
||||
recognition.start();
|
||||
}
|
||||
}
|
||||
|
||||
// Example 3
|
||||
/*
|
||||
This example has some changes from the one in spec.
|
||||
`var i = resultIndex` -> `var i = event.resultIndex` (Recorded as Errata 16)
|
||||
`event.results.final` -> `event.results[i].isFinal` (Recorded as Errata 02)
|
||||
*/
|
||||
declare var textarea: HTMLTextAreaElement;
|
||||
declare var button: HTMLButtonElement;
|
||||
() => {
|
||||
var recognizing: boolean;
|
||||
var recognition = new SpeechRecognition();
|
||||
recognition.continuous = true;
|
||||
reset();
|
||||
recognition.onend = reset;
|
||||
|
||||
recognition.onresult = function (event) {
|
||||
for (var i = event.resultIndex; i < event.results.length; ++i) {
|
||||
if (event.results[i].isFinal) {
|
||||
textarea.value += event.results[i][0].transcript;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function reset() {
|
||||
recognizing = false;
|
||||
button.innerHTML = "Click to Speak";
|
||||
}
|
||||
|
||||
function toggleStartStop() {
|
||||
if (recognizing) {
|
||||
recognition.stop();
|
||||
reset();
|
||||
} else {
|
||||
recognition.start();
|
||||
recognizing = true;
|
||||
button.innerHTML = "Click to Stop";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Example 4
|
||||
/*
|
||||
This example has a change from the one in spec.
|
||||
`recognition.interim = true;` -> `recognition.interimResults = true;` (Recorded as Errata 01)
|
||||
`event.results[i].final` -> `event.results[i].isFinal` (Recorded as Errata 02)
|
||||
*/
|
||||
declare var button: HTMLButtonElement;
|
||||
declare var final_span: HTMLSpanElement;
|
||||
declare var interim_span: HTMLSpanElement;
|
||||
() => {
|
||||
var recognizing: boolean;
|
||||
var recognition = new SpeechRecognition();
|
||||
recognition.continuous = true;
|
||||
recognition.interimResults = true;
|
||||
reset();
|
||||
recognition.onend = reset;
|
||||
|
||||
recognition.onresult = function (event) {
|
||||
var final = "";
|
||||
var interim = "";
|
||||
for (var i = 0; i < event.results.length; ++i) {
|
||||
if (event.results[i].isFinal) {
|
||||
final += event.results[i][0].transcript;
|
||||
} else {
|
||||
interim += event.results[i][0].transcript;
|
||||
}
|
||||
}
|
||||
final_span.innerHTML = final;
|
||||
interim_span.innerHTML = interim;
|
||||
}
|
||||
|
||||
function reset() {
|
||||
recognizing = false;
|
||||
button.innerHTML = "Click to Speak";
|
||||
}
|
||||
|
||||
function toggleStartStop() {
|
||||
if (recognizing) {
|
||||
recognition.stop();
|
||||
reset();
|
||||
} else {
|
||||
recognition.start();
|
||||
recognizing = true;
|
||||
button.innerHTML = "Click to Stop";
|
||||
final_span.innerHTML = "";
|
||||
interim_span.innerHTML = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 6.2 Speech Synthesis Examples
|
||||
|
||||
// Example 1
|
||||
/*
|
||||
This example has a change from the one in spec.
|
||||
`SpeechSynthesisUtterance('Hello World')` -> `new SpeechSynthesisUtterance('Hello World')` (Recorded as Errata 09)
|
||||
*/
|
||||
() => {
|
||||
speechSynthesis.speak(new SpeechSynthesisUtterance('Hello World'));
|
||||
}
|
||||
|
||||
//Example 2
|
||||
() => {
|
||||
var u = new SpeechSynthesisUtterance();
|
||||
u.text = 'Hello World';
|
||||
u.lang = 'en-US';
|
||||
u.rate = 1.2;
|
||||
u.onend = function (event) { alert('Finished in ' + event.elapsedTime + ' seconds.'); }
|
||||
speechSynthesis.speak(u);
|
||||
}
|
||||
164
webspeechapi/webspeechapi.d.ts
vendored
Normal file
164
webspeechapi/webspeechapi.d.ts
vendored
Normal file
@@ -0,0 +1,164 @@
|
||||
// Type definitions for Web Speech API
|
||||
// Project: https://dvcs.w3.org/hg/speech-api/raw-file/tip/speechapi.html
|
||||
// Definitions by: SaschaNaz <https://github.com/saschanaz>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
// Spec version: 19 October 2012
|
||||
// Errata version: 6 June 2014
|
||||
// Corrected unofficial spec version: 6 June 2014
|
||||
|
||||
interface SpeechRecognition extends EventTarget {
|
||||
grammars: SpeechGrammarList;
|
||||
lang: string;
|
||||
continuous: boolean;
|
||||
interimResults: boolean;
|
||||
maxAlternatives: number;
|
||||
serviceURI: string;
|
||||
|
||||
start(): void;
|
||||
stop(): void;
|
||||
abort(): void;
|
||||
|
||||
onaudiostart: (ev: Event) => any;
|
||||
onsoundstart: (ev: Event) => any;
|
||||
onspeechstart: (ev: Event) => any;
|
||||
onspeechend: (ev: Event) => any;
|
||||
onsoundend: (ev: Event) => any;
|
||||
onresult: (ev: SpeechRecognitionEvent) => any;
|
||||
onnomatch: (ev: SpeechRecognitionEvent) => any;
|
||||
onerror: (ev: SpeechRecognitionError) => any;
|
||||
onstart: (ev: Event) => any;
|
||||
onend: (ev: Event) => any;
|
||||
}
|
||||
interface SpeechRecognitionStatic {
|
||||
prototype: SpeechRecognition;
|
||||
new (): SpeechRecognition;
|
||||
}
|
||||
declare var SpeechRecognition: SpeechRecognitionStatic;
|
||||
declare var webkitSpeechRecognition: SpeechRecognitionStatic;
|
||||
|
||||
interface SpeechRecognitionError extends Event {
|
||||
error: string;
|
||||
message: string;
|
||||
}
|
||||
|
||||
interface SpeechRecognitionAlternative {
|
||||
transcript: string;
|
||||
confidence: number;
|
||||
}
|
||||
|
||||
interface SpeechRecognitionResult {
|
||||
length: number;
|
||||
item(index: number): SpeechRecognitionAlternative;
|
||||
[index: number]: SpeechRecognitionAlternative;
|
||||
/* Errata 02 */
|
||||
isFinal: boolean;
|
||||
}
|
||||
|
||||
interface SpeechRecognitionResultList {
|
||||
length: number;
|
||||
item(index: number): SpeechRecognitionResult;
|
||||
[index: number]: SpeechRecognitionResult;
|
||||
}
|
||||
|
||||
interface SpeechRecognitionEvent extends Event {
|
||||
resultIndex: number;
|
||||
results: SpeechRecognitionResultList;
|
||||
interpretation: any;
|
||||
emma: Document;
|
||||
}
|
||||
|
||||
interface SpeechGrammar {
|
||||
src: string;
|
||||
weight: number;
|
||||
}
|
||||
interface SpeechGrammarStatic {
|
||||
prototype: SpeechGrammar;
|
||||
new (): SpeechGrammar;
|
||||
}
|
||||
declare var SpeechGrammar: SpeechGrammarStatic;
|
||||
declare var webkitSpeechGrammar: SpeechGrammarStatic;
|
||||
|
||||
interface SpeechGrammarList {
|
||||
length: number;
|
||||
item(index: number): SpeechGrammar;
|
||||
[index: number]: SpeechGrammar;
|
||||
addFromURI(src: string, weight: number): void;
|
||||
addFromString(string: string, weight: number): void;
|
||||
}
|
||||
interface SpeechGrammarListStatic {
|
||||
prototype: SpeechGrammarList;
|
||||
new (): SpeechGrammarList;
|
||||
}
|
||||
declare var SpeechGrammarList: SpeechGrammarListStatic;
|
||||
declare var webkitSpeechGrammarList: SpeechGrammarListStatic;
|
||||
|
||||
/* Errata 08 */
|
||||
interface SpeechSynthesis extends EventTarget {
|
||||
pending: boolean;
|
||||
speaking: boolean;
|
||||
paused: boolean;
|
||||
|
||||
/* Errata 11 */
|
||||
onvoiceschanged: (ev: Event) => any;
|
||||
|
||||
speak(utterance: SpeechSynthesisUtterance): void;
|
||||
cancel(): void;
|
||||
pause(): void;
|
||||
resume(): void;
|
||||
/* Errata 05 */
|
||||
getVoices(): SpeechSynthesisVoice[];
|
||||
}
|
||||
|
||||
interface SpeechSynthesisGetter {
|
||||
speechSynthesis: SpeechSynthesis;
|
||||
}
|
||||
interface Window extends SpeechSynthesisGetter {
|
||||
}
|
||||
declare var speechSynthesis: SpeechSynthesis;
|
||||
|
||||
interface SpeechSynthesisUtterance extends EventTarget {
|
||||
text: string;
|
||||
lang: string;
|
||||
/* Errata 07 */
|
||||
voice: SpeechSynthesisVoice;
|
||||
volume: number;
|
||||
rate: number;
|
||||
pitch: number;
|
||||
|
||||
onstart: (ev: SpeechSynthesisEvent) => any;
|
||||
onend: (ev: SpeechSynthesisEvent) => any;
|
||||
/* Errata 12 */
|
||||
onerror: (ev: SpeechSynthesisErrorEvent) => any;
|
||||
onpause: (ev: SpeechSynthesisEvent) => any;
|
||||
onresume: (ev: SpeechSynthesisEvent) => any;
|
||||
onmark: (ev: SpeechSynthesisEvent) => any;
|
||||
onboundary: (ev: SpeechSynthesisEvent) => any;
|
||||
}
|
||||
interface SpeechSynthesisUtteranceStatic {
|
||||
prototype: SpeechSynthesisUtterance;
|
||||
new (): SpeechSynthesisUtterance;
|
||||
new (text: string): SpeechSynthesisUtterance;
|
||||
}
|
||||
declare var SpeechSynthesisUtterance: SpeechSynthesisUtteranceStatic;
|
||||
|
||||
interface SpeechSynthesisEvent extends Event {
|
||||
/* Errata 08 */
|
||||
utterance: SpeechSynthesisUtterance;
|
||||
charIndex: number;
|
||||
elapsedTime: number;
|
||||
name: string;
|
||||
}
|
||||
|
||||
/* Errata 12 */
|
||||
interface SpeechSynthesisErrorEvent extends SpeechSynthesisEvent {
|
||||
error: string;
|
||||
}
|
||||
|
||||
interface SpeechSynthesisVoice {
|
||||
voiceURI: string;
|
||||
name: string;
|
||||
lang: string;
|
||||
localService: boolean;
|
||||
default: boolean;
|
||||
}
|
||||
Reference in New Issue
Block a user