add TencentTTS

This commit is contained in:
oscarz
2024-08-30 17:46:21 +08:00
parent 27c160beaf
commit 487aead433
30 changed files with 1558 additions and 14 deletions

View File

@ -0,0 +1,25 @@
//
// QPlayerError.h
// cloud-tts-sdk-ios
//
// Created by renqiu on 2022/1/11.
//
#import <Foundation/Foundation.h>
typedef NS_ENUM (NSInteger,QCPlayerErrorCode){
QPLAYER_ERROR_CODE_EXCEPTION = -201,
QPLAYER_ERROR_CODE_PLAY_QUEUE_IS_FULL = -202,
QPLAYER_ERROR_CODE_AUDIO_READ_FAILEDL = -203,
QPLAYER_ERROR_CODE_UNKNOW = -204,
};
@interface QCPlayerError : NSObject
@property (nonatomic, assign) NSInteger mCode;
@property (nonatomic,copy)NSString *message;
@property (nonatomic,strong)NSError *errorMessage;
+(instancetype)getQCPlayerErrorWithMcode:(NSInteger)mCode ErrorMessage:(NSError*)errorMessage;
@end

View File

@ -0,0 +1,47 @@
//
// QCloudMediaPlayer.h
// cloud-tts-sdk-ios
//
// Created by renqiu on 2022/1/11.
//
#import <Foundation/Foundation.h>
#import <QCloudTTS/QCPlayerError.h>
#import <QCloudTTS/QCloudPlayerDelegate.h>
/// <#Description#>
@interface QCloudMediaPlayer : NSObject
@property (assign,nonnull)id <QCloudPlayerDelegate>playerDelegate;
//
/// 数据入队列
/// @param data 加入队列的音频
/// @param text 音频对应的文本
/// @param utteranceId 音频对应的ID
-(void)enqueueWithData:(NSData* _Nonnull )data Text:(NSString* _Nullable)text UtteranceId:(NSString* _Nullable)utteranceId;
//
/// 数据入队列
/// @param data 加入队列的音频
/// @param text 音频对应的文本
/// @param utteranceId 音频对应的ID
/// @param server 服务端返回的信息,包含Subtitles时会使用Subtitle来进行时间戳判断
-(void)enqueueWithData:(NSData* _Nonnull )data Text:(NSString* _Nullable)text UtteranceId:(NSString* _Nullable)utteranceId RespJson:(NSString* _Nullable)respjson;
/// 数据入队列
/// @param file 加入队列的音频文件
/// @param text 音频文件对应的文本
/// @param utteranceId 音频文件对应的ID
-(void)enqueueWithFile:(NSURL* _Nullable)file Text:(NSString* _Nullable)text UtteranceId:(NSString* _Nullable)utteranceId;
//
/// 停止播放
-(QCPlayerError* _Nullable)StopPlay;
/// 暂停播放
-(QCPlayerError* _Nullable)PausePlay;
/// 恢复播放
-(QCPlayerError* _Nullable)ResumePlay;
-(NSInteger)getAudioQueueSize;
@end

View File

@ -0,0 +1,35 @@
//
// QCloudMediaPlayer.h
// cloud-tts-sdk-ios
//
// Created by renqiu on 2022/1/11.
//
#import <Foundation/Foundation.h>
#import <QCloudTTS/QCPlayerError.h>
#import <QCloudTTS/QCloudMediaPlayer.h>
/// <#Description#>
@interface QCloudMediaPlayer2 : NSObject
@property (weak)id <QCloudPlayerDelegate> _Nullable playerDelegate;
//
/// 数据入队列
/// @param data 加入队列的音频
/// @param text 音频对应的文本
/// @param utteranceId 音频对应的ID
-(void)enqueueWithData:(NSData* _Nonnull )data Text:(NSString* _Nullable)text UtteranceId:(NSString* _Nullable)utteranceId;
/// 数据入队列
/// @param file 加入队列的音频文件
/// @param text 音频文件对应的文本
/// @param utteranceId 音频文件对应的ID
-(void)enqueueWithFile:(NSURL* _Nullable)file Text:(NSString* _Nullable)text UtteranceId:(NSString* _Nullable)utteranceId;
//
/// 停止播放
-(QCPlayerError* _Nullable)StopPlay;
/// 暂停播放
-(QCPlayerError* _Nullable)PausePlay;
/// 恢复播放
-(QCPlayerError* _Nullable)ResumePlay;
-(NSInteger)getAudioQueueSize;
@end

