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

upgrade to latest cartesia 1.0.3 #587

Merged
merged 6 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 6 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ groq = { version = "^0.9.0", optional = true }
# Synthesizers
google-cloud-texttospeech = { version = "^2.16.3", optional = true }
pvkoala = { version = "^2.0.1", optional = true }
cartesia = { version = "^0.1.1", optional = true }
cartesia = "^1.0.3"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this go back to being optional?


# Transcribers
google-cloud-speech = { version = "^2.26.0", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion vocode/streaming/models/synthesizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class PollySynthesizerConfig(SynthesizerConfig, type=SynthesizerType.POLLY.value
sampling_rate: int = DEFAULT_POLLY_SAMPLING_RATE


DEFAULT_CARTESIA_MODEL_ID = "upbeat-moon"
DEFAULT_CARTESIA_MODEL_ID = "sonic-english"
DEFAULT_CARTESIA_VOICE_ID = "5345cf08-6f37-424d-a5d9-8ae1101b9377"


Expand Down
47 changes: 29 additions & 18 deletions vocode/streaming/synthesizer/cartesia_synthesizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,39 @@ def __init__(

# Lazy import the cartesia module
try:
from cartesia.tts import AsyncCartesiaTTS
from cartesia import AsyncCartesia
except ImportError as e:
raise ImportError(
f"Missing required dependancies for CartesiaSynthesizer"
) from e

self.cartesia_tts = AsyncCartesiaTTS

self.api_key = synthesizer_config.api_key or getenv("CARTESIA_API_KEY")
if not self.api_key:
raise ValueError("Missing Cartesia API key")

self.cartesia_tts = AsyncCartesia

if synthesizer_config.audio_encoding == AudioEncoding.LINEAR16:
self.channel_width = 2
match synthesizer_config.sampling_rate:
case SamplingRate.RATE_44100:
self.sampling_rate = 44100
self.output_format = "pcm_44100"
self.output_format = {
"sample_rate": 44100,
"encoding": "pcm_s16le",
"container": "raw",
}
case SamplingRate.RATE_22050:
self.sampling_rate = 22050
self.output_format = "pcm_22050"
self.output_format = {
"sample_rate": 22050,
"encoding": "pcm_s16le",
"container": "raw",
}
case SamplingRate.RATE_16000:
self.sampling_rate = 16000
self.output_format = "pcm_16000"
self.output_format = {
"sample_rate": 16000,
"encoding": "pcm_s16le",
"container": "raw",
}
case _:
raise ValueError(
f"Unsupported PCM sampling rate {synthesizer_config.sampling_rate}"
Expand All @@ -52,41 +60,44 @@ def __init__(
# Cartesia has issues with MuLaw/8000. Use pcm/16000 and
# create_synthesis_result_from_wav will handle the conversion to mulaw/8000
self.channel_width = 2
self.output_format = "pcm_16000"
self.sampling_rate = 16000
self.output_format = {
"sample_rate": 16000,
"encoding": "pcm_s16le",
"container": "raw",
}
else:
raise ValueError(
f"Unsupported audio encoding {synthesizer_config.audio_encoding}"
)

if not isinstance(self.output_format["sample_rate"], int):
raise ValueError(f"Invalid type for sample_rate")
self.sampling_rate = self.output_format["sample_rate"]
self.num_channels = 1
self.model_id = synthesizer_config.model_id
self.voice_id = synthesizer_config.voice_id
self.client = self.cartesia_tts(api_key=self.api_key)
self.voice_embedding = self.client.get_voice_embedding(voice_id=self.voice_id)


async def create_speech_uncached(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if interested, would love to support you making this properly streaming!!

would be something like:

def chunk_generator(sse):
  async for chunk in sse:
    yield SynthesisResult.ChunkResult(data=chunk)
    
return SynthesisResult(chunk_generator=chunk_generator(generator), lambda seconds: self.get_message_cutoff_from_voice_speed(message, seconds, 150))

you would have to also make the synthesizer support mulaw properly (which I think they fixed recently!)

self,
message: BaseMessage,
chunk_size: int,
is_first_text_chunk: bool = False,
is_sole_text_chunk: bool = False,
) -> SynthesisResult:
generator = await self.client.generate(
generator = await self.client.tts.sse(
model_id=self.model_id,
transcript=message.text,
voice=self.voice_embedding,
voice_id=self.voice_id,
stream=True,
model_id=self.model_id,
data_rtype='bytes',
output_format=self.output_format
)

audio_file = io.BytesIO()
with wave.open(audio_file, 'wb') as wav_file:
wav_file.setnchannels(self.num_channels)
wav_file.setsampwidth(self.channel_width)
wav_file.setframerate(self.sampling_rate)
wav_file.setframerate(float(self.sampling_rate))
async for chunk in generator:
wav_file.writeframes(chunk['audio'])
audio_file.seek(0)
Expand Down
Loading