SpeechRecognition
Namespace: Wisej.Web.Ext.Speech
Assembly: Wisej.Web.Ext.Speech (4.1.0.0)
Represents a component that uses the SpeechRecognition interface of the Web Speech API to capture the browser's audio stream and convert it to text. It also extends TextBoxBase controls to enable dictation.
- C#
- VB.NET
public class SpeechRecognition : Component, IExtenderProvider
Public Class SpeechRecognition
Inherits Component
Implements IExtenderProvider
Speech recognition runs in the browser and requires a browser that supports the Web Speech API (i.e. Chrome, Edge, Safari), a secure (HTTPS) connection and the user's permission to use the microphone. When the browser doesn't support it, the component does nothing.
Set Enabled to true to listen continuously (the recognition service is restarted automatically every time it ends), or call Start to listen for a single session. Recognized text is returned by the Result event and, for the extended text controls that have the focus, assigned to their Text property.
Example:
Enabling dictation in a text box and handling the results:
var speech = new SpeechRecognition { Language = "en-US" };
speech.GetSpeechRecognition(this.textBoxNotes).Enabled = true;
speech.GetSpeechRecognition(this.textBoxNotes).RecognitionMode = SpeechRecognition.RecognitionMode.WhenFocused;
speech.Result += (s, e) =>
{
foreach (var result in e.Results)
Application.Session.LastTranscript = result.Transcript;
};
speech.Error += (s, e) => AlertBox.Show(e.Error, MessageBoxIcon.Error);
speech.Enabled = true;
Constructors
SpeechRecognition()
Initializes a new instance of the SpeechRecognition component.
Properties
Continuous
Boolean: Returns or sets whether continuous results are returned for each recognition, or only a single result. (Default: False)
When false (default), the recognition session ends after the first final result. When true, the browser keeps listening and fires a Result event for each recognized phrase until Stop is called. The value is used when the recognition session starts.
Enabled
Boolean: Returns or sets whether the SpeechRecognition component is listening. (Default: False)
Setting this property to true starts the recognition service and restarts it automatically every time it ends, until the property is set back to false (which calls Stop on the client). The default is false. The browser asks the user for permission to use the microphone the first time.
Grammars
String[]: Returns or sets a collection of grammar definitions - using the JSpeech Grammar Format (JSGF) https://www.w3.org/TR/jsgf/.
Each string is a complete JSGF grammar, added to the browser's grammar list with weight 1. Grammars are only hints and many browsers ignore them.
Example:
Limiting the recognition to a list of colors:
this.speechRecognition1.Grammars = new[]
{
"#JSGF V1.0; grammar colors; public <color> = red | green | blue | yellow ;"
};
InterimResults
Boolean: Returns or sets whether interim results should be returned (true) or not (false.) Interim results are results that are not yet final (e.g. the IsFinal property is false.) (Default: False)
Interim results are delivered only to the Result event handlers; the extended text controls are updated only with final results.
Language
String: Returns or sets the language of the current SpeechRecognition. If not specified, this defaults to the HTML lang attribute value, or the user agent's language setting if that isn't set either. (Default: null)
The value is a BCP 47 language tag, i.e. "en-US", "de-DE", "it-IT". An empty string is converted to null.
Example:
Recognizing the language of the current session culture:
this.speechRecognition1.Language = Application.CurrentCulture.Name;
MaxAlternatives
Int32: Returns or sets the maximum number of alternatives provided per each speech recognition result. (Default: 1)
The default is 1. Each alternative is returned as a separate SpeechRecognitionResult in Results, with its own Confidence.
Throws:
- ArgumentOutOfRangeException The value is less than 0 or greater than 10.
Methods
Abort()
Stops the speech recognition service from listening to incoming audio, and doesn't attempt to return a result.
If Enabled is true, the service is restarted automatically; set Enabled to false to stop listening.
Example:
Discarding the current recognition:
private void buttonCancel_Click(object sender, EventArgs e)
{
this.speechRecognition1.Abort();
}
CanExtend(target)
Returns true if SpeechRecognition can offer an extender property to the specified target component.
| Parameter | Type | Description |
|---|---|---|
| target | Object | The target object to add an extender property to. |
Returns: Boolean. true if the SpeechRecognition class can offer one or more extender properties; otherwise, false.
Only controls derived from TextBoxBase can be extended.
Example:
Enabling dictation in all the text boxes of a form:
foreach (Control control in this.Controls)
{
if (this.speechRecognition1.CanExtend(control))
this.speechRecognition1.GetSpeechRecognition(control).Enabled = true;
}
Clear()
Removes all speech extenders.
After this call the recognized text is no longer assigned to any control; the Result event is still fired.
Example:
Disabling dictation in all controls:
this.speechRecognition1.Clear();
Dispose(disposing)
| Parameter | Type | Description |
|---|---|---|
| disposing | Boolean |
GetSpeechRecognition(control)
Returns the speech recognition properties associated with the specified control.
Returns: Properties. A Properties instance with the SpeechRecognition properties.
The properties are created the first time they are requested for a control and are removed automatically when the control is disposed.
Throws:
- ArgumentNullException control is null.
Example:
Filling a text box with the next phrase spoken while it has the focus:
var props = this.speechRecognition1.GetSpeechRecognition(this.textBoxSearch);
props.RecognitionMode = SpeechRecognition.RecognitionMode.WhenFocusedOnce;
props.Enabled = true;
this.speechRecognition1.Enabled = true;
OnError(e)
Fires the Error event.
| Parameter | Type | Description |
|---|---|---|
| e | SpeechRecognitionEventArgs | A SpeechRecognitionEventArgs that contains the event data. |
OnNoMatch(e)
Fires the NoMatch event.
| Parameter | Type | Description |
|---|---|---|
| e | EventArgs |
OnResult(e)
Fires the Result event.
| Parameter | Type | Description |
|---|---|---|
| e | SpeechRecognitionEventArgs | A SpeechRecognitionEventArgs that contains the event data. |
OnSpeechEnd(e)
Fires the SpeechEnd event.
| Parameter | Type | Description |
|---|---|---|
| e | EventArgs |
OnSpeechStart(e)
Fires the SpeechStart event.
| Parameter | Type | Description |
|---|---|---|
| e | EventArgs |
OnWebEvent(e)
Processes the event from the client.
| Parameter | Type | Description |
|---|---|---|
| e | WisejEventArgs | Event arguments. |
OnWebRender(config)
Renders the client component.
| Parameter | Type | Description |
|---|---|---|
| config | Object | Dynamic configuration object. |
SetSpeechRecognition(control, properties)
Assigns the speech recognition properties to the control.
| Parameter | Type | Description |
|---|---|---|
| control | Control | The control that receives the recognized text. |
| properties | Properties | An instance of Properties defining the speech listeners. |
Throws:
- ArgumentNullException control or properties is null.
Example:
Assigning a new set of properties to a text box:
this.speechRecognition1.SetSpeechRecognition(this.textBoxNotes, new SpeechRecognition.Properties
{
Enabled = true,
RecognitionMode = SpeechRecognition.RecognitionMode.WhenFocused
});
Start()
Starts the speech recognition service listening to incoming audio.
The call is ignored if the service is already listening. Unless Enabled is true, the session ends after the first result (or after the silence timeout) when Continuous is false.
Example:
Listening while a button is pressed:
private void buttonMic_MouseDown(object sender, MouseEventArgs e)
{
this.speechRecognition1.Start();
}
private void buttonMic_MouseUp(object sender, MouseEventArgs e)
{
this.speechRecognition1.Stop();
}
Stop()
Stops the speech recognition service from listening to incoming audio, and attempts to return a result using the audio captured so far.
If Enabled is true, the service is restarted automatically; set Enabled to false to stop listening.
Example:
Stopping and processing the audio captured so far:
private void buttonDone_Click(object sender, EventArgs e)
{
this.speechRecognition1.Stop();
}
Events
Error
SpeechRecognitionEventHandler Occurs when a speech recognition error is detected.
NoMatch
EventHandler Occurs when the speech recognition service returns a final result with no significant recognition (when the nomatch event fires.)
Result
SpeechRecognitionEventHandler Fired when the speech recognition service returns a result — a word or phrase has been positively recognized and this has been communicated back to the app (when the result event fires.)
SpeechEnd
EventHandler Occurs when speech recognised by the speech recognition service has stopped being detected (when the speechend event fires.)
SpeechStart
EventHandler Occurs when sound recognised by the speech recognition service as speech has been detected.
Implements
| Name | Description |
|---|---|
| IUserData | |
| IWisejComponent | |
| IWisejSerializable |