react-native-sherpa-onnx 0.1.0

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.
Files changed (83) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +402 -0
  3. package/SherpaOnnx.podspec +84 -0
  4. package/android/build.gradle +193 -0
  5. package/android/src/main/AndroidManifest.xml +2 -0
  6. package/android/src/main/cpp/CMakeLists.txt +121 -0
  7. package/android/src/main/cpp/include/sherpa-onnx/c-api/c-api.h +1918 -0
  8. package/android/src/main/cpp/include/sherpa-onnx/c-api/cxx-api.h +841 -0
  9. package/android/src/main/cpp/jni/sherpa-onnx-jni.cpp +129 -0
  10. package/android/src/main/cpp/jni/sherpa-onnx-wrapper.cpp +649 -0
  11. package/android/src/main/cpp/jni/sherpa-onnx-wrapper.h +56 -0
  12. package/android/src/main/java/com/sherpaonnx/SherpaOnnxModule.kt +316 -0
  13. package/android/src/main/java/com/sherpaonnx/SherpaOnnxPackage.kt +33 -0
  14. package/ios/Frameworks/sherpa_onnx.xcframework.zip +0 -0
  15. package/ios/SherpaOnnx.h +5 -0
  16. package/ios/SherpaOnnx.mm +293 -0
  17. package/ios/SherpaOnnx.xcconfig +19 -0
  18. package/ios/include/sherpa-onnx/c-api/c-api.h +1918 -0
  19. package/ios/include/sherpa-onnx/c-api/cxx-api.h +841 -0
  20. package/ios/sherpa-onnx-wrapper.h +57 -0
  21. package/ios/sherpa-onnx-wrapper.mm +432 -0
  22. package/lib/module/NativeSherpaOnnx.js +5 -0
  23. package/lib/module/NativeSherpaOnnx.js.map +1 -0
  24. package/lib/module/diarization/index.js +54 -0
  25. package/lib/module/diarization/index.js.map +1 -0
  26. package/lib/module/enhancement/index.js +54 -0
  27. package/lib/module/enhancement/index.js.map +1 -0
  28. package/lib/module/index.js +25 -0
  29. package/lib/module/index.js.map +1 -0
  30. package/lib/module/package.json +1 -0
  31. package/lib/module/separation/index.js +54 -0
  32. package/lib/module/separation/index.js.map +1 -0
  33. package/lib/module/stt/index.js +79 -0
  34. package/lib/module/stt/index.js.map +1 -0
  35. package/lib/module/stt/types.js +4 -0
  36. package/lib/module/stt/types.js.map +1 -0
  37. package/lib/module/tts/index.js +54 -0
  38. package/lib/module/tts/index.js.map +1 -0
  39. package/lib/module/types.js +2 -0
  40. package/lib/module/types.js.map +1 -0
  41. package/lib/module/utils.js +93 -0
  42. package/lib/module/utils.js.map +1 -0
  43. package/lib/module/vad/index.js +54 -0
  44. package/lib/module/vad/index.js.map +1 -0
  45. package/lib/typescript/package.json +1 -0
  46. package/lib/typescript/src/NativeSherpaOnnx.d.ts +39 -0
  47. package/lib/typescript/src/NativeSherpaOnnx.d.ts.map +1 -0
  48. package/lib/typescript/src/diarization/index.d.ts +49 -0
  49. package/lib/typescript/src/diarization/index.d.ts.map +1 -0
  50. package/lib/typescript/src/enhancement/index.d.ts +47 -0
  51. package/lib/typescript/src/enhancement/index.d.ts.map +1 -0
  52. package/lib/typescript/src/index.d.ts +9 -0
  53. package/lib/typescript/src/index.d.ts.map +1 -0
  54. package/lib/typescript/src/separation/index.d.ts +48 -0
  55. package/lib/typescript/src/separation/index.d.ts.map +1 -0
  56. package/lib/typescript/src/stt/index.d.ts +53 -0
  57. package/lib/typescript/src/stt/index.d.ts.map +1 -0
  58. package/lib/typescript/src/stt/types.d.ts +39 -0
  59. package/lib/typescript/src/stt/types.d.ts.map +1 -0
  60. package/lib/typescript/src/tts/index.d.ts +47 -0
  61. package/lib/typescript/src/tts/index.d.ts.map +1 -0
  62. package/lib/typescript/src/types.d.ts +59 -0
  63. package/lib/typescript/src/types.d.ts.map +1 -0
  64. package/lib/typescript/src/utils.d.ts +53 -0
  65. package/lib/typescript/src/utils.d.ts.map +1 -0
  66. package/lib/typescript/src/vad/index.d.ts +48 -0
  67. package/lib/typescript/src/vad/index.d.ts.map +1 -0
  68. package/package.json +221 -0
  69. package/scripts/copy-headers.js +184 -0
  70. package/scripts/setup-assets.js +323 -0
  71. package/scripts/setup-ios-framework.sh +282 -0
  72. package/scripts/switch-registry.js +75 -0
  73. package/src/NativeSherpaOnnx.ts +44 -0
  74. package/src/diarization/index.ts +69 -0
  75. package/src/enhancement/index.ts +67 -0
  76. package/src/index.tsx +30 -0
  77. package/src/separation/index.ts +68 -0
  78. package/src/stt/index.ts +83 -0
  79. package/src/stt/types.ts +42 -0
  80. package/src/tts/index.ts +67 -0
  81. package/src/types.ts +73 -0
  82. package/src/utils.ts +97 -0
  83. package/src/vad/index.ts +70 -0
