Skip to content

EdgeTTS.DotNet is a C# (.NET) library that allows you to use Microsoft Edge's online text-to-speech service. It is a feature-complete migration of the popular Python edge-tts library, designed for performance, cross-platform compatibility, and ease of use.

License

Notifications You must be signed in to change notification settings

twn39/EdgeTTS.DotNet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EdgeTTS.DotNet

NuGet Version NuGet Downloads Build Status License: MIT .NET Platform

EdgeTTS.DotNet is a C# (.NET) library that allows you to use Microsoft Edge's online text-to-speech service. It is a feature-complete migration of the popular Python edge-tts library, designed for performance, cross-platform compatibility, and ease of use.

Features

  • High-Quality Speech: Access Microsoft Edge's neural TTS voices for natural-sounding speech.
  • Multilingual Support: Supports over 400 voices across numerous languages and regions.
  • Subtitles: Generate SRT formatted subtitles from WordBoundary or SentenceBoundary events.
  • Customizable Prosody: Adjust speech rate, volume, and pitch to suit your needs.
  • Cross-Platform: Built with .NET 10.0, fully compatible with Windows, macOS, Linux, and mobile platforms like .NET MAUI.
  • Robustness: Includes built-in clock skew correction (DRM), automatic 403 retry, WebSocket compression, and comprehensive error handling.
  • Voice Metadata: Access voice tags including ContentCategories and VoicePersonalities.

Installation

dotnet add package EdgeTTS.DotNet

Quick Start

Basic Usage

Save text to an MP3 file:

using EdgeTTS.DotNet;

var request = new Communicate("Hello, world!", voice: "en-US-EmmaMultilingualNeural");
await request.SaveAsync("hello.mp3");

Generating Subtitles

Use the SubMaker class to create subtitles:

using EdgeTTS.DotNet;
using EdgeTTS.DotNet.Models;

var communicate = new Communicate("Hello world!", rate: "+10%", boundaryType: "WordBoundary");
var subMaker = new SubMaker();

await foreach (var chunk in communicate.StreamAsync())
{
    if (chunk is AudioChunk audio)
    {
        // Handle audio data
    }
    else if (chunk is MetadataChunk metadata)
    {
        subMaker.Feed(metadata);
    }
}

string srt = subMaker.GetSrt();
File.WriteAllText("hello.srt", srt);

Listing Available Voices

using EdgeTTS.DotNet;

var voices = await Voices.ListVoicesAsync();
foreach (var voice in voices)
{
    Console.WriteLine($"{voice.ShortName} ({voice.Gender}) - {voice.Locale}");
    Console.WriteLine($"  Categories: {string.Join(", ", voice.VoiceTag.ContentCategories)}");
    Console.WriteLine($"  Personalities: {string.Join(", ", voice.VoiceTag.VoicePersonalities)}");
}

Advanced Options

Option Description Format
voice The short name of the voice (e.g., en-US-EmmaMultilingualNeural) String
rate The speed of the speech +0%, -50%, etc.
volume The volume of the speech +0%, -25%, etc.
pitch The pitch of the speech +0Hz, -5Hz, etc.
boundaryType Type of metadata events to receive SentenceBoundary (default) or WordBoundary
proxy Proxy server for network requests http://proxy:port

Error Handling

The library provides specific exception types for robust error handling:

Exception Description
NoAudioReceivedException No audio was received from the service
UnexpectedResponseException Unexpected response format from the server
UnknownResponseException Unknown response type received
WebSocketException WebSocket connection or communication error
SkewAdjustmentException Clock skew correction failed (missing server date)

Testing

The project includes a comprehensive test suite using xUnit:

dotnet test

License

This project is licensed under the MIT License. See the Python project edge-tts for more details on the original implementation.

About

EdgeTTS.DotNet is a C# (.NET) library that allows you to use Microsoft Edge's online text-to-speech service. It is a feature-complete migration of the popular Python edge-tts library, designed for performance, cross-platform compatibility, and ease of use.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages