In VB.Net windows form application, we can create the advanced project using the Visual Studio IDE. You will aware of the voice recognition and speech synthesis in the windows application. A voice recognition software recognizes the voice as a pattern. The Visual Studio provides the advanced tools to create the more user interactive applications. I’m here with a new post in which we will know how to create a text to speech application in VB.Net. The entered text will convert into the speech. Really it is a very interesting feature provided by the Microsoft. In our last tutorial, we created the splash screen for the windows form application. Also, I used a progress bar to make the progression effect of loading the project. So if you missed that post, you can go with the link given below.
The text to speech application is very easy to create in VB.Net. So before creating the project, you must know about the Text to Speech API. So let’s start with this.
Text to Speech Application
The Microsoft provides an API to convert the text to speech which is SAPI. The SAPI (Speech Application Programming Interface) is developed by the Microsoft which is generally used for the speech recognition and the speech synthesis. This feature is available for the windows form application in VB.Net. The best example is Microsoft Office, Microsoft Agent, and the Microsoft Speech Server which consists the SAPI. Currently, the Microsoft has released the latest version of the SAPI 5.4.
So let’s start with creating a new project in Visual Studio.
Creating Text to Speech Application Project
- Open the Visual Studio IDE and take a New project.
- Give the Project Name, select the .NET Framework (choose the higher)
- Then select the project saving location and click on OK.
- Once the default windows form is loaded, let’s start to create a design for the Text to Speech Application.
Design of the Text to Speech Application
- Configure the windows form as we have seen before.
- Make the StartPosition to CenterScreen, change the form Text, BorderStyle to Fixed3D and WindowState to Normal.
- Set the BackColor of the form as you want.
- Drag a GroupBox control from the ToolBox. Change the Text to Application.
- Then drag a Label and change its text.
- Now, in the next step drag a TextBox in which we will write some text.
- We need to drag a Button so that when we click on the button, the entered text will convert into the speech.
- Once your design is completed, let’s move to the code part.
Code of the Text to Speech Application
- Here, you will have to write some code for converting the text to speech.
- Just double-click on the button, it will appear the code block of the Button.
- Now, just paste the below code.
Public Class frmTextToSpeech
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim speech As Object
speech = CreateObject("SAPI.spvoice")
- This is the sample of code window of the Button click.
Explanation of the Code
- In the above code, I have declared a variable speech of Object data type.
- Object Data Type: In VB.Net, the Object data type is a just like a pointer data type. It gives the reference to the memory. In other words, the declared data type of the Object type is an address and it refers to the object in the memory.
- In the next line, the speech variable will hold the created object of the SAPI (Speech Application Programming Interface).
- Here SpVoice is an object that brings the text-to-speech (TTS) engine capabilities to applications using SAPI automation.
- Then in the next, the speak method returns the audio (voice) to the speaker. So that we can listen to the Text to speech.
- Save and execute the above program code to see the output as shown below.
- Enter some text in the TextBox and click on the Speak Button.
- Here the entered text will convert into the speech and you can listen to it easily.
- Increase the volume of your system to listen to the typed text as speech.
Bingo you have successfully created the Text to Speech application in VB.NET. If you find it helpful then don’t forget to share it with your friends. If you need any help from me regarding any post here then please ask through the comment. Thank you 🙂