From dca5c058e0c43019ff0e988c9983327509478783 Mon Sep 17 00:00:00 2001 From: JS00000 Date: Sun, 23 Apr 2023 23:56:32 +0800 Subject: [PATCH] fix: Avoid the same filename under multithreading (#933) --- voice/azure/azure_voice.py | 3 ++- voice/baidu/baidu_voice.py | 3 ++- voice/google/google_voice.py | 3 ++- voice/pytts/pytts_voice.py | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/voice/azure/azure_voice.py b/voice/azure/azure_voice.py index 98ccdf1..1a0a8ed 100644 --- a/voice/azure/azure_voice.py +++ b/voice/azure/azure_voice.py @@ -79,7 +79,8 @@ class AzureVoice(Voice): 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) diff --git a/voice/baidu/baidu_voice.py b/voice/baidu/baidu_voice.py index 114e4a7..406157b 100644 --- a/voice/baidu/baidu_voice.py +++ b/voice/baidu/baidu_voice.py @@ -82,7 +82,8 @@ class BaiduVoice(Voice): {"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)) diff --git a/voice/google/google_voice.py b/voice/google/google_voice.py index 2cc20aa..6dcadad 100644 --- a/voice/google/google_voice.py +++ b/voice/google/google_voice.py @@ -35,7 +35,8 @@ class GoogleVoice(Voice): 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)) diff --git a/voice/pytts/pytts_voice.py b/voice/pytts/pytts_voice.py index eb5a849..bd70086 100644 --- a/voice/pytts/pytts_voice.py +++ b/voice/pytts/pytts_voice.py @@ -34,7 +34,7 @@ class PyttsVoice(Voice): 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))