<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" media="screen" href="/styles/xslt/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:c9="http://channel9.msdn.com">
<channel>
	<title>Channel 9 Forums - Tech Off - Kinect Speech Help</title>
	<atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/Forums/rss"></atom:link>
	<image>
		<url>http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.png</url>
		<title>Channel 9 Forums - Tech Off - Kinect Speech Help</title>
		<link>http://channel9.msdn.com/Forums</link>
	</image>
	<description>Channel 9 keeps you up to date with the latest news and behind the scenes info from Microsoft that developers love to keep up with. From LINQ to SilverLight – Watch videos and hear about all the cool technologies coming and the people behind them.</description>
	<link>http://channel9.msdn.com/Forums</link>
	<language>en</language>
	<pubDate>Tue, 21 May 2013 12:27:01 GMT</pubDate>
	<lastBuildDate>Tue, 21 May 2013 12:27:01 GMT</lastBuildDate>
	<generator>Rev9</generator>
	<c9:totalResults>2</c9:totalResults>
	<c9:pageCount>-2</c9:pageCount>
	<c9:pageSize>-1</c9:pageSize>
	<item>
		<title>Tech Off - Kinect Speech Help</title>
		<description><![CDATA[<p>Hi,<br>I'm making an C# application that uses the Kinects Skeletal tracking and its voice recognition as an method of input. I've got to a point where the application uses skeletal data to position a cursor and I'm trying to integrate voice commands.<br>I have run into a problem, where I have created and started the speech recogniser but I can't get the react to any audio inputs ( display whether the speech command was recognised). <br>Any help will be greatly appreciated, thanks.</p><p><pre class="brush: csharp">using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
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.Threading;
using System.Windows.Shapes;
using Microsoft.Kinect;
using Coding4Fun.Kinect.Wpf;
using Microsoft.Speech.AudioFormat;
using Microsoft.Speech.Recognition;

namespace SkeletalTracking
{
    /// &lt;summary&gt;
    /// Interaction logic for MainWindow.xaml
    /// &lt;/summary&gt;
    public partial class MainWindow : Window
    {
        // Variables
        bool closing = false;
        const int skeletonCount = 6;
        Skeleton[] allSkeletons = new Skeleton[skeletonCount];
        private SpeechRecognitionEngine speechRecognizer;
        private KinectAudioSource kinectAudioSource;
        private KinectSensor kinect;

        public MainWindow()
        {
            InitializeComponent();
            // Set up the Kinect
            kinectSensorChooser1.KinectSensorChanged &#43;= this.kinectSensorChooser1_KinectSensorChanged;
        }

        void kinectSensorChooser1_KinectSensorChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            KinectSensor old = (KinectSensor)e.OldValue as KinectSensor;

            StopKinect(old);

            KinectSensor sensor = (from sensorToCheck in KinectSensor.KinectSensors where sensorToCheck.Status == KinectStatus.Connected select sensorToCheck).FirstOrDefault();
            if (sensor == null)
            {
                Console.WriteLine(
                        &quot;No Kinect sensors are attached to this computer or none of the ones that are\n&quot; &#43;
                        &quot;attached are \&quot;Connected\&quot;.\n&quot; &#43;
                        &quot;Attach the KinectSensor and restart this application.\n&quot; &#43;
                        &quot;If that doesn't work run SkeletonViewer-WPF to better understand the Status of\n&quot; &#43;
                        &quot;the Kinect sensors.\n\n&quot; &#43;
                        &quot;Press any key to continue.\n&quot;);

                // Give a chance for user to see console output before it is dismissed
                Console.ReadKey(true);
                return;
            }

            this.kinect = sensor;
            if (sensor != null)
            {
                this.InitializeKinect();
            }
        }

        private void InitializeKinect()
        {
            // ===========KINECT SENSOR============================================================== //
            // Set up the sensor for the Kinect 
            var sensor = this.kinect;

            var parameters = new TransformSmoothParameters
            {
                Smoothing = 0.3f,
                Correction = 0.0f,
                Prediction = 0.0f,
                JitterRadius = 1.0f,
                MaxDeviationRadius = 0.5f
            };

            try
            {
                sensor.SkeletonStream.Enable(parameters);
                // sensor.SkeletonStream.Enable();

                sensor.AllFramesReady &#43;= new EventHandler&lt;AllFramesReadyEventArgs&gt;(sensor_AllFramesReady);
                sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
                sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                sensor.Start();

            }
            catch (System.IO.IOException)
            {
                kinectSensorChooser1.AppConflictOccurred();
            }

            // ===========KINECT SPEECH RECOGNIZER=================================================== //
            // Setup the speech recognizer            
            this.speechRecognizer = CreateSpeechRecognizer();

            if (this.speechRecognizer != null &amp;&amp; sensor != null)
            {
                StartRecognizer(sensor.AudioSource);
            }

        }

        private SpeechRecognitionEngine CreateSpeechRecognizer()
        {
            SpeechRecognitionEngine sre;                    //Creates the speech recognition engine
            RecognizerInfo ri = GetKinectRecognizer();      //Creates the speech recognizer info

            if (ri == null)
            {
                MessageBox.Show(@&quot;There was a problem initializing Speech Recognition.Ensure you have the Microsoft Speech SDK installed.&quot;,
                    &quot;Failed to load Speech SDK&quot;,
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
                this.Close();
                return null;
            }

            try
            {
                sre = new SpeechRecognitionEngine(ri.Id);
            }
            catch
            {
                MessageBox.Show(@&quot;There was a problem initializing Speech Recognition.Ensure you have the Microsoft Speech SDK installed and configured.&quot;,
                        &quot;Failed to load Speech SDK&quot;,
                        MessageBoxButton.OK,
                        MessageBoxImage.Error);
                this.Close();
                return null;
            }

            var grammar = new Choices();
            grammar.Add(&quot;red&quot;);
            grammar.Add(&quot;green&quot;);
            grammar.Add(&quot;blue&quot;);

            var gb = new GrammarBuilder { Culture = ri.Culture };
            gb.Append(grammar);

            // Create the actual Grammar instance, and then load it into the speech recognizer.
            var g = new Grammar(gb);

            sre.LoadGrammar(g);
            sre.SpeechRecognized &#43;= this.SreSpeechRecognized;
            sre.SpeechHypothesized &#43;= this.SreSpeechHypothesized;
            sre.SpeechRecognitionRejected &#43;= this.SreSpeechRecognitionRejected;
            return sre;
        }

        private void StartRecognizer(KinectAudioSource kinectSource)
        {

            // Obtain the KinectAudioSource to do audio capture
            this.kinectAudioSource = kinectSource;
            this.kinectAudioSource.AutomaticGainControlEnabled = false;
            this.kinectAudioSource.EchoCancellationMode = EchoCancellationMode.None;    // No AEC for this sample
            this.kinectAudioSource.AutomaticGainControlEnabled = false;                 // Important to turn this off for speech recognition

            using (Stream s = kinectAudioSource.Start())
            {
                speechRecognizer.SetInputToAudioStream(s,
                                                       new SpeechAudioFormatInfo(
                                                           EncodingFormat.Pcm, 16000, 16, 1,
                                                           32000, 2, null));             //creates the audio stream for the use of a single Kinect

                speechRecognizer.RecognizeAsync(RecognizeMode.Multiple);
            }
        }



        private RecognizerInfo GetKinectRecognizer()
        //Creates the Kinect Recogizer used for the voice commands 
        {
            Func&lt;RecognizerInfo, bool&gt; matchingFunc = r =&gt;
            {
                string value;
                r.AdditionalInfo.TryGetValue(&quot;Kinect&quot;, out value);
                return &quot;True&quot;.Equals(value, StringComparison.InvariantCultureIgnoreCase) &amp;&amp; &quot;en-US&quot;.Equals(r.Culture.Name, StringComparison.InvariantCultureIgnoreCase);
            };
            return SpeechRecognitionEngine.InstalledRecognizers().Where(matchingFunc).FirstOrDefault();
        }



        private void SreSpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
        {
            Console.WriteLine(&quot;\nSpeech Rejected&quot;);
        }

        private void SreSpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
        {
            Console.Write(&quot;\rSpeech Hypothesized: \t{0}&quot;, e.Result.Text);
        }

        private void SreSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            Console.WriteLine(&quot;\nSpeech Recognized as: \t{0}&quot;, e.Result.Text);
        }

        void sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            if (closing)
            {
                return;
            }

            //Get a skeleton
            Skeleton first = GetFirstSkeleton(e);

            if (first == null)
            {
                return;
            }

            //set scaled position
            ScalePosition(rightEllipse, first.Joints[JointType.HandRight]);
        }

        void GetCameraPoint(Skeleton first, AllFramesReadyEventArgs e)
        {

            using (DepthImageFrame depth = e.OpenDepthImageFrame())
            {
                if (depth == null ||
                    kinectSensorChooser1.Kinect == null)
                {
                    return;
                }

                //Map a joint location to a point on the depth map
                //right hand
                DepthImagePoint rightDepthPoint =
                    depth.MapFromSkeletonPoint(first.Joints[JointType.HandRight].Position);

                //Map a depth point to a point on the color image
                //head
                ColorImagePoint rightColorPoint =
                    depth.MapToColorImagePoint(rightDepthPoint.X, rightDepthPoint.Y,
                    ColorImageFormat.RgbResolution640x480Fps30);


                //Set location
                CameraPosition(rightEllipse, rightColorPoint);
            }
        }


        Skeleton GetFirstSkeleton(AllFramesReadyEventArgs e)
        {
            using (SkeletonFrame skeletonFrameData = e.OpenSkeletonFrame())
            {
                if (skeletonFrameData == null)
                {
                    return null;
                }

                skeletonFrameData.CopySkeletonDataTo(allSkeletons);

                //get the first tracked skeleton
                Skeleton first = (from s in allSkeletons
                                  where s.TrackingState == SkeletonTrackingState.Tracked
                                  select s).FirstOrDefault();

                return first;

            }
        }

        private void StopKinect(KinectSensor sensor)
        {
            if (sensor != null)
            {
                if (sensor.IsRunning)
                {
                    //stop sensor 
                    sensor.Stop();

                    //stop audio if not null
                    if (sensor.AudioSource != null)
                    {
                        sensor.AudioSource.Stop();
                    }
                }
            }
        }

        private void CameraPosition(FrameworkElement element, ColorImagePoint point)
        {
            //Divide by 2 for width and height so point is right in the middle 
            // instead of in top/left corner
            Canvas.SetLeft(element, point.X - element.Width / 2);
            Canvas.SetTop(element, point.Y - element.Height / 2);
        }

        private void ScalePosition(FrameworkElement element, Joint joint)
        {
            //convert &amp; scale (.3 = means 1/3 of joint distance)
            Joint scaledJoint = joint.ScaleTo(1280, 1280, .2f, .2f);

            Canvas.SetLeft(element, scaledJoint.Position.X);
            Canvas.SetTop(element, scaledJoint.Position.Y);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            closing = false;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            closing = true;
            StopKinect(kinectSensorChooser1.Kinect);
        }
    }
}
</pre></p><p>&nbsp;</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/Kinect-Speech-Help/dec9cc16fe82413d92eea02a00fcde85#dec9cc16fe82413d92eea02a00fcde85</link>
		<pubDate>Thu, 05 Apr 2012 15:20:40 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/Kinect-Speech-Help/dec9cc16fe82413d92eea02a00fcde85#dec9cc16fe82413d92eea02a00fcde85</guid>
		<dc:creator>Largo010</dc:creator>
		<slash:comments>2</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Largo010/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - Kinect Speech Help</title>
		<description><![CDATA[<p><strong>There is a documented bug in the Kinect SDK: If the Skeletal Recognition engine is&nbsp;</strong><br><strong>initialized after the audio sensor, then the audio sensor will be stopped.&nbsp;</strong></p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/Kinect-Speech-Help/13f7014481b24f5f82e8a118010cfe38#13f7014481b24f5f82e8a118010cfe38</link>
		<pubDate>Thu, 29 Nov 2012 16:19:22 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/Kinect-Speech-Help/13f7014481b24f5f82e8a118010cfe38#13f7014481b24f5f82e8a118010cfe38</guid>
		<dc:creator>skydancerbg</dc:creator>
		<slash:comments>2</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/skydancerbg/Discussions/RSS</wfw:commentRss>
	</item>
</channel>
</rss>