void MainWindow_Closed(object sender, EventArgs e)
 {
     recordState = ErecordState.cancel;
     subjectDisplayTimer.Dispose();
     voice.Dispose();
     kinect.Dispose();
 }
 private void Cancel_Click(object sender, RoutedEventArgs e)
 {
     lastFrame   = -1;
     recordState = ErecordState.cancel;
     SendMessageToClients("cancel");
 }
 private void Stop_Click(object sender, RoutedEventArgs e)
 {
     lastFrame   = kinect.GetLastFrame().FrameNumber;
     recordState = ErecordState.stop;
     SendMessageToClients("stop");
 }
 void Record_Elapsed(object sender, EventArgs e)
 {
     lastFrame   = kinect.GetLastFrame().FrameNumber;
     recordState = ErecordState.done;
     SendMessageToClients("done");
 }
        bool Record(string identifier, string exercise, string path, int duration, string filename, bool readInstructions)
        {
            TimeSpan gapBetweenCalls = new TimeSpan(0, 0, 5);

            recordState = ErecordState.run;
            if (readInstructions == true)
            {
                voice.SpeakBlocking(Exercises.GetDescription(exercise));
            }
            Thread.Sleep(2000);

            if (recordState != ErecordState.run)
            {
                return(false);
            }
            for (int i = countdown; i > 0; i--)
            {
                voice.Speak(i.ToString());
                Thread.Sleep(1500);
                if (recordState != ErecordState.run)
                {
                    return(false);
                }
            }
            kinect.intensityBasedCutoffThreshold = recordingSettings.reflectiveSeperation ? ushort.Parse(ConfigurationManager.AppSettings["reflectiveSeperationCutoff"]) : (ushort)(0);
            voice.Speak("Go!");
            firstFrame = kinect.GetLastFrame().FrameNumber;
            SendMessageToClients("record", identifier, exercise, path, duration.ToString(), filename);
            recordState          = ErecordState.run;
            timeCounter          = new Timer(duration * 1000);
            timeCounter.Elapsed += Record_Elapsed;
            timeCounter.Start();
            DateTime startTime = DateTime.Now;
            DateTime nextRead  = startTime + gapBetweenCalls;

            try
            {
                while (recordState == ErecordState.run)
                {
                    if (DateTime.Now > nextRead)
                    {
                        var  timeCount             = (DateTime.Now - startTime).TotalMilliseconds;
                        long collectedKinectFrames = kinect.GetLastFrame().FrameNumber - firstFrame;

                        if (audiobaleWarnings && collectedKinectFrames < timeCount * 0.95 * 30.0 / 1000.0)
                        {
                            voice.Speak("Alert: too many frames lost!");
                        }
                        else
                        {
                            voice.Speak(((int)(timeCount / 1000)).ToString());
                        }
                        nextRead += gapBetweenCalls;
                    }
                    Thread.Sleep(10);
                }
                voice.SpeakBlocking("done");
            }
            finally
            {
                timeCounter.Dispose();
            }
            return(true);
        }
        private void Start_Click(object sender, RoutedEventArgs e)
        {
            ClearLog();

            kinect.ClearMemory();
            var path       = currentIdentifier.Path;
            var identifier = currentIdentifier.Identifier;
            var exercise   = currentIdentifier.Exercise;

            if (string.IsNullOrEmpty(identifier))
            {
                Notify(ELevel.warning, "please specify an identifier");
                return;
            }
            if (string.IsNullOrEmpty(exercise))
            {
                Notify(ELevel.warning, "please select the exercise to be performed");
                return;
            }
            int durationInt = -1;
            var flag        = Int32.TryParse(duration.Text, out durationInt);

            if (!flag || durationInt <= 0)
            {
                Notify(ELevel.warning, "please select the duration of the exercise");
                return;
            }

            Notify(ELevel.information, "Start recording");
            var    extendedIdentifier = System.IO.Path.Combine(path, String.Format("{0}_{1}", identifier, DateTime.Now.ToString(Tools.dateFormat)));
            string filename           = extendedIdentifier + "_" + exercise + "_kinect.xml";
            string metricFileName     = extendedIdentifier + "_" + exercise + "_analysis.tsv";
            bool   readInstructions   = recordingSettings.playInstructions == true;

            worker = new BackgroundWorker
            {
                WorkerSupportsCancellation = true
            };
            worker.DoWork += delegate(object s, DoWorkEventArgs args)
            {
                args.Result = Record(identifier, exercise, path, durationInt, filename, readInstructions);
            };
            if (recordingCounter <= 0)
            {
                recordingCounter = recordingsPerSession;
            }
            Notify(ELevel.information, "Recordings left in this session: {0}", recordingCounter);
            worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
            {
                switch (recordState)
                {
                case ErecordState.cancel:
                    Notify(ELevel.warning, "Recording canceled");
                    recordingCounter = 0;
                    break;

                case ErecordState.done:
                case ErecordState.stop:
                    Notify(ELevel.information, "Recording completed");
                    BackgroundWorker analysisWorker = new BackgroundWorker();
                    analysisWorker.DoWork += delegate(object s1, DoWorkEventArgs args1)
                    {
                        kinect.WriteToFile(filename, firstFrame, lastFrame);
                        var analyzer = Exercises.GetAnalyzer(exercise);
                        var metrics  = analyzer.analyzeAndAnnotate(filename);
                        metrics.save(metricFileName);
                    };
                    analysisWorker.RunWorkerAsync();
                    break;

                default:
                    Notify(ELevel.failure, "Internal error: recordState == {0}", Enum.GetName(typeof(ErecordState), recordState));
                    break;
                }
                recordState      = ErecordState.idle;
                Start.Visibility = System.Windows.Visibility.Visible;
                Stop.Visibility  = System.Windows.Visibility.Hidden;
                if (--recordingCounter > 0)
                {
                    Start_Click(sender, e);
                }
                else if (autoShutDown)
                { // wait for the analysis to terminate and exit
                    Thread.Sleep(5000);
                    Environment.Exit(0);
                }
            };
            worker.RunWorkerAsync();
            Start.Visibility = System.Windows.Visibility.Hidden;
            Stop.Visibility  = System.Windows.Visibility.Visible;
        }