using Midi; // ....... private void LoadMidiDevices() { foreach (InputDevice d in InputDevice.InstalledDevices) { DeviceList.Items.Add(d.Name); } DeviceList.SelectedIndex = 0; UpdateDevice(); }
private void UpdateDevice() { if (d != null) { if (d.IsReceiving) d.StopReceiving(); if (d.IsOpen) d.Close(); } d = InputDevice.InstalledDevices[DeviceList.SelectedIndex]; if (!d.IsOpen) d.Open(); if (d.IsReceiving) d.StopReceiving(); d.StartReceiving(null); if (d != null) { d.NoteOn += new InputDevice.NoteOnHandler(this.NoteOn); d.NoteOff += new InputDevice.NoteOffHandler(this.NoteOff); d.ControlChange += new InputDevice.ControlChangeHandler(this.ControlChange); } }
private void SettingsWindow_FormClosing(object sender, FormClosingEventArgs e) { d.StopReceiving(); d.Close(); }
private List<Pitch> notes; private Dictionary<Pitch, float> events; //.... public void NoteOn(NoteOnMessage msg) { lock (this) { events[msg.Pitch] = msg.Time; } } public void NoteOff(NoteOffMessage msg) { lock (this) { if (events.ContainsKey(msg.Pitch)) { if (msg.Time - events[msg.Pitch] > 0.05) { notes.Add(msg.Pitch); } events.Remove(msg.Pitch); if ((events.Count == 0) && (notes.Count > 0)) { SendKeys.SendWait(" " + cons.Convert(notes)); notes.Clear(); } } } }
bool sustain; // ....... public void ControlChange(ControlChangeMessage msg) { if (msg.Control == Midi.Control.SustainPedal) { if ((msg.Value > 64) && !sustain) { sustain = true; SendKeys.SendWait("{(}"); } if ((msg.Value < 64) && sustain) { sustain = false; SendKeys.SendWait("{)}"); } } return; }
private static int[] majorScale = { 0, 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6 }; private static int[] majorAcc = { 0, -1, 0, -1, 0, 0, 1, 0, -1, 0, -1, 0 }; private static int[] minorScale = { 0, 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6 }; private static int[] minorAcc = { 0, -1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1 }; private Degree PitchToGrade(Pitch p) { // int keybase = (keys * 7) % 12 - (isMajor?0:3); // int offset = ((int)p - keybase) % 12; // 7- int octave = (((int)p - keybase) / 12) * 7; int num, acc; if (offset < 0) offset += 12; if (isMajor) { num = majorScale[offset] + octave; acc = majorAcc[offset]; } else { num = minorScale[offset] + octave; acc = minorAcc[offset]; } return new Degree(num, acc); }
private static String[] Naturals = { "c", "d", "e", "f", "g", "a", "h" }; private static String[] Sharps = { "cis", "dis", "eis", "fis", "gis", "ais", "his" }; private static String[] Flats = { "ces", "des", "es", "fes", "ges", "as", "b" }; private static String[] DoubleSharps = { "cisis", "disis", "eisis", "fisis", "gisis", "aisis", "bisis" }; private static String[] DoubleFlats = { "ceses", "deses", "eses", "feses", "geses", "ases", "beses" }; public String resolveIn(int keys, bool isMajor) { int fAcc = Acc; int fs; int fNum; int numMod = Number % 7; fs = isMajor ? 6 : 3; fs = (fs - 2*numMod) % 7; if (fs <= 0) fs += 7; if (keys < 0) fs = 8 - fs; if (fs <= Math.Abs(keys)) fAcc += keys / Math.Abs(keys); fNum = (numMod + keys*4 - (isMajor ? 0 : 2)) % 7; if (fNum < 0) fNum += 7; switch (fAcc) { case -2: return DoubleFlats[fNum]; case -1: return Flats[fNum]; case 0: return Naturals[fNum]; case 1: return Sharps[fNum]; case 2: return DoubleSharps[fNum]; default: return ""; } }
// class Degree public static String operator -(Degree a, Degree g) { int o; o = a.Number - g.Number; o = (int)Math.Round((double)o / 7.0); if (o > 0) return new String('\'', o); if (o < 0) return new String(',', -o); return ""; } // class PitchConsumer public String Convert(List<Pitch> pitches) { Pitch localLast = lastPitch; String accum; if ((int)lastPitch == 0) lastPitch = pitches[0]; pitches.Sort(); if (pitches.Count == 1) { accum = PitchToString(pitches[0], lastPitch); lastPitch = pitches[0]; } else { lastPitch = pitches[0]; accum = "<"; foreach (Pitch p in pitches) { if (accum.Length > 1) accum += " "; accum += PitchToString(p, localLast); localLast = p; } accum += ">"; } return accum; } private String PitchToString(Pitch p, Pitch last) { Degree g, glast; String note; g = PitchToGrade(p); glast = PitchToGrade(last); note = g.resolveIn(keys, isMajor); return note + (g - glast); }
Source: https://habr.com/ru/post/149075/
All Articles