View File

@ -0,0 +1,48 @@
//
// QCloudOfflineAuthInfo.h
// QCloudTTS
//
// Created by dgw on 2022/6/15.
//
#import <Foundation/Foundation.h>
/*如果您下载的是在线版TTS SDK ,请忽略此接口文件*/
NS_ASSUME_NONNULL_BEGIN
// 离线授权错误吗
typedef NS_ENUM(NSInteger, AuthErrorCode) {
OFFLINE_AUTH_SUCCESS = 0,//"Auth Success"
OFFLINE_AUTH_NETWORK_CONNECT_FAILED = -10,//"Network connect failed."
OFFLINE_AUTH_NETWORK_SERVER_AUTH_FAILED = -11,//"Server Authorization Error See Response Message."
OFFLINE_AUTH_PARAMETERS_ERROR = -12,//"Parameter cannot be empty."
OFFLINE_AUTH_PACKAGENAME_ERROR = -13,//"Authorization package name error."
OFFLINE_AUTH_DEVICE_ID_ERROR = -14,//"Authorization device ID error."
OFFLINE_AUTH_GET_DEVICE_ID_FAILED = -15,//"The device is abnormal and the device ID cannot be obtained."
OFFLINE_AUTH_PLATFORM_ERROR = -16,//"The platform is not authorized."
OFFLINE_AUTH_BIZCODE_ERROR = -17,//"License business does not match."
OFFLINE_AUTH_EXPIRED = -18,//"Authorization has expired.")
OFFLINE_AUTH_JSON_PARSE_FAILED = -19,//"JSON Parsing Error."
OFFLINE_AUTH_DECODE_ERROR = -20,//"Could not decode license, please check input parameters."
OFFLINE_AUTH_UNKNOWN_ERROR = -21,//"Unknown authorization error."
};
// 离线授权信息类
@interface QCloudOfflineAuthInfo : NSObject
@property (nonatomic, copy, nullable) NSString* respose; //使用在线拉取授权时服务器返回到json数据
@property (nonatomic, assign) AuthErrorCode err_code; //错误码
@property (nonatomic, copy, nullable) NSString* err_msg; //错误信息
@property (nonatomic, copy, nullable) NSString* deviceId; //设备id
@property (nonatomic, copy, nullable) NSString* expireTime; //到期时间
@property (nonatomic, copy, nullable) NSString* voiceAuthList; //已授权的音色名列表,用分号隔开
+(instancetype _Nonnull )getQCloudOfflineAuthInfo:(AuthErrorCode)err_code Respose:(NSString*_Nullable)respose;
+(NSString* _Nonnull )getErrorMsg:(AuthErrorCode)code;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,37 @@
#import <Foundation/Foundation.h>
#import <QCloudTTS/QCPlayerError.h>
@protocol QCloudPlayerDelegate <NSObject>
//播放开始
-(void) onTTSPlayStart;
//队列所有音频播放完成,音频等待中
-(void) onTTSPlayWait;
//恢复播放
-(void) onTTSPlayResume;
//暂停播放
-(void) onTTSPlayPause;
//播放中止
-(void)onTTSPlayStop;
//即将播放播放下一句即将播放音频对应的句子以及这句话utteranceId
/// 即将播放播放下一句即将播放音频对应的句子以及这句话utteranceId
/// @param text 当前播放句子的文本
/// @param utteranceId 当前播放音频对应的ID
-(void) onTTSPlayNextWithText:(NSString* _Nullable)text UtteranceId:(NSString* _Nullable)utteranceId;
//播放器异常
-(void)onTTSPlayError:(QCPlayerError* _Nullable)playError;
/// 当前播放的字符,当前播放的字符在所在的句子中的下标.
/// @param currentWord 当前读到的单个字,是一个估算值不一定准确
/// @param currentIdex 当前播放句子中读到文字的下标
-(void)onTTSPlayProgressWithCurrentWord:(NSString*_Nullable)currentWord CurrentIndex:(NSInteger)currentIdex;
@end

View File

