From 8b0b0acff36ab48c25ca05afa3001d32d8acdecf Mon Sep 17 00:00:00 2001 From: Fraxy V Date: Tue, 28 Nov 2023 19:03:17 +0200 Subject: [PATCH] wchess : remove vad --- examples/wchess/libwchess/WChess.cpp | 5 +- examples/wchess/wchess.wasm/index-tmpl.html | 107 ++++++-------------- examples/wchess/wchess.wasm/wchess.wasm.cpp | 20 +--- 3 files changed, 37 insertions(+), 95 deletions(-) diff --git a/examples/wchess/libwchess/WChess.cpp b/examples/wchess/libwchess/WChess.cpp index 370fc923..9ed49d44 100644 --- a/examples/wchess/libwchess/WChess.cpp +++ b/examples/wchess/libwchess/WChess.cpp @@ -115,12 +115,11 @@ void WChess::run() { { get_audio(m_settings.vad_ms, pcmf32_cur); - if (::vad_simple(pcmf32_cur, WHISPER_SAMPLE_RATE, 1000, m_settings.vad_thold, m_settings.freq_thold, m_settings.print_energy)) { + if (!pcmf32_cur.empty()) { fprintf(stdout, "%s: Speech detected! Processing ...\n", __func__); set_status("Speech detected! Processing ..."); if (!have_prompt) { - get_audio(m_settings.prompt_ms, pcmf32_cur); m_wparams.i_start_rule = grammar_parsed.symbol_ids.at("prompt"); const auto txt = ::trim(transcribe(pcmf32_cur, logprob_min, logprob_sum, n_tokens, t_ms)); @@ -149,8 +148,6 @@ void WChess::run() { have_prompt = true; } } else { - get_audio(m_settings.command_ms, pcmf32_cur); - // prepend 3 second of silence pcmf32_cur.insert(pcmf32_cur.begin(), 3*WHISPER_SAMPLE_RATE, 0.0f); diff --git a/examples/wchess/wchess.wasm/index-tmpl.html b/examples/wchess/wchess.wasm/index-tmpl.html index 48c1ccdc..bd74d8cc 100644 --- a/examples/wchess/wchess.wasm/index-tmpl.html +++ b/examples/wchess/wchess.wasm/index-tmpl.html @@ -50,7 +50,7 @@
Whisper model: - + @@ -67,9 +67,7 @@
- - - +

@@ -115,10 +113,6 @@ // web audio context var context = null; - // audio data - var audio = null; - var audio0 = null; - // the command instance var instance = null; @@ -165,8 +159,7 @@ document.getElementById('model-whisper-status').innerHTML = 'loaded "' + model_whisper + '"!'; if (model_whisper != null) { - document.getElementById('start').disabled = false; - document.getElementById('stop' ).disabled = true; + document.getElementById('toggler').disabled = false; } } @@ -225,10 +218,7 @@ function stopRecording() { Module.set_status("paused"); - doRecording = false; - audio0 = null; - audio = null; - context = null; + mediaRecorder.stop(); } function startRecording() { @@ -242,12 +232,6 @@ }); } - Module.set_status(""); - - document.getElementById('start').disabled = true; - document.getElementById('stop').disabled = false; - - doRecording = true; startTime = Date.now(); var chunks = []; @@ -277,22 +261,15 @@ source.start(0); offlineContext.startRendering().then(function(renderedBuffer) { - audio = renderedBuffer.getChannelData(0); - - //printTextarea('js: audio recorded, size: ' + audio.length + ', old size: ' + (audio0 == null ? 0 : audio0.length)); - - var audioAll = new Float32Array(audio0 == null ? audio.length : audio0.length + audio.length); - if (audio0 != null) { - audioAll.set(audio0, 0); - } - audioAll.set(audio, audio0 == null ? 0 : audio0.length); + let audio = renderedBuffer.getChannelData(0); if (instance) { - Module.set_audio(instance, audioAll); + Module.set_audio(instance, audio); } }); - }, function(e) { - audio = null; + + mediaRecorder = null; + context = null; }); } @@ -300,48 +277,16 @@ }; mediaRecorder.onstop = function(e) { - if (doRecording) { - setTimeout(function() { - startRecording(); - }); - } + stream.getTracks().forEach(function(track) { + track.stop(); + }); }; - mediaRecorder.start(kIntervalAudio_ms); + mediaRecorder.start(); }) .catch(function(err) { printTextarea('js: error getting audio stream: ' + err); }); - - var interval = setInterval(function() { - if (!doRecording) { - clearInterval(interval); - mediaRecorder.stop(); - stream.getTracks().forEach(function(track) { - track.stop(); - }); - - document.getElementById('start').disabled = false; - document.getElementById('stop').disabled = true; - - mediaRecorder = null; - } - - // if audio length is more than kRestartRecording_s seconds, restart recording - if (audio != null && audio.length > kSampleRate*kRestartRecording_s) { - if (doRecording) { - //printTextarea('js: restarting recording'); - - clearInterval(interval); - audio0 = audio; - audio = null; - mediaRecorder.stop(); - stream.getTracks().forEach(function(track) { - track.stop(); - }); - } - } - }, 100); } // @@ -352,6 +297,16 @@ var intervalUpdate = null; var movesAll = ''; + document.getElementById('toggler').addEventListener('mousedown', function(event) { + this.innerText = "Release"; + onStart(); + }, true); + + document.getElementById('toggler').addEventListener('mouseup', function(event) { + this.innerText = "Hold"; + onStop(); + }, true); + function onStart() { if (!instance) { instance = Module.init('whisper.bin'); @@ -367,11 +322,15 @@ } startRecording(); + } - intervalUpdate = setInterval(function() { + function onStop() { + stopRecording(); + var interval = setInterval(function() { var moves = Module.get_moves(); if (moves != null && moves.length > 1) { + clearInterval(interval); for (move of moves.split(' ')) { board.move(move); @@ -388,17 +347,13 @@ nLines--; } } + + document.getElementById('state-status').innerHTML = Module.get_status(); + document.getElementById('state-moves').innerHTML = movesAll; } - - document.getElementById('state-status').innerHTML = Module.get_status(); - document.getElementById('state-moves').innerHTML = movesAll; }, 100); } - function onStop() { - stopRecording(); - } - diff --git a/examples/wchess/wchess.wasm/wchess.wasm.cpp b/examples/wchess/wchess.wasm/wchess.wasm.cpp index 6ce27464..9b3d5adb 100644 --- a/examples/wchess/wchess.wasm/wchess.wasm.cpp +++ b/examples/wchess/wchess.wasm/wchess.wasm.cpp @@ -29,28 +29,18 @@ void set_moves(const std::string & moves) { g_moves = moves; } -void get_audio(int ms, std::vector & audio) { - const int64_t n_samples = (ms * WHISPER_SAMPLE_RATE) / 1000; - - int64_t n_take = 0; - if (n_samples > (int) g_pcmf32.size()) { - n_take = g_pcmf32.size(); - } else { - n_take = n_samples; - } - - audio.resize(n_take); - std::copy(g_pcmf32.end() - n_take, g_pcmf32.end(), audio.begin()); +void get_audio(int /* ms */, std::vector & audio) { + std::lock_guard lock(g_mutex); + audio = g_pcmf32; } bool check_running() { - //g_pcmf32.clear(); return g_running; } -bool clear_audio() { +void clear_audio() { + std::lock_guard lock(g_mutex); g_pcmf32.clear(); - return true; } void wchess_main(size_t i) {