Exemplo n.º 1
0
 /// <summary>
 /// Simply reraises the events it's subscribed to
 /// </summary>
 /// <param name="o"></param>
 /// <param name="e"></param>
 public void HandleInputEvent(object o, PianoKeyStrokeEventArgs e)
 {
     if (MessageReceived != null)
     {
         MessageReceived(o, e);
     }
 }
Exemplo n.º 2
0
        public static PianoKeyStrokeEventArgs ConvertChannelMessageToKeyStrokeEventArgs(ChannelMessage channelMessage)
        {
            var pksea = new PianoKeyStrokeEventArgs();

            //If the key stroke is neither a press or a release, return a null object
            switch (channelMessage.Command)
            {
            case ChannelCommand.NoteOn:
                pksea.KeyStrokeType = KeyStrokeType.KeyPress;
                break;

            case ChannelCommand.NoteOff:
                pksea.KeyStrokeType = KeyStrokeType.KeyRelease;
                break;

            default: return(null);
            }

            pksea.midiKeyId   = channelMessage.Data1;
            pksea.KeyVelocity = channelMessage.Data2;

            //Velocity 0 is effectively a key release
            if (pksea.KeyVelocity == 0)
            {
                pksea.KeyStrokeType = KeyStrokeType.KeyRelease;
            }

            return(pksea);
        }
Exemplo n.º 3
0
        private void HandleInputEvent(object sender, PianoKeyStrokeEventArgs e)
        {
            if (e == null)
            {
                return;
            }
            if (e.KeyStrokeType != KeyStrokeType.KeyPress)
            {
                return;
            }

            //Get the current note time from the playing song
            double noteTime = _songEventController.GetCurrentNoteTime();

            //Check if note played is in any of these 3 note times.
            bool   inPreviousNotes;
            bool   inNextNotes;
            double compareNoteTime = -1;

            inPreviousNotes = _song[_lastNoteTime].KeyPresses.Any(n => n.PitchId == e.midiKeyId);
            inNextNotes     = _song[_nextNoteTime].KeyPresses.Any(n => n.PitchId == e.midiKeyId);

            if (inPreviousNotes)
            {
                compareNoteTime = _lastNoteTime;
            }
            if (inNextNotes)
            {
                compareNoteTime = _nextNoteTime;
            }

            int markForNote = 0;

            if (compareNoteTime == -1)
            {
                markForNote = 0;
            }
            else
            {
                double accuracy = Math.Abs(noteTime - compareNoteTime);
                if (accuracy < 1.0 / 4)
                {
                    markForNote = 10;
                }
                else if (accuracy < 1.0 / 2)
                {
                    markForNote = 6;
                }
                else if (accuracy < 1.1)
                {
                    markForNote = 4;
                }
                else
                {
                    markForNote = 1;
                }
            }

            _scoreParser.MarkNote(compareNoteTime, markForNote, e.midiKeyId);
        }
