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,282 +1,379 @@
1
- #!/bin/bash
2
-
3
- # Script to download and manage iOS Framework
4
- # Can be called manually or by Podfile during pod install
5
- # Usage:
6
- # ./scripts/setup-ios-framework.sh # Downloads/updates framework (auto mode, no interactive)
7
- # ./scripts/setup-ios-framework.sh 1.12.23 # Downloads specific version
8
- # ./scripts/setup-ios-framework.sh --interactive # Interactive mode with prompts
9
-
10
- set -e
11
-
12
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
13
- PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
14
- FRAMEWORKS_DIR="$PROJECT_ROOT/ios/Frameworks"
15
- VERSION_FILE="$FRAMEWORKS_DIR/.framework-version"
16
-
17
- # Colors for output
18
- RED='\033[0;31m'
19
- GREEN='\033[0;32m'
20
- YELLOW='\033[1;33m'
21
- BLUE='\033[0;34m'
22
- NC='\033[0m' # No Color
23
-
24
- # Detect if running in interactive mode (terminal)
25
- INTERACTIVE=false
26
- [ -t 0 ] && INTERACTIVE=true
27
-
28
- # Check for explicit flags
29
- if [ "$1" = "--interactive" ]; then
30
- INTERACTIVE=true
31
- shift
32
- fi
33
-
34
- # Only print header if interactive
35
- if [ "$INTERACTIVE" = true ]; then
36
- echo -e "${BLUE}iOS Framework Setup Script${NC}"
37
- echo "Project root: $PROJECT_ROOT"
38
- echo ""
39
- fi
40
-
41
- # Create frameworks directory if it doesn't exist
42
- mkdir -p "$FRAMEWORKS_DIR"
43
-
44
- # Function to compare semantic versions (e.g., "1.12.23" vs "1.12.24")
45
- compare_versions() {
46
- local v1=$1
47
- local v2=$2
48
-
49
- # Convert to arrays
50
- IFS='.' read -ra v1_parts <<< "$v1"
51
- IFS='.' read -ra v2_parts <<< "$v2"
52
-
53
- # Pad arrays to same length
54
- local max_len=${#v1_parts[@]}
55
- [ ${#v2_parts[@]} -gt $max_len ] && max_len=${#v2_parts[@]}
56
-
57
- # Compare each part
58
- for ((i=0; i<max_len; i++)); do
59
- local v1_num=${v1_parts[$i]:-0}
60
- local v2_num=${v2_parts[$i]:-0}
61
-
62
- if [ "$v1_num" -lt "$v2_num" ]; then
63
- echo "-1" # v1 < v2
64
- return 0
65
- elif [ "$v1_num" -gt "$v2_num" ]; then
66
- echo "1" # v1 > v2
67
- return 0
68
- fi
69
- done
70
-
71
- echo "0" # v1 == v2
72
- }
73
-
74
- # Function to get latest framework version from GitHub
75
- get_latest_framework_version() {
76
- echo -e "${YELLOW}Fetching latest framework release from GitHub...${NC}" >&2
77
-
78
- local releases_json=$(curl -s -H "Accept: application/vnd.github+json" "https://api.github.com/repos/XDcobra/react-native-sherpa-onnx/releases" 2>/dev/null || echo "")
79
-
80
- if [ -z "$releases_json" ]; then
81
- echo -e "${RED}Error: Could not fetch releases from GitHub API${NC}" >&2
82
- return 1
83
- fi
84
-
85
- # Avoid jq errors on rate-limit HTML or plain-text responses
86
- if ! echo "$releases_json" | grep -q '"tag_name"'; then
87
- echo -e "${RED}Error: GitHub API response did not contain release data (possible rate limit).${NC}" >&2
88
- echo "Response (truncated):" >&2
89
- echo "$releases_json" | head -5 >&2
90
- return 1
91
- fi
92
-
93
- local version=""
94
-
95
- if command -v jq &> /dev/null; then
96
- if echo "$releases_json" | jq -e . > /dev/null 2>&1; then
97
- version=$(echo "$releases_json" | jq -r '.[] | select(.tag_name | startswith("framework-v")) | .tag_name' | head -1 | sed 's/framework-v//')
98
- else
99
- echo -e "${RED}Error: GitHub releases response is not valid JSON${NC}" >&2
100
- echo "$releases_json" | head -5 >&2
101
- return 1
102
- fi
103
- else
104
- version=$(echo "$releases_json" | grep -o '"tag_name": "framework-v[0-9.]*' | head -1 | sed 's/.*framework-v//')
105
- fi
106
-
107
- if [ -z "$version" ]; then
108
- echo -e "${RED}Error: No framework releases found with tag format 'framework-vX.Y.Z'${NC}" >&2
109
- return 1
110
- fi
111
-
112
- echo "$version"
113
- }
114
-
115
- # Function to get local framework version
116
- get_local_framework_version() {
117
- if [ -f "$VERSION_FILE" ]; then
118
- cat "$VERSION_FILE"
119
- else
120
- echo ""
121
- fi
122
- }
123
-
124
- # Function to download and extract framework
125
- download_and_extract_framework() {
126
- local version=$1
127
- local tag="framework-v$version"
128
-
129
- echo -e "${YELLOW}Downloading framework version $version...${NC}" >&2
130
-
131
- # Get download URL from GitHub API
132
- local release_json=$(curl -s -H "Accept: application/vnd.github+json" "https://api.github.com/repos/XDcobra/react-native-sherpa-onnx/releases/tags/$tag" 2>/dev/null || echo "")
133
-
134
- if [ -z "$release_json" ]; then
135
- echo -e "${RED}Error: Could not fetch release information for tag $tag${NC}" >&2
136
- return 1
137
- fi
138
-
139
- if ! echo "$release_json" | grep -q '"assets"'; then
140
- echo -e "${RED}Error: GitHub API response for $tag did not contain assets (possible rate limit).${NC}" >&2
141
- echo "Response (truncated):" >&2
142
- echo "$release_json" | head -5 >&2
143
- return 1
144
- fi
145
-
146
- # Extract download URL using jq if available, otherwise grep/sed
147
- local download_url
148
- if command -v jq &> /dev/null; then
149
- if echo "$release_json" | jq -e . > /dev/null 2>&1; then
150
- download_url=$(echo "$release_json" | jq -r '.assets[] | select(.name == "sherpa_onnx.xcframework.zip") | .browser_download_url' | head -1)
151
- else
152
- echo -e "${RED}Error: Release response is not valid JSON${NC}" >&2
153
- echo "$release_json" | head -5 >&2
154
- return 1
155
- fi
156
- else
157
- download_url=$(echo "$release_json" | grep -o '"browser_download_url": "[^"]*' | grep 'xcframework.zip' | head -1 | sed 's/.*: "//' | sed 's/"$//')
158
- fi
159
-
160
- if [ -z "$download_url" ]; then
161
- echo -e "${RED}Error: Could not find download URL for version $version${NC}" >&2
162
- echo -e "${RED}Available assets:${NC}" >&2
163
- if command -v jq &> /dev/null; then
164
- echo "$release_json" | jq -r '.assets[].name' | sed 's/^/ - /' >&2 || true
165
- fi
166
- return 1
167
- fi
168
-
169
- echo "Downloading from: $download_url" >&2
170
-
171
- # Download the zip file
172
- local zip_path="$FRAMEWORKS_DIR/sherpa_onnx.xcframework.zip"
173
-
174
- if ! curl -L -f -o "$zip_path" "$download_url" 2>/dev/null; then
175
- echo -e "${RED}Error: Failed to download framework from $download_url${NC}" >&2
176
- rm -f "$zip_path"
177
- return 1
178
- fi
179
-
180
- # Check if zip file is valid
181
- if ! file "$zip_path" 2>/dev/null | grep -q "Zip archive"; then
182
- echo -e "${RED}Error: Downloaded file is not a valid zip archive${NC}" >&2
183
- echo "File type: $(file "$zip_path" 2>/dev/null || echo "unknown")" >&2
184
- rm -f "$zip_path"
185
- return 1
186
- fi
187
-
188
- # Remove old framework if it exists
189
- if [ -d "$FRAMEWORKS_DIR/sherpa_onnx.xcframework" ]; then
190
- echo -e "${YELLOW}Removing old framework...${NC}" >&2
191
- rm -rf "$FRAMEWORKS_DIR/sherpa_onnx.xcframework"
192
- fi
193
-
194
- # Extract the zip
195
- echo -e "${YELLOW}Extracting framework...${NC}" >&2
196
- unzip -q -o "$zip_path" -d "$FRAMEWORKS_DIR"
197
-
198
- if [ ! -d "$FRAMEWORKS_DIR/sherpa_onnx.xcframework" ]; then
199
- echo -e "${RED}Error: Framework extraction failed${NC}" >&2
200
- echo "Contents of $FRAMEWORKS_DIR:" >&2
201
- ls -la "$FRAMEWORKS_DIR" 2>/dev/null | head -20 >&2 || true
202
- rm -f "$zip_path"
203
- return 1
204
- fi
205
-
206
- # Remove zip file
207
- rm -f "$zip_path"
208
-
209
- # Write version file
210
- echo "$version" > "$VERSION_FILE"
211
-
212
- echo -e "${GREEN}Framework v$version downloaded and extracted successfully${NC}" >&2
213
- return 0
214
- }
215
-
216
- # Main logic
217
- if [ -n "$1" ]; then
218
- # User provided a specific version
219
- download_and_extract_framework "$1"
220
- else
221
- # Auto mode: check version and download only if needed
222
- [ "$INTERACTIVE" = true ] && echo -e "${YELLOW}Checking framework version...${NC}" >&2
223
-
224
- local_version=$(get_local_framework_version)
225
-
226
- if ! latest_version=$(get_latest_framework_version); then
227
- if [ -d "$FRAMEWORKS_DIR/sherpa_onnx.xcframework" ]; then
228
- echo -e "${YELLOW}Warning: Could not fetch latest framework version, using existing local framework.${NC}" >&2
229
- exit 0
230
- fi
231
- exit 1
232
- fi
233
-
234
- if [ -z "$latest_version" ]; then
235
- echo -e "${RED}Error: Could not fetch framework version from GitHub${NC}" >&2
236
- exit 1
237
- fi
238
-
239
- [ "$INTERACTIVE" = true ] && echo -e "${GREEN}Latest framework version: $latest_version${NC}" >&2
240
-
241
- # Check if framework exists
242
- if [ ! -d "$FRAMEWORKS_DIR/sherpa_onnx.xcframework" ]; then
243
- [ "$INTERACTIVE" = true ] && echo "Framework not found locally, downloading..." >&2
244
- download_and_extract_framework "$latest_version" || exit 1
245
- elif [ -z "$local_version" ]; then
246
- # Framework exists but no version file
247
- [ "$INTERACTIVE" = true ] && echo "Framework exists but no version info, updating version file..." >&2
248
- echo "$latest_version" > "$VERSION_FILE"
249
- else
250
- # Compare versions
251
- local version_cmp=$(compare_versions "$local_version" "$latest_version")
252
-
253
- if [ "$version_cmp" = "0" ]; then
254
- [ "$INTERACTIVE" = true ] && echo -e "${GREEN}Framework is up to date (v$local_version)${NC}" >&2
255
- elif [ "$version_cmp" = "-1" ]; then
256
- [ "$INTERACTIVE" = true ] && echo -e "${YELLOW}Update available: v$local_version → v$latest_version${NC}" >&2
257
- if [ "$INTERACTIVE" = true ]; then
258
- read -p "Do you want to update? (y/N): " -n 1 -r
259
- echo
260
- if [[ $REPLY =~ ^[Yy]$ ]]; then
261
- download_and_extract_framework "$latest_version" || exit 1
262
- fi
263
- else
264
- # Auto-update in non-interactive mode (e.g., from Podfile)
265
- download_and_extract_framework "$latest_version" || exit 1
266
- fi
267
- else
268
- [ "$INTERACTIVE" = true ] && echo -e "${YELLOW}Local version (v$local_version) is newer than latest release (v$latest_version)${NC}" >&2
269
- fi
270
- fi
271
- fi
272
-
273
- if [ "$INTERACTIVE" = true ]; then
274
- echo "" >&2
275
- echo -e "${GREEN}Framework setup complete!${NC}" >&2
276
- echo "Framework location: $FRAMEWORKS_DIR/sherpa_onnx.xcframework" >&2
277
- echo "" >&2
278
- echo "Next steps:" >&2
279
- echo " 1. cd example" >&2
280
- echo " 2. pod install" >&2
281
- echo " 3. Open ios/SherpaOnnxExample.xcworkspace in Xcode" >&2
282
- fi
1
+ #!/bin/bash
2
+
3
+ # Script to download and manage iOS Framework
4
+ # Can be called manually or by Podfile during pod install
5
+ # Usage:
6
+ # ./scripts/setup-ios-framework.sh # Downloads/updates framework (auto mode, no interactive)
7
+ # ./scripts/setup-ios-framework.sh 1.12.24 # Downloads specific version
8
+ # ./scripts/setup-ios-framework.sh --force # Remove local cache and re-download (same version from IOS_RELEASE_TAG)
9
+ # ./scripts/setup-ios-framework.sh --interactive # Interactive mode with prompts
10
+ # To force re-download during pod install: SHERPA_ONNX_IOS_FORCE_DOWNLOAD=1 pod install
11
+
12
+ set -e
13
+
14
+ # Resolve package root: explicit (CI), then pod install, then script dir or PWD.
15
+ PROJECT_ROOT=""
16
+ if [ -n "${SHERPA_ONNX_PROJECT_ROOT}" ] && [ -d "${SHERPA_ONNX_PROJECT_ROOT}" ]; then
17
+ PROJECT_ROOT="${SHERPA_ONNX_PROJECT_ROOT}"
18
+ fi
19
+ if [ -z "$PROJECT_ROOT" ] && [ -n "${GITHUB_WORKSPACE}" ] && [ -d "${GITHUB_WORKSPACE}/ios" ]; then
20
+ PROJECT_ROOT="${GITHUB_WORKSPACE}"
21
+ fi
22
+ if [ -z "$PROJECT_ROOT" ] && [ -n "${PODS_TARGET_SRCROOT}" ] && [ -d "${PODS_TARGET_SRCROOT}" ]; then
23
+ PROJECT_ROOT="${PODS_TARGET_SRCROOT}"
24
+ fi
25
+ if [ -z "$PROJECT_ROOT" ]; then
26
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
27
+ if [ -d "$SCRIPT_DIR/../ios" ]; then
28
+ PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
29
+ fi
30
+ fi
31
+ if [ -z "$PROJECT_ROOT" ] && [ -d "$(pwd)/ios" ]; then
32
+ PROJECT_ROOT="$(pwd)"
33
+ fi
34
+ if [ -z "$PROJECT_ROOT" ]; then
35
+ echo "Error: Could not resolve project root. Run from package root or run 'pod install' from example/ios." >&2
36
+ exit 1
37
+ fi
38
+ FRAMEWORKS_DIR="$PROJECT_ROOT/ios/Frameworks"
39
+ VERSION_FILE="$FRAMEWORKS_DIR/.framework-version"
40
+
41
+ # Colors for output
42
+ RED='\033[0;31m'
43
+ GREEN='\033[0;32m'
44
+ YELLOW='\033[1;33m'
45
+ BLUE='\033[0;34m'
46
+ NC='\033[0m' # No Color
47
+
48
+ # Detect if running in interactive mode (terminal)
49
+ INTERACTIVE=false
50
+ [ -t 0 ] && INTERACTIVE=true
51
+
52
+ # Check for explicit flags
53
+ FORCE_DOWNLOAD=false
54
+ if [ "$1" = "--interactive" ]; then
55
+ INTERACTIVE=true
56
+ shift
57
+ fi
58
+ if [ "$1" = "--force" ]; then
59
+ FORCE_DOWNLOAD=true
60
+ shift
61
+ fi
62
+ if [ -n "$SHERPA_ONNX_IOS_FORCE_DOWNLOAD" ] && [ "$SHERPA_ONNX_IOS_FORCE_DOWNLOAD" != "0" ]; then
63
+ FORCE_DOWNLOAD=true
64
+ fi
65
+
66
+ # Only print header if interactive
67
+ if [ "$INTERACTIVE" = true ]; then
68
+ echo -e "${BLUE}iOS Framework Setup Script${NC}"
69
+ echo "Project root: $PROJECT_ROOT"
70
+ echo ""
71
+ fi
72
+
73
+ # Create frameworks directory if it doesn't exist
74
+ mkdir -p "$FRAMEWORKS_DIR"
75
+
76
+ # Helper: check if a framework path is valid for building (has library + required headers for compiler)
77
+ framework_valid() {
78
+ local fw_root="$1"
79
+ [ -f "$fw_root/ios-arm64/libsherpa-onnx.a" ] || return 1
80
+ [ -f "$fw_root/ios-arm64_x86_64-simulator/Headers/sherpa-onnx/c-api/cxx-api.h" ] || return 1
81
+ return 0
82
+ }
83
+
84
+ # When run as Xcode build phase (prepare_command): if framework is already present and valid, exit successfully.
85
+ # Avoids network/TAG-file dependency during build and prevents "PhaseScriptExecution failed" when nothing is needed.
86
+ # We require both the library and the Headers (sherpa-onnx/c-api/cxx-api.h) so an incomplete framework triggers a re-download.
87
+ if [ "$FORCE_DOWNLOAD" != true ]; then
88
+ if [ -d "$FRAMEWORKS_DIR/sherpa_onnx.xcframework" ] && framework_valid "$FRAMEWORKS_DIR/sherpa_onnx.xcframework"; then
89
+ echo "[SherpaOnnx] Framework already present at $FRAMEWORKS_DIR/sherpa_onnx.xcframework, skipping download." >&2
90
+ exit 0
91
+ fi
92
+ if [ -d "$FRAMEWORKS_DIR/sherpa-onnx.xcframework" ] && framework_valid "$FRAMEWORKS_DIR/sherpa-onnx.xcframework"; then
93
+ echo "[SherpaOnnx] Framework already present at $FRAMEWORKS_DIR/sherpa-onnx.xcframework, skipping download." >&2
94
+ exit 0
95
+ fi
96
+ fi
97
+
98
+ # Prepare GitHub auth header if GITHUB_TOKEN is provided (helps avoid API rate limits)
99
+ AUTH_ARGS=()
100
+ if [ -n "$GITHUB_TOKEN" ]; then
101
+ AUTH_ARGS+=("-H" "Authorization: Bearer $GITHUB_TOKEN")
102
+ fi
103
+
104
+ # If SHERPA_ONNX_VERSION is set, treat it as the desired framework version
105
+ # and do not perform the usual "auto-upgrade to latest" behavior. This helps
106
+ # prevent accidental upgrades during CI or automated installs.
107
+ if [ -n "$SHERPA_ONNX_VERSION" ]; then
108
+ DESIRED_VERSION="$SHERPA_ONNX_VERSION"
109
+ fi
110
+ # If no env var was provided, use repo-level IOS_RELEASE_TAG (single source of truth for iOS framework version).
111
+ # Format: framework-vX.Y.Z (e.g. framework-v1.12.24). Do not use ANDROID_RELEASE_TAG for iOS.
112
+ if [ -z "$DESIRED_VERSION" ]; then
113
+ IOS_TAG_FILE="$PROJECT_ROOT/third_party/sherpa-onnx-prebuilt/IOS_RELEASE_TAG"
114
+ if [ -f "$IOS_TAG_FILE" ]; then
115
+ TAG=$(grep -v '^#' "$IOS_TAG_FILE" | grep -v '^[[:space:]]*$' | head -1 | tr -d '\r\n')
116
+ if [ -n "$TAG" ] && [ "${TAG#framework-v}" != "$TAG" ]; then
117
+ DESIRED_VERSION="${TAG#framework-v}"
118
+ [ "$INTERACTIVE" = true ] && echo -e "${YELLOW}Using iOS framework version from IOS_RELEASE_TAG: $DESIRED_VERSION${NC}" >&2
119
+ fi
120
+ fi
121
+ if [ -z "$DESIRED_VERSION" ]; then
122
+ echo -e "${RED}Error: IOS_RELEASE_TAG not found at $IOS_TAG_FILE. Reinstall the package or run from repo.${NC}" >&2
123
+ exit 1
124
+ fi
125
+ fi
126
+
127
+ # Function to compare semantic versions (e.g., "1.12.23" vs "1.12.24")
128
+ compare_versions() {
129
+ local v1=$1
130
+ local v2=$2
131
+
132
+ # Convert to arrays
133
+ IFS='.' read -ra v1_parts <<< "$v1"
134
+ IFS='.' read -ra v2_parts <<< "$v2"
135
+
136
+ # Pad arrays to same length
137
+ local max_len=${#v1_parts[@]}
138
+ [ ${#v2_parts[@]} -gt $max_len ] && max_len=${#v2_parts[@]}
139
+
140
+ # Compare each part
141
+ for ((i=0; i<max_len; i++)); do
142
+ local v1_num=${v1_parts[$i]:-0}
143
+ local v2_num=${v2_parts[$i]:-0}
144
+
145
+ if [ "$v1_num" -lt "$v2_num" ]; then
146
+ echo "-1" # v1 < v2
147
+ return 0
148
+ elif [ "$v1_num" -gt "$v2_num" ]; then
149
+ echo "1" # v1 > v2
150
+ return 0
151
+ fi
152
+ done
153
+
154
+ echo "0" # v1 == v2
155
+ }
156
+
157
+ # Function to get latest framework version from GitHub
158
+ get_latest_framework_version() {
159
+ echo -e "${YELLOW}Fetching latest framework release from GitHub...${NC}" >&2
160
+
161
+ local releases_json=$(curl -s "${AUTH_ARGS[@]}" -H "Accept: application/vnd.github+json" "https://api.github.com/repos/XDcobra/react-native-sherpa-onnx/releases" 2>/dev/null || echo "")
162
+
163
+ if [ -z "$releases_json" ]; then
164
+ echo -e "${RED}Error: Could not fetch releases from GitHub API${NC}" >&2
165
+ return 1
166
+ fi
167
+
168
+ # Avoid jq errors on rate-limit HTML or plain-text responses
169
+ if ! echo "$releases_json" | grep -q '"tag_name"'; then
170
+ echo -e "${RED}Error: GitHub API response did not contain release data (possible rate limit).${NC}" >&2
171
+ echo "Response (truncated):" >&2
172
+ echo "$releases_json" | head -5 >&2
173
+ return 1
174
+ fi
175
+
176
+ local version=""
177
+
178
+ if command -v jq &> /dev/null; then
179
+ if echo "$releases_json" | jq -e . > /dev/null 2>&1; then
180
+ version=$(echo "$releases_json" | jq -r '.[] | select(.tag_name | startswith("framework-v")) | .tag_name' | head -1 | sed 's/framework-v//')
181
+ else
182
+ echo -e "${RED}Error: GitHub releases response is not valid JSON${NC}" >&2
183
+ echo "$releases_json" | head -5 >&2
184
+ return 1
185
+ fi
186
+ else
187
+ version=$(echo "$releases_json" | grep -o '"tag_name": "framework-v[0-9.]*' | head -1 | sed 's/.*framework-v//')
188
+ fi
189
+
190
+ if [ -z "$version" ]; then
191
+ echo -e "${RED}Error: No framework releases found with tag format 'framework-vX.Y.Z'${NC}" >&2
192
+ return 1
193
+ fi
194
+
195
+ echo "$version"
196
+ }
197
+
198
+ # Function to get local framework version
199
+ get_local_framework_version() {
200
+ # Prefer explicit version file written by this script
201
+ if [ -f "$VERSION_FILE" ]; then
202
+ cat "$VERSION_FILE"
203
+ return 0
204
+ fi
205
+
206
+ # If .framework-version missing, try to read VERSION.txt from the XCFramework
207
+ for f in "sherpa_onnx.xcframework" "sherpa-onnx.xcframework"; do
208
+ if [ -f "$FRAMEWORKS_DIR/$f/VERSION.txt" ]; then
209
+ # Extract first semantic version-like token (e.g. 1.12.24)
210
+ ver=$(grep -Eo '([0-9]+\.)+[0-9]+' "$FRAMEWORKS_DIR/$f/VERSION.txt" | head -n1 || true)
211
+ if [ -n "$ver" ]; then
212
+ # Cache it for future runs
213
+ echo "$ver" > "$VERSION_FILE" 2>/dev/null || true
214
+ echo "$ver"
215
+ return 0
216
+ fi
217
+ fi
218
+ done
219
+
220
+ echo ""
221
+ }
222
+
223
+ # Function to download and extract framework
224
+ download_and_extract_framework() {
225
+ local version=$1
226
+ local tag="framework-v$version"
227
+
228
+ echo -e "${YELLOW}Downloading framework version $version...${NC}" >&2
229
+
230
+ # Get download URL from GitHub API
231
+ local release_json=$(curl -s "${AUTH_ARGS[@]}" -H "Accept: application/vnd.github+json" "https://api.github.com/repos/XDcobra/react-native-sherpa-onnx/releases/tags/$tag" 2>/dev/null || echo "")
232
+
233
+ if [ -z "$release_json" ]; then
234
+ echo -e "${RED}Error: Could not fetch release information for tag $tag${NC}" >&2
235
+ return 1
236
+ fi
237
+
238
+ if ! echo "$release_json" | grep -q '"assets"'; then
239
+ echo -e "${RED}Error: GitHub API response for $tag did not contain assets (possible rate limit).${NC}" >&2
240
+ echo "Response (truncated):" >&2
241
+ echo "$release_json" | head -5 >&2
242
+ return 1
243
+ fi
244
+
245
+ # Extract download URL using jq if available, otherwise grep/sed
246
+ local download_url
247
+ if command -v jq &> /dev/null; then
248
+ if echo "$release_json" | jq -e . > /dev/null 2>&1; then
249
+ download_url=$(echo "$release_json" | jq -r '.assets[] | select(.name == "sherpa_onnx.xcframework.zip") | .browser_download_url' | head -1)
250
+ else
251
+ echo -e "${RED}Error: Release response is not valid JSON${NC}" >&2
252
+ echo "$release_json" | head -5 >&2
253
+ return 1
254
+ fi
255
+ else
256
+ download_url=$(echo "$release_json" | grep -o '"browser_download_url": "[^"]*' | grep 'xcframework.zip' | head -1 | sed 's/.*: "//' | sed 's/"$//')
257
+ fi
258
+
259
+ if [ -z "$download_url" ]; then
260
+ echo -e "${RED}Error: Could not find download URL for version $version${NC}" >&2
261
+ echo -e "${RED}Available assets:${NC}" >&2
262
+ if command -v jq &> /dev/null; then
263
+ echo "$release_json" | jq -r '.assets[].name' | sed 's/^/ - /' >&2 || true
264
+ fi
265
+ return 1
266
+ fi
267
+
268
+ echo "Downloading from: $download_url" >&2
269
+
270
+ # Download the zip file
271
+ local zip_path="$FRAMEWORKS_DIR/sherpa_onnx.xcframework.zip"
272
+
273
+ if ! curl -L -f "${AUTH_ARGS[@]}" -o "$zip_path" "$download_url" 2>/dev/null; then
274
+ echo -e "${RED}Error: Failed to download framework from $download_url${NC}" >&2
275
+ rm -f "$zip_path"
276
+ return 1
277
+ fi
278
+
279
+ # Check if zip file is valid
280
+ if ! file "$zip_path" 2>/dev/null | grep -q "Zip archive"; then
281
+ echo -e "${RED}Error: Downloaded file is not a valid zip archive${NC}" >&2
282
+ echo "File type: $(file "$zip_path" 2>/dev/null || echo "unknown")" >&2
283
+ rm -f "$zip_path"
284
+ return 1
285
+ fi
286
+
287
+ # Remove old framework if it exists
288
+ if [ -d "$FRAMEWORKS_DIR/sherpa_onnx.xcframework" ]; then
289
+ echo -e "${YELLOW}Removing old framework...${NC}" >&2
290
+ rm -rf "$FRAMEWORKS_DIR/sherpa_onnx.xcframework"
291
+ fi
292
+
293
+ # Extract the zip
294
+ echo -e "${YELLOW}Extracting framework...${NC}" >&2
295
+ unzip -q -o "$zip_path" -d "$FRAMEWORKS_DIR"
296
+
297
+ # Normalize name: podspec expects sherpa_onnx.xcframework; zip may contain sherpa-onnx.xcframework
298
+ if [ -d "$FRAMEWORKS_DIR/sherpa-onnx.xcframework" ] && [ ! -d "$FRAMEWORKS_DIR/sherpa_onnx.xcframework" ]; then
299
+ mv "$FRAMEWORKS_DIR/sherpa-onnx.xcframework" "$FRAMEWORKS_DIR/sherpa_onnx.xcframework"
300
+ fi
301
+
302
+ if [ ! -d "$FRAMEWORKS_DIR/sherpa_onnx.xcframework" ]; then
303
+ echo -e "${RED}Error: Framework extraction failed${NC}" >&2
304
+ echo "Contents of $FRAMEWORKS_DIR:" >&2
305
+ ls -la "$FRAMEWORKS_DIR" 2>/dev/null | head -20 >&2 || true
306
+ rm -f "$zip_path"
307
+ return 1
308
+ fi
309
+
310
+ # Verify required headers are present (needed for iOS build: #include "sherpa-onnx/c-api/cxx-api.h")
311
+ if ! framework_valid "$FRAMEWORKS_DIR/sherpa_onnx.xcframework"; then
312
+ echo -e "${RED}Error: Downloaded framework is missing required headers for building.${NC}" >&2
313
+ echo "Expected: $FRAMEWORKS_DIR/sherpa_onnx.xcframework/ios-arm64_x86_64-simulator/Headers/sherpa-onnx/c-api/cxx-api.h" >&2
314
+ echo "Simulator Headers directory:" >&2
315
+ ls -la "$FRAMEWORKS_DIR/sherpa_onnx.xcframework/ios-arm64_x86_64-simulator/Headers" 2>/dev/null || echo " (missing)" >&2
316
+ if [ -d "$FRAMEWORKS_DIR/sherpa_onnx.xcframework/ios-arm64_x86_64-simulator/Headers" ]; then
317
+ echo "sherpa-onnx/c-api under Headers:" >&2
318
+ ls -la "$FRAMEWORKS_DIR/sherpa_onnx.xcframework/ios-arm64_x86_64-simulator/Headers/sherpa-onnx/c-api" 2>/dev/null || echo " (missing)" >&2
319
+ fi
320
+ rm -rf "$FRAMEWORKS_DIR/sherpa_onnx.xcframework"
321
+ rm -f "$zip_path"
322
+ return 1
323
+ fi
324
+
325
+ # Remove zip file
326
+ rm -f "$zip_path"
327
+
328
+ # Write version file
329
+ echo "$version" > "$VERSION_FILE"
330
+
331
+ echo -e "${GREEN}Framework v$version downloaded and extracted successfully${NC}" >&2
332
+ return 0
333
+ }
334
+
335
+ # Force: remove existing framework and version file so we always re-download
336
+ if [ "$FORCE_DOWNLOAD" = true ]; then
337
+ [ "$INTERACTIVE" = true ] && echo -e "${YELLOW}Force download: removing local framework and version file${NC}" >&2
338
+ rm -rf "$FRAMEWORKS_DIR/sherpa_onnx.xcframework"
339
+ rm -f "$VERSION_FILE"
340
+ fi
341
+
342
+ # Main logic
343
+ if [ -n "$1" ]; then
344
+ # User provided a specific version -> explicit, always honor
345
+ download_and_extract_framework "$1"
346
+ else
347
+ # If env var was set, enforce that version and do not auto-upgrade to latest
348
+ if [ -n "$DESIRED_VERSION" ]; then
349
+ [ "$INTERACTIVE" = true ] && echo -e "${YELLOW}Using SHERPA_ONNX_VERSION=$DESIRED_VERSION${NC}" >&2
350
+ local_version=$(get_local_framework_version)
351
+ if [ "$local_version" != "$DESIRED_VERSION" ] || [ "$FORCE_DOWNLOAD" = true ]; then
352
+ [ "$INTERACTIVE" = true ] && echo -e "${YELLOW}Downloading v$DESIRED_VERSION...${NC}" >&2
353
+ download_and_extract_framework "$DESIRED_VERSION" || exit 1
354
+ else
355
+ [ "$INTERACTIVE" = true ] && echo -e "${GREEN}Framework is already v$local_version${NC}" >&2
356
+ fi
357
+ else
358
+ # DESIRED_VERSION is set above from IOS_RELEASE_TAG (required).
359
+ [ "$INTERACTIVE" = true ] && echo -e "${YELLOW}Using pinned version from IOS_RELEASE_TAG.${NC}" >&2
360
+ local_version=$(get_local_framework_version)
361
+ if [ "$local_version" != "$DESIRED_VERSION" ] || [ "$FORCE_DOWNLOAD" = true ]; then
362
+ download_and_extract_framework "$DESIRED_VERSION" || exit 1
363
+ else
364
+ [ "$INTERACTIVE" = true ] && echo -e "${GREEN}Framework is already v$local_version${NC}" >&2
365
+ fi
366
+ fi
367
+ fi
368
+
369
+ if [ "$INTERACTIVE" = true ]; then
370
+ echo "" >&2
371
+ echo -e "${GREEN}Framework setup complete!${NC}" >&2
372
+ echo "Framework location: $FRAMEWORKS_DIR/sherpa_onnx.xcframework" >&2
373
+ echo "" >&2
374
+ echo "Next steps:" >&2
375
+ echo " 1. cd example" >&2
376
+ echo " 2. pod install" >&2
377
+ echo " 3. Open ios/SherpaOnnxExample.xcworkspace in Xcode" >&2
378
+ fi
379
+ exit 0