mirror of
https://github.com/Yidadaa/ChatGPT-Next-Web.git
synced 2025-09-09 10:18:14 +08:00
.github
.husky
app
api
client
components
config
icons
lib
locales
masks
store
styles
utils
cloud
audio.ts
auth-settings-events.ts
baidu.ts
chat.ts
clone.ts
cloudflare.ts
format.ts
hmac.ts
hooks.ts
indexedDB-storage.ts
merge.ts
model.ts
ms_edge_tts.ts
object.ts
store.ts
stream.ts
sync.ts
tencent.ts
token.ts
command.ts
constant.ts
global.d.ts
layout.tsx
page.tsx
polyfill.ts
typing.ts
utils.ts
docs
public
scripts
src-tauri
test
.babelrc
.dockerignore
.env.template
.eslintignore
.eslintrc.json
.gitignore
.gitpod.yml
.lintstagedrc.json
.prettierrc.js
CODE_OF_CONDUCT.md
Dockerfile
LICENSE
README.md
README_CN.md
README_JA.md
docker-compose.yml
jest.config.ts
jest.setup.ts
next.config.mjs
package.json
tsconfig.json
vercel.json
yarn.lock
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
type TTSPlayer = {
|
|
init: () => void;
|
|
play: (audioBuffer: ArrayBuffer, onended: () => void | null) => Promise<void>;
|
|
stop: () => void;
|
|
};
|
|
|
|
export function createTTSPlayer(): TTSPlayer {
|
|
let audioContext: AudioContext | 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) => {
|
|
if (audioBufferSourceNode) {
|
|
audioBufferSourceNode.stop();
|
|
audioBufferSourceNode.disconnect();
|
|
}
|
|
|
|
const buffer = await audioContext!.decodeAudioData(audioBuffer);
|
|
audioBufferSourceNode = audioContext!.createBufferSource();
|
|
audioBufferSourceNode.buffer = buffer;
|
|
audioBufferSourceNode.connect(audioContext!.destination);
|
|
audioContext!.resume().then(() => {
|
|
audioBufferSourceNode!.start();
|
|
});
|
|
audioBufferSourceNode.onended = onended;
|
|
};
|
|
|
|
const stop = () => {
|
|
if (audioBufferSourceNode) {
|
|
audioBufferSourceNode.stop();
|
|
audioBufferSourceNode.disconnect();
|
|
audioBufferSourceNode = null;
|
|
}
|
|
if (audioContext) {
|
|
audioContext.close();
|
|
audioContext = null;
|
|
}
|
|
};
|
|
|
|
return { init, play, stop };
|
|
}
|