Exemplo n.º 4
0
        public static ChannelMessage ConvertKeyStrokeEventArgsToChannelMessage(PianoKeyStrokeEventArgs keyStrokeEventArgs)
        {
            if (keyStrokeEventArgs == null)
            {
                return(null);
            }

            var channelCommand = new ChannelCommand();
            int data1          = keyStrokeEventArgs.midiKeyId;
            int data2          = keyStrokeEventArgs.KeyVelocity;

            //If the key stroke is neither a press or a release, return a null object
            switch (keyStrokeEventArgs.KeyStrokeType)
            {
            case KeyStrokeType.KeyPress:
                channelCommand = ChannelCommand.NoteOn;
                break;

            case KeyStrokeType.KeyRelease:
                channelCommand = ChannelCommand.NoteOn;
                data2          = 0;
                break;

            default: return(null);
            }

            return(new ChannelMessage(channelCommand, 1, data1, data2));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Handles the Midi message received
        /// Only called if in recording mode
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HandleChannelMessageReceived(object sender, PianoKeyStrokeEventArgs e)
        {
            m_Session.Record(SanfordUtils.ConvertKeyStrokeEventArgsToChannelMessage(e));

            //_keyboard.HandleIncomingMessage(null, new PianoKeyStrokeEventArgs(_testKeyId, KeyStrokeType.KeyRelease, 100));

            this.NumMsgsReceived++;
        }
Exemplo n.º 6
0
        private void HandleInputDeviceInput(object o, PianoKeyStrokeEventArgs e)
        {
            if (_testActive)
            {
                if (e.KeyStrokeType == KeyStrokeType.KeyPress)
                {
                    int keyDifference = Math.Abs(_testKeyId - e.midiKeyId);

                    if (keyDifference == 0)
                    {
                        Result = "Nailed it - you're a hero";
                    }
                    else if (keyDifference % 12 == 0)
                    {
                        Result = "Hmm, try another octave";
                    }
                    else if (keyDifference > 11)
                    {
                        Result = "Seriously??! Do your ears even work?";
                    }
                    else if (keyDifference > 3)
                    {
                        Result = "Not bad, for a deaf beach boy";
                    }
                    else
                    {
                        Result = "Only marginally disappointing";
                    }

                    var worker = new BackgroundWorker();
                    worker.DoWork += (ob, arg) => { Thread.Sleep(1000); };

                    _keyboard.HandleIncomingMessage(null, new PianoKeyStrokeEventArgs(_testKeyId, KeyStrokeType.KeyPress, 100));

                    //I need to be a bit more careful here
                    //If this method is called again too quickly, the lift key event won't fire
                    worker.RunWorkerCompleted += (ob, arg) =>
                    {
                        _keyboard.HandleIncomingMessage(null, new PianoKeyStrokeEventArgs(_testKeyId, KeyStrokeType.KeyRelease, 100));
                        if (!_testActive)
                        {
                            Result = "...go again..";
                        }
                    };

                    worker.RunWorkerAsync();

                    TestActive = false;
                }
            }
        }
Exemplo n.º 7
0
        public void HandleIncomingMessage(object sender, PianoKeyStrokeEventArgs e)
        {
            if (e == null) return;

            if (_keyDictionary.Keys.ContainsKey(e.midiKeyId))
            {
                switch (e.keyStrokeType)
                {
                    case KeyStrokeType.KeyPress:
                        _keyDictionary.Keys[e.midiKeyId].SetKeyPressedColour(e.KeyVelocity);
                        break;
                    case KeyStrokeType.KeyRelease:
                        _keyDictionary.Keys[e.midiKeyId].SetDefaultKeyColour();
                        break;
                }
            }
        }
Exemplo n.º 8
0
 public void Send(object sender, PianoKeyStrokeEventArgs args)
 {
     if (args == null)
     {
         return;
     }
     if (IsInitialised)
     {
         try
         {
             _logger.Log(this, LogLevel.Info, "Sending message");
             _outputDevice.Send(SanfordUtils.ConvertKeyStrokeEventArgsToChannelMessage(args));
         }
         catch (Exception ex)
         {
             //Catch ex...
         }
     }
 }
Exemplo n.º 9
0
        private void SendNoteEvents(Collection<SongNote> songNotes)
        {
            foreach (var songNote in songNotes)
            {
                var pianoKeyStrokeEventArgs = new PianoKeyStrokeEventArgs(songNote.PitchId, KeyStrokeType.KeyPress, songNote.Velocity);
                var pianoKeyStrokeReleaseEventArgs = new PianoKeyStrokeEventArgs(songNote.PitchId, KeyStrokeType.KeyRelease, songNote.Velocity);

                _output.Send(this, pianoKeyStrokeEventArgs);

                //Spark up a background worker to wait for the note to finish and release the key press
                var worker = new BackgroundWorker();
                worker.DoWork += (ob, arg) => { Thread.Sleep(CalculateNoteDuration(songNote)); };

                worker.RunWorkerCompleted += (ob, arg) =>
                {
                    _output.Send(null, pianoKeyStrokeReleaseEventArgs);
                };

                worker.RunWorkerAsync();
            }
        }
Exemplo n.º 10
0
        private void SendNoteEvents(Collection <SongNote> songNotes)
        {
            foreach (var songNote in songNotes)
            {
                var pianoKeyStrokeEventArgs        = new PianoKeyStrokeEventArgs(songNote.PitchId, KeyStrokeType.KeyPress, songNote.Velocity);
                var pianoKeyStrokeReleaseEventArgs = new PianoKeyStrokeEventArgs(songNote.PitchId, KeyStrokeType.KeyRelease, songNote.Velocity);

                _output.Send(this, pianoKeyStrokeEventArgs);

                //Spark up a background worker to wait for the note to finish and release the key press
                var worker = new BackgroundWorker();
                worker.DoWork += (ob, arg) => { Thread.Sleep(CalculateNoteDuration(songNote)); };

                worker.RunWorkerCompleted += (ob, arg) =>
                {
                    _output.Send(null, pianoKeyStrokeReleaseEventArgs);
                };

                worker.RunWorkerAsync();
            }
        }
Exemplo n.º 11
0
        public static ChannelMessage ConvertKeyStrokeEventArgsToChannelMessage(PianoKeyStrokeEventArgs keyStrokeEventArgs)
        {
            var channelCommand = new ChannelCommand();
            int data1 = keyStrokeEventArgs.midiKeyId;
            int data2 = keyStrokeEventArgs.KeyVelocity;

            //If the key stroke is neither a press or a release, return a null object
            switch (keyStrokeEventArgs.keyStrokeType)
            {
                case KeyStrokeType.KeyPress:
                    channelCommand = ChannelCommand.NoteOn;
                    break;
                case KeyStrokeType.KeyRelease:
                    channelCommand = ChannelCommand.NoteOn;
                    data2 = 0;
                    break;
                default: return null;
            }

            return new ChannelMessage(channelCommand, 1, data1, data2);
        }
Exemplo n.º 12
0
        public void HandleIncomingMessage(object sender, PianoKeyStrokeEventArgs e)
        {
            _logger.Log(this, LogLevel.Debug, "Handling keyboard input event");

            if (e == null)
            {
                return;
            }

            if (_keyDictionary.ContainsKey(e.midiKeyId))
            {
                switch (e.KeyStrokeType)
                {
                case KeyStrokeType.KeyPress:
                    _keyDictionary[e.midiKeyId].SetKeyPressedColour(e.KeyVelocity);
                    break;

                case KeyStrokeType.KeyRelease:
                    _keyDictionary[e.midiKeyId].SetDefaultKeyColour();
                    break;
                }
            }
        }
Exemplo n.º 13
0
        public static PianoKeyStrokeEventArgs ConvertChannelMessageToKeyStrokeEventArgs(ChannelMessage channelMessage)
        {
            var pksea = new PianoKeyStrokeEventArgs();

            //If the key stroke is neither a press or a release, return a null object
            switch (channelMessage.Command)
            {
                case ChannelCommand.NoteOn:
                    pksea.keyStrokeType = KeyStrokeType.KeyPress;
                    break;
                case ChannelCommand.NoteOff:
                    pksea.keyStrokeType = KeyStrokeType.KeyRelease;
                    break;
                default: return null;
            }

            pksea.midiKeyId = channelMessage.Data1;
            pksea.KeyVelocity = channelMessage.Data2;

            //Velocity 0 is effectively a key release
            if (pksea.KeyVelocity == 0) pksea.keyStrokeType = KeyStrokeType.KeyRelease;

            return pksea;
        }
Exemplo n.º 14
0
        private void HandleInputEvent(object sender, PianoKeyStrokeEventArgs e)
        {
            if (e == null) return;
            if (e.KeyStrokeType != KeyStrokeType.KeyPress) return;

            //Get the current note time from the playing song
            double noteTime = _songEventController.GetCurrentNoteTime();

            //Check if note played is in any of these 3 note times.
            bool inPreviousNotes;
            bool inNextNotes;
            double compareNoteTime = -1;

            inPreviousNotes = _song[_lastNoteTime].KeyPresses.Any(n => n.PitchId == e.midiKeyId);
            inNextNotes = _song[_nextNoteTime].KeyPresses.Any(n => n.PitchId == e.midiKeyId);

            if (inPreviousNotes) compareNoteTime = _lastNoteTime;
            if (inNextNotes) compareNoteTime = _nextNoteTime;

            int markForNote = 0;

            if (compareNoteTime == -1)
            {
                markForNote = 0;
            }
            else
            {
                double accuracy = Math.Abs(noteTime - compareNoteTime);
                if (accuracy < 1.0 / 4)
                {
                    markForNote = 10;
                }
                else if (accuracy < 1.0 / 2)
                {
                    markForNote = 6;
                }
                else if (accuracy < 1.1)
                {
                    markForNote = 4;
                }
                else
                {
                    markForNote = 1;
                }
            }

            _scoreParser.MarkNote(compareNoteTime, markForNote, e.midiKeyId);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Handles the Midi message received
        /// Only called if in recording mode
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HandleChannelMessageReceived(object sender, PianoKeyStrokeEventArgs e)
        {
            m_Session.Record(SanfordUtils.ConvertKeyStrokeEventArgsToChannelMessage(e));

            //_keyboard.HandleIncomingMessage(null, new PianoKeyStrokeEventArgs(_testKeyId, KeyStrokeType.KeyRelease, 100));

            this.NumMsgsReceived++;
        }
Exemplo n.º 16
0
        public void HandleKeyboardPress(object sender, KeyEventArgs e)
        {
            int keyIdOffset = -1;
            int baseKey     = 60;

            if (e.IsRepeat)
            {
                return;
            }

            if (e.IsDown || e.IsUp)
            {
                switch (e.Key)
                {
                case System.Windows.Input.Key.A:
                    keyIdOffset = 0;
                    break;

                case System.Windows.Input.Key.W:
                    keyIdOffset = 1;
                    break;

                case System.Windows.Input.Key.S:
                    keyIdOffset = 2;
                    break;

                case System.Windows.Input.Key.E:
                    keyIdOffset = 3;
                    break;

                case System.Windows.Input.Key.D:
                    keyIdOffset = 4;
                    break;

                case System.Windows.Input.Key.F:
                    keyIdOffset = 5;
                    break;

                case System.Windows.Input.Key.T:
                    keyIdOffset = 6;
                    break;

                case System.Windows.Input.Key.G:
                    keyIdOffset = 7;
                    break;

                case System.Windows.Input.Key.Y:
                    keyIdOffset = 8;
                    break;

                case System.Windows.Input.Key.H:
                    keyIdOffset = 9;
                    break;

                case System.Windows.Input.Key.U:
                    keyIdOffset = 10;
                    break;

                case System.Windows.Input.Key.J:
                    keyIdOffset = 11;
                    break;

                case System.Windows.Input.Key.K:
                    keyIdOffset = 12;
                    break;
                }
            }

            if (keyIdOffset != -1)
            {
                KeyStrokeType keyPress        = (e.IsDown ? KeyStrokeType.KeyPress: KeyStrokeType.KeyRelease);
                var           keyStrokeEvents = new PianoKeyStrokeEventArgs(baseKey + keyIdOffset, keyPress, 110);

                KeyPressEvent(this, keyStrokeEvents);
                HandleIncomingMessage(this, keyStrokeEvents);
            }
        }
Exemplo n.º 17
0
        public void HandleKeyboardPress(object sender, KeyEventArgs e)
        {
            int keyIdOffset = -1;
            int baseKey = 60;

            if (e.IsRepeat) return;

            if(e.IsDown || e.IsUp)
            {
                switch (e.Key)
                {
                    case System.Windows.Input.Key.A:
                        keyIdOffset = 0;
                        break;
                    case System.Windows.Input.Key.W:
                        keyIdOffset = 1;
                        break;
                    case System.Windows.Input.Key.S:
                        keyIdOffset = 2;
                        break;
                    case System.Windows.Input.Key.E:
                        keyIdOffset = 3;
                        break;
                    case System.Windows.Input.Key.D:
                        keyIdOffset = 4;
                        break;
                    case System.Windows.Input.Key.F:
                        keyIdOffset = 5;
                        break;
                    case System.Windows.Input.Key.T:
                        keyIdOffset = 6;
                        break;
                    case System.Windows.Input.Key.G:
                        keyIdOffset = 7;
                        break;
                    case System.Windows.Input.Key.Y:
                        keyIdOffset = 8;
                        break;
                    case System.Windows.Input.Key.H:
                        keyIdOffset = 9;
                        break;
                    case System.Windows.Input.Key.U:
                        keyIdOffset = 10;
                        break;
                    case System.Windows.Input.Key.J:
                        keyIdOffset = 11;
                        break;
                    case System.Windows.Input.Key.K:
                        keyIdOffset = 12;
                        break;
                }
            }

            if (keyIdOffset != -1)
            {
                KeyStrokeType keyPress = (e.IsDown ? KeyStrokeType.KeyPress: KeyStrokeType.KeyRelease);
                var keyStrokeEvents = new PianoKeyStrokeEventArgs(baseKey + keyIdOffset, keyPress, 110);

                KeyPressEvent(this, keyStrokeEvents);
                HandleIncomingMessage(this, keyStrokeEvents);
            }
        }
Exemplo n.º 18
0
        public void HandleIncomingMessage(object sender, PianoKeyStrokeEventArgs e)
        {
            _logger.Log(this, LogLevel.Debug, "Handling keyboard input event");

            if (e == null) return;

            if (_keyDictionary.ContainsKey(e.midiKeyId))
            {
                switch (e.KeyStrokeType)
                {
                    case KeyStrokeType.KeyPress:
                        _keyDictionary[e.midiKeyId].SetKeyPressedColour(e.KeyVelocity);
                        break;
                    case KeyStrokeType.KeyRelease:
                        _keyDictionary[e.midiKeyId].SetDefaultKeyColour();
                        break;
                }
            }
        }
Exemplo n.º 19
0
 public void Send(PianoKeyStrokeEventArgs args)
 {
     _outputDevice.Send(SanfordUtils.ConvertKeyStrokeEventArgsToChannelMessage(args));
 }
Exemplo n.º 20
0
 public SongEventArgs(PianoKeyStrokeEventArgs noteEventArgs, double noteTime, double nextNoteTime)
 {
     NoteKeyStrokeEvenArguments = noteEventArgs;
     NextNoteTime = nextNoteTime;
     NoteTime = noteTime;
 }
Exemplo n.º 21
0
 public SongEventArgs(PianoKeyStrokeEventArgs noteEventArgs, double noteTime, double nextNoteTime)
 {
     NoteKeyStrokeEvenArguments = noteEventArgs;
     NextNoteTime = nextNoteTime;
     NoteTime     = noteTime;
 }
Exemplo n.º 22
0
 /// <summary>
 /// Simply reraises the events it's subscribed to
 /// </summary>
 /// <param name="o"></param>
 /// <param name="e"></param>
 public void HandleInputEvent(object o, PianoKeyStrokeEventArgs e)
 {
     if(MessageReceived != null) MessageReceived(o, e);
 }