From 86bb4c46a0127314db71b553e16b5eb56f8555ce Mon Sep 17 00:00:00 2001 From: tzim Date: Thu, 15 May 2014 13:51:04 +0000 Subject: [PATCH] Feedback Midi fonctionnel --- DMX-2.0/DMX-2.0.csproj | 1 + DMX-2.0/MidiEventProvider.PInvoke.cs | 8 +++ DMX-2.0/MidiEventProvider.cs | 95 ++++++++++++++++++++++++++-- DMX-2.0/SequenceurLineaire.cs | 2 +- 4 files changed, 101 insertions(+), 5 deletions(-) diff --git a/DMX-2.0/DMX-2.0.csproj b/DMX-2.0/DMX-2.0.csproj index 4b716d3..bd0ae91 100644 --- a/DMX-2.0/DMX-2.0.csproj +++ b/DMX-2.0/DMX-2.0.csproj @@ -20,6 +20,7 @@ prompt 4 false + aguibtn none diff --git a/DMX-2.0/MidiEventProvider.PInvoke.cs b/DMX-2.0/MidiEventProvider.PInvoke.cs index fdc11b2..c186222 100644 --- a/DMX-2.0/MidiEventProvider.PInvoke.cs +++ b/DMX-2.0/MidiEventProvider.PInvoke.cs @@ -209,6 +209,8 @@ namespace DMX2 public int result; } + const int sizeof_snd_seq_event_t = 24; + [StructLayout(LayoutKind.Explicit)] struct snd_seq_event_t { [FieldOffset(0)] @@ -255,6 +257,12 @@ namespace DMX2 [DllImport(ASOUND_LIB_NAME, CallingConvention = CallingConvention.Cdecl)] static extern int snd_seq_free_event(IntPtr ev); + + [DllImport(ASOUND_LIB_NAME, CallingConvention = CallingConvention.Cdecl)] + static extern int snd_seq_event_output (IntPtr seq, IntPtr ev); + + [DllImport(ASOUND_LIB_NAME, CallingConvention = CallingConvention.Cdecl)] + static extern int snd_seq_drain_output (IntPtr seq); } } diff --git a/DMX-2.0/MidiEventProvider.cs b/DMX-2.0/MidiEventProvider.cs index ef5a3bc..32dff1e 100644 --- a/DMX-2.0/MidiEventProvider.cs +++ b/DMX-2.0/MidiEventProvider.cs @@ -40,12 +40,28 @@ namespace DMX2 } } + int inport; + int outport; + + public static int InPort { + get { + return singleton.inport; + } + } + public static int OutPort { + get { + return singleton.outport; + } + } MidiSeqHandle(){ snd_seq_open(out midi_seq_handle, "default",SND_SEQ_OPEN_DUPLEX,0); snd_seq_set_client_name(midi_seq_handle,"DMX2"); - snd_seq_create_simple_port(midi_seq_handle,"dmx_ctrl", + inport= snd_seq_create_simple_port(midi_seq_handle,"dmx_ctrl_in", SND_SEQ_PORT_CAP_WRITE + SND_SEQ_PORT_CAP_SUBS_WRITE, SND_SEQ_PORT_TYPE_APPLICATION); + outport= snd_seq_create_simple_port(midi_seq_handle,"dmx_ctrl_out", + SND_SEQ_PORT_CAP_READ + SND_SEQ_PORT_CAP_SUBS_READ, + SND_SEQ_PORT_TYPE_APPLICATION); } #region IDisposable implementation @@ -72,17 +88,76 @@ namespace DMX2 } } - class midiFeedbackInfo : IFeedbackInfo { + + class feedbackinfo : IFeedbackInfo { + + MidiEventProvider prov; + snd_seq_event_t ev; + + public feedbackinfo(MidiEventProvider _prov, byte channel,uint param){ + ev = new snd_seq_event_t(); prov=_prov; + ev.data_ev_ctrl.channel = channel; + ev.data_ev_ctrl.param= param; + ev.type = snd_seq_event_type_t.SND_SEQ_EVENT_CONTROLLER; + } + #region IFeedbackInfo implementation bool IFeedbackInfo.FeedBack (byte data) { - throw new System.NotImplementedException (); + ev.data_ev_ctrl.value = (byte)((int)data * 127 / 255) ; + prov.SendEvent(ev); + return true; } #endregion } - Dictionary eventlist = new Dictionary(); + public class PointerWrapper : IDisposable + { + public IntPtr Pointer { get; private set; } + public PointerWrapper (int pointerSize) + { + Pointer = Marshal.AllocHGlobal (pointerSize); + } + + public PointerWrapper (IntPtr ptr) + { + Pointer = ptr; + } + + ~PointerWrapper () + { + Dispose (false); + } + + public void Dispose () + { + Dispose (true); + GC.SuppressFinalize (this); + } + + protected virtual void Dispose (bool disposing) + { + if (Pointer != IntPtr.Zero) { + Marshal.FreeHGlobal (Pointer); + } + } + } + + PointerWrapper evPtr = new PointerWrapper(32); + + void SendEvent (snd_seq_event_t ev) + { + ev.queue = 253; + ev.source.port = (byte) MidiSeqHandle.OutPort; + ev.dest.client=254; + ev.dest.port= 0; + Marshal.StructureToPtr(ev,evPtr.Pointer,false); + snd_seq_event_output(MidiSeqHandle.Handle,evPtr.Pointer); + snd_seq_drain_output(MidiSeqHandle.Handle); + } + + Dictionary eventlist = new Dictionary(); EventData last; internalEvent levent=null; bool connected=false; @@ -212,9 +287,21 @@ namespace DMX2 } } + static System.Text.RegularExpressions.Regex regexCtrlEventID = new System.Text.RegularExpressions.Regex( + @"MIDI-CTRL-C(?\d+)P(?\d+)", + System.Text.RegularExpressions.RegexOptions.Compiled); + IFeedbackInfo IEventProvider.GetFeedbackInfo (string eventId) { + var res = regexCtrlEventID.Match (eventId); + if (res.Success) { + Console.WriteLine("Succes"); + byte chan = byte.Parse (res.Groups ["chan"].Value); + uint param = uint.Parse (res.Groups ["param"].Value); + return new feedbackinfo (this, chan, param); + } return null; + } #endregion diff --git a/DMX-2.0/SequenceurLineaire.cs b/DMX-2.0/SequenceurLineaire.cs index 47fdb54..67ac925 100644 --- a/DMX-2.0/SequenceurLineaire.cs +++ b/DMX-2.0/SequenceurLineaire.cs @@ -173,7 +173,7 @@ namespace DMX2 List feedbacks = new List(); - bool IEventTarget.CanFeedback { get { return false; } } + bool IEventTarget.CanFeedback { get { return true; } } void IEventTarget.AddFeedback (IFeedbackInfo info) {