Cleanup examples and fix VoicesManager types (#332)
Signed-off-by: rany <rany2@riseup.net>
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
Basic audio streaming example.
|
||||
|
||||
This example shows how to stream the audio data from the TTS engine,
|
||||
and how to get the WordBoundary events from the engine (which could
|
||||
be ignored if not needed).
|
||||
|
||||
The example streaming_with_subtitles.py shows how to use the
|
||||
WordBoundary events to create subtitles using SubMaker.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
import edge_tts
|
||||
|
||||
TEXT = "Hello World!"
|
||||
VOICE = "en-GB-SoniaNeural"
|
||||
OUTPUT_FILE = "test.mp3"
|
||||
|
||||
|
||||
async def amain() -> None:
|
||||
"""Main function"""
|
||||
communicate = edge_tts.Communicate(TEXT, VOICE)
|
||||
with open(OUTPUT_FILE, "wb") as file:
|
||||
async for chunk in communicate.stream():
|
||||
if chunk["type"] == "audio":
|
||||
file.write(chunk["data"])
|
||||
elif chunk["type"] == "WordBoundary":
|
||||
print(f"WordBoundary: {chunk}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(amain())
|
||||
@@ -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
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
This example shows that sync version of save function also works when run from
|
||||
a sync function called itself from an async function.
|
||||
The simple implementation of save_sync() with only asyncio.run would fail in this scenario,
|
||||
that's why ThreadPoolExecutor is used in implementation.
|
||||
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
import edge_tts
|
||||
|
||||
TEXT = "Hello World!"
|
||||
VOICE = "en-GB-SoniaNeural"
|
||||
OUTPUT_FILE = "test.mp3"
|
||||
|
||||
|
||||
def sync_main() -> None:
|
||||
"""Main function"""
|
||||
communicate = edge_tts.Communicate(TEXT, VOICE)
|
||||
communicate.save_sync(OUTPUT_FILE)
|
||||
|
||||
|
||||
async def amain() -> None:
|
||||
"""Main function"""
|
||||
sync_main()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
loop = asyncio.get_event_loop_policy().get_event_loop()
|
||||
try:
|
||||
loop.run_until_complete(amain())
|
||||
finally:
|
||||
loop.close()
|
||||
@@ -1,42 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
This example shows the sync version of stream function which also
|
||||
works when run from a sync function called itself from an async function.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
import edge_tts
|
||||
|
||||
TEXT = "Hello World!"
|
||||
VOICE = "en-GB-SoniaNeural"
|
||||
OUTPUT_FILE = "test.mp3"
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Main function to process audio and metadata synchronously."""
|
||||
communicate = edge_tts.Communicate(TEXT, VOICE)
|
||||
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}")
|
||||
|
||||
|
||||
async def amain() -> None:
|
||||
"""
|
||||
Async main function to call sync main function
|
||||
|
||||
This demonstrates that this works even when called from an async function.
|
||||
"""
|
||||
main()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
loop = asyncio.get_event_loop_policy().get_event_loop()
|
||||
try:
|
||||
loop.run_until_complete(amain())
|
||||
finally:
|
||||
loop.close()
|
||||
@@ -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__":
|
||||
Reference in New Issue
Block a user