Esempio n. 1
0
        public MainWindow()
        {
            InitializeComponent();
            this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

            this.KeyDown += new KeyEventHandler(MainWindow_KeyDown);

            // Print out all the possible input devices to console. Mostly for debugging.
            int waveInDevices = WaveIn.DeviceCount;

            for (int waveInDevice = 0; waveInDevice < waveInDevices; waveInDevice++)
            {
                WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
                Console.WriteLine("Device {0}: {1}, {2} channels",
                                  waveInDevice, deviceInfo.ProductName, deviceInfo.Channels);
            }

            // Instantiate a waveIn device and start recording.
            waveIn = new WaveIn();
            waveIn.BufferMilliseconds = 47 * buffersize / 2048;
            waveIn.DeviceNumber       = 0;
            waveIn.WaveFormat         = new WaveFormat(44100, 32, 1);
            waveIn.DataAvailable     += waveIn_DataAvailable;

            try
            {
                waveIn.StartRecording();
            }
            catch (NAudio.MmException e)
            {
                Console.WriteLine(e.ToString() + "\nPlug in a microphone!");
            }

            history         = new List <List <int> >();
            inverse_history = new List <List <int> >();
            pointHist       = new PointCollection();

            bin        = new int[buffersize * 2];
            sampledata = new float[buffersize * 2];
            priori     = new double[buffersize * 2];


            //Initializing all the global variables to base values for 1 speaker configuration.
            channelLabel    = new int[1];
            channelLabel[0] = 1;
            velocity        = new int[1];
            velocity[0]     = 0;

            prev_displacement    = new int[1];
            prev_displacement[0] = 0;

            instant_displacement    = new int[1];
            instant_displacement[0] = 0;

            towards_displacement    = new int[1];
            towards_displacement[0] = 1;


            displacement    = new int[1];
            displacement[0] = 0;

            for (int i = 0; i < buffersize * 2; i++)
            {
                bin[i]        = i;
                sampledata[i] = 0;
                priori[i]     = 0;
            }

            // Kalman filter related stuff.
            filter = new VDKalman(2);
            filter.initialize(1, .1, 1, 0);

            // To prevent problems with empty lists, we assume 1 channel to start.
            history.Add(new List <int> {
                0
            });
            inverse_history.Add(new List <int> {
                0
            });

            // Load up the classifier model file.
            WekaHelper.initialize();
        }
