Click here to Skip to main content
15,879,095 members
Articles / Programming Languages / C#
Tip/Trick

Implementing Text-to-Speech in C# using System.Speech.Synthesis

Rate me:
Please Sign up or sign in to vote.
3.82/5 (6 votes)
16 Mar 2023CPOL2 min read 18.4K   16   10
How to implement a text-to-speech (TTS) application in C# using the System.Speech.Synthesis namespace
This article provides a step-by-step guide on implementing a simple text-to-speech (TTS) application using C# and the System.Speech.Synthesis namespace. It explains the code used, including creating a SpeechSynthesizer instance and synthesizing speech from text, and offers suggestions for exploring more advanced TTS features.

Introduction

This tip explains how to implement a text-to-speech (TTS) application in C# using the System.Speech.Synthesis namespace. TTS technology has many practical use cases, such as in accessibility tools and speech-enabled applications. By following the step-by-step guide provided, readers will be able to create a simple console application that synthesizes speech from text. The article also provides an explanation of the code used and offers suggestions for exploring more advanced features of the SpeechSynthesizer class.

Implementing Text-to-Speech in C# using System.Speech.Synthesis

Text-to-speech (TTS) technology has been around for a while and has found many use cases, such as in language learning, accessibility tools for visually impaired individuals, and in speech-enabled applications. In this article, we will explore how to implement a simple TTS application using C# and the System.Speech.Synthesis namespace.

Prerequisites

Before we begin, you need to have the following installed on your machine:

  1. .NET Framework 4.6.1 or higher
  2. Visual Studio 2017 or higher

Implementation

We will be using the System.Speech.Synthesis namespace, which provides classes for synthesizing speech from text. Follow the steps below to create a console application in C# and implement TTS.

  1. Open Visual Studio and create a new Console Application project.
  2. Add a reference to the System.Speech assembly. Right-click on the project in Solution Explorer, select Add Reference, and then choose System.Speech from the list of assemblies.
  3. In the Program.cs file, add the following code:
C#
using System;
using System.IO;
using System.Speech.Synthesis;

class Program
{
    static void Main(string[] args)
    {
        // Basic TTS
        SpeechSynthesizer synth = new SpeechSynthesizer();
        synth.SetOutputToDefaultAudioDevice();
        synth.Speak("Hello, world!");

        // Changing the Voice
        synth.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Adult);
        synth.Speak("Hello, I am a female voice!");

        // Changing the Pitch and Rate
        synth.Rate = -2;
        synth.Volume = 100;
        synth.Speak("Hello, I am speaking slower and louder!");

        // Pausing and Resuming Speech
        synth.Speak("Hello, I will pause for 3 seconds now.");
        synth.Pause();
        System.Threading.Thread.Sleep(3000); // wait for 3 seconds
        synth.Resume();
        synth.Speak("I am back!");

        // Saving Speech to a WAV File
        synth.SetOutputToWaveFile("output.wav");
        synth.Speak("Hello, I am saving my speech to a WAV file!");

        // Setting the Speech Stream
        MemoryStream stream = new MemoryStream();
        synth.SetOutputToWaveStream(stream);
        synth.Speak("Hello, I am being streamed to a memory stream!");
        byte[] speechBytes = stream.GetBuffer();

        // Changing the Voice and Pronunciation
        PromptBuilder builder = new PromptBuilder();
        builder.StartVoice(VoiceGender.Female, VoiceAge.Adult, 1);
        builder.AppendText("Hello, my name is Emily.");
        builder.StartVoice(VoiceGender.Female, VoiceAge.Teen, 2);
        builder.AppendText("I am from New York City.");
        builder.StartStyle(new PromptStyle() { Emphasis = PromptEmphasis.Strong });
        builder.AppendText("I really love chocolate!");
        builder.EndStyle();
        builder.StartStyle(new PromptStyle() { Emphasis = PromptEmphasis.Reduced });
        builder.AppendText("But I'm allergic to it...");
        builder.EndStyle();
        synth.Speak(builder);

        Console.ReadLine();
    }
}

Code Outline

  1. Basic TTS

    Creates a SpeechSynthesizer instance and synthesizes the text "Hello, world!" using the default audio device.

  2. Changing the Voice

    Selects a female adult voice and synthesizes the text "Hello, I am a female voice!" using that voice.

  3. Changing the Pitch and Rate

    Sets the speech rate to -2 (slower) and the volume to 100 (louder), and synthesizes the text "Hello, I am speaking slower and louder!".

  4. Pausing and Resuming Speech

    Synthesizes the text "Hello, I will pause for 3 seconds now.", pauses the speech for 3 seconds, and then resumes the speech and synthesizes the text "I am back!".

  5. Saving Speech to a WAV File

    Sets the output of the SpeechSynthesizer to a WAV file named "output.wav", and synthesizes the text "Hello, I am saving my speech to a WAV file!".

  6. Setting the Speech Stream

    Sets the output of the SpeechSynthesizer to a memory stream, synthesizes the text "Hello, I am being streamed to a memory stream!", and gets the resulting speech bytes from the memory stream.

  7. Changing the Voice and Pronunciation

    Uses the PromptBuilder class to create a more complex prompt, changing the voice for certain parts of the prompt, and adding emphasis and reduced emphasis to certain parts of the prompt. The resulting prompt is then synthesized using the SpeechSynthesizer.

These code examples demonstrate some of the basic and advanced functionality of the SpeechSynthesizer class, including changing the voice and pitch, pausing and resuming speech, and saving synthesized speech to a file or memory stream.

History

  • 16th March, 2023: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
As an Artificial Intelligence Engineer with over 15 years of experience, I excel in innovating, designing, and developing state-of-the-art technology. My expertise lies in coding complex algorithms, and engineering robots to automate tasks with precision and efficiency. I'm a passionate coder who is always exploring cutting-edge technologies and pushing the boundaries of what's possible.

Comments and Discussions

 
QuestionDifferent languages within the same project Pin
mmm-it7-Dec-23 23:20
mmm-it7-Dec-23 23:20 
GeneralMy vote of 2 Pin
Porpentine20-Mar-23 20:50
Porpentine20-Mar-23 20:50 
QuestionVery Interesting ... but ... Pin
victorbos20-Mar-23 6:49
victorbos20-Mar-23 6:49 
QuestionCode had an error...you didn't "EndVoice()" after the "StartVoice()" calls Pin
SBGTrading18-Mar-23 11:33
SBGTrading18-Mar-23 11:33 
GeneralMy vote of 4 Pin
Member 1370414317-Mar-23 2:38
Member 1370414317-Mar-23 2:38 
QuestionNice work Pin
Mike Hankey16-Mar-23 11:20
mveMike Hankey16-Mar-23 11:20 
AnswerRe: Nice work Pin
Mar.And17-Mar-23 22:23
Mar.And17-Mar-23 22:23 
QuestionEdge's Read Aloud Pin
dandy7216-Mar-23 9:52
dandy7216-Mar-23 9:52 
SuggestionRe: Edge's Read Aloud Pin
Daniele Rota Nodari17-Mar-23 1:00
Daniele Rota Nodari17-Mar-23 1:00 
GeneralRe: Edge's Read Aloud Pin
dandy7218-Mar-23 4:25
dandy7218-Mar-23 4:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.