2 Copyright (C) 2016 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <cairomm/context.h>
20 #include <cairomm/surface.h>
21 #include <pangomm/layout.h>
23 #include "pbd/compose.h"
24 #include "pbd/debug.h"
25 #include "pbd/failed_constructor.h"
27 #include "midi++/parser.h"
29 #include "ardour/async_midi_port.h"
30 #include "ardour/audioengine.h"
31 #include "ardour/debug.h"
32 #include "ardour/midiport_manager.h"
33 #include "ardour/session.h"
36 using namespace ARDOUR;
40 using namespace ArdourSurface;
44 #include "pbd/abstract_ui.cc" // instantiate template
46 const int Push2::cols = 960;
47 const int Push2::rows = 160;
48 const int Push2::pixels_per_row = 1024;
50 #define ABLETON 0x2982
53 Push2::Push2 (ARDOUR::Session& s)
54 : ControlProtocol (s, string (X_("Ableton Push 2")))
55 , AbstractUI<Push2Request> (name())
58 , frame_buffer (Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, cols, rows))
73 if ((handle = libusb_open_device_with_vid_pid (NULL, ABLETON, PUSH2)) == 0) {
77 if ((err = libusb_claim_interface (handle, 0x00))) {
81 device_frame_buffer[0] = new uint16_t[rows*pixels_per_row];
82 device_frame_buffer[1] = new uint16_t[rows*pixels_per_row];
84 memset (device_frame_buffer[0], 0, sizeof (uint16_t) * rows * pixels_per_row);
85 memset (device_frame_buffer[1], 0, sizeof (uint16_t) * rows * pixels_per_row);
87 frame_header[0] = 0xef;
88 frame_header[1] = 0xcd;
89 frame_header[2] = 0xab;
90 frame_header[3] = 0x89;
91 memset (&frame_header[4], 0, 12);
95 _async_in[0] = AudioEngine::instance()->register_input_port (DataType::MIDI, X_("push2 in1"), true);
96 _async_out[0] = AudioEngine::instance()->register_output_port (DataType::MIDI, X_("push2 out1"), true);
98 if (_async_in[0] == 0 || _async_out[0] == 0) {
102 _input_port[0] = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_in[0]).get();
103 _output_port[0] = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_out[0]).get();
105 _async_in[1] = AudioEngine::instance()->register_input_port (DataType::MIDI, X_("push2 in2"), true);
106 _async_out[1] = AudioEngine::instance()->register_output_port (DataType::MIDI, X_("push2 out2"), true);
108 if (_async_in[1] == 0 || _async_out[1] == 0) {
112 _input_port[1] = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_in[1]).get();
113 _output_port[1] = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_out[1]).get();
117 asp = dynamic_cast<AsyncMIDIPort*> (_input_port[0]);
118 asp->xthread().set_receive_handler (sigc::bind (sigc::mem_fun (this, &Push2::midi_input_handler), _input_port[0]));
119 asp->xthread().attach (main_loop()->get_context());
121 asp = dynamic_cast<AsyncMIDIPort*> (_input_port[1]);
122 asp->xthread().set_receive_handler (sigc::bind (sigc::mem_fun (this, &Push2::midi_input_handler), _input_port[1]));
123 asp->xthread().attach (main_loop()->get_context());
125 connect_to_parser ();
133 AudioEngine::instance()->unregister_port (_async_in[0]);
134 AudioEngine::instance()->unregister_port (_async_out[0]);
135 AudioEngine::instance()->unregister_port (_async_in[1]);
136 AudioEngine::instance()->unregister_port (_async_out[1]);
138 _async_in[0].reset ((ARDOUR::Port*) 0);
139 _async_out[0].reset ((ARDOUR::Port*) 0);
140 _async_in[1].reset ((ARDOUR::Port*) 0);
141 _async_out[1].reset ((ARDOUR::Port*) 0);
143 vblank_connection.disconnect ();
144 periodic_connection.disconnect ();
145 session_connections.drop_connections ();
148 libusb_release_interface (handle, 0x00);
149 libusb_close (handle);
152 delete [] device_frame_buffer[0];
153 device_frame_buffer[0] = 0;
155 delete [] device_frame_buffer[1];
156 device_frame_buffer[1] = 0;
164 libusb_device_handle *h;
167 if ((h = libusb_open_device_with_vid_pid (NULL, ABLETON, PUSH2)) == 0) {
168 DEBUG_TRACE (DEBUG::Push2, "no Push2 device found\n");
173 DEBUG_TRACE (DEBUG::Push2, "Push2 device located\n");
178 Push2::request_factory (uint32_t num_requests)
180 /* AbstractUI<T>::request_buffer_factory() is a template method only
181 instantiated in this source module. To provide something visible for
182 use in the interface/descriptor, we have this static method that is
185 return request_buffer_factory (num_requests);
189 Push2::do_request (Push2Request * req)
191 DEBUG_TRACE (DEBUG::Push2, string_compose ("doing request type %1\n", req->type));
192 if (req->type == CallSlot) {
194 call_slot (MISSING_INVALIDATOR, req->the_slot);
196 } else if (req->type == Quit) {
210 /** render host-side frame buffer (a Cairo ImageSurface) to the current
211 * device-side frame buffer. The device frame buffer will be pushed to the
212 * device on the next call to vblank()
218 /* ensure that all drawing has been done before we fetch pixel data */
220 frame_buffer->flush ();
222 const int stride = 3840; /* bytes per row for Cairo::FORMAT_ARGB32 */
223 const uint8_t* data = frame_buffer->get_data ();
225 /* fill frame buffer (320kB) */
227 Glib::Threads::Mutex::Lock lm (fb_lock);
229 uint16_t* fb = (uint16_t*) device_frame_buffer[device_buffer];
231 for (int row = 0; row < rows; ++row) {
233 const uint8_t* dp = data + row * stride;
235 for (int col = 0; col < cols; ++col) {
237 /* fetch r, g, b (range 0..255). Ignore alpha */
239 const int r = (*((const uint32_t*)dp) >> 16) & 0xff;
240 const int g = (*((const uint32_t*)dp) >> 8) & 0xff;
241 const int b = *((const uint32_t*)dp) & 0xff;
243 /* convert to 5 bits, 6 bits, 5 bits, respectively */
244 /* generate 16 bit BGB565 value */
246 *fb++ = (r >> 3) | ((g & 0xfc) << 3) | ((b & 0xf8) << 8);
251 /* skip 128 bytes to next line. This is filler, used to avoid line borders occuring in the middle of 512
255 fb += 64; /* 128 bytes = 64 int16_t */
258 /* swap buffers (under lock protection) */
259 // device_buffer = (device_buffer ? 0 : 1);
268 const int timeout_msecs = 1000;
271 if ((err = libusb_bulk_transfer (handle, 0x01, frame_header, sizeof (frame_header), &transferred, timeout_msecs))) {
276 Glib::Threads::Mutex::Lock lm (fb_lock);
278 if ((err = libusb_bulk_transfer (handle, 0x01, (uint8_t*) device_frame_buffer[device_buffer] , 2 * rows * pixels_per_row, &transferred, timeout_msecs))) {
287 Push2::set_active (bool yn)
289 DEBUG_TRACE (DEBUG::Push2, string_compose("Push2Protocol::set_active init with yn: '%1'\n", yn));
291 if (yn == active()) {
297 /* start event loop */
302 DEBUG_TRACE (DEBUG::Push2, "device open failed\n");
307 connect_session_signals ();
311 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (frame_buffer);
313 cerr << "Cannot create context\n";
316 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
318 cerr << "Cannot create layout\n";
322 layout->set_text ("hello, Ardour");
323 Pango::FontDescription fd ("Sans Bold 12");
324 layout->set_font_description (fd);
326 context->set_source_rgb (0.0, 1.0, 1.0);
327 context->rectangle (0, 0, 960, 160);
329 context->set_source_rgb (0.0, 0.0, 0.0);
330 context->rectangle (50, 50, 860, 60);
332 context->move_to (60, 60);
333 context->set_source_rgb ((random()%255) / 255.0, (random()%255) / 255.0, (random()%255) / 255.0);
334 layout->update_from_cairo_context (context);
335 layout->show_in_cairo_context (context);
339 /* set up periodic task used to push a frame buffer to the
340 * device (25fps). The device can handle 60fps, but we don't
341 * need that frame rate.
344 Glib::RefPtr<Glib::TimeoutSource> vblank_timeout = Glib::TimeoutSource::create (40); // milliseconds
345 vblank_connection = vblank_timeout->connect (sigc::mem_fun (*this, &Push2::vblank));
346 vblank_timeout->attach (main_loop()->get_context());
349 Glib::RefPtr<Glib::TimeoutSource> periodic_timeout = Glib::TimeoutSource::create (1000); // milliseconds
350 periodic_connection = periodic_timeout->connect (sigc::mem_fun (*this, &Push2::periodic));
351 periodic_timeout->attach (main_loop()->get_context());
359 ControlProtocol::set_active (yn);
361 DEBUG_TRACE (DEBUG::Push2, string_compose("Push2Protocol::set_active done with yn: '%1'\n", yn));
367 Push2::write (int port, const MidiByteArray& data)
369 /* immediate delivery */
370 cerr << data << endl;
371 _output_port[port]->write (&data[0], data.size(), 0);
375 Push2::midi_input_handler (IOCondition ioc, MIDI::Port* port)
378 DEBUG_TRACE (DEBUG::Push2, "MIDI port closed\n");
384 // DEBUG_TRACE (DEBUG::Push2, string_compose ("something happend on %1\n", port->name()));
386 AsyncMIDIPort* asp = dynamic_cast<AsyncMIDIPort*>(port);
391 //DEBUG_TRACE (DEBUG::Push2, string_compose ("data available on %1\n", port->name()));
392 framepos_t now = AudioEngine::instance()->sample_time();
406 Push2::connect_to_parser ()
408 DEBUG_TRACE (DEBUG::Push2, string_compose ("Connecting to signals on port %2\n", _input_port[0]->name()));
410 MIDI::Parser* p = _input_port[0]->parser();
413 p->sysex.connect_same_thread (*this, boost::bind (&Push2::handle_midi_sysex, this, _1, _2, _3));
414 /* V-Pot messages are Controller */
415 p->controller.connect_same_thread (*this, boost::bind (&Push2::handle_midi_controller_message, this, _1, _2));
416 /* Button messages are NoteOn */
417 p->note_on.connect_same_thread (*this, boost::bind (&Push2::handle_midi_note_on_message, this, _1, _2));
418 /* Button messages are NoteOn but libmidi++ sends note-on w/velocity = 0 as note-off so catch them too */
419 p->note_off.connect_same_thread (*this, boost::bind (&Push2::handle_midi_note_on_message, this, _1, _2));
420 /* Fader messages are Pitchbend */
421 p->channel_pitchbend[0].connect_same_thread (*this, boost::bind (&Push2::handle_midi_pitchbend_message, this, _1, _2));
425 Push2::handle_midi_sysex (MIDI::Parser&, MIDI::byte* raw_bytes, size_t sz)
427 cerr << "sysex, " << sz << " bytes\n";
431 Push2::handle_midi_controller_message (MIDI::Parser&, MIDI::EventTwoBytes* ev)
433 cerr << "controller " << (int) ev->controller_number << " = " << (int) ev->value << endl;
434 CCButtonMap::iterator b = cc_button_map.find (ev->controller_number);
435 if (b != cc_button_map.end()) {
436 if (ev->value == 0) {
437 (this->*b->second->release_method)();
439 (this->*b->second->press_method)();
445 Push2::handle_midi_note_on_message (MIDI::Parser&, MIDI::EventTwoBytes* ev)
447 cerr << "note on" << (int) ev->note_number << ", velocity " << (int) ev->velocity << endl;
451 Push2::handle_midi_note_off_message (MIDI::Parser&, MIDI::EventTwoBytes* ev)
453 cerr << "note on" << (int) ev->note_number << ", velocity " << (int) ev->velocity << endl;
457 Push2::handle_midi_pitchbend_message (MIDI::Parser&, MIDI::pitchbend_t pb)
459 cerr << "pitchbend @ " << pb << endl;
469 #define MAKE_PAD(x,y,nn) \
470 pad = new Pad ((x), (y), (nn)); \
471 nn_pad_map.insert (make_pair (pad->extra(), pad)); \
472 coord_pad_map.insert (make_pair (pad->coord(), pad));
538 /* Now color buttons */
542 #define MAKE_COLOR_BUTTON(i,cc) \
543 button = new ColorButton ((i), (cc)); \
544 cc_button_map.insert (make_pair (button->controller_number(), button)); \
545 id_button_map.insert (make_pair (button->id, button));
546 #define MAKE_COLOR_BUTTON_PRESS(i,cc,p)\
547 button = new ColorButton ((i), (cc), (p)); \
548 cc_button_map.insert (make_pair (button->controller_number(), button)); \
549 id_button_map.insert (make_pair (button->id, button))
551 MAKE_COLOR_BUTTON (Upper1, 102);
552 MAKE_COLOR_BUTTON (Upper2, 103);
553 MAKE_COLOR_BUTTON (Upper3, 104);
554 MAKE_COLOR_BUTTON (Upper4, 105);
555 MAKE_COLOR_BUTTON (Upper5, 106);
556 MAKE_COLOR_BUTTON (Upper6, 107);
557 MAKE_COLOR_BUTTON (Upper7, 108);
558 MAKE_COLOR_BUTTON (Upper8, 109);
559 MAKE_COLOR_BUTTON (Lower1, 21);
560 MAKE_COLOR_BUTTON (Lower2, 22);
561 MAKE_COLOR_BUTTON (Lower3, 23);
562 MAKE_COLOR_BUTTON (Lower4, 24);
563 MAKE_COLOR_BUTTON (Lower5, 25);
564 MAKE_COLOR_BUTTON (Lower6, 26);
565 MAKE_COLOR_BUTTON (Lower7, 27);
566 MAKE_COLOR_BUTTON (Mute, 60);
567 MAKE_COLOR_BUTTON (Solo, 61);
568 MAKE_COLOR_BUTTON (Stop, 29);
569 MAKE_COLOR_BUTTON (Fwd32ndT, 43);
570 MAKE_COLOR_BUTTON (Fwd32nd,42 );
571 MAKE_COLOR_BUTTON (Fwd16thT, 41);
572 MAKE_COLOR_BUTTON (Fwd16th, 40);
573 MAKE_COLOR_BUTTON (Fwd8thT, 39 );
574 MAKE_COLOR_BUTTON (Fwd8th, 38);
575 MAKE_COLOR_BUTTON (Fwd4trT, 37);
576 MAKE_COLOR_BUTTON (Fwd4tr, 36);
577 MAKE_COLOR_BUTTON (Automate, 89);
578 MAKE_COLOR_BUTTON_PRESS (RecordEnable, 86, &Push::button_recenable);
579 MAKE_COLOR_BUTTON_PRESS (Play, 85, &Push2::button_play);
581 #define MAKE_WHITE_BUTTON(i,cc)\
582 button = new WhiteButton ((i), (cc)); \
583 cc_button_map.insert (make_pair (button->controller_number(), button)); \
584 id_button_map.insert (make_pair (button->id, button))
585 #define MAKE_WHITE_BUTTON_PRESS(i,cc,p)\
586 button = new WhiteButton ((i), (cc), (p)); \
587 cc_button_map.insert (make_pair (button->controller_number(), button)); \
588 id_button_map.insert (make_pair (button->id, button))
590 MAKE_WHITE_BUTTON (TapTempo, 3);
591 MAKE_WHITE_BUTTON (Metronome, 9);
592 MAKE_WHITE_BUTTON (Setup, 30);
593 MAKE_WHITE_BUTTON (User, 59);
594 MAKE_WHITE_BUTTON (Delete, 118);
595 MAKE_WHITE_BUTTON (AddDevice, 52);
596 MAKE_WHITE_BUTTON (Device, 110);
597 MAKE_WHITE_BUTTON (Mix, 112);
598 MAKE_WHITE_BUTTON (Undo, 119);
599 MAKE_WHITE_BUTTON (AddTrack, 53);
600 MAKE_WHITE_BUTTON (Browse, 113);
601 MAKE_WHITE_BUTTON (Convert, 35);
602 MAKE_WHITE_BUTTON (DoubleLoop, 117);
603 MAKE_WHITE_BUTTON (Quantize, 116);
604 MAKE_WHITE_BUTTON (Duplicate, 88);
605 MAKE_WHITE_BUTTON (New, 87);
606 MAKE_WHITE_BUTTON (FixedLength, 90);
607 MAKE_WHITE_BUTTON_PRESS (Up, 46, &Push2::button_up);
608 MAKE_WHITE_BUTTON (Right, 45);
609 MAKE_WHITE_BUTTON_PRESS (Down, 47, &Push2::button_down);
610 MAKE_WHITE_BUTTON (Left, 44);
611 MAKE_WHITE_BUTTON (Repeat, 56);
612 MAKE_WHITE_BUTTON (Accent, 57);
613 MAKE_WHITE_BUTTON (Scale, 58);
614 MAKE_WHITE_BUTTON (Layout, 31);
615 MAKE_WHITE_BUTTON (OctaveUp, 55);
616 MAKE_WHITE_BUTTON (PageRight, 63);
617 MAKE_WHITE_BUTTON (OctaveDown, 54);
618 MAKE_WHITE_BUTTON (PageLeft, 62);
619 MAKE_WHITE_BUTTON (Shift, 49);
620 MAKE_WHITE_BUTTON (Select, 48);
624 Push2::thread_init ()
626 struct sched_param rtparam;
628 pthread_set_name (event_loop_name().c_str());
630 PBD::notify_event_loops_about_thread_creation (pthread_self(), event_loop_name(), 2048);
631 ARDOUR::SessionEvent::create_per_thread_pool (event_loop_name(), 128);
633 memset (&rtparam, 0, sizeof (rtparam));
634 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
636 if (pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam) != 0) {
637 // do we care? not particularly.
642 Push2::connect_session_signals()
644 // receive routes added
645 //session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_routes_added, this, _1), this);
646 // receive VCAs added
647 //session->vca_manager().VCAAdded.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_vca_added, this, _1), this);
649 // receive record state toggled
650 session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_record_state_changed, this), this);
651 // receive transport state changed
652 session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_transport_state_changed, this), this);
653 session->TransportLooped.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_loop_state_changed, this), this);
654 // receive punch-in and punch-out
655 Config->ParameterChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_parameter_changed, this, _1), this);
656 session->config.ParameterChanged.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_parameter_changed, this, _1), this);
657 // receive rude solo changed
658 session->SoloActive.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_solo_active_changed, this, _1), this);
662 Push2::notify_record_state_changed ()
664 IDButtonMap::iterator b = id_button_map.find (RecordEnable);
666 if (b == id_button_map.end()) {
670 if (session->actively_recording ()) {
671 b->second->set_state (LED::OneShot24th);
672 b->second->set_color (127);
674 b->second->set_state (LED::Off);
677 write (0, b->second->state_msg());
681 Push2::notify_transport_state_changed ()
683 cerr << "ts change, id button map holds " << id_button_map.size() << endl;
685 IDButtonMap::iterator b = id_button_map.find (Play);
687 if (b == id_button_map.end()) {
688 cerr << " no button\n";
692 if (session->transport_rolling()) {
693 b->second->set_state (LED::OneShot24th);
694 b->second->set_color (125);
696 b->second->set_state (LED::Off);
699 write (0, b->second->state_msg());
703 Push2::notify_loop_state_changed ()
708 Push2::notify_parameter_changed (std::string)
713 Push2::notify_solo_active_changed (bool yn)
715 IDButtonMap::iterator b = id_button_map.find (Solo);
717 if (b == id_button_map.end()) {
722 b->second->set_state (LED::Blinking24th);
724 b->second->set_state (LED::Off);
727 write (0, b->second->state_msg());
733 XMLNode& node (ControlProtocol::get_state());
735 DEBUG_TRACE (DEBUG::Push2, "Push2::get_state done\n");
741 Push2::set_state (const XMLNode & node, int version)
743 DEBUG_TRACE (DEBUG::Push2, string_compose ("Push2::set_state: active %1\n", active()));
747 if (ControlProtocol::set_state (node, version)) {