From 89fd0f36c6830f04da7cabae02a12d1f7624b0d3 Mon Sep 17 00:00:00 2001 From: tzim Date: Sun, 25 May 2014 21:13:59 +0000 Subject: [PATCH] --- DMX-2.0/AlsaSeqLib.AlsaTypes.cs | 4 +- DMX-2.0/AlsaSeqLib.Invoke.cs | 7 + DMX-2.0/AlsaSeqLib.cs | 178 ++++++++------- DMX-2.0/MidiEventProvider.cs | 378 +++++++++++++++++--------------- 4 files changed, 306 insertions(+), 261 deletions(-) diff --git a/DMX-2.0/AlsaSeqLib.AlsaTypes.cs b/DMX-2.0/AlsaSeqLib.AlsaTypes.cs index d16cbbe..51b0dd1 100644 --- a/DMX-2.0/AlsaSeqLib.AlsaTypes.cs +++ b/DMX-2.0/AlsaSeqLib.AlsaTypes.cs @@ -27,7 +27,8 @@ namespace DMX2 public const byte SND_SEQ_ADDRESS_BROADCAST = 255; public const byte SND_SEQ_QUEUE_DIRECT = 253; - public enum snd_seq_client_type_t : int { + public enum snd_seq_client_type_t : int + { SND_SEQ_USER_CLIENT = 1, SND_SEQ_KERNEL_CLIENT = 2 } @@ -276,6 +277,7 @@ namespace DMX2 } return _subscriberInfoSize; } + static int GetQuerySubscribeInfoSize () { if (_query_subscribeInfoSize == 0) { diff --git a/DMX-2.0/AlsaSeqLib.Invoke.cs b/DMX-2.0/AlsaSeqLib.Invoke.cs index 07f82e8..bdb6be1 100644 --- a/DMX-2.0/AlsaSeqLib.Invoke.cs +++ b/DMX-2.0/AlsaSeqLib.Invoke.cs @@ -97,20 +97,25 @@ namespace DMX2 [DllImport(ASOUND_LIB_NAME, CallingConvention = CallingConvention.Cdecl)] public static extern int snd_seq_port_subscribe_sizeof (); + [DllImport(ASOUND_LIB_NAME, CallingConvention = CallingConvention.Cdecl)] public static extern int snd_seq_query_subscribe_sizeof (); [DllImport(ASOUND_LIB_NAME, CallingConvention = CallingConvention.Cdecl)] public static extern int snd_seq_query_subscribe_get_client (IntPtr info); + [DllImport(ASOUND_LIB_NAME, CallingConvention = CallingConvention.Cdecl)] public static extern int snd_seq_query_subscribe_get_port (IntPtr info); [DllImport(ASOUND_LIB_NAME, CallingConvention = CallingConvention.Cdecl)] public static extern int snd_seq_query_subscribe_get_queue (IntPtr info); + [DllImport(ASOUND_LIB_NAME, CallingConvention = CallingConvention.Cdecl)] public static extern int snd_seq_query_subscribe_get_time_real (IntPtr info); + [DllImport(ASOUND_LIB_NAME, CallingConvention = CallingConvention.Cdecl)] public static extern int snd_seq_query_subscribe_get_time_update (IntPtr info); + [DllImport(ASOUND_LIB_NAME, CallingConvention = CallingConvention.Cdecl)] public static extern int snd_seq_query_subscribe_get_exclusive (IntPtr info); @@ -140,6 +145,7 @@ namespace DMX2 [DllImport(ASOUND_LIB_NAME, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr snd_seq_query_subscribe_get_addr (IntPtr subs); + [DllImport(ASOUND_LIB_NAME, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr snd_seq_query_subscribe_get_root (IntPtr subs); @@ -175,6 +181,7 @@ namespace DMX2 [DllImport(ASOUND_LIB_NAME, CallingConvention = CallingConvention.Cdecl)] public static extern int snd_seq_connect_to (IntPtr handle, int myport, int dest_client, int dest_port); + [DllImport(ASOUND_LIB_NAME, CallingConvention = CallingConvention.Cdecl)] public static extern int snd_seq_disconnect_from (IntPtr handle, int myport, int src_client, int src_port); diff --git a/DMX-2.0/AlsaSeqLib.cs b/DMX-2.0/AlsaSeqLib.cs index e0ce4ff..c2c3141 100644 --- a/DMX-2.0/AlsaSeqLib.cs +++ b/DMX-2.0/AlsaSeqLib.cs @@ -7,8 +7,7 @@ namespace DMX2 { public static partial class AlsaSeqLib { - static SeqHandleWrapper seq_handle=null; - + static SeqHandleWrapper seq_handle = null; static int clientId; static int inport; static int outport; @@ -19,8 +18,7 @@ namespace DMX2 int id; snd_seq_client_type_t type; string name; - - List ports = new List(); + List ports = new List (); public int Id { get { @@ -33,6 +31,7 @@ namespace DMX2 return type; } } + public string Name { get { return name; @@ -41,21 +40,22 @@ namespace DMX2 public ReadOnlyCollection Ports { get { - return ports.AsReadOnly(); + return ports.AsReadOnly (); } } - internal Client(PointerWrapper clientInfo){ - id = Invoke.snd_seq_client_info_get_client(clientInfo.Pointer); - IntPtr nameptr = Invoke.snd_seq_client_info_get_name(clientInfo.Pointer); - name = nameptr.PtrToString(); - type = (snd_seq_client_type_t)Invoke.snd_seq_client_info_get_type(clientInfo.Pointer); + internal Client (PointerWrapper clientInfo) + { + id = Invoke.snd_seq_client_info_get_client (clientInfo.Pointer); + IntPtr nameptr = Invoke.snd_seq_client_info_get_name (clientInfo.Pointer); + name = nameptr.PtrToString (); + type = (snd_seq_client_type_t)Invoke.snd_seq_client_info_get_type (clientInfo.Pointer); using (PointerWrapper portInfo = new PointerWrapper(GetPortInfoSize ())) { Invoke.snd_seq_port_info_set_client (portInfo.Pointer, id); Invoke.snd_seq_port_info_set_port (portInfo.Pointer, -1); - while(Invoke.snd_seq_query_next_port(seq_handle.Handle, portInfo.Pointer) >= 0) { - ports.Add(new Port(portInfo)); + while (Invoke.snd_seq_query_next_port(seq_handle.Handle, portInfo.Pointer) >= 0) { + ports.Add (new Port (portInfo)); } } } @@ -79,6 +79,7 @@ namespace DMX2 return clientId; } } + public int PortId { get { return portId; @@ -103,54 +104,59 @@ namespace DMX2 } } - internal Port(PointerWrapper portInfo){ - clientId = Invoke.snd_seq_port_info_get_client(portInfo.Pointer); - portId = Invoke.snd_seq_port_info_get_port(portInfo.Pointer); - IntPtr namePtr = Invoke.snd_seq_port_info_get_name(portInfo.Pointer); + internal Port (PointerWrapper portInfo) + { + clientId = Invoke.snd_seq_port_info_get_client (portInfo.Pointer); + portId = Invoke.snd_seq_port_info_get_port (portInfo.Pointer); + IntPtr namePtr = Invoke.snd_seq_port_info_get_name (portInfo.Pointer); caps = Invoke.snd_seq_port_info_get_capability (portInfo.Pointer); - type = Invoke.snd_seq_port_info_get_type(portInfo.Pointer); - name = namePtr.PtrToString(); + type = Invoke.snd_seq_port_info_get_type (portInfo.Pointer); + name = namePtr.PtrToString (); } } public static void Init () { - if(seq_handle!=null) return; + if (seq_handle != null) + return; - seq_handle = new SeqHandleWrapper(); + seq_handle = new SeqHandleWrapper (); - clientId = Invoke.snd_seq_client_id(seq_handle.Handle); + clientId = Invoke.snd_seq_client_id (seq_handle.Handle); - outport = inport= Invoke.snd_seq_create_simple_port(seq_handle.Handle,"dmx_ctrl", + outport = inport = Invoke.snd_seq_create_simple_port (seq_handle.Handle, "dmx_ctrl", SND_SEQ_PORT_CAP_WRITE + SND_SEQ_PORT_CAP_SUBS_WRITE + - SND_SEQ_PORT_CAP_READ + SND_SEQ_PORT_CAP_SUBS_READ, + SND_SEQ_PORT_CAP_READ + SND_SEQ_PORT_CAP_SUBS_READ, SND_SEQ_PORT_TYPE_APPLICATION); } public static void Close () { - if(seq_handle==null) return; - seq_handle.Dispose(); + if (seq_handle == null) + return; + seq_handle.Dispose (); seq_handle = null; } - - public static bool GetEvent(out snd_seq_event_t ev){ - if(seq_handle==null) throw new InvalidOperationException(); - ev = new snd_seq_event_t(); - if(Invoke.snd_seq_event_input_pending(seq_handle.Handle,1)<=0) return false; + public static bool GetEvent (out snd_seq_event_t ev) + { + if (seq_handle == null) + throw new InvalidOperationException (); + ev = new snd_seq_event_t (); + if (Invoke.snd_seq_event_input_pending (seq_handle.Handle, 1) <= 0) + return false; IntPtr evPtr; // Recup du pointeur vers l'ev - Invoke.snd_seq_event_input(seq_handle.Handle, out evPtr); + Invoke.snd_seq_event_input (seq_handle.Handle, out evPtr); // Copie de la zone mémoire dans une structure managee - ev =(snd_seq_event_t) Marshal.PtrToStructure(evPtr,typeof(snd_seq_event_t)); + ev = (snd_seq_event_t)Marshal.PtrToStructure (evPtr, typeof(snd_seq_event_t)); // liberation du pointeur - Invoke.snd_seq_free_event(evPtr); + Invoke.snd_seq_free_event (evPtr); // TODO : Manage System Events here @@ -158,43 +164,45 @@ namespace DMX2 } - - - static PointerWrapper evOutPtr = new PointerWrapper(32); + static PointerWrapper evOutPtr = new PointerWrapper (32); public static void SendEvent (snd_seq_event_t ev) { - if(seq_handle==null) throw new InvalidOperationException(); + if (seq_handle == null) + throw new InvalidOperationException (); ev.queue = SND_SEQ_QUEUE_DIRECT; ev.source.client = (byte)clientId; - ev.source.port = (byte) outport; - Marshal.StructureToPtr(ev,evOutPtr.Pointer,false); - Invoke.snd_seq_event_output(seq_handle.Handle,evOutPtr.Pointer); - Invoke.snd_seq_drain_output(seq_handle.Handle); + ev.source.port = (byte)outport; + Marshal.StructureToPtr (ev, evOutPtr.Pointer, false); + Invoke.snd_seq_event_output (seq_handle.Handle, evOutPtr.Pointer); + Invoke.snd_seq_drain_output (seq_handle.Handle); } public static void SendEventToSubscribers (snd_seq_event_t ev) { - if(seq_handle==null) throw new InvalidOperationException(); - ev.dest.client=SND_SEQ_ADDRESS_SUBSCRIBERS; - ev.dest.port= SND_SEQ_ADDRESS_UNKNOWN; - SendEvent(ev); + if (seq_handle == null) + throw new InvalidOperationException (); + ev.dest.client = SND_SEQ_ADDRESS_SUBSCRIBERS; + ev.dest.port = SND_SEQ_ADDRESS_UNKNOWN; + SendEvent (ev); } public static IEnumerable EnumClients () { - if(seq_handle==null) throw new InvalidOperationException(); + if (seq_handle == null) + throw new InvalidOperationException (); using (PointerWrapper clientInfo = new PointerWrapper(GetClientInfoSize())) { - Invoke.snd_seq_client_info_set_client(clientInfo.Pointer, -1); + Invoke.snd_seq_client_info_set_client (clientInfo.Pointer, -1); while (Invoke.snd_seq_query_next_client(seq_handle.Handle,clientInfo.Pointer)>=0) { - yield return new Client(clientInfo); + yield return new Client (clientInfo); } } } public static Client GetClientByID (int client) { - if(seq_handle==null) throw new InvalidOperationException(); + if (seq_handle == null) + throw new InvalidOperationException (); using (PointerWrapper clientInfo = new PointerWrapper(GetClientInfoSize())) { if (Invoke.snd_seq_get_any_client_info (seq_handle.Handle, client, clientInfo.Pointer) >= 0) { return new Client (clientInfo); @@ -203,90 +211,96 @@ namespace DMX2 } } - public static Port GetPortByIDs(int client,int port){ - if(seq_handle==null) throw new InvalidOperationException(); + public static Port GetPortByIDs (int client, int port) + { + if (seq_handle == null) + throw new InvalidOperationException (); using (PointerWrapper portInfo = new PointerWrapper(GetPortInfoSize ())) { - if(Invoke.snd_seq_get_any_port_info(seq_handle.Handle,client,port,portInfo.Pointer) >= 0) { - return new Port(portInfo); - } - else + if (Invoke.snd_seq_get_any_port_info (seq_handle.Handle, client, port, portInfo.Pointer) >= 0) { + return new Port (portInfo); + } else return null; } } - public static bool Connect(Port port) + public static bool Connect (Port port) { - if(seq_handle==null) throw new InvalidOperationException(); + if (seq_handle == null) + throw new InvalidOperationException (); bool isInput = (port.Caps & SND_SEQ_PORT_CAP_WRITE) == SND_SEQ_PORT_CAP_WRITE; bool isOutput = (port.Caps & SND_SEQ_PORT_CAP_READ) == SND_SEQ_PORT_CAP_READ; if (isInput) - if (!ConnectTo (port.ClientId, port.PortId)) - return false; + if (!ConnectTo (port.ClientId, port.PortId)) + return false; if (isOutput) - if (!ConnectFrom (port.ClientId, port.PortId)) - return false; + if (!ConnectFrom (port.ClientId, port.PortId)) + return false; return true; } - public static bool ConnectTo(int client, int port){ - if(seq_handle==null) throw new InvalidOperationException(); - return Invoke.snd_seq_connect_to(seq_handle.Handle,outport,client,port)==0; + public static bool ConnectTo (int client, int port) + { + if (seq_handle == null) + throw new InvalidOperationException (); + return Invoke.snd_seq_connect_to (seq_handle.Handle, outport, client, port) == 0; } - public static bool ConnectFrom(int client, int port){ - if(seq_handle==null) throw new InvalidOperationException(); - return Invoke.snd_seq_connect_from(seq_handle.Handle,inport,client,port)==0; + + public static bool ConnectFrom (int client, int port) + { + if (seq_handle == null) + throw new InvalidOperationException (); + return Invoke.snd_seq_connect_from (seq_handle.Handle, inport, client, port) == 0; } public static void Deconnecte (int client, int port) { - if(seq_handle==null) throw new InvalidOperationException(); + if (seq_handle == null) + throw new InvalidOperationException (); snd_seq_addr_t local = new snd_seq_addr_t (); local.client = (byte)clientId; local.port = (byte)inport; snd_seq_addr_t addr; using (PointerWrapper subqueryInfo = new PointerWrapper(GetQuerySubscribeInfoSize())) - using (PointerWrapper localAddr =new PointerWrapper(local.SndSeqAddrToPtr())) - - { - Invoke.snd_seq_query_subscribe_set_root(subqueryInfo.Pointer,localAddr.Pointer); - Invoke.snd_seq_query_subscribe_set_type(subqueryInfo.Pointer,SND_SEQ_QUERY_SUBS_WRITE); + using (PointerWrapper localAddr =new PointerWrapper(local.SndSeqAddrToPtr())) { + Invoke.snd_seq_query_subscribe_set_root (subqueryInfo.Pointer, localAddr.Pointer); + Invoke.snd_seq_query_subscribe_set_type (subqueryInfo.Pointer, SND_SEQ_QUERY_SUBS_WRITE); for (Invoke.snd_seq_query_subscribe_set_index(subqueryInfo.Pointer,0); Invoke.snd_seq_query_port_subscribers (seq_handle.Handle,subqueryInfo.Pointer)>=0; Invoke.snd_seq_query_subscribe_set_index(subqueryInfo.Pointer, Invoke.snd_seq_query_subscribe_get_index(subqueryInfo.Pointer) +1) - ){ + ) { //root = Invoke.snd_seq_query_subscribe_get_root(subqueryInfo.Pointer).PtrToSndSeqAddr(); - addr = Invoke.snd_seq_query_subscribe_get_addr(subqueryInfo.Pointer).PtrToSndSeqAddr(); + addr = Invoke.snd_seq_query_subscribe_get_addr (subqueryInfo.Pointer).PtrToSndSeqAddr (); - if( addr.client != client || addr.port != port) + if (addr.client != client || addr.port != port) continue; - Invoke.snd_seq_disconnect_from(seq_handle.Handle,inport,client,port); + Invoke.snd_seq_disconnect_from (seq_handle.Handle, inport, client, port); } - Invoke.snd_seq_query_subscribe_set_type(subqueryInfo.Pointer,SND_SEQ_QUERY_SUBS_READ); + Invoke.snd_seq_query_subscribe_set_type (subqueryInfo.Pointer, SND_SEQ_QUERY_SUBS_READ); for (Invoke.snd_seq_query_subscribe_set_index(subqueryInfo.Pointer,0); Invoke.snd_seq_query_port_subscribers (seq_handle.Handle,subqueryInfo.Pointer)>=0; Invoke.snd_seq_query_subscribe_set_index(subqueryInfo.Pointer, Invoke.snd_seq_query_subscribe_get_index(subqueryInfo.Pointer) +1) - ){ + ) { //root = Invoke.snd_seq_query_subscribe_get_root(subqueryInfo.Pointer).PtrToSndSeqAddr(); - addr = Invoke.snd_seq_query_subscribe_get_addr(subqueryInfo.Pointer).PtrToSndSeqAddr(); + addr = Invoke.snd_seq_query_subscribe_get_addr (subqueryInfo.Pointer).PtrToSndSeqAddr (); - if( addr.client != client || addr.port != port) + if (addr.client != client || addr.port != port) continue; - Invoke.snd_seq_disconnect_to(seq_handle.Handle,outport,client,port); + Invoke.snd_seq_disconnect_to (seq_handle.Handle, outport, client, port); } } diff --git a/DMX-2.0/MidiEventProvider.cs b/DMX-2.0/MidiEventProvider.cs index b6696a0..1e4d01a 100644 --- a/DMX-2.0/MidiEventProvider.cs +++ b/DMX-2.0/MidiEventProvider.cs @@ -15,7 +15,6 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ - using System; using System.Runtime.InteropServices; using System.Collections.Generic; @@ -26,10 +25,11 @@ namespace DMX2 public class MidiEventProvider : IEventProvider, IDisposable { - class internalEvent { + class internalEvent + { readonly string internalName; - bool bound=false; + bool bound = false; readonly uint page; readonly int midiEvCode; @@ -60,15 +60,14 @@ namespace DMX2 } } - - public internalEvent(string _id, uint _page, int _evHCode) + public internalEvent (string _id, uint _page, int _evHCode) { - internalName=_id; - page=_page; + internalName = _id; + page = _page; midiEvCode = _evHCode; } - int lastknownvalue=-1; + int lastknownvalue = -1; public int LastKnownValue { get { @@ -80,6 +79,7 @@ namespace DMX2 } AlsaSeqLib.snd_seq_event_t storedevent; + public AlsaSeqLib.snd_seq_event_t StoredEvent { get { return storedevent; @@ -89,18 +89,20 @@ namespace DMX2 } } } - abstract class feedbackinfo : IFeedbackInfo { + + abstract class feedbackinfo : IFeedbackInfo + { MidiEventProvider prov; readonly internalEvent iev; protected AlsaSeqLib.snd_seq_event_t ev; - - public feedbackinfo(MidiEventProvider _prov, internalEvent _iev){ + public feedbackinfo (MidiEventProvider _prov, internalEvent _iev) + { prov = _prov; - iev=_iev; + iev = _iev; } - protected abstract bool UpdateEvent(byte data); + protected abstract bool UpdateEvent (byte data); #region IFeedbackInfo implementation bool IFeedbackInfo.FeedBack (byte data) @@ -108,14 +110,15 @@ namespace DMX2 bool update = UpdateEvent (data); iev.StoredEvent = ev; - if (!update) return true; + if (!update) + return true; iev.LastKnownValue = data; if (prov.CurrentPage == iev.Page || iev.Page == 0) { prov.SendEvent (ev); foreach (int srcid in prov.feedbacksources) { - prov.lastValueOfSrc[CombineHash( srcid , iev.MidiEvCode)] = data; + prov.lastValueOfSrc [CombineHash (srcid, iev.MidiEvCode)] = data; } } @@ -123,55 +126,65 @@ namespace DMX2 } #endregion } - class ctrlfeedbackinfo : feedbackinfo { - public ctrlfeedbackinfo(MidiEventProvider _prov, internalEvent _iev, byte channel,uint param) + + class ctrlfeedbackinfo : feedbackinfo + { + public ctrlfeedbackinfo (MidiEventProvider _prov, internalEvent _iev, byte channel, uint param) :base(_prov,_iev) { - ev = new AlsaSeqLib.snd_seq_event_t(); + ev = new AlsaSeqLib.snd_seq_event_t (); ev.data_ev_ctrl.channel = channel; - ev.data_ev_ctrl.param= param; + ev.data_ev_ctrl.param = param; ev.type = AlsaSeqLib.snd_seq_event_type_t.SND_SEQ_EVENT_CONTROLLER; } protected override bool UpdateEvent (byte data) { - int newvalue= (int)data * 127 / 255; - if(newvalue==ev.data_ev_ctrl.value) return false; + int newvalue = (int)data * 127 / 255; + if (newvalue == ev.data_ev_ctrl.value) + return false; ev.data_ev_ctrl.value = newvalue; return true; } } - class pitchbendfeedbackinfo : feedbackinfo { - public pitchbendfeedbackinfo(MidiEventProvider _prov, internalEvent _iev, byte channel) + + class pitchbendfeedbackinfo : feedbackinfo + { + public pitchbendfeedbackinfo (MidiEventProvider _prov, internalEvent _iev, byte channel) :base(_prov,_iev) { - ev = new AlsaSeqLib.snd_seq_event_t(); + ev = new AlsaSeqLib.snd_seq_event_t (); ev.data_ev_ctrl.channel = channel; ev.type = AlsaSeqLib.snd_seq_event_type_t.SND_SEQ_EVENT_PITCHBEND; } protected override bool UpdateEvent (byte data) { - int newvalue = (int)data * 16000 / 255-8000; - if(newvalue==ev.data_ev_ctrl.value) return false; + int newvalue = (int)data * 14000 / 255 - 7000; + if (newvalue == ev.data_ev_ctrl.value) + return false; ev.data_ev_ctrl.value = newvalue; return true; } } - public class MidiDev{ + public class MidiDev + { string name; - public string Name {get{return name;} } + + public string Name { get { return name; } } + public bool HasFeedback { get; set; } - readonly List connected = new List(); + readonly List connected = new List (); + public List ConnectedPorts { - get{return connected;} + get{ return connected;} } - public MidiDev(string _name) + public MidiDev (string _name) { - name=_name; + name = _name; } } @@ -179,45 +192,42 @@ namespace DMX2 /// /// Etat interne des evenements midi paginés. /// - readonly Dictionary eventlist = new Dictionary(); + readonly Dictionary eventlist = new Dictionary (); /// /// Liste des peripheriques connus (presents ou non) /// - readonly Dictionary knowndevices = new Dictionary(); + readonly Dictionary knowndevices = new Dictionary (); /// /// Liste des ports connectés avec feedback /// - readonly List feedbacksources = new List(); + readonly List feedbacksources = new List (); //static readonly Dictionary srcidToDev = new Dictionary(); - readonly List unpaginatedchannels = new List(); + readonly List unpaginatedchannels = new List (); /// /// Derniere valeur connue pour une evenement sur source donnée : /// Soit recue, soit envoyée (feedback / changement de page) /// - readonly Dictionary lastValueOfSrc = new Dictionary(); - - - EventData last; - internalEvent levent=null; - bool connected=false; - - bool guirefreshflag=false; - - uint page=1; - uint maxpage=8; + readonly Dictionary lastValueOfSrc = new Dictionary (); + EventData last; + internalEvent levent = null; + bool connected = false; + bool guirefreshflag = false; + uint page = 1; + uint maxpage = 8; public uint CurrentPage { get { return page; } set { - if(value<1 || value > maxpage) return; + if (value < 1 || value > maxpage) + return; page = value; - Refresh(); + Refresh (); } } @@ -229,6 +239,7 @@ namespace DMX2 maxpage = value; } } + public List UnpaginatedChannels { get { return unpaginatedchannels; @@ -237,30 +248,29 @@ namespace DMX2 public bool GuiRefreshFlag { get { - if(guirefreshflag){ - guirefreshflag=false; + if (guirefreshflag) { + guirefreshflag = false; return true; } return false; } } - public void ConnectDevice (string name) { - knowndevices.Add(name,new MidiDev(name)); - AutoConnect(); + knowndevices.Add (name, new MidiDev (name)); + AutoConnect (); } public void RefreshFeedback (string name) { foreach (int port in knowndevices[name].ConnectedPorts) { - if(knowndevices[name].HasFeedback){ - if(!feedbacksources.Contains(port)) - feedbacksources.Add(port); - }else{ - if(feedbacksources.Contains(port)) - feedbacksources.Remove(port); + if (knowndevices [name].HasFeedback) { + if (!feedbacksources.Contains (port)) + feedbacksources.Add (port); + } else { + if (feedbacksources.Contains (port)) + feedbacksources.Remove (port); } } } @@ -275,12 +285,13 @@ namespace DMX2 foreach (int connectedport in dev.ConnectedPorts) { int client = connectedport >> 8; int port = connectedport & 0xFF; - AlsaSeqLib.Deconnecte(client,port); + AlsaSeqLib.Deconnecte (client, port); } } - public bool IsKnownDevice(string name){ - return knowndevices.ContainsKey(name); + public bool IsKnownDevice (string name) + { + return knowndevices.ContainsKey (name); } public IEnumerable KnownDevices { @@ -289,7 +300,6 @@ namespace DMX2 } } - public MidiEventProvider (EventManager manager) { /*MidiDev dev = new MidiDev("VMPK Input:VMPK Input"); @@ -302,16 +312,16 @@ namespace DMX2 manager.RegisterProvider (this); AlsaSeqLib.Init (); - AlsaSeqLib.ConnectFrom(AlsaSeqLib.SND_SEQ_CLIENT_SYSTEM, AlsaSeqLib.SND_SEQ_PORT_SYSTEM_ANNOUNCE); + AlsaSeqLib.ConnectFrom (AlsaSeqLib.SND_SEQ_CLIENT_SYSTEM, AlsaSeqLib.SND_SEQ_PORT_SYSTEM_ANNOUNCE); - AutoConnect(); + AutoConnect (); } void AutoConnect () { foreach (var cli in AlsaSeqLib.EnumClients()) { - foreach(var p in cli.Ports){ - PortDetected(cli,p); + foreach (var p in cli.Ports) { + PortDetected (cli, p); } } } @@ -322,21 +332,21 @@ namespace DMX2 // teste si connection auto au port et connecte si besoin - guirefreshflag=true; + guirefreshflag = true; string fullportname = cli.Name + ':' + p.Name; - if(knowndevices.ContainsKey(fullportname)){ - int srcid = p.ClientId <<8 + p.PortId; - if(knowndevices[fullportname].ConnectedPorts.Contains(srcid)) + if (knowndevices.ContainsKey (fullportname)) { + int srcid = p.ClientId << 8 + p.PortId; + if (knowndevices [fullportname].ConnectedPorts.Contains (srcid)) return; - AlsaSeqLib.Connect(p); + AlsaSeqLib.Connect (p); } } void PortConnect (AlsaSeqLib.snd_seq_connect_t cn, bool connect) { - int clientId,portId; + int clientId, portId; if (cn.dest.client == AlsaSeqLib.ClientId) { clientId = cn.sender.client; portId = cn.sender.port; @@ -347,14 +357,15 @@ namespace DMX2 int srcid = clientId << 8 + portId; if (connect) { - string fpname= AlsaSeqLib.GetClientByID(clientId).Name + ":"+ AlsaSeqLib.GetPortByIDs (clientId, portId).Name; - if (!knowndevices.ContainsKey(fpname)) return; + string fpname = AlsaSeqLib.GetClientByID (clientId).Name + ":" + AlsaSeqLib.GetPortByIDs (clientId, portId).Name; + if (!knowndevices.ContainsKey (fpname)) + return; if (knowndevices [fpname].ConnectedPorts.Contains (srcid)) return; knowndevices [fpname].ConnectedPorts.Add (srcid); - if(knowndevices [fpname].HasFeedback) + if (knowndevices [fpname].HasFeedback) feedbacksources.Add (srcid); - guirefreshflag=true; + guirefreshflag = true; //srcidToDev[srcid] = knowndevices [fpname]; @@ -362,12 +373,11 @@ namespace DMX2 } foreach (var dev in knowndevices.Values) { - if(dev.ConnectedPorts.Contains (srcid)) - { + if (dev.ConnectedPorts.Contains (srcid)) { /*if(srcidToDev.ContainsKey(srcid)) srcidToDev.Remove(srcid);*/ - dev.ConnectedPorts.Remove(srcid); - guirefreshflag=true; + dev.ConnectedPorts.Remove (srcid); + guirefreshflag = true; return; } } @@ -383,23 +393,23 @@ namespace DMX2 protected bool HasFeedback (int source) { - return feedbacksources.Contains(source); + return feedbacksources.Contains (source); } public void SendEvent (AlsaSeqLib.snd_seq_event_t ev) { - AlsaSeqLib.SendEventToSubscribers(ev); + AlsaSeqLib.SendEventToSubscribers (ev); } public void Refresh () { foreach (var ievent in eventlist.Values) { - if (ievent.Page == page){ - SendEvent(ievent.StoredEvent); - foreach(int src in feedbacksources){ - int lnvk = CombineHash(src, ievent.MidiEvCode); - if(ievent.LastKnownValue!=-1) - lastValueOfSrc[lnvk] = (byte)ievent.LastKnownValue; + if (ievent.Page == page) { + SendEvent (ievent.StoredEvent); + foreach (int src in feedbacksources) { + int lnvk = CombineHash (src, ievent.MidiEvCode); + if (ievent.LastKnownValue != -1) + lastValueOfSrc [lnvk] = (byte)ievent.LastKnownValue; } } } @@ -408,7 +418,7 @@ namespace DMX2 #region IEventProvider implementation - static System.Text.RegularExpressions.Regex regexEventID = new System.Text.RegularExpressions.Regex( + static System.Text.RegularExpressions.Regex regexEventID = new System.Text.RegularExpressions.Regex ( @"MIDI-PAGE(?\d+)-(?.+)", System.Text.RegularExpressions.RegexOptions.Compiled); @@ -420,26 +430,29 @@ namespace DMX2 if (! eventlist.ContainsKey (eventId)) { var res = regexEventID.Match (eventId); - if (!res.Success) return false; + if (!res.Success) + return false; uint _page = uint.Parse (res.Groups ["page"].Value); - int _evHC = res.Groups["id"].Value.GetHashCode(); - eventlist.Add (eventId, new internalEvent (eventId,_page,_evHC )); + int _evHC = res.Groups ["id"].Value.GetHashCode (); + eventlist.Add (eventId, new internalEvent (eventId, _page, _evHC)); } - eventlist[eventId].Bound = true; + eventlist [eventId].Bound = true; return true; } void IEventProvider.Unbind (string eventId) { - if(! eventlist.ContainsKey(eventId)) return; - eventlist[eventId].Bound = false; + if (! eventlist.ContainsKey (eventId)) + return; + eventlist [eventId].Bound = false; return; } Gtk.Menu IEventProvider.GetProviderSubMenu (EventManager.EventMenuData state, Gtk.ButtonPressEventHandler handler) { - if(!connected) return null; // Si pas encore recu d'evenements => pas de menu + if (!connected) + return null; // Si pas encore recu d'evenements => pas de menu Gtk.Menu retmenu = new Gtk.Menu (); if (levent != null) { // Creation du sous menu "Dernier" @@ -447,9 +460,9 @@ namespace DMX2 retmenu.Add (lmenuitem); Gtk.Menu lmenu = new Gtk.Menu (); lmenuitem.Submenu = lmenu;*/ - Gtk.MenuItem item = new Gtk.MenuItem(GetDescription(levent.InternalName)); - item.Data[EventManager.EventIdKey] = levent.InternalName; - item.Data[EventManager.StateKey] = state; + Gtk.MenuItem item = new Gtk.MenuItem (GetDescription (levent.InternalName)); + item.Data [EventManager.EventIdKey] = levent.InternalName; + item.Data [EventManager.StateKey] = state; item.ButtonPressEvent += handler; retmenu.Add (item); } @@ -459,14 +472,14 @@ namespace DMX2 Gtk.Menu evmenu = new Gtk.Menu (); evmenuitem.Submenu = evmenu; - List sortedKeys = eventlist.Keys.ToList(); // On recupere des IDs - sortedKeys.Sort(); // et on les trie + List sortedKeys = eventlist.Keys.ToList (); // On recupere des IDs + sortedKeys.Sort (); // et on les trie - foreach ( string key in sortedKeys ) { - internalEvent evt= eventlist[key]; - Gtk.MenuItem item = new Gtk.MenuItem(GetDescription(evt.InternalName)); - item.Data[EventManager.EventIdKey] = evt.InternalName; - item.Data[EventManager.StateKey] = state; + foreach (string key in sortedKeys) { + internalEvent evt = eventlist [key]; + Gtk.MenuItem item = new Gtk.MenuItem (GetDescription (evt.InternalName)); + item.Data [EventManager.EventIdKey] = evt.InternalName; + item.Data [EventManager.StateKey] = state; item.ButtonPressEvent += handler; evmenu.Add (item); } @@ -480,49 +493,54 @@ namespace DMX2 AlsaSeqLib.snd_seq_event_t evS; uint evpage; - // Tant qu'il y des evenements midi en attente + // Tant qu'il y des evenements midi en attente while (AlsaSeqLib.GetEvent(out evS)) { string id = null; - int value=0; + int value = 0; byte channel = 255; switch (evS.type) { case AlsaSeqLib.snd_seq_event_type_t.SND_SEQ_EVENT_PORT_SUBSCRIBED: // Connection d'un périph midi case AlsaSeqLib.snd_seq_event_type_t.SND_SEQ_EVENT_PORT_UNSUBSCRIBED: - PortConnect( + PortConnect ( evS.data_connect, - evS.type==AlsaSeqLib.snd_seq_event_type_t.SND_SEQ_EVENT_PORT_SUBSCRIBED - ); + evS.type == AlsaSeqLib.snd_seq_event_type_t.SND_SEQ_EVENT_PORT_SUBSCRIBED + ); continue; case AlsaSeqLib.snd_seq_event_type_t.SND_SEQ_EVENT_CONTROLLER: - if(evS.data_ev_ctrl.param==127 && evS.data_ev_ctrl.value >0){ - CurrentPage++; continue; + if (evS.data_ev_ctrl.param == 127 && evS.data_ev_ctrl.value > 0) { + CurrentPage++; + continue; } - if(evS.data_ev_ctrl.param==126 && evS.data_ev_ctrl.value>0){ - CurrentPage--; continue; + if (evS.data_ev_ctrl.param == 126 && evS.data_ev_ctrl.value > 0) { + CurrentPage--; + continue; } - id = string.Format("CTRL-C{0}P{1}",evS.data_ev_ctrl.channel,evS.data_ev_ctrl.param); + id = string.Format ("CTRL-C{0}P{1}", evS.data_ev_ctrl.channel, evS.data_ev_ctrl.param); channel = evS.data_ev_ctrl.channel; value = 255 * evS.data_ev_ctrl.value / 127; // Conversion {0,127} => {0,255} break; case AlsaSeqLib.snd_seq_event_type_t.SND_SEQ_EVENT_NOTEON: - id = string.Format("NOTE-C{0}N{1}",evS.data_ev_note.channel, evS.data_ev_note.note ); + id = string.Format ("NOTE-C{0}N{1}", evS.data_ev_note.channel, evS.data_ev_note.note); channel = evS.data_ev_note.channel; - value = 255 ; + value = 255; break; case AlsaSeqLib.snd_seq_event_type_t.SND_SEQ_EVENT_NOTEOFF: - id = string.Format("NOTE-C{0}N{1}",evS.data_ev_note.channel, evS.data_ev_note.note ); + id = string.Format ("NOTE-C{0}N{1}", evS.data_ev_note.channel, evS.data_ev_note.note); channel = evS.data_ev_note.channel; value = 0; break; case AlsaSeqLib.snd_seq_event_type_t.SND_SEQ_EVENT_PITCHBEND: - id = string.Format("PB-C{0}",evS.data_ev_ctrl.channel); + id = string.Format ("PB-C{0}", evS.data_ev_ctrl.channel); channel = evS.data_ev_ctrl.channel; - value = ((evS.data_ev_ctrl.value + 8000) *255 / 16000); - if(value<0) value =0; if(value>255) value = 255; + value = ((evS.data_ev_ctrl.value + 7000) * 255 / 14000); + if (value < 0) + value = 0; + if (value > 255) + value = 255; break; case AlsaSeqLib.snd_seq_event_type_t.SND_SEQ_EVENT_CLOCK: case AlsaSeqLib.snd_seq_event_type_t.SND_SEQ_EVENT_SENSING: @@ -531,16 +549,16 @@ namespace DMX2 CurrentPage = (uint)evS.data_ev_ctrl.value; continue; case AlsaSeqLib.snd_seq_event_type_t.SND_SEQ_EVENT_PORT_START: - PortDetected( - AlsaSeqLib.GetClientByID(evS.data_addr.client), - AlsaSeqLib.GetPortByIDs(evS.data_addr.client,evS.data_addr.port) - ); + PortDetected ( + AlsaSeqLib.GetClientByID (evS.data_addr.client), + AlsaSeqLib.GetPortByIDs (evS.data_addr.client, evS.data_addr.port) + ); continue; //TODO : Regarder si d'autres controles interessants. default: - id= null; + id = null; #if DEBUG Console.WriteLine(string.Format ("event {0}", evS.type) ); Info.Publish(string.Format ("event {0}", evS.type) ); // On affiche les evenements inconnus @@ -548,49 +566,49 @@ namespace DMX2 continue; } - connected=true; + connected = true; - if(id!=null) - { + if (id != null) { // Hashcode de l'ev Midi, non pagine - int evHC = id.GetHashCode(); - int srcid = evS.source.client <<8 + evS.source.port; - int lnvk = CombineHash( srcid , evHC); + int evHC = id.GetHashCode (); + int srcid = evS.source.client << 8 + evS.source.port; + int lnvk = CombineHash (srcid, evHC); - if(channel == 255 || unpaginatedchannels.Contains(channel)) - evpage= 0; + if (channel == 255 || unpaginatedchannels.Contains (channel)) + evpage = 0; else - evpage= page; + evpage = page; // Construction de l'ID evenement - id = string.Format("MIDI-PAGE{0}-{1}",evpage,id); + id = string.Format ("MIDI-PAGE{0}-{1}", evpage, id); // Creation de l'objet interne si innexistant - if(!eventlist.ContainsKey(id)) - eventlist.Add(id,new internalEvent(id,page,evHC)); - levent= eventlist[id]; //Dernier Evenement recu conserve pour menu + if (!eventlist.ContainsKey (id)) + eventlist.Add (id, new internalEvent (id, page, evHC)); + levent = eventlist [id]; //Dernier Evenement recu conserve pour menu - if(!lastValueOfSrc.ContainsKey(lnvk)) - lastValueOfSrc[lnvk]= (byte)value; + if (!lastValueOfSrc.ContainsKey (lnvk)) + lastValueOfSrc [lnvk] = (byte)value; - EventData evData = new EventData(); + EventData evData = new EventData (); evData.id = id; evData.value = (byte)value; - evData.prev_value = lastValueOfSrc[lnvk]; - if(evData.Equals(last)) continue; + evData.prev_value = lastValueOfSrc [lnvk]; + if (evData.Equals (last)) + continue; last = evData; - if(eventlist[id].Bound) { - callback(evData); + if (eventlist [id].Bound) { + callback (evData); } - lastValueOfSrc[lnvk] = (byte)value; + lastValueOfSrc [lnvk] = (byte)value; - eventlist[id].StoredEvent = evS; - eventlist[id].LastKnownValue = (byte)value; + eventlist [id].StoredEvent = evS; + eventlist [id].LastKnownValue = (byte)value; } } @@ -604,59 +622,59 @@ namespace DMX2 //TODO gerer pages et feeddback - static System.Text.RegularExpressions.Regex regexCtrlEventID = new System.Text.RegularExpressions.Regex( + static System.Text.RegularExpressions.Regex regexCtrlEventID = new System.Text.RegularExpressions.Regex ( @"MIDI-PAGE(?\d+)-CTRL-C(?\d+)P(?\d+)", System.Text.RegularExpressions.RegexOptions.Compiled); - - static System.Text.RegularExpressions.Regex regexPbEventID = new System.Text.RegularExpressions.Regex( + static System.Text.RegularExpressions.Regex regexPbEventID = new System.Text.RegularExpressions.Regex ( @"MIDI-PAGE(?\d+)-PB-C(?\d+)", System.Text.RegularExpressions.RegexOptions.Compiled); - - static System.Text.RegularExpressions.Regex regexNoteEventID = new System.Text.RegularExpressions.Regex( + static System.Text.RegularExpressions.Regex regexNoteEventID = new System.Text.RegularExpressions.Regex ( @"MIDI-PAGE(?\d+)-NOTE-C(?\d+)N(?\d+)", System.Text.RegularExpressions.RegexOptions.Compiled); string GetDescription (string eventId) { - if(!eventlist.ContainsKey(eventId)) return null; + if (!eventlist.ContainsKey (eventId)) + return null; var res = regexCtrlEventID.Match (eventId); if (res.Success) { - uint page = uint.Parse(res.Groups ["page"].Value); + uint page = uint.Parse (res.Groups ["page"].Value); byte chan = byte.Parse (res.Groups ["chan"].Value); uint param = uint.Parse (res.Groups ["param"].Value); - return string.Format("Page {2} => Control-Change C({0}) Param-{1}",chan+1,param,page); + return string.Format ("Page {2} => Control-Change C({0}) Param-{1}", chan + 1, param, page); } res = regexPbEventID.Match (eventId); if (res.Success) { - uint page = uint.Parse(res.Groups ["page"].Value); + uint page = uint.Parse (res.Groups ["page"].Value); byte chan = byte.Parse (res.Groups ["chan"].Value); - return string.Format("Page {1} => PitchBend C({0})",chan+1,page); + return string.Format ("Page {1} => PitchBend C({0})", chan + 1, page); } res = regexNoteEventID.Match (eventId); if (res.Success) { - uint page = uint.Parse(res.Groups ["page"].Value); + uint page = uint.Parse (res.Groups ["page"].Value); byte chan = byte.Parse (res.Groups ["chan"].Value); byte note = byte.Parse (res.Groups ["note"].Value); - return string.Format("Page {2} => Note C({0}) Note-{1}",chan+1,note,page); + return string.Format ("Page {2} => Note C({0}) Note-{1}", chan + 1, note, page); } return eventId; } IFeedbackInfo IEventProvider.GetFeedbackInfo (string eventId) { - if(!eventlist.ContainsKey(eventId)) return null; + if (!eventlist.ContainsKey (eventId)) + return null; var res = regexCtrlEventID.Match (eventId); if (res.Success) { byte chan = byte.Parse (res.Groups ["chan"].Value); uint param = uint.Parse (res.Groups ["param"].Value); - return new ctrlfeedbackinfo (this, eventlist[eventId],chan, param); + return new ctrlfeedbackinfo (this, eventlist [eventId], chan, param); } res = regexPbEventID.Match (eventId); if (res.Success) { byte chan = byte.Parse (res.Groups ["chan"].Value); - return new pitchbendfeedbackinfo (this, eventlist[eventId], chan); + return new pitchbendfeedbackinfo (this, eventlist [eventId], chan); } return null; @@ -667,16 +685,19 @@ namespace DMX2 #region IDisposable implementation - bool disposed=false; + bool disposed = false; - ~MidiEventProvider(){ - Dispose(); + ~MidiEventProvider () + { + Dispose (); } + public void Dispose () { - if(disposed)return; - disposed=true; - AlsaSeqLib.Close(); + if (disposed) + return; + disposed = true; + AlsaSeqLib.Close (); } #endregion @@ -695,14 +716,15 @@ namespace DMX2 foreach (byte ch in unpaginatedchannels) { el.AppendChild (xmlEl = parent.OwnerDocument.CreateElement ("UPC")); - xmlEl.SetAttribute ("ch", ch.ToString()); + xmlEl.SetAttribute ("ch", ch.ToString ()); } } public void Load (System.Xml.XmlElement el) { - if(el==null) return; + if (el == null) + return; maxpage = uint.Parse (el.TryGetAttribute ("maxpage", "8")); foreach (var xd in el.GetElementsByTagName("MidiDev")) { @@ -712,13 +734,13 @@ namespace DMX2 knowndevices.Add (name, new MidiDev (name)); knowndevices [name].HasFeedback = bool.Parse (xdev.TryGetAttribute ("feedback", "false")); } - unpaginatedchannels.Clear(); + unpaginatedchannels.Clear (); foreach (var xu in el.GetElementsByTagName("UPC")) { System.Xml.XmlElement xupc = xu as System.Xml.XmlElement; - unpaginatedchannels.Add(byte.Parse(xupc.GetAttribute("ch"))); + unpaginatedchannels.Add (byte.Parse (xupc.GetAttribute ("ch"))); } - AutoConnect(); + AutoConnect (); } }