How do I develop native apps with/without platform- and API-specific code?

Last reviewed: 3/25/2023

HOW Article ID: H032304

The information in this article applies to:

  • SpeechKit 12

Summary

Explore common classes that provide platform dependent access to platform APIs.

More Information

Chant classes continue to enable apps to access API-specific services on various platforms. Apps can allocate and destroy API-specific objects as needed.


JChantRecognizer _Recognizer = null;
//JAndroidRecognizer _Recognizer = null;
JChantSynthesizer _Synthesizer = null;
//JAndroidSynthesizer _Synthesizer = null;
JSpeechKit _SpeechKit = null;
_SpeechKit = new JSpeechKit();
_SpeechKit.setCredentials("Credentials");
// Create Synthesizer
_Recognizer = _SpeechKit.createChantRecognizer();
if (_Recognizer != null) {
    // Set the callback
    _Recognizer.setChantSpeechKitEvents(this);
    // Register Callbacks
    _Recognizer.registerCallback(ChantSpeechKitCallback.CCSRInitComplete);
    _Recognizer.registerCallback(ChantSpeechKitCallback.CCSRRecognitionCommand);
    // Set app context
    _Recognizer.setContext(getApplicationContext());
}
// Create Synthesizer
_Synthesizer = _SpeechKit.createChantSynthesizer();
if (_Synthesizer != null)
{
    // Set the callback
    _Synthesizer.setChantSpeechKitEvents(this);
    // Register Callbacks for engine init
    _Synthesizer.registerCallback(ChantSpeechKitCallback.CCTTSInitComplete);
    _Synthesizer.registerCallback(ChantSpeechKitCallback.CCTTSRangeStart);
    // Set app context
    _Synthesizer.setContext(_Context);
}
        

NSpeechKit _SpeechKit = null;
NChantRecognizer _Recognizer = null;
//NDgnRecognizer _Recognizer = null;
//NMSPRecognizer _Recognizer = null;
//NSAPI5Recognizer _Recognizer = null;
//NWindowsMediaRecognizer _Recognizer = null;
NChantSynthesizer _Synthesizer = null;
//NAcaTTSSynthesizer _Synthesizer = null;
//NCereVoiceSynthesizer _Synthesizer = null;
//NMSPSynthesizer _Synthesizer = null;
//NSAPI5Synthesizer _Synthesizer = null;
//NSwiftSynthesizer _Synthesizer = null;
//NWindowsMediaSynthesizer _Synthesizer = null;
// Instantiate SpeechKit
_SpeechKit = new NSpeechKit();
if (_SpeechKit != null)
{
    // Set credentials
    _SpeechKit.SetCredentials("Credentials");
    _Recognizer = _SpeechKit.CreateChantRecognizer();
    //_Recognizer = _SpeechKit.CreateDgnRecognizer();
    //_Recognizer = _SpeechKit.CreateMSPRecognizer();
    //_Recognizer = _SpeechKit.CreateSAPI5Recognizer();
    //_Recognizer = _SpeechKit.CreateWindowsMediaRecognizer();
    if (_Recognizer != null)
    {
        _Recognizer.RecognitionCommand += this.Recognizer_RecognitionCommand;
    }
    _Synthesizer = _SpeechKit.CreateChantSynthesizer();
    //_Synthesizer = _SpeechKit.CreateAcaTTSSynthesizer();
    //_Synthesizer = _SpeechKit.CreateCereVoiceSynthesizer();
    //_Synthesizer = _SpeechKit.CreateMSPSynthesizer();
    //_Synthesizer = _SpeechKit.CreateSAPI5Synthesizer();
    //_Synthesizer = _SpeechKit.CreateSwiftSynthesizer();
    //_Synthesizer = _SpeechKit.CreateWindowsMediaSynthesizer();
    if (_Synthesizer != null)
    {
        _Synthesizer.WordPosition += Synthesizer_WordPosition;
    }
}

