How to convert Text to Speech
using C#


This example demonstrates how to implement the text-to-speech feature in c#, which is able to convert text messages to audio data. To fully understand this article, you might have to study the How to register to a SIP PBX chapter and the How to ring a SIP extension chapters first.
To use this example, you need to have Ozeki VoIP SIP SDK installed, and a reference to ozeki.dll should be added to your visual studio project.

text to speech conversion
Figure 1 - Text to Speech conversion

What is Text-To-Speech used for during a SIP voice call? How does it work?

A text-to-speech (TTS) system converts normal language text into speech; other systems render symbolic linguistic representation like phonetic transcriptions into speech.
Users can set text files to be read and sent into the call as speech, which is a very useful feature for example at voicemails, IVRs (Interactive Voice Response), autodialers etc.

Text-to-Speech refers to the ability of computers to read text aloud. A TTS Engine converts written text to a phonemic representation, then converts the phonemic representation to waveforms that can be output as sound. TTS engines with different languages, dialects and specialized vocabularies are available through third-party publishers.

How to implement text-to-speech feature using C#?

Ozeki VoIP SIP SDK provides the c# TextToSpeech class for the purpose to create instances which are able to convert text - which is (or it's path is) given as parameter - to voice data. This instance can be attached to the call through the correct sender object, so the stream of voice data can be be sent to the destination.
The example below introduces how to convert a text message into voice, and play that on the default speaker.

Read and send text message as voice data example in C#

´╗┐using System;
using Ozeki.Media;
using Ozeki.VoIP;

namespace Text_To_Speech
    class Program
        static ISoftPhone softphone;   // softphone object
        static IPhoneLine phoneLine;   // phoneline object
        static IPhoneCall call;
        static MediaConnector connector;
        static PhoneCallAudioSender mediaSender;

        private static void Main(string[] args)
            // Create a softphone object with RTP port range 5000-10000
            softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000);

            // SIP account registration data, (supplied by your VoIP service provider)
            var registrationRequired = true;
            var userName = "858";
            var displayName = "858";
            var authenticationId = "858";
            var registerPassword = "858";
            var domainHost = "";
            var domainPort = 5060;

            var account = new SIPAccount(registrationRequired, displayName, userName, authenticationId, registerPassword, domainHost, domainPort);

            // Send SIP regitration request

            mediaSender = new PhoneCallAudioSender();
            connector = new MediaConnector();

            // Prevents the termination of the application

        static void RegisterAccount(SIPAccount account)
                phoneLine = softphone.CreatePhoneLine(account);
                phoneLine.RegistrationStateChanged += line_RegStateChanged;
            catch (Exception ex)
                Console.WriteLine("Error during SIP registration: " + ex);

        static void line_RegStateChanged(object sender, RegistrationStateChangedArgs e)
            if (e.State == RegState.NotRegistered || e.State == RegState.Error)
                Console.WriteLine("Registration failed!");

            if (e.State == RegState.RegistrationSucceeded)
                Console.WriteLine("Registration succeeded - Online!");

        private static void CreateCall()
            var numberToDial = "853";
            call = softphone.CreateCallObject(phoneLine, numberToDial);
            call.CallStateChanged += call_CallStateChanged;

        static void SetupTextToSpeech()
            var textToSpeech = new TextToSpeech();

            connector.Connect(textToSpeech, mediaSender);   
            textToSpeech.AddAndStartText("Hello World!");

            Console.WriteLine("The text is converted to speech and being played into the call.");

        static void call_CallStateChanged(object sender, CallStateChangedArgs e)
            Console.WriteLine("Call state: {0}.", e.State);

            if (e.State == CallState.Answered)


Related Pages