ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

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.

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.

  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.ComponentModel
  4. Imports System.Data
  5. Imports System.Drawing
  6. Imports System.Text
  7. Imports System.Windows.Forms
  8. Imports System.Diagnostics
  9. Imports SpeechLib

Then, declare all the variables that are needed.

  1. 'SET THE SPEECH RECOGNIZER
  2. Dim s_recognizer As SpInprocRecognizer
  3. 'SET THE GRAMMAR RECOGNIZER
  4. Dim s_grammar As ISpeechRecoGrammar
  5. 'SET THE FILE STREAM CONTAINING THE SPEECH
  6. Dim s_fileStream As SpFileStream
  7. 'SET THE CONTEXT RECOGNIZER
  8. Dim WithEvents speech_RecoContext As SpInProcRecoContext

After that, create a sub procedure for recognizing the audio file.

  1. Private Sub Transcribe_AudioFile(ByVal file_Name As String)
  2.  
  3. 'CREATE AN INSTANCE OF THE RECOGNIZER
  4. s_recognizer = New SpInprocRecognizer()
  5.  
  6. 'THE SPEECH RECOGNIZED THE FILE THAT YOU HAVE PASSED
  7. s_fileStream = New SpFileStream()
  8. s_fileStream.Open(file_Name, SpeechStreamFileMode.SSFMOpenForRead, True)
  9. s_recognizer.AudioInputStream = s_fileStream
  10.  
  11. 'MAKE A RECOGNITION CONTEXT
  12. speech_RecoContext = CType(s_recognizer.CreateRecoContext(), SpInProcRecoContext)
  13.  
  14. 'SET A STANDARD GRAMMAR
  15. s_grammar = speech_RecoContext.CreateGrammar(10)
  16. Try
  17. 'RECOGNITION BEGINS WHEN THE DICTATION HAS BEGUN
  18. s_grammar.DictationSetState(SpeechRuleState.SGDSActive)
  19. Catch ce As System.Runtime.InteropServices.COMException
  20. Debug.WriteLine(ce.ToString())
  21. End Try
  22. End Sub

Then, as the data was analyzed, create a sub procedure that can be called for many times.

  1. Private Sub On_Hypothesis(ByVal StreamNumber As Integer, ByVal Stream_Position As Object, ByVal Results As ISpeechRecoResult) Handles speech_RecoContext.Hypothesis
  2. SyncLock Me
  3. Dim info As ISpeechPhraseInfo = Results.PhraseInfo
  4. 'YOU COULD STORE THIS FOR FURTHER ANALYSIS
  5. Dim el As ISpeechPhraseElement
  6. For Each el In info.Elements
  7. Debug.WriteLine(el.DisplayText)
  8. Next
  9. Debug.WriteLine("--Hypothesis over--")
  10. End SyncLock
  11. End Sub

After that, create a sub procedure to be called once after the entire file was analyzed.

  1. 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
  2. Dim phraseInfo As ISpeechPhraseInfo = Result.PhraseInfo
  3.  
  4. 'THE COMPLETE ROCOGNIZED TEXT WILL BE SHOWN IN THE RICHTEXTBOX
  5. Dim speech As String = phraseInfo.GetText(0, -1, True)
  6. RichTextBox1.AppendText(speech)
  7.  
  8. 'REQUESTING UP TO 10 ALTERNATES FROM THE INDEX POSITION 0 CONSIDERING ALL THE ELEMENTS(-1)
  9. Dim alternate As ISpeechPhraseAlternate
  10. For Each alternate In Result.Alternates(10, 10, -1)
  11. Dim altResult As ISpeechRecoResult = alternate.RecoResult
  12. Dim altInfo As ISpeechPhraseInfo = altResult.PhraseInfo
  13. Dim altString As String = altInfo.GetText(0, -1, True)
  14. Debug.WriteLine(altString)
  15. Next
  16. End Sub

Then, create a sub procedure again for stopping the recognition.

  1. Private Sub On_AudioStreamEnd(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal someBool As Boolean) Handles speech_RecoContext.EndStream
  2. 'RECOGNITION WILL STOP
  3. s_grammar.DictationSetState(SpeechRuleState.SGDSInactive)
  4. 'THE ACTIVE DICTATION TOPIC IS UNLOAD
  5. s_grammar.DictationUnload()
  6. End Sub

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.

  1. Private Sub openbutton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
  2. 'CREATE AN INSTANCE OF THE OPENFILEDIALOG.
  3. Dim openFd As OpenFileDialog = New OpenFileDialog()
  4. 'FILTERING THE FILE TO .WAV FILES.
  5. openFd.DefaultExt = ".wav"
  6. openFd.Filter = "Wav files (.wav)|*.wav"
  7. 'CHECKING IF THE DIALOG RESULT IS OK.
  8. If openFd.ShowDialog() = Windows.Forms.DialogResult.OK Then
  9. 'SET THE FILE SOURCE IN THE TEXTBOX.
  10. TextBox1.Text = openFd.FileName
  11. End If
  12. End Sub

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.

  1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  2. RichTextBox1.Clear()
  3. 'WHEN CLICKING THE BUTTON, THE CURSOR WILL BE CHANGED
  4. Cursor = Cursors.WaitCursor
  5.  
  6. 'CALL A METHOD YOU HAVE CREATED TO
  7. 'PERFORM THE RECOGNITION OF A FILE IN THE TEXTBOX
  8. Transcribe_AudioFile(TextBox1.Text)
  9.  
  10. End Sub

Lastly, do this following code for restoring the mouse cursor to default.

  1. Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
  2. 'WHEN THE RICHBOX IS EMPTY, THE MOUSE CURSOR WILL SET TO DEFAULT.
  3. If RichTextBox1.Text "" Then
  4. Cursor = Cursors.Default
  5. End If
  6. End Sub

Output:

audioconverter_output

 

Download Sourcecode

 

Facebook Comments

Check Your Domain Ranking

2 Comments

Add a Comment
  1. Could you please reupload the file because it appears that it’s corrupted

    1. I already update the sourcecode file. You can download it now.

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!