Last reviewed: 3/23/2024 10:19:42 AM

C++Builder Android Applications

Develop Android applications that speak and listen using your favorite version of C++Builder.

The following sections describe the steps for integrating SpeechKit with C++Builder Android applications.

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 13\Android\lib\speechkit.jar and
  2. Program Files\Chant\SpeechKit 13\Android\lib\chant.shared.jar.

Azure Jar

For Azure Speech, right click the Libraries folder in your Android Target Platform and select the Add... option to add the Azure Speech SDK classes to your project:

  1. Program Files\Chant\SpeechKit 13\Android\client-sdk-1.31.0\jars\classes.jar.

SpeechKit Units

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 13\Delphi\source.
  3. Add Chant.SpeechKit.Android.pas and Chant.SpeechKit.Bridge.pas to the project.

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 13\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"

private:		// User declarations
    CSpeechKit* _SpeechKit;
    CChantRecognizer* _Recognizer; // Platform default - Android Speech
    //CMCSRecognizer* _Recognizer; // Azure Speech
    CChantSynthesizer* _Synthesizer; // Platform default - Android Speech
    //CMCSSynthesizer* _Synthesizer; // Azure Speech
};
// Event handlers
void Recognition(void* Sender, CRecognitionDictationEventArgs* Args);
void InitComplete(void* Sender, CTTSEventArgs* Args);

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


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

Azure Deployment Libraries

Add libraries to deployment under Project->Deployment:

  1. Select All configurations 32-bit platform
  2. Add all the applicable SDK files (e.g., C:\Program Files\Chant\SpeechKit 13\Android\client-sdk-1.31.0\jni\armeabi-v7a\libMicrosoft.CognitiveServices.Speech.core.so)
  3. Set the Remote Path for each SDK File added (e.g., library\lib\armeabi-v7a\)

Object Instantiation

Instantiate SpeechKit and set the credentials. For speech recognition, verify permissions, instantiate object, set callback event handler, and set application context. For speech synthesis, instantiate object, set callback event handler, and set application context.


// Instantiate SpeechKit object
_SpeechKit = new CSpeechKit();
if (_SpeechKit =! NULL)
{
    // Set credentials
    _SpeechKit->SetCredentials("Credentials");

    // Get permission
    DynamicArray<String> permissions;
    permissions.Length = 1;
    permissions[0] = JStringToString(TJManifest_permission::JavaClass->RECORD_AUDIO);
    PermissionsService()->RequestPermissions(permissions,
        [this](const DynamicArray<String> APermissions, const DynamicArray<TPermissionStatus> AGrantResults)
        {
            // 1 permissions involved: RECORD_AUDIO
            bool LocationPermissionGranted = ((AGrantResults.Length == 1) && (AGrantResults[0] == TPermissionStatus::Granted));
            if (LocationPermissionGranted)
            {
            Form1->InitReco();
        }
    });

    // Create synthesizer
    _Synthesizer = _SpeechKit->CreateChantSynthesizer();
    //_Synthesizer = _SpeechKit->CreateMCSSynthesizer("speechKey", "speechRegion");
    if (_Synthesizer != NULL)
    {
        // Register Event Handlers
        _Synthesizer->SetInitComplete(InitComplete);
    }
}

void __fastcall TForm1::InitReco()
{
    // Create recognizer
    _Recognizer = _SpeechKit->CreateChantRecognizer();
    //_Recognizer = _SpeechKit->CreateMCSRecognizer("speechKey", "speechRegion");

    if (_Recognizer == NULL)
    {
        // Unable to initialize, return;
        return;
    }
    // Register for recognition events
    _Recognizer->SetRecognitionDictation(Recognition);
    ...
}

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.

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))
    {
        ...
    }
}

void InitComplete(void* Sender, CTTSEventArgs* Args)
{
    // Enumerate synthesizers
    if (Form1->_Synthesizer->GetChantEngines() != NULL)
    {
        CChantEngine* pChantEngine = NULL;
        for (int i = 0; i < Form1->_Synthesizer->GetChantEngines()->GetCount(); i++)
        {
            pChantEngine = Form1->_Synthesizer->GetChantEngines()->GetChantEngine(i);
            // Add name to list
            Form1->ListBox1->Items->Add(pChantEngine->GetName());
        }
    }
    ...
}

Development and Deployment Checklist

When developing and deploying C++Builder Android applications, ensure you have a valid license from Chant. See the section License for more information about licensing Chant software.

Sample Projects

C++Builder Android sample projects are installed at the following location:

  • Documents\Chant\SpeechKit 13\CBuilder.