import axios , { AxiosRequestConfig } from "axios" ; import crypto from "crypto" ; import http from "http" ; import querystring from "querystring" ; import { isNumber } from "util" ; import { ICommonResponseBody , ITTSResponseBody } from "./responsemessage" ; export class Client { type { string } memberof public IATServiceEndPoint : string = "http://api.xfyun.cn/v1/service/v1/iat" ; type { string } memberof public TTSServiceEndPoint : string = "http://api.xfyun.cn/v1/service/v1/tts" ; type { string } memberof public ISEServiceEndPoint : string = "http://api.xfyun.cn/v1/service/v1/ise" ; type { string } memberof public AppID : string ; type { IProxyConfig } memberof public Proxy ? : IProxyConfig ; type { string } memberof public IATAppKey ? : string ; type { string } memberof public TTSAppKey ? : string ; type { string } memberof public ISEAppKey ? : string ; param { string } appid param { string } iatappkey param { string } ttsappkey param { string } iseappkey memberof constructor ( appid , iatappkey ? , ttsappkey ? , iseappkey ? ) { this . AppID = appid ; this . IATAppKey = iatappkey ; this . TTSAppKey = ttsappkey ; this . ISEAppKey = iseappkey ; } param { string } text param { ( TTSAufType | string ) } auf param { ( TTSAueType | string ) } aue param { ( TTSVoiceName | string ) } voicename param { number } speed param { number } volume param { number } pitch param { ( TTSEngineType | string ) } enginetype param { ( TTSTextType | string ) } texttype returns { Promise < ITTSResponseBody > } memberof public async TTS ( text , auf , aue , voicename = TTSVoiceName . XiaoYan , speed = 50 , volume = 50 , pitch = 50 , enginetype = TTSEngineType . INTP65CN , texttype = TTSTextType . TEXT ) { return new Promise < ITTSResponseBody > ( ( resolve , reject ) => { try { if ( this . TTSAppKey === undefined ) { reject ( "尚未设定语音合成服务的 AppKey" ) ; } speed = Math . max ( Math . min ( Math . floor ( speed ) , 100 ) , 0 ) ; volume = Math . max ( Math . min ( Math . floor ( volume ) , 100 ) , 0 ) ; pitch = Math . max ( Math . min ( Math . floor ( pitch ) , 100 ) , 0 ) ; const requestParams = { auf , aue , voice_name : voicename , speed : speed ? speed . toFixed ( 0 ) : undefined , volume : volume ? volume . toFixed ( 0 ) : undefined , pitch : pitch ? pitch . toFixed ( 0 ) : undefined , engine_type : enginetype , text_type : texttype , } ; const requestParamsJson = JSON . stringify ( requestParams ) ; const requestConfig = this . SetRequestParams ( requestParamsJson , this . TTSAppKey as string ) ; requestConfig . responseType = "arraybuffer" ; const requestData = querystring . stringify ( { text } ) ; axios . post ( this . TTSServiceEndPoint , requestData , requestConfig ) . then ( ( response ) => { const result = { } ; if ( response . headers [ "content-type" ] === "audio/mpeg" ) { result . sid = response . headers . sid ; result . audio = new Buffer ( response . data , "binary" ) ; } else if ( response . headers [ "content-type" ] === "text/plain" ) { const x = new Buffer ( response . data , "utf8" ) ; const resultString = x . toString ( "utf8" ) ; const resultObject = JSON . parse ( resultString ) ; result . code = resultObject . code ; result . desc = resultObject . desc ; result . sid = resultObject . sid ; } else { reject ( "请求中发生未知错误" ) ; } resolve ( result ) ; } ) . catch ( ( reason ) => { reject ( reason ) ; } ) ; } catch ( error ) { reject ( error ) ; } } ) ; } param { ( any [ ] | Buffer | ArrayBuffer ) } audio param { ( IATEngineType | string ) } enginetype param { ( IATAueType | string ) } aue param { ( string | number ) } speexsize param { string } scene param { number } vadeos returns { Promise < IResponseBody > } memberof public async IAT ( audio , enginetype , aue , speexsize ? , scene ? , vadeos = 1800 ) { return new Promise < ICommonResponseBody > ( ( resolve , reject ) => { try { if ( this . IATAppKey === undefined ) { reject ( "尚未设定语音听写服务的 AppKey" ) ; } const requestParams = { engine_type : enginetype , aue , speexsize : speexsize ? speexsize . toString ( ) : undefined , scene : scene ? encodeURIComponent ( scene ) : undefined , vadeos : vadeos ? vadeos . toString ( ) : undefined , } ; const requestParamsJson = JSON . stringify ( requestParams ) ; const requestConfig = this . SetRequestParams ( requestParamsJson , this . IATAppKey as string ) ; const requestData = querystring . stringify ( { audio : this . GetEncodedContent ( audio ) } ) ; axios . post ( this . IATServiceEndPoint , requestData , requestConfig ) . then ( ( response ) => { resolve ( { code : response . data . code , data : response . data . data , desc : response . data . desc , sid : response . data . sid , } ) ; } ) . catch ( ( reason ) => { reject ( reason ) ; } ) ; } catch ( error ) { reject ( error ) ; } } ) ; } param { ( any [ ] | Buffer | ArrayBuffer ) } audio param { string } text param { ( ISEAueType | string ) } aue param { ( ISELanguageType | string ) } language param { ( ISECategoryType | string ) } category param { ( ISEResultLevelType | string ) } resultlevel param { number } speexsize param { ISEExtraAbilityType } extraability returns { Promise < ICommonResponseBody > } memberof see 有空再补充返回结果的内容吧 public async ISE ( audio , text , aue , language , category , resultlevel = ISEResultLevelType . COMPLETE , speexsize ? , extraability ? ) { return new Promise < ICommonResponseBody > ( async ( resolve , reject ) => { try { if ( this . ISEAppKey === undefined ) { reject ( "尚未设定语音评测服务的 AppKey" ) ; } const requestParams = { aue , speex_size : speexsize ? Math . floor ( speexsize ) . toString ( ) : undefined , result_level : resultlevel , language , category , extra_ability : extraability , } ; const requestParamsJson = JSON . stringify ( requestParams ) ; const requestConfig = this . SetRequestParams ( requestParamsJson , this . ISEAppKey as string ) ; const requestData = querystring . stringify ( { audio : this . GetEncodedContent ( audio ) , text } ) ; const result = await axios . post < ICommonResponseBody > ( this . ISEServiceEndPoint , requestData , requestConfig ) ; resolve ( result . data ) ; } catch ( error ) { reject ( error ) ; } } ) ; } private SetRequestParams ( requestParamsJson , appkey ) { const xAppid = this . AppID ; const xParam = Buffer . from ( requestParamsJson ) . toString ( "base64" ) ; const xCurTime = Math . floor ( ( new Date ( ) ) . valueOf ( ) / 1000 ) ; const md5 = crypto . createHash ( "md5" ) ; const xCheckSum = md5 . update ( appkey + xCurTime + xParam ) . digest ( "hex" ) ; const requestConfig = { headers : { "X-Appid" : xAppid , "X-CurTime" : xCurTime , "X-Param" : xParam , "X-CheckSum" : xCheckSum , "Content-Type" : "application/x-www-form-urlencoded; charset=utf-8" , } , } ; if ( this . Proxy ) { requestConfig . proxy = this . Proxy ; } return requestConfig ; } private GetEncodedContent ( fileContent ) { const base64ed = encodeURI ( Buffer . from ( fileContent ) . toString ( "base64" ) ) ; return base64ed ; } } export enum { number } export enum ISEAueType { /**
     * 未压缩的pcm或者wav格式
     */ RAW = "raw" , /**
     * speex格式
     */ SPEEX = "speex" } export enum { number } export enum ISELanguageType { /**
     * 中文
     */ CN = "cn" , /**
     * 英文
     */ EN = "en" } export enum { number } export enum ISEResultLevelType { /**
     * 精简
     */ PLAIN = "plain" , /**
     * 完整
     */ COMPLETE = "complete" } export enum { number } export enum ISECategoryType { /**
     * 单字，汉语特有
     */ SYLLABLE = "read_syllable" , /**
     * 单词
     */ WORD = "read_word" , /**
     * 句子
     */ SENTENCE = "read_sentence" , /**
     * 段落
     */ CHAPTER = "read_chapter" } export enum { number } export enum ISEExtraAbilityType { /**
     * 全维度
     */ MULTI_DIMENSION = "multi_dimension" , /**
     * 段落
     */ CHAPTER = "chapter" } export enum { number } export enum TTSTextType { /**
     * 普通格式文本
     */ TEXT = "text" } export enum { string } export enum TTSEngineType { /**
     * 普通效果
     */ AISound = "aisound" , /**
     * 中文
     */ INTP65CN = "intp65" , /**
     * 英文
     */ INTP65EN = "intp65_en" , /**
     * 小语种
     */ MTTS = "mtts" , /**
     * 优化效果
     */ X = "x" } export enum { string } export enum TTSAufType { L16_8K = "audio/L16;rate=8000" , L16_16K = "audio/L16;rate=16000" } export enum { string } export enum TTSAueType { /**
     * 未压缩的pcm或者wav格式
     */ RAW = "raw" , /**
     * mp3格式
     */ LAME = "lame" } export enum { string } export enum TTSVoiceName { /**
     * 晓燕
     */ XiaoYan = "xiaoyan" } export enum { string } export enum IATEngineType { /**
     * 8K采样率普通话
     */ SMS8K_Mandarin = "sms8k" , /**
     * 16k采样率普通话
     */ SMS16K_Mandarin = "sms16k" , /**
     * 8k采样率英语
     */ SMS8K_English = "sms-en8k" , /**
     * 16k采样率英语
     */ SMS16K_English = "sms-en16k" } export enum { string } export enum IATAueType { /**
     * 未压缩的pcm或wav格式
     */ RAW = "raw" , /**
     * speex格式
     */ SPEEX = "speex" , /**
     * 宽频speex格式
     */ SPEEX_WB = "speex-wb" } export interface export interface IProxyAuth { type { string } username : string ; type { string } password : string ; } export interface export interface IProxyConfig { type { string } memberof host : string ; type { number } memberof port : number ; type { IProxyAuth } memberof auth ? : IProxyAuth ; }