🔊 Text-to-Speech (TTS) - Guida Completa

✨ 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.

javascript
// 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

LanguageCodeFemale VoiceMale Voice
🇮🇹 Italianoitit-IT-Neural2-Ait-IT-Neural2-C
🇬🇧 Inglese (US)enen-US-Neural2-Fen-US-Neural2-D
🇩🇪 Tedescodede-DE-Neural2-Ade-DE-Neural2-B
🇫🇷 Francesefrfr-FR-Neural2-Afr-FR-Neural2-B
🇪🇸 Spagnoloeses-ES-Neural2-Aes-ES-Neural2-B
🇵🇹 Portoghese (BR)ptpt-BR-Neural2-Apt-BR-Neural2-B
🇳🇱 Olandesenlnl-NL-Neural2-Anl-NL-Neural2-B
🇵🇱 Polaccoplpl-PL-Neural2-Apl-PL-Neural2-B
🇷🇺 Russoruru-RU-Neural2-Aru-RU-Neural2-B
🇯🇵 Giapponesejaja-JP-Neural2-Bja-JP-Neural2-C
🇰🇷 Coreanokoko-KR-Neural2-Ako-KR-Neural2-C
🇨🇳 Cinesezhzh-CN-Neural2-Azh-CN-Neural2-B

🌍 Automatic browser language detection

To automatically detect the user's browser language:

javascript
// 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
javascript
// 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

javascript
// 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)