| 1 | # Copyright (C) 2025 AIDC-AI |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | # See the License for the specific language governing permissions and |
| 11 | # limitations under the License. |
| 12 | |
| 13 | """ |
| 14 | TTS (Text-to-Speech) endpoints |
| 15 | """ |
| 16 | |
| 17 | from fastapi import APIRouter, HTTPException |
| 18 | from loguru import logger |
| 19 | |
| 20 | from api.dependencies import PixelleVideoDep |
| 21 | from api.schemas.tts import TTSSynthesizeRequest, TTSSynthesizeResponse |
| 22 | from pixelle_video.utils.tts_util import get_audio_duration |
| 23 | |
| 24 | router = APIRouter(prefix="/tts", tags=["Basic Services"]) |
| 25 | |
| 26 | |
| 27 | @router.post("/synthesize", response_model=TTSSynthesizeResponse) |
| 28 | async def tts_synthesize( |
| 29 | request: TTSSynthesizeRequest, |
| 30 | pixelle_video: PixelleVideoDep |
| 31 | ): |
| 32 | """ |
| 33 | Text-to-Speech synthesis endpoint |
| 34 | |
| 35 | Convert text to speech audio using ComfyUI workflows. |
| 36 | |
| 37 | - **text**: Text to synthesize |
| 38 | - **workflow**: TTS workflow key (optional, uses default if not specified) |
| 39 | - **ref_audio**: Reference audio for voice cloning (optional) |
| 40 | - **voice_id**: (Deprecated) Voice ID for legacy compatibility |
| 41 | |
| 42 | Returns path to generated audio file and duration. |
| 43 | |
| 44 | Examples: |
| 45 | ```json |
| 46 | { |
| 47 | "text": "Hello, welcome to Pixelle-Video!", |
| 48 | "workflow": "runninghub/tts_edge.json" |
| 49 | } |
| 50 | ``` |
| 51 | |
| 52 | With voice cloning: |
| 53 | ```json |
| 54 | { |
| 55 | "text": "Hello, this is a cloned voice", |
| 56 | "workflow": "runninghub/tts_index2.json", |
| 57 | "ref_audio": "path/to/reference.wav" |
| 58 | } |
| 59 | ``` |
| 60 | """ |
| 61 | try: |
| 62 | logger.info(f"TTS synthesis request: {request.text[:50]}...") |
| 63 | |
| 64 | # Build TTS parameters |
| 65 | tts_params = {"text": request.text} |
| 66 | |
| 67 | # Add workflow if specified |
| 68 | if request.workflow: |
| 69 | tts_params["workflow"] = request.workflow |
| 70 | |
| 71 | # Add ref_audio if specified |
| 72 | if request.ref_audio: |
| 73 | tts_params["ref_audio"] = request.ref_audio |
| 74 | |
| 75 | # Legacy voice_id support (deprecated) |
| 76 | if request.voice_id and not request.workflow: |
| 77 | logger.warning("voice_id parameter is deprecated, please use workflow instead") |
| 78 | tts_params["voice"] = request.voice_id |
| 79 | |
| 80 | # Call TTS service |
| 81 | audio_path = await pixelle_video.tts(**tts_params) |
| 82 | |
| 83 | # Get audio duration |
| 84 | duration = get_audio_duration(audio_path) |
| 85 | |
| 86 | return TTSSynthesizeResponse( |
| 87 | audio_path=audio_path, |
| 88 | duration=duration |
| 89 | ) |
| 90 | |
| 91 | except Exception as e: |
| 92 | logger.error(f"TTS synthesis error: {e}") |
| 93 | raise HTTPException(status_code=500, detail=str(e)) |
| 94 | |
| 95 |