This repository has been archived on 2025-08-26. You can view files and clone it, but cannot push or open issues or pull requests.
Files
enginex-mr_series-sherpa-onnx/sherpa-onnx/csrc/online-stream.h

119 lines
3.8 KiB
C
Raw Normal View History

2023-02-19 11:42:15 +08:00
// sherpa-onnx/csrc/online-stream.h
//
// Copyright (c) 2023 Xiaomi Corporation
#ifndef SHERPA_ONNX_CSRC_ONLINE_STREAM_H_
#define SHERPA_ONNX_CSRC_ONLINE_STREAM_H_
#include <memory>
#include <vector>
#include "kaldi-decoder/csrc/faster-decoder.h"
2023-02-19 12:45:38 +08:00
#include "onnxruntime_cxx_api.h" // NOLINT
#include "sherpa-onnx/csrc/context-graph.h"
2023-02-19 11:42:15 +08:00
#include "sherpa-onnx/csrc/features.h"
#include "sherpa-onnx/csrc/online-ctc-decoder.h"
2023-08-14 10:32:14 +08:00
#include "sherpa-onnx/csrc/online-paraformer-decoder.h"
2023-02-19 11:42:15 +08:00
#include "sherpa-onnx/csrc/online-transducer-decoder.h"
#include "sherpa-onnx/csrc/transducer-keyword-decoder.h"
2023-02-19 11:42:15 +08:00
namespace sherpa_onnx {
2024-01-28 23:29:39 +08:00
struct TransducerKeywordResult;
2023-02-19 11:42:15 +08:00
class OnlineStream {
public:
explicit OnlineStream(const FeatureExtractorConfig &config = {},
ContextGraphPtr context_graph = nullptr);
2023-02-19 11:42:15 +08:00
~OnlineStream();
/**
2023-03-03 16:42:33 +08:00
@param sampling_rate The sampling_rate of the input waveform. If it does
not equal to config.sampling_rate, we will do
resampling inside.
@param waveform Pointer to a 1-D array of size n. It must be normalized to
the range [-1, 1].
2023-02-19 11:42:15 +08:00
@param n Number of entries in waveform
*/
2023-03-26 08:53:42 +08:00
void AcceptWaveform(int32_t sampling_rate, const float *waveform,
int32_t n) const;
2023-02-19 11:42:15 +08:00
/**
* InputFinished() tells the class you won't be providing any
* more waveform. This will help flush out the last frame or two
* of features, in the case where snip-edges == false; it also
* affects the return value of IsLastFrame().
*/
2023-03-26 08:53:42 +08:00
void InputFinished() const;
2023-02-19 11:42:15 +08:00
int32_t NumFramesReady() const;
/** Note: IsLastFrame() will only ever return true if you have called
* InputFinished() (and this frame is the last frame).
*/
bool IsLastFrame(int32_t frame) const;
/** Get n frames starting from the given frame index.
*
* @param frame_index The starting frame index
* @param n Number of frames to get.
* @return Return a 2-D tensor of shape (n, feature_dim).
* which is flattened into a 1-D vector (flattened in in row major)
*/
std::vector<float> GetFrames(int32_t frame_index, int32_t n) const;
void Reset();
int32_t FeatureDim() const;
2023-02-22 15:35:55 +08:00
// Return a reference to the number of processed frames so far
// before subsampling..
2023-02-19 11:42:15 +08:00
// Initially, it is 0. It is always less than NumFramesReady().
//
// The returned reference is valid as long as this object is alive.
int32_t &GetNumProcessedFrames(); // It's reset after calling Reset()
int32_t GetNumFramesSinceStart() const;
2023-02-19 11:42:15 +08:00
int32_t &GetCurrentSegment();
2023-02-19 11:42:15 +08:00
void SetResult(const OnlineTransducerDecoderResult &r);
OnlineTransducerDecoderResult &GetResult();
2023-02-19 11:42:15 +08:00
void SetKeywordResult(const TransducerKeywordResult &r);
TransducerKeywordResult &GetKeywordResult(bool remove_duplicates = false);
void SetCtcResult(const OnlineCtcDecoderResult &r);
OnlineCtcDecoderResult &GetCtcResult();
2023-08-14 10:32:14 +08:00
void SetParaformerResult(const OnlineParaformerDecoderResult &r);
OnlineParaformerDecoderResult &GetParaformerResult();
2023-02-19 12:45:38 +08:00
void SetStates(std::vector<Ort::Value> states);
std::vector<Ort::Value> &GetStates();
/**
* Get the context graph corresponding to this stream.
*
* @return Return the context graph for this stream.
*/
const ContextGraphPtr &GetContextGraph() const;
// for online ctc decoder
void SetFasterDecoder(std::unique_ptr<kaldi_decoder::FasterDecoder> decoder);
kaldi_decoder::FasterDecoder *GetFasterDecoder() const;
int32_t &GetFasterDecoderProcessedFrames();
// for streaming paraformer
2023-08-14 10:32:14 +08:00
std::vector<float> &GetParaformerFeatCache();
std::vector<float> &GetParaformerEncoderOutCache();
std::vector<float> &GetParaformerAlphaCache();
2023-02-19 11:42:15 +08:00
private:
class Impl;
std::unique_ptr<Impl> impl_;
};
} // namespace sherpa_onnx
#endif // SHERPA_ONNX_CSRC_ONLINE_STREAM_H_