public override void Run() { // Utility function prompts user to choose an output device (or if there is only one, // returns that one). OutputDevice outputDevice = ExampleUtil.ChooseOutputDeviceFromConsole(); if (outputDevice == null) { Console.WriteLine("No output devices, so can't run this example."); ExampleUtil.PressAnyKeyToContinue(); return; } outputDevice.Open(); Console.WriteLine("Press alphabetic keys (with and without SHIFT) to play MIDI " + "percussion sounds."); Console.WriteLine("Press Escape when finished."); Console.WriteLine(); while (true) { ConsoleKeyInfo keyInfo = Console.ReadKey(true); if (keyInfo.Key == ConsoleKey.Escape) { break; } else if ((keyInfo.Modifiers & ConsoleModifiers.Shift) != 0) { if (shiftedNotes.ContainsKey(keyInfo.Key)) { Percussion note = shiftedNotes[keyInfo.Key]; Console.Write("\rNote {0} ({1}) ", (int)note, note.Name()); outputDevice.SendPercussion(note, 90); } } else { if (unshiftedNotes.ContainsKey(keyInfo.Key)) { Percussion note = unshiftedNotes[keyInfo.Key]; Console.Write("\rNote {0} ({1}) ", (int)note, note.Name()); outputDevice.SendPercussion(note, 90); } } } // Close the output device. outputDevice.Close(); // All done. }
public override void Run() { // Prompt the user to choose an output device (or if there is only one, use that one). OutputDevice outputDevice = ExampleUtil.ChooseOutputDeviceFromConsole(); if (outputDevice == null) { Console.WriteLine("No output devices, so can't run this example."); ExampleUtil.PressAnyKeyToContinue(); return; } outputDevice.Open(); // Generate two maps from console keys to percussion sounds: one for when alphabetic // keys are pressed and one for when they're pressed with shift. Dictionary <ConsoleKey, Percussion> unshiftedKeys = new Dictionary <ConsoleKey, Percussion>(); Dictionary <ConsoleKey, Percussion> shiftedKeys = new Dictionary <ConsoleKey, Percussion>(); for (int i = 0; i < 26; ++i) { unshiftedKeys[QwertyOrder[i]] = Percussion.BassDrum1 + i; if (i < 20) { shiftedKeys[QwertyOrder[i]] = Percussion.BassDrum1 + 26 + i; } } Console.WriteLine("Press alphabetic keys (with and without SHIFT) to play MIDI " + "percussion sounds."); Console.WriteLine("Press Escape when finished."); Console.WriteLine(); while (true) { ConsoleKeyInfo keyInfo = Console.ReadKey(true); if (keyInfo.Key == ConsoleKey.Escape) { break; } else if ((keyInfo.Modifiers & ConsoleModifiers.Shift) != 0) { if (shiftedKeys.ContainsKey(keyInfo.Key)) { Percussion note = shiftedKeys[keyInfo.Key]; Console.Write("\rNote {0} ({1}) ", (int)note, note.Name()); outputDevice.SendPercussion(note, 90); } } else { if (unshiftedKeys.ContainsKey(keyInfo.Key)) { Percussion note = unshiftedKeys[keyInfo.Key]; Console.Write("\rNote {0} ({1}) ", (int)note, note.Name()); outputDevice.SendPercussion(note, 90); } } } // Close the output device. outputDevice.Close(); // All done. }