How to Convert an Audio File to a Text In VB.net
In this tutorial I will show how to convert an audio file into a text by using Visual Basic 2008. This method helps you, how to recognize the content of your audio file. For instance, if the audio file cannot be heard or cannot be understood. It will be recognized, because the content of your audio file will appear in the Box through text.
What is Visual Basic’s purpose?
The third-generation programming language was created to aid developers in the creation of Windows applications. It has a programming environment that allows programmers to write code in.exe or executable files. They can also utilize it to create in-house front-end solutions for interacting with huge databases. Because the language allows for continuing changes, you can keep coding and revising your work as needed.
However, there are some limits to the Microsoft Visual Basic download. If you want to make applications that take a long time to process, this software isn’t for you. That implies you won’t be able to use VB to create games or large apps because the system’s graphic interface requires a lot of memory and space. Furthermore, the language is limited to Microsoft and does not support other operating systems.
What are the most important characteristics of Visual Basic?
Microsoft Visual Basic for Applications Download, unlike other programming languages, allows for speedier app creation. It has string processing capabilities and is compatible with C++, MFC, and F#. Multi-targeting and the Windows Presentation Framework are also supported by the system, allowing developers to create a variety of Windows apps, desktop tools, metro-style programs, and hardware drivers.
Let’s begin:
Open the Visual Basic 2008, create a new Windows Application and drag a RichTextBox, TextBox and the Button. Then do the Form just like this.
After that, double click the Form and do the following code for your imports.
[vbnet]
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Diagnostics
Imports SpeechLib
[/vbnet]
Then, declare all the variables that are needed.
[vbnet]
‘SET THE SPEECH RECOGNIZER
Dim s_recognizer As SpInprocRecognizer
‘SET THE GRAMMAR RECOGNIZER
Dim s_grammar As ISpeechRecoGrammar
‘SET THE FILE STREAM CONTAINING THE SPEECH
Dim s_fileStream As SpFileStream
‘SET THE CONTEXT RECOGNIZER
Dim WithEvents speech_RecoContext As SpInProcRecoContext
[/vbnet]
After that, create a sub procedure for recognizing the audio file.
[vbnet]
Private Sub Transcribe_AudioFile(ByVal file_Name As String)
‘CREATE AN INSTANCE OF THE RECOGNIZER
s_recognizer = New SpInprocRecognizer()
‘THE SPEECH RECOGNIZED THE FILE THAT YOU HAVE PASSED
s_fileStream = New SpFileStream()
s_fileStream.Open(file_Name, SpeechStreamFileMode.SSFMOpenForRead, True)
s_recognizer.AudioInputStream = s_fileStream
‘MAKE A RECOGNITION CONTEXT
speech_RecoContext = CType(s_recognizer.CreateRecoContext(), SpInProcRecoContext)
‘SET A STANDARD GRAMMAR
s_grammar = speech_RecoContext.CreateGrammar(10)
Try
‘RECOGNITION BEGINS WHEN THE DICTATION HAS BEGUN
s_grammar.DictationSetState(SpeechRuleState.SGDSActive)
Catch ce As System.Runtime.InteropServices.COMException
Debug.WriteLine(ce.ToString())
End Try
End Sub
[/vbnet]
Then, as the data was analyzed, create a sub procedure that can be called for many times.
[vbnet]
Private Sub On_Hypothesis(ByVal StreamNumber As Integer, ByVal Stream_Position As Object, ByVal Results As ISpeechRecoResult) Handles speech_RecoContext.Hypothesis
SyncLock Me
Dim info As ISpeechPhraseInfo = Results.PhraseInfo
‘YOU COULD STORE THIS FOR FURTHER ANALYSIS
Dim el As ISpeechPhraseElement
For Each el In info.Elements
Debug.WriteLine(el.DisplayText)
Next
Debug.WriteLine(“–Hypothesis over–“)
End SyncLock
End Sub
[/vbnet]
After that, create a sub procedure to be called once after the entire file was analyzed.
[vbnet]
Private Sub On_Recognition(ByVal Stream_Number As Integer, ByVal StreamPosition As Object, ByVal RecognitionType As SpeechRecognitionType, ByVal Result As ISpeechRecoResult) Handles speech_RecoContext.Recognition
Dim phraseInfo As ISpeechPhraseInfo = Result.PhraseInfo
‘THE COMPLETE ROCOGNIZED TEXT WILL BE SHOWN IN THE RICHTEXTBOX
Dim speech As String = phraseInfo.GetText(0, -1, True)
RichTextBox1.AppendText(speech)
‘REQUESTING UP TO 10 ALTERNATES FROM THE INDEX POSITION 0 CONSIDERING ALL THE ELEMENTS(-1)
Dim alternate As ISpeechPhraseAlternate
For Each alternate In Result.Alternates(10, 10, -1)
Dim altResult As ISpeechRecoResult = alternate.RecoResult
Dim altInfo As ISpeechPhraseInfo = altResult.PhraseInfo
Dim altString As String = altInfo.GetText(0, -1, True)
Debug.WriteLine(altString)
Next
End Sub
[/vbnet]
Then, create a sub procedure again for stopping the recognition.
[vbnet]
Private Sub On_AudioStreamEnd(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal someBool As Boolean) Handles speech_RecoContext.EndStream
‘RECOGNITION WILL STOP
s_grammar.DictationSetState(SpeechRuleState.SGDSInactive)
‘THE ACTIVE DICTATION TOPIC IS UNLOAD
s_grammar.DictationUnload()
End Sub
[/vbnet]
After we create all the sub procedures that are needed. Go back to the design view, double click the “open file” Button and do the following code for getting the audio file in the explorer.
[vbnet]
Private Sub openbutton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
‘CREATE AN INSTANCE OF THE OPENFILEDIALOG.
Dim openFd As OpenFileDialog = New OpenFileDialog()
‘FILTERING THE FILE TO .WAV FILES.
openFd.DefaultExt = “.wav”
openFd.Filter = “Wav files (.wav)|*.wav”
‘CHECKING IF THE DIALOG RESULT IS OK.
If openFd.ShowDialog() = Windows.Forms.DialogResult.OK Then
‘SET THE FILE SOURCE IN THE TEXTBOX.
TextBox1.Text = openFd.FileName
End If
End Sub
[/vbnet]
Go back to the design view again, double click the “speech” button and do the following code for starting the conversion of the audio file into a text.
[vbnet]
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
RichTextBox1.Clear()
‘WHEN CLICKING THE BUTTON, THE CURSOR WILL BE CHANGED
Cursor = Cursors.WaitCursor
‘CALL A METHOD YOU HAVE CREATED TO
‘PERFORM THE RECOGNITION OF A FILE IN THE TEXTBOX
Transcribe_AudioFile(TextBox1.Text)
End Sub
[/vbnet]
Lastly, do this following code for restoring the mouse cursor to default.
[vbnet]
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
‘WHEN THE RICHBOX IS EMPTY, THE MOUSE CURSOR WILL SET TO DEFAULT.
If RichTextBox1.Text “” Then
Cursor = Cursors.Default
End If
End Sub
[/vbnet]
Output:
Download Source code
Related topic(s) that you may like:
If you have any questions or suggestions in How to Convert an Audio File to a Text In VB.net, please feel free to leave a comment below.
Could you please reupload the file because it appears that it’s corrupted
I already update the sourcecode file. You can download it now.
I HAVE TRIED TO CONVERT THE MP3 AUDIO FILE BUT UNFORTUNATELY COULD NOT BE ABLE TO CONVERT INTO TEXT