Speech in a web app
Keep your key on the server: a small route forwards the request to Alania and streams the audio back, and the browser starts playing on the first chunk.
- Add a
/api/speakroute on your server. It forwards the request to Alania withstream: trueand passes the response straight back. - In the browser,
speak(text)reads the WAV header, then queues each PCM chunk with Web Audio. - Wire it to a button. The first audio arrives about 190 ms after the request (depending on your network).
1 · The server route
The key lives in the PATIENTDESK_API_KEY environment variable and never reaches the browser. The route passes the response through without buffering it, so audio reaches the browser while the rest of the sentence is still being made.
2 · Playing in the browser
The response starts with a 44-byte WAV header; the sample rate is at byte 24 (48,000 on Alania-2). After that it is raw PCM16. Network chunks can split a sample, so a leftover byte is carried over to the next chunk.
speak.js
3 · Wiring a button
Browsers only play audio after a user gesture, so start speak from a click.
React
In production
- If you speak the same sentences again and again (greetings, menus), render them with a
seedand cache the file: the same seed, text and settings give the same audio. - Use the pronunciation dictionary for brand and person names; your route does not change.
- If a voice agent says many sentences in a row, one WebSocket connection from your server is more efficient.
- Tell listeners the audio is AI-generated; every response carries
x-disclosure: ai-generated.