iOS SDK (Legacy)

Updated at:

The legacy iOS SDK provides speech synthesis interfaces to convert text to speech and receive synthesized audio through callbacks.

Important

The legacy iOS SDK is no longer updated. We recommend using the new SDK. For more information, see iOS SDK.

Prerequisites

  • Create a project in the Intelligent Speech Interaction console and obtain an Appkey. For more information, see Create a project.

  • Obtain an access token for Intelligent Speech Interaction. For more information, see Obtain a token.

Speech synthesis

Speech synthesis supports multiple voices and audio output in PCM, WAV, and MP3 formats. The example in this topic synthesizes and plays PCM audio. Before making a request, read the speech synthesis API reference for request parameters and callback events.

Call procedure

Integrate the SDK into the project before making a request. The static and dynamic frameworks in the SDK package require different integration methods:

Library type

Architectures in the package

Integration method

Static

x86_64, armv7, arm64

Add the framework to the linking phase. Do not add it to the dynamic framework embedding phase.

Dynamic

armv7, arm64

Link the framework and embed it in the application.

  1. Import the AliyunNlsClientAdaptor.h, NlsSpeechSynthesizerRequest.h, and SynthesizerRequestParam.h header files.

  2. Implement the NlsSpeechSynthesizerDelegate callback methods.

  3. Create an NlsClientAdaptor object named nlsClient. Create this client once globally and reuse it.

  4. Call the createSynthesizerRequest method of nlsClient to create an NlsSpeechSynthesizerRequest object, and set its delegate. Request objects cannot be reused. Create a new object for each request.

  5. Use SynthesizerRequestParam to set parameters such as the token, Appkey, text to synthesize, and audio format.

  6. Call the setSynthesizerParams method of the request object to pass in the configured SynthesizerRequestParam object.

  7. Call the start method of the request object to start speech synthesis.

  8. Receive synthesized audio in the OnBinaryDataReceived:voiceData:length: callback of NlsSpeechSynthesizerDelegate.

  9. Use the NLSPlayAudio utility in the sample project to play the received PCM audio.

Key interfaces

Class or protocol

Description

NlsClientAdaptor

A speech processing client for short sentence recognition, real-time speech recognition, and speech synthesis. The client is thread-safe. We recommend creating only one instance globally.

NlsSpeechSynthesizerRequest

A thread-safe speech synthesis request object. Create a new object for each request.

SynthesizerRequestParam

Parameters for a speech synthesis request.

NlsSpeechSynthesizerDelegate

A speech synthesis callback protocol. The corresponding callback is triggered when audio is received, synthesis is completed, or an error occurs.

Sample code

The following Objective-C code shows the main speech synthesis and playback logic in the sample project. It depends on Synthesizer.h, NLSPlayAudio, and the UI control configuration in that project, and is not a standalone source file. Before use, replace the empty strings passed to setToken and setAppkey with a matching token and Appkey.

#import <Foundation/Foundation.h>
#import "Synthesizer.h"
@interface Synthesizer()<NlsSpeechSynthesizerDelegate>{
    IBOutlet UITextView *textViewSynthesizer;
}
@end
@implementation Synthesizer
-(void)viewDidLoad{
    [super viewDidLoad];
    // 1. Initialize global parameters.
    // 1.1 Initialize the speech synthesis client.
    _nlsClient = [[NlsClientAdaptor alloc]init];
    // 1.2 Initialize the audio playback utility class.
    _nlsAudioPlayer = [[NLSPlayAudio alloc]init];
    // 1.3 Initialize the synthesis parameter class.
    _requestParam = [[SynthesizerRequestParam alloc]init];
    // 1.4 Set the log level.
    [_nlsClient setLog:NULL logLevel:1];
}
-(IBAction)startSynthesizer{
    // 2. Create a request object and start speech synthesis.
    if(_synthesizerRequest!= NULL){
        _synthesizerRequest = NULL;
    }
    // 2.1 Initialize the audio playback class.
    [_nlsAudioPlayer cleanup];
    _nlsAudioPlayer = [[NLSPlayAudio alloc]init];
    // 2.2 Create a request object and set the NlsSpeechSynthesizerRequest callback.
    _synthesizerRequest = [_nlsClient createSynthesizerRequest];
    _synthesizerRequest.delegate = self;
    // 2.3 Get the text for synthesis from the page.
    NSString *inputText = [textViewSynthesizer text];
    // 2.4 Set the SynthesizerRequestParam request parameters.
    [_requestParam setFormat:@"pcm"];
    [_requestParam setText:inputText];
    [_requestParam setToken:@""];
    [_requestParam setAppkey:@""];
    // 2.5 Pass the request parameters.
    [_synthesizerRequest setSynthesizerParams:_requestParam];
    // 2.6 Start speech synthesis.
    [_synthesizerRequest start];
}
/**
 *3. NlsSpeechSynthesizerDelegate interface callbacks
 */
// 3.1 The current request failed.
- (void)OnTaskFailed:(NlsDelegateEvent)event statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
    NSLog(@"OnTaskFailed, statusCode is: %@ error message: %@",statusCode,eMsg);
}
// 3.2 The server-side connection is closed.
- (void)OnChannelClosed:(NlsDelegateEvent)event statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
    NSLog(@"OnChannelClosed, statusCode is: %@",statusCode);
}
// 3.3 The synthesized audio data is returned in the callback. Play the data using the NLSPlayAudio utility.
- (void)OnBinaryDataReceived:(NlsDelegateEvent)event voiceData:(Byte *)data length:(NSInteger)length{
    NSLog(@"Received voice data length %lu", length);
    [_nlsAudioPlayer process:data length:length];
}
// 3.4 Synthesis is complete.
- (void)OnSynthesizerCompleted:(NlsDelegateEvent)event result:(NSString *)result statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
}
// 3.5 Synthesis starts.
- (void)OnSynthesizerStarted:(NlsDelegateEvent)event result:(NSString *)result statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
}
@end