All Products
Search
Document Center

iOS SDK 2.0

Last Updated: Jun 02, 2020

Notes:

Speech synthesis

Speech synthesis is used to convert text to speech. It supports multiple speakers and output files in PCM, WAV, and MP3 formats. This demo implements PCM-based speech synthesis and playback.

SDK download

  • Download the iOS SDK 2.0
  • Use Xcode 9.3 to open the demo project in the NlsDemo.xcodeproj folder. If you use another version of Xcode, you may need to manually import the demo project.
  • The iOS SDK in the demo, that is, NlsDemo.framework or AliyunNlsSdk.framework, supports the following emulators: x86_64, ARMv7, or ARM64.
  • Apple App Store does not support the x86_64 architecture. Therefore, the iOS SDK provides an iOS device framework. You can build the following framework: NlsDemo-iOS/ReleaseFramework-iphoneOS/AliyunNlsSdk.framework.
  • AliyunNlsSdk is a dynamic library. Therefore, you need to add NlsDemo.framework or AliyunNlsSdk.framework as an embedded binary when you import files to your project.

SDK usage

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

  1. Import header files AliyunNlsClientAdaptor.h, NlsSpeechSynthesizerRequest.h, and SynthesizerRequestParam.h in the NlsSdk directory.
  2. Call the NlsSpeechSynthesizerDelegate method of the NlsSpeechSynthesizerRequest object.
  3. Create an NlsClient object of AliyunNlsClientAdaptor. You can globally create an NlsClient object and reuse it if necessary.
  4. Call the createSynthesizerRequest method of the NlsClient object to create the NlsSpeechSynthesizerRequest object. The NlsSpeechSynthesizerRequest object cannot be reused. You need to create it for each request.
  5. Call the SynthesizerRequestParam object to set recognition parameters, such as accessToken and appkey. For more information about parameters, see below.
  6. Call the setSynthesizerParams method of the NlsSpeechSynthesizerRequest object to pass parameters 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 send audio data to the server.
  9. Use the NLSPlayAudio tool to play the audio data received in step 8.

Key objects

  • AliyunNlsClientAdaptor: the speech processing client, which is equivalent to a factory for all speech processing classes. You can globally create an AliyunNlsClientAdaptor instance. This object is thread-safe.
  • NlsSpeechSynthesizerRequest: the request object of speech synthesis. This object is thread-safe.
  • SynthesizerRequestParam: parameters for speech synthesis.
  • NlsSpeechSynthesizerDelegate: the delegate that defines multiple callback functions of speech synthesis. These callbacks are fired when synthesis results are returned or any errors occur.

Sample code

  1. #import <Foundation/Foundation.h>
  2. #import "Synthesizer.h"
  3. @interface Synthesizer()<NlsSpeechSynthesizerDelegate>{
  4. IBOutlet UITextView *textViewSynthesizer;
  5. }
  6. @end
  7. @implementation Synthesizer
  8. -(void)viewDidLoad{
  9. [super viewDidLoad];
  10. // 1. Initialize global parameters.
  11. // 1.1 Initialize the speech synthesis client.
  12. _nlsClient = [[NlsClientAdaptor alloc]init];
  13. // 1.2 Initialize the audio playback object nlsAudioPlayer.
  14. _nlsAudioPlayer = [[NLSPlayAudio alloc]init];
  15. // 1.3 Initialize the synthesis object SynthesizerRequestParam.
  16. _requestParam = [[SynthesizerRequestParam alloc]init];
  17. // 1.4 Specify the logging level.
  18. [_nlsClient setLog:NULL logLevel:1];
  19. }
  20. -(IBAction)startSynthesizer{
  21. // 2. Create the request object and start speech synthesis.
  22. if(_synthesizerRequest!= NULL){
  23. _synthesizerRequest = NULL;
  24. }
  25. // 2.1 Initialize the audio playback object nlsAudioPlayer.
  26. [_nlsAudioPlayer cleanup];
  27. _nlsAudioPlayer = [[NLSPlayAudio alloc]init];
  28. // 2.2 Create the request object and set the NlsSpeechSynthesizerRequest object.
  29. _synthesizerRequest = [_nlsClient createSynthesizerRequest];
  30. _synthesizerRequest.delegate = self;
  31. // 2.3 Obtain the text to be synthesized.
  32. NSString *inputText = [textViewSynthesizer text];
  33. // 2.4 Set request parameters for the SynthesizerRequestParam object.
  34. [_requestParam setServiceUrl:@"wss://nls-gateway-ap-southeast-1.aliyuncs.com/ws/v1"];
  35. [_requestParam setFormat:@"pcm"];
  36. [_requestParam setText:inputText];
  37. // Obtain a dynamic token. For more information, see https://www.alibabacloud.com/help/doc-detail/72153.htm.
  38. // <AccessKeyId><AccessKeySecret>Use your Alibaba Cloud account to generate the AccessKey ID and AccessKey secret in Alibaba Cloud console (https://ak-console.aliyun.com/).
  39. [_requestParam setToken:@""];
  40. // Obtain an appkey in the Intelligent Speech Interaction console (https://nls-portal.console.aliyun.com/).
  41. [_requestParam setAppkey:@""];
  42. // 2.5 Pass request parameters.
  43. [_synthesizerRequest setSynthesizerParams:_requestParam];
  44. // 2.6 Start speech synthesis.
  45. [_synthesizerRequest start];
  46. }
  47. /**
  48. * 3. Call the NlsSpeechSynthesizerDelegate object.
  49. */
  50. // 3.1 The request fails.
  51. - (void)OnTaskFailed:(NlsDelegateEvent)event statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
  52. NSLog(@"OnTaskFailed, statusCode is: %@ error message: %@",statusCode,eMsg);
  53. }
  54. // 3.2 The server is disconnected.
  55. - (void)OnChannelClosed:(NlsDelegateEvent)event statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
  56. NSLog(@"OnChannelClosed, statusCode is: %@",statusCode);
  57. }
  58. // 3.3 Call back the synthesized speech data and play the data by using NlsAudioPlayer.
  59. - (void)OnBinaryDataReceived:(NlsDelegateEvent)event voiceData:(Byte *)data length:(NSInteger)length{
  60. NSLog(@"Received voice data length %lu", length);
  61. [_nlsAudioPlayer process:data length:length];
  62. }
  63. // 3.4 Synthesis is completed.
  64. - (void)OnSynthesizerCompleted:(NlsDelegateEvent)event result:(NSString *)result statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
  65. }
  66. // 3.5 Synthesis is started.
  67. - (void)OnSynthesizerStarted:(NlsDelegateEvent)event result:(NSString *)result statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
  68. }
  69. @end