@ -0,0 +1,222 @@
//
// QCloudTTSEngine.h
// cloud-tts-sdk-ios
//
// Created by dgw on 2022/1/10.
//
#import <Foundation/Foundation.h>
#import <QCloudTTS/TtsError.h>
#import <QCloudTTS/QCloudOfflineAuthInfo.h>
typedef NS_ENUM(NSUInteger, TtsMode) {
TTS_MODE_ONLINE = 0, //在线模式
TTS_MODE_OFFLINE = 1, //离线模式
TTS_MODE_MIX = 2 //离在线混合模式
};
/// 回调接口
@protocol QCloudTTSEngineDelegate <NSObject>
@optional
/// 合成结果回调
/// @param data 音频数据
/// @param utteranceId utteranceId
/// @param text text
/// @param type 该句话是以何种引擎生成的0:在线 1:离线
/// @deprecated
-(void) onSynthesizeData:(NSData *_Nullable)data UtteranceId:(NSString *_Nullable)utteranceId Text:(NSString *_Nullable)text EngineType:(NSInteger)type;
/// 合成结果回调
/// @param data 音频数据
/// @param utteranceId utteranceId
/// @param text text
/// @param type 该句话是以何种引擎生成的0:在线 1:离线
/// @param requestId 请求ID仅engineType为0时不为nil用于排查问题
-(void) onSynthesizeData:(NSData *_Nullable)data UtteranceId:(NSString *_Nullable)utteranceId Text:(NSString *_Nullable)text EngineType:(NSInteger)type RequestId:(NSString* _Nullable)requestId;
/// 合成结果回调
/// @param data 音频数据
/// @param utteranceId utteranceId
/// @param text text
/// @param type 该句话是以何种引擎生成的0:在线 1:离线
/// @param respJson 请求结果
-(void) onSynthesizeData:(NSData *_Nullable)data UtteranceId:(NSString *_Nullable)utteranceId Text:(NSString *_Nullable)text EngineType:(NSInteger)type RequestId:(NSString* _Nullable)requestId RespJson:(NSString* _Nullable)respJson;
@required
/// 错误回调
/// @param error 错误信息
/// @param utteranceId utteranceId
/// @param text text
-(void) onError:(TtsError *_Nullable)error UtteranceId:(NSString *_Nullable)utteranceId Text:(NSString *_Nullable)text;
/// 返回离线合成模块授权信息,使用混合或者离线模式时,收到此方法成功回调后才可以调用合成接口
/// 如果您下载的是在线版TTS SDK ,或者只使用在线合成模式,请忽略此方法
/// @param OfflineAuthInfo 授权信息当OfflineAuthInfo.err_msg为0时授权成功详见QCloudOfflineAuthInfo.h
-(void) onOfflineAuthInfo:(QCloudOfflineAuthInfo* _Nonnull )OfflineAuthInfo;
@end
/// 语音合成引擎接口
@interface QCloudTTSEngine : NSObject
/// 获得QCloudTTSEngine实例
+(id _Nullable )getShareInstance;
/// 销毁QCloudTTSEngine实例
+(void) instanceRelease;
/// 初始化引擎
/// @param mode 引擎模式在线、离线、混合如切换引擎需要先执行instanceRelease
/// @param delegate 用于接收结果的代理
-(void) engineInit:(TtsMode)mode Delegate:(id<QCloudTTSEngineDelegate> _Nonnull) delegate;
/// 取消合成,清空内部合成队列
-(TtsError *_Nullable) cancel;
/// 合成接口,支持持续调用添加文本
/// @param text 需要合成的文本
/// @param utteranceId 用于标记文本的id合成完成后随音频文件或者错误接口返回,可为空
-(TtsError *_Nullable) synthesize:(NSString *_Nonnull)text UtteranceId:(NSString *_Nullable)utteranceId;
/// 配置在线引擎鉴权参数
/// @param appId appId
/// @param secretId secretId
/// @param secretKey secretKey
/// @param token token可为空如果使用sts临时证书鉴权secretId和secretKey均入参临时的同时需要入参对应的token
/// sts临时证书具体详见https://cloud.tencent.com/document/product/598/33416 开发调试时建议先用普通方式鉴权token填nil
-(void) setOnlineAuthParam:(NSInteger)appId SecretId:(NSString* _Nonnull)secretId SecretKey:(NSString* _Nonnull)secretKey Token:(NSString* _Nullable)token;
///online语音相关设置
/// 设置在线语速,对setOnlineParam的封装
/// @param voiceSpeed 默认0代表正常速度
-(void)setOnlineVoiceSpeed:(float)voiceSpeed;
/// 设置在线音色ID,对setOnlineParam的封装
/// @param voiceType 默认1001音色id可查看官网文档https://cloud.tencent.com/document/product/1073/37995
-(void)setOnlineVoiceType:(int)voiceType;
/// 设置在线引擎音量,对setOnlineParam的封装
/// @param voiceVolume 默认0代表正常音量没有静音选项
-(void)setOnlineVoiceVolume:(float)voiceVolume;
/// 设置主语言类型,对setOnlineParam的封装
/// @param primaryLanguage 1:中文 2:英文 默认1
-(void)setOnlineVoiceLanguage:(int)primaryLanguage;
///在线编码格式,如无业务特殊需求不建议更改(默认"mp3",目前支持"mp3","wav","pcm",若更改为pcm不支持直接播放,对setOnlineParam的封装
-(void)setOnlineCodec:(NSString* _Nonnull) code;
/// 项目ID
/// @param projectId 默认0, 对setOnlineParam的封装
-(void)setOnlineProjectId:(int)projectId;
/// 是否开启时间戳功能默认为false,对setOnlineParam的封装
- (void)setOnlineEnableSubtitle:(Boolean)val;
/// 断句敏感阈值默认值为0,对setOnlineParam的封装
- (void)setOnlineSegmentRate:(int)val;
/// 控制合成音频的情感,仅支持多情感音色使用,对setOnlineParam的封装
- (void)setOnlineEmotionCategory:(NSString* _Nullable)val;
/// 控制合成音频情感程度,对setOnlineParam的封装
- (void)setOnlineEmotionIntensity:(int)val;
/// 设置自定义参数,可使用该方法控制请求时的参数
/// @param value为nil时将删除参数,否则会在请求中添加参数
- (void)setOnlineParam:(NSString* _Nonnull)key value:(NSObject* _Nullable)value;
/// 设置区域
/// @param region 默认为ap-shanghai,无特殊需求请勿更改
- (void)setOnlineRegion:(NSString *)region;
//设置是否输出日志
-(void)setEnableDebugLog:(BOOL)enableDebugLog;
/// timeoutIntervalForRequest ,0.5s - 30s 默认15000ms(15s)
/// @param timeout [2200,60000] 单位ms
- (void)setTimeoutIntervalForRequest:(int)timeout;
/// timeoutIntervalForResource 2.2s - 60s 默认30000ms(30s)
/// @param timeout [500,30000] 单位ms
-(void)setTimeoutIntervalForResource:(int) timeout;
/// Mix模式下出现网络错误后的检测间隔时间, 默认值5分钟
///注意:每次检测时将使用所入参的一句文本请求服务器,如果后端合成成功将会额外消耗该句字数的合成额度
/// @param checkNetworkIntervalTime 大于等于0 单位s, 等于0时持续检测直到成功
-(void)setCheckNetworkIntervalTime:(int) checkNetworkIntervalTime;
//以下为离线语音相关参数配置
//resourceDir 离线资源路径
-(void)setOfflineResourceDir:(NSString * _Nonnull) resourceDir;
//voiceSpeed [0.5,2.0]
-(void)setOfflineVoiceSpeed:(float) voiceSpeed;
//voiceType 离线音色名称,名称配置位于音色资源目录\voices\config.json 中可自行指定更多的音色demo中仅提供"pb"、"femalen"两种
-(void)setOfflineVoiceType:(NSString *_Nonnull) voiceType;
//voiceVolume >0
-(void)setOfflineVoiceVolume:(float) voiceVolume;
/// 配置离线TTS授权参数: 在线下载密钥(第一次激活设备需要联网)
/// @param licPk 密钥对应的licPk,请在腾讯云官网页面获取,或者由腾讯云商务线下下发
/// @param licKey 密钥对应的licKey,请在腾讯云官网页面获取,或者由腾讯云商务线下下发
/// @param secretId 腾讯云secretId 可能与在线模式不是同一个账号需要输入购买离线SDK对应的账号的secretId
/// @param secretKey 腾讯云 secretKey可能与在线模式不是同一个账号需要输入购买离线SDK对应的账号的secretKey
/// @param token token可为空如果使用sts临时证书鉴权secretId和secretKey均入参临时的同时需要入参对应的token
-(void)setOfflineAuthParamDoOnline:(NSString* _Nonnull)licPk
LicKey:(NSString* _Nonnull)licKey
SecretId:(NSString* _Nonnull)secretId
SecretKey:(NSString* _Nonnull)secretKey
Token:(NSString* _Nullable)token;
/// 配置离线TTS授权参数: 在线下载密钥(第一次激活设备需要联网)
/// @param licPk 密钥对应的licPk,请在腾讯云官网页面获取,或者由腾讯云商务线下下发
/// @param licKey 密钥对应的licKey,请在腾讯云官网页面获取,或者由腾讯云商务线下下发
/// @param secretId 腾讯云secretId 可能与在线模式不是同一个账号需要输入购买离线SDK对应的账号的secretId
/// @param secretKey 腾讯云 secretKey可能与在线模式不是同一个账号需要输入购买离线SDK对应的账号的secretKey
/// @param token token可为空如果使用sts临时证书鉴权secretId和secretKey均入参临时的同时需要入参对应的token
/// @param refreshAuth 是否强制联网刷新授权(NO:仅第一次联网激活下载授权文件; YES:联网刷新授权文件,无网络下将激活失败)
-(void)setOfflineAuthParamDoOnline:(NSString* _Nonnull)licPk
LicKey:(NSString* _Nonnull)licKey
SecretId:(NSString* _Nonnull)secretId
SecretKey:(NSString* _Nonnull)secretKey
Token:(NSString* _Nullable)token
RefreshAuth:(BOOL)refreshAuth;
/// 配置离线TTS授权参数: 直接传入密钥(第一次激活也不需要联网)
/// @param lic 授权密钥 ,请在腾讯云官网页面获取,或者由腾讯云商务线下下发
/// @param licPk 该密钥对应的licPk请在腾讯云官网页面获取或者由腾讯云商务线下下发
/// @param licSign 该密钥对应的licSign请在腾讯云官网页面获取或者由腾讯云商务线下下发
-(void)setOfflineAuthParamDoOffline:(NSString* _Nonnull)lic
LicPk:(NSString* _Nonnull)licPk
LicSign:(NSString* _Nonnull)licSign;
@end

