2024.03.08
Building a Nepali Text-to-Speech API.
Wrapping Google Cloud TTS and a custom Nepali voice model into a simple REST API for production use.
TTSAPINepalNode.js
Nepali TTS is a solved problem in theory — Google Cloud TTS has a standard Nepali voice — but getting it into a production API that handles Unicode correctly and doesn't break on mixed script input takes some work.
Setup
npm init -y
npm install express @google-cloud/text-to-speech dotenv
# Set GOOGLE_APPLICATION_CREDENTIALS in .envCore TTS endpoint
const textToSpeech = require('@google-cloud/text-to-speech');
const client = new textToSpeech.TextToSpeechClient();
app.post('/synthesize', async (req, res) => {
const { text } = req.body;
const [response] = await client.synthesizeSpeech({
input: { text },
voice: { languageCode: 'ne-NP', ssmlGender: 'FEMALE' },
audioConfig: { audioEncoding: 'MP3' },
});
res.set('Content-Type', 'audio/mpeg');
res.send(response.audioContent);
});Gotchas with Nepali Unicode
- Devanagari text must be NFC-normalized before synthesis
- Mixed Nepali-English input confuses the language model — split and synthesize separately
- Some conjunct characters render incorrectly in older Google voice models
- The 'ne-NP' voice doesn't support SSML — keep input as plain text
What this API powers
- →Offline pronunciation guides for Nepali vocabulary apps
- →Voice notifications for IoT sensor alerts in rural areas
- →Read-aloud mode for a Nepali news aggregator
- →Accessibility layer for forms in government portals
For higher-quality output, OpenAI TTS can be used with Romanized Nepali — the transcription quality is better, though it loses the native accent. For a native voice, Google remains the practical choice as of 2024.