public void KeyPressEvent(MidiKeyboard keyboard, int key, ChannelCommand command) { if (command != ChannelCommand.NoteOn) { return; } lock (this) { if (ready) { return; } var on = keyboard.OnKeys; if (on.Count() == 1) { timeFirstNotePlayed = DateTime.Now; } if (on.Count() != chord.Count()) { return; } if ((DateTime.Now - timeFirstNotePlayed).TotalMilliseconds >= 500) { return; } for (int i = 0; i < chord.Count(); i++) { if ((absolute && (on.ElementAt(i).AbsolutePitch != chord.ElementAt(i))) || (!absolute && (on.ElementAt(i).RelativePitch != chord.ElementAt(i)))) { Played = false; ready = true; return; } } Played = true; ready = true; return; } }
public void KeyPressEvent(MidiKeyboard keyboard, int absolutePitch, ChannelCommand command) { if (command != ChannelCommand.NoteOn) { return; } lock (this) { if (ready) { return; } this.Played = this.keyToPlay.AbsolutePitch == absolutePitch; this.ready = true; } }
public void KeyPressEvent(MidiKeyboard keyboard, int absolutePitch, ChannelCommand command) { if (command != ChannelCommand.NoteOn) { return; } lock (this) { if (ready) { return; } if ((DateTime.Now - lastKeyPress).TotalMilliseconds <= 500) { // Don't accept key press if it happens nearly simultaneously return; } int relativePitch = absolutePitch % 12; if ((absolute && (absolutePitch == melody.ElementAt(idx))) || (!absolute && (relativePitch == melody.ElementAt(idx)))) { // If we played the correct note in the sequence then move to next note lastKeyPress = DateTime.Now; idx++; if (idx == melody.Count) { // If we've played all the notes in the sequence then end Played = true; ready = true; return; } } else if (breakOnError) { // we played a wrong note and breakOnError == true ready = true; } } }
static void Main(string[] args) { var circle = new CircleOf5ths(); //var cMajor = new Scale(Note.c); //var gMinor = new Scale(Note.g, mode: Mode.Minor); //var notes = ""; //foreach(var ns in cMajor.SelectNotes(group)) //{ // var a = Accidental.n; // var n = cMajor.GetNoteName(ns.interval, out a); // notes += a != Accidental.n ? n.ToString() + a.ToString() : n.ToString(); // notes += " "; //} //Console.WriteLine(notes); //Console.WriteLine(); //notes = ""; //foreach (var ns in gMinor.SelectNotes(cMajor.SelectNotes(group))) //{ // var a = Accidental.n; // var n = gMinor.GetNoteName(ns.interval, out a); // notes += a != Accidental.n ? n.ToString() + a.ToString() : n.ToString(); // notes += " "; //} //Console.WriteLine(notes); //Console.WriteLine(); //Console.ReadKey(); //var k = circle[0]; //k.Mode = Mode.Minor; //var aChord = k.SelectNotes(new List<KeyNote>() //{ // new KeyNote(){ interval = 1 }, // new KeyNote(){ interval = 3 }, // new KeyNote(){ interval = 5 }, //}); //Console.WriteLine(GetNoteNames(k, aChord.Select(o => o.interval).ToList())); //foreach (var key in circle) //{ // key.Mode = Mode.Ionian; // Console.WriteLine(GetNoteNames(key, key.Select(o => o.Interval).ToList())); // key.Mode = Mode.Dorian; // Console.WriteLine(GetNoteNames(key, key.Select(o => o.Interval).ToList())); // key.Mode = Mode.Phrygian; // Console.WriteLine(GetNoteNames(key, key.Select(o => o.Interval).ToList())); // key.Mode = Mode.Lydian; // Console.WriteLine(GetNoteNames(key, key.Select(o => o.Interval).ToList())); // key.Mode = Mode.Mixolydian; // Console.WriteLine(GetNoteNames(key, key.Select(o => o.Interval).ToList())); // key.Mode = Mode.Aeolian; // Console.WriteLine(GetNoteNames(key, key.Select(o => o.Interval).ToList())); // key.Mode = Mode.Locrian; // Console.WriteLine(GetNoteNames(key, key.Select(o => o.Interval).ToList())); // Console.WriteLine(); //} //Console.ReadKey(); //var cMajorTrebble = new List<int>() //{ // MidiKeyboard.Key.ToPitch((int)Note.c, 4), // MidiKeyboard.Key.ToPitch((int)Note.e, 4), // MidiKeyboard.Key.ToPitch((int)Note.g, 4), //}; //var cMajorClef = new List<int>() //{ // MidiKeyboard.Key.ToPitch((int)Note.c, 3), // MidiKeyboard.Key.ToPitch((int)Note.e, 3), // MidiKeyboard.Key.ToPitch((int)Note.g, 3), //}; //var xml = OutputMei.Song("", 4, 4, "0", "major", // OutputMei.Measure(1, // OutputMei.Chord(cMajorTrebble, OutputMei.NoteValue.Quarter), // OutputMei.Chord(cMajorClef, OutputMei.NoteValue.Quarter))); //File.WriteAllText(@"C:\Users\tyni\Desktop\test.xml", xml); List <Note> triad = new List <Note>() { new Note() { Interval = 1 }, new Note() { Interval = 3 }, new Note() { Interval = 5 }, }; Deck deck = new Deck(); foreach (var scale in circle) { scale.Mode = Mode.Major; deck.Add(new Card( new KeyValuePair <string, List <int> > ( GetNoteName(scale, 1).ToUpper(), scale.Transpose(triad) .Select(n => (int)n.RelativePitch) .ToList() ))); scale.Mode = Mode.Minor; deck.Add(new Card( new KeyValuePair <string, List <int> > ( GetNoteName(scale, 1).ToUpper() + $"m", scale.Transpose(triad) .Select(n => (int)n.RelativePitch) .ToList() ))); } if (InputDevice.DeviceCount < 1) { Console.WriteLine("No MIDI device found. Connect device and restart."); return; } for (int i = 0; i < InputDevice.DeviceCount; i++) { Console.WriteLine($"{i + 1}: {InputDevice.GetDeviceCapabilities(i).name}"); } var key = Console.ReadKey(); int deviceId = 0; // We check input for a Digit if (char.IsDigit(key.KeyChar)) { deviceId = int.Parse(key.KeyChar.ToString()) - 1; // use Parse if it's a Digit } Console.WriteLine("Using Device: " + InputDevice.GetDeviceCapabilities(deviceId).name); using (InputDevice inDevice = new InputDevice(deviceId)) { keyboard = new MidiKeyboard.MidiKeyboard(inDevice); keyboard.StartRecording(inDevice, KeyActions); while (true) { Card card = deck.Pick(); var kvp = (KeyValuePair <string, List <int> >)card.Get(); Console.WriteLine($"Play {kvp.Key}"); var playAction = new PlayChord(kvp.Value); Stopwatch watch = new Stopwatch(); watch.Start(); do { playAction.WaitForKeyInput(KeyActions); Console.WriteLine(playAction.Played ? "You did it!" : "Nope..."); }while (!playAction.Played); watch.Stop(); } } }