Ejemplo n.º 1
0
        public override void Run()
        {
            // Print a table of the input device names, or "No input devices" if there are none.
            if (InputDevice.InstalledDevices.Count == 0)
            {
                Console.WriteLine("No input devices.");
            }
            else
            {
                Console.WriteLine("Input Devices:");
                foreach (InputDevice device in InputDevice.InstalledDevices)
                {
                    Console.WriteLine("  {0}", device.Spec);
                }
            }
            Console.WriteLine();

            // Print a table of the output device names, or "No output devices" if there are none.
            if (OutputDevice.InstalledDevices.Count == 0)
            {
                Console.WriteLine("No output devices.");
            }
            else
            {
                Console.WriteLine("Output Devices:");
                foreach (OutputDevice device in OutputDevice.InstalledDevices)
                {
                    Console.WriteLine("  {0}", device.Spec);
                }
            }

            // All done.
            Console.WriteLine();
            ExampleUtil.PressAnyKeyToContinue();
        }
Ejemplo n.º 2
0
        public override void Run()
        {
            // Prompt user to choose an input device (or if there is only one, use that one).
            InputDevice inputDevice = ExampleUtil.ChooseInputDeviceFromConsole();

            if (inputDevice == null)
            {
                Console.WriteLine("No input devices, so can't run this example.");
                ExampleUtil.PressAnyKeyToContinue();
                return;
            }
            inputDevice.Open();
            inputDevice.StartReceiving(null);

            Summarizer summarizer = new Summarizer(inputDevice);

            ExampleUtil.PressAnyKeyToContinue();
            inputDevice.StopReceiving();
            inputDevice.Close();
            inputDevice.RemoveAllEventHandlers();
        }
Ejemplo n.º 3
0
        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.
        }
Ejemplo n.º 4
0
        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();
        }
Ejemplo n.º 5
0
        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.
        }