Return to Snippet

Revision: 68802
at February 27, 2015 01:14 by warnerBro19

Initial Code
using System;
using Ozeki.Media.MediaHandlers;
using Ozeki.VoIP;
using Ozeki.VoIP.SDK;

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

        private static void Main(string[] args)
            softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000);

            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);


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


        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)

Initial URL

Initial Description
I have heard about this solution on the Facebook, and I thought it is worth to share my source code to help other developers interested in converting text to speech using C#. Text-to-speech refers to the ability of computers to read txt aloud. This functionality can be greatly used during SIP communication in autodialer or IVR systems. The source code below is ready for use, so you only need to copy&paste it to your Visual Studio, then modify the necessary fields. (Do not forget to add the necessary DLL file providing the VoIP background to your references: 

This solution assumes that you have a PBX with some SIP extensions installed previously. After creating the necessary using media handler objects, you need to define your PBX and provide the appropriate SIP account details in order to be able to register your application to the phone system. When you have created all the required methods for SIP calling, you can implement the text-to-speech  feature by using the SetupTextToSpeech() method.

Have a good time!

Initial Title
How to implement text-to-speech during a SIP voice call in C#?

Initial Tags
phone, text, convert, c#

Initial Language