Wave to Text - Need help with custom grammar


  • I created a wav to text conversion using a ISpeechRecoContext and it works, but it is not precised. I was hopping to add a custom grammar to it but im not sure how to. Any ideas?

    Here is a WPF MainWindow.xaml.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Microsoft.Win32;
    using SpeechLib;
    namespace WaveToText
      public partial class MainWindow : Window
        private ISpeechRecoContext WaveRecognizer = null;
        private SpFileStream InputWAV = null;
        private ISpeechRecoGrammar Grammar = null;
        public MainWindow()
        private void ExitButtonClick(object sender, RoutedEventArgs e)
        private void ConvertButtonClick(object sender, RoutedEventArgs e)
          ResultTextBox.Text = "";
          OpenFileDialog dialog = new OpenFileDialog();
          dialog.Title = "Select a Speech file";
          dialog.Filter = "Wave Files (.WAV)|*.wav"; // Filter files by extension
          dialog.InitialDirectory = Environment.CurrentDirectory + "\\Content";
          Nullable<bool> result = dialog.ShowDialog();
          if (result == true)
            WaveRecognizer = new SpInProcRecoContext();
            ((SpInProcRecoContext)WaveRecognizer).Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(wavRecoContext_Recognition);
            ((SpInProcRecoContext)WaveRecognizer).EndStream += new _ISpeechRecoContextEvents_EndStreamEventHandler(wavRecoContext_EndStream);
            Grammar = WaveRecognizer.CreateGrammar(2);
            Grammar.DictationLoad("", SpeechLoadOption.SLOStatic);
            InputWAV = new SpFileStreamClass();
            InputWAV.Open(dialog.FileName, SpeechStreamFileMode.SSFMOpenForRead, false);
            WaveRecognizer.Recognizer.AudioInputStream = InputWAV;
        private void wavRecoContext_Recognition(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
          ResultTextBox.Text = ResultTextBox.Text + " " + Result.PhraseInfo.GetText(0, -1, true);
        private void wavRecoContext_EndStream(int StreamNumber, object StreamPosition, bool f)

    Here is the WPF MainWindow.xaml

    <Window x:Class="WaveToText.MainWindow"
        Title="Wav To Text" Height="500" Width="525">
        <LinearGradientBrush EndPoint="0.504,1.5" StartPoint="0.504,0.03">
          <GradientStop Color="#f5f5f5" Offset="0"/>
          <GradientStop Color="#323fa2" Offset="0.5"/>
        <TextBlock Height="183" Name="ResultTextBox" Width="277" Text="Wav to Text will be displayed here" Background="WhiteSmoke" TextWrapping="WrapWithOverflow"/>
        <Button Height="35" Width="100" Margin="114,339,290,87" Click="ExitButtonClick" Content="Exit"/>
        <Button Height="35" Width="100" Margin="290,339,112,87" Click="ConvertButtonClick" Content="Load Wav"/>
    Tuesday, September 07, 2010 9:55 PM


All replies