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


/ Published in: C#
Save to your folder(s)

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!


Copy this code and paste it in your HTML
  1. using System;
  2. using Ozeki.Media.MediaHandlers;
  3. using Ozeki.VoIP;
  4. using Ozeki.VoIP.SDK;
  5.  
  6. namespace Text_To_Speech
  7. {
  8. class Program
  9. {
  10. static ISoftPhone softphone;
  11. static IPhoneLine phoneLine;
  12. static IPhoneCall call;
  13. static MediaConnector connector;
  14. static PhoneCallAudioSender mediaSender;
  15.  
  16. private static void Main(string[] args)
  17. {
  18. softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000);
  19.  
  20. var registrationRequired = true;
  21. var userName = "858";
  22. var displayName = "858";
  23. var authenticationId = "858";
  24. var registerPassword = "858";
  25. var domainHost = "192.168.115.100";
  26. var domainPort = 5060;
  27.  
  28. var account = new SIPAccount(registrationRequired, displayName, userName, authenticationId, registerPassword, domainHost, domainPort);
  29.  
  30. RegisterAccount(account);
  31.  
  32. mediaSender = new PhoneCallAudioSender();
  33. connector = new MediaConnector();
  34.  
  35. Console.ReadLine();
  36. }
  37.  
  38. static void RegisterAccount(SIPAccount account)
  39. {
  40. try
  41. {
  42. phoneLine = softphone.CreatePhoneLine(account);
  43. phoneLine.RegistrationStateChanged += line_RegStateChanged;
  44. softphone.RegisterPhoneLine(phoneLine);
  45. }
  46. catch (Exception ex)
  47. {
  48. Console.WriteLine("Error during SIP registration: " + ex);
  49. }
  50. }
  51.  
  52. static void line_RegStateChanged(object sender, RegistrationStateChangedArgs e)
  53. {
  54. if (e.State == RegState.NotRegistered || e.State == RegState.Error)
  55. Console.WriteLine("Registration failed!");
  56.  
  57. if (e.State == RegState.RegistrationSucceeded)
  58. {
  59. Console.WriteLine("Registration succeeded - Online!");
  60. CreateCall();
  61. }
  62. }
  63.  
  64. private static void CreateCall()
  65. {
  66. var numberToDial = "853";
  67. call = softphone.CreateCallObject(phoneLine, numberToDial);
  68. call.CallStateChanged += call_CallStateChanged;
  69. call.Start();
  70. }
  71.  
  72. static void SetupTextToSpeech()
  73. {
  74. var textToSpeech = new TextToSpeech();
  75.  
  76. mediaSender.AttachToCall(call);
  77. connector.Connect(textToSpeech, mediaSender);
  78. textToSpeech.AddAndStartText("Hello World!");
  79.  
  80. Console.WriteLine("The text is converted to speech and being played into the call.");
  81. }
  82.  
  83. static void call_CallStateChanged(object sender, CallStateChangedArgs e)
  84. {
  85. Console.WriteLine("Call state: {0}.", e.State);
  86.  
  87. if (e.State == CallState.Answered)
  88. SetupTextToSpeech();
  89. }
  90. }
  91. }

URL: http://www.voip-sip-sdk.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.