How to convert Text to Speech
using C#
![]() |
Download: | Text_To_Speech.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.
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 = "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();
}
}
}
Wireshark communication
Wireshark captures the SIP signaling and RTP media used in the Text-to-Speech call. SIP INVITE, 100 Trying, 180 Ringing, 200 OK, ACK, and BYE messages establish and end the call, while RTP packets carry the TTS-generated “Hello World!” audio from extension 858 to extension 853.
SIP INVITE PDU
INVITE sip:853@192.168.115.100 SIP/2.0 Via: SIP/2.0/UDP 192.168.115.20:5060;branch=z9hG4bK-tts-858-01 Max-Forwards: 70 From: <sip:858@192.168.115.100>;tag=858-tts-tag-1 To: <sip:853@192.168.115.100> Call-ID: tts-858-853-0001 CSeq: 1 INVITE Contact: <sip:858@192.168.115.20:5060> Content-Type: application/sdp Content-Length: 191 v=0 o=858 2890844526 2890844526 IN IP4 192.168.115.20 s=Text to Speech Call c=IN IP4 192.168.115.20 t=0 0 m=audio 5200 RTP/AVP 0 a=rtpmap:0 PCMU/8000 a=sendrecv
RTP PDU
UDP header Source port: 5200 Destination port: 6000 Length: 188 bytes Checksum: calculated RTP header (12 bytes) 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |V=2|P|X| CC=0 |M| PT=0 | sequence number=1001 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | timestamp=160000 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | synchronization source (SSRC)=0x3C4D5E6F | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ RTP payload (160 bytes) G.711 mu-law encoded audio generated from the text "Hello World!"
