Refactor ASR plugin options and improve audio processing

- Renamed `hideTimeout` to `autoHideTimeout` for clarity.
- Updated the default options destructuring for better readability.
- Removed unnecessary comments and cleaned up the audio processing logic.
- Ensured that the audio buffer is processed correctly before sending to the `onAudioChunk` callback.
This commit is contained in:
Harvey Zack committed 2025-05-23 10:08:20 +08:00
1 parent 1ece90c582
commit 3f0a1bd433
12 files changed
+41 -45

No files matched your search

+16 -17
View File
@@ -11,16 +11,25 @@ var art = new Artplayer({
sampleRate: 16000,
autoHideTimeout: 10000,
// Use your AI tool to convert buffer into subtitles
onAudioChunk: (buffer) => tencentASR(buffer),
onAudioChunk: (buffer) => startAsr(buffer),
}),
]
});
var ws = null;
var loading = false;
async function tencentASR(buffer) {
if (loading) return;
function stopAsr() {
try {
ws.send(JSON.stringify({ type: 'end' }));
ws.close();
} catch {}
ws = null;
loading = false;
}
async function startAsr(buffer) {
if (loading) return;
if (!ws) {
loading = true;
const api = 'https://api.aimu.app/asr/tencent?engine_model_type=16k_en';
@@ -30,26 +39,16 @@ async function tencentASR(buffer) {
ws.onmessage = (event) => {
const { code, result } = JSON.parse(event.data);
if (code === 0) {
const subtitle = result?.voice_text_str;
art.plugins.artplayerPluginAsr.append(subtitle);
art.plugins.artplayerPluginAsr.append(result.voice_text_str);
} else {
ws.close();
ws = null;
stopAsr();
}
};
loading = false;
}
if (ws.readyState === WebSocket.OPEN) {
if (ws?.readyState === WebSocket.OPEN) {
ws.send(buffer);
}
}
art.on('destroy', () => {
try {
ws.send(JSON.stringify({ type: 'end' }));
ws.close();
} catch {}
ws = null;
});
art.on('destroy', stopAsr);