Exception is now raised in stream() proper

This commit is contained in:
rany2
2023-05-03 22:21:53 +03:00
parent a7db09ab0e
commit 0094e3b952

View File

@@ -449,7 +449,6 @@ class Communicate:
""" """
Save the audio and metadata to the specified files. Save the audio and metadata to the specified files.
""" """
written_audio: bool = False
metadata: Union[TextIOWrapper, ContextManager[None]] = ( metadata: Union[TextIOWrapper, ContextManager[None]] = (
open(metadata_fname, "w", encoding="utf-8") open(metadata_fname, "w", encoding="utf-8")
if metadata_fname is not None if metadata_fname is not None
@@ -459,15 +458,9 @@ class Communicate:
async for message in self.stream(): async for message in self.stream():
if message["type"] == "audio": if message["type"] == "audio":
audio.write(message["data"]) audio.write(message["data"])
written_audio = True
elif ( elif (
isinstance(metadata, TextIOWrapper) isinstance(metadata, TextIOWrapper)
and message["type"] == "WordBoundary" and message["type"] == "WordBoundary"
): ):
json.dump(message, metadata) json.dump(message, metadata)
metadata.write("\n") metadata.write("\n")
if not written_audio:
raise NoAudioReceived(
"No audio was received from the service, so the file is empty."
)