def say(self, msg, cache=False, plugin='', onCompleted=None, wait=False): """ 说一句话 :param msg: 内容 :param cache: 是否缓存这句话的音频 :param plugin: 来自哪个插件的消息(将带上插件的说明) :param onCompleted: 完成的回调 :param wait: 是否要等待说完(为True将阻塞主线程直至说完这句话) """ if plugin != '': self.appendHistory(1, "[{}] {}".format(plugin, msg)) else: self.appendHistory(1, msg) pattern = r'^https?://.+' if re.match(pattern, msg): logger.info("内容包含URL,所以不读出来") return voice = '' cache_path = '' if utils.getCache(msg): logger.info("命中缓存,播放缓存语音") if config.get('/tts_engine') == 'hass-tts': voice = self.tts.get_speech(msg) else: voice = utils.getCache(msg) cache_path = utils.getCache(msg) else: try: voice = self.tts.get_speech(msg) if voice != '': cache_path = utils.saveCache(voice, msg) except Exception as e: logger.error('保存缓存失败:{}'.format(e)) if self.onSay: logger.info(cache) audio = 'http://{}:{}/audio/{}'.format( config.get('/server/host'), config.get('/server/port'), os.path.basename(cache_path)) logger.info('onSay: {}, {}'.format(msg, audio)) if plugin != '': self.onSay("[{}] {}".format(plugin, msg), audio) else: self.onSay(msg, audio) self.onSay = None if onCompleted is None: onCompleted = lambda: self._onCompleted(msg) self.player = Player.SoxPlayer() self.player.play(voice, not cache, onCompleted, wait) if not cache: utils.check_and_delete(cache_path, 60) # 60秒后将自动清理不缓存的音频 utils.lruCache() # 清理缓存
def say(self, msg, cache=False, plugin='', onCompleted=None, wait=False, resident=False): """ 说一句话 :param resident: :param msg: 内容 :param cache: 是否缓存这句话的音频 :param plugin: 来自哪个插件的消息(将带上插件的说明) :param onCompleted: 完成的回调 :param wait: 是否要等待说完(为True将阻塞主线程直至说完这句话) """ self.appendHistory(1, msg, plugin=plugin) if re.match(r'^https?://.+', msg): logger.info("内容包含URL,所以不读出来") self.onSay(msg, '', plugin=plugin) self.onSay = None return voice = utils.getCache(msg, resident=resident) cache_path = '' if voice: logger.info("命中缓存,播放缓存语音") cache_path = voice else: try: voice = self.tts.get_speech(msg, resident=resident) cache_path = utils.saveCache(voice, msg, resident=resident) except Exception as e: logger.error('保存缓存失败:{}'.format(e)) if self.onSay: logger.info(cache) audio = 'http://{}:{}/audio/{}'.format( config.get('/server/host'), config.get('/server/port'), os.path.basename(cache_path)) logger.info('onSay: {}, {}'.format(msg, audio)) self.onSay(msg, audio, plugin=plugin) self.onSay = None if onCompleted is None: def onCompleted(): self._onCompleted(msg) self.player = Player.getPlayerByFileName(voice) self.player.play(voice, not cache, onCompleted, wait) utils.lruCache() # 清理缓存
def say(self, msg, cache=False, plugin="", onCompleted=None, wait=False): """ 说一句话 :param msg: 内容 :param cache: 是否缓存这句话的音频 :param plugin: 来自哪个插件的消息(将带上插件的说明) :param onCompleted: 完成的回调 :param wait: 是否要等待说完(为True将阻塞主线程直至说完这句话) """ self.appendHistory(1, msg, plugin=plugin) pattern = r"^https?://.+" if re.match(pattern, msg): logger.info("内容包含URL,所以不读出来") self.onSay(msg, "", plugin=plugin) self.onSay = None return voice = "" cache_path = "" if utils.getCache(msg): logger.info("命中缓存,播放缓存语音") voice = utils.getCache(msg) cache_path = utils.getCache(msg) else: try: voice = self.tts.get_speech(msg) cache_path = utils.saveCache(voice, msg) except Exception as e: logger.error("语音合成失败:{}".format(e)) if self.onSay: logger.info(cache) audio = "http://{}:{}/audio/{}".format( config.get("/server/host"), config.get("/server/port"), os.path.basename(cache_path), ) logger.info("onSay: {}, {}".format(msg, audio)) self.onSay(msg, audio, plugin=plugin) self.onSay = None if onCompleted is None: onCompleted = lambda: self._onCompleted(msg) self.player = Player.SoxPlayer() self.player.play(voice, not cache, onCompleted, wait) if not cache: utils.check_and_delete(cache_path, 60) # 60秒后将自动清理不缓存的音频 utils.lruCache() # 清理缓存
def say(self, msg, cache=False, plugin='', onCompleted=None, wait=False): """ 说一句话 :param msg: 内容 :param cache: 是否缓存这句话的音频 :param plugin: 来自哪个插件的消息(将带上插件的说明) :param onCompleted: 完成的回调 :param wait: 是否要等待说完(为True将阻塞主线程直至说完这句话) """ self.appendHistory(1, msg, plugin=plugin) pattern = r'^https?://.+' if re.match(pattern, msg): logger.info("内容包含URL,所以不读出来") print("内容包含URL,所以不读出来") self.onSay(msg, '', plugin=plugin) self.onSay = None return voice = '' cache_path = '' if utils.getCache(msg): logger.info("命中缓存,播放缓存语音") print("命中缓存,播放缓存语音") voice = utils.getCache(msg) cache_path = utils.getCache(msg) else: try: voice = self.tts.get_speech(msg) cache_path = utils.saveCache(voice, msg) except Exception as e: logger.error('保存缓存失败:{}'.format(e)) print('保存缓存失败:{}'.format(e)) if self.onSay: logger.info(cache) print(cache) self.onSay(msg, audio, plugin=plugin) self.onSay = None if onCompleted is None: onCompleted = lambda: self._onCompleted(msg) self.player = Player.SoxPlayer() self.player.play(voice, not cache, onCompleted, wait) if not cache: utils.check_and_delete(cache_path, 60) # 60秒后将自动清理不缓存的音频 utils.lruCache() # 清理缓存
def say(self, msg, cache=False, plugin='', onCompleted=None): """ 说一句话 """ if plugin != '': self.appendHistory(1, "[{}] {}".format(plugin, msg)) else: self.appendHistory(1, msg) pattern = r'^https?://.+' if re.match(pattern, msg): logger.info("内容包含URL,所以不读出来") return voice = '' cache_path = '' if utils.getCache(msg): logger.info("命中缓存,播放缓存语音") voice = utils.getCache(msg) cache_path = utils.getCache(msg) else: try: voice = self.tts.get_speech(msg) cache_path = utils.saveCache(voice, msg) except Exception as e: logger.error('保存缓存失败:{}'.format(e)) if self.onSay: logger.info(cache) audio = 'http://{}:{}/audio/{}'.format(config.get('/server/host'), config.get('/server/port'), os.path.basename(cache_path)) logger.info('onSay: {}, {}'.format(msg, audio)) if plugin != '': self.onSay("[{}] {}".format(plugin, msg), audio) else: self.onSay(msg, audio) self.onSay = None if onCompleted is None: onCompleted = lambda: self._onCompleted(msg) self.player = Player.SoxPlayer() self.player.play(voice, not cache, onCompleted) if not cache: utils.check_and_delete(cache_path, 60) # 60秒后将自动清理不缓存的音频 utils.lruCache() # 清理缓存