UNPKG

@aituber-onair/core

Version:

Core library for AITuber OnAir providing voice synthesis and chat processing

77 lines 2.84 kB
import { VOICE_VOX_API_URL } from '../../../constants'; /** * VoiceVox voice synthesis engine */ export class VoiceVoxEngine { constructor() { this.apiEndpoint = VOICE_VOX_API_URL; } async fetchAudio(input, speaker) { const talk = input; // get emotion from talk.style const emotion = talk.style || 'neutral'; const ttsQueryResponse = await fetch(`${this.apiEndpoint}/audio_query?speaker=${speaker}&text=${encodeURIComponent(talk.message)}`, { method: 'POST' }); if (!ttsQueryResponse.ok) { throw new Error('Failed to fetch TTS query.'); } const ttsQueryJson = await ttsQueryResponse.json(); // adjust parameters according to emotion this.adjustEmotionParameters(ttsQueryJson, emotion); const synthesisResponse = await fetch(`${this.apiEndpoint}/synthesis?speaker=${speaker}`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked', }, body: JSON.stringify(ttsQueryJson), }); if (!synthesisResponse.ok) { throw new Error('Failed to fetch TTS synthesis result.'); } const blob = await synthesisResponse.blob(); return await blob.arrayBuffer(); } /** * Adjust parameters according to emotion */ adjustEmotionParameters(ttsQueryJson, emotion) { // default values ttsQueryJson.speedScale = 1.16; ttsQueryJson.pitchScale = -0.02; ttsQueryJson.intonationScale = 1.26; switch (emotion.toLowerCase()) { case 'happy': ttsQueryJson.speedScale = 1.25; ttsQueryJson.pitchScale = 0.05; ttsQueryJson.intonationScale = 1.4; break; case 'sad': ttsQueryJson.speedScale = 1.0; ttsQueryJson.pitchScale = -0.1; ttsQueryJson.intonationScale = 1.0; break; case 'angry': ttsQueryJson.speedScale = 1.2; ttsQueryJson.pitchScale = -0.05; ttsQueryJson.intonationScale = 1.5; break; case 'surprised': ttsQueryJson.speedScale = 1.3; ttsQueryJson.pitchScale = 0.1; ttsQueryJson.intonationScale = 1.4; break; // default: "neutral" etc. other than default values } } getTestMessage(textVoiceText) { return textVoiceText || 'ボイスボックスを使用します'; } /** * Set custom API endpoint URL * @param apiUrl custom API endpoint URL */ setApiEndpoint(apiUrl) { this.apiEndpoint = apiUrl; } } //# sourceMappingURL=VoiceVoxEngine.js.map