info@voip-sip-sdk.com Tel: +36 1 371 0150

How to convert Text to Speech
using C#

Download: TextToSpeech.zip

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.


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.

Pulse-code modulation (PCM) is a method, used to digitally represent sampled analog signals. In this example, PCM means the uncompressed form of digital audio data. This example is using the G.711 waveform codec, which is an ITU-T standard for audio companding, and it is primarily used in telephony and to communicate over IP networks. It is a lossless, encoded data (to about 50%), which has got 64 kbit/s transfer rate.

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 = "192.168.115.100";
            var domainPort = 5060;

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

            // Send SIP regitration request
            RegisterAccount(account);

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

            // Prevents the termination of the application
            Console.ReadLine();
        }

        static void RegisterAccount(SIPAccount account)
        {
            try
            {
                phoneLine = softphone.CreatePhoneLine(account);
                phoneLine.RegistrationStateChanged += line_RegStateChanged;
                softphone.RegisterPhoneLine(phoneLine);
            }
            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!");
                CreateCall();
            }
        }

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

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

            mediaSender.AttachToCall(call);
            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)
                SetupTextToSpeech();
        }
    }

}

Training guides, simple examples

If you would like to visit the detailed developer documentations, you can learn much more from the Ozeki VoIP SIP SDK Training chapter.


If you have any questions or need assistance, please contact us at info@voip-sip-sdk.com

Related Pages

Ozeki Cookie Policy
Ozeki Informatics Ltd uses cookies to provide you the best experience on this website. The further use of the website will be considered as an agreement to the use of cookies. For more information read this website.

Cookies are enabled You are browsing the optimized version of this website. For more information read this website.