Home > Code > C# > How to implement text-to-speech during a SIP voice call in C#.NET?

How to implement text-to-speech during a SIP voice call in C#.NET?

by Justin Warner   on Feb 26, 2015   Category: C#   |  Views: 509    |  Points: 25   |  Starter 

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: http://www.voip-sip-sdk.com)

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!


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 = "5555";
var displayName = "5555";
var authenticationId = "5555";
var registerPassword = "5555";
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)

Post Code  |  Code Snippet Home

User Responses

No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts