Skip to content

Commit

Permalink
Fix the last character not being recognized for streaming paraformer …
Browse files Browse the repository at this point in the history
…models. (#799)
  • Loading branch information
csukuangfj authored Apr 22, 2024
1 parent 9a68b92 commit 494cb5c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions sherpa-onnx/csrc/sherpa-onnx-alsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ as the device_name.

bool is_endpoint = recognizer.IsEndpoint(stream.get());

if (is_endpoint && !config.model_config.paraformer.encoder.empty()) {
// For streaming paraformer models, since it has a large right chunk size
// we need to pad it on endpointing so that the last character
// can be recognized
std::vector<float> tail_paddings(
static_cast<int>(1.0 * expected_sample_rate));
stream->AcceptWaveform(expected_sample_rate, tail_paddings.data(),
tail_paddings.size());
while (recognizer.IsReady(stream.get())) {
recognizer.DecodeStream(stream.get());
}
text = recognizer.GetResult(stream.get()).text;
}

if (!text.empty() && last_text != text) {
last_text = text;

Expand Down
13 changes: 13 additions & 0 deletions sherpa-onnx/csrc/sherpa-onnx-microphone.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ for a list of pre-trained models to download.
auto text = recognizer.GetResult(s.get()).text;
bool is_endpoint = recognizer.IsEndpoint(s.get());

if (is_endpoint && !config.model_config.paraformer.encoder.empty()) {
// For streaming paraformer models, since it has a large right chunk size
// we need to pad it on endpointing so that the last character
// can be recognized
std::vector<float> tail_paddings(static_cast<int>(1.0 * mic_sample_rate));
s->AcceptWaveform(mic_sample_rate, tail_paddings.data(),
tail_paddings.size());
while (recognizer.IsReady(s.get())) {
recognizer.DecodeStream(s.get());
}
text = recognizer.GetResult(s.get()).text;
}

if (!text.empty() && last_text != text) {
last_text = text;

Expand Down

0 comments on commit 494cb5c

Please sign in to comment.