Spaces:
Sleeping
Sleeping
| import logging | |
| import os | |
| import time | |
| import numpy as np | |
| import soundfile as sf | |
| from typing import Optional, Generator, Tuple, List | |
| from .tts_base import TTSBase | |
| # Configure logging | |
| logger = logging.getLogger(__name__) | |
| class DummyTTS(TTSBase): | |
| """Dummy TTS engine that generates sine wave audio | |
| This class is used as a fallback when no other TTS engine is available. | |
| """ | |
| def generate_speech(self, text: str, voice: str = 'default', speed: float = 1.0) -> str: | |
| """Generate a dummy sine wave audio file | |
| Args: | |
| text (str): Input text (not used) | |
| voice (str): Voice ID (not used) | |
| speed (float): Speech speed multiplier (not used) | |
| Returns: | |
| str: Path to the generated audio file | |
| """ | |
| logger.info(f"Generating dummy speech for text length: {len(text)}") | |
| # Generate a simple sine wave | |
| sample_rate = 24000 | |
| duration = min(len(text) / 20, 10) # Rough approximation of speech duration | |
| t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False) | |
| audio = 0.5 * np.sin(2 * np.pi * 440 * t) # 440 Hz sine wave | |
| # Save to file | |
| output_path = self._generate_output_path(prefix="dummy") | |
| sf.write(output_path, audio, sample_rate) | |
| logger.info(f"Generated dummy audio: {output_path}") | |
| return output_path | |
| def generate_speech_stream(self, text: str, voice: str = 'default', speed: float = 1.0) -> Generator[Tuple[int, np.ndarray], None, None]: | |
| """Generate a dummy sine wave audio stream | |
| Args: | |
| text (str): Input text (not used) | |
| voice (str): Voice ID (not used) | |
| speed (float): Speech speed multiplier (not used) | |
| Yields: | |
| tuple: (sample_rate, audio_data) pairs | |
| """ | |
| logger.info(f"Generating dummy speech stream for text length: {len(text)}") | |
| # Generate a simple sine wave | |
| sample_rate = 24000 | |
| duration = min(len(text) / 20, 10) # Rough approximation of speech duration | |
| t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False) | |
| audio = 0.5 * np.sin(2 * np.pi * 440 * t) # 440 Hz sine wave | |
| # Yield the audio data | |
| yield sample_rate, audio |