-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(tts): add Typecast TTS extension #2202
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
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
51ea400
feat(tts): add Typecast TTS extension
hmmhmmhm 47b7f54
test(tts): add Typecast TTS runtime tests
hmmhmmhm f9c7eb3
fix(tts): use Python 3.10-compatible Typecast SDK
hmmhmmhm 766c8b4
Merge branch 'main' into TASK-13879
hmmhmmhm 27ebc22
Merge branch 'main' into TASK-13879
YiminW 13a7779
fix(tts): rename Typecast endpoint config to url
hmmhmmhm bf1e7dc
test(tts): expand Typecast validation coverage
hmmhmmhm 2e5d539
Merge remote-tracking branch 'origin/main' into TASK-13879
hmmhmmhm d238374
Merge branch 'main' into TASK-13879
YiminW d749439
fix(tts): flush Typecast dump tail before request end
hmmhmmhm 754c8a3
Merge branch 'main' into TASK-13879
YiminW 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
34 changes: 34 additions & 0 deletions
34
ai_agents/agents/ten_packages/extension/typecast_tts_python/README.md
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,34 @@ | ||
| # Typecast TTS Extension | ||
|
|
||
| Typecast text-to-speech extension for TEN Framework. | ||
|
|
||
| The extension uses the official `typecast-python` SDK and Typecast's | ||
| `/v1/text-to-speech/stream` endpoint. Typecast streams WAV audio at 32 kHz, | ||
| 16-bit, mono. The extension strips the initial WAV header and forwards PCM16 | ||
| mono audio to TEN. | ||
|
|
||
| ## Configuration | ||
|
|
||
| ```json | ||
| { | ||
| "params": { | ||
| "api_key": "<typecast-api-key>", | ||
| "voice_id": "tc_60e5426de8b95f1d3000d7b5", | ||
| "model": "ssfm-v30", | ||
| "output": { | ||
| "audio_format": "wav" | ||
| } | ||
| }, | ||
| "chunk_size": 8192 | ||
| } | ||
| ``` | ||
|
|
||
| `output.audio_format` is forced to `wav` because TEN consumes PCM audio frames. | ||
| MP3 streaming would require an additional decoder and is intentionally not used. | ||
|
|
||
| ## Validation | ||
|
|
||
| ```bash | ||
| tman -y install --standalone | ||
| ./tests/bin/start | ||
| ``` |
6 changes: 6 additions & 0 deletions
6
ai_agents/agents/ten_packages/extension/typecast_tts_python/__init__.py
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,6 @@ | ||
| # | ||
| # This file is part of TEN Framework, an open source project. | ||
| # Licensed under the Apache License, Version 2.0. | ||
| # See the LICENSE file for more information. | ||
| # | ||
| from . import addon |
15 changes: 15 additions & 0 deletions
15
ai_agents/agents/ten_packages/extension/typecast_tts_python/addon.py
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,15 @@ | ||
| # | ||
| # This file is part of TEN Framework, an open source project. | ||
| # Licensed under the Apache License, Version 2.0. | ||
| # See the LICENSE file for more information. | ||
| # | ||
| from ten_runtime import Addon, TenEnv, register_addon_as_extension | ||
|
|
||
| from .extension import TypecastTTSExtension | ||
|
|
||
|
|
||
| @register_addon_as_extension("typecast_tts_python") | ||
| class TypecastTTSExtensionAddon(Addon): | ||
| def on_create_instance(self, ten_env: TenEnv, name: str, context) -> None: | ||
| ten_env.log_info("TypecastTTSExtensionAddon on_create_instance") | ||
| ten_env.on_create_instance_done(TypecastTTSExtension(name), context) |
71 changes: 71 additions & 0 deletions
71
ai_agents/agents/ten_packages/extension/typecast_tts_python/config.py
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,71 @@ | ||
| # | ||
| # This file is part of TEN Framework, an open source project. | ||
| # Licensed under the Apache License, Version 2.0. | ||
| # See the LICENSE file for more information. | ||
| # | ||
| from pathlib import Path | ||
| from typing import Any | ||
| import copy | ||
|
|
||
| from pydantic import Field | ||
| from ten_ai_base import utils | ||
| from ten_ai_base.tts2_http import AsyncTTS2HttpConfig | ||
|
|
||
|
|
||
| class TypecastTTSConfig(AsyncTTS2HttpConfig): | ||
| """Typecast TTS config.""" | ||
|
|
||
| dump: bool = Field(default=False, description="Typecast TTS dump") | ||
| dump_path: str = Field( | ||
| default_factory=lambda: str( | ||
| Path(__file__).parent / "typecast_tts_in.pcm" | ||
| ), | ||
| description="Typecast TTS dump path", | ||
| ) | ||
| host: str = Field( | ||
| default="https://api.typecast.ai", | ||
| description="Typecast API host", | ||
| ) | ||
| params: dict[str, Any] = Field( | ||
| default_factory=dict, | ||
| description="Typecast TTS params", | ||
| ) | ||
| chunk_size: int = Field( | ||
| default=8192, | ||
| ge=1, | ||
| description="Maximum bytes read from each Typecast streaming chunk", | ||
| ) | ||
|
|
||
| def update_params(self) -> None: | ||
| if "host" in self.params: | ||
| self.host = str( | ||
| self.params.pop("host") # pylint: disable=no-member | ||
| ).rstrip("/") | ||
| else: | ||
| self.host = self.host.rstrip("/") | ||
|
|
||
| if "model" not in self.params: | ||
| self.params["model"] = "ssfm-v30" | ||
|
|
||
| output = dict( | ||
| self.params.get("output") or {} # pylint: disable=no-member | ||
| ) | ||
| output["audio_format"] = "wav" | ||
| self.params["output"] = output | ||
|
|
||
| def validate(self) -> None: | ||
| if not self.params.get("api_key"): # pylint: disable=no-member | ||
| raise ValueError("api_key is required for Typecast TTS") | ||
| if not self.params.get("voice_id"): # pylint: disable=no-member | ||
| raise ValueError("voice_id is required for Typecast TTS") | ||
| if not self.params.get("model"): # pylint: disable=no-member | ||
| raise ValueError("model is required for Typecast TTS") | ||
|
|
||
| def to_str(self, sensitive_handling: bool = True) -> str: | ||
| if not sensitive_handling: | ||
| return f"{self}" | ||
|
|
||
| config = copy.deepcopy(self) | ||
| if config.params and "api_key" in config.params: | ||
| config.params["api_key"] = utils.encrypt(config.params["api_key"]) | ||
| return f"{config}" | ||
37 changes: 37 additions & 0 deletions
37
ai_agents/agents/ten_packages/extension/typecast_tts_python/extension.py
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,37 @@ | ||
| # | ||
| # This file is part of TEN Framework, an open source project. | ||
| # Licensed under the Apache License, Version 2.0. | ||
| # See the LICENSE file for more information. | ||
| # | ||
| from ten_ai_base.tts2_http import ( | ||
| AsyncTTS2HttpClient, | ||
| AsyncTTS2HttpConfig, | ||
| AsyncTTS2HttpExtension, | ||
| ) | ||
| from ten_runtime import AsyncTenEnv | ||
|
|
||
| from .config import TypecastTTSConfig | ||
| from .typecast_tts import TYPECAST_STREAM_SAMPLE_RATE, TypecastTTSClient | ||
|
|
||
|
|
||
| class TypecastTTSExtension(AsyncTTS2HttpExtension): | ||
| """Typecast TTS extension using Typecast streaming TTS.""" | ||
|
|
||
| def __init__(self, name: str) -> None: | ||
| super().__init__(name) | ||
| self.config: TypecastTTSConfig = None | ||
| self.client: TypecastTTSClient = None | ||
|
|
||
| async def create_config(self, config_json_str: str) -> AsyncTTS2HttpConfig: | ||
| return TypecastTTSConfig.model_validate_json(config_json_str) | ||
|
|
||
| async def create_client( | ||
| self, config: AsyncTTS2HttpConfig, ten_env: AsyncTenEnv | ||
| ) -> AsyncTTS2HttpClient: | ||
| return TypecastTTSClient(config=config, ten_env=ten_env) | ||
|
|
||
| def vendor(self) -> str: | ||
| return "typecast" | ||
|
|
||
| def synthesize_audio_sample_rate(self) -> int: | ||
| return TYPECAST_STREAM_SAMPLE_RATE |
83 changes: 83 additions & 0 deletions
83
ai_agents/agents/ten_packages/extension/typecast_tts_python/manifest.json
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,83 @@ | ||
| { | ||
| "type": "extension", | ||
| "name": "typecast_tts_python", | ||
| "version": "0.1.0", | ||
| "dependencies": [ | ||
| { | ||
| "type": "system", | ||
| "name": "ten_runtime_python", | ||
| "version": "0.11" | ||
| }, | ||
| { | ||
| "type": "system", | ||
| "name": "ten_ai_base", | ||
| "version": "0.7" | ||
| } | ||
| ], | ||
| "package": { | ||
| "include": [ | ||
| "manifest.json", | ||
| "property.json", | ||
| "BUILD.gn", | ||
| "**.tent", | ||
| "**.py", | ||
| "README.md", | ||
| "pyproject.toml", | ||
| "requirements.txt" | ||
| ] | ||
| }, | ||
| "api": { | ||
| "interface": [ | ||
| { | ||
| "import_uri": "../../system/ten_ai_base/api/tts-interface.json" | ||
| } | ||
| ], | ||
| "property": { | ||
| "properties": { | ||
| "params": { | ||
| "type": "object", | ||
| "properties": { | ||
| "api_key": { | ||
| "type": "string" | ||
| }, | ||
| "host": { | ||
| "type": "string" | ||
| }, | ||
| "voice_id": { | ||
| "type": "string" | ||
| }, | ||
| "model": { | ||
| "type": "string" | ||
| }, | ||
| "language": { | ||
| "type": "string" | ||
| }, | ||
| "output": { | ||
| "type": "object", | ||
| "properties": { | ||
| "audio_pitch": { | ||
| "type": "int64" | ||
| }, | ||
| "audio_tempo": { | ||
| "type": "float64" | ||
| }, | ||
| "audio_format": { | ||
| "type": "string" | ||
| }, | ||
| "target_lufs": { | ||
| "type": "float64" | ||
| } | ||
| } | ||
| }, | ||
| "seed": { | ||
| "type": "int64" | ||
| } | ||
| } | ||
| }, | ||
| "chunk_size": { | ||
| "type": "int64" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
ai_agents/agents/ten_packages/extension/typecast_tts_python/pcm.py
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,41 @@ | ||
| # | ||
| # This file is part of TEN Framework, an open source project. | ||
| # Licensed under the Apache License, Version 2.0. | ||
| # See the LICENSE file for more information. | ||
| # | ||
| BYTES_PER_SAMPLE = 2 | ||
| NUMBER_OF_CHANNELS = 1 | ||
| WAV_HEADER_BYTES = 44 | ||
|
|
||
|
|
||
| class StreamingWavToPcm16: | ||
| """Strip the first WAV header and return PCM16-aligned chunks.""" | ||
|
|
||
| def __init__(self) -> None: | ||
| self._header = bytearray() | ||
| self._remainder = bytearray() | ||
| self._header_stripped = False | ||
|
|
||
| def feed(self, chunk: bytes) -> bytes: | ||
| if not chunk: | ||
| return b"" | ||
|
|
||
| if not self._header_stripped: | ||
| needed = WAV_HEADER_BYTES - len(self._header) | ||
| self._header.extend(chunk[:needed]) | ||
| chunk = chunk[needed:] | ||
| if len(self._header) < WAV_HEADER_BYTES: | ||
| return b"" | ||
| self._header_stripped = True | ||
|
|
||
| if self._remainder: | ||
| chunk = bytes(self._remainder) + chunk | ||
| self._remainder.clear() | ||
|
|
||
| frame_size = BYTES_PER_SAMPLE * NUMBER_OF_CHANNELS | ||
| left_size = len(chunk) % frame_size | ||
| if left_size: | ||
| self._remainder.extend(chunk[-left_size:]) | ||
| chunk = chunk[:-left_size] | ||
|
|
||
| return bytes(chunk) |
13 changes: 13 additions & 0 deletions
13
ai_agents/agents/ten_packages/extension/typecast_tts_python/property.json
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,13 @@ | ||
| { | ||
| "params": { | ||
| "api_key": "", | ||
| "voice_id": "tc_60e5426de8b95f1d3000d7b5", | ||
| "model": "ssfm-v30", | ||
| "output": { | ||
| "audio_format": "wav" | ||
| } | ||
| }, | ||
| "chunk_size": 8192, | ||
| "dump": false, | ||
| "dump_path": "/tmp/typecast_tts_audio_dump" | ||
| } |
7 changes: 7 additions & 0 deletions
7
ai_agents/agents/ten_packages/extension/typecast_tts_python/pyproject.toml
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,7 @@ | ||
| [project] | ||
| name = "typecast-tts-python" | ||
| version = "0.1.0" | ||
| requires-python = ">=3.10" | ||
| dependencies = [ | ||
| "typecast-python==0.3.9", | ||
| ] |
1 change: 1 addition & 0 deletions
1
ai_agents/agents/ten_packages/extension/typecast_tts_python/requirements.txt
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 @@ | ||
| typecast-python==0.3.9 |
9 changes: 9 additions & 0 deletions
9
ai_agents/agents/ten_packages/extension/typecast_tts_python/tests/bin/start
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,9 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -e | ||
|
|
||
| cd "$(dirname "${BASH_SOURCE[0]}")/../.." | ||
|
|
||
| export PYTHONPATH=.ten/app:.ten/app/ten_packages/system/ten_runtime_python/lib:.ten/app/ten_packages/system/ten_runtime_python/interface:.ten/app/ten_packages/system/ten_ai_base/interface:$PYTHONPATH | ||
|
|
||
| pytest -s tests/ "$@" |
Oops, something went wrong.
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.