All Products
Search
Document Center

Intelligent Speech Interaction:SDK for iOS

Last Updated:Mar 29, 2023

The speech synthesis service provides an SDK for iOS. This topic describes how to download and install the SDK. This topic also provides sample code for you to use the SDK.

Note

We recommend that you use the latest version of the SDK for iOS. The current version is no longer updated. For more information, see NUI SDK for iOS.

Prerequisites

  • You understand how the SDK works. For more information, see Overview.

  • A project is created in the Intelligent Speech Interaction console and the appkey of the project is obtained. For more information, see Create a project.

  • An access token that is used to call Intelligent Speech Interaction services is obtained. For more information, see Obtain an access token.

Speech synthesis

The speech synthesis service converts input text to speech. The speech synthesis service provides multiple speaker types. You can specify a speaker to convert input text to audio files in pulse-code modulation (PCM), WAV, or MP3 format. This demo demonstrates how to synthesize and play audio files in PCM format.

Call procedure

Note

To import the SDK for iOS to your project, add destination files as embedded binaries.

  1. Import the header files from the AliyunNlsSdk.framework/Headers directory, including AliyunNlsClientAdaptor.h, NlsSpeechSynthesizerRequest.h, and SynthesizerRequestParam.h.

  2. Call the NlsSpeechSynthesizerDelegate method of the NlsSpeechSynthesizerRequest object.

  3. Create an nlsClient object of AliyunNlsClientAdaptor.

    You can globally create an nlsClient object for all requests.

  4. Call the createSynthesizerRequest method of the nlsClient object to create the NlsSpeechSynthesizerRequest object.

    The NlsSpeechSynthesizerRequest object cannot be reused. You must create an NlsSpeechSynthesizerRequest object for each request.

  5. Call the SynthesizerRequestParam object to set parameters, such as the access token and appkey.

  6. Call the setSynthesizerParams method of the NlsSpeechSynthesizerRequest object to pass parameters that are set by the SynthesizerRequestParam object in Step 5.

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

  8. Call the -(void)OnBinaryDataReceived:(NlsDelegateEvent)event voiceData:(Byte *)data length:(NSInteger)length; method of the NlsSpeechSynthesizerDelegate object to pass the synthesized audio data.

  9. Use NLSPlayAudio to play the audio data that is received in Step 8.

Key objects

  • AliyunNlsClientAdaptor: the speech processing client. You can use this client to process short sentence recognition, real-time speech recognition, and speech synthesis tasks. The client is thread-safe. We recommend that you globally create one AliyunNlsClientAdaptor instance.

  • NlsSpeechSynthesizerRequest: the request object of speech synthesis. This object is thread-safe.

  • SynthesizerRequestParam: the object that is used to set parameters for speech synthesis.

  • NlsSpeechSynthesizerDelegate: the delegate that defines multiple callback functions of speech synthesis. Callbacks are fired when synthesis results are returned or any errors occur.

Sample code

#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 object NlsAudioPlayer.
    _nlsAudioPlayer = [[NLSPlayAudio alloc]init];
    // 1.3 Initialize the synthesis object SynthesizerRequestParam.
    _requestParam = [[SynthesizerRequestParam alloc]init];
    // 1.4 Specify the logging 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 object NlsAudioPlayer.
    [_nlsAudioPlayer cleanup];
    _nlsAudioPlayer = [[NLSPlayAudio alloc]init];
    // 2.2 Create a request object and set the NlsSpeechTranscriberDelegate object.
    _synthesizerRequest = [_nlsClient createSynthesizerRequest];
    _synthesizerRequest.delegate = self;
    // 2.3 Obtain the text from which you want to synthesize speech.
    NSString *inputText = [textViewSynthesizer text];
    // 2.4 Set request parameters for the SynthesizerRequestParam object.
    [_requestParam setFormat:@"pcm"];
    [_requestParam setText:inputText];
    [_requestParam setToken:@""];
    [_requestParam setAppkey:@""];
    // 2.5 Pass request parameters.
    [_synthesizerRequest setSynthesizerParams:_requestParam];
    // 2.6 Start speech synthesis.
    [_synthesizerRequest start];
}
/**
 *3. Call the NlsSpeechSynthesizerDelegate object.
 */
// 3.1 The request fails.
- (void)OnTaskFailed:(NlsDelegateEvent)event statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
    NSLog(@"OnTaskFailed, statusCode is: %@ error message :%@",statusCode,eMsg);
}
// 3.2 The server is disconnected.
- (void)OnChannelClosed:(NlsDelegateEvent)event statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
    NSLog(@"OnChannelClosed, statusCode is: %@",statusCode);
}
// 3.3 Call back the synthesized audio data. Play the audio data by using NlsAudioPlayer.
- (void)OnBinaryDataReceived:(NlsDelegateEvent)event voiceData:(Byte *)data length:(NSInteger)length{
    NSLog(@"Received voice data length %lu", length);
    [_nlsAudioPlayer process:data length:length];
}
// 3.4 Speech synthesis is completed.
- (void)OnSynthesizerCompleted:(NlsDelegateEvent)event result:(NSString *)result statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
}
// 3.5 Speech synthesis is started.
- (void)OnSynthesizerStarted:(NlsDelegateEvent)event result:(NSString *)result statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
}
@end