Skip to content

Commit 490e8f7

Browse files
committed
feat: tts with preloaded models
1 parent 25c6a2c commit 490e8f7

File tree

14 files changed

+2017
-2290
lines changed

14 files changed

+2017
-2290
lines changed

wasm/combined/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Generated WASM files
22
*.wasm
33
sherpa-onnx-wasm-combined.js
4-
4+
sherpa-onnx-wasm-combined.data
55
# Local model files
66
*.onnx
77
*tokens.txt

wasm/combined/CMakeLists.txt

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
if(NOT $ENV{SHERPA_ONNX_IS_USING_BUILD_WASM_SH})
2-
message(FATAL_ERROR "Please use ./build-wasm-combined.sh to build")
2+
message(FATAL_ERROR "Please use ./build-wasm-combined.sh to build for wasm combined module")
3+
endif()
4+
5+
# Check for asset directories
6+
if(NOT IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/assets/asr")
7+
message(WARNING "ASR assets directory not found at ${CMAKE_CURRENT_SOURCE_DIR}/assets/asr")
8+
endif()
9+
10+
if(NOT IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/assets/vad")
11+
message(WARNING "VAD assets directory not found at ${CMAKE_CURRENT_SOURCE_DIR}/assets/vad")
12+
endif()
13+
14+
if(NOT IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/assets/tts")
15+
message(WARNING "TTS assets directory not found at ${CMAKE_CURRENT_SOURCE_DIR}/assets/tts")
16+
endif()
17+
18+
if(NOT IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/assets/kws")
19+
message(WARNING "KWS assets directory not found at ${CMAKE_CURRENT_SOURCE_DIR}/assets/kws")
320
endif()
421

522
# Collect all exported functions from all modules
@@ -100,6 +117,31 @@ string(APPEND MY_FLAGS " -sEXPORTED_FUNCTIONS=[_malloc,_free,${all_exported_func
100117
# No preloaded assets - all models will be loaded dynamically
101118
string(APPEND MY_FLAGS " -sEXPORTED_RUNTIME_METHODS=['ccall','stringToUTF8','setValue','getValue','lengthBytesUTF8','UTF8ToString','FS'] ")
102119

120+
# Load precompiled assets using structured paths
121+
if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/assets/asr")
122+
string(APPEND MY_FLAGS "--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/assets/asr@/sherpa_assets/asr ")
123+
endif()
124+
125+
if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/assets/vad")
126+
string(APPEND MY_FLAGS "--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/assets/vad@/sherpa_assets/vad ")
127+
endif()
128+
129+
if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/assets/tts")
130+
string(APPEND MY_FLAGS "--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/assets/tts@/sherpa_assets/tts ")
131+
endif()
132+
133+
if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/assets/kws")
134+
string(APPEND MY_FLAGS "--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/assets/kws@/sherpa_assets/kws ")
135+
endif()
136+
137+
if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/assets/speakers")
138+
string(APPEND MY_FLAGS "--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/assets/speakers@/sherpa_assets/speakers ")
139+
endif()
140+
141+
if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/assets/enhancement")
142+
string(APPEND MY_FLAGS "--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/assets/enhancement@/sherpa_assets/enhancement ")
143+
endif()
144+
103145
message(STATUS "MY_FLAGS: ${MY_FLAGS}")
104146

105147
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MY_FLAGS}")
@@ -113,9 +155,16 @@ install(TARGETS sherpa-onnx-wasm-combined DESTINATION bin/wasm/combined)
113155
install(
114156
FILES
115157
"$<TARGET_FILE_DIR:sherpa-onnx-wasm-combined>/sherpa-onnx-wasm-combined.js"
116-
"index.html"
117-
"sherpa-onnx-combined.js"
118158
"$<TARGET_FILE_DIR:sherpa-onnx-wasm-combined>/sherpa-onnx-wasm-combined.wasm"
159+
"$<TARGET_FILE_DIR:sherpa-onnx-wasm-combined>/sherpa-onnx-wasm-combined.data"
160+
"sherpa-onnx-core.js"
161+
"sherpa-onnx-asr.js"
162+
"sherpa-onnx-vad.js"
163+
"sherpa-onnx-tts.js"
164+
"sherpa-onnx-kws.js"
165+
"sherpa-onnx-speaker.js"
166+
"sherpa-onnx-enhancement.js"
167+
"sherpa-onnx-combined.js"
119168
DESTINATION
120169
bin/wasm/combined
121170
)
@@ -144,6 +193,13 @@ if(SHERPA_ONNX_INSTALL_TO_REPO)
144193
-DCOPY_FILES="sherpa-onnx-wasm-combined.wasm"
145194
-P ${CMAKE_CURRENT_SOURCE_DIR}/copy_with_confirm.cmake
146195

196+
# Copy the DATA file
197+
COMMAND ${CMAKE_COMMAND}
198+
-DSRC_DIR=${CMAKE_BINARY_DIR}/bin
199+
-DDEST_DIR=${SHERPA_ONNX_REPO_PATH}
200+
-DCOPY_FILES="sherpa-onnx-wasm-combined.data"
201+
-P ${CMAKE_CURRENT_SOURCE_DIR}/copy_with_confirm.cmake
202+
147203
# Copy the index.html file
148204
COMMAND ${CMAKE_COMMAND}
149205
-DSRC_DIR=${CMAKE_CURRENT_SOURCE_DIR}

wasm/combined/README.md

Lines changed: 35 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,60 @@
1-
# Sherpa-ONNX Combined WebAssembly Module
2-
3-
This directory contains a combined WebAssembly module for the Sherpa-ONNX project, which integrates multiple features:
1+
# Sherpa-ONNX WASM Combined Module
42

3+
This directory contains the WebAssembly (WASM) combined module for Sherpa-ONNX, which includes support for:
54
- Automatic Speech Recognition (ASR)
65
- Voice Activity Detection (VAD)
7-
- Text-to-Speech Synthesis (TTS)
8-
- Speech Enhancement
6+
- Text-to-Speech (TTS)
7+
- Keyword Spotting (KWS)
98
- 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
9+
- Speech Enhancement
7610

77-
The included `index.html` demonstrates how to use the combined module. It shows:
11+
## File Structure
7812

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
13+
When built, the following files are generated:
14+
- `sherpa-onnx-wasm-combined.js` - The main JavaScript glue code
15+
- `sherpa-onnx-wasm-combined.wasm` - The WebAssembly binary
16+
- `sherpa-onnx-wasm-combined.data` - The preloaded assets (models)
17+
- JS library files:
18+
- `sherpa-onnx-core.js` - Core functionality
19+
- `sherpa-onnx-asr.js` - ASR functionality
20+
- `sherpa-onnx-vad.js` - VAD functionality
21+
- `sherpa-onnx-tts.js` - TTS functionality
22+
- `sherpa-onnx-kws.js` - Keyword Spotting functionality
23+
- `sherpa-onnx-speaker.js` - Speaker Diarization functionality
24+
- `sherpa-onnx-enhancement.js` - Speech Enhancement functionality
25+
- `sherpa-onnx-combined.js` - Combined functionality wrapper
8326

84-
## Building the Module
27+
## Building
8528

86-
The WebAssembly module can be built using the provided build script:
29+
To build the WASM module:
8730

8831
```bash
8932
cd /path/to/sherpa-onnx
9033
./build-wasm-combined.sh
9134
```
9235

93-
The built files will be located in `bin/wasm/combined/` and are also copied to `wasm/combined/`.
94-
95-
## Setting Up Models
96-
97-
Before using the demo, you need to set up model files:
36+
This script will:
37+
1. Create a `build-wasm-combined` directory
38+
2. Configure CMake with the necessary options
39+
3. Build the WASM module
40+
4. Install the files to `bin/wasm/combined`
41+
5. Copy the files to the original repo at `wasm/combined`
9842

99-
```bash
100-
cd /path/to/sherpa-onnx/wasm/combined
101-
./setup-assets.sh
102-
```
43+
## Important Notes
10344

104-
This script will download necessary model files to the `assets/` directory.
45+
1. **Large Asset Bundle**: The `.data` file can be very large (300MB+) as it contains all preloaded models. For production, consider using dynamic loading of models instead.
10546

106-
## Testing the Demos
47+
2. **File Locations**: All files must be in the same directory for the WASM module to work correctly. The `.data` file MUST be in the same directory as the `.js` and `.wasm` files.
10748

108-
After building the module and setting up the models, you can test the demos using Python's built-in HTTP server:
49+
3. **Local Testing**: To test locally, run a web server from the `wasm/combined` directory:
10950

11051
```bash
11152
cd /path/to/sherpa-onnx/wasm/combined
112-
python3 -m http.server 8080
53+
python -m http.server 8000
11354
```
11455

115-
Then navigate to http://localhost:8080/demos/ in your web browser to access the demos.
56+
Then open `http://localhost:8000` in your browser.
11657

117-
## Troubleshooting
58+
## License
11859

119-
- **Module load errors**: Ensure the WASM module is loaded before any other scripts
120-
- **Model load errors**: Check the browser console for specific error messages
121-
- **Audio capture issues**: Make sure your browser has permission to access the microphone
122-
- **Performance issues**: Try reducing buffer sizes or using smaller models
60+
Same as Sherpa-ONNX.

0 commit comments

Comments
 (0)