Adding Voice to a VB.Net application
In this article I will explain how to add voice to a VB.Net application. We will make the computer read the text entered in a text box. In the next application, we will input some numbers in a text box, the application will convert it into corresponding words as money amount and then will read it.
Introduction
We see many applications developed on .Net platform but nearly most of them are mute. The actions we perform are never spoken by the computer. So today lets learn how to add voice to a VB.Net application.Getting Started
I am going to explain this concept practically using two different applications. Application 1 will be learning the know-how and application 2 will be advanced implementation.
The core of both the applications is their ability to speak by reading the given text. Hence let's prepare the core first which will be useful in both the examples.Core of the Applications
Let's design the core first to add voice to our VB.Net application and make them read the input text.
To make this functioning we require the following two packages.
System.Speech
SpeechLib
The next step is creating an object of SpVoice class and using its Speak function to make the application read the input text.
Steps for making the Core
1. In the Project Menu -> Add Reference -> Under .Net tab -> System.Speech -> OK
2. Again in the Project Menu -> Add Reference -> Under COM tab -> Microsoft Speech Object Library -> OK
3. In the Solution Explorer Right Click on the project name - > Add ->
New Item -> Select Class and give it name as clsSpeak.vb -> OK
4. In the class import following packages and add a ReadText function as follows.
Imports SpeechLib
Imports System.Speech
Public Class clsSpeak
Public Shared Function ReadText(ByVal text As String)
Dim sv As New SpVoice
sv.Speak(text, SpeechVoiceSpeakFlags.SVSFlagsAsync)
End Function
End Class
The ReadText() function takes an string argument text and passes it to the Speak() function of sv object of SpVoice class. The Speak() function makes the application read the text.
Now the Core for both the applications is ready. We will use this class in both the example applications.Application 1
This application will show you a simple way of making the computer read whatever you enter in a text box.
Step 1:
Make the core as explained above.
Step 2:
On the form take a text box (txtData) and a button (btnReader).
Step 3:
On the Button's click write the following code.
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
clsSpeak.ReadText(txtData.Text)
End Sub
Note:As ReadText() is a shared function, we do not need to create object of clsSpeak class to access this method.
Step 4:
Now the application is ready and time to test it. Run the application, enter Hello World in the text box and click on the read button (Keep the speakers on). You will hear a voice saying "Hello World". Enter some other text in the text box and make the application read it.
You can download Voice Application Example 1 attached at the end of this article for your reference.
So we have learned the know-how of a voice application with Application 1.
Now let's make one more interesting and complex application.Application 2
In this application we will input some numbers in a text box, the application will convert it into corresponding words as money amount and then will read it.
Step 1:
Make the core as explained in the beginning or if you have already created it; you can add the class into this application.
Step 2:
This step is to make a class with a functions to convert a number into money amount.
e.g.
10025.5 will be converted to Ten Thousand Twenty Five Rupees and Fifty Paise only.
In the Solution Explorer Right Click on the project name - > Add -> New Item -> Select Class and give it name as clsNumToWord.vb -> OK
The Num2WordConverter() function of clsNumToWord class will take the Number as input in String format and will return it in words. The code for the complete class is as follows.
Public Class clsNumToWord
Public Function Num2WordConverter(ByVal Number As String)
Dim Words As String
Dim Length As Integer
Dim Whole As Decimal
Dim Fraction As Decimal
Whole = Math.Floor(Convert.ToDecimal(Number))
Fraction = (Convert.ToDecimal(Number) - Whole).ToString + "000"
Length = Whole.ToString.Length
If Fraction.ToString.Length >= 4 Then 'coz if the fraction part is just 0 it will generate error in substring
Fraction = Convert.ToInt32(Fraction.ToString.Substring(2, 2))
End If
If Length = 9 Then 'For 10 to 99 Crores
Words = MakeWord(Convert.ToInt32(Whole.ToString.Substring(0, 2))) + " Crore"
If Convert.ToInt32(Whole.ToString.Substring(2, 2)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(2, 2))) + " Lac"
End If
If Convert.ToInt32(Whole.ToString.Substring(4, 2)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(4, 2))) + " Thousand"
End If
If Convert.ToInt32(Whole.ToString.Substring(6, 1)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(6, 1))) + " Hundred"
End If
If Convert.ToInt32(Whole.ToString.Substring(7, 2)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(7, 2)))
End If
Words += " Rupees And " + MakeWord(Fraction) + " Paise Only."
ElseIf Length = 8 Then
Words = MakeWord(Convert.ToInt32(Whole.ToString.Substring(0, 1))) + " Crore"
If Convert.ToInt32(Whole.ToString.Substring(1, 2)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(1, 2))) + " Lac"
End If
If Convert.ToInt32(Whole.ToString.Substring(3, 2)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(3, 2))) + " Thousand"
End If
If Convert.ToInt32(Whole.ToString.Substring(5, 1)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(5, 1))) + " Hundred"
End If
If Convert.ToInt32(Whole.ToString.Substring(6, 2)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(6, 2)))
End If
Words += " Rupees And " + MakeWord(Fraction) + " Paise Only."
ElseIf Length = 7 Then
Words = MakeWord(Convert.ToInt32(Whole.ToString.Substring(0, 2))) + " Lac"
If Convert.ToInt32(Whole.ToString.Substring(2, 2)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(2, 2))) + " Thousand"
End If
If Convert.ToInt32(Whole.ToString.Substring(4, 1)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(4, 1))) + " Hundred"
End If
If Convert.ToInt32(Whole.ToString.Substring(5, 2)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(5, 2)))
End If
Words += " Rupees And " + MakeWord(Fraction) + " Paise Only."
ElseIf Length = 6 Then
Words = MakeWord(Convert.ToInt32(Whole.ToString.Substring(0, 1))) + " Lac"
If Convert.ToInt32(Whole.ToString.Substring(1, 2)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(1, 2))) + " Thousand"
End If
If Convert.ToInt32(Whole.ToString.Substring(3, 1)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(3, 1))) + " Hundred"
End If
If Convert.ToInt32(Whole.ToString.Substring(4, 2)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(4, 2)))
End If
Words += " Rupees And " + MakeWord(Fraction) + " Paise Only."
ElseIf Length = 5 Then
Words = MakeWord(Convert.ToInt32(Whole.ToString.Substring(0, 2))) + " Thousand"
If Convert.ToInt32(Whole.ToString.Substring(2, 1)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(2, 1))) + " Hundred"
End If
If Convert.ToInt32(Whole.ToString.Substring(3, 2)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(3, 2)))
End If
Words += " Rupees And " + MakeWord(Fraction) + " Paise Only."
ElseIf Length = 4 Then
Words = MakeWord(Convert.ToInt32(Whole.ToString.Substring(0, 1))) + " Thousand"
If Convert.ToInt32(Whole.ToString.Substring(1, 1)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(1, 1))) + " Hundred"
End If
If Convert.ToInt32(Whole.ToString.Substring(2, 2)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(2, 2)))
End If
Words += " Rupees And " + MakeWord(Fraction) + " Paise Only."
ElseIf Length = 3 Then
Words = MakeWord(Convert.ToInt32(Whole.ToString.Substring(0, 1))) + " Hundred"
If Convert.ToInt32(Whole.ToString.Substring(1, 2)) <> 0 Then
Words += " " + MakeWord(Convert.ToInt32(Whole.ToString.Substring(1, 2)))
End If
Words += " Rupees And " + MakeWord(Fraction) + " Paise Only."
ElseIf Length <= 2 Then
Words = MakeWord(Whole) + " Rupees And " + MakeWord(Fraction) + " Paise Only."
Else
Words = "Range Exceeded."
End If
Return Words
End Function
Private Function MakeWord(ByVal Number As Integer)
Select Case Number
Case 0
Return "Zero"
Case 1
Return "One"
Case 2
Return "Two"
Case 3
Return "Three"
Case 4
Return "Four"
Case 5
Return "Five"
Case 6
Return "Six"
Case 7
Return "Seven"
Case 8
Return "Eight"
Case 9
Return "Nine"
Case 10
Return "Ten"
Case 11
Return "Eleven"
Case 12
Return "Tweleve"
Case 13
Return "Thirteen"
Case 14
Return "Fourteen"
Case 15
Return "Fifteen"
Case 16
Return "Sixteen"
Case 17
Return "Seventeen"
Case 18
Return "Eighteen"
Case 19
Return "Nineteen"
Case 20
Return "Twenty"
Case 21
Return "Twenty One"
Case 22
Return "Twenty Two"
Case 23
Return "Twenty Three"
Case 24
Return "Twenty Four"
Case 25
Return "Twenty Five"
Case 26
Return "Twenty Six"
Case 27
Return "Twenty Seven"
Case 28
Return "Twenty Eight"
Case 29
Return "Twenty Nine"
Case 30
Return "Thirty"
Case 31
Return "Thirty One"
Case 32
Return "Thirty Two"
Case 33
Return "Thirty Three"
Case 34
Return "Thirty Four"
Case 35
Return "Thirty Five"
Case 36
Return "Thirty Six"
Case 37
Return "Thirty Seven"
Case 38
Return "Thirty Eight"
Case 39
Return "Thirty Nine"
Case 40
Return "Forty"
Case 41
Return "Forty One"
Case 42
Return "Forty Two"
Case 43
Return "Forty Three"
Case 44
Return "Forty Four"
Case 45
Return "Forty Five"
Case 46
Return "Forty Six"
Case 47
Return "Forty Seven"
Case 48
Return "Forty Eight"
Case 49
Return "Forty Nine"
Case 50
Return "Fifty"
Case 51
Return "Fifty One"
Case 52
Return "Fifty Two"
Case 53
Return "Fifty Three"
Case 54
Return "Fifty Four"
Case 55
Return "Fifty Five"
Case 56
Return "Fifty Six"
Case 57
Return "Fifty Seven"
Case 58
Return "Fifty Eight"
Case 59
Return "Fifty Nine"
Case 60
Return "Sixty"
Case 61
Return "Sixty One"
Case 62
Return "Sixty Two"
Case 63
Return "Sixty Three"
Case 64
Return "Sixty Four"
Case 65
Return "Sixty Five"
Case 66
Return "Sixty Six"
Case 67
Return "Sixty Seven"
Case 68
Return "Sixty Eight"
Case 69
Return "Sixty Nine"
Case 70
Return "Seventy"
Case 71
Return "Seventy One"
Case 72
Return "Seventy Two"
Case 73
Return "Seventy Three"
Case 74
Return "Seventy Four"
Case 75
Return "Seventy Five"
Case 76
Return "Seventy Six"
Case 77
Return "Seventy Seven"
Case 78
Return "Seventy Eight"
Case 79
Return "Seventy Nine"
Case 80
Return "Eighty"
Case 81
Return "Eighty One"
Case 82
Return "Eighty Two"
Case 83
Return "Eighty Three"
Case 84
Return "Eighty Four"
Case 85
Return "Eighty Five"
Case 86
Return "Eighty Six"
Case 87
Return "Eighty Seven"
Case 88
Return "Eighty Eight"
Case 89
Return "Eighty Nine"
Case 90
Return "Ninety"
Case 91
Return "Ninety One"
Case 92
Return "Ninety Two"
Case 93
Return "Ninety Three"
Case 94
Return "Ninety Four"
Case 95
Return "Ninety Five"
Case 96
Return "Ninety Six"
Case 97
Return "Ninety Seven"
Case 98
Return "Ninety Eight"
Case 99
Return "Ninety Nine"
Case Else
Return "Error"
End Select
End Function
End Class
Step 3:
On the form take a text box (txtData) and a button (btnReader).
Step 4:
On the Button's click write the following code.
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
Dim obj As New clsNumToWord
clsSpeak.ReadText(obj.Num2WordConverter(txtData.Text))
End Sub
Step 5
It's time to test the application. Enter amount in the text box like 109.89 and click on read. Keep the speakers on and you will hear a voice reading it as One hundred Nine Rupees and eighty nine paise only.
You can download Voice Application Example 2 attached at the end of this article for your reference.What Next?
We have learned how to add voice to our .net applications and make them speak. In this way we can make the application talk on various events of the controls.
After this adventure, Next we will speak with the applications. Soon I am coming with one more article regarding how to give voice commands to the applications developed in .Net and make them work. Keep Reading !
I hope that you have enjoyed the article, the examples (specially Application 2) and learnt something new, something interesting. And very much sure that this code will add value to your knowledge.
(Don't forget to rate the content and leave your responses.)
Thanks 'n Regards,
Sibtain Masih
"Man is still the most extraordinary computer of all."