-
Notifications
You must be signed in to change notification settings - Fork 3
tts changes #54
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
Merged
Merged
tts changes #54
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
1588661
tts changes
lgavincrl f8bf8c3
README updates for tts
lgavincrl 0a5eb24
tts async tests
lgavincrl 935c6a6
Fix async TTS Test awaits
lgavincrl 93a9dd6
Update tests/tts/async_http_test.py
lgavincrl d7cec7a
Update Makefile
lgavincrl d80b8f7
changes to makefile
lgavincrl e9ca729
Transport changes
lgavincrl 7b0a2fc
Transport & test adjustments
lgavincrl 1ec9012
TTS README updates
lgavincrl 8a26e5a
tts working example with output
lgavincrl f17be1b
tts async example- removal of error
lgavincrl 0741741
tts_example review changes
lgavincrl b0a7404
TTS README update
lgavincrl f8a7d95
RT - Removal of diff
lgavincrl 111c9fe
RT formatting error fix
lgavincrl f64b0d6
tts init changes
lgavincrl 78b6184
tts auth (change from batch config)
lgavincrl 768126b
gitignore output.wav
lgavincrl 6ec56a7
Formatting
lgavincrl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,3 +171,6 @@ cython_debug/ | |
|
|
||
| # PyPI configuration file | ||
| .pypirc | ||
|
|
||
| # Examples | ||
| **/output.wav | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import os | ||
| import asyncio | ||
|
|
||
| import wave | ||
| from pathlib import Path | ||
|
|
||
| from speechmatics.tts import AsyncClient, Voice, OutputFormat | ||
|
|
||
|
|
||
| # Set configuration | ||
| TEXT = "Welcome to the future of audio generation from text!" | ||
| VOICE = Voice.SARAH | ||
| OUTPUT_FORMAT = OutputFormat.RAW_PCM_16000 | ||
| OUTPUT_FILE = "output.wav" | ||
|
|
||
| # Set Format Parameters for WAV output file | ||
| SAMPLE_RATE = 16000 #Hz | ||
| SAMPLE_WIDTH = 2 # 16-bit audio | ||
| CHANNELS = 1 # Mono audio | ||
|
|
||
| # Save audio to WAV file | ||
| async def save_audio_to_wav(audio_data: bytes, | ||
| output_file_name: str) -> None: | ||
| with wave.open(output_file_name, "wb") as wav_file: | ||
| wav_file.setnchannels(CHANNELS) | ||
| wav_file.setsampwidth(SAMPLE_WIDTH) | ||
| wav_file.setframerate(SAMPLE_RATE) | ||
| wav_file.writeframes(audio_data) | ||
|
|
||
| # Generate speech from text and save to WAV file | ||
| async def main(): | ||
| print(f"Generating speech from text: {TEXT}") | ||
|
|
||
| try: | ||
| async with AsyncClient() as client: | ||
| async with await client.generate( | ||
| text=TEXT, | ||
| voice=VOICE, | ||
| output_format=OUTPUT_FORMAT | ||
| ) as response: | ||
| # Process the response in chunks and save to WAV | ||
| audio_chunks = [] | ||
| async for chunk in response.content.iter_chunked(1024): | ||
| audio_chunks.append(chunk) | ||
|
|
||
| # Combine chunks and save to WAV | ||
| audio_data = b''.join(audio_chunks) | ||
| await save_audio_to_wav(audio_data, OUTPUT_FILE) | ||
| print(f"Speech saved to {Path(OUTPUT_FILE).resolve()}") | ||
| except Exception as e: | ||
| print(f"An error occurred: {e}") | ||
|
|
||
| # Run the async main function | ||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| # Speechmatics TTS API Client | ||
|
|
||
| [](https://pypi.org/project/speechmatics-tts/) | ||
|  | ||
|
|
||
| Async Python client for Speechmatics TTS API. | ||
|
|
||
| ## Features | ||
|
|
||
| - Async API client with comprehensive error handling | ||
| - Type hints throughout for better IDE support | ||
| - Environment variable support for credentials | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| pip install speechmatics-tts | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Quick Start | ||
|
|
||
| ```python | ||
| import asyncio | ||
|
|
||
| import wave | ||
| from pathlib import Path | ||
|
|
||
| from speechmatics.tts import AsyncClient, Voice, OutputFormat | ||
|
|
||
| async def save_audio(audio_data: bytes, filename: str) -> None: | ||
| with wave.open(filename, "wb") as wav: | ||
| wav.setnchannels(1) # Mono | ||
| wav.setsampwidth(2) # 16-bit | ||
| wav.setframerate(16000) # 16kHz | ||
| wav.writeframes(audio_data) | ||
|
|
||
| # Generate speech data from text and save to WAV file | ||
| async def main(): | ||
| async with AsyncClient() as client: | ||
| async with await client.generate( | ||
| text="Welcome to the future of audio generation from text!", | ||
| voice=Voice.SARAH, | ||
| output_format=OutputFormat.RAW_PCM_16000 | ||
| ) as response: | ||
| audio = b''.join([chunk async for chunk in response.content.iter_chunked(1024)]) | ||
| await save_audio(audio, "output.wav") | ||
lgavincrl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| # Run the async main function | ||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
|
|
||
lgavincrl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| ### Error Handling | ||
|
|
||
| ```python | ||
| import asyncio | ||
| from speechmatics.tts import ( | ||
| AsyncClient, | ||
| AuthenticationError, | ||
| TimeoutError | ||
| ) | ||
|
|
||
| async def main(): | ||
| try: | ||
| async with AsyncClient() as client: | ||
| response = await client.generate(text="Hello, this is the Speechmatics TTS API. We are excited to have you here!") | ||
|
|
||
| except AuthenticationError: | ||
| print("Invalid API key") | ||
| except JobError as e: | ||
| print(f"Job processing failed: {e}") | ||
lgavincrl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| except TimeoutError as e: | ||
| print(f"Job timed out: {e}") | ||
| except FileNotFoundError: | ||
| print("Audio file not found") | ||
lgavincrl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| asyncio.run(main()) | ||
| ``` | ||
|
|
||
| ### Connection Configuration | ||
|
|
||
| ```python | ||
| import asyncio | ||
| from speechmatics.tts import AsyncClient, ConnectionConfig | ||
|
|
||
| async def main(): | ||
| # Custom connection settings | ||
| config = ConnectionConfig( | ||
| url="https://preview.tts.speechmatics.com", | ||
| api_key="your-api-key", | ||
| connect_timeout=30.0, | ||
| operation_timeout=600.0 | ||
| ) | ||
|
|
||
| async with AsyncClient(conn_config=config) as client: | ||
| response = await client.generate(text="Hello World") | ||
|
|
||
|
|
||
| asyncio.run(main()) | ||
| ``` | ||
|
|
||
| ## Logging | ||
|
|
||
| The client supports logging with job id tracing for debugging. To increase logging verbosity, set `DEBUG` level in your example code: | ||
|
|
||
| ```python | ||
| import logging | ||
| import sys | ||
|
|
||
| logging.basicConfig( | ||
| level=logging.DEBUG, | ||
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | ||
| handlers=[ | ||
| logging.StreamHandler(sys.stdout) | ||
| ] | ||
| ) | ||
| ``` | ||
|
|
||
| ## Environment Variables | ||
|
|
||
| The client supports the following environment variables: | ||
|
|
||
| - `SPEECHMATICS_API_KEY`: Your Speechmatics API key | ||
| - `SPEECHMATICS_TTS_URL`: Custom API endpoint URL (optional) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| [build-system] | ||
| requires = ["setuptools>=61.0.0"] | ||
| build-backend = "setuptools.build_meta" | ||
|
|
||
| [project] | ||
| name = "speechmatics-tts" | ||
| dynamic = ["version"] | ||
| description = "Speechmatics TTS API Client" | ||
| readme = "README.md" | ||
| authors = [{ name = "Speechmatics", email = "[email protected]" }] | ||
| license = "MIT" | ||
| requires-python = ">=3.9" | ||
| dependencies = ["aiohttp", "aiofiles"] | ||
| classifiers = [ | ||
| "Development Status :: 4 - Beta", | ||
| "Intended Audience :: Developers", | ||
| "Programming Language :: Python :: 3", | ||
| "Programming Language :: Python :: 3.9", | ||
| "Programming Language :: Python :: 3.10", | ||
| "Programming Language :: Python :: 3.11", | ||
| "Programming Language :: Python :: 3.12", | ||
| "Operating System :: OS Independent", | ||
| "Topic :: Multimedia :: Sound/Audio :: Speech", | ||
| "Topic :: Software Development :: Libraries :: Python Modules", | ||
| ] | ||
| keywords = ["speechmatics", "speech-to-text", "tts", "transcription", "api"] | ||
|
|
||
| [project.optional-dependencies] | ||
| dev = [ | ||
| "black", | ||
| "ruff", | ||
| "mypy", | ||
| "types-aiofiles", | ||
| "pre-commit", | ||
| "pytest", | ||
| "pytest-asyncio", | ||
| "pytest-cov", | ||
| "pytest-mock", | ||
| "build", | ||
| ] | ||
|
|
||
| [project.urls] | ||
| homepage = "https://github.com/speechmatics/speechmatics-python-sdk" | ||
| documentation = "https://docs.speechmatics.com/" | ||
| repository = "https://github.com/speechmatics/speechmatics-python-sdk" | ||
| issues = "https://github.com/speechmatics/speechmatics-python-sdk/issues" | ||
|
|
||
| [tool.setuptools.dynamic] | ||
| version = { attr = "speechmatics.tts.__version__" } | ||
|
|
||
| [tool.setuptools.packages.find] | ||
| where = ["."] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| __version__ = "0.0.0" | ||
|
|
||
| from ._async_client import AsyncClient | ||
| from ._auth import AuthBase | ||
| from ._auth import JWTAuth | ||
| from ._auth import StaticKeyAuth | ||
| from ._exceptions import AuthenticationError | ||
| from ._exceptions import ConfigurationError | ||
| from ._exceptions import ConnectionError | ||
| from ._exceptions import TimeoutError | ||
| from ._exceptions import TransportError | ||
| from ._models import ConnectionConfig | ||
| from ._models import OutputFormat | ||
| from ._models import Voice | ||
|
|
||
| __all__ = [ | ||
| "AsyncClient", | ||
| "AuthBase", | ||
| "JWTAuth", | ||
| "StaticKeyAuth", | ||
| "ConfigurationError", | ||
| "AuthenticationError", | ||
| "ConnectionError", | ||
| "TransportError", | ||
| "TimeoutError", | ||
| "ConnectionConfig", | ||
| "Voice", | ||
| "OutputFormat", | ||
| ] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.