# EarShelf Turn PDF, EPUB and TXT books into audiobooks **inside the browser**. No server does any work: parsing, text-to-speech and storage all happen on the person's device. It installs as a PWA and works offline. Ideas taken from the two reference projects: - [abogen](https://github.com/denizsafak/abogen): Kokoro-82M as the voice, sentence-by-sentence generation, chapter detection from EPUB/PDF structure. - [ebook2audiobook](https://github.com/DrewThomasson/ebook2audiobook): EPUB/PDF → chapters → audio pipeline, multi-language support (here through the device voices). ## How it works | Step | Where | How | |---|---|---| | Read the file | main thread | PDF: `pdfjs-dist` (outline → chapters, page numbers removed, hyphenated line breaks joined). EPUB: `JSZip` + DOMParser (OPF spine, nav/NCX titles, cover, footnote markers dropped, DRM refused). TXT: "Chapter N" headings. | | Split | `src/text.ts` | Sentences, long ones cut at commas (≤170 chars ≈ ≤10 s of audio, so playback starts fast). Language guessed from stopwords. | | Speak (natural) | Web Worker `src/tts-worker.ts` | Kokoro-82M through `kokoro-js`/Transformers.js on WASM (fp16). Generates 6 sentences ahead; each clip is saved to IndexedDB, so replay is instant and offline. | | Speak (device) | `speechSynthesis` | The OS voice: instant, every language, no download. | | Store | IndexedDB `earshelf` | Books (text, cover, position), generated audio (capped at 400 MB, oldest dropped first), settings. | | Offline | `public/sw.js` | App shell cache. The model is cached by Transformers.js itself, never twice. | ### Voice logic (the important part) 1. First open: the **device voice starts at once**, so the first win is immediate. 2. The natural voice downloads in the background (≈160 MB, once). It downloads automatically only on unmetered connections; on mobile data or Data Saver the app asks first. 3. When it is ready, the app **test-generates one sentence**. If this device generates faster than speech, it switches by itself at the next sentence. If not, the device voice keeps reading and the app offers **"Prepare this chapter"**, which generates the natural audio in the background. 4. Non-English books use the device voice (Kokoro in the browser speaks English only). 5. No device voice available (e.g. Chrome on Linux without speech packages)? It switches to the natural voice. ### Player (UX round 2, 2026-11-18) - Back 15 s / forward 30 s (≈15 characters a second at 1×, landing on a sentence start); the lock screen's seek buttons use the same jumps; arrow keys still step one sentence. - Sleep timer chip: 15 / 30 / 45 / 60 min or end of chapter (it pauses at the start of the next one and steps back a sentence after a timed stop). - Screen wake lock while playing (live voices stop when a phone locks the screen); one-time tip on touch devices. - Library home: a "Continue listening" card with one-tap play once a book is on the shelf; the hero shrinks. - Download progress is a one-line chip; toasts sit at the top, below it. SVG icons only, no emoji. ## Measured (2026-11-18) | Device | Setting | Real-time factor (lower is faster; <1 = live) | |---|---|---| | Desktop, Ryzen 5 7500F, Chrome 157 | WASM fp16, 4 threads | **0.52** (q8 0.94, q4f16 0.52, fp32 0.52; threads 4/8/11 no gain) | | Galaxy Note 8 (2017), Android 16, Chrome 157 | WASM fp16, 2 threads | **≈5** (q8 ≈6–7; 4+ threads slower) | - Real phone: device voice speaks **0.5–0.8 s** after the tap; model ready in ~21 s from the app's own host; decision (switch or offer Prepare) at ~50 s; Prepare: 5 of 13 sentences in 2 min. - Desktop: cached natural audio starts in **144 ms**. - WebGPU: needs the fp32 model (326 MB), which failed to cache and was not faster in tests. Off by default (`localStorage['earshelf.device']='webgpu'` forces it). ## Run ```bash npm install --onnxruntime-node-install-cuda=skip npm run fetch-model # optional: self-host the voice model under public/models (else it loads from Hugging Face) npm run dev # http://localhost:5173 npm run build # static files in dist/ ``` ## Test ```bash npm test # unit (vitest) npm run fixtures # rebuild test books npm run e2e # Playwright, real Chrome, fresh profiles, desktop + phone viewport (includes the real Kokoro model, offline, WebView-without-speech) ``` Scripts used for device checks: `scripts/bench.mjs` (desktop speed), `scripts/phone*.mjs` (real Android over `adb reverse` + DevTools). ## Deploy Any static host. For multi-threaded WASM, serve with these headers (already in `public/_headers` for Netlify/Cloudflare and `vercel.json`): ``` Cross-Origin-Opener-Policy: same-origin Cross-Origin-Embedder-Policy: credentialless ``` Self-hosting the model (`npm run fetch-model`) puts a 163 MB file in `dist/models`. Cloudflare Pages refuses files over 25 MB, so either skip it there (the app then loads the model from Hugging Face) or use a host without that limit. ### Hugging Face Space `bash scripts/deploy-hf.sh [owner/space]` (default `WilburDev/earshelf`, needs `hf auth login`). It builds into a temp folder, drops `models/` (the app then loads Kokoro from its own Hugging Face repo, same CDN) and writes the Space README with `sdk: static` and `custom_headers` for COOP/COEP (HF allows only those COOP/COEP/CORP headers, lowercase). ## Licence EarShelf is free software under the **GNU GPL-3.0-or-later** (`LICENSE`). The reason is that `kokoro-js` → `phonemizer` is built from **eSpeak NG (GPL-3.0)**. Selling is still allowed under the GPL: the Pro key is a paid convenience, and the source stays public. Every deploy uploads the full source to the Space's `source/` folder. Third-party licences: `THIRD_PARTY_NOTICES.md`. Owner decision 2026-11-19: GPL now, no eSpeak swap (the replacement study is `docs/research/phonemizer-replacement.md`). ## Limits (MVP) - Natural voice: English only, and slower than real time on older phones (Prepare mode covers it). - Scanned PDFs (images only) are refused with a message: there is no OCR. - DRM-protected EPUBs are refused. - Not tested on iOS Safari (no device here). Safari has no `credentialless` COEP, so the model runs single-threaded there. - No export to an audio file yet (the audio is kept inside the browser). - Screen locked / app in the background: phones pause web pages. Prepared (cached) natural audio keeps playing best; the device voice and live generation can stop until the screen is back on. A future version could stream audio continuously (MediaSource) to keep the session alive. - Natural voice only for English books; other languages and scripts (detected by writing system, incl. RTL and CJK) use the device voice.