CSpeechKit* _SpeechKit;
CChantRecognizer* _Recognizer;
//CDgnRecognizer* _Recognizer;
//CMSPRecognizer* _Recognizer;
//CSAPI5Recognizer* _Recognizer;
//CWindowsMediaRecognizer* _Recognizer;
CChantSynthesizer* _Synthesizer;
//CAcaTTSSynthesizer* _Synthesizer;
//CCereVoiceSynthesizer* _Synthesizer;
//CMSPSynthesizer* _Synthesizer;
//CSAPI5Synthesizer* _Synthesizer;
//CSwiftSynthesizer* _Synthesizer;
//CWindowsMediaSynthesizer* _Synthesizer;
_SpeechKit = new CSpeechKit();
if (_SpeechKit =! NULL)
{
    // Set credentials
    _SpeechKit->SetCredentials(L"Credentials");
    // Create recognizer
    _Recognizer = _SpeechKit->CreateChantRecognizer();
    //_Recognizer = _SpeechKit->CreateDgnRecognizer();
    //_Recognizer = _SpeechKit->CreateMSPRecognizer();
    //_Recognizer = _SpeechKit->CreateSAPI5Recognizer();
    //_Recognizer = _SpeechKit->CreateWindowsMediaRecognizer();
    if (_Recognizer != NULL)
    {
        // Register Event Handlers
        _Recognizer->SetRecognitionCommand(RecognitionCommand);
    }
    // Create synthesizer
    _Synthesizer = _SpeechKit->CreateChantSynthesizer();
    //_Synthesizer = _SpeechKit->CreateAcaTTSSynthesizer();
    //_Synthesizer = _SpeechKit->CreateCereVoiceSynthesizer();
    //_Synthesizer = _SpeechKit->CreateMSPSynthesizer();
    //_Synthesizer = _SpeechKit->CreateSAPI5Synthesizer();
    //_Synthesizer = _SpeechKit->CreateSwiftSynthesizer();
    //_Synthesizer = _SpeechKit->CreateWindowsMediaSynthesizer();
    if (_Synthesizer != NULL)
    {
        // Register Event Handlers
        _Synthesizer->SetWordPosition(WordPosition);
    }
}
    

CSpeechKit* _SpeechKit;
CChantRecognizer* _Recognizer;
//CDgnRecognizer* _Recognizer;
//CMSPRecognizer* _Recognizer;
//CSAPI5Recognizer* _Recognizer;
//CWindowsMediaRecognizer* _Recognizer;
CChantSynthesizer* _Synthesizer;
//CAcaTTSSynthesizer* _Synthesizer;
//CCereVoiceSynthesizer* _Synthesizer;
//CMSPSynthesizer* _Synthesizer;
//CSAPI5Synthesizer* _Synthesizer;
//CSwiftSynthesizer* _Synthesizer;
//CWindowsMediaSynthesizer* _Synthesizer;
_SpeechKit = new CSpeechKit();
if (_SpeechKit =! NULL)
{
    // Set credentials
    _SpeechKit->SetCredentials("Credentials");
    // Create recognizer
    _Recognizer = _SpeechKit->CreateChantRecognizer();
    //_Recognizer = _SpeechKit->CreateDgnRecognizer();
    //_Recognizer = _SpeechKit->CreateMSPRecognizer();
    //_Recognizer = _SpeechKit->CreateSAPI5Recognizer();
    //_Recognizer = _SpeechKit->CreateWindowsMediaRecognizer();

    if (_Recognizer != NULL)
    {
        // Register Event Handlers
        _Recognizer->SetRecognitionCommand(RecognitionCommand);
    }
    // Create synthesizer
    _Synthesizer = _SpeechKit->CreateChantSynthesizer();
    //_Synthesizer = _SpeechKit->CreateAcaTTSSynthesizer();
    //_Synthesizer = _SpeechKit->CreateCereVoiceSynthesizer();
    //_Synthesizer = _SpeechKit->CreateMSPSynthesizer();
    //_Synthesizer = _SpeechKit->CreateSAPI5Synthesizer();
    //_Synthesizer = _SpeechKit->CreateSwiftSynthesizer();
    //_Synthesizer = _SpeechKit->CreateWindowsMediaSynthesizer();
    if (_Synthesizer != NULL)
    {
        // Register Event Handlers
        _Synthesizer->SetWordPosition(WordPosition);
    }
}

