コード例 #1
0
ファイル: Example05.cs プロジェクト: Robertorob/GuitarMaster
        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.
        }
コード例 #2
0
        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.
        }
コード例 #3
0
ファイル: Example06.cs プロジェクト: Meddington/xyzmidi
        public override void Run()
        {
            // Create a clock running at the specified beats per minute.
            int beatsPerMinute = 180;
            Clock clock = new Clock(beatsPerMinute);

            // 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();

            // Prompt user to choose an input device (or if there is only one, use that one).
            InputDevice inputDevice = ExampleUtil.ChooseInputDeviceFromConsole();
            if (inputDevice != null)
            {
                inputDevice.Open();
            }

            Arpeggiator arpeggiator = new Arpeggiator(inputDevice, outputDevice, clock);
            Drummer drummer = new Drummer(clock, outputDevice, 4);

            clock.Start();
            if (inputDevice != null)
            {
                inputDevice.StartReceiving(clock);
            }

            bool 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");
                ConsoleKey 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();
                        if (inputDevice != null)
                        {
                            inputDevice.StopReceiving();
                        }
                        outputDevice.SilenceAllNotes();
                    }
                    else
                    {
                        clock.Start();
                        if (inputDevice != null)
                        {
                            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.
                    NoteOnMessage 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.
                    NoteOffMessage 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();
                if (inputDevice != null)
                {
                    inputDevice.StopReceiving();
                }
                outputDevice.SilenceAllNotes();
            }

            outputDevice.Close();
            if (inputDevice != null)
            {
                inputDevice.Close();
                inputDevice.RemoveAllEventHandlers();
            }

            // All done.
        }
コード例 #4
0
ファイル: Example05.cs プロジェクト: Robertorob/GuitarMaster
        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.
        }