( params: Partial<CartesiaTtsParams> )
| 487 | } |
| 488 | |
| 489 | async function synthesizeWithCartesia( |
| 490 | params: Partial<CartesiaTtsParams> |
| 491 | ): Promise<{ audioBuffer: Buffer; format: string; mimeType: string }> { |
| 492 | const { |
| 493 | text, |
| 494 | apiKey, |
| 495 | modelId = 'sonic-3', |
| 496 | voice, |
| 497 | language = 'en', |
| 498 | outputFormat, |
| 499 | speed, |
| 500 | emotion, |
| 501 | } = params |
| 502 | |
| 503 | if (!text || !apiKey) { |
| 504 | throw new Error('text and apiKey are required for Cartesia') |
| 505 | } |
| 506 | |
| 507 | const requestBody: Record<string, unknown> = { |
| 508 | model_id: modelId, |
| 509 | transcript: text, |
| 510 | language, |
| 511 | } |
| 512 | |
| 513 | if (voice) { |
| 514 | requestBody.voice = { |
| 515 | mode: 'id', |
| 516 | id: voice, |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | const generationConfig: Record<string, unknown> = {} |
| 521 | if (speed !== undefined) generationConfig.speed = speed |
| 522 | if (emotion !== undefined) generationConfig.emotion = emotion |
| 523 | if (Object.keys(generationConfig).length > 0) { |
| 524 | requestBody.generation_config = generationConfig |
| 525 | } |
| 526 | |
| 527 | if (outputFormat && typeof outputFormat === 'object') { |
| 528 | requestBody.output_format = outputFormat |
| 529 | } |
| 530 | |
| 531 | if (!requestBody.output_format) { |
| 532 | requestBody.output_format = { |
| 533 | container: 'wav', |
| 534 | encoding: 'pcm_s16le', |
| 535 | sample_rate: 24000, |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | logger.info('Cartesia API request:', { |
| 540 | model_id: requestBody.model_id, |
| 541 | has_voice: !!requestBody.voice, |
| 542 | language: requestBody.language, |
| 543 | output_format: requestBody.output_format, |
| 544 | has_generation_config: !!requestBody.generation_config, |
| 545 | }) |
| 546 |
no test coverage detected