Esempio n. 2
0
        public void gestureCompleted()
        {
            // Minimum number of frames of no motion to be segmented as a gesture
            int motion_threshold = 3; //originally 5

            // Minimum length of time after a gesture is completed before another gesture can be started.
            int ignore_threshold = 10;

            // If users are still reseting their hands, ignore all the movements and clear all buffers.
            if (ignoreFrames <= ignore_threshold)
            {
                motion_free         = 0;
                readyforgesture     = false;
                colorBox.Background = new SolidColorBrush(Colors.Red);
                gesture_started     = false;
                //Clear the buffers
                foreach (List <int> sublist in history)
                {
                    sublist.Clear();
                }
                foreach (List <int> sublist in inverse_history)
                {
                    sublist.Clear();
                }
                pointHist.Clear();
            }

            if (gesture_started && ignoreFrames > ignore_threshold && motion_free > motion_threshold && selectedChannels >= 2)
            {
                // Use LINQ to remove all the frames at the end that correspond to the motion free periods.
                pointHist = new PointCollection(pointHist.Reverse().Skip(motion_threshold).Reverse());
                S         = new StylusPointCollection(S.Reverse().Skip(motion_threshold).Reverse());
                for (int i = 0; i < history.Count; i++)
                {
                    history[i]         = new List <int>(history[i].Reverse <int>().Skip(motion_threshold).Reverse <int>());
                    inverse_history[i] = new List <int>(inverse_history[i].Reverse <int>().Skip(motion_threshold).Reverse <int>());
                }

                //If we are in detect mode, pass it to WEKA for classification.
                if (detectMode.IsChecked.Value && pointHist.Count > 9)
                {
                    //Call function to find features and test with weka machine
                    if (selectedChannels == 2)
                    {
                        float[] speakers = { (float)KF[0].speakerTheta, (float)KF[1].speakerTheta };
                        //temp stores the string identifier of the gesture
                        string temp = WekaHelper.Classify(false, pointHist.Count() * waveIn.BufferMilliseconds,
                                                          true, new List <float>(speakers), pointHist, S, history, inverse_history);

                        //switch statement to rename up/down gestures to forward/back when displaying in the application
                        switch (temp)
                        {
                        case "swipe_up":
                            temp = "swipe_forward";
                            break;

                        case "swipe_down":
                            temp = "swipe_back";
                            break;

                        case "tap_up":
                            temp = "tap_forward";
                            break;

                        case "tap_down":
                            temp = "tap_back";
                            break;
                        }
                        gestureDetected.Text = temp;

                        //TODO Put interaction with other applications in this switch statement
                        // Allows for changing between workspaces in windows 10.
                        if (shellIntegration.IsChecked.Value)
                        {
                            switch (temp)
                            {
                            case "swipe_forward":
                                sim.Keyboard.KeyDown(VirtualKeyCode.LWIN);
                                sim.Keyboard.KeyPress(VirtualKeyCode.TAB);
                                sim.Keyboard.KeyUp(VirtualKeyCode.LWIN);
                                break;

                            case "swipe_back":
                                break;

                            case "swipe_left":
                                if (!chrome)
                                {
                                    sim.Keyboard.KeyDown(VirtualKeyCode.LWIN);
                                    sim.Keyboard.KeyDown(VirtualKeyCode.LCONTROL);
                                    sim.Keyboard.KeyPress(VirtualKeyCode.LEFT);
                                    sim.Keyboard.KeyUp(VirtualKeyCode.LWIN);
                                    sim.Keyboard.KeyUp(VirtualKeyCode.LCONTROL);
                                }
                                else
                                {
                                    sim.Keyboard.KeyDown(VirtualKeyCode.LCONTROL);
                                    sim.Keyboard.KeyDown(VirtualKeyCode.LSHIFT);
                                    sim.Keyboard.KeyPress(VirtualKeyCode.TAB);
                                    sim.Keyboard.KeyUp(VirtualKeyCode.LSHIFT);
                                    sim.Keyboard.KeyUp(VirtualKeyCode.LCONTROL);
                                }
                                break;

                            case "swipe_right":
                                if (!chrome)
                                {
                                    sim.Keyboard.KeyDown(VirtualKeyCode.LWIN);
                                    sim.Keyboard.KeyDown(VirtualKeyCode.LCONTROL);
                                    sim.Keyboard.KeyPress(VirtualKeyCode.RIGHT);
                                    sim.Keyboard.KeyUp(VirtualKeyCode.LWIN);
                                    sim.Keyboard.KeyUp(VirtualKeyCode.LCONTROL);
                                }
                                else
                                {
                                    sim.Keyboard.KeyDown(VirtualKeyCode.LCONTROL);
                                    sim.Keyboard.KeyPress(VirtualKeyCode.TAB);
                                    sim.Keyboard.KeyUp(VirtualKeyCode.LCONTROL);
                                }
                                break;

                            case "tap_forward":
                                chrome = true;
                                break;

                            case "tap_back":
                                chrome = false;
                                break;

                            case "tap_left":
                                break;

                            case "tap_right":
                                break;
                            }
                        }
                    }


                    ignoreFrames = 0;
                }
                // Clear the buffers
                foreach (List <int> sublist in history)
                {
                    sublist.Clear();
                }
                foreach (List <int> sublist in inverse_history)
                {
                    sublist.Clear();
                }
                pointHist.Clear();

                // Prepare for next gesture (might need a button press)
                readyforgesture     = false;
                colorBox.Background = new SolidColorBrush(Colors.Red);
                gesture_started     = false;
                motion_free         = 0;
            }
        }