public void analyze()
        {

            bfpa = this.parent.bodyFrameHandler.bodyFramePreAnalysis;
            apa = this.parent.audioHandler.audioPreAnalysis;

            searchMistakes();

            saveCurrentBodyAsOldBody();

            
        }
        public AudioHandler(KinectSensor kinectSensor)
        {
            audioPreAnalysis = new AudioPreAnalysis();
            this.kinectSensor = kinectSensor;
            // Get its audio source
            AudioSource audioSource = this.kinectSensor.AudioSource;

            // Allocate 1024 bytes to hold a single audio sub frame. Duration sub frame 
            // is 16 msec, the sample rate is 16khz, which means 256 samples per sub frame. 
            // With 4 bytes per sample, that gives us 1024 bytes.
            this.audioBuffer = new byte[audioSource.SubFrameLengthInBytes];

            // Open the reader for the audio frames
            this.reader = audioSource.OpenReader();

           // PixelFormats.Indexed1;
            this.energyBitmap = new WriteableBitmap(EnergyBitmapWidth, EnergyBitmapHeight, 96, 96, PixelFormats.Indexed4, new BitmapPalette(new List<Color> { Colors.White, Colors.Green, Colors.Red, Colors.LightBlue, Colors.Aquamarine, Colors.Pink, Colors.Orange}));

            // Initialize foreground pixels
            this.foregroundPixels = new byte[EnergyBitmapHeight];
            for (int i = 0; i < this.foregroundPixels.Length; ++i)
            {
                this.foregroundPixels[i] = 0xff;
            }
            for (int i = 0; i < this.lineVolumePixels.Length; ++i)
            {
                this.lineVolumePixels[i] = 0x55;
            }
            for (int i = 0; i < this.lineVolumePixelsIsSpeaking.Length; ++i)
            {
                this.lineVolumePixelsIsSpeaking[i] = 0x11;
            }
            for (int i = 0; i < this.lineVolumePixelsSoft.Length; ++i)
            {
                this.lineVolumePixelsSoft[i] = 0x22;
            }
            for (int i = 0; i < this.lineVolumePixelsLoud.Length; ++i)
            {
                this.lineVolumePixelsLoud[i] = 0x33;
            }

        //    this.kinectImage.Source = this.energyBitmap;
            CompositionTarget.Rendering += this.UpdateEnergy;
            
            

            //if (this.reader != null)
            //{
            //    // Subscribe to new audio frame arrived events
            //    this.reader.FrameArrived += this.Reader_FrameArrived;
            //}


            getSpeechthings();

        }
        public void AnalyseRules()
        {
            noMistake = true;
            bfpa = this.parent.bodyFrameHandler.bodyFramePreAnalysis;
            apa = this.parent.audioHandler.audioPreAnalysis;
   
            checkPeriodicMovements();
           

            //DETECT HAND MOVEMENT CODE 
            bfpa.calcIsMovingArms();
             /**Structure of switch blocks: 
             * If good:
             - end mistake that is busy, if there is one.
             * If not good:
             -Same Type Mistake Busy
             --Change gravity to worst kind
             --set bool to true
             -Different Type Mistake Busy
             --End that mistake and add the new one
             --set bool to true
             -No Mistake Busy
             --Start new mistake
             --set bool to true
             * */

             volumeMistake = false;
             cadenceMistake = false;
             postureMistake = false;
             handMovementMistake = false;

            if(parent.freestyleMode!=null)
            {
                if (parent.freestyleMode.myState == PresentationTrainer.FreestyleMode.currentState.play)
                {
                    checkMistakes();
                    checkStyle();
                    Mistake biggestMistake = findBiggestMistake();
                    //Style biggestStyle = findBiggestStyle();
                    fireBiggestEvent(biggestMistake, null);
                    
                    
                }
            }
             
             saveCurrentBodyAsOldBody();
             
        }