DialogueLab
French-first text-to-speech web tool backed by Google Cloud Text-to-Speech, running entirely on a single Cloudflare Worker.
- Voices: Chirp 3: HD (default) and, when a service account is configured, Gemini-TTS (style prompts such as "read slowly, like a storyteller", and two-speaker dialogue).
- Language: French (
fr-FR) by default; ~50 languages on Chirp 3: HD and ~90 on Gemini-TTS.
- No sign-up: anyone can use it. Google credentials stay on the server, and abuse is limited by Turnstile, per-IP rate limits, a length cap and a response cache.
Stack
| Layer |
Choice |
| Runtime |
Cloudflare Workers |
| API |
Hono under /api/* (server/) |
| Pages |
React Router v8 framework mode, home page prerendered at build time (app/) |
| Build |
Vite + @cloudflare/vite-plugin |
| Styles |
Tailwind CSS; every component is hand-written, no UI or component library |
| Tests |
Vitest (test/) |
workers/app.ts is the Worker entry: requests to /api/* go to Hono, and everything else is server-rendered by React Router.
The home page is the same for every visitor, since each one's scripts load
from their own browser afterwards, so it is prerendered at build time
(prerender in react-router.config.ts). The build starts a local preview of
the Worker, renders / once, and writes build/client/index.html, which
Cloudflare then serves as a static file: a page view never runs the Worker,
costs no CPU time, and does not count against the Workers request quota. Only
the API, robots.txt, sitemap.xml, llms.txt and unknown paths reach it.
Two consequences:
- The page holds the vars it was built with. Change one in
wrangler.jsonc
and the next build picks it up; a local build also reads .dev.vars, so
deploy from CI, where there is none.
- Its canonical address comes from
SITE_URL, because the build renders it
on localhost.
The reader
One page at /: a dialogue reader for language practice, not a one-shot text
box. You write a scene, give each character a voice, and play it back line by
line.
- Cast — any number of speakers, each with a name, a colour, a catalog
voice and, on Gemini, a direction such as "anxious, speaking quickly".
A speaker can be set to skip, which leaves them out of playback.
- Script — a line editor.
Enter starts the next line with the other
speaker, so typing a dialogue is uninterrupted. Write [1.5] or
[pause 2] inside a line for real silence at that point.
- Plain text — the script as text in the DialogueLab format, editable, with
every problem listed by line.
- Playback — repeat each line, leave a gap, or leave a shadowing pause
proportional to the line so you can say it back. Loop, per-speaker speed and
volume, and a dictation mode that blurs the text until you reveal each line.
- Dictionary — spelling to pronunciation, applied before synthesis only, so
the text on screen never changes.
- Translations — each line can carry a translation, shown under it and
never read aloud.
- Library — several scripts in the browser. Import listening material, and
export a script or the whole library as text, a JSON backup, or audio
(every line joined into one MP3).
Listening material
Material is written in the DialogueLab format, a plain-text format for
dialogues, monologues and single sentences with optional translations. The
specification, with a prompt for converting existing material with a chat
model, is collaborators/listening-format.md; a working
sample is collaborators/listening-pack.txt.
Library → Import scripts… checks a file line by line before importing it,
and items carrying an @id replace their earlier version on re-import.
The page is server-rendered: the sample scene, the cast, the 30 voices and
every language are in the first HTML response, from the same buildCatalog()
the API serves. The browser's saved library replaces the sample only after
hydration, so the first client render still matches the server's HTML.
Speed and volume are applied to the audio element rather than sent to Google,
so changing them is instant, free, and leaves every clip cache-identical.
Google returns no word timings, so the highlight follows whole segments rather
than individual words.
/robots.txt and /sitemap.xml are generated from the request host, so they
are correct on whatever domain the Worker is deployed to.
API
Clips: audio for other sites
POST /api/v1/clips and GET /api/v1/clips/<id>.mp3 give pages built
elsewhere, such as a course's HTML exercises, permanent audio URLs. The full
guide, written for the AI agents that build those pages, is
collaborators/clips-api.md; a deployment serves it at /llms.txt
with its own address filled in.
- Made once, with a key.
POST /api/v1/clips takes up to 100 sentences or
whole dialogues (turns, joined into one clip) and returns a URL for each. It
needs Authorization: Bearer <key> and no Turnstile, since a page on another
site could never pass a Turnstile check bound to this one.
- Played forever, without one.
GET /api/v1/clips/<id>.mp3 is public, has
open CORS, supports byte ranges (Safari needs them to play audio), is cached
by browsers for a year, and never calls Google. The key therefore never
has to appear in a page, and replaying costs nothing.
- Same input, same URL. A clip's id hashes everything that shapes its
sound, so asking again is free (
created: false). Clips do not read the
reader's KV cache: an existence check in R2 is all a repeat costs, which
keeps each request's subrequest budget for new clips.
- Bounded. Each request stays within a subrequest budget (see below); what
does not fit comes back
pending, and the client sends those items again.
Each key has a daily character budget (API_DAILY_CHARS) and its own rate
limit.
Clips live in R2 without expiry. Unlike the 30-day synthesis cache, a published
exercise must keep working.
GET /api/health
{ "ok": true }
GET /api/catalog
Everything a client needs to build its controls: defaults, limits, output formats, the 30 voices, and the languages and models for each engine. The default language is listed first. The data is static, so this endpoint costs nothing and is cacheable.
POST /api/tts/batch
Synthesizes many short lines in one call. A reader plays a script line by line;
one request per line would exhaust the per-IP rate limit within seconds, so the
whole script is one request and one rate-limit unit.
{
"engine": "chirp3-hd", // shared by every item
"language": "fr-FR",
"format": "mp3",
"items": [
{ "text": "Bonjour madame.", "voice": "Charon" },
{ "text": "Un cafe, merci.", "voice": "Kore" }
]
}
Each item may carry voice, and prompt (Gemini) or speakingRate (Chirp).
At most 300 items and 20,000 characters per request; a client splits longer
scripts itself, guided by limits.batch in the catalog.
The response is JSON, with base64 audio per item:
{
"format": "mp3",
"contentType": "audio/mpeg",
"characters": 30,
"synthesized": 1, // how many actually reached Google
"items": [
{ "audio": "<base64>", "characters": 15, "cache": "HIT" },
{ "audio": "<base64>", "characters": 15, "cache": "MISS" }
]
}
Every line is cached on its own, so editing one line only re-bills that
line.
A request only does as much as the subrequest budget allows. Lines it did not
reach come back as { "status": "pending" }, with "complete": false; send
those again. The reader does this on its own.
The subrequest budget
On Workers, every call to Google and every KV or R2 operation counts as a
subrequest, and the free plan allows 50 per request (10,000 on paid). A batch
line can cost three (cache read, synthesis, cache write), so an unbounded batch
fails on the free plan beyond about fifteen new lines. Both batch endpoints
therefore spend at most SUBREQUEST_BUDGET (default 40) per request: they work
in order, reserve the worst case per item, and let cache hits fund more items.
They always finish at least one item, so a client that resends the pending ones
cannot loop forever. On a paid plan, raise the budget and one request does
everything.
POST /api/tts
Request headers:
Content-Type: application/json
X-Turnstile-Token: <token>: required in production. Tokens are single-use, so reset the widget after every request.
Body (only text is required):
| Field |
Type |
Default |
Notes |
text |
string |
— |
Up to MAX_CHARS characters (default 5,000). Longer text is split at sentence boundaries and the audio is joined. |
engine |
"chirp3-hd" | "gemini" |
chirp3-hd |
|
language |
string |
fr-FR |
Must be supported by the engine (see catalog). Case-insensitive. |
voice |
string |
Charon (Chirp), Kore (Gemini) |
One of the 30 catalog voices. Case-insensitive. |
format |
"mp3" | "wav" | "ogg_opus" |
mp3 |
ogg_opus only for text that fits in a single chunk (~4.5 KB). gemini-3.8-flash-tts returns wav only, its default. |
speakingRate |
number 0.25–2 |
1 |
Chirp 3: HD only. |
model |
string |
gemini-2.5-flash-tts |
Gemini only. Also gemini-2.5-pro-tts, gemini-3.1-flash-tts-preview, gemini-2.5-flash-lite-preview-tts, gemini-3.8-flash-tts. |
prompt |
string ≤ 1,000 chars |
— |
Gemini only. A natural-language style instruction. |
speakers |
array of 2 |
— |
Gemini only. Dialogue mode; replaces text. See below. |
turns |
array |
— |
Gemini only. Dialogue mode; replaces text. See below. |
Unknown fields are rejected, so a typo like speed returns an error instead of being silently ignored.
Dialogue (two speakers)
Send speakers and turns instead of text. Gemini-TTS only.
{
"engine": "gemini",
"prompt": "A relaxed conversation between two friends in a cafe.",
"speakers": [
{ "alias": "Marie", "voice": "Kore" },
{ "alias": "Paul", "voice": "Charon" }
],
"turns": [
{ "speaker": "Marie", "text": "Bonjour Paul !" },
{ "speaker": "Paul", "text": "Salut Marie." }
]
}
- Exactly two speakers. Three or more is rejected by Google with
"Multi-speaker synthesis requires two distinct speakers".
- Aliases must be alphanumeric with no spaces, must be unique, and every
turns[].speaker must name one of them.
voice is not used: each speaker carries its own.
- A dialogue is one Google request and is never split, because splitting it
would break speaker continuity. The combined turn text is therefore capped at
3,800 bytes (Google's limit is 4,000) and longer input returns
413.
On success, the response body is the audio (audio/mpeg, audio/wav or audio/ogg), with these headers:
X-Cache: HIT | MISS: whether Google was called.
X-TTS-Characters: number of characters synthesized.
X-TTS-Chunks: number of Google requests the text was split into.
Errors are returned as JSON: { "error": { "code": "...", "message": "..." } }.
| Status |
code |
Meaning |
| 400 |
invalid_request, invalid_json, unsupported_language, unknown_voice |
Fix the request. |
| 401 |
unauthorized |
Clips API: missing or wrong API key. |
| 403 |
turnstile_required, turnstile_failed |
Get a fresh Turnstile token. |
| 413 |
text_too_long, text_too_long_for_format, payload_too_large |
Shorten the text or use mp3/wav. |
| 422 |
synthesis_rejected |
Google refused the input. The message comes from Google, e.g. a sentence that is too long. |
| 429 |
rate_limited |
Per-IP (or per-key) limit hit. Respect Retry-After. |
| 429 |
quota_exceeded |
Clips API: the key's daily characters are spent; details.resetsAt says when they renew. |
| 500 |
server_misconfigured |
A secret is missing or invalid. Check the Worker logs. |