From 7c67c970b433160342f3129c4c6ccd187348edaf Mon Sep 17 00:00:00 2001 From: InspectorCaracal <51038201+InspectorCaracal@users.noreply.github.com> Date: Mon, 6 Jan 2025 22:52:48 -0700 Subject: [PATCH] streamline piper synthesis method --- RealtimeTTS/engines/piper_engine.py | 29 ++++------------------------- 1 file changed, 4 insertions(+), 25 deletions(-) diff --git a/RealtimeTTS/engines/piper_engine.py b/RealtimeTTS/engines/piper_engine.py index 213ac66..7cca8f7 100644 --- a/RealtimeTTS/engines/piper_engine.py +++ b/RealtimeTTS/engines/piper_engine.py @@ -90,16 +90,12 @@ def synthesize(self, text: str) -> bool: print("No voice set. Please provide a PiperVoice configuration.") return False - # Create a unique temporary WAV file. - with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_wav_file: - output_wav_path = tmp_wav_file.name - # Build the argument list for Piper (no shell piping). # If piper_path is on the PATH, you can use just "piper". Otherwise, use the full path. cmd_list = [ self.piper_path, "-m", self.voice.model_file, - "-f", output_wav_path + "--output-raw", ] # If a JSON config file is available, add it. @@ -115,25 +111,12 @@ def synthesize(self, text: str) -> bool: result = subprocess.run( cmd_list, input=text.encode("utf-8"), # Piper reads from STDIN - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + capture_output=True, check=True, # Raises CalledProcessError on non-zero exit shell=False # No shell means no special quoting issues ) - - # Open the synthesized WAV file and (optionally) validate audio properties. - with wave.open(output_wav_path, "rb") as wf: - # If you require specific WAV properties, check them: - if wf.getnchannels() != 1 or wf.getframerate() != 16000 or wf.getsampwidth() != 2: - print(f"Unexpected WAV properties: " - f"Channels={wf.getnchannels()}, " - f"Rate={wf.getframerate()}, " - f"Width={wf.getsampwidth()}") - return False - - # Read audio data and put it into the queue. - audio_data = wf.readframes(wf.getnframes()) - self.queue.put(audio_data) + # Put the captured audio data in the queue + self.queue.put(result.stdout) return True @@ -144,10 +127,6 @@ def synthesize(self, text: str) -> bool: # Piper returned an error code; show the stderr output for troubleshooting. print(f"Error running Piper: {e.stderr.decode('utf-8', errors='replace')}") return False - finally: - # Clean up the temporary WAV file after reading it. - if os.path.isfile(output_wav_path): - os.remove(output_wav_path) def set_voice(self, voice: PiperVoice): """