Skip to content

Commit dfee884

Browse files
committed
vad : store VAD context in whisper_state
This commit stores the VAD context in the whisper_state structure, allowing for better management and reuse of the VAD context across multiple calls to the whisper_vad function. The motivation for this change is that when updating the stream example I noticed that the VAD context was being re-initialized every time the whisper_vad function was called. This involved loading the VAD model which is expensive and unnecessary if the context can be reused. Storing this in the whisper_state seems follow the pattern simliar to how whisper_coreml_context and whisper_openvion_context are stored.
1 parent 5c1ce1c commit dfee884

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/whisper.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,8 @@ struct whisper_state {
954954
// [EXPERIMENTAL] speed-up techniques
955955
int32_t exp_n_audio_ctx = 0; // 0 - use default
956956

957+
whisper_vad_context * vad_context = nullptr;
958+
957959
struct vad_segment_info {
958960
float orig_start;
959961
float orig_end;
@@ -6613,12 +6615,16 @@ static bool whisper_vad(
66136615
WHISPER_LOG_INFO("%s: VAD is enabled, processing speach segments only\n", __func__);
66146616
filtered_n_samples = 0;
66156617

6616-
struct whisper_vad_context_params vad_ctx_params = whisper_vad_default_context_params();
6617-
struct whisper_vad_context * vctx = whisper_vad_init_from_file_with_params(params.vad_model_path, vad_ctx_params);
6618-
if (vctx == nullptr) {
6619-
WHISPER_LOG_ERROR("%s: failed to initialize VAD context\n", __func__);
6620-
return false;
6618+
if (state->vad_context == nullptr) {
6619+
struct whisper_vad_context_params vad_ctx_params = whisper_vad_default_context_params();
6620+
struct whisper_vad_context * vctx = whisper_vad_init_from_file_with_params(params.vad_model_path, vad_ctx_params);
6621+
if (vctx == nullptr) {
6622+
WHISPER_LOG_ERROR("%s: failed to initialize VAD context\n", __func__);
6623+
return false;
6624+
}
6625+
state->vad_context = vctx;
66216626
}
6627+
auto vctx = state->vad_context;
66226628

66236629
const whisper_vad_params & vad_params = params.vad_params;
66246630

0 commit comments

Comments
 (0)