public void Send(byte [] mevent, int offset, int length, long timestamp) { foreach (var evt in MidiEvent.Convert(mevent, offset, length)) { if (evt.StatusByte < 0xF0) { WinMMNatives.midiOutShortMsg(handle, (uint)(evt.StatusByte + (evt.Msb << 8) + (evt.Lsb << 16))); } else { MidiHdr sysex = default(MidiHdr); unsafe { fixed(void *ptr = evt.Data) { sysex.Data = (IntPtr)ptr; sysex.BufferLength = evt.Data.Length; sysex.Flags = 0; WinMMNatives.midiOutPrepareHeader(handle, ref sysex, (uint)Marshal.SizeOf(typeof(MidiHdr))); WinMMNatives.midiOutLongMsg(handle, ref sysex, evt.Data.Length); } } } } }
public void Convert() { int [] bytes = { 0xF0, 0x0A, 0x41, 0x10, 0x42, 0x12, 0x40, 0, 0x7F, 0, 0x41, 0xF7 }; // am too lazy to add cast to byte... var msgs = MidiEvent.Convert(bytes.Select(i => (byte)i).ToArray(), 0, bytes.Length); Assert.AreEqual(1, msgs.Count(), "message length"); Assert.AreEqual(bytes.Length, msgs.First().ExtraDataLength); }
public void Send(byte [] mevent, int offset, int length, long timestamp) { foreach (var evt in MidiEvent.Convert(mevent, offset, length)) { if (evt.StatusByte < 0xF0) { DieOnError(WinMMNatives.midiOutShortMsg(handle, (uint)(evt.StatusByte + (evt.Msb << 8) + (evt.Lsb << 16)))); } else { var header = new MidiHdr(); bool prepared = false; IntPtr ptr = IntPtr.Zero; var hdrSize = Marshal.SizeOf(typeof(MidiHdr)); try { // allocate unmanaged memory and hand ownership over to the device driver header.Data = Marshal.AllocHGlobal(evt.Data.Length); header.BufferLength = evt.Data.Length; Marshal.Copy(evt.Data, 0, header.Data, header.BufferLength); ptr = Marshal.AllocHGlobal(hdrSize); Marshal.StructureToPtr(header, ptr, false); DieOnError(WinMMNatives.midiOutPrepareHeader(handle, ptr, hdrSize)); prepared = true; DieOnError(WinMMNatives.midiOutLongMsg(handle, ptr, hdrSize)); } finally { // reclaim ownership and free if (prepared) { DieOnError(WinMMNatives.midiOutUnprepareHeader(handle, ptr, hdrSize)); } if (header.Data != IntPtr.Zero) { Marshal.FreeHGlobal(header.Data); } if (ptr != IntPtr.Zero) { Marshal.FreeHGlobal(ptr); } } } } }
public void MidiEventConvert() { var bytes1 = new byte [] { 0xF8 }; var events1 = MidiEvent.Convert(bytes1, 0, bytes1.Length); Assert.AreEqual(1, events1.Count(), "bytes1 count"); var bytes2 = new byte [] { 0xFE }; var events2 = MidiEvent.Convert(bytes2, 0, bytes2.Length); Assert.AreEqual(1, events2.Count(), "bytes2 count"); }
//private static void PatchChange(int value, int channel, int device = 0) //{ // MidiOutDevice(device).Send(MidiMessage.ChangePatch(value, channel).RawData); //} //public static void CCOut(string allparams) //{ // string[] words = allparams.Split('|'); // new Thread(() => // { // Thread.CurrentThread.IsBackground = true; // try // { // CCOut(Convert.ToInt16(words[0]), Convert.ToInt16(words[1]), Convert.ToInt16(words[2]),Midi.MidiOutDeviceNumber); // } // catch (Exception ex) // { // Debug.WriteLine("CCOut:", ex); // } // }).Start(); //} //public static void NoteOut(string allparams) //{ // string[] words = allparams.Split('|'); // new Thread(() => // { // Thread.CurrentThread.IsBackground = true; // Thread.CurrentThread.Priority = ThreadPriority.Highest; // try // { // int n, v, c, l; // if (int.TryParse(words[0], out n) && int.TryParse(words[1], out v) && int.TryParse(words[2], out c) && int.TryParse(words[3], out l)) // { // NoteOut(n,v,c,l, Midi.MidiOutDeviceNumber); // } // else // { // Debug.WriteLine("NoteOut:","Failed to input:"+ allparams); // } // } // catch (Exception ex) // { // Debug.WriteLine("NoteOut:", ex); // } // }).Start(); //} //public static void PatchChange(string allparams) //{ // string[] words = allparams.Split('|'); // new Thread(() => // { // Thread.CurrentThread.IsBackground = true; // try // { // PatchChange(Convert.ToInt16(words[0]), Convert.ToInt16(words[1]), Midi.MidiOutDeviceNumber); // } // catch (Exception ex) // { // Debug.WriteLine("PatchChange:", ex); // } // }).Start(); //} private static void Event_Received(object sender, MidiReceivedEventArgs eargs) { MidiEvent e = MidiEvent.Convert(eargs.Data, 0, eargs.Length).First(); Debug.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(e, Newtonsoft.Json.Formatting.Indented)); Debug.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(MidiEvent.Convert(eargs.Data, 0, eargs.Length).First(), Newtonsoft.Json.Formatting.Indented)); try { //// Exit if the MidiEvent is null or is the AutoSensing command code if (e.EventType == MidiEvent.ActiveSense) { return; } else if (e.EventType == MidiEvent.CC) { //TODO: value is wrong Midi.SetCCValue(e.Channel, (int)e.Msb, (int)e.Lsb); } //else if ( e.EventType == MidiEvent.TimingClock) //{ // rawticks++; // if (rawticks % tickdivider == 0) // { // // if (clockrunning ) // { // Combustion.Tick(); // } // } //} //else if ( e.EventType == MidiEvent.StartSequence) //{ // rawticks = 0; // clockrunning = true; // Combustion.Start(); //} //else if ( e.EventType == MidiEvent.StopSequence) //{ // clockrunning = false; // Combustion.Stop(); //} else { Debug.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(e, Newtonsoft.Json.Formatting.Indented)); Debug.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(MidiEvent.Convert(eargs.Data, 0, eargs.Length).First(), Newtonsoft.Json.Formatting.Indented)); } } catch (Exception ex) { Debug.WriteLine(ex.Message); } }
private void Message_Received(object sender, MidiReceivedEventArgs eargs) { MidiEvent e = MidiEvent.Convert(eargs.Data, 0, eargs.Length).First(); if (e.EventType == MidiEvent.CC) { int CC = (int)e.Msb; int value = (int)e.Lsb; if ((CC >= 48) && (CC <= 55)) { Faders[CC - 48] = value; } return; } int NoteNumber = (int)e.Msb; if (NoteNumber >= 84 && NoteNumber <= 88 && ((int)CurrentPageType) != (NoteNumber - 84)) { pagetypehistory.Add(CurrentPageType); CurrentPageType = (PageType)(NoteNumber - 84); } else if (shiftdown && (NoteNumber == 66 || NoteNumber == 67) && e.EventType == MidiEvent.NoteOn) { pagetypehistory.Add(CurrentPageType); if (NoteNumber == 67) { CurrentPageType = Enum.GetValues(typeof(PageType)).Cast <PageType>().Where(ze => (int)ze > (int)CurrentPageType).OrderBy(ze => ze).FirstOrDefault(); } else if (NoteNumber == 67) { CurrentPageType = Enum.GetValues(typeof(PageType)).Cast <PageType>().Where(ze => (int)ze < (int)CurrentPageType).OrderByDescending(ze => ze).FirstOrDefault(); } } else if (NoteNumber == 98 & (e.EventType == MidiEvent.NoteOn || e.EventType == MidiEvent.NoteOff)) { shiftdown = !shiftdown; } else if (CurrentPageType == PageType.Seq64 && e.EventType == MidiEvent.NoteOn) { if (NoteNumber <= 63) { if (shiftdown) { Seq64Length = NoteNumber + 1; } else { Seq64[NoteNumber] = !Seq64[NoteNumber]; } } if (NoteNumber == 89) { foreach (var x in Seq64.Keys.ToList()) { Seq64[x] = false; } } } else if (CurrentPageType == PageType.Clip && e.EventType == MidiEvent.NoteOn) { if (NoteNumber <= 63) { Clips[NoteNumber] = !Clips[NoteNumber]; } if (NoteNumber == 89) { foreach (var x in Clips.Keys.ToList()) { Clips[x] = false; } } } else if (CurrentPageType == PageType.Seq8a && e.EventType == MidiEvent.NoteOn) { if (NoteNumber <= 63) { Seq8a[(NoteNumber - NoteNumber % 8) / 8][NoteNumber % 8] = !Seq8a[(NoteNumber - NoteNumber % 8) / 8][NoteNumber % 8]; } if (NoteNumber == 89) { foreach (var x in Seq8a.Keys.ToList()) { foreach (var y in Seq8a[x].Keys.ToList()) { Seq8a[x][y] = false; } } } } else if (CurrentPageType == PageType.Seq8b && e.EventType == MidiEvent.NoteOn) { if (NoteNumber <= 63) { Seq8b[(NoteNumber - NoteNumber % 8) / 8][NoteNumber % 8] = !Seq8b[(NoteNumber - NoteNumber % 8) / 8][NoteNumber % 8]; } if (NoteNumber == 89) { foreach (var x in Seq8b.Keys.ToList()) { foreach (var y in Seq8b[x].Keys.ToList()) { Seq8b[x][y] = false; } } } } else if (CurrentPageType == PageType.Chord && e.EventType == MidiEvent.NoteOn) { if (NoteNumber <= 63) { if (Chords[(NoteNumber - NoteNumber % 8) / 8].Contains(NoteNumber % 8)) { Chords[(NoteNumber - NoteNumber % 8) / 8].Remove(NoteNumber % 8); } else { Chords[(NoteNumber - NoteNumber % 8) / 8].Add(NoteNumber % 8); } } if (NoteNumber == 89) { foreach (var x in Chords.Keys.ToList()) { Chords[x].Clear(); } } } DrawPage(); }