@@ -0,0 +1,44 @@
1
+ import { TurboModuleRegistry, type TurboModule } from 'react-native';
2
+
3
+ export interface Spec extends TurboModule {
4
+ /**
5
+ * Test method to verify sherpa-onnx native library is loaded.
6
+ * Phase 1: Minimal "Hello World" test.
7
+ */
8
+ testSherpaInit(): Promise<string>;
9
+
10
+ /**
11
+ * Resolve model path based on configuration.
12
+ * Handles asset paths, file system paths, and auto-detection.
13
+ * Returns an absolute path that can be used by native code.
14
+ *
15
+ * @param config - Object with 'type' ('asset' | 'file' | 'auto') and 'path' (string)
16
+ */
17
+ resolveModelPath(config: { type: string; path: string }): Promise<string>;
18
+
19
+ /**
20
+ * Initialize sherpa-onnx with model directory.
21
+ * Expects an absolute path (use resolveModelPath first for asset/file paths).
22
+ * @param modelDir - Absolute path to model directory
23
+ * @param preferInt8 - Optional: true = prefer int8 models, false = prefer regular models, undefined = try int8 first (default)
24
+ * @param modelType - Optional: explicit model type ('transducer', 'paraformer', 'nemo_ctc', 'auto'), undefined = auto (default)
25
+ */
26
+ initializeSherpaOnnx(
27
+ modelDir: string,
28
+ preferInt8?: boolean,
29
+ modelType?: string
30
+ ): Promise<void>;
31
+
32
+ /**
33
+ * Transcribe an audio file.
34
+ * Phase 1: Stub implementation.
35
+ */
36
+ transcribeFile(filePath: string): Promise<string>;
37
+
38
+ /**
39
+ * Release sherpa-onnx resources.
40
+ */
41
+ unloadSherpaOnnx(): Promise<void>;
42
+ }
43
+
44
+ export default TurboModuleRegistry.getEnforcing<Spec>('SherpaOnnx');
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Speaker Diarization feature module
3
+ *
4
+ * @remarks
5
+ * This feature is not yet implemented. This module serves as a placeholder
6
+ * for future speaker diarization functionality.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * // Future usage:
11
+ * import { initializeDiarization, diarizeAudio } from 'react-native-sherpa-onnx/diarization';
12
+ *
13
+ * await initializeDiarization({ modelPath: 'models/diarization-model' });
14
+ * const segments = await diarizeAudio('path/to/audio.wav');
15
+ * ```
16
+ */
17
+
18
+ /**
19
+ * Diarization initialization options (placeholder)
20
+ */
21
+ export interface DiarizationInitializeOptions {
22
+ modelPath: string;
23
+ // Additional diarization-specific options will be added here
24
+ }
25
+
26
+ /**
27
+ * Speaker segment with speaker ID
28
+ */
29
+ export interface SpeakerSegment {
30
+ speakerId: string;
31
+ start: number;
32
+ end: number;
33
+ // Additional segment fields will be added here
34
+ }
35
+
36
+ /**
37
+ * Initialize Speaker Diarization with model directory.
38
+ *
39
+ * @throws {Error} Not yet implemented
40
+ */
41
+ export async function initializeDiarization(
42
+ _options: DiarizationInitializeOptions
43
+ ): Promise<void> {
44
+ throw new Error(
45
+ 'Speaker Diarization feature is not yet implemented. This is a placeholder module.'
46
+ );
47
+ }
48
+
49
+ /**
50
+ * Perform speaker diarization on an audio file.
51
+ *
52
+ * @throws {Error} Not yet implemented
53
+ */
54
+ export function diarizeAudio(_filePath: string): Promise<SpeakerSegment[]> {
55
+ throw new Error(
56
+ 'Speaker Diarization feature is not yet implemented. This is a placeholder module.'
57
+ );
58
+ }
59
+
60
+ /**
61
+ * Release diarization resources.
62
+ *
63
+ * @throws {Error} Not yet implemented
64
+ */
65
+ export function unloadDiarization(): Promise<void> {
66
+ throw new Error(
67
+ 'Speaker Diarization feature is not yet implemented. This is a placeholder module.'
68
+ );
69
+ }
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Speech Enhancement feature module
3
+ *
4
+ * @remarks
5
+ * This feature is not yet implemented. This module serves as a placeholder
6
+ * for future speech enhancement functionality.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * // Future usage:
11
+ * import { initializeEnhancement, enhanceAudio } from 'react-native-sherpa-onnx/enhancement';
12
+ *
13
+ * await initializeEnhancement({ modelPath: 'models/enhancement-model' });
14
+ * const enhancedPath = await enhanceAudio('path/to/noisy-audio.wav');
15
+ * ```
16
+ */
17
+
18
+ /**
19
+ * Enhancement initialization options (placeholder)
20
+ */
21
+ export interface EnhancementInitializeOptions {
22
+ modelPath: string;
23
+ // Additional enhancement-specific options will be added here
24
+ }
25
+
26
+ /**
27
+ * Enhancement result
28
+ */
29
+ export interface EnhancementResult {
30
+ outputPath: string;
31
+ // Additional result fields will be added here
32
+ }
33
+
34
+ /**
35
+ * Initialize Speech Enhancement with model directory.
36
+ *
37
+ * @throws {Error} Not yet implemented
38
+ */
39
+ export async function initializeEnhancement(
40
+ _options: EnhancementInitializeOptions
41
+ ): Promise<void> {
42
+ throw new Error(
43
+ 'Speech Enhancement feature is not yet implemented. This is a placeholder module.'
44
+ );
45
+ }
46
+
47
+ /**
48
+ * Enhance speech quality in an audio file.
49
+ *
50
+ * @throws {Error} Not yet implemented
51
+ */
52
+ export function enhanceAudio(_filePath: string): Promise<EnhancementResult> {
53
+ throw new Error(
54
+ 'Speech Enhancement feature is not yet implemented. This is a placeholder module.'
55
+ );
56
+ }
57
+
58
+ /**
59
+ * Release enhancement resources.
60
+ *
61
+ * @throws {Error} Not yet implemented
62
+ */
63
+ export function unloadEnhancement(): Promise<void> {
64
+ throw new Error(
65
+ 'Speech Enhancement feature is not yet implemented. This is a placeholder module.'
66
+ );
67
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,30 @@
1
+ import SherpaOnnx from './NativeSherpaOnnx';
2
+
3
+ // Export types and utilities
4
+ export type { InitializeOptions, ModelPathConfig, ModelType } from './types';
5
+ export {
6
+ assetModelPath,
7
+ autoModelPath,
8
+ fileModelPath,
9
+ getDefaultModelPath,
10
+ resolveModelPath,
11
+ } from './utils';
12
+
13
+ // Re-export STT functionality
14
+ export { initializeSTT, transcribeFile, unloadSTT } from './stt';
15
+ export type { STTInitializeOptions, TranscriptionResult } from './stt';
16
+
17
+ // TODO: Uncomment these exports once the features are implemented
18
+ // Re-export other features (when implemented)
19
+ // export * from './tts';
20
+ // export * from './vad';
21
+ // export * from './diarization';
22
+ // export * from './enhancement';
23
+ // export * from './separation';
24
+
25
+ /**
26
+ * Test method to verify sherpa-onnx native library is loaded.
27
+ */
28
+ export function testSherpaInit(): Promise<string> {
29
+ return SherpaOnnx.testSherpaInit();
30
+ }
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Source Separation feature module
3
+ *
4
+ * @remarks
5
+ * This feature is not yet implemented. This module serves as a placeholder
6
+ * for future source separation functionality.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * // Future usage:
11
+ * import { initializeSeparation, separateSources } from 'react-native-sherpa-onnx/separation';
12
+ *
13
+ * await initializeSeparation({ modelPath: 'models/separation-model' });
14
+ * const sources = await separateSources('path/to/mixed-audio.wav');
15
+ * ```
16
+ */
17
+
18
+ /**
19
+ * Separation initialization options (placeholder)
20
+ */
21
+ export interface SeparationInitializeOptions {
22
+ modelPath: string;
23
+ // Additional separation-specific options will be added here
24
+ }
25
+
26
+ /**
27
+ * Separated audio source
28
+ */
29
+ export interface SeparatedSource {
30
+ sourceId: string;
31
+ outputPath: string;
32
+ // Additional source fields will be added here
33
+ }
34
+
35
+ /**
36
+ * Initialize Source Separation with model directory.
37
+ *
38
+ * @throws {Error} Not yet implemented
39
+ */
40
+ export async function initializeSeparation(
41
+ _options: SeparationInitializeOptions
42
+ ): Promise<void> {
43
+ throw new Error(
44
+ 'Source Separation feature is not yet implemented. This is a placeholder module.'
45
+ );
46
+ }
47
+
48
+ /**
49
+ * Separate audio sources from a mixed audio file.
50
+ *
51
+ * @throws {Error} Not yet implemented
52
+ */
53
+ export function separateSources(_filePath: string): Promise<SeparatedSource[]> {
54
+ throw new Error(
55
+ 'Source Separation feature is not yet implemented. This is a placeholder module.'
56
+ );
57
+ }
58
+
59
+ /**
60
+ * Release separation resources.
61
+ *
62
+ * @throws {Error} Not yet implemented
63
+ */
64
+ export function unloadSeparation(): Promise<void> {
65
+ throw new Error(
66
+ 'Source Separation feature is not yet implemented. This is a placeholder module.'
67
+ );
68
+ }
@@ -0,0 +1,83 @@
1
+ import SherpaOnnx from '../NativeSherpaOnnx';
2
+ import type { STTInitializeOptions } from './types';
3
+ import type { InitializeOptions } from '../types';
4
+ import { resolveModelPath } from '../utils';
5
+
6
+ /**
7
+ * Initialize Speech-to-Text (STT) with model directory.
8
+ *
9
+ * Supports multiple model source types:
10
+ * - Asset models (bundled in app)
11
+ * - File system models (downloaded or user-provided)
12
+ * - Auto-detection (tries asset first, then file system)
13
+ *
14
+ * @param options - STT initialization options or model path configuration
15
+ * @example
16
+ * ```typescript
17
+ * // Simple string (auto-detect)
18
+ * await initializeSTT('models/sherpa-onnx-model');
19
+ *
20
+ * // Asset model
21
+ * await initializeSTT({
22
+ * modelPath: { type: 'asset', path: 'models/sherpa-onnx-model' }
23
+ * });
24
+ *
25
+ * // File system model with preferInt8 option
26
+ * await initializeSTT({
27
+ * modelPath: { type: 'file', path: '/path/to/model' },
28
+ * preferInt8: true // Prefer quantized int8 models (smaller, faster)
29
+ * });
30
+ *
31
+ * // With explicit model type
32
+ * await initializeSTT({
33
+ * modelPath: { type: 'asset', path: 'models/sherpa-onnx-nemo-parakeet-tdt-ctc-en' },
34
+ * modelType: 'nemo_ctc'
35
+ * });
36
+ * ```
37
+ */
38
+ export async function initializeSTT(
39
+ options: STTInitializeOptions | InitializeOptions['modelPath']
40
+ ): Promise<void> {
41
+ // Handle both object syntax and direct path syntax
42
+ let modelPath: InitializeOptions['modelPath'];
43
+ let preferInt8: boolean | undefined;
44
+ let modelType: string | undefined;
45
+
46
+ if (typeof options === 'object' && 'modelPath' in options) {
47
+ modelPath = options.modelPath;
48
+ preferInt8 = options.preferInt8;
49
+ modelType = options.modelType;
50
+ } else {
51
+ modelPath = options as InitializeOptions['modelPath'];
52
+ preferInt8 = undefined;
53
+ modelType = undefined;
54
+ }
55
+
56
+ const resolvedPath = await resolveModelPath(modelPath);
57
+ return SherpaOnnx.initializeSherpaOnnx(resolvedPath, preferInt8, modelType);
58
+ }
59
+
60
+ /**
61
+ * Transcribe an audio file.
62
+ *
63
+ * @param filePath - Path to WAV file (16kHz, mono, 16-bit PCM)
64
+ * @returns Promise resolving to transcribed text
65
+ * @example
66
+ * ```typescript
67
+ * const transcription = await transcribeFile('path/to/audio.wav');
68
+ * console.log('Transcription:', transcription);
69
+ * ```
70
+ */
71
+ export function transcribeFile(filePath: string): Promise<string> {
72
+ return SherpaOnnx.transcribeFile(filePath);
73
+ }
74
+
75
+ /**
76
+ * Release STT resources.
77
+ */
78
+ export function unloadSTT(): Promise<void> {
79
+ return SherpaOnnx.unloadSherpaOnnx();
80
+ }
81
+
82
+ // Export types
83
+ export type { STTInitializeOptions, TranscriptionResult } from './types';
@@ -0,0 +1,42 @@
1
+ import type { InitializeOptions, ModelType } from '../types';
2
+
3
+ /**
4
+ * STT-specific initialization options
5
+ */
6
+ export interface STTInitializeOptions {
7
+ /**
8
+ * Model directory path configuration
9
+ */
10
+ modelPath: InitializeOptions['modelPath'];
11
+
12
+ /**
13
+ * Model quantization preference
14
+ * - true: Prefer int8 quantized models (model.int8.onnx) - smaller, faster
15
+ * - false: Prefer regular models (model.onnx) - higher accuracy
16
+ * - undefined: Try int8 first, then fall back to regular (default behavior)
17
+ */
18
+ preferInt8?: boolean;
19
+
20
+ /**
21
+ * Explicit model type specification for STT models
22
+ * - 'transducer': Force detection as Zipformer/Transducer model
23
+ * - 'paraformer': Force detection as Paraformer model
24
+ * - 'nemo_ctc': Force detection as NeMo CTC model
25
+ * - 'whisper': Force detection as Whisper model
26
+ * - 'wenet_ctc': Force detection as WeNet CTC model
27
+ * - 'sense_voice': Force detection as SenseVoice model
28
+ * - 'funasr_nano': Force detection as FunASR Nano model
29
+ * - 'auto': Automatic detection based on files (default)
30
+ */
31
+ modelType?: ModelType;
32
+ }
33
+
34
+ /**
35
+ * Transcription result
36
+ */
37
+ export interface TranscriptionResult {
38
+ /**
39
+ * Transcribed text
40
+ */
41
+ text: string;
42
+ }
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Text-to-Speech (TTS) feature module
3
+ *
4
+ * @remarks
5
+ * This feature is not yet implemented. This module serves as a placeholder
6
+ * for future TTS functionality.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * // Future usage:
11
+ * import { initializeTTS, synthesizeText } from 'react-native-sherpa-onnx/tts';
12
+ *
13
+ * await initializeTTS({ modelPath: 'models/tts-model' });
14
+ * const audioPath = await synthesizeText('Hello, world!');
15
+ * ```
16
+ */
17
+
18
+ /**
19
+ * TTS initialization options (placeholder)
20
+ */
21
+ export interface TTSInitializeOptions {
22
+ modelPath: string;
23
+ // Additional TTS-specific options will be added here
24
+ }
25
+
26
+ /**
27
+ * TTS synthesis result (placeholder)
28
+ */
29
+ export interface SynthesisResult {
30
+ audioPath: string;
31
+ // Additional result fields will be added here
32
+ }
33
+
34
+ /**
35
+ * Initialize Text-to-Speech (TTS) with model directory.
36
+ *
37
+ * @throws {Error} Not yet implemented
38
+ */
39
+ export async function initializeTTS(
40
+ _options: TTSInitializeOptions
41
+ ): Promise<void> {
42
+ throw new Error(
43
+ 'TTS feature is not yet implemented. This is a placeholder module.'
44
+ );
45
+ }
46
+
47
+ /**
48
+ * Synthesize text to speech audio.
49
+ *
50
+ * @throws {Error} Not yet implemented
51
+ */
52
+ export function synthesizeText(_text: string): Promise<string> {
53
+ throw new Error(
54
+ 'TTS feature is not yet implemented. This is a placeholder module.'
55
+ );
56
+ }
57
+
58
+ /**
59
+ * Release TTS resources.
60
+ *
61
+ * @throws {Error} Not yet implemented
62
+ */
63
+ export function unloadTTS(): Promise<void> {
64
+ throw new Error(
65
+ 'TTS feature is not yet implemented. This is a placeholder module.'
66
+ );
67
+ }
package/src/types.ts ADDED
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Model path configuration options
3
+ */
4
+ export type ModelPathConfig =
5
+ | {
6
+ /**
7
+ * Path type: 'asset' - Model is bundled in app assets
8
+ * On iOS: Bundle path (e.g., "models/sherpa-onnx-model")
9
+ * On Android: Asset path (e.g., "models/sherpa-onnx-model")
10
+ */
11
+ type: 'asset';
12
+ path: string;
13
+ }
14
+ | {
15
+ /**
16
+ * Path type: 'file' - Model is in file system
17
+ * Absolute path to model directory
18
+ */
19
+ type: 'file';
20
+ path: string;
21
+ }
22
+ | {
23
+ /**
24
+ * Path type: 'auto' - Automatically detect path type
25
+ * Tries asset first, then file system
26
+ */
27
+ type: 'auto';
28
+ path: string;
29
+ };
30
+
31
+ /**
32
+ * Model type for explicit model detection
33
+ */
34
+ export type ModelType =
35
+ | 'transducer'
36
+ | 'paraformer'
37
+ | 'nemo_ctc'
38
+ | 'whisper'
39
+ | 'wenet_ctc'
40
+ | 'sense_voice'
41
+ | 'funasr_nano'
42
+ | 'auto';
43
+
44
+ /**
45
+ * Model initialization options
46
+ */
47
+ export interface InitializeOptions {
48
+ /**
49
+ * Model directory path configuration
50
+ */
51
+ modelPath: ModelPathConfig | string; // string for backward compatibility
52
+
53
+ /**
54
+ * Model quantization preference
55
+ * - true: Prefer int8 quantized models (model.int8.onnx) - smaller, faster
56
+ * - false: Prefer regular models (model.onnx) - higher accuracy
57
+ * - undefined: Try int8 first, then fall back to regular (default behavior)
58
+ */
59
+ preferInt8?: boolean;
60
+
61
+ /**
62
+ * Explicit model type specification
63
+ * - 'transducer': Force detection as Zipformer/Transducer model
64
+ * - 'paraformer': Force detection as Paraformer model
65
+ * - 'nemo_ctc': Force detection as NeMo CTC model
66
+ * - 'whisper': Force detection as Whisper model
67
+ * - 'wenet_ctc': Force detection as WeNet CTC model
68
+ * - 'sense_voice': Force detection as SenseVoice model
69
+ * - 'funasr_nano': Force detection as FunASR Nano model
70
+ * - 'auto': Automatic detection based on files (default)
71
+ */
72
+ modelType?: ModelType;
73
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,97 @@
1
+ import { Platform } from 'react-native';
2
+ import type { ModelPathConfig } from './types';
3
+ import SherpaOnnx from './NativeSherpaOnnx';
4
+
5
+ /**
6
+ * Utility functions for model path handling
7
+ */
8
+
9
+ /**
10
+ * Predefined model identifiers
11
+ */
12
+ export const MODELS = {
13
+ ZIPFORMER_EN: 'sherpa-onnx-zipformer-small-en',
14
+ PARAFORMER_ZH: 'sherpa-onnx-paraformer-zh-small',
15
+ } as const;
16
+
17
+ export type ModelId = (typeof MODELS)[keyof typeof MODELS];
18
+
19
+ /**
20
+ * Get the default model directory path for the current platform.
21
+ * This is a helper for apps that want to use a standard location.
22
+ *
23
+ * @returns Platform-specific default path
24
+ */
25
+ export function getDefaultModelPath(): string {
26
+ if (Platform.OS === 'ios') {
27
+ // iOS: Documents directory
28
+ return 'Documents/models';
29
+ } else {
30
+ // Android: Internal storage
31
+ return 'models';
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Create a model path configuration for asset models.
37
+ * Use this when models are bundled in your app's assets.
38
+ *
39
+ * @param assetPath - Path relative to assets (e.g., "models/sherpa-onnx-model")
40
+ * @returns Model path configuration
41
+ */
42
+ export function assetModelPath(assetPath: string): ModelPathConfig {
43
+ return {
44
+ type: 'asset',
45
+ path: assetPath,
46
+ };
47
+ }
48
+
49
+ /**
50
+ * Create a model path configuration for file system models.
51
+ * Use this when models are downloaded or stored in file system.
52
+ *
53
+ * @param filePath - Absolute path to model directory
54
+ * @returns Model path configuration
55
+ */
56
+ export function fileModelPath(filePath: string): ModelPathConfig {
57
+ return {
58
+ type: 'file',
59
+ path: filePath,
60
+ };
61
+ }
62
+
63
+ /**
64
+ * Create a model path configuration with auto-detection.
65
+ * Tries asset first, then file system.
66
+ *
67
+ * @param path - Path to try (will be checked as both asset and file)
68
+ * @returns Model path configuration
69
+ */
70
+ export function autoModelPath(path: string): ModelPathConfig {
71
+ return {
72
+ type: 'auto',
73
+ path: path,
74
+ };
75
+ }
76
+
77
+ /**
78
+ * Resolve model path based on configuration.
79
+ * This handles different path types (asset, file, auto) and returns
80
+ * a platform-specific absolute path that can be used by native code.
81
+ *
82
+ * @param config - Model path configuration or simple string path
83
+ * @returns Promise resolving to absolute path usable by native code
84
+ */
85
+ export async function resolveModelPath(
86
+ config: ModelPathConfig | string
87
+ ): Promise<string> {
88
+ // Backward compatibility: if string is passed, treat as auto
89
+ if (typeof config === 'string') {
90
+ return SherpaOnnx.resolveModelPath({
91
+ type: 'auto',
92
+ path: config,
93
+ });
94
+ }
95
+
96
+ return SherpaOnnx.resolveModelPath(config);
97
+ }