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/features.h

87 lines
2.6 KiB
C
Raw Normal View History

2023-02-19 19:36:03 +08:00
// sherpa-onnx/csrc/features.h
2023-02-18 21:35:15 +08:00
//
// Copyright (c) 2023 Xiaomi Corporation
#ifndef SHERPA_ONNX_CSRC_FEATURES_H_
#define SHERPA_ONNX_CSRC_FEATURES_H_
#include <memory>
2023-02-19 12:45:38 +08:00
#include <string>
2023-02-18 21:35:15 +08:00
#include <vector>
#include "sherpa-onnx/csrc/parse-options.h"
2023-02-18 21:35:15 +08:00
namespace sherpa_onnx {
2023-02-19 10:39:07 +08:00
struct FeatureExtractorConfig {
2023-03-26 08:53:42 +08:00
// Sampling rate used by the feature extractor. If it is different from
// the sampling rate of the input waveform, we will do resampling inside.
int32_t sampling_rate = 16000;
2023-03-26 08:53:42 +08:00
// Feature dimension
2023-02-19 10:39:07 +08:00
int32_t feature_dim = 80;
2023-02-19 12:45:38 +08:00
2023-08-14 10:32:14 +08:00
// Set internally by some models, e.g., paraformer sets it to false.
// This parameter is not exposed to users from the commandline
// If true, the feature extractor expects inputs to be normalized to
// the range [-1, 1].
// If false, we will multiply the inputs by 32768
bool normalize_samples = true;
2023-02-19 12:45:38 +08:00
std::string ToString() const;
void Register(ParseOptions *po);
2023-02-19 10:39:07 +08:00
};
2023-02-18 21:35:15 +08:00
class FeatureExtractor {
public:
2023-02-19 10:39:07 +08:00
explicit FeatureExtractor(const FeatureExtractorConfig &config = {});
2023-02-19 09:57:56 +08:00
~FeatureExtractor();
2023-02-18 21:35:15 +08:00
/**
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-18 21:35: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-18 21:35:15 +08:00
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-18 21:35:15 +08:00
int32_t NumFramesReady() const;
2023-02-19 11:42:15 +08:00
/** Note: IsLastFrame() will only ever return true if you have called
* InputFinished() (and this frame is the last frame).
*/
2023-02-18 21:35:15 +08:00
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;
2023-02-19 09:57:56 +08:00
/// Return feature dim of this extractor
int32_t FeatureDim() const;
2023-02-18 21:35:15 +08:00
private:
2023-02-19 09:57:56 +08:00
class Impl;
std::unique_ptr<Impl> impl_;
2023-02-18 21:35:15 +08:00
};
} // namespace sherpa_onnx
#endif // SHERPA_ONNX_CSRC_FEATURES_H_