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

refactor: enhance error handling and logging in voice loading and synthesis processes #87

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
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
33 changes: 21 additions & 12 deletions wyoming_microsoft_tts/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,18 @@ async def main() -> None:
_LOGGER.debug("Arguments parsed successfully.")

# Load voice info
voices_info = get_voices(
args.download_dir,
update_voices=args.update_voices,
region=args.service_region,
key=args.subscription_key,
)
try:
_LOGGER.info("Starting voices loading process.")
voices_info = get_voices(
args.download_dir,
update_voices=args.update_voices,
region=args.service_region,
key=args.subscription_key,
)
_LOGGER.info("Voices loaded successfully.")
except Exception as e:
_LOGGER.error(f"Failed to load voices: {e}")
return

# Resolve aliases for backwards compatibility with old voice names
aliases_info: dict[str, Any] = {}
Expand Down Expand Up @@ -166,13 +172,16 @@ async def main() -> None:
server = AsyncServer.from_uri(args.uri)

_LOGGER.info("Ready")
await server.run(
partial(
MicrosoftEventHandler,
wyoming_info,
args,
try:
await server.run(
partial(
MicrosoftEventHandler,
wyoming_info,
args,
)
)
)
except Exception as e:
_LOGGER.error(f"An error occurred while running the server: {e}")


# -----------------------------------------------------------------------------
Expand Down
66 changes: 38 additions & 28 deletions wyoming_microsoft_tts/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,41 +67,51 @@ async def handle_event(self, event: Event) -> bool:
if not has_punctuation:
text = text + self.cli_args.auto_punctuation[0]

output_path = self.microsoft_tts.synthesize(text=synthesize.text, voice=voice)

wav_file: wave.Wave_read = wave.open(output_path, "rb")
with wav_file:
rate = wav_file.getframerate()
width = wav_file.getsampwidth()
channels = wav_file.getnchannels()

await self.write_event(
AudioStart(
rate=rate,
width=width,
channels=channels,
).event(),
)

# Audio
audio_bytes = wav_file.readframes(wav_file.getnframes())
bytes_per_sample = width * channels
bytes_per_chunk = bytes_per_sample * self.cli_args.samples_per_chunk
num_chunks = int(math.ceil(len(audio_bytes) / bytes_per_chunk))

# Split into chunks
for i in range(num_chunks):
offset = i * bytes_per_chunk
chunk = audio_bytes[offset : offset + bytes_per_chunk]
_LOGGER.debug("Synthesizing: %s", text)
try:
output_path = self.microsoft_tts.synthesize(text=text, voice=voice)
except Exception as e:
_LOGGER.error("Failed to synthesize text: %s", e)
return False

_LOGGER.debug("Synthesized text")
try:
wav_file: wave.Wave_read = wave.open(output_path, "rb")
with wav_file:
rate = wav_file.getframerate()
width = wav_file.getsampwidth()
channels = wav_file.getnchannels()

await self.write_event(
AudioChunk(
audio=chunk,
AudioStart(
rate=rate,
width=width,
channels=channels,
).event(),
)

# Audio
audio_bytes = wav_file.readframes(wav_file.getnframes())
bytes_per_sample = width * channels
bytes_per_chunk = bytes_per_sample * self.cli_args.samples_per_chunk
num_chunks = int(math.ceil(len(audio_bytes) / bytes_per_chunk))

# Split into chunks
for i in range(num_chunks):
offset = i * bytes_per_chunk
chunk = audio_bytes[offset : offset + bytes_per_chunk]
await self.write_event(
AudioChunk(
audio=chunk,
rate=rate,
width=width,
channels=channels,
).event(),
)
except Exception as e:
_LOGGER.error("Failed to send audio: %s", e)
return False

await self.write_event(AudioStop().event())
_LOGGER.debug("Completed request")

Expand Down