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 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(); Console.WriteLine("Playing an arpeggiated C chord and then bending it down."); outputDevice.SendControlChange(Channel.Channel1, Control.SustainPedal, 0); outputDevice.SendPitchBend(Channel.Channel1, 8192); // Play C, E, G in half second intervals. outputDevice.SendNoteOn(Channel.Channel1, Pitch.C4, 80); Thread.Sleep(500); outputDevice.SendNoteOn(Channel.Channel1, Pitch.E4, 80); Thread.Sleep(500); outputDevice.SendNoteOn(Channel.Channel1, Pitch.G4, 80); Thread.Sleep(500); // Now apply the sustain pedal. outputDevice.SendControlChange(Channel.Channel1, Control.SustainPedal, 127); // Now release the C chord notes, but they should keep ringing because of the sustain // pedal. outputDevice.SendNoteOff(Channel.Channel1, Pitch.C4, 80); outputDevice.SendNoteOff(Channel.Channel1, Pitch.E4, 80); outputDevice.SendNoteOff(Channel.Channel1, Pitch.G4, 80); // Now bend the pitches down. for (int i = 0; i < 17; ++i) { outputDevice.SendPitchBend(Channel.Channel1, 8192 - i * 450); Thread.Sleep(200); } // Now release the sustain pedal, which should silence the notes, then center // the pitch bend again. outputDevice.SendControlChange(Channel.Channel1, Control.SustainPedal, 0); outputDevice.SendPitchBend(Channel.Channel1, 8192); Console.WriteLine("Playing the first two bars of Mary Had a Little Lamb..."); Clock clock = new Clock(120); clock.Schedule(new NoteOnMessage(outputDevice, Channel.Channel1, Pitch.E4, 80, 0)); clock.Schedule(new NoteOffMessage(outputDevice, Channel.Channel1, Pitch.E4, 80, 1)); clock.Schedule(new NoteOnMessage(outputDevice, Channel.Channel1, Pitch.D4, 80, 1)); clock.Schedule(new NoteOffMessage(outputDevice, Channel.Channel1, Pitch.D4, 80, 2)); clock.Schedule(new NoteOnMessage(outputDevice, Channel.Channel1, Pitch.C4, 80, 2)); clock.Schedule(new NoteOffMessage(outputDevice, Channel.Channel1, Pitch.C4, 80, 3)); clock.Schedule(new NoteOnMessage(outputDevice, Channel.Channel1, Pitch.D4, 80, 3)); clock.Schedule(new NoteOffMessage(outputDevice, Channel.Channel1, Pitch.D4, 80, 4)); clock.Schedule(new NoteOnMessage(outputDevice, Channel.Channel1, Pitch.E4, 80, 4)); clock.Schedule(new NoteOffMessage(outputDevice, Channel.Channel1, Pitch.E4, 80, 5)); clock.Schedule(new NoteOnMessage(outputDevice, Channel.Channel1, Pitch.E4, 80, 5)); clock.Schedule(new NoteOffMessage(outputDevice, Channel.Channel1, Pitch.E4, 80, 6)); clock.Schedule(new NoteOnMessage(outputDevice, Channel.Channel1, Pitch.E4, 80, 6)); clock.Schedule(new NoteOffMessage(outputDevice, Channel.Channel1, Pitch.E4, 80, 7)); clock.Start(); Thread.Sleep(5000); clock.Stop(); Console.WriteLine("Playing sustained chord runs up the keyboard..."); outputDevice.SendControlChange(Channel.Channel1, Control.SustainPedal, 127); PlayChordRun(outputDevice, new Chord("C"), 100); outputDevice.SendControlChange(Channel.Channel1, Control.SustainPedal, 0); outputDevice.SendControlChange(Channel.Channel1, Control.SustainPedal, 127); PlayChordRun(outputDevice, new Chord("F"), 100); outputDevice.SendControlChange(Channel.Channel1, Control.SustainPedal, 0); outputDevice.SendControlChange(Channel.Channel1, Control.SustainPedal, 127); PlayChordRun(outputDevice, new Chord("G"), 100); outputDevice.SendControlChange(Channel.Channel1, Control.SustainPedal, 0); outputDevice.SendControlChange(Channel.Channel1, Control.SustainPedal, 127); PlayChordRun(outputDevice, new Chord("C"), 100); Thread.Sleep(2000); outputDevice.SendControlChange(Channel.Channel1, Control.SustainPedal, 0); // Close the output device. outputDevice.Close(); // All done. Console.WriteLine(); ExampleUtil.PressAnyKeyToContinue(); }
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. }
public override void Run() { // Create a clock running at the specified beats per minute. var beatsPerMinute = 180; var clock = new Clock(beatsPerMinute); // Prompt user to choose an output device (or if there is only one, use that one. var outputDevice = ExampleUtil.ChooseOutputDeviceFromConsole(); if (outputDevice == null) { Console.WriteLine("No output devices, so can't run this example."); ExampleUtil.PressAnyKeyToContinue(); return; } outputDevice.Open(); // Prompt user to choose an input device (or if there is only one, use that one). var inputDevice = ExampleUtil.ChooseInputDeviceFromConsole(); inputDevice?.Open(); var arpeggiator = new Arpeggiator(inputDevice, outputDevice, clock); var drummer = new Drummer(clock, outputDevice, 4); clock.Start(); inputDevice?.StartReceiving(clock); var done = false; while (!done) { Console.Clear(); Console.WriteLine("BPM = {0}, Playing = {1}, Arpeggiator Mode = {2}", clock.BeatsPerMinute, clock.IsRunning, arpeggiator.Status); Console.WriteLine("Escape : Quit"); Console.WriteLine("Down : Slower"); Console.WriteLine("Up: Faster"); Console.WriteLine("Left: Previous Chord or Scale"); Console.WriteLine("Right: Next Chord or Scale"); Console.WriteLine("Space = Toggle Play"); Console.WriteLine("Enter = Toggle Scales/Chords"); var key = Console.ReadKey(true).Key; Pitch pitch; if (key == ConsoleKey.Escape) { done = true; } else if (key == ConsoleKey.DownArrow) { clock.BeatsPerMinute -= 2; } else if (key == ConsoleKey.UpArrow) { clock.BeatsPerMinute += 2; } else if (key == ConsoleKey.RightArrow) { arpeggiator.Change(1); } else if (key == ConsoleKey.LeftArrow) { arpeggiator.Change(-1); } else if (key == ConsoleKey.Spacebar) { if (clock.IsRunning) { clock.Stop(); inputDevice?.StopReceiving(); outputDevice.SilenceAllNotes(); } else { clock.Start(); inputDevice?.StartReceiving(clock); } } else if (key == ConsoleKey.Enter) { arpeggiator.ToggleMode(); } else if (ExampleUtil.IsMockPitch(key, out pitch)) { // We've hit a QUERTY key which is meant to simulate a MIDI note, so // send the Note On to the output device and tell the arpeggiator. var noteOn = new NoteOnMessage(outputDevice, 0, pitch, 100, clock.Time); clock.Schedule(noteOn); arpeggiator.NoteOn(noteOn); // We don't get key release events for the console, so schedule a // simulated Note Off one beat from now. var noteOff = new NoteOffMessage(outputDevice, 0, pitch, 100, clock.Time + 1); CallbackMessage.CallbackType noteOffCallback = beatTime => { arpeggiator.NoteOff(noteOff); }; clock.Schedule(new CallbackMessage(beatTime => arpeggiator.NoteOff(noteOff), noteOff.Time)); } } if (clock.IsRunning) { clock.Stop(); inputDevice?.StopReceiving(); outputDevice.SilenceAllNotes(); } outputDevice.Close(); if (inputDevice != null) { inputDevice.Close(); inputDevice.RemoveAllEventHandlers(); } // All done. }
public override void Run() { // Create a clock running at the specified beats per minute. int beatsPerMinute = 180; Clock clock = new Clock(beatsPerMinute); // 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(); // Utility function prompts user to choose an input device (or if there is only one, // returns that one). InputDevice inputDevice = ExampleUtil.ChooseInputDeviceFromConsole(); if (inputDevice != null) { inputDevice.Open(); } Arpeggiator arpeggiator = new Arpeggiator(inputDevice, outputDevice, clock); Console.WriteLine("Press Escape when finished."); clock.Start(); if (inputDevice != null) { inputDevice.StartReceiving(clock); } while (true) { ConsoleKeyInfo keyInfo = Console.ReadKey(true); if (keyInfo.Key == ConsoleKey.Escape) { break; } Note note; if (ExampleUtil.IsMockNote(keyInfo.Key, out note)) { NoteOnMessage noteOn = new NoteOnMessage(outputDevice, 0, note, 100, clock.BeatTime); NoteOffMessage noteOff = new NoteOffMessage(outputDevice, 0, note, 100, clock.BeatTime + 1); clock.Schedule(noteOn); clock.Schedule(noteOff); arpeggiator.NoteOn(noteOn); arpeggiator.NoteOff(noteOff); } } clock.Stop(); // Close the devices. outputDevice.Close(); if (inputDevice != null) { inputDevice.StopReceiving(); inputDevice.Close(); } // All done. }