wchess : remove vad

This commit is contained in:
Fraxy V 2023-11-28 19:03:17 +02:00
parent 02ade14f67
commit 8b0b0acff3
3 changed files with 37 additions and 95 deletions

View File

@ -115,12 +115,11 @@ void WChess::run() {
{ {
get_audio(m_settings.vad_ms, pcmf32_cur); 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__); fprintf(stdout, "%s: Speech detected! Processing ...\n", __func__);
set_status("Speech detected! Processing ..."); set_status("Speech detected! Processing ...");
if (!have_prompt) { if (!have_prompt) {
get_audio(m_settings.prompt_ms, pcmf32_cur);
m_wparams.i_start_rule = grammar_parsed.symbol_ids.at("prompt"); 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)); 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; have_prompt = true;
} }
} else { } else {
get_audio(m_settings.command_ms, pcmf32_cur);
// prepend 3 second of silence // prepend 3 second of silence
pcmf32_cur.insert(pcmf32_cur.begin(), 3*WHISPER_SAMPLE_RATE, 0.0f); pcmf32_cur.insert(pcmf32_cur.begin(), 3*WHISPER_SAMPLE_RATE, 0.0f);

View File

@ -50,7 +50,7 @@
<div id="model-whisper"> <div id="model-whisper">
Whisper model: <span id="model-whisper-status"></span> Whisper model: <span id="model-whisper-status"></span>
<span id="fetch-whisper-progress"></span> <span id="fetch-whisper-progress"></span>
<button id="clear" onclick="clearCache()">Clear Cache</button>
<!-- <!--
<input type="file" id="file" name="file" onchange="loadFile(event, 'whisper.bin')" /> <input type="file" id="file" name="file" onchange="loadFile(event, 'whisper.bin')" />
--> -->
@ -67,9 +67,7 @@
<br> <br>
<div id="input"> <div id="input">
<button id="start" onclick="onStart()" disabled>Start</button> <button id="toggler" disabled>Hold</button>
<button id="stop" onclick="onStop()" disabled>Stop</button>
<button id="clear" onclick="clearCache()">Clear Cache</button>
</div> </div>
<br> <br>
@ -115,10 +113,6 @@
// web audio context // web audio context
var context = null; var context = null;
// audio data
var audio = null;
var audio0 = null;
// the command instance // the command instance
var instance = null; var instance = null;
@ -165,8 +159,7 @@
document.getElementById('model-whisper-status').innerHTML = 'loaded "' + model_whisper + '"!'; document.getElementById('model-whisper-status').innerHTML = 'loaded "' + model_whisper + '"!';
if (model_whisper != null) { if (model_whisper != null) {
document.getElementById('start').disabled = false; document.getElementById('toggler').disabled = false;
document.getElementById('stop' ).disabled = true;
} }
} }
@ -225,10 +218,7 @@
function stopRecording() { function stopRecording() {
Module.set_status("paused"); Module.set_status("paused");
doRecording = false; mediaRecorder.stop();
audio0 = null;
audio = null;
context = null;
} }
function startRecording() { function startRecording() {
@ -242,12 +232,6 @@
}); });
} }
Module.set_status("");
document.getElementById('start').disabled = true;
document.getElementById('stop').disabled = false;
doRecording = true;
startTime = Date.now(); startTime = Date.now();
var chunks = []; var chunks = [];
@ -277,22 +261,15 @@
source.start(0); source.start(0);
offlineContext.startRendering().then(function(renderedBuffer) { offlineContext.startRendering().then(function(renderedBuffer) {
audio = renderedBuffer.getChannelData(0); let 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);
if (instance) { 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) { mediaRecorder.onstop = function(e) {
if (doRecording) { stream.getTracks().forEach(function(track) {
setTimeout(function() { track.stop();
startRecording(); });
});
}
}; };
mediaRecorder.start(kIntervalAudio_ms); mediaRecorder.start();
}) })
.catch(function(err) { .catch(function(err) {
printTextarea('js: error getting audio stream: ' + 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 intervalUpdate = null;
var movesAll = ''; 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() { function onStart() {
if (!instance) { if (!instance) {
instance = Module.init('whisper.bin'); instance = Module.init('whisper.bin');
@ -367,11 +322,15 @@
} }
startRecording(); startRecording();
}
intervalUpdate = setInterval(function() { function onStop() {
stopRecording();
var interval = setInterval(function() {
var moves = Module.get_moves(); var moves = Module.get_moves();
if (moves != null && moves.length > 1) { if (moves != null && moves.length > 1) {
clearInterval(interval);
for (move of moves.split(' ')) { for (move of moves.split(' ')) {
board.move(move); board.move(move);
@ -388,17 +347,13 @@
nLines--; 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); }, 100);
} }
function onStop() {
stopRecording();
}
</script> </script>
<script type="text/javascript" src="js/chess.js"></script> <script type="text/javascript" src="js/chess.js"></script>
</body> </body>

View File

@ -29,28 +29,18 @@ void set_moves(const std::string & moves) {
g_moves = moves; g_moves = moves;
} }
void get_audio(int ms, std::vector<float> & audio) { void get_audio(int /* ms */, std::vector<float> & audio) {
const int64_t n_samples = (ms * WHISPER_SAMPLE_RATE) / 1000; std::lock_guard<std::mutex> lock(g_mutex);
audio = g_pcmf32;
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());
} }
bool check_running() { bool check_running() {
//g_pcmf32.clear();
return g_running; return g_running;
} }
bool clear_audio() { void clear_audio() {
std::lock_guard<std::mutex> lock(g_mutex);
g_pcmf32.clear(); g_pcmf32.clear();
return true;
} }
void wchess_main(size_t i) { void wchess_main(size_t i) {