Skip to content

Commit

Permalink
fix: Avoid the same filename under multithreading (#933)
Browse files Browse the repository at this point in the history
  • Loading branch information
JS00000 committed Apr 23, 2023
1 parent d6a4b35 commit dca5c05
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
3 changes: 2 additions & 1 deletion voice/azure/azure_voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def textToVoice(self, text):
self.speech_config.speech_synthesis_voice_name = self.config["speech_synthesis_voice_name"]
else:
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)
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=self.speech_config, audio_config=audio_config)
result = speech_synthesizer.speak_text(text)
Expand Down
3 changes: 2 additions & 1 deletion voice/baidu/baidu_voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def textToVoice(self, text):
{"spd": self.spd, "pit": self.pit, "vol": self.vol, "per": self.per},
)
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:
f.write(result)
logger.info("[Baidu] textToVoice text={} voice file name={}".format(text, fileName))
Expand Down
3 changes: 2 additions & 1 deletion voice/google/google_voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def voiceToText(self, voice_file):

def textToVoice(self, text):
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.save(mp3File)
logger.info("[Google] textToVoice text={} voice file name={}".format(text, mp3File))
Expand Down
2 changes: 1 addition & 1 deletion voice/pytts/pytts_voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self):

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

0 comments on commit dca5c05

Please sign in to comment.