Skip to content

Commit bad1722

Browse files
Update the on-device speech recognition methods (#143)
* Rewrite the on-device speech recognition methods * Rewrite the on-device speech recognition methods * Rewrite the on-device speech recognition methods * Rewrite the on-device speech recognition methods * Rewrite the on-device speech recognition methods * Rewrite the on-device speech recognition methods --------- Co-authored-by: Evan Liu <evliu@google.com>
1 parent c0694cb commit bad1722

1 file changed

Lines changed: 47 additions & 5 deletions

File tree

index.bs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ interface SpeechRecognition : EventTarget {
168168
undefined start(MediaStreamTrack audioTrack);
169169
undefined stop();
170170
undefined abort();
171-
static Promise<boolean> availableOnDevice(DOMString lang);
171+
static Promise<AvailabilityStatus> availableOnDevice(DOMString lang);
172172
static Promise<boolean> installOnDevice(DOMString lang);
173173

174174
// event methods
@@ -201,6 +201,13 @@ enum SpeechRecognitionMode {
201201
"cloud-only", // Cloud speech recognition only.
202202
};
203203

204+
enum AvailabilityStatus {
205+
"unavailable",
206+
"downloadable",
207+
"downloading",
208+
"available"
209+
};
210+
204211
[Exposed=Window]
205212
interface SpeechRecognitionErrorEvent : Event {
206213
constructor(DOMString type, SpeechRecognitionErrorEventInit eventInitDict);
@@ -255,7 +262,7 @@ dictionary SpeechRecognitionEventInit : EventInit {
255262

256263
<dt><dfn attribute for=SpeechRecognition>lang</dfn> attribute</dt>
257264
<dd>This attribute will set the language of the recognition for the request, using a valid BCP 47 language tag. [[!BCP47]]
258-
If unset it remains unset for getting in script, but will default to use the <a spec=html>language</a> of the html document root element and associated hierarchy.
265+
If unset it remains unset for getting in script, but will default to use the language of the html document root element and associated hierarchy.
259266
This default value is computed and used when the input request opens a connection to the recognition service.</dd>
260267

261268
<dt><dfn attribute for=SpeechRecognition>continuous</dfn> attribute</dt>
@@ -323,12 +330,47 @@ See <a href="https://lists.w3.org/Archives/Public/public-speech-api/2012Sep/0072
323330
If the abort method is called on an object which is already stopped or aborting (that is, start was never called on it, the <a event for=SpeechRecognition>end</a> or <a event for=SpeechRecognition>error</a> event has fired on it, or abort was previously called on it), the user agent must ignore the call.</dd>
324331

325332
<dt><dfn method for=SpeechRecognition>availableOnDevice({{DOMString}} lang)</dfn> method</dt>
326-
<dd>The availableOnDevice method returns a Promise that resolves to a boolean indicating whether on-device speech recognition is available for a given BCP 47 language tag. [[!BCP47]]</dd>
333+
<dd>
334+
The {{SpeechRecognition/availableOnDevice}} method returns a {{Promise}} that resolves to a {{AvailabilityStatus}} indicating the on-device speech recognition availability for a given [[!BCP47]] language tag.
335+
336+
When invoked, run these steps:
337+
1. Let <var>promise</var> be <a>a new promise</a>.
338+
1. Run the <a>on-device availability algorithm</a> with <var>lang</var> and <var>promise</var>. If it returns an exception, throw it and abort these steps.
339+
1. Return <var>promise</var>.
340+
</dd>
327341

328342
<dt><dfn method for=SpeechRecognition>installOnDevice({{DOMString}} lang)</dfn> method</dt>
329-
<dd>The installOnDevice method returns a Promise that resolves to a boolean indicating whether the installation of on-device speech recognition for a given BCP 47 language tag initiated successfully. [[!BCP47]]</dd>
343+
<dd>
344+
The {{SpeechRecognition/installOnDevice}} method returns a {{Promise}} that resolves to a {{boolean}} when and whether the installation of on-device speech recognition for a given [[!BCP47]] language tag succeeded.
345+
346+
When invoked, run these steps:
347+
1. If the [=current settings object=]'s [=relevant global object=]'s [=associated Document=] is NOT [=fully active=], throw an {{InvalidStateError}} and abort these steps.
348+
1. If <var>lang</var> is not a valid [[!BCP47]] language tag, throw a {{SyntaxError}} and abort these steps.
349+
1. If the on-device speech recognition language pack for <var>lang</var> is unsupported, return a resolved {{Promise}} with false and skip the rest of these steps.
350+
1. Let <var>promise</var> be <a>a new promise</a>.
351+
1. Initiate the download of the on-device speech recognition language for <var>lang</var>.
352+
<p class=note>
353+
Note: The user agent can prompt the user for explicit permission to download the on-device speech recognition language pack.
354+
</p>
355+
1. [=Queue a task=] on the [=relevant global object=]'s [=task queue=] to run the following step:
356+
- If the download succeeds, resolve <var>promise</var> with <code>true</code>, otherwise resolve it with <code>false</code>.
357+
<p class="note">
358+
Note: The <code>false</code> resolution of the Promise does not indicate the specific cause of failure. User agents are encouraged to provide more detailed information about the failure in developer tools console messages. However, this detailed error information is not exposed to the script.
359+
</p>
360+
1. Return <var>promise</var>.
361+
</dd>
330362

331363
</dl>
364+
<p>When the <dfn>on-device availability algorithm</dfn> with <var>lang</var> and <var>promise</var> is invoked, the user agent MUST run the following steps:
365+
1. If the [=current settings object=]'s [=relevant global object=]'s [=associated Document=] is NOT [=fully active=], throw an {{InvalidStateError}} and abort these steps.
366+
1. If <var>lang</var> is not a valid [[!BCP47]] language tag, throw a {{SyntaxError}} and abort these steps.
367+
1. Determine the availability status for <var>lang</var>:
368+
- If the on-device speech recognition language pack for <var>lang</var> is unsupported, let <var>status</var> be {{AvailabilityStatus/unavailable}}.
369+
- Else if the on-device speech recognition language pack for <var>lang</var> is supported but not installed, let <var>status</var> be {{AvailabilityStatus/downloadable}}.
370+
- Else if the on-device speech recognition language pack for <var>lang</var> is downloading, let <var>status</var> be {{AvailabilityStatus/downloading}}.
371+
- Else if the on-device speech recognition language pack for <var>lang</var> is installed, let <var>status</var> be {{AvailabilityStatus/available}}.
372+
1. [=Queue a task=] on the [=relevant global object=]'s [=task queue=] to run the following step:
373+
- Resolve <var>promise</var> with <var>status</var>.
332374

333375
When the <dfn>start session algorithm</dfn> with
334376
|requestMicrophonePermission| is invoked, the user agent MUST run the
@@ -685,7 +727,7 @@ interface SpeechSynthesisVoice {
685727

686728
<dt><dfn attribute for=SpeechSynthesisUtterance>lang</dfn> attribute</dt>
687729
<dd>This attribute specifies the language of the speech synthesis for the utterance, using a valid BCP 47 language tag. [[!BCP47]]
688-
If unset it remains unset for getting in script, but will default to use the <a spec=html>language</a> of the html document root element and associated hierarchy.
730+
If unset it remains unset for getting in script, but will default to use the language of the html document root element and associated hierarchy.
689731
This default value is computed and used when the input request opens a connection to the recognition service.</dd>
690732

691733
<dt><dfn attribute for=SpeechSynthesisUtterance>voice</dfn> attribute</dt>

0 commit comments

Comments
 (0)