add fish speech tts

This commit is contained in:
Junha Heo
2024-10-12 01:21:16 +09:00
parent 648f4a8597
commit 826e59dfc5
4 changed files with 132 additions and 1 deletions

View File

@@ -311,6 +311,49 @@ export async function sayTTS(character:character,text:string) {
throw new Error(text);
}
}
case 'fishspeech':{
if (character.fishSpeechConfig.model._id === ''){
throw new Error('FishSpeech Model is not selected')
}
const audioContext = new AudioContext();
const body = {
text: text,
reference_id: character.fishSpeechConfig.model._id,
chunk_length: character.fishSpeechConfig.chunk_length,
normalize: character.fishSpeechConfig.normalize,
format: 'mp3',
mp3_bitrate: 192,
}
console.log(body)
const response = await globalFetch(`https://api.fish.audio/v1/tts`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${db.fishSpeechKey}`
},
body: body,
rawResponse: true,
})
console.log(response)
if (response.ok) {
const audioBuffer = response.data.buffer;
audioContext.decodeAudioData(audioBuffer, (decodedData) => {
const sourceNode = audioContext.createBufferSource();
sourceNode.buffer = decodedData;
sourceNode.connect(audioContext.destination);
sourceNode.start();
});
} else {
const textBuffer: Uint8Array = response.data.buffer
const text = Buffer.from(textBuffer).toString('utf-8')
throw new Error(text);
}
}
}
} catch (error) {
alertError(`TTS Error: ${error}`)