react-native-sherpa-onnx 0.4.1 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/android/src/main/assets/model_licenses/alignment-models-license-status.csv +5 -0
- package/android/src/main/cpp/CMakeLists.txt +3 -0
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-alignment-wrapper.cpp +66 -0
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-alignment-wrapper.h +17 -0
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-model-detect-alignment.cpp +108 -0
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-model-detect.h +30 -0
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-validate-alignment.cpp +66 -0
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-validate-alignment.h +30 -0
- package/android/src/main/cpp/jni/module/sherpa-onnx-module-jni.cpp +21 -0
- package/android/src/main/java/com/sherpaonnx/SherpaOnnxAlignmentHelper.kt +555 -0
- package/android/src/main/java/com/sherpaonnx/SherpaOnnxModule.kt +76 -0
- package/android/src/main/java/com/sherpaonnx/SherpaOnnxTextSegmenter.kt +330 -0
- package/android/src/main/java/com/sherpaonnx/SherpaOnnxTtsHelper.kt +114 -10
- package/ios/Resources/model_licenses/alignment-models-license-status.csv +5 -0
- package/ios/SherpaOnnx+Alignment.mm +704 -0
- package/ios/SherpaOnnx+STT.mm +6 -0
- package/ios/SherpaOnnx+TTS.mm +624 -50
- package/ios/model_detect/sherpa-onnx-model-detect-alignment.mm +108 -0
- package/ios/model_detect/sherpa-onnx-model-detect.h +31 -0
- package/ios/model_detect/sherpa-onnx-validate-alignment.h +30 -0
- package/ios/model_detect/sherpa-onnx-validate-alignment.mm +66 -0
- package/ios/stt/sherpa-onnx-stt-wrapper.h +3 -1
- package/ios/stt/sherpa-onnx-stt-wrapper.mm +6 -0
- package/lib/module/NativeSherpaOnnx.js.map +1 -1
- package/lib/module/alignment/index.js +27 -0
- package/lib/module/alignment/index.js.map +1 -0
- package/lib/module/alignment/types.js +2 -0
- package/lib/module/alignment/types.js.map +1 -0
- package/lib/module/alignment/vocab.js +40 -0
- package/lib/module/alignment/vocab.js.map +1 -0
- package/lib/module/download/paths.js +9 -1
- package/lib/module/download/paths.js.map +1 -1
- package/lib/module/download/registry.js +17 -1
- package/lib/module/download/registry.js.map +1 -1
- package/lib/module/download/types.js +1 -0
- package/lib/module/download/types.js.map +1 -1
- package/lib/module/index.js +6 -4
- package/lib/module/index.js.map +1 -1
- package/lib/module/licenses.js +8 -2
- package/lib/module/licenses.js.map +1 -1
- package/lib/module/stt/types.js.map +1 -1
- package/lib/module/tts/index.js +68 -2
- package/lib/module/tts/index.js.map +1 -1
- package/lib/module/tts/subtitles.js +400 -0
- package/lib/module/tts/subtitles.js.map +1 -0
- package/lib/module/tts/tempAudio.js +17 -0
- package/lib/module/tts/tempAudio.js.map +1 -0
- package/lib/module/tts/types.js.map +1 -1
- package/lib/typescript/src/NativeSherpaOnnx.d.ts +34 -3
- package/lib/typescript/src/NativeSherpaOnnx.d.ts.map +1 -1
- package/lib/typescript/src/alignment/index.d.ts +8 -0
- package/lib/typescript/src/alignment/index.d.ts.map +1 -0
- package/lib/typescript/src/alignment/types.d.ts +23 -0
- package/lib/typescript/src/alignment/types.d.ts.map +1 -0
- package/lib/typescript/src/alignment/vocab.d.ts +5 -0
- package/lib/typescript/src/alignment/vocab.d.ts.map +1 -0
- package/lib/typescript/src/download/paths.d.ts +5 -2
- package/lib/typescript/src/download/paths.d.ts.map +1 -1
- package/lib/typescript/src/download/registry.d.ts.map +1 -1
- package/lib/typescript/src/download/types.d.ts +2 -1
- package/lib/typescript/src/download/types.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +1 -0
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/licenses.d.ts.map +1 -1
- package/lib/typescript/src/stt/types.d.ts +5 -2
- package/lib/typescript/src/stt/types.d.ts.map +1 -1
- package/lib/typescript/src/tts/index.d.ts +2 -1
- package/lib/typescript/src/tts/index.d.ts.map +1 -1
- package/lib/typescript/src/tts/subtitles.d.ts +24 -0
- package/lib/typescript/src/tts/subtitles.d.ts.map +1 -0
- package/lib/typescript/src/tts/tempAudio.d.ts +3 -0
- package/lib/typescript/src/tts/tempAudio.d.ts.map +1 -0
- package/lib/typescript/src/tts/types.d.ts +68 -2
- package/lib/typescript/src/tts/types.d.ts.map +1 -1
- package/package.json +6 -1
- package/scripts/alignment-models/README.md +90 -0
- package/scripts/alignment-models/build_and_upload.js +724 -0
- package/scripts/alignment-models/sources.csv +5 -0
- package/scripts/alignment-models/sync_alignment_license_status.js +123 -0
- package/src/NativeSherpaOnnx.ts +35 -3
- package/src/alignment/index.ts +41 -0
- package/src/alignment/types.ts +22 -0
- package/src/alignment/vocab.ts +38 -0
- package/src/download/paths.ts +18 -5
- package/src/download/registry.ts +23 -3
- package/src/download/types.ts +1 -0
- package/src/index.tsx +6 -4
- package/src/licenses.ts +12 -1
- package/src/stt/types.ts +5 -2
- package/src/tts/index.ts +110 -3
- package/src/tts/subtitles.ts +611 -0
- package/src/tts/tempAudio.ts +31 -0
- package/src/tts/types.ts +79 -2
- package/third_party/sherpa-onnx-prebuilt/IOS_RELEASE_TAG +1 -1
package/README.md
CHANGED
|
@@ -121,6 +121,7 @@ Full step-by-step: [Download manager – Setup (iOS & Android)](docs/download-ma
|
|
|
121
121
|
| Live capture API | ✅ **Supported** | [PCM live stream](./docs/pcm-live-stream.md) | Native microphone capture with resampling for live transcription (use with streaming STT). |
|
|
122
122
|
| Text-to-Speech | ✅ **Supported** | [TTS](./docs/tts.md) | Multiple model types (VITS, Matcha, Kokoro, etc.). See [Supported Model Types](#supported-model-types). |
|
|
123
123
|
| Streaming Text-to-Speech | ✅ **Supported** | [Streaming TTS](./docs/tts-streaming.md) | Incremental speech generation for low time-to-first-byte and playback while generating. |
|
|
124
|
+
| TTS Alignment / Timestamps | ✅ **Supported** | [TTS Alignment](./docs/tts-alignment.md) | Full implementation: **`fast`** (native chunk-based, estimated timing) and **`accurate`** (wav2vec2 CTC forced alignment, `timingMode: 'aligned'`). Optional alignment ONNX via `react-native-sherpa-onnx/alignment` (see [TTS Alignment](./docs/tts-alignment.md)). Standalone API: `generateSubtitlesFromAudio()`. |
|
|
124
125
|
| Execution providers (CPU, NNAPI, XNNPACK, Core ML, QNN) | ✅ **Supported** | [Execution providers](./docs/execution-providers.md) | CPU default; optional accelerators per platform. |
|
|
125
126
|
| Play Asset Delivery (PAD) | ✅ **Supported** | [Model setup](./docs/model-setup.md) | Android only. Archives: [Extraction API](./docs/extraction.md). |
|
|
126
127
|
| Automatic Model type detection | ✅ **Supported** | [Model detection](./docs/model-setup.md#model-detection) | `detectSttModel()` and `detectTtsModel()` for a path. |
|
|
@@ -204,6 +205,7 @@ APIs, batch vs online processing, and initialization are covered in [Speech Enha
|
|
|
204
205
|
- [Streaming (Online) Speech-to-Text](./docs/stt-streaming.md) – Real-time recognition, partial results, endpoint detection
|
|
205
206
|
- [PCM Live Stream](./docs/pcm-live-stream.md) – Native microphone capture with resampling for live transcription (use with streaming STT)
|
|
206
207
|
- [Text-to-Speech (TTS)](./docs/tts.md) – Offline and streaming generation
|
|
208
|
+
- [TTS Alignment / Timestamps](./docs/tts-alignment.md) – `fast` and `accurate` modes, sentence/word/character granularity, alignment model download, `generateSpeechWithTimestamps()` and `generateSubtitlesFromAudio()`
|
|
207
209
|
- [Streaming Text-to-Speech](./docs/tts-streaming.md) – Incremental TTS (createStreamingTTS)
|
|
208
210
|
- [Execution provider support (QNN, NNAPI, XNNPACK, Core ML)](./docs/execution-providers.md) – Checking and using acceleration backends
|
|
209
211
|
- [Voice Activity Detection (VAD)](./docs/vad.md)
|
|
@@ -237,6 +239,7 @@ The example app included in this repository demonstrates audio-to-text transcrip
|
|
|
237
239
|
- **Offline** audio file transcription
|
|
238
240
|
- **Online (streaming) STT** – live transcription from the microphone with partial results
|
|
239
241
|
- **Streaming TTS** – incremental speech generation and playback
|
|
242
|
+
- **Generate timestamp** – subtitle/timestamp generation from audio (`fast` / `accurate` with optional alignment model download)
|
|
240
243
|
- Test audio files for different languages
|
|
241
244
|
|
|
242
245
|
**Getting started:**
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
asset_name,license_type,commercial_use,confidence,detection_source,license_file
|
|
2
|
+
wav2vec2-base-960h-fp16.tar.bz2,apache-2.0,yes,high,manual,https://huggingface.co/datasets/choosealicense/licenses/resolve/main/markdown/apache-2.0.md
|
|
3
|
+
wav2vec2-base-960h-int8.tar.bz2,apache-2.0,yes,high,manual,https://huggingface.co/datasets/choosealicense/licenses/resolve/main/markdown/apache-2.0.md
|
|
4
|
+
wav2vec2-base-960h-q4f16.tar.bz2,apache-2.0,yes,high,manual,https://huggingface.co/datasets/choosealicense/licenses/resolve/main/markdown/apache-2.0.md
|
|
5
|
+
wav2vec2-base-960h.tar.bz2,apache-2.0,yes,high,manual,https://huggingface.co/datasets/choosealicense/licenses/resolve/main/markdown/apache-2.0.md
|
|
@@ -84,13 +84,16 @@ set(SOURCES
|
|
|
84
84
|
jni/model_detect/sherpa-onnx-model-detect-stt.cpp
|
|
85
85
|
jni/model_detect/sherpa-onnx-model-detect-tts.cpp
|
|
86
86
|
jni/model_detect/sherpa-onnx-model-detect-enhancement.cpp
|
|
87
|
+
jni/model_detect/sherpa-onnx-model-detect-alignment.cpp
|
|
87
88
|
jni/model_detect/sherpa-onnx-validate-stt.cpp
|
|
88
89
|
jni/model_detect/sherpa-onnx-validate-tts.cpp
|
|
89
90
|
jni/model_detect/sherpa-onnx-validate-enhancement.cpp
|
|
91
|
+
jni/model_detect/sherpa-onnx-validate-alignment.cpp
|
|
90
92
|
jni/model_detect/sherpa-onnx-detect-jni-common.cpp
|
|
91
93
|
jni/model_detect/sherpa-onnx-stt-wrapper.cpp
|
|
92
94
|
jni/model_detect/sherpa-onnx-tts-wrapper.cpp
|
|
93
95
|
jni/model_detect/sherpa-onnx-enhancement-wrapper.cpp
|
|
96
|
+
jni/model_detect/sherpa-onnx-alignment-wrapper.cpp
|
|
94
97
|
jni/audio/sherpa-onnx-audio-convert-jni.cpp
|
|
95
98
|
crypto/sha256.cpp
|
|
96
99
|
)
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#include "sherpa-onnx-alignment-wrapper.h"
|
|
2
|
+
|
|
3
|
+
#include "sherpa-onnx-detect-jni-common.h"
|
|
4
|
+
|
|
5
|
+
namespace sherpaonnx {
|
|
6
|
+
namespace {
|
|
7
|
+
|
|
8
|
+
const char* AlignmentModelKindToString(AlignmentModelKind k) {
|
|
9
|
+
switch (k) {
|
|
10
|
+
case AlignmentModelKind::kWav2Vec2:
|
|
11
|
+
return "wav2vec2";
|
|
12
|
+
default:
|
|
13
|
+
return "unknown";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
} // namespace
|
|
18
|
+
|
|
19
|
+
jobject AlignmentDetectResultToJava(
|
|
20
|
+
JNIEnv* env,
|
|
21
|
+
const AlignmentDetectResult& result
|
|
22
|
+
) {
|
|
23
|
+
jclass mapClass = env->FindClass("java/util/HashMap");
|
|
24
|
+
if (!mapClass) return nullptr;
|
|
25
|
+
jmethodID mapInit = env->GetMethodID(mapClass, "<init>", "()V");
|
|
26
|
+
jmethodID mapPut =
|
|
27
|
+
env->GetMethodID(mapClass, "put",
|
|
28
|
+
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
|
|
29
|
+
if (!mapInit || !mapPut) {
|
|
30
|
+
env->DeleteLocalRef(mapClass);
|
|
31
|
+
return nullptr;
|
|
32
|
+
}
|
|
33
|
+
jobject map = env->NewObject(mapClass, mapInit);
|
|
34
|
+
env->DeleteLocalRef(mapClass);
|
|
35
|
+
if (!map) return nullptr;
|
|
36
|
+
|
|
37
|
+
PutBoolean(env, map, mapPut, "success", result.ok);
|
|
38
|
+
PutString(env, map, mapPut, "error", result.error);
|
|
39
|
+
PutString(env, map, mapPut, "modelType",
|
|
40
|
+
AlignmentModelKindToString(result.selectedKind));
|
|
41
|
+
|
|
42
|
+
jobject detectedList = BuildDetectedModelsList(env, result.detectedModels);
|
|
43
|
+
if (detectedList) {
|
|
44
|
+
jstring keyDetected = env->NewStringUTF("detectedModels");
|
|
45
|
+
env->CallObjectMethod(map, mapPut, keyDetected, detectedList);
|
|
46
|
+
env->DeleteLocalRef(keyDetected);
|
|
47
|
+
env->DeleteLocalRef(detectedList);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
jclass hashMapClass = env->FindClass("java/util/HashMap");
|
|
51
|
+
if (hashMapClass) {
|
|
52
|
+
jobject pathsMap = env->NewObject(hashMapClass, mapInit);
|
|
53
|
+
env->DeleteLocalRef(hashMapClass);
|
|
54
|
+
if (pathsMap) {
|
|
55
|
+
PutString(env, pathsMap, mapPut, "model", result.paths.model);
|
|
56
|
+
jstring keyPaths = env->NewStringUTF("paths");
|
|
57
|
+
env->CallObjectMethod(map, mapPut, keyPaths, pathsMap);
|
|
58
|
+
env->DeleteLocalRef(keyPaths);
|
|
59
|
+
env->DeleteLocalRef(pathsMap);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return map;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
} // namespace sherpaonnx
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#ifndef SHERPA_ONNX_ALIGNMENT_WRAPPER_H
|
|
2
|
+
#define SHERPA_ONNX_ALIGNMENT_WRAPPER_H
|
|
3
|
+
|
|
4
|
+
#include <jni.h>
|
|
5
|
+
|
|
6
|
+
#include "sherpa-onnx-model-detect.h"
|
|
7
|
+
|
|
8
|
+
namespace sherpaonnx {
|
|
9
|
+
|
|
10
|
+
jobject AlignmentDetectResultToJava(
|
|
11
|
+
JNIEnv* env,
|
|
12
|
+
const AlignmentDetectResult& result
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
} // namespace sherpaonnx
|
|
16
|
+
|
|
17
|
+
#endif // SHERPA_ONNX_ALIGNMENT_WRAPPER_H
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
#include "sherpa-onnx-model-detect.h"
|
|
2
|
+
#include "sherpa-onnx-model-detect-helper.h"
|
|
3
|
+
#include "sherpa-onnx-validate-alignment.h"
|
|
4
|
+
|
|
5
|
+
#include <optional>
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <vector>
|
|
8
|
+
|
|
9
|
+
namespace {
|
|
10
|
+
|
|
11
|
+
using namespace sherpaonnx::model_detect;
|
|
12
|
+
|
|
13
|
+
sherpaonnx::AlignmentModelKind ParseAlignmentModelType(const std::string& modelType) {
|
|
14
|
+
if (modelType == "wav2vec2") return sherpaonnx::AlignmentModelKind::kWav2Vec2;
|
|
15
|
+
return sherpaonnx::AlignmentModelKind::kUnknown;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
sherpaonnx::AlignmentDetectResult DetectAlignmentModelFromFiles(
|
|
19
|
+
const std::vector<FileEntry>& files,
|
|
20
|
+
const std::string& modelDir,
|
|
21
|
+
const std::string& modelType
|
|
22
|
+
) {
|
|
23
|
+
sherpaonnx::AlignmentDetectResult result;
|
|
24
|
+
|
|
25
|
+
const std::string wav2vec2Model =
|
|
26
|
+
FindOnnxByAnyToken(files, {"wav2vec2", "model"}, std::nullopt);
|
|
27
|
+
|
|
28
|
+
if (!wav2vec2Model.empty()) {
|
|
29
|
+
result.detectedModels.push_back({"wav2vec2", modelDir});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
sherpaonnx::AlignmentModelKind selected = sherpaonnx::AlignmentModelKind::kUnknown;
|
|
33
|
+
if (modelType == "auto" || modelType.empty()) {
|
|
34
|
+
if (!wav2vec2Model.empty()) {
|
|
35
|
+
selected = sherpaonnx::AlignmentModelKind::kWav2Vec2;
|
|
36
|
+
}
|
|
37
|
+
} else {
|
|
38
|
+
selected = ParseAlignmentModelType(modelType);
|
|
39
|
+
if (selected == sherpaonnx::AlignmentModelKind::kUnknown) {
|
|
40
|
+
result.error = "Alignment: unknown model type: " + modelType;
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
switch (selected) {
|
|
46
|
+
case sherpaonnx::AlignmentModelKind::kWav2Vec2:
|
|
47
|
+
result.paths.model = wav2vec2Model;
|
|
48
|
+
break;
|
|
49
|
+
default:
|
|
50
|
+
result.error = "Alignment: no compatible model type detected in " +
|
|
51
|
+
modelDir;
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
auto validation =
|
|
56
|
+
sherpaonnx::ValidateAlignmentPaths(selected, result.paths, modelDir);
|
|
57
|
+
if (!validation.ok) {
|
|
58
|
+
result.error = validation.error;
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
result.selectedKind = selected;
|
|
63
|
+
result.ok = true;
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
} // namespace
|
|
68
|
+
|
|
69
|
+
namespace sherpaonnx {
|
|
70
|
+
|
|
71
|
+
using namespace model_detect;
|
|
72
|
+
|
|
73
|
+
AlignmentDetectResult DetectAlignmentModel(
|
|
74
|
+
const std::string& modelDir,
|
|
75
|
+
const std::string& modelType
|
|
76
|
+
) {
|
|
77
|
+
AlignmentDetectResult result;
|
|
78
|
+
|
|
79
|
+
if (modelDir.empty()) {
|
|
80
|
+
result.error = "Alignment: model directory is empty";
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
if (!FileExists(modelDir) || !IsDirectory(modelDir)) {
|
|
84
|
+
result.error =
|
|
85
|
+
"Alignment: model directory does not exist or is not a directory: " +
|
|
86
|
+
modelDir;
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const std::vector<model_detect::FileEntry> files = ListFilesRecursive(modelDir, 4);
|
|
91
|
+
return DetectAlignmentModelFromFiles(files, modelDir, modelType);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Test-only: used by host-side model_detect_test; not used in production.
|
|
95
|
+
AlignmentDetectResult DetectAlignmentModelFromFileList(
|
|
96
|
+
const std::vector<model_detect::FileEntry>& files,
|
|
97
|
+
const std::string& modelDir,
|
|
98
|
+
const std::string& modelType
|
|
99
|
+
) {
|
|
100
|
+
AlignmentDetectResult result;
|
|
101
|
+
if (modelDir.empty()) {
|
|
102
|
+
result.error = "Alignment: model directory is empty";
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
return DetectAlignmentModelFromFiles(files, modelDir, modelType);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
} // namespace sherpaonnx
|
|
@@ -49,6 +49,11 @@ enum class EnhancementModelKind {
|
|
|
49
49
|
kDpdfNet
|
|
50
50
|
};
|
|
51
51
|
|
|
52
|
+
enum class AlignmentModelKind {
|
|
53
|
+
kUnknown,
|
|
54
|
+
kWav2Vec2
|
|
55
|
+
};
|
|
56
|
+
|
|
52
57
|
struct SttModelPaths {
|
|
53
58
|
std::string encoder;
|
|
54
59
|
std::string decoder;
|
|
@@ -184,6 +189,10 @@ struct EnhancementModelPaths {
|
|
|
184
189
|
std::string model;
|
|
185
190
|
};
|
|
186
191
|
|
|
192
|
+
struct AlignmentModelPaths {
|
|
193
|
+
std::string model;
|
|
194
|
+
};
|
|
195
|
+
|
|
187
196
|
struct SttDetectResult {
|
|
188
197
|
bool ok = false;
|
|
189
198
|
std::string error;
|
|
@@ -213,6 +222,14 @@ struct EnhancementDetectResult {
|
|
|
213
222
|
EnhancementModelPaths paths;
|
|
214
223
|
};
|
|
215
224
|
|
|
225
|
+
struct AlignmentDetectResult {
|
|
226
|
+
bool ok = false;
|
|
227
|
+
std::string error;
|
|
228
|
+
std::vector<DetectedModel> detectedModels;
|
|
229
|
+
AlignmentModelKind selectedKind = AlignmentModelKind::kUnknown;
|
|
230
|
+
AlignmentModelPaths paths;
|
|
231
|
+
};
|
|
232
|
+
|
|
216
233
|
SttDetectResult DetectSttModel(
|
|
217
234
|
const std::string& modelDir,
|
|
218
235
|
const std::optional<bool>& preferInt8,
|
|
@@ -251,6 +268,11 @@ EnhancementDetectResult DetectEnhancementModel(
|
|
|
251
268
|
const std::string& modelType
|
|
252
269
|
);
|
|
253
270
|
|
|
271
|
+
AlignmentDetectResult DetectAlignmentModel(
|
|
272
|
+
const std::string& modelDir,
|
|
273
|
+
const std::string& modelType
|
|
274
|
+
);
|
|
275
|
+
|
|
254
276
|
/** Test-only: Like DetectEnhancementModel but takes a pre-built file list; no filesystem access.
|
|
255
277
|
* Only used by the host-side C++ test suite (test/cpp/model_detect_test.cpp). */
|
|
256
278
|
EnhancementDetectResult DetectEnhancementModelFromFileList(
|
|
@@ -259,6 +281,14 @@ EnhancementDetectResult DetectEnhancementModelFromFileList(
|
|
|
259
281
|
const std::string& modelType = "auto"
|
|
260
282
|
);
|
|
261
283
|
|
|
284
|
+
/** Test-only: Like DetectAlignmentModel but takes a pre-built file list; no filesystem access.
|
|
285
|
+
* Only used by the host-side C++ test suite (test/cpp/model_detect_test.cpp). */
|
|
286
|
+
AlignmentDetectResult DetectAlignmentModelFromFileList(
|
|
287
|
+
const std::vector<model_detect::FileEntry>& files,
|
|
288
|
+
const std::string& modelDir,
|
|
289
|
+
const std::string& modelType = "auto"
|
|
290
|
+
);
|
|
291
|
+
|
|
262
292
|
} // namespace sherpaonnx
|
|
263
293
|
|
|
264
294
|
#endif // SHERPA_ONNX_MODEL_DETECT_H
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#include "sherpa-onnx-validate-alignment.h"
|
|
2
|
+
|
|
3
|
+
#include <cstddef>
|
|
4
|
+
|
|
5
|
+
namespace sherpaonnx {
|
|
6
|
+
namespace {
|
|
7
|
+
|
|
8
|
+
static const AlignmentFieldRequirement kWav2Vec2Reqs[] = {
|
|
9
|
+
{"model", &AlignmentModelPaths::model, true},
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
static const AlignmentFieldRequirement* GetRequirements(
|
|
13
|
+
AlignmentModelKind kind,
|
|
14
|
+
size_t& count
|
|
15
|
+
) {
|
|
16
|
+
switch (kind) {
|
|
17
|
+
case AlignmentModelKind::kWav2Vec2:
|
|
18
|
+
count = std::size(kWav2Vec2Reqs);
|
|
19
|
+
return kWav2Vec2Reqs;
|
|
20
|
+
default:
|
|
21
|
+
count = 0;
|
|
22
|
+
return nullptr;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static const char* AlignmentKindToName(AlignmentModelKind kind) {
|
|
27
|
+
switch (kind) {
|
|
28
|
+
case AlignmentModelKind::kWav2Vec2:
|
|
29
|
+
return "wav2vec2";
|
|
30
|
+
default:
|
|
31
|
+
return "unknown";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
} // namespace
|
|
36
|
+
|
|
37
|
+
AlignmentValidationResult ValidateAlignmentPaths(
|
|
38
|
+
AlignmentModelKind kind,
|
|
39
|
+
const AlignmentModelPaths& paths,
|
|
40
|
+
const std::string& modelDir
|
|
41
|
+
) {
|
|
42
|
+
AlignmentValidationResult result;
|
|
43
|
+
size_t count = 0;
|
|
44
|
+
const auto* reqs = GetRequirements(kind, count);
|
|
45
|
+
if (!reqs) return result;
|
|
46
|
+
|
|
47
|
+
for (size_t i = 0; i < count; ++i) {
|
|
48
|
+
if (reqs[i].required && (paths.*(reqs[i].field)).empty()) {
|
|
49
|
+
result.missingRequired.push_back(reqs[i].fieldName);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!result.missingRequired.empty()) {
|
|
54
|
+
result.ok = false;
|
|
55
|
+
result.error = std::string("Alignment ") + AlignmentKindToName(kind) +
|
|
56
|
+
": missing required files in " + modelDir + ": ";
|
|
57
|
+
for (size_t i = 0; i < result.missingRequired.size(); ++i) {
|
|
58
|
+
if (i > 0) result.error += ", ";
|
|
59
|
+
result.error += result.missingRequired[i];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
} // namespace sherpaonnx
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
#ifndef SHERPA_ONNX_VALIDATE_ALIGNMENT_H
|
|
2
|
+
#define SHERPA_ONNX_VALIDATE_ALIGNMENT_H
|
|
3
|
+
|
|
4
|
+
#include "sherpa-onnx-model-detect.h"
|
|
5
|
+
#include <string>
|
|
6
|
+
#include <vector>
|
|
7
|
+
|
|
8
|
+
namespace sherpaonnx {
|
|
9
|
+
|
|
10
|
+
struct AlignmentFieldRequirement {
|
|
11
|
+
const char* fieldName;
|
|
12
|
+
std::string AlignmentModelPaths::* field;
|
|
13
|
+
bool required;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
struct AlignmentValidationResult {
|
|
17
|
+
bool ok = true;
|
|
18
|
+
std::vector<std::string> missingRequired;
|
|
19
|
+
std::string error;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
AlignmentValidationResult ValidateAlignmentPaths(
|
|
23
|
+
AlignmentModelKind kind,
|
|
24
|
+
const AlignmentModelPaths& paths,
|
|
25
|
+
const std::string& modelDir
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
} // namespace sherpaonnx
|
|
29
|
+
|
|
30
|
+
#endif // SHERPA_ONNX_VALIDATE_ALIGNMENT_H
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
#include "sherpa-onnx-stt-wrapper.h"
|
|
22
22
|
#include "sherpa-onnx-tts-wrapper.h"
|
|
23
23
|
#include "sherpa-onnx-enhancement-wrapper.h"
|
|
24
|
+
#include "sherpa-onnx-alignment-wrapper.h"
|
|
24
25
|
|
|
25
26
|
extern "C" {
|
|
26
27
|
|
|
@@ -208,4 +209,24 @@ Java_com_sherpaonnx_SherpaOnnxModule_nativeDetectEnhancementModel(
|
|
|
208
209
|
return sherpaonnx::EnhancementDetectResultToJava(env, result);
|
|
209
210
|
}
|
|
210
211
|
|
|
212
|
+
// Detect alignment model in directory. Returns HashMap with success, error, detectedModels, modelType, paths.
|
|
213
|
+
JNIEXPORT jobject JNICALL
|
|
214
|
+
Java_com_sherpaonnx_SherpaOnnxModule_nativeDetectAlignmentModel(
|
|
215
|
+
JNIEnv* env,
|
|
216
|
+
jobject /* this */,
|
|
217
|
+
jstring j_model_dir,
|
|
218
|
+
jstring j_model_type) {
|
|
219
|
+
const char* model_dir_c = env->GetStringUTFChars(j_model_dir, nullptr);
|
|
220
|
+
const char* model_type_c =
|
|
221
|
+
j_model_type ? env->GetStringUTFChars(j_model_type, nullptr) : nullptr;
|
|
222
|
+
std::string model_dir(model_dir_c ? model_dir_c : "");
|
|
223
|
+
std::string model_type(model_type_c ? model_type_c : "auto");
|
|
224
|
+
env->ReleaseStringUTFChars(j_model_dir, model_dir_c);
|
|
225
|
+
if (model_type_c) env->ReleaseStringUTFChars(j_model_type, model_type_c);
|
|
226
|
+
|
|
227
|
+
sherpaonnx::AlignmentDetectResult result =
|
|
228
|
+
sherpaonnx::DetectAlignmentModel(model_dir, model_type);
|
|
229
|
+
return sherpaonnx::AlignmentDetectResultToJava(env, result);
|
|
230
|
+
}
|
|
231
|
+
|
|
211
232
|
} // extern "C"
|