Skip to content

Commit f362c81

Browse files
committed
feat: web wasm with single file
1 parent e328002 commit f362c81

26 files changed

+7160
-2
lines changed

.gitignore

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,11 @@ kokoro-multi-lang-v1_0
136136
sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16
137137
cmake-build-debug
138138
README-DEV.txt
139-
*.rknn
140-
*.jit
139+
141140
##clion
142141
.idea
142+
scripts/dotnet/examples/obj/Debug/net8.0/Common.AssemblyInfo.cs
143+
scripts/dotnet/examples/obj/Debug/net8.0/Common.GeneratedMSBuildEditorConfig.editorconfig
144+
scripts/dotnet/examples/obj/Debug/net8.0/Common.AssemblyInfoInputs.cache
145+
143146
sherpa-onnx-dolphin-base-ctc-multi-lang-int8-2025-04-02

wasm/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ endif()
2929
if(SHERPA_ONNX_ENABLE_WASM_NODEJS)
3030
add_subdirectory(nodejs)
3131
endif()
32+
33+
if(SHERPA_ONNX_ENABLE_WASM_COMBINED)
34+
add_subdirectory(combined)
35+
endif()

wasm/combined/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Generated WASM files
2+
*.wasm
3+
sherpa-onnx-wasm-combined.js
4+
5+
# Local model files
6+
*.onnx
7+
*tokens.txt