View File

@ -0,0 +1,70 @@
//
// TtsError.h
// cloud-tts-sdk-ios
//
// Created by dgw on 2022/1/10.
//
#ifndef TtsError_h
#define TtsError_h
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger, TtsErrorCode) {
TTS_ERROR_CODE_UNINITIALIZED = -100, //Engine uninitialized
TTS_ERROR_CODE_GENERATE_SIGN_FAIL = -101, //generate sign failed
TTS_ERROR_CODE_NETWORK_CONNECT_FAILED = 102, //network connect failed
TTS_ERROR_CODE_DECODE_FAIL = -103, //JSON Response Parsing Error
TTS_ERROR_CODE_SERVER_RESPONSE_ERROR = -104, //Server response error
TTS_ERROR_CODE_CANCEL_FAILURE = -106, //Cancel failureplease try again later
TTS_ERROR_CODE_OFFLINE_FAILURE = -107,//Offline synthesize failed, please check your text and VoiceType
TTS_ERROR_CODE_OFFLINE_INIT_FAILURE = -108,//Offline engine initialization failed, please check resource files
TTS_ERROR_CODE_OFFLINE_AUTH_FAILURE = -109,//Offline engine auth failure
TTS_ERROR_CODE_OFFLINE_TEXT_TOO_LONG = -110, //Offline synthesize failed,text too long,MAX <= 1024 byte
TTS_ERROR_CODE_OFFLINE_VOICE_AUTH_FAILURE = -111, //This voice not authorized, please change it
TTS_ERROR_CODE_OFFLINE_NOSUPPORT = -900 //This SDK only supports online mode
};
/// 后端返回的错误信息
@interface TtsServiceError : NSObject
@property (nonatomic, copy) NSString* _Nullable err_code;
@property (nonatomic, copy) NSString* _Nullable msg;
@property (nonatomic, copy) NSString* _Nullable respose;
+(instancetype _Nonnull)getTtsServiceError:(NSString *_Nullable)err_code ErrorMsg:(NSString* _Nullable)msg Respose:(NSString* _Nullable)respose;
@end
/// 错误信息类
@interface TtsError : NSObject
@property (nonatomic, assign) TtsErrorCode err_code; //错误码
@property (nonatomic, copy, nullable) NSString* msg; //错误信息
@property (nonatomic, copy, nullable) NSError * error; //系统抛出的错误信息(不一定有,可能为空)
@property (nonatomic, strong ,nullable) TtsServiceError * serviceError;//服务器返回的错误信息(不一定有,可能为空)
+(instancetype _Nonnull)getTtsError:(TtsErrorCode)err_code
TtsServiceError:(TtsServiceError* _Nullable)serviceError
NSErrorMsg:(NSError *_Nullable)error;
@end
#endif /* TtsError_h */