Page 1 of 1

c# ventuz help

Posted: 29 Jan 2015, 13:59
by hgoodsir
hi all,

just experimenting with c# in ventuz and a little stuck with abit of the script i have made text to speech work,
but now been trying to get speech to text.


when compile is says sRecognize does not exist and sRecognize_SpeechRecognized does not exist
i have look at examples and think its something to do with this private void but not really sure.

Code: Select all

private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result.Text == "play")
            {
                Play();
            }
         }



Code: Select all

using System;
using Ventuz.Kernel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Threading;



public class Script : ScriptBase, System.IDisposable
{
    
	// This member is used by the Validate() method to indicate
	// whether the Generate() method should return true or false
	// during its next execution.
	private bool changed;
    
	// This Method is called if the component is loaded/created.
	public Script()
	{
		// Note: Accessing input or output properties from this method
		// will have no effect as they have not been allocated yet.
	}
    
	// This Method is called if the component is unloaded/disposed
	public virtual void Dispose()
	{
	
		
	}
    
	// This Method is called if an input property has changed its value
	public override void Validate()
	{
		// Remember: set changed to true if any of the output 
		// properties has been changed, see Generate()
		
		SpeechSynthesizer sSynth = new SpeechSynthesizer();
		PromptBuilder pBuilder = new PromptBuilder();
		SpeechRecognitionEngine sReconize = new SpeechRecognitionEngine();
		
		
		
		pBuilder.ClearContent();
		pBuilder.AppendText(text);
		sSynth.Speak(pBuilder);
		
		Choices sList = new Choices();
		sList.Add(new string[] {"play","stop"});
		Grammar gr = new Grammar (new GrammarBuilder(sList));
		try
		{
			sRecognize.RequestRecognizerUpdate();
			sRecognize.LoadGrammar(gr);
			sRecognize.SpeechRecognized += sRecognize_SpeechRecognized;
			sRecognize.SetInputToDefaultAudioDevice();
			sRecognize.RecognizeAsync(RecognizeMode.Multiple);
			sRecognize.Recognize();
		}
		catch
		{
			return;	
		}	
		
	}
    
	// This Method is called every time before a frame is rendered.
	// Return value: if true, Ventuz will notify all nodes bound to this
	//               script node that one of the script's outputs has a
	//               new value and they therefore need to validate. For
	//               performance reasons, only return true if output
	//               values really have been changed.
	public override bool Generate()
	{
		if (changed)
		{
			changed = false;
			return true;
		}

		return false;
	}
	
	// This Method is called if the function/method speak is invoked by the user or a bound event.
	// Return true, if this component has to be revalidated!
	public bool Onspeak(int arg)
	{
		
		return false;
	}
	
	// This Method is called if the function/method Method1 is invoked by the user or a bound event.
	// Return true, if this component has to be revalidated!
	public bool OnMethod1(int arg)
	{
		return false;
	}

}
	

Re: c# ventuz help

Posted: 30 Jan 2015, 23:35
by zoidberg
Hi,

sRecognize does not exist: Please check following line in your code:

Code: Select all

SpeechRecognitionEngine sReconize = new SpeechRecognitionEngine();
The g is missing in sReconize. That`s the reason for this error.

sRecognize_SpeechRecognized does not exist: You have to add this method at the end of the script node class:

Code: Select all

private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result.Text == "play")
            {
                Play();
            }
         }
But you have to add the method PLAY as well to this script node. Otherwise you will get the next error message.

Here is the whole script node code. But SAVE ist missing ;-)

Code: Select all

using System;
using Ventuz.Kernel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Threading;



public class Script : ScriptBase, System.IDisposable
{
    
	// This member is used by the Validate() method to indicate
	// whether the Generate() method should return true or false
	// during its next execution.
	private bool changed;
    
	// This Method is called if the component is loaded/created.
	public Script()
	{
		// Note: Accessing input or output properties from this method
		// will have no effect as they have not been allocated yet.
	}
    
	// This Method is called if the component is unloaded/disposed
	public virtual void Dispose()
	{
   
      
	}
    
	// This Method is called if an input property has changed its value
	public override void Validate()
	{
		// Remember: set changed to true if any of the output 
		// properties has been changed, see Generate()
      
		SpeechSynthesizer sSynth = new SpeechSynthesizer();
		PromptBuilder pBuilder = new PromptBuilder();
		SpeechRecognitionEngine sRecognize = new SpeechRecognitionEngine();
      
      
      
		pBuilder.ClearContent();
		pBuilder.AppendText(text);
		sSynth.Speak(pBuilder);
      
		Choices sList = new Choices();
		sList.Add(new string[] {"play","stop"});
		Grammar gr = new Grammar (new GrammarBuilder(sList));
		try
		{
			sRecognize.RequestRecognizerUpdate();
			sRecognize.LoadGrammar(gr);
			sRecognize.SpeechRecognized += sRecognize_SpeechRecognized;
			sRecognize.SetInputToDefaultAudioDevice();
			sRecognize.RecognizeAsync(RecognizeMode.Multiple);
			sRecognize.Recognize();
		}
		catch
		{
			return;   
		}   
      
	}
    
	// This Method is called every time before a frame is rendered.
	// Return value: if true, Ventuz will notify all nodes bound to this
	//               script node that one of the script's outputs has a
	//               new value and they therefore need to validate. For
	//               performance reasons, only return true if output
	//               values really have been changed.
	public override bool Generate()
	{
		if (changed)
		{
			changed = false;
			return true;
		}

		return false;
	}
   
	// This Method is called if the function/method speak is invoked by the user or a bound event.
	// Return true, if this component has to be revalidated!
	public bool Onspeak(int arg)
	{
      
		return false;
	}
   
	// This Method is called if the function/method Method1 is invoked by the user or a bound event.
	// Return true, if this component has to be revalidated!
	public bool OnMethod1(int arg)
	{
		return false;
	}
	
	private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
	{
		if (e.Result.Text == "play")
		{
			Play();
		}
	}
}
   
Best regards,
Anton

Re: c# ventuz help

Posted: 05 Feb 2015, 11:50
by hgoodsir
cool thanks Anton :)