wasm/combined/CMakeLists.txt

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
if(NOT $ENV{SHERPA_ONNX_IS_USING_BUILD_WASM_SH})
2+
message(FATAL_ERROR "Please use ./build-wasm-combined.sh to build")
3+
endif()
4+
5+
# Collect all exported functions from all modules
6+
set(exported_functions
7+
# Core utilities
8+
CopyHeap
9+
MyPrintOnlineASR
10+
MyPrintVAD
11+
MyPrintTTS
12+
MyPrintSpeakerDiarization
13+
MyPrintSpeechEnhancement
14+
MyPrintKeywordSpotting
15+
SherpaOnnxFileExists
16+
17+
# Online ASR
18+
SherpaOnnxCreateOnlineRecognizer
19+
SherpaOnnxCreateOnlineStream
20+
SherpaOnnxDecodeOnlineStream
21+
SherpaOnnxDestroyOfflineStreamResultJson
22+
SherpaOnnxDestroyOnlineRecognizer
23+
SherpaOnnxDestroyOnlineRecognizerResult
24+
SherpaOnnxDestroyOnlineStream
25+
SherpaOnnxDestroyOnlineStreamResultJson
26+
SherpaOnnxGetOfflineStreamResultAsJson
27+
SherpaOnnxGetOnlineStreamResult
28+
SherpaOnnxGetOnlineStreamResultAsJson
29+
SherpaOnnxIsOnlineStreamReady
30+
SherpaOnnxOnlineStreamAcceptWaveform
31+
SherpaOnnxOnlineStreamInputFinished
32+
SherpaOnnxOnlineStreamIsEndpoint
33+
SherpaOnnxOnlineStreamReset
34+
35+
# Offline ASR
36+
SherpaOnnxCreateOfflineRecognizer
37+
SherpaOnnxCreateOfflineStream
38+
SherpaOnnxDecodeOfflineStream
39+
SherpaOnnxDecodeMultipleOfflineStreams
40+
SherpaOnnxDestroyOfflineRecognizer
41+
SherpaOnnxDestroyOfflineRecognizerResult
42+
SherpaOnnxDestroyOfflineStream
43+
SherpaOnnxAcceptWaveformOffline
44+
SherpaOnnxGetOfflineStreamResult
45+
46+
# TTS
47+
SherpaOnnxCreateOfflineTts
48+
SherpaOnnxDestroyOfflineTts
49+
SherpaOnnxDestroyOfflineTtsGeneratedAudio
50+
SherpaOnnxOfflineTtsGenerate
51+
SherpaOnnxOfflineTtsGenerateWithCallback
52+
SherpaOnnxOfflineTtsSampleRate
53+
SherpaOnnxOfflineTtsNumSpeakers
54+
SherpaOnnxWriteWave
55+
56+
# VAD
57+
SherpaOnnxCreateCircularBuffer
58+
SherpaOnnxDestroyCircularBuffer
59+
SherpaOnnxCircularBufferPush
60+
SherpaOnnxCircularBufferGet
61+
SherpaOnnxCircularBufferFree
62+
SherpaOnnxCircularBufferPop
63+
SherpaOnnxCircularBufferSize
64+
SherpaOnnxCircularBufferHead
65+
SherpaOnnxCircularBufferReset
66+
SherpaOnnxCreateVoiceActivityDetector
67+
SherpaOnnxDestroyVoiceActivityDetector
68+
SherpaOnnxVoiceActivityDetectorAcceptWaveform
69+
SherpaOnnxVoiceActivityDetectorEmpty
70+
SherpaOnnxVoiceActivityDetectorDetected
71+
SherpaOnnxVoiceActivityDetectorPop
72+
SherpaOnnxVoiceActivityDetectorClear
73+
SherpaOnnxVoiceActivityDetectorFront
74+
SherpaOnnxDestroySpeechSegment
75+
SherpaOnnxVoiceActivityDetectorReset
76+
SherpaOnnxVoiceActivityDetectorFlush
77+
78+
# KWS
79+
SherpaOnnxCreateKeywordSpotter
80+
SherpaOnnxDestroyKeywordSpotter
81+
SherpaOnnxCreateKeywordStream
82+
SherpaOnnxIsKeywordStreamReady
83+
SherpaOnnxDecodeKeywordStream
84+
SherpaOnnxResetKeywordStream
85+
SherpaOnnxGetKeywordResult
86+
SherpaOnnxDestroyKeywordResult
87+
)
88+
89+
set(mangled_exported_functions)
90+
foreach(x IN LISTS exported_functions)
91+
list(APPEND mangled_exported_functions "_${x}")
92+
endforeach()
93+
list(JOIN mangled_exported_functions "," all_exported_functions)
94+
95+
include_directories(${CMAKE_SOURCE_DIR})
96+
set(MY_FLAGS " -s FORCE_FILESYSTEM=1 -s INITIAL_MEMORY=512MB -s ALLOW_MEMORY_GROWTH=1")
97+
string(APPEND MY_FLAGS " -sSTACK_SIZE=10485760 ") # 10MB
98+
string(APPEND MY_FLAGS " -sASYNCIFY=1 -sFETCH=1 ") # For async loading
99+
string(APPEND MY_FLAGS " -sEXPORTED_FUNCTIONS=[_malloc,_free,${all_exported_functions}] ")
100+
# No preloaded assets - all models will be loaded dynamically
101+
string(APPEND MY_FLAGS " -sEXPORTED_RUNTIME_METHODS=['ccall','stringToUTF8','setValue','getValue','lengthBytesUTF8','UTF8ToString','FS'] ")
102+
103+
message(STATUS "MY_FLAGS: ${MY_FLAGS}")
104+
105+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MY_FLAGS}")
106+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MY_FLAGS}")
107+
set(CMAKE_EXECUTBLE_LINKER_FLAGS "${CMAKE_EXECUTBLE_LINKER_FLAGS} ${MY_FLAGS}")
108+
109+
add_executable(sherpa-onnx-wasm-combined sherpa-onnx-wasm-combined.cc)
110+
target_link_libraries(sherpa-onnx-wasm-combined sherpa-onnx-c-api)
111+
install(TARGETS sherpa-onnx-wasm-combined DESTINATION bin/wasm/combined)
112+
113+
install(
114+
FILES
115+
"$<TARGET_FILE_DIR:sherpa-onnx-wasm-combined>/sherpa-onnx-wasm-combined.js"
116+
"index.html"
117+
"sherpa-onnx-combined.js"
118+
"$<TARGET_FILE_DIR:sherpa-onnx-wasm-combined>/sherpa-onnx-wasm-combined.wasm"
119+
DESTINATION
120+
bin/wasm/combined
121+
)
122+
123+
# Add option to install to original repo
124+
option(SHERPA_ONNX_INSTALL_TO_REPO "Install compiled WASM files to original repo directory" OFF)
125+
set(SHERPA_ONNX_REPO_PATH "${CMAKE_SOURCE_DIR}/wasm/combined" CACHE PATH "Path to original repo wasm directory")
126+
127+
if(SHERPA_ONNX_INSTALL_TO_REPO)
128+
# Add a custom target that will run after the installation
129+
add_custom_target(install_to_repo ALL
130+
COMMAND ${CMAKE_COMMAND} -E echo "Installing to original repo at ${SHERPA_ONNX_REPO_PATH}..."
131+
COMMAND ${CMAKE_COMMAND} -E make_directory ${SHERPA_ONNX_REPO_PATH}
132+
133+
# Copy the JS file
134+
COMMAND ${CMAKE_COMMAND}
135+
-DSRC_DIR=${CMAKE_BINARY_DIR}/bin
136+
-DDEST_DIR=${SHERPA_ONNX_REPO_PATH}
137+
-DCOPY_FILES="sherpa-onnx-wasm-combined.js"
138+
-P ${CMAKE_CURRENT_SOURCE_DIR}/copy_with_confirm.cmake
139+
140+
# Copy the WASM file
141+
COMMAND ${CMAKE_COMMAND}
142+
-DSRC_DIR=${CMAKE_BINARY_DIR}/bin
143+
-DDEST_DIR=${SHERPA_ONNX_REPO_PATH}
144+
-DCOPY_FILES="sherpa-onnx-wasm-combined.wasm"
145+
-P ${CMAKE_CURRENT_SOURCE_DIR}/copy_with_confirm.cmake
146+
147+
# Copy the index.html file
148+
COMMAND ${CMAKE_COMMAND}
149+
-DSRC_DIR=${CMAKE_CURRENT_SOURCE_DIR}
150+
-DDEST_DIR=${SHERPA_ONNX_REPO_PATH}
151+
-DCOPY_FILES="index.html"
152+
-P ${CMAKE_CURRENT_SOURCE_DIR}/copy_with_confirm.cmake
153+
154+
# Copy the JS library file
155+
COMMAND ${CMAKE_COMMAND}
156+
-DSRC_DIR=${CMAKE_CURRENT_SOURCE_DIR}
157+
-DDEST_DIR=${SHERPA_ONNX_REPO_PATH}
158+
-DCOPY_FILES="sherpa-onnx-combined.js"
159+
-P ${CMAKE_CURRENT_SOURCE_DIR}/copy_with_confirm.cmake
160+
161+
DEPENDS sherpa-onnx-wasm-combined
162+
COMMENT "Checking and installing WASM files to original repo"
163+
)
164+
endif()

