views:

2015

answers:

4

How do you use the .NET speech namespace classes to convert audio in a WAV file to textual form which I can display on the screen or save to file?

I am looking for some tutorial samples.

UPDATE

Found a code sample here. But when I tried it it gives incorrect results. Below is the vb code sample I've adopted. (Actually I don't mind the lang as long as its either vb/c#...). It is not giving me proper results. I assume if we put the right grammar - i.e. the words we expect in the recording - we should get the textual output of that. First I've tried with sample words that are in the call. It sometimes printed only that (one) word and nothing else. Then I tried words which we totally do not expect in the recording...Unfortunately it printed out that too... :(

Imports System
Imports System.Speech.Recognition

Public Class Form1

    Dim WithEvents sre As SpeechRecognitionEngine

    Private Sub btnLiterate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLiterate.Click
        If TextBox1.Text.Trim.Length = 0 Then Exit Sub
        sre.SetInputToWaveFile(TextBox1.Text)
        Dim r As RecognitionResult
        r = sre.Recognize()
        If r Is Nothing Then
            TextBox2.Text = "Could not fetch result"
            Return
        End If
        TextBox2.Text = r.Text
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = String.Empty
        Dim dr As DialogResult
        dr = OpenFileDialog1.ShowDialog()
        If dr = Windows.Forms.DialogResult.OK Then
            If Not OpenFileDialog1.FileName.Contains("wav") Then
                MessageBox.Show("Incorrect file")
            Else
                TextBox1.Text = OpenFileDialog1.FileName
            End If
        End If
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        sre = New SpeechRecognitionEngine()

    End Sub

    Private Sub sre_LoadGrammarCompleted(ByVal sender As Object, ByVal e As System.Speech.Recognition.LoadGrammarCompletedEventArgs) Handles sre.LoadGrammarCompleted

    End Sub

    Private Sub sre_SpeechHypothesized(ByVal sender As Object, ByVal e As System.Speech.Recognition.SpeechHypothesizedEventArgs) Handles sre.SpeechHypothesized
        System.Diagnostics.Debug.Print(e.Result.Text)
    End Sub

    Private Sub sre_SpeechRecognitionRejected(ByVal sender As Object, ByVal e As System.Speech.Recognition.SpeechRecognitionRejectedEventArgs) Handles sre.SpeechRecognitionRejected
        System.Diagnostics.Debug.Print("Rejected: " & e.Result.Text)
    End Sub

    Private Sub sre_SpeechRecognized(ByVal sender As Object, ByVal e As System.Speech.Recognition.SpeechRecognizedEventArgs) Handles sre.SpeechRecognized
        System.Diagnostics.Debug.Print(e.Result.Text)
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim words As String() = New String() {"triskaidekaphobia"}
        Dim c As New Choices(words)
        Dim grmb As New GrammarBuilder(c)
        Dim grm As Grammar = New Grammar(grmb)
        sre.LoadGrammar(grm)
    End Sub

End Class

UPDATE(after Nov 28th)

Found a way to load a default grammar. It goes something like this:

sre.LoadGrammar(New DictationGrammar)

There are still problems here. The recognition is not exact. The output is rubbish. For a 6min file it gives probably 5-6 words of text totally irrelevant to the voice file.

+6  A: 

The classes in System.Speech are for text to speech (primarily an acessibility feature).

You are looking for voice recognition. There is the System.Speech.Recognition namespace available since .Net 3.0. It uses the Windows Desktop Speech engine. This might get you started, but I guess there are better engines out there.

Voice recognition is very complicated and hard to do right, there are also some commercial products available.

Johannes Rudolph
will this require actually "playing" the voice file?
deostroll
Maybe that and then rooting the audio output to the mic input like some cd rippers do. Otherwise try checking out how SpeechRecognizer.EmulateRecognize works with reflector.
Johannes Rudolph
Maybe check that out also:http://msdn.microsoft.com/en-us/library/system.speech.audioformat.speechaudioformatinfo_members.aspxsorry, Im no expert for that. Hope it helps you.
Johannes Rudolph
The EmulateRecognize won't help - it's meant to bypass the recognition by giving the recognized output.
configurator
A: 

You actually need Natural Language toolkit. In python I have used NTLK http://www.nltk.org/

In .Net I have just found Antelope http://stackoverflow.com/questions/1762040/natural-language-toolkit-equivalent-in-c

see the article as well http://en.wikipedia.org/wiki/Speech_recognition

Tinku
http://www.proxem.com/WhatisNLP/tabid/59/Default.aspx for Antelope
Tinku
A: 

You should use the SpeechRecognitionEngine. To use a wave file, call SetInputToWaveFile. I wish I could help you more, but I'm no expert.

Oh, and if your word is really triskaidekaphobia, I don't think even a human speech recognition engine would recognize that...

configurator
A: 

I have tested your code , but it is not grabbing wave file properly. It is catching

If Not OpenFileDialog1.FileName.Contains("wav") Then MessageBox.Show("Incorrect file") Else TextBox1.Text = OpenFileDialog1.FileName End If

Not the else condition. I tried using .wav in the string also.

I am also in need of a sample code for transcribing wav file to text not from Microphone. Please if u came to a good solution so please post it here.

Umaid