How do I voice-enable Android and iOS applications with C++Builder?

Last reviewed: 10/1/2020

HOW Article ID: H052013

The information in this article applies to:

  • SpeechKit 9

Summary

Voice enabling C++Builder Android and iOS apps is easier with SpeechKit classes for managing speech recognition and speech synthesis.

More Information

Develop C++ applications that speak and listen using your favorite version of C++Builder. SpeechKit includes C++ header files comprised of the C++Builder classes that manage recognizers and synthesizers. These are common classes, methods, and events across all platforms.

SpeechKit Header files

SpeechKit includes C++ header files comprised of the C++Builder classes that manage speech recognition and speech synthesis.

To access the SpeechKit C++Builder classes within your application, first add a project reference to the C++Builder header include files:

  • Within your C++Builder project, select Project Options.
  • Select the C++Builder compiler options.
  • Add Include path reference to the SpeechKit header files directory: C:\Program Files\Chant\SpeechKit 9\CBuilder\include.

To access the SpeechKit C++Builder classes within your application, add a reference to the Chant shared and SpeechKit header files in your C++Builder application header file. Add event handler declarations and object instance variables to the application declarations.


#include "Chant.Shared.h"
#include "Chant.SpeechKit.h"

Add a reference in your C++Builder application source file to access SpeechKit.


#include "Chant.Shared.cpp"
#include "Chant.SpeechKit.cpp"

SpeechKit Instantiation

Instantiate SpeechKit and set the license properties.


// Instantiate SpeechKit object
_SpeechKit = new CSpeechKit();
if (_SpeechKit =! NULL)
{
	// Set license properties
	//_SpeechKit->SetLicense("LicenseRegistrationNumber", "LicenseSerialNumber");
	// Else, for evaluation, set only evaluation serial number
	_SpeechKit->SetLicense("", "EvaluationSerialNumber");
}

Event Callbacks

Event callbacks are the mechanism in which the class object sends information back to the application such as speech recognition occurred, audio playback finished, or there was an error.


void Recognition(void* Sender, CRecognitionDictationEventArgs* Args)
{
	if ((Args != NULL) && (Args->GetText().Length() > 0))
	{
		...
	}
}

Android Devices

The following are C++Builder project configuration and application implementation considerations for targeting Android devices.

SpeechKit Jars

SpeechKit includes Android compatible jar libraries required by the SpeechKit C++Builder classes.

Right click the Libraries folder in your Android Target Platform and select the Add... option to add the following SpeechKit jars to your project:

  1. Program Files\Chant\SpeechKit 9\Android\lib\speechkit.jar and
  2. Program Files\Chant\SpeechKit 9\Android\lib\chant-shared.jar.

SpeechKit Unit

SpeechKit includes Object Pascal source files Chant.SpeechKit.Android.pas and Chant.SpeechKit.Bridge.pas that serve as a bridge for the C++Builder class to communicate with Android.

Add the SpeechKit Object Pascal classes to your application project:

  1. Within your C++Builder project, select Project->Add to Project.
  2. Navigate to C:\Program Files\Chant\SpeechKit 9\Delphi\source.
  3. Add Chant.SpeechKit.Android.pas and Chant.SpeechKit.Bridge.pas to the project.

Permissions

Speech recognition requires the user to grant RECORD_AUDIO permission. Add the appropriate permission in the manifest file by selecting Project->Options->Application->Uses Permission and setting Record audio to true.

Speech synthesis streamed to a file requires WRITE_EXTERNAL_STORAGE permission. Add the appropriate permission in the manifest file by selecting Project->Options->Application->Uses Permission and setting Write external storage to true.

iOS Devices

The following are C++Builder project configuration and application implementation considerations for targeting iOS devices.

SpeechKit Static Library

Add the SpeechKit iOS compatible static library to your application project:

  1. Within your C++Builder project, select Project->Add to Project.
  2. Navigate to C:\Program Files\Chant\SpeechKit 9\iOS\lib.
  3. Add libChantSpeechKit.a to the project.

Add static library linking options to your project by selecting Project->Options->Building->C++ Linker->Advanced->Additional Options->Value from "All configurations - iOS Device 64-bit":

-ObjC -bequiet

SpeechKit Unit

SpeechKit includes Object Pascal source files iOSapiChantSpeechKit.pas and Chant.SpeechKit.Bridge.pas that serve as a bridge for the C++Builder class to communicate with iOS.

Add the SpeechKit Object Pascal classes to your application project:

  1. Within your C++Builder project, select Project->Add to Project.
  2. Navigate to C:\Program Files\Chant\SpeechKit 9\Delphi\source.
  3. Add iOSapi.ChantSpeechKit.pas and Chant.SpeechKit.Bridge.pas to the project.

Permissions

Speech recognition requires the user to grant permission for speech recognition and microphone usage. Add the appropriate permission via version string key values by selecting Project->Options->VerionInfo and setting string values for NSSpeechRecognitionUsageDescription and NSMicrophoneUsageDescription.

iOS Frameworks

Verify that you have the necessary iOS development prerequisites and configured them for development per the RAD Studio C++Builder documentation.

There may be additional iOS frameworks required by the iOS for an application to use Speech Recognition. To verify or add them to your SDK, select Tools->Options->Deployment->SDK Manager->iOS Device 64-bit.

  1. Scroll through the list to verify or add the following Frameworks to your project:
    • Speech
    • CoreMIDI
    • ImageIO
    • MediaToolbox
    • AudioToolbox
    • CoreAudio
    • IOSurface
  2. Click the Update Local File Cache button to bring them from your device into the C++Builder environment.
  3. Click the Save button to save changes to the environment.
The required Frameworks may change over time as iOS changes. The linker displays messages for missing frameworks.