wasm/combined/README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Sherpa-ONNX Combined WebAssembly Module
2+
3+
This directory contains a combined WebAssembly module for the Sherpa-ONNX project, which integrates multiple features:
4+
5+
- Automatic Speech Recognition (ASR)
6+
- Voice Activity Detection (VAD)
7+
- Text-to-Speech Synthesis (TTS)
8+
- Speech Enhancement
9+
- Speaker Diarization
10+
- Keyword Spotting
11+
12+
## How to Use
13+
14+
### Loading the Module
15+
16+
You can use the combined module in two ways:
17+
18+
#### Option 1: Load Individual Modules (Recommended)
19+
20+
This approach loads only the components you need:
21+
22+
```html
23+
<!-- First load the WASM module -->
24+
<script src="sherpa-onnx-wasm-combined.js"></script>
25+
26+
<!-- Load the core module which is required by all other modules -->
27+
<script src="sherpa-onnx-core.js"></script>
28+
29+
<!-- Then load only the modules you need -->
30+
<script src="sherpa-onnx-vad.js"></script>
31+
<!-- Add other modules as needed -->
32+
33+
<script>
34+
// This callback is called when the WASM module is loaded
35+
window.onModuleReady = function() {
36+
// Your initialization code here
37+
console.log("Module ready!");
38+
};
39+
</script>
40+
```
41+
42+
#### Option 2: Load All Modules via the Combined Loader
43+
44+
This approach loads all available modules:
45+
46+
```html
47+
<!-- First load the WASM module -->
48+
<script src="sherpa-onnx-wasm-combined.js"></script>
49+
50+
<!-- Then load the combined module that will load all other modules -->
51+
<script src="sherpa-onnx-combined.js"></script>
52+
53+
<script>
54+
// This callback is called when all modules are loaded
55+
window.onSherpaOnnxReady = function() {
56+
// Your initialization code here
57+
console.log("All modules loaded!");
58+
};
59+
</script>
60+
```
61+
62+
### Module Structure
63+
64+
The codebase has been organized into modular files:
65+
66+
- `sherpa-onnx-core.js`: Core functionality, utilities, and file system operations
67+
- `sherpa-onnx-vad.js`: Voice Activity Detection functionality
68+
- `sherpa-onnx-combined.js`: Loader that loads all individual modules
69+
70+
Additional modules will be added in the future:
71+
- `sherpa-onnx-asr.js`: Automatic Speech Recognition functionality
72+
- `sherpa-onnx-tts.js`: Text-to-Speech functionality
73+
- And more...
74+
75+
## Demo Application
76+
77+
The included `index.html` demonstrates how to use the combined module. It shows:
78+
79+
1. How to load models from URLs
80+
2. How to initialize each component (ASR, VAD, TTS)
81+
3. How to stream audio from the microphone
82+
4. How to get results from each component
83+
84+
## Building the Module
85+
86+
The WebAssembly module is built using Emscripten. To rebuild it:
87+
88+
```bash
89+
cd /path/to/sherpa-onnx
90+
mkdir -p build-wasm-combined
91+
cd build-wasm-combined
92+
emcmake cmake -DCMAKE_BUILD_TYPE=Release -DSHERPA_ONNX_ENABLE_WASM=ON -DSHERPA_ONNX_ENABLE_CHECK=OFF ..
93+
make -j$(nproc)
94+
```
95+
96+
The built files will be located in `build-wasm-combined/wasm/combined/`.
97+
98+
## Setting Up Models
99+
100+
Before using the demo, you need to set up model files:
101+
102+
```bash
103+
cd /path/to/sherpa-onnx/wasm/combined
104+
./setup-assets.sh
105+
```
106+
107+
This script will download necessary model files to the `assets/` directory.
108+
109+
## Troubleshooting
110+
111+
- **Module load errors**: Ensure the WASM module is loaded before any other scripts
112+
- **Model load errors**: Check the browser console for specific error messages
113+
- **Audio capture issues**: Make sure your browser has permission to access the microphone
114+
- **Performance issues**: Try reducing buffer sizes or using smaller models

wasm/combined/assets/.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Ignore all ONNX model files and subdirectories
2+
*.onnx
3+
asr/
4+
vad/
5+
tts/
6+
speakers/
7+
enhancement/
8+
kws/
9+
10+
# Ignore tokens files
11+
*tokens.txt
12+
13+
# Ignore temporary files
14+
tmp/
15+
16+
# But keep the README.md and setup script
17+
!README.md
18+
!setup-assets.sh

0 commit comments

Comments
 (0)