Commit 727d3e76 for whisper.cpp
commit 727d3e7666485e12450746b523bfb4e7f057cc5e
Author: Lucius <37386791+luciusacarr@users.noreply.github.com>
Date: Thu Nov 5 21:18:00 2026 -0800
whisper : clamp segment end times to the audio length (#4137)
* whisper : clamp segment end times to the audio length
* make `n_len` variable and keep things dry
diff --git a/src/whisper.cpp b/src/whisper.cpp
index be0b865d..25a7aa12 100644
--- a/src/whisper.cpp
+++ b/src/whisper.cpp
@@ -7034,8 +7034,10 @@ int whisper_full_with_state(
}
}
+ const int n_len = whisper_n_len_from_state(state);
const int seek_start = params.offset_ms/10;
- const int seek_end = params.duration_ms == 0 ? whisper_n_len_from_state(state) : seek_start + params.duration_ms/10;
+ const int seek_end = params.duration_ms == 0 ? n_len : std::min(seek_start + params.duration_ms/10, n_len);
+ const int audio_len = n_samples > 0 ? (int) ((100LL * n_samples) / WHISPER_SAMPLE_RATE) : n_len;
// if length of spectrogram is less than 100ms (10 frames), then return
// basically don't process anything that is less than 100ms
@@ -7828,7 +7830,7 @@ int whisper_full_with_state(
}
if (tokens_cur[i].id > whisper_token_beg(ctx) && !params.single_segment) {
- const auto t1 = seek + 2*(tokens_cur[i].tid - whisper_token_beg(ctx));
+ const auto t1 = std::min(seek + 2*(tokens_cur[i].tid - whisper_token_beg(ctx)), audio_len);
if (!text.empty()) {
const auto tt0 = t0;
@@ -7879,7 +7881,7 @@ int whisper_full_with_state(
}
if (!text.empty()) {
- const auto t1 = seek + seek_delta;
+ const auto t1 = std::min(seek + seek_delta, audio_len);
const auto tt0 = t0;
const auto tt1 = t1;