react-native-sherpa-onnx 0.2.0 → 0.3.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 (175) hide show
  1. package/README.md +232 -236
  2. package/SherpaOnnx.podspec +68 -64
  3. package/android/build.gradle +182 -192
  4. package/android/codegen.gradle +57 -0
  5. package/android/prebuilt-download.gradle +428 -0
  6. package/android/prebuilt-versions.gradle +43 -0
  7. package/android/proguard-rules.pro +10 -0
  8. package/android/src/main/assets/testModels/add_mul_add.onnx +28 -0
  9. package/android/src/main/assets/testModels/nnapi_internal_uint8_support.onnx +0 -0
  10. package/android/src/main/assets/testModels/qnn_multi_ctx_embed.onnx +0 -0
  11. package/android/src/main/cpp/CMakeLists.txt +166 -129
  12. package/android/src/main/cpp/CMakePresets.json +54 -0
  13. package/android/src/main/cpp/crypto/sha256.cpp +174 -0
  14. package/android/src/main/cpp/crypto/sha256.h +16 -0
  15. package/android/src/main/cpp/jni/archive/sherpa-onnx-archive-helper.cpp +404 -0
  16. package/android/src/main/cpp/jni/archive/sherpa-onnx-archive-helper.h +56 -0
  17. package/android/src/main/cpp/jni/archive/sherpa-onnx-archive-jni.cpp +181 -0
  18. package/android/src/main/cpp/jni/audio/sherpa-onnx-audio-convert-jni.cpp +888 -0
  19. package/{ios → android/src/main/cpp/jni/model_detect}/sherpa-onnx-common.h +18 -18
  20. package/android/src/main/cpp/jni/model_detect/sherpa-onnx-detect-jni-common.cpp +86 -0
  21. package/android/src/main/cpp/jni/model_detect/sherpa-onnx-detect-jni-common.h +20 -0
  22. package/android/src/main/cpp/jni/model_detect/sherpa-onnx-model-detect-helper.cpp +423 -0
  23. package/android/src/main/cpp/jni/model_detect/sherpa-onnx-model-detect-helper.h +55 -0
  24. package/android/src/main/cpp/jni/model_detect/sherpa-onnx-model-detect-stt.cpp +399 -0
  25. package/android/src/main/cpp/jni/model_detect/sherpa-onnx-model-detect-tts.cpp +238 -0
  26. package/{ios → android/src/main/cpp/jni/model_detect}/sherpa-onnx-model-detect.h +122 -89
  27. package/android/src/main/cpp/jni/model_detect/sherpa-onnx-stt-wrapper.cpp +99 -0
  28. package/android/src/main/cpp/jni/model_detect/sherpa-onnx-stt-wrapper.h +16 -0
  29. package/android/src/main/cpp/jni/model_detect/sherpa-onnx-tts-wrapper.cpp +78 -0
  30. package/android/src/main/cpp/jni/model_detect/sherpa-onnx-tts-wrapper.h +16 -0
  31. package/android/src/main/cpp/jni/module/sherpa-onnx-module-jni.cpp +190 -0
  32. package/android/src/main/cpp/jni/tts/sherpa-onnx-tts-zipvoice-jni.cpp +301 -0
  33. package/android/src/main/java/com/sherpaonnx/SherpaOnnxArchiveHelper.kt +94 -0
  34. package/android/src/main/java/com/sherpaonnx/{SherpaOnnxCoreHelper.kt → SherpaOnnxAssetHelper.kt} +350 -236
  35. package/android/src/main/java/com/sherpaonnx/SherpaOnnxModule.kt +791 -483
  36. package/android/src/main/java/com/sherpaonnx/SherpaOnnxSttHelper.kt +699 -109
  37. package/android/src/main/java/com/sherpaonnx/SherpaOnnxTtsHelper.kt +1123 -668
  38. package/android/src/main/java/com/sherpaonnx/ZipvoiceTtsWrapper.kt +187 -0
  39. package/ios/SherpaOnnx+Assets.h +11 -0
  40. package/ios/SherpaOnnx+Assets.mm +325 -0
  41. package/ios/SherpaOnnx+STT.mm +455 -118
  42. package/ios/SherpaOnnx+TTS.mm +1101 -712
  43. package/ios/SherpaOnnx.h +17 -6
  44. package/ios/SherpaOnnx.mm +206 -311
  45. package/ios/SherpaOnnx.xcconfig +19 -19
  46. package/ios/SherpaOnnxCoreMLHelper.swift +24 -0
  47. package/ios/archive/sherpa-onnx-archive-helper.h +21 -0
  48. package/ios/archive/sherpa-onnx-archive-helper.mm +296 -0
  49. package/ios/libarchive_darwin_config.h +153 -0
  50. package/{android/src/main/cpp/jni → ios/model_detect}/sherpa-onnx-common.h +18 -18
  51. package/ios/model_detect/sherpa-onnx-model-detect-helper.h +49 -0
  52. package/ios/model_detect/sherpa-onnx-model-detect-helper.mm +210 -0
  53. package/ios/model_detect/sherpa-onnx-model-detect-stt.mm +344 -0
  54. package/ios/model_detect/sherpa-onnx-model-detect-tts.mm +201 -0
  55. package/{android/src/main/cpp/jni → ios/model_detect}/sherpa-onnx-model-detect.h +117 -89
  56. package/ios/scripts/patch-libarchive-includes.sh +61 -0
  57. package/ios/scripts/setup-ios-libarchive.sh +98 -0
  58. package/ios/stt/sherpa-onnx-stt-wrapper.h +129 -0
  59. package/ios/stt/sherpa-onnx-stt-wrapper.mm +523 -0
  60. package/ios/{sherpa-onnx-tts-wrapper.h → tts/sherpa-onnx-tts-wrapper.h} +90 -85
  61. package/ios/{sherpa-onnx-tts-wrapper.mm → tts/sherpa-onnx-tts-wrapper.mm} +376 -345
  62. package/lib/module/NativeSherpaOnnx.js +3 -0
  63. package/lib/module/NativeSherpaOnnx.js.map +1 -1
  64. package/lib/module/audio/index.js +22 -0
  65. package/lib/module/audio/index.js.map +1 -0
  66. package/lib/module/diarization/index.js +1 -1
  67. package/lib/module/diarization/index.js.map +1 -1
  68. package/lib/module/download/ModelDownloadManager.js +918 -0
  69. package/lib/module/download/ModelDownloadManager.js.map +1 -0
  70. package/lib/module/download/extractTarBz2.js +53 -0
  71. package/lib/module/download/extractTarBz2.js.map +1 -0
  72. package/lib/module/download/index.js +6 -0
  73. package/lib/module/download/index.js.map +1 -0
  74. package/lib/module/download/validation.js +178 -0
  75. package/lib/module/download/validation.js.map +1 -0
  76. package/lib/module/enhancement/index.js +1 -1
  77. package/lib/module/enhancement/index.js.map +1 -1
  78. package/lib/module/index.js +41 -3
  79. package/lib/module/index.js.map +1 -1
  80. package/lib/module/separation/index.js +1 -1
  81. package/lib/module/separation/index.js.map +1 -1
  82. package/lib/module/stt/index.js +127 -60
  83. package/lib/module/stt/index.js.map +1 -1
  84. package/lib/module/stt/sttModelLanguages.js +512 -0
  85. package/lib/module/stt/sttModelLanguages.js.map +1 -0
  86. package/lib/module/stt/types.js +53 -1
  87. package/lib/module/stt/types.js.map +1 -1
  88. package/lib/module/tts/index.js +216 -289
  89. package/lib/module/tts/index.js.map +1 -1
  90. package/lib/module/tts/types.js +86 -1
  91. package/lib/module/tts/types.js.map +1 -1
  92. package/lib/module/types.js.map +1 -1
  93. package/lib/module/utils.js +86 -73
  94. package/lib/module/utils.js.map +1 -1
  95. package/lib/module/vad/index.js +1 -1
  96. package/lib/module/vad/index.js.map +1 -1
  97. package/lib/typescript/src/NativeSherpaOnnx.d.ts +192 -38
  98. package/lib/typescript/src/NativeSherpaOnnx.d.ts.map +1 -1
  99. package/lib/typescript/src/audio/index.d.ts +13 -0
  100. package/lib/typescript/src/audio/index.d.ts.map +1 -0
  101. package/lib/typescript/src/diarization/index.d.ts +3 -2
  102. package/lib/typescript/src/diarization/index.d.ts.map +1 -1
  103. package/lib/typescript/src/download/ModelDownloadManager.d.ts +108 -0
  104. package/lib/typescript/src/download/ModelDownloadManager.d.ts.map +1 -0
  105. package/lib/typescript/src/download/extractTarBz2.d.ts +14 -0
  106. package/lib/typescript/src/download/extractTarBz2.d.ts.map +1 -0
  107. package/lib/typescript/src/download/index.d.ts +7 -0
  108. package/lib/typescript/src/download/index.d.ts.map +1 -0
  109. package/lib/typescript/src/download/validation.d.ts +57 -0
  110. package/lib/typescript/src/download/validation.d.ts.map +1 -0
  111. package/lib/typescript/src/enhancement/index.d.ts +3 -2
  112. package/lib/typescript/src/enhancement/index.d.ts.map +1 -1
  113. package/lib/typescript/src/index.d.ts +26 -2
  114. package/lib/typescript/src/index.d.ts.map +1 -1
  115. package/lib/typescript/src/separation/index.d.ts +3 -2
  116. package/lib/typescript/src/separation/index.d.ts.map +1 -1
  117. package/lib/typescript/src/stt/index.d.ts +31 -43
  118. package/lib/typescript/src/stt/index.d.ts.map +1 -1
  119. package/lib/typescript/src/stt/sttModelLanguages.d.ts +52 -0
  120. package/lib/typescript/src/stt/sttModelLanguages.d.ts.map +1 -0
  121. package/lib/typescript/src/stt/types.d.ts +196 -9
  122. package/lib/typescript/src/stt/types.d.ts.map +1 -1
  123. package/lib/typescript/src/tts/index.d.ts +25 -211
  124. package/lib/typescript/src/tts/index.d.ts.map +1 -1
  125. package/lib/typescript/src/tts/types.d.ts +148 -25
  126. package/lib/typescript/src/tts/types.d.ts.map +1 -1
  127. package/lib/typescript/src/types.d.ts +0 -32
  128. package/lib/typescript/src/types.d.ts.map +1 -1
  129. package/lib/typescript/src/utils.d.ts +28 -13
  130. package/lib/typescript/src/utils.d.ts.map +1 -1
  131. package/lib/typescript/src/vad/index.d.ts +3 -2
  132. package/lib/typescript/src/vad/index.d.ts.map +1 -1
  133. package/package.json +250 -222
  134. package/scripts/check-qnn-support.sh +78 -0
  135. package/scripts/setup-ios-framework.sh +379 -282
  136. package/src/NativeSherpaOnnx.ts +474 -251
  137. package/src/audio/index.ts +32 -0
  138. package/src/diarization/index.ts +4 -2
  139. package/src/download/ModelDownloadManager.ts +1325 -0
  140. package/src/download/extractTarBz2.ts +78 -0
  141. package/src/download/index.ts +43 -0
  142. package/src/download/validation.ts +279 -0
  143. package/src/enhancement/index.ts +4 -2
  144. package/src/index.tsx +78 -27
  145. package/src/separation/index.ts +4 -2
  146. package/src/stt/index.ts +249 -89
  147. package/src/stt/sttModelLanguages.ts +237 -0
  148. package/src/stt/types.ts +263 -9
  149. package/src/tts/index.ts +470 -458
  150. package/src/tts/types.ts +373 -218
  151. package/src/types.ts +0 -44
  152. package/src/utils.ts +145 -131
  153. package/src/vad/index.ts +4 -2
  154. package/third_party/ffmpeg_prebuilt/ANDROID_RELEASE_TAG +1 -0
  155. package/third_party/libarchive_prebuilt/ANDROID_RELEASE_TAG +1 -0
  156. package/third_party/libarchive_prebuilt/IOS_RELEASE_TAG +1 -0
  157. package/third_party/sherpa-onnx-prebuilt/ANDROID_RELEASE_TAG +1 -0
  158. package/third_party/sherpa-onnx-prebuilt/IOS_RELEASE_TAG +1 -0
  159. package/android/src/main/cpp/include/sherpa-onnx/c-api/c-api.h +0 -1918
  160. package/android/src/main/cpp/include/sherpa-onnx/c-api/cxx-api.h +0 -841
  161. package/android/src/main/cpp/jni/sherpa-onnx-model-detect.cpp +0 -541
  162. package/android/src/main/cpp/jni/sherpa-onnx-stt-jni.cpp +0 -336
  163. package/android/src/main/cpp/jni/sherpa-onnx-stt-wrapper.cpp +0 -222
  164. package/android/src/main/cpp/jni/sherpa-onnx-stt-wrapper.h +0 -68
  165. package/android/src/main/cpp/jni/sherpa-onnx-tts-jni.cpp +0 -823
  166. package/android/src/main/cpp/jni/sherpa-onnx-tts-wrapper.cpp +0 -387
  167. package/android/src/main/cpp/jni/sherpa-onnx-tts-wrapper.h +0 -147
  168. package/ios/Frameworks/sherpa_onnx.xcframework.zip +0 -0
  169. package/ios/include/sherpa-onnx/c-api/c-api.h +0 -1918
  170. package/ios/include/sherpa-onnx/c-api/cxx-api.h +0 -841
  171. package/ios/sherpa-onnx-model-detect.mm +0 -441
  172. package/ios/sherpa-onnx-stt-wrapper.h +0 -48
  173. package/ios/sherpa-onnx-stt-wrapper.mm +0 -201
  174. package/scripts/copy-headers.js +0 -184
  175. package/scripts/setup-assets.js +0 -323