var
_SpeechKit: TSpeechKit;
_Recognizer: TChantRecognizer;
//_Recognizer: TDgnRecognizer;
//_Recognizer: TMSPRecognizer;
//_Recognizer: TSAPI5Recognizer;
//_Recognizer: TWindowsMediaRecognizer;
_Synthesizer: TChantSynthesizer;
//_Synthesizer: TAcaTTSSynthesizer;
//_Synthesizer: TCereVoiceSynthesizer;
//_Synthesizer: TMSPSynthesizer;
//_Synthesizer: TSAPI5Synthesizer;
//_Synthesizer: TSwiftSynthesizer;
//_Synthesizer: TWindowsMediaSynthesizer;
begin
    // Instantiate SpeechKit object
    _SpeechKit := TSpeechKit.Create();
    if (_SpeechKit <> nil) then
    begin
        // Set credentials
        _SpeechKit.SetCredentials('Credentials');

        // Create recognizer
        _Recognizer := _SpeechKit.CreateChantRecognizer();
        //_Recognizer := _SpeechKit.CreateDgnRecognizer();
        //_Recognizer := _SpeechKit.CreateMSPRecognizer();
        //_Recognizer := _SpeechKit.CreateSAPI5Recognizer();
        //_Recognizer := _SpeechKit.CreateWindowsMediaRecognizer();

        if (_Recognizer <> nil) then
        begin
            // Register Event Handlers
            _Recognizer.RecognitionCommand := RecognitionCommand;
        end;

        // Create synthesizer
        _Synthesizer := _SpeechKit.CreateChantSynthesizer();
        //_Synthesizer := _SpeechKit.CreateAcaTTSSynthesizer();
        //_Synthesizer := _SpeechKit.CreateCereVoiceSynthesizer();
        //_Synthesizer := _SpeechKit.CreateMSPSynthesizer();
        //_Synthesizer := _SpeechKit.CreateSAPI5Synthesizer();
        //_Synthesizer := _SpeechKit.CreateSwiftSynthesizer();
        //_Synthesizer := _SpeechKit.CreateWindowsMediaSynthesizer();

        if (_Synthesizer <> nil) then
        begin
            // Register Event Handlers
            _Synthesizer.WordPosition := WordPosition;
        end;
    end;
end;
    

JSpeechKit _SpeechKit = null;
JChantRecognizer _Recognizer = null;
//JDgnRecognizer _Recognizer;
//JMSPRecognizer _Recognizer;
//JSAPI5Recognizer _Recognizer;
//JWindowsMediaRecognizer _Recognizer;
JChantSynthesizer _Synthesizer = null;
//JAcaTTSSynthesizer _Synthesizer = null;
//JCereVoiceSynthesizer _Synthesizer = null;
//JMSPSynthesizer _Synthesizer = null;
//JSAPI5Synthesizer _Synthesizer = null;
//JSwiftSynthesizer _Synthesizer = null;
//JWindowsMediaSynthesizer _Synthesizer = null;

// Create SpeechKit object
_SpeechKit = new JSpeechKit();
// Set credentials
_SpeechKit.setCredentials("Credentials");

_Recognizer = _SpeechKit.createChantRecognizer();
//_Recognizer = _SpeechKit.createDgnRecognizer();
//_Recognizer = _SpeechKit.createMSPRecognizer();
//_Recognizer = _SpeechKit.createSAPI5Recognizer();
//_Recognizer = _SpeechKit.createWindowsMediaRecognizer();
if (_Recognizer != null)
{
    // Set the callback object
    _Recognizer.setChantSpeechKitEvents(this);
    // Register for callbacks
    _Recognizer.registerCallback(ChantSpeechKitCallback.CCSRRecognitionCommand);
}

_Synthesizer = _SpeechKit.createChantSynthesizer();
//_Synthesizer = _SpeechKit.createAcaTTSSynthesizer();
//_Synthesizer = _SpeechKit.createCereVoiceSynthesizer();
//_Synthesizer = _SpeechKit.createMSPSynthesizer();
//_Synthesizer = _SpeechKit.createSAPI5Synthesizer();
//_Synthesizer = _SpeechKit.createSwiftSynthesizer();
//_Synthesizer = _SpeechKit.createWindowsMediaSynthesizer();
if (_Synthesizer != null)
{
    // Set the callback object
    _Synthesizer.setChantSpeechKitEvents(this);
    // Register for callbacks
    _Synthesizer.registerCallback(ChantSpeechKitCallback.CCTTSWordPosition);
}

