whisper: add whisper_vad_set_context to set vad_context in the state

* whisper_vad_set_context set the vad_context of the current state and
   set external_vad to true.

 * whisper_vad_set_context_to_state set the vad_context of any state
   and set external_vad to true.

If a vad_context is set and external_vad is set to false then the
existing vad_context is set.

Co-authored-by: Thomas Guillem <thomas@gllm.fr>
This commit is contained in:
xlmod 2026-02-02 14:19:38 +01:00
parent f53dc74843
commit 6cb1d73bc0
No known key found for this signature in database
2 changed files with 24 additions and 1 deletions

View File

@ -718,6 +718,12 @@ extern "C" {
WHISPER_API void whisper_vad_free_segments(struct whisper_vad_segments * segments);
WHISPER_API void whisper_vad_free (struct whisper_vad_context * ctx);
// Inject external VAD context for use with params.vad = true.
// The caller retains ownership - whisper will not free this context.
// Frees any previously set internal VAD context.
WHISPER_API void whisper_state_set_vad(struct whisper_state * state, struct whisper_vad_context * vctx);
WHISPER_API void whisper_set_vad (struct whisper_context * ctx, struct whisper_vad_context * vctx);
////////////////////////////////////////////////////////////////////////////
// Temporary helpers needed for exposing ggml interface

View File

@ -921,6 +921,7 @@ struct whisper_state {
int32_t exp_n_audio_ctx = 0; // 0 - use default
whisper_vad_context * vad_context = nullptr;
bool vad_external = false;
struct vad_segment_info {
int64_t orig_start;
@ -3835,7 +3836,7 @@ void whisper_free_state(struct whisper_state * state) {
// [EXPERIMENTAL] Token-level timestamps with DTW
aheads_masks_free(state->aheads_masks);
if (state->vad_context != nullptr) {
if (state->vad_context != nullptr && !state->vad_external) {
whisper_vad_free(state->vad_context);
state->vad_context = nullptr;
}
@ -5467,6 +5468,22 @@ void whisper_vad_free_segments(whisper_vad_segments * segments) {
}
}
void whisper_state_set_vad(
struct whisper_state * state,
struct whisper_vad_context * vctx) {
if (state->vad_context != nullptr && !state->vad_external) {
whisper_vad_free(state->vad_context);
}
state->vad_context = vctx;
state->vad_external = (vctx != nullptr);
}
void whisper_set_vad(
struct whisper_context * ctx,
struct whisper_vad_context * vctx) {
whisper_state_set_vad(ctx->state, vctx);
}
//////////////////////////////////
// Grammar - ported from llama.cpp
//////////////////////////////////