Browse Source

fix: Avoid the same filename under multithreading (#933)

master
JS00000 GitHub 1 year ago
parent
commit
dca5c058e0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 7 additions and 4 deletions
  1. +2
    -1
      voice/azure/azure_voice.py
  2. +2
    -1
      voice/baidu/baidu_voice.py
  3. +2
    -1
      voice/google/google_voice.py
  4. +1
    -1
      voice/pytts/pytts_voice.py

+ 2
- 1
voice/azure/azure_voice.py View File

@@ -79,7 +79,8 @@ class AzureVoice(Voice):
self.speech_config.speech_synthesis_voice_name = self.config["speech_synthesis_voice_name"] self.speech_config.speech_synthesis_voice_name = self.config["speech_synthesis_voice_name"]
else: else:
self.speech_config.speech_synthesis_voice_name = self.config["speech_synthesis_voice_name"] self.speech_config.speech_synthesis_voice_name = self.config["speech_synthesis_voice_name"]
fileName = TmpDir().path() + "reply-" + str(int(time.time())) + ".wav"
# Avoid the same filename under multithreading
fileName = TmpDir().path() + "reply-" + str(int(time.time())) + "-" + str(hash(text) & 0x7FFFFFFF) + ".wav"
audio_config = speechsdk.AudioConfig(filename=fileName) audio_config = speechsdk.AudioConfig(filename=fileName)
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=self.speech_config, audio_config=audio_config) speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=self.speech_config, audio_config=audio_config)
result = speech_synthesizer.speak_text(text) result = speech_synthesizer.speak_text(text)


+ 2
- 1
voice/baidu/baidu_voice.py View File

@@ -82,7 +82,8 @@ class BaiduVoice(Voice):
{"spd": self.spd, "pit": self.pit, "vol": self.vol, "per": self.per}, {"spd": self.spd, "pit": self.pit, "vol": self.vol, "per": self.per},
) )
if not isinstance(result, dict): if not isinstance(result, dict):
fileName = TmpDir().path() + "reply-" + str(int(time.time())) + ".mp3"
# Avoid the same filename under multithreading
fileName = TmpDir().path() + "reply-" + str(int(time.time())) + "-" + str(hash(text) & 0x7FFFFFFF) + ".mp3"
with open(fileName, "wb") as f: with open(fileName, "wb") as f:
f.write(result) f.write(result)
logger.info("[Baidu] textToVoice text={} voice file name={}".format(text, fileName)) logger.info("[Baidu] textToVoice text={} voice file name={}".format(text, fileName))


+ 2
- 1
voice/google/google_voice.py View File

@@ -35,7 +35,8 @@ class GoogleVoice(Voice):


def textToVoice(self, text): def textToVoice(self, text):
try: try:
mp3File = TmpDir().path() + "reply-" + str(int(time.time())) + ".mp3"
# Avoid the same filename under multithreading
mp3File = TmpDir().path() + "reply-" + str(int(time.time())) + "-" + str(hash(text) & 0x7FFFFFFF) + ".mp3"
tts = gTTS(text=text, lang="zh") tts = gTTS(text=text, lang="zh")
tts.save(mp3File) tts.save(mp3File)
logger.info("[Google] textToVoice text={} voice file name={}".format(text, mp3File)) logger.info("[Google] textToVoice text={} voice file name={}".format(text, mp3File))


+ 1
- 1
voice/pytts/pytts_voice.py View File

@@ -34,7 +34,7 @@ class PyttsVoice(Voice):


def textToVoice(self, text): def textToVoice(self, text):
try: try:
# avoid the same filename
# Avoid the same filename under multithreading
wavFileName = "reply-" + str(int(time.time())) + "-" + str(hash(text) & 0x7FFFFFFF) + ".wav" wavFileName = "reply-" + str(int(time.time())) + "-" + str(hash(text) & 0x7FFFFFFF) + ".wav"
wavFile = TmpDir().path() + wavFileName wavFile = TmpDir().path() + wavFileName
logger.info("[Pytts] textToVoice text={} voice file name={}".format(text, wavFile)) logger.info("[Pytts] textToVoice text={} voice file name={}".format(text, wavFile))


Loading…
Cancel
Save