def get_tts_audio(self, message, language, options=None): """Load TTS from BaiduTTS.""" from aip import AipSpeech aip_speech = AipSpeech( self._app_data['appid'], self._app_data['apikey'], self._app_data['secretkey'] ) if options is None: result = aip_speech.synthesis( message, language, 1, self._speech_conf_data ) else: speech_data = self._speech_conf_data.copy() for key, value in options.items(): speech_data[_OPTIONS[key]] = value result = aip_speech.synthesis( message, language, 1, speech_data ) if isinstance(result, dict): _LOGGER.error( "Baidu TTS error-- err_no:%d; err_msg:%s; err_detail:%s", result['err_no'], result['err_msg'], result['err_detail'] ) return None, None return self._codec, result
def hecheng(name,rank,text,type): if type == 'sayhello': path = 'Hello-audio/sayhello/%s/hello%s.mp3'%(name,rank)#这里是存入sayhello的语音,需要传入name和等级两个参数 if type == 'opendoor': path = 'Hello-audio/opendoor/%s%s.mp3'%(name,rank) #开门的语音设置,只需要传入name:授权开门,或者不开这两个参数 if type == 'equipment': path = 'Hello-audio/equipment/%s%s.mp3'%(name,rank) client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) result = client.synthesis('%s'%(text),'zh',1,{ 'vol':6, 'per':4, }) if not isinstance(result, dict): with open(path, 'wb') as f: f.write(result) return path
def audio_out(string_, path, audio_set=None): # 百度云网络网络配置 APP_ID = '14636839' API_KEY = 'u34cdKCsR61tzM0r20Qnp3YM' SECRET_KEY = 'Rehs5G02EqbrRDDgGdPY9NKY2ZlCX8Oq' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) if len(string_) > 500: num = int(len(string_) / 500) # 第一段音频 result = client.synthesis(string_[0:500], 'zh', 1, {'spd': audio_set[0], 'pit': audio_set[1], 'vol': audio_set[2], 'per': audio_set[3]}) open(path, 'wb').write(result) # 中间段落的音频 for i in range(1, num): result = client.synthesis(string_[i * 500:(i + 1) * 500], 'zh', 1, {'spd': audio_set[0], 'pit': audio_set[1], 'vol': audio_set[2], 'per': audio_set[3]}) open(path, 'ab').write(result) # 最后一段音频 result = client.synthesis(string_[num * 500:], 'zh', 1, {'spd': audio_set[0], 'pit': audio_set[1], 'vol': audio_set[2], 'per': audio_set[3]}) open(path, 'ab').write(result) else: result = client.synthesis(string_, 'zh', 1, {'spd': audio_set[0], 'pit': audio_set[1], 'vol': audio_set[2], 'per': audio_set[3]}) open(path, 'wb').write(result)
def text2mp3(text, mp3): client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) with open(text, 'rb') as g: t = g.read() #textfile = "中新网7月20日电 据国家卫健委网站消息,7月19日0—24时,31个省(自治区、直辖市)和新疆生产建设兵团报告新增确诊病例22例,其中境外输入病例5例(四川3例,内蒙古1例,山东1例),本土病例17例(均在新疆);无新增死亡病例;新增疑似病例1例,为境外输入病例(在上海)。当日新增治愈出院病例24例,解除医学观察的密切接触者465人,重症病例较前一日增加2例。" if len(t) != 0: res = client.synthesis(t, "zh", 1, { "vlo": 6, "spd": 3, "pit": 7, "per": 4 }) with open(mp3, "wb") as f: f.write(res) else: res = client.synthesis("小播忙不过来了啊。", "zh", 1, { "vlo": 6, "spd": 3, "pit": 7, "per": 4 }) with open(mp3, "wb") as f: f.write(res)
def get_tts_audio(self, message, language, options=None): """Load TTS from BaiduTTS.""" aip_speech = AipSpeech( self._app_data["appid"], self._app_data["apikey"], self._app_data["secretkey"], ) if options is None: result = aip_speech.synthesis(message, language, 1, self._speech_conf_data) else: speech_data = self._speech_conf_data.copy() for key, value in options.items(): speech_data[_OPTIONS[key]] = value result = aip_speech.synthesis(message, language, 1, speech_data) if isinstance(result, dict): _LOGGER.error( "Baidu TTS error-- err_no:%d; err_msg:%s; err_detail:%s", result["err_no"], result["err_msg"], result["err_detail"], ) return None, None return self._codec, result
class play: def __init__(self): self.root=tk.Tk() self.root.title("语音合成系统") self.root.geometry("700x700") self.lb=tk.Label(self.root,text='请选择语音类型') self.tt=tk.Text(self.root,width=80,height=30) self.cb=ttk.Combobox(self.root, width=12) self.cb['values'] = ('请选择-----','甜美型','萝莉型','大叔型','精神小伙型') #设置下拉列表框的内容 self.cb.current(0) #将当前选择状态置为0,也就是第一项 self.cb.bind("<<ComboboxSelected>>",self.go) #绑定go函数,然后触发事件 self.lb1=tk.Label(self.root,text='请输入文件名:') self.e=tk.Entry(self.root,width=30,show=None, font=('Arial', 12)) self.b1=tk.Button(self.root, text='生成音频文件', width=10,height=1,command=self.sc) self.b1.place(x=200,y=520) self.lb.place(x=30,y=30) self.cb.place(x=154,y=30) self.e.place(x=130,y=490) self.lb1.place(x=30,y=490) self.tt.place(x=30,y=60) self.root.mainloop() #启动页面 def go(self,*arg): self.APP_ID = '18386899' self.API_KEY = 'OcPQ4cGoGBRtW23jemKvgmU5' self.SECRET_KEY = 'n2rGtvq9HmOGxgxG8H9a5kqFOes8ggHx' self.client = AipSpeech(self.APP_ID, self.API_KEY, self.SECRET_KEY) if self.cb.get()=='请选择-----': self.tt.delete('1.0','end') elif self.cb.get()=='甜美型': self.res=self.client.synthesis(self.tt.get('0.0','end'),'zh',1,{'vol':3,'spd':3,'pit':4,'per':0}) return self.res elif self.cb.get()=='萝莉型': self.res=self.client.synthesis(self.tt.get('0.0','end'),'zh',1,{'vol':2,'spd':2,'pit':3,'per':0}) return self.res elif self.cb.get()=='大叔型': self.res=self.client.synthesis(self.tt.get('0.0','end'),'zh',1,{'vol':5,'spd':7,'pit':6,'per':1}) return self.res elif self.cb.get()=='精神小伙型': self.res=self.client.synthesis(self.tt.get('0.0','end'),'zh',1,{'vol':7,'spd':8,'pit':8,'per':1}) return self.res def sc(self): self.go() aa=self.tt.get('0.0','end') ab=os.path.dirname(sys.argv[0])+os.sep+self.e.get()+'.mp3' if len(aa)>=1024: messagebox.showerror(title = '出错了!',message='^_^最多不超过1024个字节^_^') else: if not os.path.exists(ab): with open(ab,'wb') as f: f.write(self.res) messagebox.showinfo(title = '完毕!',message='生成完毕,文件在程序目录下') else: messagebox.showerror(title = '出错了!',message='文件名已存在')
class Baidu_Asr: #baidu语音识别 def __init__(self, APP_ID, API_KEY, SECRET_KEY, FILEPATH): self.__app_id = APP_ID self.__api_key = API_KEY self.__secret_key = SECRET_KEY self.__client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) # 创建百度用户 self.filepath = FILEPATH # 录音文件地址 def get_file_content(self): with open(self.filepath, 'rb') as fp: return fp.read() def get_asr_result(self): text = self.__client.asr(self.get_file_content(), 'wav', RESPEAKER_RATE, {'dev_pid': 1537, }) # print(text) if text['err_msg'] == 'success.': text_output = text['result'][0].encode('utf-8') # linux使用utf-8编码 return text_output else: return None #百度语音合成 def tts_file(self,file_name): f = open(filename,'r') command = f.read() if len(command) != 0: word = command f.close() result = self.__client.synthesis(word,'zh',1, { 'vol': 5,'per':0, }) # 合成正确返回audio.mp3,错误则返回dict if not isinstance(result, dict): with open('audio.mp3', 'wb') as f: f.write(result) f.close() print("tts successful") def tts_word(self,word): result = self.__client.synthesis(word,'zh',1, { 'vol': 5,'per':4, }) # 合成正确返回audio.mp3,错误则返回dict if not isinstance(result, dict): with open('audio.mp3', 'wb') as f: f.write(result) f.close() print("tts successful") else: print("error")
def change_voice(self, id): res = self.env['audio.config.settings'].get_default_info(None) # 定义常量 APP_ID = res['APP_ID'] API_KEY = res['API_KEY'] SECRET_KEY = res['SECRET_KEY'] # 初始化AipSpeech对象 aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY) result = aipSpeech.synthesis( self.context, 'zh', 1, { 'vol': 5, # 'per':4 }) # 识别正确返回语音二进制 错误则返回dict 参照下面错误码 strtime = time.localtime() datetime = time.strftime('%Y%m%d%H%M%S', strtime) path = os.path.dirname(os.path.split( os.path.realpath(__file__))[0]) + '\\static\\audio\\' + datetime if not isinstance(result, dict): with open(path + '.mp3', 'wb') as f: f.write(result) self.write( {'url': "/ct_marketing_voice/static/audio/" + datetime + ".mp3"}) self.write({'state': 'complte'})
class Voice2Word: def __init__(self): """ 你的 APPID AK SK """ self.APP_ID = '17896871' self.API_KEY = 'Qjva533G96GmTKVblEYRZWSA' self.SECRET_KEY = 'Ya67pHTOs6OCkL35A8LPXQnI13B1wEXV' self.client = AipSpeech(self.APP_ID, self.API_KEY, self.SECRET_KEY) # 读取文件 def __get_file_content(self, filePath): with open(filePath, 'rb') as fp: return fp.read() def voice2word(self, filename='output.wav'): # 识别本地文件 data = self.client.asr(self.__get_file_content(filename), 'wav', 16000, { 'dev_pid': 1536, }) try: data['result'] except: return [] return data['result'] def word2voice(self, word, filename='audio.mp3'): result = self.client.synthesis(word, 'zh', 1, { 'vol': 5, }) # 识别正确返回语音二进制 错误则返回dict 参照下面错误码 if not isinstance(result, dict): with open(filename, 'wb+') as f: f.write(result)
class AipClient(object): def __init__(self, appId, apiKey, secretKey): self.appId = appId self.apiKey = apiKey self.secretKey = secretKey self.lang = 'zh' #语言选择,填写zh self.ctp = 1 #客户端类型选择,web端填写1 self.voiceCfg = {} self.client = AipSpeech(appId, apiKey, secretKey) self.setVoiceCfg() def setVoiceCfg(self, voiceSet=None): if voiceSet == None: self.voiceCfg['spd'] = 2 # 语速,取值0-9,默认为5中语速 self.voiceCfg['pit'] = 5 # 音调,取值0-9,默认为5中语调 self.voiceCfg['vol'] = 12 # 音量,取值0-15,默认为5中音量 self.voiceCfg[ 'per'] = 0 # 发音人选择, 0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女 else: self.voiceCfg = voiceSet def getVoice(self, tex, voiceCfg=None): if voiceCfg == None: voiceCfg = self.voiceCfg voice = self.client.synthesis(tex, self.lang, self.ctp, voiceCfg) if not isinstance(voice, dict): return voice else: print('make voice error:%s' % voice['err_msg']) return None
def getPlayText(text): """ 直接拷贝百度申请注册的 APPID API_KEY SECRET_KEY """ APP_ID = '19610626' API_KEY = 'm8ayfZMs7yQtVXAK3QH3Ywl4' SECRET_KEY = 'R4z6xOhCy3x5pmsbkFwy7r9hM1r6VeNX' #初始化百度云服务客户端 client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) # 进行文字转语音 ,设置语音、语速、男声女声 result = client.synthesis( text, #text: 合成的文本,使用UTF-8编码,请注意文本长度必须小于1024字节 'zh', ## lang: 语言,中文:zh 英文:en 1, # 用户唯一标志号,目前写1 { 'per': 0, # 发音人选择,0为女生,1为男生,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女 'vol': 15, # 合成音频文件的准音量 'pit': 9, # 语调音调,取值0-9,默认为5 中语调 'spd': 5 # 语速 取值0-9,默认为5 中语速 }) # 识别正确返回语音二进制 错误则返回dict if not isinstance(result, dict): with open('.\data\empChecked.mp3', 'wb') as f: f.write(result) f.close() # 播放转换后的语音文件 playsound(".\data\empChecked.mp3")
def text2Audio(self, text, name): audiopath = r'test-res\%s-%s.mp3' % (self.name, name) if os.path.exists(audiopath): duration = librosa.get_duration(filename=audiopath) self.audioduration = duration print( "audiopath:%s with duration:%d is exist, do not need to generate agin." % (audiopath, duration)) return audiopath, duration APP_ID = '18701222' API_KEY = 'I1x7GAHVTKeiAImiHlG3xzGY' SECRET_KEY = 'I1GaRrI5NRqPnLxAerXsiO7vpiVREEEr' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) #result = client.synthesis('欢迎使用百度语音只能服务', 'zh', 1, {'vol':5,}) result = client.synthesis(text, 'zh', 1, { 'vol': 5, }) if not isinstance(result, dict): with open(audiopath, 'wb') as f: f.write(result) duration = librosa.get_duration(filename=audiopath) self.audioduration = duration return audiopath, duration
class BaiduTTS(): """ 使用百度语音合成技术 要使用本模块, 首先到 yuyin.baidu.com 注册一个开发者账号, 之后创建一个新应用, 然后在应用管理的"查看key"中获得 API Key 和 Secret Key 填入 config.xml 中. ... baidu_yuyin: appid: '9670645' api_key: 'qg4haN8b2bGvFtCbBGqhrmZy' secret_key: '585d4eccb50d306c401d7df138bb02e7' ... """ SLUG = "baidu-tts" def __init__(self, appid, api_key, secret_key, **args): super(self.__class__, self).__init__() self.client = AipSpeech(appid, api_key, secret_key) def get_speech(self, phrase): result = self.client.synthesis(phrase, 'zh', 1, { 'vol': 5, }) # 识别正确返回语音二进制 错误则返回dict 参照下面错误码 if not isinstance(result, dict): logger.info('语音合成成功') with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as f: f.write(result) tmpfile = f.name logger.info('合成路径:' + tmpfile) return tmpfile else: logger.critical('合成失败!')
def synthesis(self, words): if len(words) >= 1024: return False #从数据库中查找 filename = self.model.find(words) if filename != None: self.model.updateHit(words) return filename aipSpeech = AipSpeech(self.APP_ID, self.API_KEY, self.SECRET_KEY) result = aipSpeech.synthesis(words, 'zh', 1, {'vol': 10, 'per': 4}) if not isinstance(result, dict): name = hashlib.md5(words).hexdigest() filename = name + ".wav" tempfilename = "../temp/" + name + ".mp3" with open(tempfilename, 'wb') as f: f.write(result) subprocess.call([ 'ffmpeg', '-i', tempfilename, '-y', "../data/voice/" + filename ]) os.remove(tempfilename) #更新数据库 self.model.insert(words, filename) return filename return None else: return None
def handle_tts(req): APP_ID = '15844718' API_KEY = 'OOGuG62OAlu63CBfpKa8NLr7' SECRET_KEY = 'SYC2Gk63myGcHXMv3vg46o212FtjK7ZT' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) result = client.synthesis(req.text, 'zh', 1, { 'vol': 5, }) # 识别正确返回语音二进制 错误则返回dict if not isinstance(result, dict): if not os.path.exists(path): os.makedirs(path) print(path + req.text + '.mp3') with open(path + req.text + '.mp3', 'wb') as f: f.write(result) playsound(path + req.text + '.mp3') print "Transform %s succesfully" % (req.text) return 0 else: print(result) return 1
def getVoice(self): # 1.使用在线请求合成语音 # # 两次编码 # self.text = quote(quote(self.text)) # print(self.text) # # cuidInput = '54-BF-64-0A-A6-FE' # # token = '24.7852505c794af690f9960c1acdc99d5b.2592000.1596210517.282335-20679887' # requestUrl = 'https://tsn.baidu.com/text2audio?tex=%s&lan=zh&cuid=%s&ctp=1&tok=%s' % ( # self.text, cuidInput, token) # print(requestUrl) # 2.使用sdk合成语音 client = AipSpeech(self.appID, self.ak, self.sk) result = client.synthesis(self.text, 'zh', 1, { 'vol': 5, 'per': 0, 'spd': 5, 'pit': 5, }) # 识别正确返回语音二进制 错误则返回dict 参照下面错误码 if not isinstance(result, dict): with open('app\\static\\audio\\audio.mp3', 'wb+') as f: f.write(result) print("转换成功") else: print(result)
def play(self): global TTS_APPID, TTS_APPKEY, TTS_SECRET if not (TTS_APPID and TTS_APPKEY and TTS_SECRET): logger.error('Lack of app key configuration before speaking!') assert() client = AipSpeech(TTS_APPID, TTS_APPKEY, TTS_SECRET) result = client.synthesis(self.text, self.language, 1, { 'vol': 15, # 音量,取值0-15,默认为5中音量 'pit': 0, # 音调,取值0-9,默认为5中语调 'per': 0, # 发音人, 0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫 'cuid': platform }) if isinstance(result, dict): logger.error(result) return False tmp_file = mkstemp('-tts-voice.mp3') self.__filepath = tmp_file[1] with open(self.__filepath, 'w') as f: f.write(result) return self.replay()
class speak_lib(): #初始化函数 def __init__(self): """ 你的 APPID AK SK """ self.APP_ID = '16425806' self.API_KEY = 'Lz5VMGB5qufER9kxiik0IR1a' self.SECRET_KEY = 'EgGkUUHWHLtkkYG2GIpAswG9hK7hOzMH' #AipSpeech是语音合成的Python SDK客户端,为使用语音合成的开发人员提供了一系列的交互方法。 self.client = AipSpeech(self.APP_ID, self.API_KEY, self.SECRET_KEY) self.data_back = 0 #存储所有语音数据 self.file_name = 'auido.mp3' #为文件起名 open(self.file_name, 'wb') #打开一个文件,如果文件存在,则删除所有信息,重新以二进制写入。 #定义一个方法:以女生发音,需要一个data参数。data存储所要生成的文字 def women_speak(self, data): result = self.client.synthesis( data, 'zh', 1, { 'spd': 4, #语速 0最小 9最大 'pit': 8, #音调 0最小 9最大 'vol': 14 #音量 0最小 15最大 }) # 识别正确返回语音二进制 错误则返回dict 参照下面错误码 if not isinstance(result, dict): with open(self.file_name, 'ab') as f: #打开一个文件,如果文件存在,则在文件最末尾添加二进制写入。 f.write(result) #将数据写入。
def gen_voice(words): APP_ID = 'xxxxxxxxxxxx' # 从百度AI注册获取,免费 API_KEY = 'xxxxxxxxxxxxxxxx' SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) result = client.synthesis(words, 'zh', 1, {'vol': 3, 'per': 4}) return result
def checked_voice(name): """ 语音合成,传入签到者姓名 """ client = AipSpeech( getConfig('baidu-aip', 'app_id'), getConfig('baidu-aip', 'api_key'), getConfig('baidu-aip', 'secret_key'), ) result = client.synthesis(f'{name}同学,签到成功', 'zh', 1, { 'vol': 15, }) if not isinstance(result, dict): tempFile = tempfile.NamedTemporaryFile('wb', delete=False) tempFile.write(result) tempFile.close() pygame.mixer.init() clock = pygame.time.Clock() # 加载音乐 track = pygame.mixer.music.load(tempFile.name) # 播放音乐 pygame.mixer.music.play() while pygame.mixer.music.get_busy(): clock.tick(30) else: pygame.mixer.quit() try: os.remove(tempFile.name) except PermissionError: pass
class Baidu: def __init__(self, config): APP_ID = config[SLUG]['app_id'] API_KEY = config[SLUG]['api_key'] SECRET_KEY = config[SLUG]['secret_key'] self._client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) # 读取文件 # 识别本地文件 def recognize(self,audio_file = 'record.wav'): file = get_file_content(audio_file) res = self._client.asr(file, 'wav', 16000, { 'dev_pid': 1536, }) return res def synthesis(self,text = '你好百度',lang = 'zh',type = 1 , vol = 5): result = self._client.synthesis(text, lang, type, { 'vol': vol, }) # 识别正确返回语音二进制 错误则返回dict 参照下面错误码 if not isinstance(result, dict): with open('speak.mp3', 'wb') as f: f.write(result) return 1 else: print(result) return 0 def say(self,audio_file = 'auido.mp3'): os.system('mpg123 ' + audio_file)
def synthesis(text): # 初始化AipSpeech对象 aip_speech = AipSpeech(APP_ID, API_KEY, SECRET_KEY) result = aip_speech.synthesis(text, 'zh', 1, {'per': PERSON, 'spd': SPEED}) return result
class Speech: def __init__(self): APP_ID = '11468048' API_KEY = 'BF0OERtklEYDGG9k6mGh5cL4' SECRET_KEY = 'alw8VKwzadfbWfP5T1iM6xVjxFimfLXd' self.client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) def get_speech(self, text, file=None, lang='zh', ctp=1, spd=5, pit=5, vol=5, per=0): """ :param text: 合成语音的文本 :param file: 文件流 :param lang: 合成语种 :param ctp: 默认为1 :param spd: 语速,取值0-9,默认为5中语速 :param pit: 音调,取值0-9,默认为5中语调 :param vol: 音量,取值0-15,默认为5中音量 :param per: 发音人选择, 0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女 :return: None """ options = {"spd": spd, "pit": pit, "vol": vol, "per": per} result = self.client.synthesis(text, lang, ctp, options) if file and not isinstance(result, dict): file.write(result) else: return result
class myDialog(interface.Ui_MainWindow, QWidget): # def __init__(self, Dialog): super(myDialog, self).__init__() super().setupUi(MainWindow) APP_ID = 'your app id' API_KEY = 'your api key' SECRET_KEY = 'your secret key' self.client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) # 绑定按钮 self.pushButton.clicked.connect(self.synthesis) # 合成按钮 self.pushButton_2.clicked.connect(self.tongbu) # 同步按钮 self.fyr = { "度小美": 0, "度小宇": 1, "度逍遥": 3, "度丫丫": 4, "度小娇": 5, "度米朵": 103, "度博文": 106, "度小童": 110, "度小萌": 111 } def synthesis(self): # 转换的文本 TEXT = self.textEdit.toPlainText() # 发音人 PER = self.fyr[self.comboBox.currentText()] # 语速 SPD = self.spinBox.value() # 音调 PIT = self.spinBox_2.value() # 音量 VOL = self.spinBox_3.value() # 格式 AUE = self.comboBox_2.currentText() # 文件名 filename = self.lineEdit.text() + "." + AUE result = self.client.synthesis(TEXT, 'zh', 1, { 'vol': VOL, 'aue': AUE, 'per': PER, 'spd': SPD, 'pit': PIT }) if not isinstance(result, dict): with open(filename, 'wb') as f: f.write(result) QMessageBox.about(self, "成功", "恭喜你生成语音成功, 已经保存到当前文件夹下...") # def tongbu(self): data = self.textEdit.toPlainText() self.lineEdit.setText(data[:5])
def TTS(sentence, result_name): """ 语音合成, 把写入的文件名放进 sentence 的 speech_file 属性 :param text: SentenceDefine对象 :param spd: 语速,取值0-9,默认为5中语速 :param pit: 音调,取值0-9,默认为5中语调 :param vol: 音量,取值0-15,默认为5中音量 :param per: 发音人选择, 0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女 :return: None """ APP_ID = '10755356' API_KEY = 'otfgcdTsefZPcq2osXotBVQe' SECRET_KEY = 'd04MCEVWFbL21jN9L4gfsEI2GVxf94NI' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) result = client.synthesis( sentence.text, 'zh', 1, { 'pit': sentence.emotion['pit'] / 10.0 - 1, 'per': sentence.role, 'spd': sentence.emotion['speed'] / 10.0 - 1, }) print(sentence.role) if not isinstance(result, dict): with open("/home/embedded/Audink/TTS_File/" + result_name + ".mp3", 'wb') as f: f.write(result) else: raise ('百度语音合成失败', result_name) sentence.set_speechFile("/home/embedded/Audink/TTS_File/" + result_name + ".mp3")
def save(msg, path): client = AipSpeech(app_id, api_key, secret_key) result = client.synthesis(msg, 'zh', 1, {'vol': 9, 'per': 1}) logging.info(path) f = open(path, 'wb') f.write(result) f.close()
def tts(str, id): client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) result = client.synthesis(str, 'zh', 1, {'spd': 6, 'vol': 9, 'per': 106}) filename = 'result/audio' if not isinstance(result, dict): with open(filename + '{}'.format(id) + '.mp3', 'wb') as f: f.write(result)
class BaiduVoiceToWord: def __init__(self): APP_ID = baidu_api.VOICE_APP_ID API_KEY = baidu_api.VOICE_API_KEY SECRET_KEY = baidu_api.VOICE_SECRET_KEY self.client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) self.file_dir = BASE_DIR + '/utils/voice' self.mixer = mixer if not self.mixer.get_init(): self.mixer.init() def words_process(self, words): words_count = math.ceil(len(words) / 500) words_list = [] for i in range(words_count): words_list.append(words[0 + i * 500:500 + i * 500]) return words_list def get_words(self, words, vol=5, pit=5, per=4, spd=5): word_list = self.words_process(words) for index, value in enumerate(word_list): result = self.client.synthesis(value, options={ 'vol': vol, 'spd': spd, 'per': per, 'pit': pit }) if not isinstance(result, dict): with open(self.file_dir + '/{}.mp3'.format(index), 'wb') as f: f.write(result) else: return result def merge(self, words): with open(self.file_dir + '/voice.mp3', 'wb') as f: for i in range(math.ceil(len(words) / 500)): with open(self.file_dir + '/{}.mp3'.format(i), 'rb') as f1: f.write(f1.read()) os.unlink(self.file_dir + '/{}.mp3'.format(i)) f.flush() audio = MP3(self.file_dir + '/voice.mp3') return audio.info.length def say_words(self, words, vol=5, pit=5, per=4, spd=5): result = self.get_words(words, vol, pit, per, spd) if isinstance(result, dict): return {'data': '播放错误'} else: song_length = math.ceil(self.merge(words)) self.mixer.music.load(self.file_dir + '/voice.mp3') self.mixer.music.play() return {'song_length': song_length} def stop(self): self.mixer.music.stop() self.mixer.music.fadeout(1) return {'song_length': 0}
def synthesis(text, text1): # 语音合成 APP_ID = key_APP_ID API_KEY = key_API_KEY SECRET_KEY = key_SECRET_KEY client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) result = client.synthesis(text, 'zh', 1, { 'vol': 5, 'spd': 5, 'pit': 8, 'per': 0 }) if os.path.exists('temptype.mp3'): os.remove('temptype.mp3') # 单词类型 # 识别正确返回语音二进制 错误则返回dict 参照下面错误码 if not isinstance(result, dict): with open('temptype.mp3', 'wb') as f: f.write(result) result = client.synthesis(text1, 'zh', 1, { 'vol': 5, 'spd': 4, 'pit': 8, 'per': 0 }) if os.path.exists('tempdescribe.mp3'): os.remove('tempdescribe.mp3') # 单词解释声音 # 识别正确返回语音二进制 错误则返回dict 参照下面错误码 if not isinstance(result, dict): with open('tempdescribe.mp3', 'wb') as f: f.write(result)
def say(what): from aip import AipSpeech '''你的 APPID AK SK''' APP_ID = '17413508' API_KEY = 'crxCtxVjGgPcHxwUcYEMoyws' SECRET_KEY = 'SBdNH6ZHHpqdNw5kobNI4w2lslAgjQUS' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) result = client.synthesis( what, 'zh', 1, { # 发音人选择, 基础音库:0为度小美,1为度小宇,3为度逍遥,4为度丫丫, # 精品音库:5为度小娇,103为度米朵,106为度博文,110为度小童,111为度小萌,默认为度小美 'per': 4, # 音库 'spd': 4, # 语速 'pit': 5, # 音调 'vol': 5 # 音量 }) print(result) # 识别正确返回语音二进制, 错误则返回dict 参照下面错误码 output_file = "say" + ".wav" if not isinstance(result, dict): with open(output_file, 'wb') as f: f.write(result) print('ok') os.system('mpg123 /home/pi/raspberry-tutorial/smartBot/' + output_file)
class baidu_word_2_sound(object): #百度AI应用提供参数 APP_ID = None API_KEY = None SECRET_KEY = None client = None def __init__(self, app_id, api_key, secret_key): #获取提供的百度AI接口参数 self.APP_ID = app_id self.API_KEY = api_key self.SECRET_KEY = secret_key #获取授权 self.client = AipSpeech(self.APP_ID, self.API_KEY, self.SECRET_KEY) def trans_word_to_sound(self, word, sound_file, sound_speed = 5, \ sound_tune = 5, sound_volume = 5, sound_type = 0): #进行语音合成 result = self.client.synthesis(word, 'zh', 1, { 'spd' : sound_speed,'pit':sound_tune,'vol': sound_volume,'per':sound_type }) # 识别正确返回语音二进制 错误则返回dict 参照下面错误码 if not isinstance(result, dict): with open(sound_file, 'wb') as f: f.write(result)
class Text2Speech: def __init__(self, APP_ID, API_KEY, SECRET_KEY, user_id='zhangxu'): self.aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY) self.user_id = user_id def t2s(self, text): while True: try: result = self.aipSpeech.synthesis(text[1], 'zh', 1, { 'cuid': self.user_id, 'vol': 7, 'per': 3 }) except Exception as e: print('\x1b[31m合成异常。重试ing\x1b[0m', text) time.sleep(1) continue if isinstance(result, dict): if result.get('error_code') in [500, 501, 502, 503]: print('\x1b[31m错误code:\x1b[0m', result) print('\x1b[31m出错重试ing\x1b[0m', text) time.sleep(1) continue else: return result break