Skip to content
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

add voice control for speed and emotions #461

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/pipecat/services/cartesia.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ def __init__(
self._context_id = None
self._receive_task = None

# Initialize default values for speed and emotion controls
self._speed = "normal" # default speed
self._emotion = [] # default to no emotion

def can_generate_metrics(self) -> bool:
return True

Expand Down Expand Up @@ -200,8 +204,19 @@ async def _receive_task_handler(self):
except Exception as e:
logger.exception(f"{self} exception: {e}")

# Method to set speed and emotion externally
def set_voice_controls(self, speed: str = "normal", emotion: list[str] = None):
"""
Set the voice controls for TTS (speed and emotion).
"""
self._speed = speed
self._emotion = emotion or []

async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]:
logger.debug(f"Generating TTS: [{text}]")
"""
Generate TTS using the configured speed and emotion settings.
"""
logger.debug(f"Generating TTS: [{text}], Speed: [{self._speed}], Emotion: [{self._emotion}]")

try:
if not self._websocket:
Expand All @@ -212,6 +227,12 @@ async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]:
await self.start_ttfb_metrics()
self._context_id = str(uuid.uuid4())

# Define the voice control parameters using stored instance variables
voice_controls = {
"speed": self._speed,
"emotion": self._emotion
}

msg = {
"transcript": text + " ",
"continue": True,
Expand All @@ -224,7 +245,9 @@ async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]:
"output_format": self._output_format,
"language": self._language,
"add_timestamps": True,
"__experimental_controls": voice_controls # Add the voice controls
}

try:
await self._websocket.send(json.dumps(msg))
await self.start_tts_usage_metrics(text)
Expand All @@ -234,6 +257,7 @@ async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]:
await self._disconnect()
await self._connect()
return

yield None
except Exception as e:
logger.exception(f"{self} exception: {e}")
logger.exception(f"{self} exception: {e}")