using System; using Midi; using System.Threading; using System.Collections.Generic; namespace MidiExamples { public class Example05 : ExampleBase { public Example05() : base("Example05.cs", "Prints notes and chords as they are played.") { } public class Summarizer { public Summarizer(InputDevice inputDevice) { this.inputDevice = inputDevice; pitchesPressed = new Dictionary<Pitch, bool>(); inputDevice.NoteOn += new InputDevice.NoteOnHandler(this.NoteOn); inputDevice.NoteOff += new InputDevice.NoteOffHandler(this.NoteOff); PrintStatus(); } private void PrintStatus() { Console.Clear(); Console.WriteLine("Play notes and chords on the MIDI input device, and watch"); Console.WriteLine("their names printed here. Press any QUERTY key to quit."); Console.WriteLine(); // Print the currently pressed notes. List<Pitch> pitches = new List<Pitch>(pitchesPressed.Keys); pitches.Sort(); Console.Write("Notes: "); for (int i = 0; i < pitches.Count; ++i) { Pitch pitch = pitches[i]; if (i > 0) { Console.Write(", "); } Console.Write("{0}", pitch.NotePreferringSharps()); if (pitch.NotePreferringSharps() != pitch.NotePreferringFlats()) { Console.Write(" or {0}", pitch.NotePreferringFlats()); } } Console.WriteLine(); // Print the currently held down chord. List<Chord> chords = Chord.FindMatchingChords(pitches); Console.Write("Chords: "); for (int i = 0; i < chords.Count; ++i) { Chord chord = chords[i]; if (i > 0) { Console.Write(", "); } Console.Write("{0}", chord); } Console.WriteLine(); } public void NoteOn(NoteOnMessage msg) { lock (this) { pitchesPressed[msg.Pitch] = true; PrintStatus(); } } public void NoteOff(NoteOffMessage msg) { lock (this) { pitchesPressed.Remove(msg.Pitch); PrintStatus(); } } private InputDevice inputDevice; private Dictionary<Pitch, bool> pitchesPressed; } 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(); } } }
File.AppendAllText(strPath, strFileContent);
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)