@property (strong, nonatomic) SPSpeechKit* speechKit;
@property (strong, nonatomic) SPChantRecognizer* recognizer;
//@property (strong, nonatomic) SPSFRecognizer* recognizer;
@property (strong, nonatomic) SPChantSynthesizer* synthesizer;
//@property (strong, nonatomic) SPAVFSynthesizer* synthesizer;
_speechKit = [[SPSpeechKit alloc] init];
if (_speechKit != nil)
{
    // Set credentials
    [_speechKit setCredentials:@"Credentials"];

    _recognizer = [_speechKit createChantRecognizer];
    //_recognizer = [_speechKit createSFRecognizer];
    if (_recognizer != nil)
    {
        [_recognizer setDelegate:(id<SPChantRecognizerDelegate>)self];
    }
    _synthesizer = [_speechKit createChantSynthesizer];
    //_synthesizer = [_speechKit createAVFSynthesizer];
    if (_synthesizer != nil)
    {
        [_synthesizer setDelegate:(id<SPChantSynthesizerDelegate>)self];
    }
}

var _SpeechKit: SPSpeechKit? = nil
var _Recognizer: SPChantRecognizer? = nil
//var _Recognizer: SPSFRecognizer? = nil
var _Synthesizer: SPChantSynthesizer? = nil
//var _Synthesizer: SPAVFSynthesizer? = nil
SpeechKit = SPSpeechKit()
if (_SpeechKit != nil)
{
    // Set credentials
    _ = _SpeechKit!.setCredentials(credentials: "Credentials")
    _Recognizer = _SpeechKit!.createChantRecognizer()
    //_Recognizer = _SpeechKit!.createSFRecognizer()
    if (_Recognizer != nil)
    {
        _Recognizer!.delegate = self
    }
    _Synthesizer = _SpeechKit!.createChantSynthesizer()
    //_Synthesizer = _SpeechKit!.createAVFSynthesizer()
    if (_Synthesizer != nil)
    {
        _Synthesizer!.delegate = self
    }
}

Dim _SpeechKit As NSpeechKit
Dim WithEvents _Recognizer As NChantRecognizer
'Dim WithEvents _Recognizer As NDgnRecognizer = Nothing
'Dim WithEvents _Recognizer As NMSPRecognizer = Nothing
'Dim WithEvents _Recognizer As NSAPI5Recognizer = Nothing
'Dim WithEvents _Recognizer As NWindowsMediaRecognizer = Nothing
Dim WithEvents _Synthesizer As NChantSynthesizer
'Dim WithEvents _Synthesizer As NAcaTTSSynthesizer = Nothing
'Dim WithEvents _Synthesizer As NCereVoiceSynthesizer = Nothing
'Dim WithEvents _Synthesizer As NMSPSynthesizer = Nothing
'Dim WithEvents _Synthesizer As NSAPI5Synthesizer = Nothing
'Dim WithEvents _Synthesizer As NSwiftSynthesizer = Nothing
'Dim WithEvents _Synthesizer As NWindowsMediaSynthesizer = Nothing
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
    ' Instantiate SpeechKit
    _SpeechKit = New NSpeechKit()
    If (_SpeechKit IsNot Nothing) Then
        ' Set credentials
        _SpeechKit.SetCredentials(String.Empty, "Credentials")
        _Recognizer = _SpeechKit.CreateChantRecognizer()
        '_Recognizer = _SpeechKit.CreateDgnRecognizer()
        '_Recognizer = _SpeechKit.CreateMSPRecognizer()
        '_Recognizer = _SpeechKit.CreateSAPI5Recognizer()
        '_Recognizer = _SpeechKit.CreateWindowsMediaRecognizer()
        _Synthesizer = _SpeechKit.CreateChantSynthesizer()
        '_Synthesizer = _SpeechKit.CreateAcaTTSSynthesizer()
        '_Synthesizer = _SpeechKit.CreateCereVoiceSynthesizer()
        '_Synthesizer = _SpeechKit.CreateMSPSynthesizer()
        '_Synthesizer = _SpeechKit.CreateSAPI5Synthesizer()
        '_Synthesizer = _SpeechKit.CreateSwiftSynthesizer()
        '_Synthesizer = _SpeechKit.CreateWindowsMediaSynthesizer()
    End If
End Sub
    

Review the details of API-specific development in the sample projects for each product.

If a specific-API is used in the app, then the corresponding speech recognition and event objects may be used. For applications using multiple APIs or are targeted cross platform, speech recognition and synthesis event result arguments are designed and classes factored to allow for shared common properties and downcasting for access to unique API-specific property values.

Review the details in CDW 2023 Downcasting event results to API-specific objects.