push2: working bidirectional communication, some transport control/display (LED only...
[ardour.git] / libs / surfaces / push2 / push2.cc
index be25357e267cca82d8f79b2ed110462836e894b4..9b6d84dbe3a5510c1bc5641597a7f61956cb0fef 100644 (file)
 #include "pbd/debug.h"
 #include "pbd/failed_constructor.h"
 
-#include "ardour/debug.h"
+#include "midi++/parser.h"
 
+#include "ardour/async_midi_port.h"
+#include "ardour/audioengine.h"
+#include "ardour/debug.h"
+#include "ardour/midiport_manager.h"
+#include "ardour/session.h"
 #include "push2.h"
 
 using namespace ARDOUR;
@@ -45,13 +50,14 @@ const int Push2::pixels_per_row = 1024;
 #define ABLETON 0x2982
 #define PUSH2   0x1967
 
-Push2::Push2 (Session& s)
-       : ControlProtocol (s, string (X_("Ableton Push2")))
+Push2::Push2 (ARDOUR::Session& s)
+       : ControlProtocol (s, string (X_("Ableton Push 2")))
        , AbstractUI<Push2Request> (name())
        , handle (0)
        , device_buffer (0)
        , frame_buffer (Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, cols, rows))
 {
+       build_maps ();
 }
 
 Push2::~Push2 ()
