Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add endpoint for websocket server #175

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions sherpa-onnx/csrc/online-recognizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,40 @@ class OnlineRecognizer::Impl {
// TODO(fangjun): Remember to change these constants if needed
int32_t frame_shift_ms = 10;
int32_t subsampling_factor = 4;
return Convert(decoder_result, sym_, frame_shift_ms, subsampling_factor);
auto ans = Convert(decoder_result,
sym_,
frame_shift_ms,
subsampling_factor);

if (!IsReady(s) && s->IsLastFrame(s->NumFramesReady() - 1)) {
ans.is_final = true;
}
ans.segment = s->GetWavSegment();
float frame_shift_s = frame_shift_ms / 1000.;
ans.start_time = s->GetStartFrame() * frame_shift_s;
s->GetNumTrailingBlankFrames() = decoder_result.num_trailing_blanks;

if (config_.enable_endpoint && IsEndpoint(s)) {
auto r = decoder_->GetEmptyResult();
s->SetResult(r);
ans.is_final = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why setting ans.is_final = true here, I thought this branch is for breaking long audios via Endpointer.

s->GetWavSegment() += 1;
s->GetStartFrame() = s->GetNumProcessedFrames();
s->GetNumTrailingBlankFrames() = 0;
}
return ans;
}

bool IsEndpoint(OnlineStream *s) const {
if (!config_.enable_endpoint) return false;
int32_t num_processed_frames = s->GetNumProcessedFrames();
int32_t num_processed_frames = s->GetNumProcessedFrames() -
s->GetStartFrame();

// frame shift is 10 milliseconds
float frame_shift_in_seconds = 0.01;

// subsampling factor is 4
int32_t trailing_silence_frames = s->GetResult().num_trailing_blanks * 4;
int32_t trailing_silence_frames = s->GetNumTrailingBlankFrames() * 4;

return endpoint_.IsEndpoint(num_processed_frames, trailing_silence_frames,
frame_shift_in_seconds);
Expand Down
22 changes: 22 additions & 0 deletions sherpa-onnx/csrc/online-stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,20 @@ class OnlineStream::Impl {

std::vector<Ort::Value> &GetStates() { return states_; }

int32_t &GetNumTrailingBlankFrames() { return num_trailing_blank_frames_; }

int32_t &GetWavSegment() { return segment_; }

int32_t &GetStartFrame() { return start_frame_; }

private:
FeatureExtractor feat_extractor_;
int32_t num_processed_frames_ = 0; // before subsampling
int32_t num_trailing_blank_frames_ = 0; // after subsampling
/// ID of this segment
int32_t segment_ = 0;
/// Starting frame of this segment.
int32_t start_frame_ = 0;
int32_t start_frame_index_ = 0; // never reset
OnlineTransducerDecoderResult result_;
std::vector<Ort::Value> states_;
Expand Down Expand Up @@ -109,4 +120,15 @@ std::vector<Ort::Value> &OnlineStream::GetStates() {
return impl_->GetStates();
}

int32_t &OnlineStream::GetNumTrailingBlankFrames() {
return impl_->GetNumTrailingBlankFrames();
}

int32_t &OnlineStream::GetWavSegment() {
return impl_->GetWavSegment();
}

int32_t &OnlineStream::GetStartFrame() {
return impl_->GetStartFrame();
}
} // namespace sherpa_onnx
12 changes: 12 additions & 0 deletions sherpa-onnx/csrc/online-stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ class OnlineStream {

void SetStates(std::vector<Ort::Value> states);
std::vector<Ort::Value> &GetStates();
// Used only for greedy search
//
// Get number of trailing blank frames decoded so far
//
// The returned reference is valid as long as this object is alive.
int32_t &GetNumTrailingBlankFrames();

// Return ID of this segment in Stream
int32_t &GetWavSegment();

// Return Starting frame of this segment.
int32_t &GetStartFrame();

private:
class Impl;
Expand Down