Vous êtes sur la page 1sur 5

How to Convert an Audio File to a

Text In VB.Net
By
janobe
-
October 7, 2015
2
960

Share on Facebook

Tweet on Twitter

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.
After that, double click the Form and do the following code for your imports.

< View plain text >

VB.NET

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.

< View plain text >

VB.NET
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.

< View plain text >

VB.NET

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.

< View plain text >

VB.NET

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.

< View plain text >

VB.NET

1. Private Sub On_Recognition(ByVal Stream_Number As Integer, ByV


alStreamPosition As Object, ByVal RecognitionType AsSpeechReco
gnitionType, ByVal Result As ISpeechRecoResult) Handlesspeech_
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.

Vous aimerez peut-être aussi