How do I voice-enable iOS and macOS applications with SpeechKit?

Last reviewed: 7/8/2022

HOW Article ID: H072215

The information in this article applies to:

  • SpeechKit 11

Summary

Voice enabling iOS snd macOS applications is easier with SpeechKit classes for managing speech recognition and speech synthesis.

More Information

Develop iOS and macOS applications that speak and listen using Objective-C or Swift with Xcode.

The following sections describe the steps for integrating SpeechKit with Objective-C and Swift iOS and macOS applications.

SpeechKit Objective-C Classes

SpeechKit includes Objective-C classes for developing applications that speak and listen.

To access the SpeechKit classes within your application, copy the following Chant SpeechKit classes to your application project:

  1. Program Files\Chant\SpeechKit 11\Objective-C\include\ChantShared.h,
  2. Program Files\Chant\SpeechKit 11\Objective-C\include\ChantShared.m,
  3. Program Files\Chant\SpeechKit 11\Objective-C\include\ChantSpeechKit.h, and
  4. Program Files\Chant\SpeechKit 11\Objective-C\include\ChantSpeechKit.m.

Import the SpeechKit classes in either your header or source file and optionally declare your objects as properties:

Add the Speech.framework for speech recognition and/or add the AVFoundation.framework for speech synthesis.


#import "ChantSpeechKit.h"

@property (strong, nonatomic) SPSpeechKit* speechKit;
@property (strong, nonatomic) SPSFRecognizer* recognizer;
@property (strong, nonatomic) SPAVFSynthesizer* synthesizer;

SpeechKit Swift Classes

SpeechKit includes Swift classes for developing applications that speak and listen in an XCFramework. The XCFramework enables simulated testing and device deployments.

To install the SpeechKit.xcframework, copy the SpeechKit.xcframework folder to your Mac development platform.

To access the SpeechKit classes within your application, drag or add the SpeechKit.xcframework to your application project under General settings.

Add the Speech.framework for speech recognition and/or add the AVFoundation.framework for speech synthesis.

Import the SpeechKit classes into your application:


#import "SpeechKit"

Object Instantiation

Instantiate SpeechKit and set the credentials. For speech recognition, instantiate a recognizer object, set delegate for events, and define recognition event handlers. For speech synthesis, instantiate a synthesizer object, optionally set delegate for events, and optionally define synthesis callback event handlers.


    _speechKit = [[SPSpeechKit alloc] init];
    if (_speechKit != nil)
    {
        // Set credentials
        [_speechKit setCredentials:@"Credentials"];

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

        _SpeechKit = SPSpeechKit()
        if (_SpeechKit != nil)
        {
            // Set credentials
            _ = _SpeechKit!.setLicense(credentials:"Credentials")
            _Recognizer = _SpeechKit!.createSFRecognizer()
            if (_Recognizer != nil)
            {
                _Recognizer!.delegate = self
            }
            _Synthesizer = _SpeechKit!.createAVFSynthesizer()
            if (_Synthesizer != nil)
            {
                _Synthesizer!.delegate = self
            }
        }

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.

For speech recognition, add the delegate protocol SPChantRecognizerDelegate to your interface (Objective-C) or class (Swift) declaration:


@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate, SPChantRecognizerDelegate>

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, SPChantRecognizerDelegate {

For speech synthesis, add the delegate protocol SPChantSynthesizerDelegate to your interface (Objective-C) or class (Swift) declaration:


@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate, SPChantSynthesizerDelegate>

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, SPChantSynthesizerrDelegate {

Add the delegate protocol methods to your class:


//  Add only for the desired events since protocol methods are optional in Objective-C
...
-(void)recognitionDictation:(NSObject *)sender args:(SPRecognitionDictationEventArgs *)args;
{
    NSString* newText = [NSString stringWithFormat:@"%@%@ ", [_textView1 text], [args text]];
    [_textView1 setText:newText];
}
...
-(void)audioDestStop:(NSObject*)sender args:(SPAudioEventArgs*)args
{
    _button1.enabled = true;
}
-(void)rangeStart:(NSObject*)sender args:(SPRangeStartEventArgs*)args
{
    [_textView1 setSelectedRange:NSMakeRange([args location], [args length])];
}

    // Add all the delegate protocol methods to your class even if you do not handle the event. All protocol methods are required in Swift
    func apiError(sender: SPChantRecognizer, args: SPChantAPIErrorEventArgs)
    {
        
    }
...
    func recognitionDictation(sender: SPChantRecognizer, args: SPRecognitionDictationEventArgs)
    {
        let newText = String(format: "%@%@ ", self.textView1.text, args.text)
        self.textView1.text = newText
    }
...
    func apiError(sender: SPChantSynthesizer, args: SPChantAPIErrorEventArgs)
    {
        
    }
...
    func audioDestStop(sender: SPChantSynthesizer, args: SPAudioEventArgs)
    {
        self.button1.isEnabled = true
    }
...
    func rangeStart(sender: SPChantSynthesizer, args: SPRangeStartEventArgs)
    {
        self.textView1.selectedRange = (NSRange(location: args.location, length: args.length))
    }
...

Permissions

Speech recognition requires the user to grant speech recognition permission and access to the microphone. The app's Info.plist must contain an NSSpeechRecognitionUsageDescription key with string value and an NSMicrophoneUsageDescription key with string value.

Select the Info.plist file in the project and add two keys with usage description text:

  • Add a key to the Information Property List by clicking the plus button.
  • Select: Privacy - Speech Recognition Usage Description.
  • Enter a string value description such as: speech recognition.
  • Add a key to the Information Property list by clicking the plus button.
  • Select: Privacy - Microphone Usage Description.
  • Enter a string value description such as: mic for speech recognition.

macOS applications require an additional project setting to enable audio input. Select the project Signing & Capabilities tab. Select the Audio Input checkbox under App Sandbox/Hardware and under Hardware Runtime/Resource Access.

Linking Objective-C Apps

Link the SpeechKit static library libChantSpeechKit.a to the application under Settings General tab. Add as a framework library. Set the app as the target.

For iOS applications there are two libraries. One targets devices and the other targets the simulator. Copy the applicable library to the project:

  1. Program Files\Chant\SpeechKit 11\iOS\Objective-C\lib\libChantSpeechKit.a, or
  2. Program Files\Chant\SpeechKit 11\iOS\Objective-C\libSimulator\libChantSpeechKit.a.
For macOS applications there is only one library. Copy the applicable library to the project:
  1. Program Files\Chant\SpeechKit 11\Objective-C\macOS\lib\libChantSpeechKit.a.