Skip to content

Narrate the book with Kokoro - #94

Open
bc1cindy wants to merge 3 commits into
mainfrom
narration
Open

bc1cindy wants to merge 3 commits into
mainfrom
narration

Conversation

@bc1cindy

Copy link
Copy Markdown
Member

Narrate each chapter with Kokoro at build time, so the book can be listened to. Closes #76.

1116.1.-github.mp4

Narration reads a chapter's text aloud. Extract it without frontmatter,
code, diagrams, tables, markup or math, one heading, list item or
paragraph per line, and name its audio by the hash of the text, so audio
rendered from other text never stands in for it.
Comment thread contrib/speak.py Resolved
Render each chapter to one MP3 per voice inside the build, with the
Kokoro from nixpkgs and its weights and voices fetched by hash. Each
section, cut before each of the chapter's headings, is read by its own
derivation per voice, so an edit re-renders only its section, and the
start of each section is kept beside the MP3. Speaking lives in
contrib/speak.py, apart from tts.py, so that a change to tts.py
re-renders only the sections whose text it changes. The book is read in
af_heart, am_echo, bf_emma and bf_isabella; the list is in nix/tts.nix.

With an empty cache the Pages build narrates the whole book, at about 10
minutes per voice on a 4 core runner, which outlasts the 30 minute
timeout.
Put the browser's audio player, one per rendered voice, at the top of
each chapter, so that narration works without JavaScript.
theme/narration.js replaces them with one player floating at the bottom
of the page, opened from the menu bar: play, a timeline marked at the
chapter's main sections, speed, voice, and the position where the reader
stopped. The audio is linked from the site URL, since the print page
holds every chapter at the root. The preprocessor lives apart from the
renderer, so that changing it does not re-render the book.
@github-actions

Copy link
Copy Markdown
range-diff for force push b6dda281e9 → 815186e765 November 16, 2026 22:04 UTC: 1 modified, 2 unchanged

reproduce: git range-diff cbb5404..b6dda28 cbb5404..815186e

1:  57f0660d67 = 1:  57f0660d67 Extract each chapter's readable text
2:  f84d21c7ce ! 2:  2ee3785979 Render narration with Kokoro
3:  b6dda281e9 = 3:  815186e765 Add an audio player to each page
2: f84d21c7ce ! 2: 2ee3785979 Render narration with Kokoro
@@ contrib/speak.py (new)
 +        sys.exit(__doc__)
 +    text, voice, out = Path(sys.argv[1]).read_text(), sys.argv[2], sys.argv[3]
 +    model = KModel(repo_id="hexgrad/Kokoro-82M",
-+                   config=os.environ["KOKORO_CONFIG"], model=os.environ["KOKORO_MODEL"])
++                   config=os.environ["KOKORO_CONFIG"], model=os.environ["KOKORO_MODEL"]).eval()
 +    # The voice's first letter is its language: a for American, b for British
 +    # English.
 +    pipeline = KPipeline(lang_code=voice[0], repo_id="hexgrad/Kokoro-82M", model=model)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

read-aloud for the book

2 participants