2025-01-20 22:42:29 -07:00
|
|
|
"""TTS service using model and voice managers."""
|
|
|
|
|
2025-02-03 03:33:12 -07:00
|
|
|
import os
|
2025-01-03 00:53:41 -07:00
|
|
|
import time
|
2025-02-03 03:33:12 -07:00
|
|
|
import tempfile
|
2025-01-25 05:25:13 -07:00
|
|
|
from typing import List, Tuple, Optional, AsyncGenerator, Union
|
2025-01-20 22:42:29 -07:00
|
|
|
|
2025-02-03 03:33:12 -07:00
|
|
|
import asyncio
|
2025-01-13 20:15:46 -07:00
|
|
|
import numpy as np
|
2025-01-21 21:44:21 -07:00
|
|
|
import torch
|
2025-01-03 00:53:41 -07:00
|
|
|
from loguru import logger
|
|
|
|
|
2025-01-09 18:41:44 -07:00
|
|
|
from ..core.config import settings
|
2025-01-20 22:42:29 -07:00
|
|
|
from ..inference.model_manager import get_manager as get_model_manager
|
|
|
|
from ..inference.voice_manager import get_manager as get_voice_manager
|
2025-01-13 20:15:46 -07:00
|
|
|
from .audio import AudioNormalizer, AudioService
|
2025-01-27 20:23:42 -07:00
|
|
|
from .text_processing.text_processor import process_text_chunk, smart_split
|
2025-01-30 04:44:04 -07:00
|
|
|
from .text_processing import tokenize
|
2025-02-03 03:33:12 -07:00
|
|
|
from ..inference.kokoro_v1 import KokoroV1
|
|
|
|
|
2025-01-22 02:33:29 -07:00
|
|
|
|
2025-01-03 00:53:41 -07:00
|
|
|
class TTSService:
|
2025-01-20 22:42:29 -07:00
|
|
|
"""Text-to-speech service."""
|
|
|
|
|
2025-01-22 02:33:29 -07:00
|
|
|
# Limit concurrent chunk processing
|
|
|
|
_chunk_semaphore = asyncio.Semaphore(4)
|
|
|
|
|
2025-01-03 00:53:41 -07:00
|
|
|
def __init__(self, output_dir: str = None):
|
2025-01-25 05:25:13 -07:00
|
|
|
"""Initialize service."""
|
2025-01-03 00:53:41 -07:00
|
|
|
self.output_dir = output_dir
|
2025-01-22 02:33:29 -07:00
|
|
|
self.model_manager = None
|
|
|
|
self._voice_manager = None
|
2025-01-03 00:53:41 -07:00
|
|
|
|
2025-01-22 02:33:29 -07:00
|
|
|
@classmethod
|
|
|
|
async def create(cls, output_dir: str = None) -> 'TTSService':
|
2025-01-25 05:25:13 -07:00
|
|
|
"""Create and initialize TTSService instance."""
|
2025-01-22 02:33:29 -07:00
|
|
|
service = cls(output_dir)
|
|
|
|
service.model_manager = await get_model_manager()
|
|
|
|
service._voice_manager = await get_voice_manager()
|
|
|
|
return service
|
2025-01-20 22:42:29 -07:00
|
|
|
|
2025-01-25 05:25:13 -07:00
|
|
|
async def _process_chunk(
|
|
|
|
self,
|
2025-02-03 03:33:12 -07:00
|
|
|
chunk_text: str,
|
2025-01-27 20:23:42 -07:00
|
|
|
tokens: List[int],
|
2025-02-03 03:33:12 -07:00
|
|
|
voice_name: str,
|
|
|
|
voice_path: str,
|
2025-01-25 05:25:13 -07:00
|
|
|
speed: float,
|
|
|
|
output_format: Optional[str] = None,
|
|
|
|
is_first: bool = False,
|
|
|
|
is_last: bool = False,
|
|
|
|
normalizer: Optional[AudioNormalizer] = None,
|
2025-02-03 03:33:12 -07:00
|
|
|
) -> AsyncGenerator[Union[np.ndarray, bytes], None]:
|
2025-01-27 20:23:42 -07:00
|
|
|
"""Process tokens into audio."""
|
2025-01-25 05:25:13 -07:00
|
|
|
async with self._chunk_semaphore:
|
|
|
|
try:
|
2025-01-27 20:23:42 -07:00
|
|
|
# Handle stream finalization
|
|
|
|
if is_last:
|
2025-01-30 05:47:28 -07:00
|
|
|
# Skip format conversion for raw audio mode
|
|
|
|
if not output_format:
|
2025-02-03 03:33:12 -07:00
|
|
|
yield np.array([], dtype=np.float32)
|
|
|
|
return
|
2025-01-30 05:47:28 -07:00
|
|
|
|
2025-02-03 03:33:12 -07:00
|
|
|
result = await AudioService.convert_audio(
|
2025-01-27 20:23:42 -07:00
|
|
|
np.array([0], dtype=np.float32), # Dummy data for type checking
|
|
|
|
24000,
|
|
|
|
output_format,
|
|
|
|
is_first_chunk=False,
|
|
|
|
normalizer=normalizer,
|
|
|
|
is_last_chunk=True
|
|
|
|
)
|
2025-02-03 03:33:12 -07:00
|
|
|
yield result
|
|
|
|
return
|
2025-01-27 20:23:42 -07:00
|
|
|
|
|
|
|
# Skip empty chunks
|
2025-02-03 03:33:12 -07:00
|
|
|
if not tokens and not chunk_text:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Get backend
|
|
|
|
backend = self.model_manager.get_backend()
|
2025-01-27 20:23:42 -07:00
|
|
|
|
2025-01-25 05:25:13 -07:00
|
|
|
# Generate audio using pre-warmed model
|
2025-02-03 03:33:12 -07:00
|
|
|
if isinstance(backend, KokoroV1):
|
|
|
|
# For Kokoro V1, pass text and voice info
|
|
|
|
async for chunk_audio in self.model_manager.generate(
|
|
|
|
chunk_text,
|
|
|
|
(voice_name, voice_path),
|
|
|
|
speed=speed
|
|
|
|
):
|
|
|
|
# For streaming, convert to bytes
|
|
|
|
if output_format:
|
|
|
|
try:
|
|
|
|
converted = await AudioService.convert_audio(
|
|
|
|
chunk_audio,
|
|
|
|
24000,
|
|
|
|
output_format,
|
|
|
|
is_first_chunk=is_first,
|
|
|
|
normalizer=normalizer,
|
|
|
|
is_last_chunk=is_last
|
|
|
|
)
|
|
|
|
yield converted
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Failed to convert audio: {str(e)}")
|
|
|
|
else:
|
|
|
|
yield chunk_audio
|
|
|
|
else:
|
|
|
|
# For legacy backends, load voice tensor
|
|
|
|
voice_tensor = await self._voice_manager.load_voice(voice_name, device=backend.device)
|
|
|
|
chunk_audio = await self.model_manager.generate(
|
|
|
|
tokens,
|
|
|
|
voice_tensor,
|
|
|
|
speed=speed
|
|
|
|
)
|
2025-01-25 05:25:13 -07:00
|
|
|
|
2025-02-03 03:33:12 -07:00
|
|
|
if chunk_audio is None:
|
|
|
|
logger.error("Model generated None for audio chunk")
|
|
|
|
return
|
2025-01-25 05:25:13 -07:00
|
|
|
|
2025-02-03 03:33:12 -07:00
|
|
|
if len(chunk_audio) == 0:
|
|
|
|
logger.error("Model generated empty audio chunk")
|
|
|
|
return
|
|
|
|
|
|
|
|
# For streaming, convert to bytes
|
|
|
|
if output_format:
|
|
|
|
try:
|
|
|
|
converted = await AudioService.convert_audio(
|
|
|
|
chunk_audio,
|
|
|
|
24000,
|
|
|
|
output_format,
|
|
|
|
is_first_chunk=is_first,
|
|
|
|
normalizer=normalizer,
|
|
|
|
is_last_chunk=is_last
|
|
|
|
)
|
|
|
|
yield converted
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Failed to convert audio: {str(e)}")
|
|
|
|
else:
|
|
|
|
yield chunk_audio
|
2025-01-25 05:25:13 -07:00
|
|
|
except Exception as e:
|
2025-01-27 20:23:42 -07:00
|
|
|
logger.error(f"Failed to process tokens: {str(e)}")
|
2025-02-03 03:33:12 -07:00
|
|
|
|
|
|
|
async def _get_voice_path(self, voice: str) -> Tuple[str, str]:
|
|
|
|
"""Get voice path, handling combined voices.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
voice: Voice name or combined voice names (e.g., 'af_jadzia+af_jessica')
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Tuple of (voice name to use, voice path to use)
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
RuntimeError: If voice not found
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
# Check if it's a combined voice
|
|
|
|
if "+" in voice:
|
|
|
|
voices = [v.strip() for v in voice.split("+") if v.strip()]
|
|
|
|
if len(voices) < 2:
|
|
|
|
raise RuntimeError(f"Invalid combined voice name: {voice}")
|
|
|
|
|
|
|
|
# Load and combine voices
|
|
|
|
voice_tensors = []
|
|
|
|
for v in voices:
|
|
|
|
path = self._voice_manager.get_voice_path(v)
|
|
|
|
if not path:
|
|
|
|
raise RuntimeError(f"Voice not found: {v}")
|
|
|
|
logger.debug(f"Loading voice tensor from: {path}")
|
|
|
|
voice_tensor = torch.load(path, map_location="cpu")
|
|
|
|
voice_tensors.append(voice_tensor)
|
|
|
|
|
|
|
|
# Average the voice tensors
|
|
|
|
logger.debug(f"Combining {len(voice_tensors)} voice tensors")
|
|
|
|
combined = torch.mean(torch.stack(voice_tensors), dim=0)
|
|
|
|
|
|
|
|
# Save combined tensor
|
|
|
|
temp_dir = tempfile.gettempdir()
|
|
|
|
combined_path = os.path.join(temp_dir, f"{voice}.pt")
|
|
|
|
logger.debug(f"Saving combined voice to: {combined_path}")
|
|
|
|
torch.save(combined, combined_path)
|
|
|
|
|
|
|
|
return voice, combined_path
|
|
|
|
else:
|
|
|
|
# Single voice
|
|
|
|
path = self._voice_manager.get_voice_path(voice)
|
|
|
|
if not path:
|
|
|
|
raise RuntimeError(f"Voice not found: {voice}")
|
|
|
|
logger.debug(f"Using single voice path: {path}")
|
|
|
|
return voice, path
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Failed to get voice path: {e}")
|
|
|
|
raise
|
2025-01-03 00:53:41 -07:00
|
|
|
|
2025-01-04 17:54:54 -07:00
|
|
|
async def generate_audio_stream(
|
2025-01-09 18:41:44 -07:00
|
|
|
self,
|
|
|
|
text: str,
|
|
|
|
voice: str,
|
2025-01-20 22:42:29 -07:00
|
|
|
speed: float = 1.0,
|
2025-01-09 18:41:44 -07:00
|
|
|
output_format: str = "wav",
|
2025-01-25 05:25:13 -07:00
|
|
|
) -> AsyncGenerator[bytes, None]:
|
|
|
|
"""Generate and stream audio chunks."""
|
2025-01-22 02:33:29 -07:00
|
|
|
stream_normalizer = AudioNormalizer()
|
2025-01-27 20:23:42 -07:00
|
|
|
chunk_index = 0
|
2025-01-22 02:33:29 -07:00
|
|
|
|
2025-01-04 17:54:54 -07:00
|
|
|
try:
|
2025-02-03 03:33:12 -07:00
|
|
|
# Get backend
|
2025-01-22 02:33:29 -07:00
|
|
|
backend = self.model_manager.get_backend()
|
2025-02-03 03:33:12 -07:00
|
|
|
|
|
|
|
# Get voice path, handling combined voices
|
|
|
|
voice_name, voice_path = await self._get_voice_path(voice)
|
|
|
|
logger.debug(f"Using voice path: {voice_path}")
|
2025-01-22 02:33:29 -07:00
|
|
|
|
2025-01-27 20:23:42 -07:00
|
|
|
# Process text in chunks with smart splitting
|
|
|
|
async for chunk_text, tokens in smart_split(text):
|
|
|
|
try:
|
|
|
|
# Process audio for chunk
|
2025-02-03 03:33:12 -07:00
|
|
|
async for result in self._process_chunk(
|
|
|
|
chunk_text, # Pass text for Kokoro V1
|
|
|
|
tokens, # Pass tokens for legacy backends
|
|
|
|
voice_name, # Pass voice name
|
|
|
|
voice_path, # Pass voice path
|
2025-01-25 05:25:13 -07:00
|
|
|
speed,
|
|
|
|
output_format,
|
2025-01-27 20:23:42 -07:00
|
|
|
is_first=(chunk_index == 0),
|
|
|
|
is_last=False, # We'll update the last chunk later
|
2025-01-25 05:25:13 -07:00
|
|
|
normalizer=stream_normalizer
|
2025-02-03 03:33:12 -07:00
|
|
|
):
|
|
|
|
if result is not None:
|
|
|
|
yield result
|
|
|
|
chunk_index += 1
|
|
|
|
else:
|
|
|
|
logger.warning(f"No audio generated for chunk: '{chunk_text[:100]}...'")
|
2025-01-27 20:23:42 -07:00
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Failed to process audio for chunk: '{chunk_text[:100]}...'. Error: {str(e)}")
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Only finalize if we successfully processed at least one chunk
|
|
|
|
if chunk_index > 0:
|
|
|
|
try:
|
|
|
|
# Empty tokens list to finalize audio
|
2025-02-03 03:33:12 -07:00
|
|
|
async for result in self._process_chunk(
|
|
|
|
"", # Empty text
|
|
|
|
[], # Empty tokens
|
|
|
|
voice_name,
|
|
|
|
voice_path,
|
2025-01-27 20:23:42 -07:00
|
|
|
speed,
|
|
|
|
output_format,
|
|
|
|
is_first=False,
|
2025-02-03 03:33:12 -07:00
|
|
|
is_last=True, # Signal this is the last chunk
|
2025-01-27 20:23:42 -07:00
|
|
|
normalizer=stream_normalizer
|
2025-02-03 03:33:12 -07:00
|
|
|
):
|
|
|
|
if result is not None:
|
|
|
|
yield result
|
2025-01-27 20:23:42 -07:00
|
|
|
except Exception as e:
|
2025-02-03 03:33:12 -07:00
|
|
|
logger.error(f"Failed to finalize audio stream: {str(e)}")
|
2025-01-30 04:44:04 -07:00
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error in phoneme audio generation: {str(e)}")
|
|
|
|
raise
|
|
|
|
|
2025-01-25 05:25:13 -07:00
|
|
|
async def generate_audio(
|
|
|
|
self, text: str, voice: str, speed: float = 1.0
|
|
|
|
) -> Tuple[np.ndarray, float]:
|
|
|
|
"""Generate complete audio for text using streaming internally."""
|
|
|
|
start_time = time.time()
|
|
|
|
chunks = []
|
2025-01-20 22:42:29 -07:00
|
|
|
|
2025-01-25 05:25:13 -07:00
|
|
|
try:
|
2025-01-30 22:56:23 -07:00
|
|
|
# Use streaming generator but collect all valid chunks
|
2025-01-25 05:25:13 -07:00
|
|
|
async for chunk in self.generate_audio_stream(
|
2025-01-30 05:47:28 -07:00
|
|
|
text, voice, speed, # Default to WAV for raw audio
|
2025-01-25 05:25:13 -07:00
|
|
|
):
|
|
|
|
if chunk is not None:
|
|
|
|
chunks.append(chunk)
|
|
|
|
|
|
|
|
if not chunks:
|
|
|
|
raise ValueError("No audio chunks were generated successfully")
|
|
|
|
|
2025-01-30 22:56:23 -07:00
|
|
|
# Combine chunks, ensuring we have valid arrays
|
|
|
|
if len(chunks) == 1:
|
|
|
|
audio = chunks[0]
|
|
|
|
else:
|
|
|
|
# Filter out any zero-dimensional arrays
|
|
|
|
valid_chunks = [c for c in chunks if c.ndim > 0]
|
|
|
|
if not valid_chunks:
|
|
|
|
raise ValueError("No valid audio chunks to concatenate")
|
|
|
|
audio = np.concatenate(valid_chunks)
|
2025-01-25 05:25:13 -07:00
|
|
|
processing_time = time.time() - start_time
|
|
|
|
return audio, processing_time
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error in audio generation: {str(e)}")
|
|
|
|
raise
|
|
|
|
|
|
|
|
async def combine_voices(self, voices: List[str]) -> str:
|
|
|
|
"""Combine multiple voices."""
|
2025-01-22 02:33:29 -07:00
|
|
|
return await self._voice_manager.combine_voices(voices)
|
2025-01-09 18:41:44 -07:00
|
|
|
|
2025-01-07 03:50:08 -07:00
|
|
|
async def list_voices(self) -> List[str]:
|
2025-01-25 05:25:13 -07:00
|
|
|
"""List available voices."""
|
2025-02-03 03:33:12 -07:00
|
|
|
return await self._voice_manager.list_voices()
|