Skip to main content

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.

public class SpeechRecognition : Component, 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​

Instance member SpeechRecognition()​

Initializes a new instance of the SpeechRecognition component.

Properties​

Instance member 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.

Instance member 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.

Instance member 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 ;"
};

Instance member 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.

Instance member 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;

Instance member 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:

Methods​

Instance member 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();
}

Instance member CanExtend(target)​

Returns true if SpeechRecognition can offer an extender property to the specified target component.

ParameterTypeDescription
targetObjectThe 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;
}

Instance member 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();

Protected member Dispose(disposing)​

ParameterTypeDescription
disposingBoolean

Instance member GetSpeechRecognition(control)​

Returns the speech recognition properties associated with the specified control.

ParameterTypeDescription
controlControlThe Control for which to retrieve the speech properties.

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:

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;

Protected member OnError(e)​

Fires the Error event.

ParameterTypeDescription
eSpeechRecognitionEventArgsA SpeechRecognitionEventArgs that contains the event data.

Protected member OnNoMatch(e)​

Fires the NoMatch event.

ParameterTypeDescription
eEventArgs

Protected member OnResult(e)​

Fires the Result event.

ParameterTypeDescription
eSpeechRecognitionEventArgsA SpeechRecognitionEventArgs that contains the event data.

Protected member OnSpeechEnd(e)​

Fires the SpeechEnd event.

ParameterTypeDescription
eEventArgs

Protected member OnSpeechStart(e)​

Fires the SpeechStart event.

ParameterTypeDescription
eEventArgs

Protected member OnWebEvent(e)​

Processes the event from the client.

ParameterTypeDescription
eWisejEventArgsEvent arguments.

Protected member OnWebRender(config)​

Renders the client component.

ParameterTypeDescription
configObjectDynamic configuration object.

Instance member SetSpeechRecognition(control, properties)​

Assigns the speech recognition properties to the control.

ParameterTypeDescription
controlControlThe control that receives the recognized text.
propertiesPropertiesAn instance of Properties defining the speech listeners.

Throws:

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
});

Instance member 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();
}

Instance member 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​

Instance member Error​

SpeechRecognitionEventHandler Occurs when a speech recognition error is detected.

Instance member NoMatch​

EventHandler Occurs when the speech recognition service returns a final result with no significant recognition (when the nomatch event fires.)

Instance member 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.)

Instance member SpeechEnd​

EventHandler Occurs when speech recognised by the speech recognition service has stopped being detected (when the speechend event fires.)

Instance member SpeechStart​

EventHandler Occurs when sound recognised by the speech recognition service as speech has been detected.

Implements​