@@ -62,11 +68,15 @@ Push2::~Push2 ()
 int
 Push2::open ()
 {
+       int err;
+
        if ((handle = libusb_open_device_with_vid_pid (NULL, ABLETON, PUSH2)) == 0) {
                return -1;
        }
 
-       libusb_claim_interface (handle, 0x00);
+       if ((err = libusb_claim_interface (handle, 0x00))) {
+               return -1;
+       }
 
        device_frame_buffer[0] = new uint16_t[rows*pixels_per_row];
        device_frame_buffer[1] = new uint16_t[rows*pixels_per_row];
@@ -80,13 +90,59 @@ Push2::open ()
        frame_header[3] = 0x89;
        memset (&frame_header[4], 0, 12);
 
+       /* setup ports */
+
+       _async_in[0]  = AudioEngine::instance()->register_input_port (DataType::MIDI, X_("push2 in1"), true);
+       _async_out[0] = AudioEngine::instance()->register_output_port (DataType::MIDI, X_("push2 out1"), true);
+
+       if (_async_in[0] == 0 || _async_out[0] == 0) {
+               return -1;
+       }
+
+       _input_port[0] = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_in[0]).get();
+       _output_port[0] = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_out[0]).get();
+
+       _async_in[1]  = AudioEngine::instance()->register_input_port (DataType::MIDI, X_("push2 in2"), true);
+       _async_out[1] = AudioEngine::instance()->register_output_port (DataType::MIDI, X_("push2 out2"), true);
+
+       if (_async_in[1] == 0 || _async_out[1] == 0) {
+               return -1;
+       }
+
+       _input_port[1] = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_in[1]).get();
+       _output_port[1] = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_out[1]).get();
+
+       AsyncMIDIPort* asp;
+
+       asp = dynamic_cast<AsyncMIDIPort*> (_input_port[0]);
+       asp->xthread().set_receive_handler (sigc::bind (sigc::mem_fun (this, &Push2::midi_input_handler), _input_port[0]));
+       asp->xthread().attach (main_loop()->get_context());
+
+       asp = dynamic_cast<AsyncMIDIPort*> (_input_port[1]);
+       asp->xthread().set_receive_handler (sigc::bind (sigc::mem_fun (this, &Push2::midi_input_handler), _input_port[1]));
+       asp->xthread().attach (main_loop()->get_context());
+
+       connect_to_parser ();
+
        return 0;
 }
 
 int
 Push2::close ()
 {
+       AudioEngine::instance()->unregister_port (_async_in[0]);
+       AudioEngine::instance()->unregister_port (_async_out[0]);
+       AudioEngine::instance()->unregister_port (_async_in[1]);
+       AudioEngine::instance()->unregister_port (_async_out[1]);
+
+       _async_in[0].reset ((ARDOUR::Port*) 0);
+       _async_out[0].reset ((ARDOUR::Port*) 0);
+       _async_in[1].reset ((ARDOUR::Port*) 0);
+       _async_out[1].reset ((ARDOUR::Port*) 0);
+
        vblank_connection.disconnect ();
+       periodic_connection.disconnect ();
+       session_connections.drop_connections ();
 
        if (handle) {
                libusb_release_interface (handle, 0x00);
@@ -146,9 +202,8 @@ Push2::do_request (Push2Request * req)
 int
 Push2::stop ()
 {
-       close ();
        BaseUI::quit ();
-
+       close ();
        return 0;
 }
 
@@ -239,17 +294,17 @@ Push2::set_active (bool yn)
 
        if (yn) {
 
+               /* start event loop */
+
+               BaseUI::run ();
+
                if (open ()) {
                        DEBUG_TRACE (DEBUG::Push2, "device open failed\n");
                        close ();
                        return -1;
                }
 
-               /* start event loop */
-
-               BaseUI::run ();
-
-               // connect_session_signals ();
+               connect_session_signals ();
 
                /* say hello */
 
@@ -290,10 +345,14 @@ Push2::set_active (bool yn)
                vblank_connection = vblank_timeout->connect (sigc::mem_fun (*this, &Push2::vblank));
                vblank_timeout->attach (main_loop()->get_context());
 
+
+               Glib::RefPtr<Glib::TimeoutSource> periodic_timeout = Glib::TimeoutSource::create (1000); // milliseconds
+               periodic_connection = periodic_timeout->connect (sigc::mem_fun (*this, &Push2::periodic));
+               periodic_timeout->attach (main_loop()->get_context());
+
        } else {
 
-               BaseUI::quit ();
-               close ();
+               stop ();
 
        }
 
@@ -303,3 +362,392 @@ Push2::set_active (bool yn)
 
        return 0;
 }
+
+void
+Push2::write (int port, const MidiByteArray& data)
+{
+       /* immediate delivery */
+       cerr << data << endl;
+       _output_port[port]->write (&data[0], data.size(), 0);
+}
+
+bool
+Push2::midi_input_handler (IOCondition ioc, MIDI::Port* port)
+{
+       if (ioc & ~IO_IN) {
+               DEBUG_TRACE (DEBUG::Push2, "MIDI port closed\n");
+               return false;
+       }
+
+       if (ioc & IO_IN) {
+
+               // DEBUG_TRACE (DEBUG::Push2, string_compose ("something happend on  %1\n", port->name()));
+
+               AsyncMIDIPort* asp = dynamic_cast<AsyncMIDIPort*>(port);
+               if (asp) {
+                       asp->clear ();
+               }
+
+               //DEBUG_TRACE (DEBUG::Push2, string_compose ("data available on %1\n", port->name()));
+               framepos_t now = AudioEngine::instance()->sample_time();
+               port->parse (now);
+       }
+
+       return true;
+}
+
+bool
+Push2::periodic ()
+{
+       return true;
+}
+
+void
+Push2::connect_to_parser ()
+{
+       DEBUG_TRACE (DEBUG::Push2, string_compose ("Connecting to signals on port %2\n", _input_port[0]->name()));
+
+       MIDI::Parser* p = _input_port[0]->parser();
+
+       /* Incoming sysex */
+       p->sysex.connect_same_thread (*this, boost::bind (&Push2::handle_midi_sysex, this, _1, _2, _3));
+       /* V-Pot messages are Controller */
+       p->controller.connect_same_thread (*this, boost::bind (&Push2::handle_midi_controller_message, this, _1, _2));
+       /* Button messages are NoteOn */
+       p->note_on.connect_same_thread (*this, boost::bind (&Push2::handle_midi_note_on_message, this, _1, _2));
+       /* Button messages are NoteOn but libmidi++ sends note-on w/velocity = 0 as note-off so catch them too */
+       p->note_off.connect_same_thread (*this, boost::bind (&Push2::handle_midi_note_on_message, this, _1, _2));
+       /* Fader messages are Pitchbend */
+       p->channel_pitchbend[0].connect_same_thread (*this, boost::bind (&Push2::handle_midi_pitchbend_message, this, _1, _2));
+}
+
+void
+Push2::handle_midi_sysex (MIDI::Parser&, MIDI::byte* raw_bytes, size_t sz)
+{
+       cerr << "sysex, " << sz << " bytes\n";
+}
+
+void
+Push2::handle_midi_controller_message (MIDI::Parser&, MIDI::EventTwoBytes* ev)
+{
+       cerr << "controller " << (int) ev->controller_number << " = " << (int) ev->value << endl;
+       CCButtonMap::iterator b = cc_button_map.find (ev->controller_number);
+       if (b != cc_button_map.end()) {
+               if (ev->value == 0) {
+                       (this->*b->second->release_method)();
+               } else {
+                       (this->*b->second->press_method)();
+               }
+       }
+}
+
+void
+Push2::handle_midi_note_on_message (MIDI::Parser&, MIDI::EventTwoBytes* ev)
+{
+       cerr << "note on" << (int) ev->note_number << ", velocity " << (int) ev->velocity << endl;
+}
+
+void
+Push2::handle_midi_note_off_message (MIDI::Parser&, MIDI::EventTwoBytes* ev)
+{
+       cerr << "note on" << (int) ev->note_number << ", velocity " << (int) ev->velocity << endl;
+}
+
+void
+Push2::handle_midi_pitchbend_message (MIDI::Parser&, MIDI::pitchbend_t pb)
+{
+       cerr << "pitchbend @ " << pb << endl;
+}
+
+void
+Push2::build_maps ()
+{
+       /* Pads */
+
+       Pad* pad;
+
+#define MAKE_PAD(x,y,nn) \
+       pad = new Pad ((x), (y), (nn)); \
+       nn_pad_map.insert (make_pair (pad->extra(), pad)); \
+       coord_pad_map.insert (make_pair (pad->coord(), pad));
+
+       MAKE_PAD (0, 1, 93);
+       MAKE_PAD (0, 2, 94);
+       MAKE_PAD (0, 3, 95);
+       MAKE_PAD (0, 4, 96);
+       MAKE_PAD (0, 5, 97);
+       MAKE_PAD (0, 6, 98);
+       MAKE_PAD (0, 7, 90);
+       MAKE_PAD (1, 0, 84);
+       MAKE_PAD (1, 1, 85);
+       MAKE_PAD (1, 2, 86);
+       MAKE_PAD (1, 3, 87);
+       MAKE_PAD (1, 4, 88);
+       MAKE_PAD (1, 5, 89);
+       MAKE_PAD (1, 6, 90);
+       MAKE_PAD (1, 7, 91);
+       MAKE_PAD (2, 0, 76);
+       MAKE_PAD (2, 1, 77);
+       MAKE_PAD (2, 2, 78);
+       MAKE_PAD (2, 3, 79);
+       MAKE_PAD (2, 4, 80);
+       MAKE_PAD (2, 5, 81);
+       MAKE_PAD (2, 6, 82);
+       MAKE_PAD (2, 7, 83);
+       MAKE_PAD (3, 0, 68);
+       MAKE_PAD (3, 1, 69);
+       MAKE_PAD (3, 2, 70);
+       MAKE_PAD (3, 3, 71);
+       MAKE_PAD (3, 4, 72);
+       MAKE_PAD (3, 5, 73);
+       MAKE_PAD (3, 6, 74);
+       MAKE_PAD (3, 7, 75);
+       MAKE_PAD (4, 0, 60);
+       MAKE_PAD (4, 1, 61);
+       MAKE_PAD (4, 2, 62);
+       MAKE_PAD (4, 3, 63);
+       MAKE_PAD (4, 4, 64);
+       MAKE_PAD (4, 5, 65);
+       MAKE_PAD (4, 6, 66);
+       MAKE_PAD (4, 7, 67);
+       MAKE_PAD (5, 0, 52);
+       MAKE_PAD (5, 1, 53);
+       MAKE_PAD (5, 2, 54);
+       MAKE_PAD (5, 3, 56);
+       MAKE_PAD (5, 4, 56);
+       MAKE_PAD (5, 5, 57);
+       MAKE_PAD (5, 6, 58);
+       MAKE_PAD (5, 7, 59);
+       MAKE_PAD (6, 0, 44);
+       MAKE_PAD (6, 1, 45);
+       MAKE_PAD (6, 2, 46);
+       MAKE_PAD (6, 3, 47);
+       MAKE_PAD (6, 4, 48);
+       MAKE_PAD (6, 5, 49);
+       MAKE_PAD (6, 6, 50);
+       MAKE_PAD (6, 7, 51);
+       MAKE_PAD (7, 0, 36);
+       MAKE_PAD (7, 1, 37);
+       MAKE_PAD (7, 2, 38);
+       MAKE_PAD (7, 3, 39);
+       MAKE_PAD (7, 4, 40);
+       MAKE_PAD (7, 5, 41);
+       MAKE_PAD (7, 6, 42);
+       MAKE_PAD (7, 7, 43);
+
+       /* Now color buttons */
+
+       Button *button;
+
+#define MAKE_COLOR_BUTTON(i,cc) \
+       button = new ColorButton ((i), (cc)); \
+       cc_button_map.insert (make_pair (button->controller_number(), button)); \
+       id_button_map.insert (make_pair (button->id, button));
+#define MAKE_COLOR_BUTTON_PRESS(i,cc,p)\
+       button = new ColorButton ((i), (cc), (p)); \
+       cc_button_map.insert (make_pair (button->controller_number(), button)); \
+       id_button_map.insert (make_pair (button->id, button))
+
+       MAKE_COLOR_BUTTON (Upper1, 102);
+       MAKE_COLOR_BUTTON (Upper2, 103);
+       MAKE_COLOR_BUTTON (Upper3, 104);
+       MAKE_COLOR_BUTTON (Upper4, 105);
+       MAKE_COLOR_BUTTON (Upper5, 106);
+       MAKE_COLOR_BUTTON (Upper6, 107);
+       MAKE_COLOR_BUTTON (Upper7, 108);
+       MAKE_COLOR_BUTTON (Upper8, 109);
+       MAKE_COLOR_BUTTON (Lower1, 21);
+       MAKE_COLOR_BUTTON (Lower2, 22);
+       MAKE_COLOR_BUTTON (Lower3, 23);
+       MAKE_COLOR_BUTTON (Lower4, 24);
+       MAKE_COLOR_BUTTON (Lower5, 25);
+       MAKE_COLOR_BUTTON (Lower6, 26);
+       MAKE_COLOR_BUTTON (Lower7, 27);
+       MAKE_COLOR_BUTTON (Mute, 60);
+       MAKE_COLOR_BUTTON (Solo, 61);
+       MAKE_COLOR_BUTTON (Stop, 29);
+       MAKE_COLOR_BUTTON (Fwd32ndT, 43);
+       MAKE_COLOR_BUTTON (Fwd32nd,42 );
+       MAKE_COLOR_BUTTON (Fwd16thT, 41);
+       MAKE_COLOR_BUTTON (Fwd16th, 40);
+       MAKE_COLOR_BUTTON (Fwd8thT, 39 );
+       MAKE_COLOR_BUTTON (Fwd8th, 38);
+       MAKE_COLOR_BUTTON (Fwd4trT, 37);
+       MAKE_COLOR_BUTTON (Fwd4tr, 36);
+       MAKE_COLOR_BUTTON (Automate, 89);
+       MAKE_COLOR_BUTTON_PRESS (RecordEnable, 86, &Push::button_recenable);
+       MAKE_COLOR_BUTTON_PRESS (Play, 85, &Push2::button_play);
+
+#define MAKE_WHITE_BUTTON(i,cc)\
+       button = new WhiteButton ((i), (cc)); \
+       cc_button_map.insert (make_pair (button->controller_number(), button)); \
+       id_button_map.insert (make_pair (button->id, button))
+#define MAKE_WHITE_BUTTON_PRESS(i,cc,p)\
+       button = new WhiteButton ((i), (cc), (p)); \
+       cc_button_map.insert (make_pair (button->controller_number(), button)); \
+       id_button_map.insert (make_pair (button->id, button))
+
+       MAKE_WHITE_BUTTON (TapTempo, 3);
+       MAKE_WHITE_BUTTON (Metronome, 9);
+       MAKE_WHITE_BUTTON (Setup, 30);
+       MAKE_WHITE_BUTTON (User, 59);
+       MAKE_WHITE_BUTTON (Delete, 118);
+       MAKE_WHITE_BUTTON (AddDevice, 52);
+       MAKE_WHITE_BUTTON (Device, 110);
+       MAKE_WHITE_BUTTON (Mix, 112);
+       MAKE_WHITE_BUTTON (Undo, 119);
+       MAKE_WHITE_BUTTON (AddTrack, 53);
+       MAKE_WHITE_BUTTON (Browse, 113);
+       MAKE_WHITE_BUTTON (Convert, 35);
+       MAKE_WHITE_BUTTON (DoubleLoop, 117);
+       MAKE_WHITE_BUTTON (Quantize, 116);
+       MAKE_WHITE_BUTTON (Duplicate, 88);
+       MAKE_WHITE_BUTTON (New, 87);
+       MAKE_WHITE_BUTTON (FixedLength, 90);
+       MAKE_WHITE_BUTTON_PRESS (Up, 46, &Push2::button_up);
+       MAKE_WHITE_BUTTON (Right, 45);
+       MAKE_WHITE_BUTTON_PRESS (Down, 47, &Push2::button_down);
+       MAKE_WHITE_BUTTON (Left, 44);
+       MAKE_WHITE_BUTTON (Repeat, 56);
+       MAKE_WHITE_BUTTON (Accent, 57);
+       MAKE_WHITE_BUTTON (Scale, 58);
+       MAKE_WHITE_BUTTON (Layout, 31);
+       MAKE_WHITE_BUTTON (OctaveUp, 55);
+       MAKE_WHITE_BUTTON (PageRight, 63);
+       MAKE_WHITE_BUTTON (OctaveDown, 54);
+       MAKE_WHITE_BUTTON (PageLeft, 62);
+       MAKE_WHITE_BUTTON (Shift, 49);
+       MAKE_WHITE_BUTTON (Select, 48);
+}
+
+void
+Push2::thread_init ()
+{
+       struct sched_param rtparam;
+
+       pthread_set_name (event_loop_name().c_str());
+
+       PBD::notify_event_loops_about_thread_creation (pthread_self(), event_loop_name(), 2048);
+       ARDOUR::SessionEvent::create_per_thread_pool (event_loop_name(), 128);
+
+       memset (&rtparam, 0, sizeof (rtparam));
+       rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
+
+       if (pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam) != 0) {
+               // do we care? not particularly.
+       }
+}
+
+void
+Push2::connect_session_signals()
+{
+       // receive routes added
+       //session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_routes_added, this, _1), this);
+       // receive VCAs added
+       //session->vca_manager().VCAAdded.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_vca_added, this, _1), this);
+
+       // receive record state toggled
+       session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_record_state_changed, this), this);
+       // receive transport state changed
+       session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_transport_state_changed, this), this);
+       session->TransportLooped.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_loop_state_changed, this), this);
+       // receive punch-in and punch-out
+       Config->ParameterChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_parameter_changed, this, _1), this);
+       session->config.ParameterChanged.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_parameter_changed, this, _1), this);
+       // receive rude solo changed
+       session->SoloActive.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_solo_active_changed, this, _1), this);
+}
+
+void
+Push2::notify_record_state_changed ()
+{
+       IDButtonMap::iterator b = id_button_map.find (RecordEnable);
+
+       if (b == id_button_map.end()) {
+               return;
+       }
+
+       if (session->actively_recording ()) {
+               b->second->set_state (LED::OneShot24th);
+               b->second->set_color (127);
+       } else {
+               b->second->set_state (LED::Off);
+       }
+
+       write (0, b->second->state_msg());
+}
+
+void
+Push2::notify_transport_state_changed ()
+{
+       cerr << "ts change, id button map holds " << id_button_map.size() << endl;
+
+       IDButtonMap::iterator b = id_button_map.find (Play);
+
+       if (b == id_button_map.end()) {
+               cerr << " no button\n";
+               return;
+       }
+
+       if (session->transport_rolling()) {
+               b->second->set_state (LED::OneShot24th);
+               b->second->set_color (125);
+       } else {
+               b->second->set_state (LED::Off);
+       }
+
+       write (0, b->second->state_msg());
+}
+
+void
+Push2::notify_loop_state_changed ()
+{
+}
+
+void
+Push2::notify_parameter_changed (std::string)
+{
+}
+
+void
+Push2::notify_solo_active_changed (bool yn)
+{
+       IDButtonMap::iterator b = id_button_map.find (Solo);
+
+       if (b == id_button_map.end()) {
+               return;
+       }
+
+       if (yn) {
+               b->second->set_state (LED::Blinking24th);
+       } else {
+               b->second->set_state (LED::Off);
+       }
+
+       write (0, b->second->state_msg());
+}
+
+XMLNode&
+Push2::get_state()
+{
+       XMLNode& node (ControlProtocol::get_state());
+
+       DEBUG_TRACE (DEBUG::Push2, "Push2::get_state done\n");
+
+       return node;
+}
+
+int
+Push2::set_state (const XMLNode & node, int version)
+{
+       DEBUG_TRACE (DEBUG::Push2, string_compose ("Push2::set_state: active %1\n", active()));
+
+       int retval = 0;
+
+       if (ControlProtocol::set_state (node, version)) {
+               return -1;
+       }
+
+
+       return retval;
+}