Part 4 of Building All to Audiobook
How do you run a speech model in a browser when the reader's computer might not have a usable GPU? In All to Audiobook, I try WebGPU first and keep a WebAssembly CPU path available.
Building All to Audiobook
I ran into these problems while building All to Audiobook for my mom. After neck surgery, holding a book became difficult for her, and many of the ebooks she wanted to read had no audiobook edition. I wanted her to turn books she already had into audio she could keep and listen to in her usual player.
The app processes documents and generates speech on the reader's own device, then exports MP3 or chaptered M4B files. This series follows the practical problems I encountered while building it, the approaches I chose, and what still needs work.
The earlier parts covered preparing PDF text, saving unfinished conversions, and checking generated passages in Studio. This last part returns to the browser app and the model that produces its audio.
Keep inference out of the page's main thread
I use Kokoro, an existing 82-million-parameter speech model, through Kokoro.js and Transformers.js. ONNX Runtime runs the exported model. My application handles the document, the choice of runtime, and the audio it gets back.
The model lives in a dedicated web worker. The job runner sends it one prepared passage and a voice selection at a time. The worker sends back loading progress, its selected engine, and audio samples.
When a passage finishes, the app encodes and saves its audio before moving on. The worker stays available for the next passage, so I can reuse the loaded model. A ten-hour audiobook doesn't mean loading the model for every paragraph.
The worker transfers the sample buffer back to the app. Keeping inference there lets the page handle controls and progress updates separately from the model's computation.
How do I choose between the GPU and CPU?
I ask the browser for a GPU adapter before choosing WebGPU. If it returns none, or the request fails, I use WebAssembly on the CPU.
The two paths load different weight files:
| Path | Weights | Model file size |
|---|---|---|
| WebGPU | 32-bit floating point | About 325.5 MB |
| CPU / WebAssembly | 8-bit quantized | About 92.4 MB |
Those sizes come from the pinned files in the app's asset manifest. The runtime, tokenizer and voice files add to the initial download.
ONNX Runtime provides both execution paths. My loader chooses the matching model file and keeps the tokenizer and voice assets on the same pinned revision.
For the WebAssembly path, I allow up to four threads when the page is cross-origin isolated, bounded by the reported hardware concurrency. Otherwise, it uses one thread.
Getting a GPU adapter only gets me to the next step. The model still has to load and return usable audio.
Check the output before accepting it
Before sending an audio chunk back, the worker checks that it contains samples and that every value is finite. It also checks the peak amplitude. A peak below 0.00001 is treated as silent output.
These checks catch empty buffers, invalid numbers and silence. A valid buffer can still contain a mispronounced name or missing words. The passage transcription check from part three is a separate Studio feature.
If model loading fails on WebGPU, I dispose of the current model and load the CPU version. During synthesis, the switch has a narrower condition. It happens only for a GPU-related error or invalid audio, before that request has returned any audio chunks.
That boundary matters because one request can produce several chunks. Replaying the text on the CPU after sending some GPU audio could duplicate words if both outputs were kept.
For an error after audio has already been emitted, the worker reports failure instead of changing engines halfway through that request. The job runner can then handle the failed passage.
After a switch, the worker stays on the CPU for the rest of its lifetime. It doesn't try the failing GPU again on every passage.
The retry boundary belongs to the current synthesis request. Earlier saved passages remain available.
Download once, reuse the pinned files
The first model download is a visible part of the experience. I report bytes received while preparing the runtime and voice assets, before the job switches to generating audio.
The loader first looks in Cache Storage. Its cache name includes the model revision, and each asset has an expected SHA-256 hash. It checks cached bytes before using them and checks newly downloaded bytes before caching them.
I keep the runtime version pinned too. This gives the loader a specific set of files to reuse on the next visit. If the browser cannot retain them, that conversion can still proceed, but a later visit may need another download.
There are two kinds of reuse here. During a conversion, the worker keeps the loaded model. Across visits, the browser keeps the downloaded files. After creating the ONNX session, I also release the loader's extra JavaScript copy of the weights.
What stays on the reader's computer
Document processing and speech generation run on the reader's device. The model files arrive over the network; the reader's document and generated audio stay local. Account and usage checks have their own network requests.
The reader's hardware still determines how long a book takes. A CPU path gives the app another way to run, but it doesn't make an older computer perform like a fast GPU. Closing the tab also stops inference. The saved passages described in part two make it possible to continue later.
You can try All to Audiobook's Kokoro browser app with a short EPUB or text-based PDF. Choose a voice and let the first run prepare its files. Start with one chapter, listen to the result, and download the audio for your usual player.


