✨ TEXT-TO-SPEECH WITH GOOGLE NEURAL2 VOICES
Generate natural audio from text using Google neural voices. Supports 12 languages, voice gender choice (male/female), and automatic language detection from browser.
📖 How TTS works
Send a request to the API with the prefix TTS: in the field prompt.
// Formato base
const response = await fetch("https://api-neuralai.com/speech", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({⚙️ Configuration parameters
👩 Female Voice (default)
prompt: "TTS:Hello!|voice:female"Neural2 Voices: it-IT-Neural2-A, en-US-Neural2-F, de-DE-Neural2-A, etc.
👨 Male Voice
prompt: "TTS:Hello!|voice:male"Neural2 Voices: it-IT-Neural2-C, en-US-Neural2-D, de-DE-Neural2-B, etc.
📋 Available languages and voices
| Language | Code | Female Voice | Male Voice |
|---|---|---|---|
| 🇮🇹 Italiano | it | it-IT-Neural2-A | it-IT-Neural2-C |
| 🇬🇧 Inglese (US) | en | en-US-Neural2-F | en-US-Neural2-D |
| 🇩🇪 Tedesco | de | de-DE-Neural2-A | de-DE-Neural2-B |
| 🇫🇷 Francese | fr | fr-FR-Neural2-A | fr-FR-Neural2-B |
| 🇪🇸 Spagnolo | es | es-ES-Neural2-A | es-ES-Neural2-B |
| 🇵🇹 Portoghese (BR) | pt | pt-BR-Neural2-A | pt-BR-Neural2-B |
| 🇳🇱 Olandese | nl | nl-NL-Neural2-A | nl-NL-Neural2-B |
| 🇵🇱 Polacco | pl | pl-PL-Neural2-A | pl-PL-Neural2-B |
| 🇷🇺 Russo | ru | ru-RU-Neural2-A | ru-RU-Neural2-B |
| 🇯🇵 Giapponese | ja | ja-JP-Neural2-B | ja-JP-Neural2-C |
| 🇰🇷 Coreano | ko | ko-KR-Neural2-A | ko-KR-Neural2-C |
| 🇨🇳 Cinese | zh | zh-CN-Neural2-A | zh-CN-Neural2-B |
🌍 Automatic browser language detection
To automatically detect the user's browser language:
// Rileva lingua del browser
const browserLang = navigator.language || navigator.userLanguage; // es: "it-IT", "en-US"
const langCode = browserLang.split('-')[0].toLowerCase(); // estrae "it", "en", etc.
// Mappa lingue supportate⚠️ IMPORTANT: Strip Markdown for Text
Problem: If you send TTS text with markdown formatting (e.g. AI responses, documents, articles), the voice will also read the formatting symbols, making the audio incomprehensible.
❌ What would happen WITHOUT strip markdown:
Text: "# Titolo\nQuesto è un **testo** con \`codice\` e [link](url)"
Audio: "Diesis Titolo... Questo è un asterisco asterisco testo asterisco asterisco con backtick codice backtick e parentesi quadra link parentesi tonda url parentesi tonda"
✅ What happens WITH strip markdown:
Clean text: "Titolo. Questo è un testo con codice e link"
Audio: "Titolo. Questo è un testo con codice e link" ✓
What the stripForTTS() function removes:
- Code blocks (\`\`\`...\`\`\`) and inline code (\`code\`)
- Heading (# ## ### ####)
- Bold (**text** or __text__)
- Italic (*text* or _text_)
- Strikethrough (~~text~~)
- Markdown links ([text](url)) → keeps only the text
- Bullet lists (- * +) and numbered lists (1. 2. 3.)
- Quotes (> text)
- Separators (--- ===)
- HTML tags (<div>...</div>)
- HTML entities (& < > ")
- Multiple newlines → converted to ". " (dots)
- Multiple spaces → single space
// Funzione COMPLETA per pulire il testo prima del TTS
function stripForTTS(raw) {
const bt = String.fromCharCode(96); // backtick \`
const codeBlockRegex = new RegExp(bt + bt + bt + '[\\s\\S]*?' + bt + bt + bt, 'g');
const inlineCodeRegex = new RegExp(bt + '(.+?)' + bt, 'g');💡 When to use stripForTTS():
- When reading AI responses aloud (that contain markdown)
- When converting articles or documents to audio
- When creating audiobooks from web content
- When generating podcasts from blog posts
- Always before sending text to TTS, unless it's already plain text
💰 TTS Token Costs:
1 token per 300 characters of clean text (after strip markdown).Minimum 1 token per request.
Example: 900 characters = 3 tokens | 3000 characters = 10 tokens
📚 Practical examples
// Esempio 1: Italiano, voce femminile (default)
const response1 = await fetch("https://api-neuralai.com/speech", {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({💡 Best Practices:
- Save the user's voice preference in localStorage
- Use strip markdown before every TTS call
- For long texts (>5000 characters), divide into separate chunks
- Show a loading indicator during audio generation
- Handle errors with visual feedback to the user
- Download audio URLs for permanent use (they expire after 1 hour)