( params: OpenAiTtsParams )
| 325 | }) |
| 326 | |
| 327 | async function synthesizeWithOpenAi( |
| 328 | params: OpenAiTtsParams |
| 329 | ): Promise<{ audioBuffer: Buffer; format: string; mimeType: string }> { |
| 330 | const { text, apiKey, model = 'tts-1', responseFormat = 'mp3', speed = 1.0 } = params |
| 331 | const voice = (params.voice || 'alloy') as OpenAiTtsParams['voice'] |
| 332 | |
| 333 | const response = await fetch('https://api.openai.com/v1/audio/speech', { |
| 334 | method: 'POST', |
| 335 | headers: { |
| 336 | Authorization: `Bearer ${apiKey}`, |
| 337 | 'Content-Type': 'application/json', |
| 338 | }, |
| 339 | body: JSON.stringify({ |
| 340 | model, |
| 341 | voice, |
| 342 | input: text, |
| 343 | response_format: responseFormat, |
| 344 | speed: Math.max(0.25, Math.min(4.0, speed)), |
| 345 | }), |
| 346 | }) |
| 347 | |
| 348 | if (!response.ok) { |
| 349 | const error = await readTtsErrorJson(response, 'OpenAI TTS error response') |
| 350 | const errorMessage = getTtsErrorMessage(error, response.statusText) |
| 351 | throw new Error(`OpenAI TTS API error: ${errorMessage}`) |
| 352 | } |
| 353 | |
| 354 | const audioBuffer = await readResponseToBufferWithLimit(response, { |
| 355 | maxBytes: MAX_TTS_AUDIO_BYTES, |
| 356 | label: 'OpenAI TTS audio response', |
| 357 | }) |
| 358 | const mimeType = getMimeType(responseFormat) |
| 359 | |
| 360 | return { |
| 361 | audioBuffer, |
| 362 | format: responseFormat, |
| 363 | mimeType, |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | async function synthesizeWithDeepgram( |
| 368 | params: DeepgramTtsParams |
no test coverage detected