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

Cleanup examples and fix VoicesManager types #332

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
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python3

"""
Example of dynamic voice selection using VoicesManager.
"""
"""Simple example to generate an audio file with randomized
dynamic voice selection based on attributes such as Gender,
Language, or Locale."""

import asyncio
import random
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env python3

"""
Basic example of edge_tts usage.
"""
"""Simple example to generate audio with preset voice using async/await"""

import asyncio

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#!/usr/bin/env python3

"""
Streaming TTS example with subtitles.
This example is similar to the example basic_audio_streaming.py, but it shows
WordBoundary events to create subtitles using SubMaker.
"""
"""Example showing how to use use .stream() method to get audio chunks
and feed them to SubMaker to generate subtitles"""

import asyncio

Expand Down
35 changes: 0 additions & 35 deletions examples/basic_audio_streaming.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env python3

"""
Basic example of edge_tts usage in synchronous function
"""
"""Sync variant of the example for generating audio with a predefined voice"""


import edge_tts

Expand Down
36 changes: 0 additions & 36 deletions examples/sync_audio_generation_in_async_context.py

This file was deleted.

42 changes: 0 additions & 42 deletions examples/sync_audio_stream_in_async_context.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
#!/usr/bin/env python3

"""
Basic audio streaming example for sync interface
"""
"""Sync variant of the async .stream() method to
get audio chunks and feed them to SubMaker to
generate subtitles"""

import edge_tts

TEXT = "Hello World!"
VOICE = "en-GB-SoniaNeural"
OUTPUT_FILE = "test.mp3"
SRT_FILE = "test.srt"


def main() -> None:
"""Main function to process audio and metadata synchronously."""
"""Main function"""
communicate = edge_tts.Communicate(TEXT, VOICE)
submaker = edge_tts.SubMaker()
with open(OUTPUT_FILE, "wb") as file:
for chunk in communicate.stream_sync():
if chunk["type"] == "audio":
file.write(chunk["data"])
elif chunk["type"] == "WordBoundary":
print(f"WordBoundary: {chunk}")
submaker.feed(chunk)

with open(SRT_FILE, "w", encoding="utf-8") as file:
file.write(submaker.get_srt())


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion src/edge_tts/communicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ async def save(
json.dump(message, metadata)
metadata.write("\n")

def stream_sync(self) -> Generator[Dict[str, Any], None, None]:
def stream_sync(self) -> Generator[TTSChunk, None, None]:
"""Synchronous interface for async stream method"""

def fetch_async_items(queue: Queue) -> None: # type: ignore
Expand Down
8 changes: 8 additions & 0 deletions src/edge_tts/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,11 @@ class VoiceManagerVoice(Voice):
"""Voice data for VoiceManager."""

Language: str


class VoiceManagerFind(TypedDict):
"""Voice data for VoiceManager.find()."""

Gender: NotRequired[Literal["Female", "Male"]]
Locale: NotRequired[str]
Language: NotRequired[str]
9 changes: 6 additions & 3 deletions src/edge_tts/voices.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

import aiohttp
import certifi
from typing_extensions import Unpack

from .constants import SEC_MS_GEC_VERSION, VOICE_HEADERS, VOICE_LIST
from .drm import DRM
from .typing import Voice, VoiceManagerVoice
from .typing import Voice, VoiceManagerFind, VoiceManagerVoice


async def __list_voices(
Expand Down Expand Up @@ -94,7 +95,9 @@ def __init__(self) -> None:
self.called_create: bool = False

@classmethod
async def create(cls: Any, custom_voices: Optional[List[Voice]] = None) -> Any:
async def create(
cls: Any, custom_voices: Optional[List[Voice]] = None
) -> "VoicesManager":
"""
Creates a VoicesManager object and populates it with all available voices.
"""
Expand All @@ -106,7 +109,7 @@ async def create(cls: Any, custom_voices: Optional[List[Voice]] = None) -> Any:
self.called_create = True
return self

def find(self, **kwargs: Any) -> List[VoiceManagerVoice]:
def find(self, **kwargs: Unpack[VoiceManagerFind]) -> List[VoiceManagerVoice]:
"""
Finds all matching voices based on the provided attributes.
"""
Expand Down
Loading