static void Main(string[] args) { int[] minorscale = new int[] { 0, 2, 3, 5, 7, 8, 10 }; List <int> minorNotes = new List <int>(); minorNotes.AddRange(minorscale.Select(x => 45 - 12 + x)); minorNotes.AddRange(minorscale.Select(x => 45 + x)); minorNotes.AddRange(minorscale.Select(x => 45 + 12 + x)); minorNotes.AddRange(minorscale.Select(x => 45 + 24 + x)); try { int midiOutDevice = 0; // GS Wavetable Synth PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); PerformanceCounter visualStudio = new PerformanceCounter("Process", "% User Time", "devenv"); PerformanceCounter diskPercentCounter = new PerformanceCounter("LogicalDisk", "% Disk Time", "_Total"); using (var midiOut = new MidiOut(midiOutDevice)) { NoteOnEvent lastNote = null; PerfCounterScaling pcfCpu = new PerfCounterScaling() { MaxValue = 100.0f }; Averager cpuAvg = new Averager(10, 0.1f); PerfCounterScaling pcuDisk = new PerfCounterScaling() { MaxValue = 100.0f }; var mtPiano = new MidiSingleNoteTracker(midiOut, 1, 0); var mtStrings = new MidiSingleNoteTracker(midiOut, 2, 49); var mtGuitar = new MidiSingleNoteTracker(midiOut, 3, 26); while (true) { var cpu = GetNextScaledValueOrNull(cpuCounter, pcfCpu, cpuAvg); var visualStudioCpu = GetNextScaledValueOrNull(visualStudio, pcfCpu); if ((cpu.HasValue && (cpu > 0.5 || cpuAvg.AverageValue > 0.3)) || (visualStudioCpu.HasValue && visualStudioCpu > 0.5)) { if (cpu.HasValue) { MapToNote(cpu.Value, cpuAvg.AverageValue, minorNotes, mtPiano, false); } System.Threading.Thread.Sleep(250); if (visualStudioCpu.HasValue) { MapToNote(visualStudioCpu.Value, visualStudioCpu.Value, minorNotes, mtGuitar, true); } System.Threading.Thread.Sleep(250); } else { //mtGuitar.StopPlaying(); //mtPiano.StopPlaying(); they ring out System.Threading.Thread.Sleep(500); } var disk = GetNextScaledValueOrNull(diskPercentCounter, pcuDisk, null); if (disk.HasValue && disk.Value > 0.05) { MapToNote(disk.Value, 0.25f, minorNotes, mtStrings, false); } else { mtStrings.StopPlaying(); } Console.WriteLine($"cpu:{cpu:P}/{cpuAvg.AverageValue:P} vs:{visualStudioCpu:P} disk:{disk:P}"); System.Threading.Thread.Sleep(500); } } } catch (Exception ex) { Console.Error.WriteLine(ex); } finally { Console.WriteLine("Done"); } }
private static void MapToNote(float cookedNote, float cookedVelocity, List <int> minorNotes, MidiSingleNoteTracker mt, bool shouldAlwaysPlayNewNote = true) { // put range limiting in here var index = (int)(cookedNote * (minorNotes.Count - 1)); var velocity = (int)(cookedVelocity * 90.0) + 30; var note = minorNotes[index]; if (shouldAlwaysPlayNewNote) { mt.PlayNewNote(note, velocity); } else { if (mt.PlayingNote == null || mt.PlayingNote.NoteNumber != note) { mt.PlayNewNote(note, velocity); } } }