How to Convert an Audio File to a Text In VB.Net

2
1079

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.

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.

audioconverter_form1

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:

audioconverter_output

 

Download Sourcecode

 

Facebook Comments

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.