public override void Run() { if (OutputDevice.InstalledDevices.Count == 0) { Console.WriteLine("Can't do anything with no output device."); return; } float beatsPerMinute = 180; Clock clock = new Clock(beatsPerMinute); OutputDevice outputDevice = OutputDevice.InstalledDevices[0]; outputDevice.Open(); Drummer drummer = new Drummer(clock, outputDevice, 4); InputDevice inputDevice = null; if (InputDevice.InstalledDevices.Count > 0) { // Just pick the first input device. This will throw an exception if there isn't // one. inputDevice = InputDevice.InstalledDevices[0]; inputDevice.Open(); } Scaler scaler = new Scaler(clock, inputDevice, outputDevice); clock.Start(); if (inputDevice != null) { inputDevice.StartReceiving(clock); } bool done = false; while (!done) { Console.Clear(); Console.WriteLine("BPM = {0}, Playing = {1}, Scale = {2}", clock.BeatsPerMinute, clock.IsRunning, scaler.GetScaletoUse()); Console.WriteLine("Escape : Quit"); Console.WriteLine("Down : Slower"); Console.WriteLine("Up: Faster"); Console.WriteLine("Left: Previous Scale"); Console.WriteLine("Right: Next Scale"); Console.WriteLine("Space = Toggle Play"); ConsoleKey key = Console.ReadKey(true).Key; Note note; 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) { scaler.NextScale(); } else if (key == ConsoleKey.LeftArrow) { scaler.PreviousScale(); } else if (key == ConsoleKey.Spacebar) { if (clock.IsRunning) { clock.Stop(); if (inputDevice != null) { inputDevice.StopReceiving(); } outputDevice.SilenceAllNotes(); } else { clock.Start(); if (inputDevice != null) { inputDevice.StartReceiving(clock); } } } else if (key == ConsoleKey.D1) { NoteOnMessage msg = new NoteOnMessage(outputDevice, Channel.Channel1, Note.C4, 80, clock.BeatTime); NoteOffMessage msg2 = new NoteOffMessage(outputDevice, Channel.Channel1, Note.C4, 80, clock.BeatTime + 0.99f); clock.Schedule(msg); clock.Schedule(msg2); scaler.NoteOn(msg); } else if (ExampleUtil.IsMockNote(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); scaler.NoteOn(noteOn); } } if (clock.IsRunning) { clock.Stop(); if (inputDevice != null) { inputDevice.StopReceiving(); } outputDevice.SilenceAllNotes(); } outputDevice.Close(); if (inputDevice != null) { inputDevice.Close(); } }
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. }