Refactor C# code and support building nuget packages for cross-platforms (#144)

This commit is contained in:
Fangjun Kuang
2023-05-10 14:53:04 +08:00
committed by GitHub
parent 0bc571f6ee
commit 7969cf44ac
40 changed files with 2050 additions and 2311 deletions

View File

@@ -18,12 +18,35 @@
extern "C" {
#endif
// See https://github.com/pytorch/pytorch/blob/main/c10/macros/Export.h
// We will set SHERPA_ONNX_BUILD_SHARED_LIBS and SHERPA_ONNX_BUILD_MAIN_LIB in
// CMakeLists.txt
#if defined(_WIN32)
#if defined(SHERPA_ONNX_BUILD_SHARED_LIBS)
#define SHERPA_ONNX_EXPORT __declspec(dllexport)
#define SHERPA_ONNX_IMPORT __declspec(dllimport)
#else
#define SHERPA_ONNX_EXPORT
#define SHERPA_ONNX_IMPORT
#endif
#else // WIN32
#define SHERPA_ONNX_EXPORT __attribute__((__visibility__("default")))
#define SHERPA_ONNX_IMPORT SHERPA_ONNX_EXPORT
#endif
#if defined(SHERPA_ONNX_BUILD_MAIN_LIB)
#define SHERPA_ONNX_API SHERPA_ONNX_EXPORT
#else
#define SHERPA_ONNX_API SHERPA_ONNX_IMPORT
#endif
/// Please refer to
/// https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html
/// to download pre-trained models. That is, you can find encoder-xxx.onnx
/// decoder-xxx.onnx, joiner-xxx.onnx, and tokens.txt for this struct
/// from there.
typedef struct SherpaOnnxOnlineTransducerModelConfig {
SHERPA_ONNX_API typedef struct SherpaOnnxOnlineTransducerModelConfig {
const char *encoder;
const char *decoder;
const char *joiner;
@@ -33,7 +56,7 @@ typedef struct SherpaOnnxOnlineTransducerModelConfig {
} SherpaOnnxOnlineTransducerModelConfig;
/// It expects 16 kHz 16-bit single channel wave format.
typedef struct SherpaOnnxFeatureConfig {
SHERPA_ONNX_API typedef struct SherpaOnnxFeatureConfig {
/// Sample rate of the input data. MUST match the one expected
/// by the model. For instance, it should be 16000 for models provided
/// by us.
@@ -44,7 +67,7 @@ typedef struct SherpaOnnxFeatureConfig {
int32_t feature_dim;
} SherpaOnnxFeatureConfig;
typedef struct SherpaOnnxOnlineRecognizerConfig {
SHERPA_ONNX_API typedef struct SherpaOnnxOnlineRecognizerConfig {
SherpaOnnxFeatureConfig feat_config;
SherpaOnnxOnlineTransducerModelConfig model_config;
@@ -75,7 +98,7 @@ typedef struct SherpaOnnxOnlineRecognizerConfig {
float rule3_min_utterance_length;
} SherpaOnnxOnlineRecognizerConfig;
typedef struct SherpaOnnxOnlineRecognizerResult {
SHERPA_ONNX_API typedef struct SherpaOnnxOnlineRecognizerResult {
const char *text;
// TODO(fangjun): Add more fields
} SherpaOnnxOnlineRecognizerResult;
@@ -83,32 +106,34 @@ typedef struct SherpaOnnxOnlineRecognizerResult {
/// Note: OnlineRecognizer here means StreamingRecognizer.
/// It does not need to access the Internet during recognition.
/// Everything is run locally.
typedef struct SherpaOnnxOnlineRecognizer SherpaOnnxOnlineRecognizer;
typedef struct SherpaOnnxOnlineStream SherpaOnnxOnlineStream;
SHERPA_ONNX_API typedef struct SherpaOnnxOnlineRecognizer
SherpaOnnxOnlineRecognizer;
SHERPA_ONNX_API typedef struct SherpaOnnxOnlineStream SherpaOnnxOnlineStream;
/// @param config Config for the recongizer.
/// @param config Config for the recognizer.
/// @return Return a pointer to the recognizer. The user has to invoke
// DestroyOnlineRecognizer() to free it to avoid memory leak.
SherpaOnnxOnlineRecognizer *CreateOnlineRecognizer(
SHERPA_ONNX_API SherpaOnnxOnlineRecognizer *CreateOnlineRecognizer(
const SherpaOnnxOnlineRecognizerConfig *config);
/// Free a pointer returned by CreateOnlineRecognizer()
///
/// @param p A pointer returned by CreateOnlineRecognizer()
void DestroyOnlineRecognizer(SherpaOnnxOnlineRecognizer *recognizer);
SHERPA_ONNX_API void DestroyOnlineRecognizer(
SherpaOnnxOnlineRecognizer *recognizer);
/// Create an online stream for accepting wave samples.
///
/// @param recognizer A pointer returned by CreateOnlineRecognizer()
/// @return Return a pointer to an OnlineStream. The user has to invoke
/// DestoryOnlineStream() to free it to avoid memory leak.
SherpaOnnxOnlineStream *CreateOnlineStream(
SHERPA_ONNX_API SherpaOnnxOnlineStream *CreateOnlineStream(
const SherpaOnnxOnlineRecognizer *recognizer);
/// Destory an online stream.
/// Destroy an online stream.
///
/// @param stream A pointer returned by CreateOnlineStream()
void DestoryOnlineStream(SherpaOnnxOnlineStream *stream);
SHERPA_ONNX_API void DestoryOnlineStream(SherpaOnnxOnlineStream *stream);
/// Accept input audio samples and compute the features.
/// The user has to invoke DecodeOnlineStream() to run the neural network and
@@ -121,16 +146,17 @@ void DestoryOnlineStream(SherpaOnnxOnlineStream *stream);
/// @param samples A pointer to a 1-D array containing audio samples.
/// The range of samples has to be normalized to [-1, 1].
/// @param n Number of elements in the samples array.
void AcceptWaveform(SherpaOnnxOnlineStream *stream, int32_t sample_rate,
const float *samples, int32_t n);
SHERPA_ONNX_API void AcceptWaveform(SherpaOnnxOnlineStream *stream,
int32_t sample_rate, const float *samples,
int32_t n);
/// Return 1 if there are enough number of feature frames for decoding.
/// Return 0 otherwise.
///
/// @param recognizer A pointer returned by CreateOnlineRecognizer
/// @param stream A pointer returned by CreateOnlineStream
int32_t IsOnlineStreamReady(SherpaOnnxOnlineRecognizer *recognizer,
SherpaOnnxOnlineStream *stream);
SHERPA_ONNX_API int32_t IsOnlineStreamReady(
SherpaOnnxOnlineRecognizer *recognizer, SherpaOnnxOnlineStream *stream);
/// Call this function to run the neural network model and decoding.
//
@@ -142,8 +168,8 @@ int32_t IsOnlineStreamReady(SherpaOnnxOnlineRecognizer *recognizer,
/// DecodeOnlineStream(recognizer, stream);
/// }
///
void DecodeOnlineStream(SherpaOnnxOnlineRecognizer *recognizer,
SherpaOnnxOnlineStream *stream);
SHERPA_ONNX_API void DecodeOnlineStream(SherpaOnnxOnlineRecognizer *recognizer,
SherpaOnnxOnlineStream *stream);
/// This function is similar to DecodeOnlineStream(). It decodes multiple
/// OnlineStream in parallel.
@@ -155,8 +181,9 @@ void DecodeOnlineStream(SherpaOnnxOnlineRecognizer *recognizer,
/// @param streams A pointer array containing pointers returned by
/// CreateOnlineRecognizer()
/// @param n Number of elements in the given streams array.
void DecodeMultipleOnlineStreams(SherpaOnnxOnlineRecognizer *recognizer,
SherpaOnnxOnlineStream **streams, int32_t n);
SHERPA_ONNX_API void DecodeMultipleOnlineStreams(
SherpaOnnxOnlineRecognizer *recognizer, SherpaOnnxOnlineStream **streams,
int32_t n);
/// Get the decoding results so far for an OnlineStream.
///
@@ -165,47 +192,188 @@ void DecodeMultipleOnlineStreams(SherpaOnnxOnlineRecognizer *recognizer,
/// @return A pointer containing the result. The user has to invoke
/// DestroyOnlineRecognizerResult() to free the returned pointer to
/// avoid memory leak.
SherpaOnnxOnlineRecognizerResult *GetOnlineStreamResult(
SHERPA_ONNX_API SherpaOnnxOnlineRecognizerResult *GetOnlineStreamResult(
SherpaOnnxOnlineRecognizer *recognizer, SherpaOnnxOnlineStream *stream);
/// Destroy the pointer returned by GetOnlineStreamResult().
///
/// @param r A pointer returned by GetOnlineStreamResult()
void DestroyOnlineRecognizerResult(const SherpaOnnxOnlineRecognizerResult *r);
SHERPA_ONNX_API void DestroyOnlineRecognizerResult(
const SherpaOnnxOnlineRecognizerResult *r);
/// Reset an OnlineStream , which clears the neural network model state
/// and the state for decoding.
///
/// @param recognizer A pointer returned by CreateOnlineRecognizer().
/// @param stream A pointer returned by CreateOnlineStream
void Reset(SherpaOnnxOnlineRecognizer *recognizer,
SherpaOnnxOnlineStream *stream);
SHERPA_ONNX_API void Reset(SherpaOnnxOnlineRecognizer *recognizer,
SherpaOnnxOnlineStream *stream);
/// Signal that no more audio samples would be available.
/// After this call, you cannot call AcceptWaveform() any more.
///
/// @param stream A pointer returned by CreateOnlineStream()
void InputFinished(SherpaOnnxOnlineStream *stream);
SHERPA_ONNX_API void InputFinished(SherpaOnnxOnlineStream *stream);
/// Return 1 if an endpoint has been detected.
///
/// @param recognizer A pointer returned by CreateOnlineRecognizer()
/// @param stream A pointer returned by CreateOnlineStream()
/// @return Return 1 if an endpoint is detected. Return 0 otherwise.
int32_t IsEndpoint(SherpaOnnxOnlineRecognizer *recognizer,
SherpaOnnxOnlineStream *stream);
SHERPA_ONNX_API int32_t IsEndpoint(SherpaOnnxOnlineRecognizer *recognizer,
SherpaOnnxOnlineStream *stream);
// for displaying results on Linux/macOS.
typedef struct SherpaOnnxDisplay SherpaOnnxDisplay;
SHERPA_ONNX_API typedef struct SherpaOnnxDisplay SherpaOnnxDisplay;
/// Create a display object. Must be freed using DestroyDisplay to avoid
/// memory leak.
SherpaOnnxDisplay *CreateDisplay(int32_t max_word_per_line);
SHERPA_ONNX_API SherpaOnnxDisplay *CreateDisplay(int32_t max_word_per_line);
void DestroyDisplay(SherpaOnnxDisplay *display);
SHERPA_ONNX_API void DestroyDisplay(SherpaOnnxDisplay *display);
/// Print the result.
void SherpaOnnxPrint(SherpaOnnxDisplay *display, int32_t idx, const char *s);
SHERPA_ONNX_API void SherpaOnnxPrint(SherpaOnnxDisplay *display, int32_t idx,
const char *s);
// ============================================================
// For offline ASR (i.e., non-streaming ASR)
// ============================================================
/// Please refer to
/// https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html
/// to download pre-trained models. That is, you can find encoder-xxx.onnx
/// decoder-xxx.onnx, and joiner-xxx.onnx for this struct
/// from there.
SHERPA_ONNX_API typedef struct SherpaOnnxOfflineTransducerModelConfig {
const char *encoder;
const char *decoder;
const char *joiner;
} SherpaOnnxOfflineTransducerModelConfig;
SHERPA_ONNX_API typedef struct SherpaOnnxOfflineParaformerModelConfig {
const char *model;
} SherpaOnnxOfflineParaformerModelConfig;
SHERPA_ONNX_API typedef struct SherpaOnnxOfflineNemoEncDecCtcModelConfig {
const char *model;
} SherpaOnnxOfflineNemoEncDecCtcModelConfig;
SHERPA_ONNX_API typedef struct SherpaOnnxOfflineLMConfig {
const char *model;
float scale;
} SherpaOnnxOfflineLMConfig;
SHERPA_ONNX_API typedef struct SherpaOnnxOfflineModelConfig {
SherpaOnnxOfflineTransducerModelConfig transducer;
SherpaOnnxOfflineParaformerModelConfig paraformer;
SherpaOnnxOfflineNemoEncDecCtcModelConfig nemo_ctc;
const char *tokens;
int32_t num_threads;
int32_t debug;
} SherpaOnnxOfflineModelConfig;
SHERPA_ONNX_API typedef struct SherpaOnnxOfflineRecognizerConfig {
SherpaOnnxFeatureConfig feat_config;
SherpaOnnxOfflineModelConfig model_config;
SherpaOnnxOfflineLMConfig lm_config;
const char *decoding_method;
int32_t max_active_paths;
} SherpaOnnxOfflineRecognizerConfig;
SHERPA_ONNX_API typedef struct SherpaOnnxOfflineRecognizer
SherpaOnnxOfflineRecognizer;
SHERPA_ONNX_API typedef struct SherpaOnnxOfflineStream SherpaOnnxOfflineStream;
/// @param config Config for the recognizer.
/// @return Return a pointer to the recognizer. The user has to invoke
// DestroyOfflineRecognizer() to free it to avoid memory leak.
SHERPA_ONNX_API SherpaOnnxOfflineRecognizer *CreateOfflineRecognizer(
const SherpaOnnxOfflineRecognizerConfig *config);
/// Free a pointer returned by CreateOfflineRecognizer()
///
/// @param p A pointer returned by CreateOfflineRecognizer()
SHERPA_ONNX_API void DestroyOfflineRecognizer(
SherpaOnnxOfflineRecognizer *recognizer);
/// Create an offline stream for accepting wave samples.
///
/// @param recognizer A pointer returned by CreateOfflineRecognizer()
/// @return Return a pointer to an OfflineStream. The user has to invoke
/// DestoryOfflineStream() to free it to avoid memory leak.
SHERPA_ONNX_API SherpaOnnxOfflineStream *CreateOfflineStream(
const SherpaOnnxOfflineRecognizer *recognizer);
/// Destroy an offline stream.
///
/// @param stream A pointer returned by CreateOfflineStream()
SHERPA_ONNX_API void DestoryOfflineStream(SherpaOnnxOfflineStream *stream);
/// Accept input audio samples and compute the features.
/// The user has to invoke DecodeOfflineStream() to run the neural network and
/// decoding.
///
/// @param stream A pointer returned by CreateOfflineStream().
/// @param sample_rate Sample rate of the input samples. If it is different
/// from config.feat_config.sample_rate, we will do
/// resampling inside sherpa-onnx.
/// @param samples A pointer to a 1-D array containing audio samples.
/// The range of samples has to be normalized to [-1, 1].
/// @param n Number of elements in the samples array.
///
/// @caution: For each offline stream, please invoke this function only once!
SHERPA_ONNX_API void AcceptWaveformOffline(SherpaOnnxOfflineStream *stream,
int32_t sample_rate,
const float *samples, int32_t n);
/// Decode an offline stream.
///
/// We assume you have invoked AcceptWaveformOffline() for the given stream
/// before calling this function.
///
/// @param recognizer A pointer returned by CreateOfflineRecognizer().
/// @param stream A pointer returned by CreateOfflineStream()
SHERPA_ONNX_API void DecodeOfflineStream(
SherpaOnnxOfflineRecognizer *recognizer, SherpaOnnxOfflineStream *stream);
/// Decode a list offline streams in parallel.
///
/// We assume you have invoked AcceptWaveformOffline() for each stream
/// before calling this function.
///
/// @param recognizer A pointer returned by CreateOfflineRecognizer().
/// @param streams A pointer pointer array containing pointers returned
/// by CreateOfflineStream().
/// @param n Number of entries in the given streams.
SHERPA_ONNX_API void DecodeMultipleOfflineStreams(
SherpaOnnxOfflineRecognizer *recognizer, SherpaOnnxOfflineStream **streams,
int32_t n);
SHERPA_ONNX_API typedef struct SherpaOnnxOfflineRecognizerResult {
const char *text;
// TODO(fangjun): Add more fields
} SherpaOnnxOfflineRecognizerResult;
/// Get the result of the offline stream.
///
/// We assume you have called DecodeOfflineStream() or
/// DecodeMultipleOfflineStreams() with the given stream before calling
/// this function.
///
/// @param stream A pointer returned by CreateOfflineStream().
/// @return Return a pointer to the result. The user has to invoke
/// DestroyOnlineRecognizerResult() to free the returned pointer to
/// avoid memory leak.
SHERPA_ONNX_API SherpaOnnxOfflineRecognizerResult *GetOfflineStreamResult(
SherpaOnnxOfflineStream *stream);
/// Destroy the pointer returned by GetOfflineStreamResult().
///
/// @param r A pointer returned by GetOfflineStreamResult()
SHERPA_ONNX_API void DestroyOfflineRecognizerResult(
const SherpaOnnxOfflineRecognizerResult *r);
#ifdef __cplusplus
} /* extern "C" */