Skip to content

Commit 3f985a4

Browse files
authored
feat: migrate Paraformer is_final to use SetOption mechanism (#3310)
Use GetOption("is_final") in OnlineRecognizerParaformerImpl to detect the final chunk. When is_final is "1", accept short chunks (less than chunk_size_ frames) and pad with zeros for decoding. Users can call SetOption("is_final", "1") on the OnlineStream before the last decode to process the remaining audio frames.
1 parent 83e78ab commit 3f985a4

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

sherpa-onnx/csrc/online-recognizer-paraformer-impl.h

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,16 @@ class OnlineRecognizerParaformerImpl : public OnlineRecognizerImpl {
160160
}
161161

162162
bool IsReady(OnlineStream *s) const override {
163-
return s->GetNumProcessedFrames() + chunk_size_ < s->NumFramesReady();
163+
if (s->GetNumProcessedFrames() + chunk_size_ < s->NumFramesReady()) {
164+
return true;
165+
}
166+
// is_final: accept short chunks (less than chunk_size_ frames)
167+
// Users should call SetOption("is_final", "1") before the last decode.
168+
if (s->GetOptionInt("is_final", 0) &&
169+
s->GetNumProcessedFrames() < s->NumFramesReady()) {
170+
return true;
171+
}
172+
return false;
164173
}
165174

166175
void DecodeStreams(OnlineStream **ss, int32_t n) const override {
@@ -222,8 +231,26 @@ class OnlineRecognizerParaformerImpl : public OnlineRecognizerImpl {
222231
private:
223232
void DecodeStream(OnlineStream *s) const {
224233
const auto num_processed_frames = s->GetNumProcessedFrames();
225-
std::vector<float> frames = s->GetFrames(num_processed_frames, chunk_size_);
226-
s->GetNumProcessedFrames() += chunk_size_ - 1;
234+
int32_t available_frames = s->NumFramesReady() - num_processed_frames;
235+
bool is_final = s->GetOptionInt("is_final", 0);
236+
237+
// For the final short chunk (fewer frames than chunk_size_):
238+
// read the remaining frames and pad with zeros to chunk_size_.
239+
bool is_short_final = is_final && available_frames < chunk_size_;
240+
241+
std::vector<float> frames =
242+
s->GetFrames(num_processed_frames,
243+
is_short_final ? available_frames : chunk_size_);
244+
245+
if (is_short_final) {
246+
int32_t feat_dim_raw = config_.feat_config.feature_dim;
247+
frames.resize(chunk_size_ * feat_dim_raw, 0.0f);
248+
// Consume all remaining frames (no overlap needed).
249+
s->GetNumProcessedFrames() += available_frames;
250+
} else {
251+
// Normal: advance by chunk_size_ - 1 to keep 1-frame overlap.
252+
s->GetNumProcessedFrames() += chunk_size_ - 1;
253+
}
227254

228255
frames = ApplyLFR(frames);
229256
ApplyCMVN(&frames);

0 commit comments

Comments
 (0)