@@ -1,184 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Script to copy sherpa-onnx header files to Android and iOS include directories.
5
- * This ensures that the headers are available when the package is published to npm,
6
- * even if the git submodule is not initialized.
7
- */
8
-
9
- const fs = require('fs');
10
- const path = require('path');
11
-
12
- const SOURCE_DIR = path.join(
13
- __dirname,
14
- '..',
15
- 'sherpa-onnx',
16
- 'sherpa-onnx',
17
- 'c-api'
18
- );
19
- const ANDROID_DEST_DIR = path.join(
20
- __dirname,
21
- '..',
22
- 'android',
23
- 'src',
24
- 'main',
25
- 'cpp',
26
- 'include',
27
- 'sherpa-onnx',
28
- 'c-api'
29
- );
30
- const IOS_DEST_DIR = path.join(
31
- __dirname,
32
- '..',
33
- 'ios',
34
- 'include',
35
- 'sherpa-onnx',
36
- 'c-api'
37
- );
38
-
39
- const HEADER_FILES = ['c-api.h', 'cxx-api.h'];
40
-
41
- // C++ API implementation file needed for iOS (compiles the C++ wrapper around C API)
42
- const CXX_IMPL_FILE = 'cxx-api.cc';
43
- const IOS_ROOT_DIR = path.join(__dirname, '..', 'ios');
44
-
45
- function ensureDirectoryExists(dirPath) {
46
- if (!fs.existsSync(dirPath)) {
47
- fs.mkdirSync(dirPath, { recursive: true });
48
- console.log(`Created directory: ${dirPath}`);
49
- }
50
- }
51
-
52
- function copyFile(source, destination) {
53
- try {
54
- fs.copyFileSync(source, destination);
55
- console.log(
56
- `Copied: ${path.basename(source)} -> ${path.relative(
57
- process.cwd(),
58
- destination
59
- )}`
60
- );
61
- return true;
62
- } catch (error) {
63
- console.error(`Failed to copy ${source}:`, error.message);
64
- return false;
65
- }
66
- }
67
-
68
- function areHeadersAlreadyPresent() {
69
- // Check if all critical headers are already in the destination directories
70
- const requiredFiles = [
71
- path.join(ANDROID_DEST_DIR, 'c-api.h'),
72
- path.join(IOS_DEST_DIR, 'c-api.h'),
73
- path.join(IOS_DEST_DIR, 'cxx-api.h'),
74
- ];
75
-
76
- return requiredFiles.every((file) => fs.existsSync(file));
77
- }
78
-
79
- function main() {
80
- console.log('Checking sherpa-onnx header files...\n');
81
-
82
- // Check if headers are already present (e.g., from npm package)
83
- if (areHeadersAlreadyPresent()) {
84
- console.log('Header files already present, skipping copy.');
85
- console.log(' Android: android/src/main/cpp/include/sherpa-onnx/c-api/');
86
- console.log(' iOS: ios/include/sherpa-onnx/c-api/');
87
- return;
88
- }
89
-
90
- console.log(
91
- 'Header files not found, attempting to copy from git submodule...\n'
92
- );
93
-
94
- // Check if source directory exists
95
- if (!fs.existsSync(SOURCE_DIR)) {
96
- console.error(`Error: Source directory not found: ${SOURCE_DIR}`);
97
- console.error('Possible causes:');
98
- console.error(' 1. Git submodule is not initialized');
99
- console.error(
100
- ' 2. You are using an npm package (headers should already be included)'
101
- );
102
- console.error('');
103
- console.error('Solutions:');
104
- console.error(
105
- ' • Initialize submodule: git submodule update --init --recursive'
106
- );
107
- console.error(
108
- ' • Check if headers exist in ios/include/ or android/.../include/'
109
- );
110
- process.exit(1);
111
- }
112
-
113
- // Ensure destination directories exist
114
- ensureDirectoryExists(ANDROID_DEST_DIR);
115
- ensureDirectoryExists(IOS_DEST_DIR);
116
-
117
- let successCount = 0;
118
- let failCount = 0;
119
-
120
- // Copy header files to Android
121
- console.log('Copying to Android...');
122
- for (const headerFile of HEADER_FILES) {
123
- const source = path.join(SOURCE_DIR, headerFile);
124
- const destination = path.join(ANDROID_DEST_DIR, headerFile);
125
-
126
- if (!fs.existsSync(source)) {
127
- console.error(`Source file not found: ${source}`);
128
- failCount++;
129
- continue;
130
- }
131
-
132
- if (copyFile(source, destination)) {
133
- successCount++;
134
- } else {
135
- failCount++;
136
- }
137
- }
138
-
139
- // Copy header files to iOS
140
- console.log('\nCopying to iOS...');
141
- for (const headerFile of HEADER_FILES) {
142
- const source = path.join(SOURCE_DIR, headerFile);
143
- const destination = path.join(IOS_DEST_DIR, headerFile);
144
-
145
- if (!fs.existsSync(source)) {
146
- console.error(`Source file not found: ${source}`);
147
- failCount++;
148
- continue;
149
- }
150
-
151
- if (copyFile(source, destination)) {
152
- successCount++;
153
- } else {
154
- failCount++;
155
- }
156
- }
157
-
158
- // Copy C++ API implementation to iOS root (needed for compilation)
159
- console.log('\nCopying C++ API implementation to iOS...');
160
- const cxxImplSource = path.join(SOURCE_DIR, CXX_IMPL_FILE);
161
- const cxxImplDest = path.join(IOS_ROOT_DIR, CXX_IMPL_FILE);
162
-
163
- if (!fs.existsSync(cxxImplSource)) {
164
- console.error(`Source file not found: ${cxxImplSource}`);
165
- failCount++;
166
- } else if (copyFile(cxxImplSource, cxxImplDest)) {
167
- successCount++;
168
- } else {
169
- failCount++;
170
- }
171
-
172
- console.log(`\n${'='.repeat(50)}`);
173
- console.log(
174
- `Summary: ${successCount} files copied successfully, ${failCount} failed`
175
- );
176
-
177
- if (failCount > 0) {
178
- process.exit(1);
179
- } else {
180
- console.log('All header files copied successfully!');
181
- }
182
- }
183
-
184
- main();
@@ -1,323 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- const fs = require('fs');
4
- const path = require('path');
5
- const { execSync } = require('child_process');
6
-
7
- // platform detection not needed currently
8
-
9
- /**
10
- * Setup script that runs after npm/yarn install
11
- * Handles:
12
- * 1. Copying C++ header files from sherpa-onnx submodule
13
- * 2. Downloading iOS framework from GitHub releases
14
- */
15
-
16
- function log(message, type = 'info') {
17
- const colors = {
18
- info: '\x1b[36m', // cyan
19
- success: '\x1b[32m', // green
20
- warning: '\x1b[33m', // yellow
21
- error: '\x1b[31m', // red
22
- reset: '\x1b[0m',
23
- };
24
-
25
- const color = colors[type] || colors.info;
26
- console.log(`${color}[setup-assets] ${message}${colors.reset}`);
27
- }
28
-
29
- /**
30
- * Run a command and return output
31
- */
32
- function runCommand(cmd, options = {}) {
33
- const { silent = false, allowFailure = false } = options;
34
-
35
- try {
36
- if (!silent) {
37
- log(`Running: ${cmd}`);
38
- }
39
-
40
- const output = execSync(cmd, {
41
- encoding: 'utf-8',
42
- stdio: silent ? 'pipe' : 'inherit',
43
- });
44
-
45
- return { ok: true, output: (output || '').trim() };
46
- } catch (error) {
47
- if (!silent) {
48
- log(
49
- `${allowFailure ? 'Warning' : 'Failed'}: ${cmd}`,
50
- allowFailure ? 'warning' : 'error'
51
- );
52
- }
53
-
54
- if (!allowFailure) {
55
- throw error;
56
- }
57
-
58
- return { ok: false, output: '' };
59
- }
60
- }
61
-
62
- /**
63
- * Verify header files are in the correct locations
64
- */
65
- function verifyHeaders() {
66
- log('Verifying header files...', 'info');
67
-
68
- const requiredHeaders = [
69
- path.join(
70
- __dirname,
71
- '..',
72
- 'ios',
73
- 'include',
74
- 'sherpa-onnx',
75
- 'c-api',
76
- 'c-api.h'
77
- ),
78
- path.join(
79
- __dirname,
80
- '..',
81
- 'ios',
82
- 'include',
83
- 'sherpa-onnx',
84
- 'c-api',
85
- 'cxx-api.h'
86
- ),
87
- path.join(
88
- __dirname,
89
- '..',
90
- 'android',
91
- 'src',
92
- 'main',
93
- 'cpp',
94
- 'include',
95
- 'sherpa-onnx',
96
- 'c-api',
97
- 'c-api.h'
98
- ),
99
- path.join(
100
- __dirname,
101
- '..',
102
- 'android',
103
- 'src',
104
- 'main',
105
- 'cpp',
106
- 'include',
107
- 'sherpa-onnx',
108
- 'c-api',
109
- 'cxx-api.h'
110
- ),
111
- ];
112
-
113
- const missingHeaders = requiredHeaders.filter(
114
- (header) => !fs.existsSync(header)
115
- );
116
-
117
- if (missingHeaders.length === 0) {
118
- log('All header files verified at correct locations', 'success');
119
- return true;
120
- } else {
121
- log(
122
- `Missing header files found (${missingHeaders.length}/${requiredHeaders.length}):`,
123
- 'error'
124
- );
125
- missingHeaders.forEach((header) => {
126
- log(` - ${header}`, 'error');
127
- });
128
- return false;
129
- }
130
- }
131
-
132
- /**
133
- * Step 1: Copy header files
134
- */
135
- function copyHeaders() {
136
- log('Step 1: Copying header files...', 'info');
137
-
138
- try {
139
- // Call the script directly to avoid depending on yarn availability in consumers
140
- const copyResult = runCommand('node scripts/copy-headers.js', {
141
- allowFailure: true,
142
- });
143
-
144
- if (copyResult.ok) {
145
- log('Header files copied successfully', 'success');
146
- } else {
147
- log(
148
- 'Warning: Header copy command reported a failure; verifying files...',
149
- 'warning'
150
- );
151
- }
152
-
153
- // Verify headers after copy
154
- console.log('');
155
- const headersOk = verifyHeaders();
156
- if (!headersOk) {
157
- log('Header verification failed after copy', 'error');
158
- }
159
- return headersOk;
160
- } catch {
161
- log('Warning: Header copy failed, may already exist', 'warning');
162
- // Still try to verify if headers exist
163
- console.log('');
164
- return verifyHeaders();
165
- }
166
- }
167
-
168
- /**
169
- * Verify iOS framework is downloaded and extracted correctly
170
- */
171
- function verifyIOSFramework() {
172
- log('Verifying iOS framework...', 'info');
173
-
174
- const frameworkPath = path.join(
175
- __dirname,
176
- '..',
177
- 'ios',
178
- 'Frameworks',
179
- 'sherpa_onnx.xcframework'
180
- );
181
-
182
- // Check if framework directory exists
183
- if (!fs.existsSync(frameworkPath)) {
184
- log(`Framework directory not found at ${frameworkPath}`, 'error');
185
- return false;
186
- }
187
-
188
- log(`Framework directory exists`, 'success');
189
-
190
- // Check for required framework files
191
- const requiredFiles = [
192
- path.join(frameworkPath, 'Info.plist'),
193
- path.join(frameworkPath, 'ios-arm64', 'libsherpa-onnx.a'),
194
- path.join(frameworkPath, 'ios-arm64_x86_64-simulator', 'libsherpa-onnx.a'),
195
- ];
196
-
197
- const missingFiles = requiredFiles.filter((file) => !fs.existsSync(file));
198
-
199
- if (missingFiles.length === 0) {
200
- // Calculate framework size
201
- let totalSize = 0;
202
- const getDirectorySize = (dirPath) => {
203
- try {
204
- const files = fs.readdirSync(dirPath);
205
- files.forEach((file) => {
206
- const filePath = path.join(dirPath, file);
207
- const stats = fs.statSync(filePath);
208
- if (stats.isDirectory()) {
209
- totalSize += getDirectorySize(filePath);
210
- } else {
211
- totalSize += stats.size;
212
- }
213
- });
214
- } catch {
215
- // Silently ignore errors
216
- }
217
- return totalSize;
218
- };
219
-
220
- getDirectorySize(frameworkPath);
221
- const frameworkSizeMB = (totalSize / (1024 * 1024)).toFixed(2);
222
-
223
- log(`All framework files present (${frameworkSizeMB}MB)`, 'success');
224
- return true;
225
- } else {
226
- log(
227
- `Missing framework files (${missingFiles.length}/${requiredFiles.length}):`,
228
- 'error'
229
- );
230
- missingFiles.forEach((file) => {
231
- log(` - ${file}`, 'error');
232
- });
233
- return false;
234
- }
235
- }
236
-
237
- /**
238
- * Step 2: Download iOS framework (only on macOS)
239
- */
240
- function downloadIOSFramework() {
241
- // Only download on macOS since it's iOS-specific
242
- if (process.platform !== 'darwin') {
243
- log('Skipping iOS framework download (not on macOS)', 'info');
244
- return true;
245
- }
246
-
247
- log('Step 2: Downloading iOS framework...', 'info');
248
-
249
- const frameworkPath = path.join(
250
- __dirname,
251
- '..',
252
- 'ios',
253
- 'Frameworks',
254
- 'sherpa_onnx.xcframework'
255
- );
256
-
257
- // Check if framework already exists
258
- if (fs.existsSync(frameworkPath)) {
259
- log('iOS framework already present, verifying...', 'success');
260
- console.log('');
261
- return verifyIOSFramework();
262
- }
263
-
264
- try {
265
- // Call the shell script directly to avoid yarn dependency in consuming apps
266
- const downloadResult = runCommand('bash scripts/setup-ios-framework.sh');
267
-
268
- if (downloadResult.ok) {
269
- log('iOS framework downloaded successfully', 'success');
270
-
271
- // Verify framework after download
272
- console.log('');
273
- return verifyIOSFramework();
274
- }
275
-
276
- log('iOS framework download command reported failure', 'error');
277
- return false;
278
- } catch {
279
- log('iOS framework download failed', 'error');
280
- return false;
281
- }
282
- }
283
-
284
- /**
285
- * Main setup function
286
- */
287
- function setup() {
288
- console.log('');
289
- log('='.repeat(60), 'info');
290
- log('SherpaOnnx SDK Setup Starting', 'info');
291
- log('='.repeat(60), 'info');
292
- console.log('');
293
-
294
- let success = true;
295
-
296
- // Step 1: Copy headers (critical for both Android and iOS)
297
- if (!copyHeaders()) {
298
- success = false;
299
- }
300
-
301
- console.log('');
302
-
303
- // Step 2: Download iOS framework (macOS only)
304
- if (!downloadIOSFramework()) {
305
- success = false;
306
- }
307
-
308
- console.log('');
309
- if (success) {
310
- log('='.repeat(60), 'success');
311
- log('Setup completed successfully!', 'success');
312
- log('='.repeat(60), 'success');
313
- console.log('');
314
- process.exit(0);
315
- } else {
316
- log('Setup failed (see logs above)', 'error');
317
- console.log('');
318
- process.exit(1);
319
- }
320
- }
321
-
322
- // Run setup
323
- setup();