Monday, August 6, 2012

How-To: Use Text-to-Speech in an ArchestrA Symbol

Microsoft included their speech engine (SAPI, a COM-based API) in Windows Server 2000 and later versions of their operating system; a managed code version of the API was introduced with the .NET Framework 3.0. The ArchestrA scripting engine, QuickScript.NET, allows calls to both COM and .NET libraries; this and Application Server's capability to import COM and .NET files into the Galaxy makes it easy for you to use Microsoft's speech engine for Text-to-Speech (TTS) and other speech-related functions.

You can use QuickScript.NET within an automation object or in an ArchestrA Symbol. While adding TTS capabilities to an automation object is possible, it might not be a good idea since most likely the object will be deployed to a server-type node with no user physically located at the station. A better approach is to add TTS capabilities to an ArchestrA Symbol since the graphic can be made available to a user through their InTouch application.

For this article, we will focus on the .NET version of the speech engine: System.Speech.Synthesis. This engine allows access to the speech synthesis engine which provides the TTS capabilities, including the conversion of text to speech (even from and to a file) and manipulation of the voice parameters (like speed and volume).

Depending on your operating system, you might have access to only one voice. You can find other voices online (not necessarily for free) that are compatible with the Microsoft speech engine. The API allows you to query the characteristics of all the voices available, like name, gender, and age.

For our example, we will use the default voice; you'll be surprise how simple it is to get it done!

We will create a graphic that uses a Text Box to capture input from the user and convert it to spoken words.

1. Using the ArchestrA IDE, import (Galaxy | Import | Script Function Library) the speech engine library, System.Speech.dll, into your Galaxy. You will find the file in the following location:

%ProgramFiles%\Reference Assemblies\Microsoft\Framework\v3.0
For 64-bit operating systems, use the 32-bit version of the library since Application Server is a 32-bit application:

%ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\v3.0

2. Create an ArchestrA Symbol. The graphic can be created within an automation object or in the Graphic Toolbox.

3. Add a custom property and configure it as follows:
  • Name: TTS
  • Data Type: String
  • Default Value: 'Static Text' mode, empty
  • Visibility: Public (this will allow to set the value from a different source if needed)

4. Add a Text Box to the symbol and add a User Input animation to it configured as follows:
  • States: String
  • Reference: TTS
  • Use Keypad: unchecked (or checked - whatever is best for you)

5. Add a named script to the symbol and configure it as follows:
  • Name: TextToSpeech (any name will do)
  • Expression: TTS
  • Trigger: DataChange
  • Body of the script:
    Dim synth As System.Speech.Synthesis.SpeechSynthesizer;
    synth = New System.Speech.Synthesis.SpeechSynthesizer();
    synth.SpeakAsync(TTS);

The SpeechSynthesizer class has to main methods for text-to-speech: Speak() and SpeakAsync(). When working with Application Server and InTouch, always use the asynchronous method due to the real-time nature of the software.

Done! Save your work and embed the new symbol in a window in your InTouch application and take it out for a spin.

No comments:

Post a Comment