190 lines
5.8 KiB
C#
190 lines
5.8 KiB
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace DMX2
|
|
{
|
|
public partial class MidiEventProvider : IEventProvider
|
|
{
|
|
|
|
/// <summary>
|
|
/// Classe pour contenir et gérer un unique Handle d'acces au sequenceur midi Alsa.
|
|
/// </summary>
|
|
public class MidiSeqHandle : IDisposable
|
|
{
|
|
static MidiSeqHandle singleton = new MidiSeqHandle();
|
|
IntPtr midi_seq_handle = IntPtr.Zero;
|
|
|
|
public static IntPtr Handle {
|
|
get {
|
|
return singleton.midi_seq_handle;
|
|
}
|
|
}
|
|
|
|
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",
|
|
SND_SEQ_PORT_CAP_WRITE + SND_SEQ_PORT_CAP_SUBS_WRITE,
|
|
SND_SEQ_PORT_TYPE_APPLICATION);
|
|
}
|
|
|
|
#region IDisposable implementation
|
|
void IDisposable.Dispose ()
|
|
{
|
|
if(midi_seq_handle != IntPtr.Zero)
|
|
snd_seq_close(midi_seq_handle);
|
|
midi_seq_handle = IntPtr.Zero;
|
|
}
|
|
#endregion
|
|
~MidiSeqHandle(){
|
|
((IDisposable)this).Dispose ();
|
|
}
|
|
|
|
}
|
|
|
|
class internalEvent {
|
|
public string internalName;
|
|
public string description;
|
|
public internalEvent(string _id, string _desc)
|
|
{
|
|
internalName=_id;
|
|
description=_desc;
|
|
}
|
|
}
|
|
|
|
Dictionary<string,internalEvent> eventlist = new Dictionary<string, internalEvent>();
|
|
EventData last;
|
|
internalEvent levent=null;
|
|
bool connected=false;
|
|
|
|
public MidiEventProvider (EventManager manager)
|
|
{
|
|
manager.RegisterProvider(this);
|
|
}
|
|
|
|
#region IEventProvider implementation
|
|
|
|
bool IEventProvider.Bind (string eventId)
|
|
{
|
|
// On indique a l'EventManager qu'on traite, si l'ID commence par 'MIDI-'
|
|
return eventId.StartsWith("MIDI-");
|
|
}
|
|
|
|
void IEventProvider.Unbind (string eventId)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Gtk.Menu IEventProvider.GetProviderSubMenu (EventManager.EventMenuData state, Gtk.ButtonPressEventHandler handler)
|
|
{
|
|
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"
|
|
Gtk.MenuItem lmenuitem = new Gtk.MenuItem ("Dernier");
|
|
retmenu.Add (lmenuitem);
|
|
Gtk.Menu lmenu = new Gtk.Menu ();
|
|
lmenuitem.Submenu = lmenu;
|
|
Gtk.MenuItem item = new Gtk.MenuItem(levent.description);
|
|
item.Data[EventManager.EventIdKey] = levent.internalName;
|
|
item.Data[EventManager.StateKey] = state;
|
|
item.ButtonPressEvent += handler;
|
|
lmenu.Add (item);
|
|
}
|
|
|
|
Gtk.MenuItem evmenuitem = new Gtk.MenuItem ("Events"); // Creation du sous menu "Events"
|
|
retmenu.Add (evmenuitem);
|
|
Gtk.Menu evmenu = new Gtk.Menu ();
|
|
evmenuitem.Submenu = evmenu;
|
|
|
|
List<string> 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(evt.description);
|
|
item.Data[EventManager.EventIdKey] = evt.internalName;
|
|
item.Data[EventManager.StateKey] = state;
|
|
item.ButtonPressEvent += handler;
|
|
evmenu.Add (item);
|
|
}
|
|
|
|
return retmenu;
|
|
|
|
}
|
|
|
|
void IEventProvider.ProcessEvents (EventManagerCallback callback)
|
|
{
|
|
IntPtr evPtr; // Pointeur vers la structure non managée alsa-seq
|
|
|
|
// Tant qu'il y des evenements midi en attente
|
|
while ((snd_seq_event_input_pending(MidiSeqHandle.Handle,1))>0) {
|
|
snd_seq_event_input(MidiSeqHandle.Handle, out evPtr); // Recup du pointeur vers l'ev
|
|
snd_seq_event_t evS =(snd_seq_event_t) Marshal.PtrToStructure(evPtr,typeof(snd_seq_event_t)); // Copie de la zone mémoire dans une structure managee
|
|
snd_seq_free_event(evPtr); // liberation du pointeur
|
|
|
|
string id=null, description=null; int value=0;
|
|
|
|
switch (evS.type) {
|
|
case snd_seq_event_type_t.SND_SEQ_EVENT_PORT_SUBSCRIBED: // Connection d'un périph midi
|
|
connected = true;
|
|
continue;
|
|
case snd_seq_event_type_t.SND_SEQ_EVENT_CONTROLLER:
|
|
id= string.Format("MIDI-CTRL-C{0}P{1}",evS.data_ev_ctrl.channel,evS.data_ev_ctrl.param);
|
|
description = string.Format("Controller Ch {0} Param {1}",evS.data_ev_ctrl.channel,evS.data_ev_ctrl.param);
|
|
value = 255 * evS.data_ev_ctrl.value / 127; // Conversion {0,127} => {0,255}
|
|
break;
|
|
case snd_seq_event_type_t.SND_SEQ_EVENT_NOTEON:
|
|
id= string.Format("MIDI-NOTE-C{0}N{1}",evS.data_ev_note.channel, evS.data_ev_note.note );
|
|
description = string.Format("Note {1} Ch {0}",evS.data_ev_note.channel, evS.data_ev_note.note );
|
|
value = 255 ;
|
|
break;
|
|
case snd_seq_event_type_t.SND_SEQ_EVENT_NOTEOFF:
|
|
id= string.Format("MIDI-NOTE-C{0}N{1}",evS.data_ev_note.channel, evS.data_ev_note.note );
|
|
description = string.Format("Note {1} Ch {0}",evS.data_ev_note.channel, evS.data_ev_note.note );
|
|
value = 0;
|
|
break;
|
|
case snd_seq_event_type_t.SND_SEQ_EVENT_CLOCK:
|
|
case snd_seq_event_type_t.SND_SEQ_EVENT_SENSING:
|
|
continue;
|
|
|
|
//TODO : Regarder si d'autres controles interessants.
|
|
default:
|
|
id= null;
|
|
#if DEBUG
|
|
Info.Publish(string.Format ("event {0}", evS.type) ); // On affiche les evenements inconnus
|
|
#endif
|
|
continue;
|
|
}
|
|
|
|
connected=true;
|
|
|
|
if(id!=null)
|
|
{
|
|
if(!eventlist.ContainsKey(id))
|
|
eventlist.Add(id,new internalEvent(id,description));
|
|
levent= eventlist[id]; //Dernier Evenement recu conserve pour menu
|
|
EventData evData = new EventData();
|
|
evData.id = id;
|
|
evData.value = (byte)value;
|
|
|
|
if(evData.Equals(last)) continue; // On ignore les evenements répétés à l'identique
|
|
|
|
callback(evData);
|
|
last = evData;
|
|
}
|
|
}
|
|
}
|
|
|
|
string IEventProvider.MenuName {
|
|
get {
|
|
return "Midi";
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
}
|
|
}
|
|
|