NYVoiceManager.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. //
  2. // NYVoiceManager.m
  3. // jiaPei
  4. //
  5. // Created by Ning.ge on 2023/7/26.
  6. // Copyright © 2023 JCZ. All rights reserved.
  7. //
  8. #import "NYVoiceManager.h"
  9. #import "payRequsestHandler.h"
  10. static NYVoiceManager *vomanger = nil;
  11. static dispatch_once_t onceToken;
  12. @interface NYVoiceManager ()<QCloudTTSEngineDelegate,QCloudPlayerDelegate>
  13. {
  14. NSMutableArray* _textArr;
  15. }
  16. //SDK 内置播放器
  17. @property(strong) QCloudMediaPlayer *player;
  18. // demo 层播放器,如SDK播放器不能满足需求 可以自定义播放器 如下只是一个例子
  19. //@property(strong) MediaPlayerDemo *player;
  20. @property (nonatomic,assign)float onlineSpeed;
  21. @property (nonatomic,assign)int onlineVoiceType;
  22. @property (nonatomic,assign)int onlineLanguage;
  23. @property (nonatomic,assign)float onlineVolume;
  24. @property (nonatomic,assign)float offlineSpeed;
  25. @property (nonatomic,copy)NSString *offlineVoiceType;
  26. @property (nonatomic,assign)int offlineLanguage;
  27. @property (nonatomic,assign)float offlineVolume;
  28. @property (nonatomic,assign)int responseTimeout;
  29. @property (nonatomic,assign)int requestTimeout;
  30. @property (nonatomic,assign)int checkTimeout;
  31. @property (nonatomic, copy) NSString* resource_dir;
  32. @property (nonatomic,assign)int cout; //统计合成句子数,当作utteranceId用于标记句子用
  33. @end
  34. @implementation NYVoiceManager
  35. #pragma mark - SystemMethod
  36. + (NYVoiceManager *)sharedManager {
  37. dispatch_once(&onceToken, ^{
  38. vomanger = [[self alloc] init];
  39. });
  40. return vomanger;
  41. }
  42. - (instancetype)init {
  43. self = [super init];
  44. if (self) {
  45. [self initBaseData];
  46. }
  47. return self;
  48. }
  49. - (void)dealloc {
  50. }
  51. #pragma mark - PublicMethods
  52. // 配置
  53. - (void)ny_configureTTSVoiceDK {
  54. QCloudTTSEngine *tts = [QCloudTTSEngine getShareInstance];
  55. [tts cancel];
  56. [_player StopPlay];
  57. [self setOnlineParam:tts];//在线
  58. }
  59. -(void)setOnlineParam:(QCloudTTSEngine*)ttsEngine {
  60. //设置合成声音速度
  61. [ttsEngine setOnlineVoiceSpeed:_onlineSpeed];
  62. //设置合成声音类型
  63. [ttsEngine setOnlineVoiceType:_onlineVoiceType];
  64. //设置合成语种
  65. [ttsEngine setOnlineVoiceLanguage:_onlineLanguage];
  66. //设置合成声音大小默认值为0
  67. [ttsEngine setOnlineVoiceVolume:_onlineVolume];
  68. //设置响应超时时间
  69. [ttsEngine setTimeoutIntervalForResource:_responseTimeout];
  70. //设置请求超时时间
  71. [ttsEngine setTimeoutIntervalForRequest:_requestTimeout];
  72. //设置检测网络间隔时间
  73. [ttsEngine setCheckNetworkIntervalTime:_checkTimeout];
  74. }
  75. - (void)setOfflineParam:(QCloudTTSEngine*)ttsEngine{
  76. [ttsEngine setOfflineVoiceType:_offlineVoiceType];
  77. [ttsEngine setOfflineVoiceSpeed:_offlineSpeed];
  78. [ttsEngine setOfflineVoiceVolume:_offlineVolume];
  79. }
  80. //开播合成播放
  81. - (void)startPayVoiceActionText:(NSString *)text completedBlock:(nonnull NYVoicePayCompletedBlock)block {
  82. _paytext = text;
  83. self.payCompletedBlock = block;
  84. QCloudTTSEngine *tts = [QCloudTTSEngine getShareInstance];
  85. [self setOnlineParam:tts];
  86. //设置ProjectId 不设置默认使用0,说明:项目功能用于按项目管理云资源,可以对云资源进行分项目管理,详情见 https://console.cloud.tencent.com/project
  87. //[tts setOnlineProjectId:0];
  88. [self stopTTSBtnAction:nil];
  89. _textArr = [self breakIntoSentencesFromString:text];
  90. for (int i = 0; i < 10 - [_player getAudioQueueSize]; i++) {
  91. if (_textArr.count > 0) {
  92. [tts synthesize:_textArr[0] UtteranceId:[NSString stringWithFormat:@"%d",_cout++]];
  93. [_textArr removeObjectAtIndex:0];
  94. }
  95. }
  96. }
  97. //暂停合成播放
  98. - (void)pausePayVoiceAction {
  99. [self pauseTTSBtnAction:nil];
  100. }
  101. //恢复合成播放
  102. - (void)resumePayVoiceAction {
  103. [self resumeTTSBtnAction:nil];
  104. }
  105. //停止合成播放
  106. - (void)stopPayVoiceAction {
  107. [self stopTTSBtnAction:nil];
  108. }
  109. - (IBAction)stopTTSBtnAction:(id)sender {
  110. QCloudTTSEngine *tts = [QCloudTTSEngine getShareInstance];
  111. [tts cancel];
  112. [_player StopPlay];
  113. [_textArr removeAllObjects];
  114. }
  115. - (IBAction)pauseTTSBtnAction:(id)sender {
  116. [_player PausePlay];
  117. }
  118. - (IBAction)resumeTTSBtnAction:(id)sender {
  119. [_player ResumePlay];
  120. }
  121. #pragma mark - PrivateMethods
  122. - (void)initBaseData {
  123. _cout = 0;
  124. _textArr = [NSMutableArray array];
  125. QCloudTTSEngine *tts = [QCloudTTSEngine getShareInstance];
  126. //如果使用STS临时证书鉴权时需要设置Token
  127. [tts setOnlineAuthParam:QDApp_Id SecretId:QDSecret_ID SecretKey:QDSecret_Key Token:nil];
  128. [tts engineInit:0 Delegate:self];
  129. [tts setEnableDebugLog:YES];
  130. //SDK 内置播放器
  131. _player = [[QCloudMediaPlayer alloc]init];
  132. // demo 层播放器,如SDK播放器不能满足需求 可以自定义播放器 如下只是一个例子 源码在MediaPlayerDemo.m文件 可自行修改
  133. //_player = [[MediaPlayerDemo alloc]init];
  134. _player.playerDelegate = self;
  135. //离线参数
  136. //在线参数
  137. _onlineSpeed = 0;
  138. _onlineVoiceType = 1001;
  139. //主语言类型:1-中文(默认)2-英文
  140. _onlineLanguage = 1;
  141. _onlineVolume = 0;
  142. //连接超时默认15000ms(15s) 范围[500,30000] 单位ms , Mix模式下建议调小此值,以获得更好的体验
  143. _responseTimeout = 15*1000;
  144. //读取超时超时默认30000ms(30s) 范围[2200,60000] 单位ms, Mix模式下建议调小此值,以获得更好的体验
  145. _requestTimeout = 30 * 1000;
  146. //离线参数,如果您下载的是在线版SDK,以下参数不需要设置,请忽略
  147. _offlineSpeed = 1.0;//离线语速[0.5,2.0]
  148. //voiceType 离线音色名称,名称配置位于音色资源目录tts_resource\voices\config.json 中,可自行增删音色
  149. _offlineVoiceType = @"pb";
  150. _offlineVolume = 1.0;//离线音量 > 0
  151. /*_checkTimeout 大于等于0 单位s, 等于0时持续检测,直到成功
  152. Mix模式下,已经连接网络,但出现网络错误后的检测间隔时间,用于从离线模式自动切换回在线模式,默认值5分钟
  153. 注意:每次检测时将使用所入参的一句文本请求服务器,如果后端合成成功将会额外消耗该句字数的合成额度
  154. */
  155. _checkTimeout = 300;
  156. // JSJP_APP_VOICE_SET
  157. @try {
  158. // NSDictionary *dict = @{
  159. // @"onlineSpeed":@"0",//设置合成声音速度0默认 1正常 1.2倍
  160. // @"onlineVoiceType":@"1001",//设置合成声音类型
  161. // @"onlineLanguage":@"1",//设置合成语种-1中文 2英文
  162. // @"onlineVolume":@"0",//设置合成声音大小默认值为0
  163. // };
  164. NSDictionary *dict = RQ_COMMON_MANAGER.JSJP_APP_VOICE_SET;
  165. if(dict){ //读取服务器配置
  166. _onlineSpeed = [dict[@"onlineSpeed"] floatValue];
  167. _onlineVoiceType = [dict[@"onlineVoiceType"] intValue];
  168. _onlineLanguage = [dict[@"onlineLanguage"] intValue];
  169. _onlineVolume = [dict[@"onlineVolume"] floatValue];
  170. }
  171. } @catch (NSException *exception) {
  172. NSLog(@"%@",exception.description);
  173. }
  174. }
  175. #pragma mark -- QCloudTTSEngineDelegate
  176. //合成结果返回
  177. //---------QCloudTTSEngineDelegate---------
  178. /// 合成回调
  179. /// @param data 语音数据
  180. /// @param utteranceId 语句id
  181. /// @param text 文本
  182. /// @param type 引擎类型 0:在线 1:离线
  183. ///
  184. -(void) onSynthesizeData:(NSData *_Nullable)data UtteranceId:(NSString *_Nullable)utteranceId Text:(NSString *_Nullable)text EngineType:(NSInteger)type RequestId:(NSString*)requestId{
  185. NSLog(@"text====%@,utteranceId ==%@,type==%ld,requestId=%@",text,utteranceId,(long)type,requestId);
  186. [_player enqueueWithData:data Text:text UtteranceId:utteranceId];
  187. // //通过保存音频的URL播放
  188. // //NSString *str = [self filePathWithName:@"tmp.mp3"];
  189. // //NSURL * url = [NSURL URLWithString:str];
  190. // //[_player enqueueWithFile:url Text:text UtteranceId:utteranceId];
  191. //
  192. //
  193. // _messageTextView.text = [NSString stringWithFormat:@"%@\n text=%@\n utteranceId=%@ requestId=%@",_messageTextView.text,text,utteranceId,requestId];
  194. // [self scrollToBottom];
  195. //
  196. // NSLog(@"onSynthesizeData %@",@(data.length));
  197. }
  198. /// 错误回调
  199. /// @param error 错误信息
  200. /// @param utteranceId utteranceId
  201. /// @param text text
  202. -(void) onError:(TtsError *_Nullable)error UtteranceId:(NSString *_Nullable)utteranceId Text:(NSString *_Nullable)text{
  203. // if (error.serviceError != nil && _ttsMode == TTS_MODE_MIX) {
  204. // //实际业务上判断一下,如果是混合模式下返回在线合成的后端错误码,应当忽略可不做处理
  205. // //SDK内会继续调用离线合成继续工作,如果没有日志需求,这里直接return忽略即可
  206. // //return;
  207. // }else{
  208. // QCloudTTSEngine *tts = [QCloudTTSEngine getShareInstance];
  209. // [tts cancel];
  210. // }
  211. //
  212. // NSString* msg = [NSString stringWithFormat:@"%@\n error=%@\n error.code=%@\n error.message=%@\n response = %@\n utteranceId=%@",_messageTextView.text,error.error,@(error.err_code),error.msg,error.serviceError.respose, utteranceId];
  213. // _messageTextView.text = msg;
  214. // [self scrollToBottom];
  215. // NSLog(@"onError %@",msg);
  216. }
  217. //离线或者混合模式下,调用初始化后必须收到此回调,并且OfflineAuthInfo.err_code为0时,才可以调用合成接口,如果err_code不为0,请检查参数或者网络,重新init
  218. - (void)onOfflineAuthInfo:(QCloudOfflineAuthInfo * _Nonnull)OfflineAuthInfo {
  219. // NSString *msg = [NSString stringWithFormat:@"OfflineAuthInfo:err_code=%@,err_msg=%@\n,deviceId=%@\n,expireTime=%@\n,respose=%@\n,authVoiceList=%@",@(OfflineAuthInfo.err_code),OfflineAuthInfo.err_msg,OfflineAuthInfo.deviceId,OfflineAuthInfo.expireTime,OfflineAuthInfo.respose,OfflineAuthInfo.voiceAuthList];
  220. //
  221. // NSLog(@"%@",msg);
  222. // _messageTextView.text = msg;
  223. // [self scrollToBottom];
  224. }
  225. //---------QCloudPlayerDelegate---------
  226. //播放开始
  227. -(void) onTTSPlayStart{
  228. // _messageTextView.text = [NSString stringWithFormat:@"%@\n onTTSPlayStart",_messageTextView.text];
  229. // [self scrollToBottom];
  230. NSLog(@"onTTSPlayStart");
  231. self.state = NYVoiceType_Start;
  232. if(self.payCompletedBlock){
  233. self.payCompletedBlock(NYVoiceType_Start);
  234. }
  235. }
  236. //队列所有音频播放完成,音频等待中
  237. -(void) onTTSPlayWait{
  238. // _messageTextView.text = [NSString stringWithFormat:@"%@\n onTTSPlayWait",_messageTextView.text];
  239. // [self scrollToBottom];
  240. self.state = NYVoiceType_Wait;
  241. NSLog(@"onTTSPlayWait");
  242. if(self.payCompletedBlock){
  243. self.payCompletedBlock(NYVoiceType_Wait);
  244. }
  245. }
  246. //恢复播放
  247. -(void) onTTSPlayResume{
  248. // _messageTextView.text = [NSString stringWithFormat:@"%@\n onTTSPlayResume",_messageTextView.text];
  249. // [self scrollToBottom];
  250. NSLog(@"onTTSPlayResume");
  251. self.state = NYVoiceType_Resume;
  252. if(self.payCompletedBlock){
  253. self.payCompletedBlock(NYVoiceType_Resume);
  254. }
  255. }
  256. //暂停播放
  257. -(void) onTTSPlayPause{
  258. // _messageTextView.text = [NSString stringWithFormat:@"%@\n onTTSPlayPause",_messageTextView.text];
  259. // [self scrollToBottom];
  260. NSLog(@"onTTSPlayPause");
  261. self.state = NYVoiceType_Pause;
  262. if(self.payCompletedBlock){
  263. self.payCompletedBlock(NYVoiceType_Pause);
  264. }
  265. }
  266. //播放中止
  267. -(void)onTTSPlayStop{
  268. _cout = 0;
  269. // _messageTextView.text = [NSString stringWithFormat:@"%@\n onTTSPlayStop",_messageTextView.text];
  270. // [self scrollToBottom];
  271. NSLog(@"onTTSPlayStop");
  272. self.state = NYVoiceType_Stop;
  273. if(self.payCompletedBlock){
  274. self.payCompletedBlock(NYVoiceType_Stop);
  275. }
  276. }
  277. //播放器异常
  278. -(void)onTTSPlayError:(QCPlayerError* _Nullable)playError{
  279. // _messageTextView.text = [NSString stringWithFormat:@"%@\n playError.code==%@\n playError.massage==%@",_messageTextView.text,@(playError.mCode),playError.message];
  280. // [self scrollToBottom];
  281. NSLog(@"playError.code==%@,playError.massage==%@",@(playError.mCode),playError.message);
  282. }
  283. //即将播放播放下一句,即将播放音频对应的句子,以及这句话utteranceId
  284. /// 即将播放播放下一句,即将播放音频对应的句子,以及这句话utteranceId
  285. /// @param text 当前播放句子的文本
  286. /// @param utteranceId 当前播放音频对应的ID
  287. -(void) onTTSPlayNextWithText:(NSString* _Nullable)text UtteranceId:(NSString* _Nullable)utteranceId{
  288. NSLog(@"text==%@,utteranceId==%@",text,utteranceId);
  289. QCloudTTSEngine *tts = [QCloudTTSEngine getShareInstance];
  290. if ([_player getAudioQueueSize] < 10) {
  291. if (_textArr.count > 0) {
  292. NSLog(@"AudioQueueSize=%@",@([_player getAudioQueueSize]));
  293. [tts synthesize:_textArr[0] UtteranceId:[NSString stringWithFormat:@"%d",_cout++]];
  294. [_textArr removeObjectAtIndex:0];
  295. }
  296. }
  297. }
  298. /// 当前播放的字符,当前播放的字符在所在的句子中的下标.
  299. /// @param currentWord 当前读到的单个字,是一个估算值不一定准确
  300. /// @param currentIdex 当前播放句子中读到文字的下标
  301. -(void)onTTSPlayProgressWithCurrentWord:(NSString*_Nullable)currentWord CurrentIndex:(NSInteger)currentIdex{
  302. // _messageTextView.text = [NSString stringWithFormat:@"%@\n CurrentWord==%@\n currentIdex==%@",_messageTextView.text,currentWord,@(currentIdex)];
  303. // NSLog(@"CurrentWord==%@,currentIdex==%@",currentWord,@(currentIdex));
  304. // [self scrollToBottom];
  305. }
  306. /*===============================使用自定义分割逻辑,以下为句子分割示例=====================*/
  307. #define Sentence_Count_Max 50 //单次最大请求字数,建议首句不要设置太长
  308. /*
  309. API最大请求字数详见官网文档https://cloud.tencent.com/document/product/1073/37995
  310. */
  311. - (NSMutableArray *)breakIntoSentencesFromString:(NSString *)string
  312. {
  313. NSMutableArray *stringsArr = [NSMutableArray array];
  314. NSString *splitString = @",;,;";
  315. //先按句子分割
  316. [string enumerateSubstringsInRange:NSMakeRange(0, string.length) options:NSStringEnumerationBySentences usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
  317. //后台接收字符不能太长
  318. const char *commitChar = [substring UTF8String];
  319. if (strlen(commitChar) > Sentence_Count_Max * 3) { //UTF-8中文的字符长度一般是3个字节,英文的长度为1
  320. //再按分号逗号分割
  321. NSArray *textArr = [self separatedString:substring split:splitString];
  322. for (NSString *object in textArr) {
  323. //再按字数分割
  324. const char *objectChar = [object UTF8String];
  325. if (strlen(objectChar) > Sentence_Count_Max * 3) {
  326. NSString *remainStr = object;
  327. while (remainStr.length > Sentence_Count_Max) {
  328. NSString *sentence = [remainStr substringToIndex:Sentence_Count_Max];
  329. if (![self isAllSplitString:sentence split:splitString]
  330. &&![self isPunct:sentence]) {
  331. [stringsArr addObject:sentence];
  332. }
  333. remainStr = [remainStr substringFromIndex:Sentence_Count_Max];
  334. }
  335. if (![self isAllSplitString:remainStr split:splitString]
  336. &&![self isPunct:remainStr]) {
  337. [stringsArr addObject:remainStr];
  338. }
  339. }
  340. else {
  341. if (![self isAllSplitString:object split:splitString]
  342. &&![self isPunct:object]) {
  343. [stringsArr addObject:object];
  344. }
  345. }
  346. }
  347. }else{
  348. if (![self isAllSplitString:substring split:splitString]
  349. &&![self isPunct:substring]) {
  350. [stringsArr addObject:substring];
  351. }
  352. }
  353. }];
  354. return stringsArr;
  355. }
  356. - (NSArray *)separatedString:(NSString *)text split:(NSString *)split {
  357. NSMutableArray *mArray = [NSMutableArray array];
  358. NSInteger loc = 0;
  359. for (NSInteger i = 0; i < [text length]; i++) {
  360. if ([split rangeOfString:[text substringWithRange:NSMakeRange(i, 1)]].location != NSNotFound) {
  361. NSString *subString = [text substringWithRange:NSMakeRange(loc, i-loc+1)];
  362. [mArray addObject:subString];
  363. loc = i + 1;
  364. }
  365. if (i + 1 == [text length] && [split rangeOfString:[text substringWithRange:NSMakeRange(i, 1)]].location == NSNotFound) { //判断最后一个字符是否包含分隔符,如果不包含把最后一句添加到数组
  366. NSString *subString = [text substringWithRange:NSMakeRange(loc, i-loc+1)];
  367. [mArray addObject:subString];
  368. }
  369. }
  370. return [mArray copy];
  371. }
  372. - (BOOL)isAllSplitString:(NSString *)text split:(NSString *)split
  373. {
  374. split = @",;,;。";
  375. if ([text length] && [split length]) {
  376. BOOL all = YES;
  377. for (NSInteger i = 0; i < [text length]; i++) {
  378. if ([split rangeOfString:[text substringWithRange:NSMakeRange(i, 1)]].location == NSNotFound) {
  379. all = NO;
  380. break;
  381. }
  382. }
  383. return all;
  384. }
  385. else {
  386. return NO;
  387. }
  388. }
  389. -(BOOL)isPunct:(NSString *)string //校验是否全为符号
  390. {
  391. NSError *error = nil;
  392. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[\\p{P}~^<>]" options:NSRegularExpressionCaseInsensitive error:&error];
  393. NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@""];
  394. NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
  395. NSString *trimmedStr = [modifiedString stringByTrimmingCharactersInSet:set];
  396. if (!trimmedStr.length) {
  397. return YES;
  398. }
  399. return NO;
  400. }
  401. @end