Groups > Microsoft > dotNET Speech SDK > Re: Recognition from multiple wav files... only last file gets recognized




Recognition from multiple wav files... only last file gets
recognized

Recognition from multiple wav files... only last file gets recognized
Sat, 01 Mar 2008 05:03:43 +010
Hello!

I wan't my program to recognize the text from multiple wav-files in a
folder (file by file) in C#.

But with my code only the LAST file of my array is getting recognized.
Why aren't the Recognitions made one after another?

I don't get it.
Maybe someone of you people has an idea.


I tried it this way:
**************
....
this.wavarray = Directory.GetFiles(".\\" + title, "*.wav");

for (int i = 0; i < this.wav.Length; i++)
{
    this.sfile = wavarray[number].ToString();			// number is 0 at this time
    Console.WriteLine(sfile);
    recognize(this.number);

    this.number++;
}

public void recognize(int number)
  {
     engine = new SpeechRecognitionEngine();
     engine.SetInputToWaveFile(this.questionarray[number].ToString());
     engine.LoadGrammar(new DictationGrammar());
     engine.RecognizeAsync(RecognizeMode.Multiple);

     engine.SpeechRecognized += new
EventHandler<SpeechRecognizedEventArgs>(engine_SpeechRecognized);		//
write result to txtfile

     engine.AudioStateChanged += new
EventHandler<AudioStateChangedEventArgs>(engine_AudioStateChanged);		//
catch the "Stopped"-state

Post Reply
Re: Recognition from multiple wav files... only last file gets recognized
Thu, 6 Mar 2008 08:03:57 -0800
Hi Stefan,

The heart of the problem is that on every call to your recognize method a 
new SpeechRecognitionEngine object is created and assigned to engine before 
the previous engine has had a chance to recognize anything.  This happens 
because RecognizeAsync returns without waiting for a recognition to occur.

With just a few changes you should be able to get the behavior your want. I 
suggest getting rid of the for-loop, only creating a single 
SpeechRecognitionEngine, and uses the RecognizeCompleted event to decide 
when to call SetInputToWaveFile to load the next file in your array.   After 
the changes your code should look something like this:

        public void recognizer()
        {
            engine = new SpeechRecognitionEngine();
            engine.SpeechRecognized += new 
EventHandler<SpeechRecognizedEventArgs>(engine_SpeechRecognized); // write

result to txtfile
            engine.AudioStateChanged += new 
EventHandler<AudioStateChangedEventArgs>(engine_AudioStateChanged); //
catch 
the "Stopped"-state
            engine.RecognizeCompleted += new 
EventHandler<RecognizeCompletedEventArgs>(engine_RecognizeCompleted);

            engine.SetInputToWaveFile(this.questionarray[0].ToString());  // 
could use MethodThatSetsInputWaveToTheNextFile() for this
            engine.LoadGrammar(new DictationGrammar());
            engine.RecognizeAsync(RecognizeMode.Multiple);
        }

        void engine_RecognizeCompleted(object sender, 
RecognizeCompletedEventArgs e)
        {
            MethodThatSetsInputWaveToTheNextFile(); // You need to write 
this!
        }

-- Steve Meyer

This posting is provided "AS IS" with no warranties, and confers no
rights.
Use of included script samples are subject to the terms specified at 
http://www.microsoft.com/info/cpyright.htm

"Stefan P." <wwwebrat@yahoo.com> wrote in message 
news:OaXMBF1eIHA.484@TK2MSFTNGP06.phx.gbl...
> Hello!
>
> I wan't my program to recognize the text from multiple wav-files in a
> folder (file by file) in C#.
>
> But with my code only the LAST file of my array is getting recognized.
> Why aren't the Recognitions made one after another?
>
> I don't get it.
> Maybe someone of you people has an idea.
>
>
> I tried it this way:
> **************
> ....
> this.wavarray = Directory.GetFiles(".\\" + title,
"*.wav");
>
> for (int i = 0; i < this.wav.Length; i++)
> {
>    this.sfile = wavarray[number].ToString(); // number is 0 at this time
>    Console.WriteLine(sfile);
>    recognize(this.number);
>
>    this.number++;
> }
>
> public void recognize(int number)
>  {
>     engine = new SpeechRecognitionEngine();
>     engine.SetInputToWaveFile(this.questionarray[number].ToString());
>     engine.LoadGrammar(new DictationGrammar());
>     engine.RecognizeAsync(RecognizeMode.Multiple);
>
>     engine.SpeechRecognized += new
> EventHandler<SpeechRecognizedEventArgs>(engine_SpeechRecognized); //
> write result to txtfile
>
>     engine.AudioStateChanged += new
> EventHandler<AudioStateChangedEventArgs>(engine_AudioStateChanged);
//
> catch the "Stopped"-state
>
> } 
Post Reply
Re: Recognition from multiple wav files... only last file gets recognized
Sat, 08 Mar 2008 00:25:15 +010
Hi Steve,

using the RecognizeComplete event worked!
Thanks for giving me this tip.

Greetings
Stefan

Steve Meyer [MSFT] schrieb:
> Hi Stefan,
>
> The heart of the problem is that on every call to your recognize 
> method a new SpeechRecognitionEngine object is created and assigned to 
> engine before the previous engine has had a chance to recognize 
> anything.  This happens because RecognizeAsync returns without waiting 
> for a recognition to occur.
>
> With just a few changes you should be able to get the behavior your 
> want. I suggest getting rid of the for-loop, only creating a single 
> SpeechRecognitionEngine, and uses the RecognizeCompleted event to 
> decide when to call SetInputToWaveFile to load the next file in your 
> array.   After the changes your code should look something like this:
>
>        public void recognizer()
>        {
>            engine = new SpeechRecognitionEngine();
>            engine.SpeechRecognized += new 
> EventHandler<SpeechRecognizedEventArgs>(engine_SpeechRecognized); //

> write result to txtfile
>            engine.AudioStateChanged += new 
> EventHandler<AudioStateChangedEventArgs>(engine_AudioStateChanged);
// 
> catch the "Stopped"-state
>            engine.RecognizeCompleted += new 
>
EventHandler<RecognizeCompletedEventArgs>(engine_RecognizeCompleted);
>
>            
> engine.SetInputToWaveFile(this.questionarray[0].ToString());  // could 
> use MethodThatSetsInputWaveToTheNextFile() for this
>            engine.LoadGrammar(new DictationGrammar());
>            engine.RecognizeAsync(RecognizeMode.Multiple);
>        }
>
>        void engine_RecognizeCompleted(object sender, 
> RecognizeCompletedEventArgs e)
>        {
>            MethodThatSetsInputWaveToTheNextFile(); // You need to 
> write this!
>        }
>
> -- Steve Meyer
>
> This posting is provided "AS IS" with no warranties, and confers
no 
> rights.
> Use of included script samples are subject to the terms specified at 
> http://www.microsoft.com/info/cpyright.htm
>
> "Stefan P." <wwwebrat@yahoo.com> wrote in message 
> news:OaXMBF1eIHA.484@TK2MSFTNGP06.phx.gbl...
>> Hello!
>>
>> I wan't my program to recognize the text from multiple wav-files in a
>> folder (file by file) in C#.
>>
>> But with my code only the LAST file of my array is getting recognized.
>> Why aren't the Recognitions made one after another?
>>
>> I don't get it.
>> Maybe someone of you people has an idea.
>>
>>
>> I tried it this way:
>> **************
>> ....
>> this.wavarray = Directory.GetFiles(".\\" + title,
"*.wav");
>>
>> for (int i = 0; i < this.wav.Length; i++)
>> {
>>    this.sfile = wavarray[number].ToString(); // number is 0 at this
time
>>    Console.WriteLine(sfile);
>>    recognize(this.number);
>>
>>    this.number++;
>> }
>>
>> public void recognize(int number)
>>  {
>>     engine = new SpeechRecognitionEngine();
>>     engine.SetInputToWaveFile(this.questionarray[number].ToString());
>>     engine.LoadGrammar(new DictationGrammar());
>>     engine.RecognizeAsync(RecognizeMode.Multiple);
>>
>>     engine.SpeechRecognized += new
>> EventHandler<SpeechRecognizedEventArgs>(engine_SpeechRecognized);
//
>> write result to txtfile
>>
>>     engine.AudioStateChanged += new
>>
EventHandler<AudioStateChangedEventArgs>(engine_AudioStateChanged); //
>> catch the "Stopped"-state
>>
>> } 
Post Reply
about | contact