This commit is contained in:
Hk-Gosuto 2024-03-11 12:49:34 +08:00
parent bdaf416a10
commit 0c8119cdc1
2 changed files with 15 additions and 6 deletions

View File

@ -990,6 +990,7 @@ function _Chat() {
api = new ClientApi(ModelProvider.GPT); api = new ClientApi(ModelProvider.GPT);
const config = useAppConfig.getState(); const config = useAppConfig.getState();
setSpeechLoading(true); setSpeechLoading(true);
ttsPlayer.init();
const audioBuffer = await api.llm.speech({ const audioBuffer = await api.llm.speech({
model: config.ttsConfig.model, model: config.ttsConfig.model,
input: text, input: text,

View File

@ -1,4 +1,5 @@
type TTSPlayer = { type TTSPlayer = {
init: () => void;
play: (audioBuffer: ArrayBuffer, onended: () => void | null) => Promise<void>; play: (audioBuffer: ArrayBuffer, onended: () => void | null) => Promise<void>;
stop: () => void; stop: () => void;
}; };
@ -7,17 +8,24 @@ export function createTTSPlayer(): TTSPlayer {
let audioContext: AudioContext | null = null; let audioContext: AudioContext | null = null;
let audioBufferSourceNode: AudioBufferSourceNode | null = null; let audioBufferSourceNode: AudioBufferSourceNode | null = null;
const init = () => {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
audioContext.suspend();
};
const play = async (audioBuffer: ArrayBuffer, onended: () => void | null) => { const play = async (audioBuffer: ArrayBuffer, onended: () => void | null) => {
if (audioBufferSourceNode) { if (audioBufferSourceNode) {
audioBufferSourceNode.stop(); audioBufferSourceNode.stop();
audioBufferSourceNode.disconnect(); audioBufferSourceNode.disconnect();
} }
audioContext = new AudioContext();
const buffer = await audioContext.decodeAudioData(audioBuffer); const buffer = await audioContext!.decodeAudioData(audioBuffer);
audioBufferSourceNode = audioContext.createBufferSource(); audioBufferSourceNode = audioContext!.createBufferSource();
audioBufferSourceNode.buffer = buffer; audioBufferSourceNode.buffer = buffer;
audioBufferSourceNode.connect(audioContext.destination); audioBufferSourceNode.connect(audioContext!.destination);
audioBufferSourceNode.start(); audioContext!.resume().then(() => {
audioBufferSourceNode!.start();
});
audioBufferSourceNode.onended = onended; audioBufferSourceNode.onended = onended;
}; };
@ -33,5 +41,5 @@ export function createTTSPlayer(): TTSPlayer {
} }
}; };
return { play, stop }; return { init, play, stop };
} }