/// <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); } }
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); }
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); }
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)); }
/// <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++; }
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; } } }
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; } } }
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... } } }
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(); } }
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(); } }
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); }
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; } } }
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; }
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); }
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); } }
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); } }
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; } } }
public void Send(PianoKeyStrokeEventArgs args) { _outputDevice.Send(SanfordUtils.ConvertKeyStrokeEventArgsToChannelMessage(args)); }
public SongEventArgs(PianoKeyStrokeEventArgs noteEventArgs, double noteTime, double nextNoteTime) { NoteKeyStrokeEvenArguments = noteEventArgs; NextNoteTime = nextNoteTime; NoteTime = noteTime; }
/// <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); }