using LibPDBinding.Managed.Events; using System; using System.Runtime.CompilerServices; namespace LibPDBinding.Managed { /// /// MIDI in Pd. /// public sealed class Midi : IDisposable { readonly Pd _pd; internal Midi (Pd pd) { _pd = pd; SetupHooks (); } ~Midi () { Dispose (false); } public void Dispose () { Dispose (true); GC.SuppressFinalize (this); } void Dispose (bool disposing) { NoteOn = null; ProgramChange = null; ControlChange = null; Pitchbend = null; Aftertouch = null; PolyAftertouch = null; MidiByte = null; } /// /// Occurs when MIDI note on message is sent from Pd. /// public event EventHandler NoteOn; /// /// Occurs when MIDI program change is sent from Pd. /// public event EventHandler ProgramChange; /// /// Occurs when MIDI control change is sent from Pd. /// public event EventHandler ControlChange; /// /// Occurs when MIDI pitchbend is sent from Pd. /// public event EventHandler Pitchbend; /// /// Occurs when MIDI aftertouch is sent from Pd. /// public event EventHandler Aftertouch; /// /// Occurs when MIDI poly aftertouch is sent from Pd. /// public event EventHandler PolyAftertouch; /// /// Occurs when raw MIDI byte is sent from Pd. /// public event EventHandler MidiByte; LibPDNoteOnHook NoteOnHook; LibPDProgramChangeHook ProgramChangeHook; LibPDControlChangeHook ControlChangeHook; LibPDPitchbendHook PitchbendHook; LibPDAftertouchHook AftertouchHook; LibPDPolyAftertouchHook PolyAftertouchHook; LibPDMidiByteHook MidiByteHook; /// /// Sends note on MIDI message. /// /// Channel. /// Pitch. /// Velocity. [MethodImpl (MethodImplOptions.Synchronized)] public void SendNoteOn (int channel, int pitch, int velocity) { _pd.Activate (); Native.Midi.noteon (channel, pitch, velocity); } /// /// Sends program change MIDI message. /// /// Channel. /// Value. [MethodImpl (MethodImplOptions.Synchronized)] public void SendProgramChange (int channel, int value) { _pd.Activate (); Native.Midi.programchange (channel, value); } /// /// Sends control change MIDI message. /// /// Channel. /// Controller. /// Value. [MethodImpl (MethodImplOptions.Synchronized)] public void SendControlChange (int channel, int controller, int value) { _pd.Activate (); Native.Midi.controlchange (channel, controller, value); } /// /// Sends pitchbend MIDI message. /// /// Channel. /// Value. [MethodImpl (MethodImplOptions.Synchronized)] public void SendPitchbend (int channel, int value) { _pd.Activate (); Native.Midi.pitchbend (channel, value); } /// /// Sends aftertouch MIDI message. /// /// Channel. /// Value. [MethodImpl (MethodImplOptions.Synchronized)] public void SendAftertouch (int channel, int value) { _pd.Activate (); Native.Midi.aftertouch (channel, value); } /// /// Sends poly aftertouch MIDI message. /// /// Channel. /// Pitch. /// Value. [MethodImpl (MethodImplOptions.Synchronized)] public void SendPolyAftertouch (int channel, int pitch, int value) { _pd.Activate (); Native.Midi.polyaftertouch (channel, pitch, value); } /// /// Sends raw midi byte. /// /// Port. /// Byte. [MethodImpl (MethodImplOptions.Synchronized)] public void SendMidiByte (int port, int value) { _pd.Activate (); Native.Midi.midibyte (port, value); } /// /// Sends MIDI system exclusive byte. /// /// Port. /// Byte. [MethodImpl (MethodImplOptions.Synchronized)] public void SendSysex (int port, int value) { _pd.Activate (); Native.Midi.sysex (port, value); } /// /// Sends MIDI system realtime byte. /// /// Port. /// Value. [MethodImpl (MethodImplOptions.Synchronized)] public void SendSysRealtime (int port, int value) { _pd.Activate (); Native.Midi.sysrealtime (port, value); } void RaiseNoteOnEvent (int channel, int pitch, int velocity) { if (NoteOn != null) { NoteOn (this, new NoteOnEventArgs (channel, pitch, velocity)); } } void RaiseProgramChangeEvent (int channel, int value) { if (ProgramChange != null) { ProgramChange (this, new ProgramChangeEventArgs (channel, value)); } } void RaiseControlChangeEvent (int channel, int controller, int value) { if (ControlChange != null) { ControlChange (this, new ControlChangeEventArgs (channel, controller, value)); } } void RaisePitchbendEvent (int channel, int value) { if (Pitchbend != null) { Pitchbend (this, new PitchbendEventArgs (channel, value)); } } void RaiseAftertouchEvent (int channel, int value) { if (Aftertouch != null) { Aftertouch (this, new AftertouchEventArgs (channel, value)); } } void RaisePolyAftertouchEvent (int channel, int pitch, int value) { if (PolyAftertouch != null) { PolyAftertouch (this, new PolyAftertouchEventArgs (channel, pitch, value)); } } void RaiseMidiByteEvent (int port, int midiByte) { if (MidiByte != null) { MidiByte (this, new MidiByteEventArgs (port, midiByte)); } } void SetupHooks () { NoteOnHook = new LibPDNoteOnHook (RaiseNoteOnEvent); Native.Midi.set_noteonhook (NoteOnHook); ProgramChangeHook = new LibPDProgramChangeHook (RaiseProgramChangeEvent); Native.Midi.set_programchangehook (ProgramChangeHook); ControlChangeHook = new LibPDControlChangeHook (RaiseControlChangeEvent); Native.Midi.set_controlchangehook (ControlChangeHook); PitchbendHook = new LibPDPitchbendHook (RaisePitchbendEvent); Native.Midi.set_pitchbendhook (PitchbendHook); AftertouchHook = new LibPDAftertouchHook (RaiseAftertouchEvent); Native.Midi.set_aftertouchhook (AftertouchHook); PolyAftertouchHook = new LibPDPolyAftertouchHook (RaisePolyAftertouchEvent); Native.Midi.set_polyaftertouchhook (PolyAftertouchHook); MidiByteHook = new LibPDMidiByteHook (RaiseMidiByteEvent); Native.Midi.set_midibytehook (MidiByteHook); } } }