Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamline PiperEngine.synthesize to allow use of medium- and high-quality models #244

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 4 additions & 25 deletions RealtimeTTS/engines/piper_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand All @@ -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):
"""
Expand Down