Speech (.wav) recognition trouble
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Speech (.wav) recognition trouble

  1. #1
    Join Date
    May 2011

    Speech (.wav) recognition trouble


    I am working on a cap-stone project for my senior design. One small part of this project is a "simple" speech to text recognition system. I did a few tutorials for C# as I didn't know C#, only C++. I then searched around for some code snippets and came across this code. I had to tweak it to get it to compile.

    Now it will compile but does nothing. The windows form pops up with the 2 buttons and text box with default properties. Clicking anything, does nothing. Have to "X" out.

    I am using MSVC 2010 and put the .mp3 in the same folder as the .cs

    Any ideas?

    //Code start

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using SpeechLib;

    namespace SpeechFromFile

    public partial class Form1 : Form

    private SpeechLib.ISpeechRecoContext wavRecoContext = null;

    private SpeechLib.SpFileStream InputWAV = null;

    private SpeechLib.ISpeechRecoGrammar Grammar = null;
    private String _WAVFile = null;
    private string strData = "No recording yet";
    private String _lastRecognized = "";

    public Form1()

    private void Form1_Load(object sender, EventArgs e)


    private void button2_Click(object sender, EventArgs e)

    private void button1_Click(object sender, EventArgs e)
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Title = "Select a Speech file";
    _WAVFile = dialog.FileName;
    if (_WAVFile == null) return;

    // Now we have the WAV file, we can set up the
    // inline SPeech Engine to process it
    // create the recognition context

    wavRecoContext = new SpeechLib.SpInProcRecoContext();

    // Register our event as a listener on the Recognition event
    // that way, anytime the speech engine thinks it "hears" something
    // that it recognize, we're called to check it out for ourselves

    ((SpInProcRecoContext)wavRecoContext).Recognition +=

    new _ISpeechRecoContextEvents_RecognitionEventHandler(wavRecoContext_Recognition);

    // Register a method on the endStream event so we can do basic clean-up
    // when the Audio file is finished

    ((SpInProcRecoContext)wavRecoContext).EndStream += new _ISpeechRecoContextEvents_EndStreamEventHandler(wavRecoContext_EndStream);

    // the parameter passed to CreateGrammar is any int. It is only used as if
    // you have more than one grammar active, so you can specify which one is
    // to be used....

    Grammar = wavRecoContext.CreateGrammar(2);

    // I simply use the default Frammar for dictation

    Grammar.DictationLoad("", SpeechLoadOption.SLOStatic);

    //Speech engine is now ready to go, so set it to the
    // audio file TO do this, we open the requested file
    // using the SPeechStreamFileMode, and pass that to the
    // speech engine to use as its input source
    // InputWAV = new SpFileStreamClass();

    InputWAV.Open("Speechtest.mp3",SpeechStreamFileMode.SSFMOpenForRead, false);

    wavRecoContext.Recognizer.AudioInputStream = InputWAV;

    // the way you "Turn On" the speech engine is by setting the Diction State
    // of its grammar to "Active"


    // the result will be handled by wavRecoContext_Recognition()



    //' Event fired when speech recognition engine recognizes audio


    private void wavRecoContext_Recognition(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
    strData = Result.PhraseInfo.GetText(0, -1, true);
    // parseSpeechResult(strData); -- Call a function to parse it if wanted
    _lastRecognized = textBox1.Text;
    textBox1.Text = strData;



    //' End of wav Input Stream reached by speech recognition engine


    private void wavRecoContext_EndStream(int StreamNumber, object StreamPosition, bool f)
    // ' Disable dictation




  2. #2
    Join Date
    Feb 2011
    United States

    Re: Speech (.wav) recognition trouble

    I seem to remember not thinking this was too terribly difficult when I (briefly) played with it a few years ago. I was trying to find the tutorial I was learning from, but wasn't quite sure. Nevertheless, you will probably find this MSDN article useful: http://msdn.microsoft.com/en-us/magazine/cc163663.aspx

    In particular, figures 8 and 9 show off the speech recognition namespace.

    Hope that helps!

    (Didn't much look at your code since I thought pointing you towards the ref might be more helpful. In the future use [code] and [/code] tags around your code to make it render with indents correctly laid out.
    Best Regards,

    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Azure Activities Information Page

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


HTML5 Development Center