Skip to content

Integrate Edge TTS as Primary TTS Provider (Free, No API Key) #280

Description

@Olbrasoft

Summary

Replace Azure TTS as the primary TTS provider with Edge TTS, which is completely free and requires no API key. Keep Azure TTS as a fallback option.

Motivation

Current problems with Azure TTS:

  • Rate limiting: 467 calls in 24 hours resulted in 29 HTTP 429 errors (6% failure rate)
  • API quota limits on Free F0 tier (20 requests/minute)
  • Requires API key management and monitoring

Edge TTS advantages:

  • 100% FREE - no API key required
  • No rate limits (or very high limits)
  • Same voice quality - uses Microsoft's neural voices
  • Czech language support - cs-CZ-AntoninNeural (male), cs-CZ-VlastaNeural (female)
  • Proven reliability - used by Microsoft Edge browser internally

Tested Implementation

Python Library (Proof of Concept)

Successfully tested with edge-tts Python library (version 7.2.7):

# Installation
pip install edge-tts

# Test command that works
edge-tts --voice cs-CZ-AntoninNeural \
  --text "Dobrý den, toto je Edge TTS." \
  --write-media output.mp3

# List available voices
edge-tts --list-voices | grep cs-CZ

Test results:

  • File size: 31 KB (short text), 68 KB (longer text)
  • Quality: 24 kHz, 48 kbps MP3
  • Generation time: ~2-3 seconds
  • No errors, 100% success rate

Available Czech voices:

  • cs-CZ-AntoninNeural - Male voice (tested ✅)
  • cs-CZ-VlastaNeural - Female voice (tested ✅)

C# Implementation (.NET)

Recommended NuGet package: EdgeTtsSharp version 2.0.6

Installation:

dotnet add package EdgeTtsSharp

GitHub repository: https://github.com/niker/EdgeTtsSharp
NuGet package: https://www.nuget.org/packages/EdgeTtsSharp

Example code (from EdgeTtsSharp documentation):

using EdgeTtsSharp;

// Get Czech male voice
var voice = await EdgeTts.GetVoice("cs-CZ-AntoninNeural");

// Option 1: Get audio stream (starts immediately)
await using var stream = voice.GetAudioStream("Text k přečtení");
// Use stream for playback or further processing

// Option 2: Save directly to file
await voice.SaveAudioToFile("Text k přečtení", "/path/to/output.mp3");

// Option 3: Stream to HTTP response (for API endpoints)
await voice.StreamText(Response.Body, "Text k přečtení");

// Get all available voices
var allVoices = await EdgeTts.GetVoices();

// Get specific voice by short name
var czechVoice = await EdgeTts.GetVoice("cs-CZ-AntoninNeural");

Key features:

  • Real-time streaming (starts playback on first audio packet)
  • Cross-platform (.NET Standard 2.1)
  • Async/await support
  • No external dependencies for core functionality

Proposed Implementation

Architecture Changes

Current TTS priority:

  1. Azure TTS (primary)
  2. Fallback TTS

Proposed TTS priority:

  1. Edge TTS (primary) ← NEW
  2. Azure TTS (fallback - when Edge TTS fails)
  3. Other fallback TTS

Implementation Steps

  1. Add NuGet package:

    cd ~/Olbrasoft/VirtualAssistant/src/VirtualAssistant.Voice
    dotnet add package EdgeTtsSharp
  2. Create EdgeTtsProvider class (similar to existing Azure TTS provider):

    • Implement ITtsProvider interface
    • Use EdgeTts.GetVoice("cs-CZ-AntoninNeural")
    • Return audio stream or save to file
  3. Update TTS service configuration:

    • Register EdgeTtsProvider as primary
    • Configure voice selection (AntoninNeural for male, VlastaNeural for female)
    • Keep Azure TTS configuration for fallback
  4. Add error handling:

    • Catch EdgeTTS exceptions
    • Automatically fall back to Azure TTS on failure
    • Log TTS provider used for monitoring
  5. Configuration (appsettings.json):

    {
      "Tts": {
        "PrimaryProvider": "EdgeTts",
        "EdgeTts": {
          "DefaultVoice": "cs-CZ-AntoninNeural",
          "FemaleVoice": "cs-CZ-VlastaNeural",
          "MaleVoice": "cs-CZ-AntoninNeural"
        },
        "FallbackProvider": "AzureTts",
        "AzureTts": {
          "Key": "...",
          "Region": "westeurope",
          "Voice": "cs-CZ-AntoninNeural"
        }
      }
    }

Testing Checklist

  • EdgeTtsSharp package installed
  • EdgeTtsProvider class created
  • TTS service updated to use Edge TTS as primary
  • Azure TTS fallback working correctly
  • Czech male voice (AntoninNeural) tested
  • Czech female voice (VlastaNeural) tested
  • Error handling and logging implemented
  • Integration tests pass
  • Deployment to production

Expected Benefits

  1. Cost savings: Zero API costs vs current Azure F0 tier limits
  2. Reliability: No rate limit errors (29 errors eliminated)
  3. Performance: Same or better generation speed
  4. Quality: Identical neural voice quality
  5. Simplicity: No API key management needed

References

Notes

Edge TTS uses Microsoft's internal TTS service (same as Microsoft Edge browser's "Read Aloud" feature). It's legitimate and free to use, but Microsoft may implement rate limiting in the future. Having Azure TTS as fallback ensures reliability.

Tested on: Debian 13 (Trixie), .NET 10, Python 3.13
Library version: edge-tts 7.2.7 (Python), EdgeTtsSharp 2.0.6 (C#)
Test date: 2024-12-14

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions