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

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: 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.

About the Author

Source Code

Important Note:
This script has not been checked by Spiceworks. Please understand the risks before using it.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

usingSystem;usingOzeki.Media.MediaHandlers;usingOzeki.VoIP;usingOzeki.VoIP.SDK;namespaceText_To_Speech{classProgram{staticISoftPhonesoftphone;staticIPhoneLinephoneLine;staticIPhoneCallcall;staticMediaConnectorconnector;staticPhoneCallAudioSendermediaSender;privatestaticvoidMain(string[]args){softphone=SoftPhoneFactory.CreateSoftPhone(5000,10000);varregistrationRequired=true;varuserName="5555";vardisplayName="5555";varauthenticationId="5555";varregisterPassword="5555";vardomainHost="192.168.117.120";vardomainPort=5060;varaccount=newSIPAccount(registrationRequired,displayName,userName,authenticationId,registerPassword,domainHost,domainPort);RegisterAccount(account);mediaSender=newPhoneCallAudioSender();connector=newMediaConnector();Console.ReadLine();}staticvoidRegisterAccount(SIPAccountaccount){try{phoneLine=softphone.CreatePhoneLine(account);phoneLine.RegistrationStateChanged+=line_RegStateChanged;softphone.RegisterPhoneLine(phoneLine);}catch(Exceptionex){Console.WriteLine("Error during SIP registration: "+ex);}}staticvoidline_RegStateChanged(objectsender,RegistrationStateChangedArgse){if(e.State==RegState.NotRegistered||e.State==RegState.Error)Console.WriteLine("Registration failed!");if(e.State==RegState.RegistrationSucceeded){Console.WriteLine("Registration succeeded - Online!");CreateCall();}}privatestaticvoidCreateCall(){varnumberToDial="853";call=softphone.CreateCallObject(phoneLine,numberToDial);call.CallStateChanged+=call_CallStateChanged;call.Start();}staticvoidSetupTextToSpeech(){vartextToSpeech=newTextToSpeech();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.");}staticvoidcall_CallStateChanged(objectsender,CallStateChangedArgse){Console.WriteLine("Call state: {0}.",e.State);if(e.State==CallState.Answered)SetupTextToSpeech();}}}