@aituber-onair/voice
Version:
Voice synthesis library for AITuber OnAir
35 lines (34 loc) • 1.12 kB
JavaScript
import { OPENAI_TTS_API_URL } from '../constants/voiceEngine';
/**
* OpenAI TTS voice synthesis engine
*/
export class OpenAiEngine {
async fetchAudio(input, speaker, apiKey) {
if (!apiKey) {
throw new Error('OpenAI API key is required');
}
const talk = input;
const text = talk.message.trim();
const response = await fetch(OPENAI_TTS_API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
model: 'tts-1',
voice: speaker,
input: text,
}),
});
if (!response.ok) {
console.error('Failed to fetch TTS from OpenAI TTS:', response.status);
throw new Error('Failed to fetch TTS from OpenAI TTS.');
}
const blob = await response.blob();
return await blob.arrayBuffer();
}
getTestMessage(textVoiceText) {
return textVoiceText || 'OpenAI TTSを使用します';
}
}