address changes

This commit is contained in:
Aaron Ng
2025-10-30 16:25:10 +00:00
parent b0acbeffb9
commit 9d509bb409
3 changed files with 182 additions and 194 deletions

View File

@@ -6,6 +6,7 @@
import os import os
import aiohttp
from dotenv import load_dotenv from dotenv import load_dotenv
from loguru import logger from loguru import logger
@@ -89,7 +90,7 @@ async def run_bot(transport: BaseTransport, runner_args: RunnerArguments):
""" """
logger.info(f"Starting bot") logger.info(f"Starting bot")
async with aiohttp.ClientSession() as session:
stt = SpeechmaticsSTTService( stt = SpeechmaticsSTTService(
api_key=os.getenv("SPEECHMATICS_API_KEY"), api_key=os.getenv("SPEECHMATICS_API_KEY"),
params=SpeechmaticsSTTService.InputParams( params=SpeechmaticsSTTService.InputParams(
@@ -105,9 +106,8 @@ async def run_bot(transport: BaseTransport, runner_args: RunnerArguments):
tts = SpeechmaticsTTSService( tts = SpeechmaticsTTSService(
api_key=os.getenv("SPEECHMATICS_API_KEY"), api_key=os.getenv("SPEECHMATICS_API_KEY"),
params=SpeechmaticsTTSService.InputParams( voice_id="sarah",
voice="sarah", aiohttp_session=session,
),
) )
llm = OpenAILLMService( llm = OpenAILLMService(

View File

@@ -6,6 +6,7 @@
import os import os
import aiohttp
from dotenv import load_dotenv from dotenv import load_dotenv
from loguru import logger from loguru import logger
@@ -82,6 +83,7 @@ async def run_bot(transport: BaseTransport, runner_args: RunnerArguments):
""" """
logger.info(f"Starting bot") logger.info(f"Starting bot")
async with aiohttp.ClientSession() as session:
stt = SpeechmaticsSTTService( stt = SpeechmaticsSTTService(
api_key=os.getenv("SPEECHMATICS_API_KEY"), api_key=os.getenv("SPEECHMATICS_API_KEY"),
params=SpeechmaticsSTTService.InputParams( params=SpeechmaticsSTTService.InputParams(
@@ -94,9 +96,8 @@ async def run_bot(transport: BaseTransport, runner_args: RunnerArguments):
tts = SpeechmaticsTTSService( tts = SpeechmaticsTTSService(
api_key=os.getenv("SPEECHMATICS_API_KEY"), api_key=os.getenv("SPEECHMATICS_API_KEY"),
params=SpeechmaticsTTSService.InputParams( voice_id="sarah",
voice="sarah", aiohttp_session=session,
),
) )
llm = OpenAILLMService( llm = OpenAILLMService(

View File

@@ -6,7 +6,6 @@
"""Speechmatics TTS service integration.""" """Speechmatics TTS service integration."""
import os
from typing import AsyncGenerator, Optional from typing import AsyncGenerator, Optional
from urllib.parse import urlencode from urllib.parse import urlencode
@@ -41,55 +40,56 @@ class SpeechmaticsTTSService(TTSService):
It converts text to speech and returns raw PCM audio data for real-time playback. It converts text to speech and returns raw PCM audio data for real-time playback.
""" """
SPEECHMATICS_SAMPLE_RATE = 16000
class InputParams(BaseModel): class InputParams(BaseModel):
"""Configuration parameters for Speechmatics TTS service. """Optional input parameters for Speechmatics TTS configuration."""
Parameters: pass
voice: Voice model to use for synthesis. Defaults to "sarah".
"""
voice: str = "sarah"
def __init__( def __init__(
self, self,
*, *,
api_key: str | None = None, api_key: str,
base_url: str | None = None, base_url: str = "https://preview.tts.speechmatics.com",
aiohttp_session: aiohttp.ClientSession | None = None, voice_id: str = "sarah",
sample_rate: Optional[int] = 16000, aiohttp_session: aiohttp.ClientSession,
params: InputParams | None = None, sample_rate: Optional[int] = SPEECHMATICS_SAMPLE_RATE,
params: Optional[InputParams] = None,
**kwargs, **kwargs,
): ):
"""Initialize the Speechmatics TTS service. """Initialize the Speechmatics TTS service.
Args: Args:
api_key: Speechmatics API key for authentication. Uses environment variable api_key: Speechmatics API key for authentication.
`SPEECHMATICS_API_KEY` if not provided. base_url: Base URL for Speechmatics TTS API.
base_url: Base URL for Speechmatics TTS API. Defaults to voice_id: Voice model to use for synthesis.
`https://preview.tts.speechmatics.com`.
aiohttp_session: Shared aiohttp session for HTTP requests. aiohttp_session: Shared aiohttp session for HTTP requests.
sample_rate: Audio sample rate in Hz. Defaults to 16000. sample_rate: Audio sample rate in Hz.
params: Optional[InputParams]: Input parameters for the service. params: Optional[InputParams]: Input parameters for the service.
**kwargs: Additional arguments passed to TTSService. **kwargs: Additional arguments passed to TTSService.
""" """
if sample_rate and sample_rate != self.SPEECHMATICS_SAMPLE_RATE:
logger.warning(
f"Speechmatics TTS only supports {self.SPEECHMATICS_SAMPLE_RATE}Hz sample rate. "
f"Current rate of {sample_rate}Hz may cause issues."
)
super().__init__(sample_rate=sample_rate, **kwargs) super().__init__(sample_rate=sample_rate, **kwargs)
# Service parameters # Service parameters
self._api_key: str = api_key or os.getenv("SPEECHMATICS_API_KEY") self._api_key: str = api_key
self._base_url: str = base_url or "https://preview.tts.speechmatics.com" self._base_url: str = base_url
self._session = aiohttp_session or aiohttp.ClientSession() self._session = aiohttp_session
# Check we have required attributes # Check we have required attributes
if not self._api_key: if not self._api_key:
raise ValueError("Missing Speechmatics API key") raise ValueError("Missing Speechmatics API key")
if not self._base_url:
raise ValueError("Missing Speechmatics base URL")
# Default parameters # Default parameters
self._params = params or SpeechmaticsTTSService.InputParams() self._params = params or SpeechmaticsTTSService.InputParams()
# Set voice from parameters # Set voice from constructor parameter
self.set_voice(self._params.voice) self.set_voice(voice_id)
def can_generate_metrics(self) -> bool: def can_generate_metrics(self) -> bool:
"""Check if this service can generate processing metrics. """Check if this service can generate processing metrics.
@@ -140,23 +140,6 @@ class SpeechmaticsTTSService(TTSService):
first_chunk = True first_chunk = True
buffer = b"" buffer = b""
# Helper to move all complete 2-byte int16 samples from buffer into a frame
def _emit_complete_samples():
nonlocal buffer
if len(buffer) < 2:
return None
complete_samples = len(buffer) // 2
complete_bytes = complete_samples * 2
audio_data = buffer[:complete_bytes]
buffer = buffer[complete_bytes:] # Keep remaining bytes for next iteration
return TTSAudioRawFrame(
audio=audio_data,
sample_rate=self.sample_rate,
num_channels=1,
)
async for chunk in response.content.iter_any(): async for chunk in response.content.iter_any():
if not chunk: if not chunk:
continue continue
@@ -166,15 +149,19 @@ class SpeechmaticsTTSService(TTSService):
buffer += chunk buffer += chunk
# Emit a frame for all complete samples currently in buffer # Emit all complete 2-byte int16 samples from buffer
frame = _emit_complete_samples() if len(buffer) >= 2:
if frame: complete_samples = len(buffer) // 2
yield frame complete_bytes = complete_samples * 2
# Process any remaining bytes in buffer after streaming ends audio_data = buffer[:complete_bytes]
frame = _emit_complete_samples() buffer = buffer[complete_bytes:] # Keep remaining bytes for next iteration
if frame:
yield frame yield TTSAudioRawFrame(
audio=audio_data,
sample_rate=self.sample_rate,
num_channels=1,
)
except Exception as e: except Exception as e:
logger.exception(f"Error generating TTS: {e}") logger.exception(f"Error generating TTS: {e}")