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"
28 #include "timecode/time.h"
29 #include "timecode/bbt_time.h"
31 #include "ardour/async_midi_port.h"
32 #include "ardour/audioengine.h"
33 #include "ardour/debug.h"
34 #include "ardour/midiport_manager.h"
35 #include "ardour/session.h"
36 #include "ardour/tempo.h"
40 using namespace ARDOUR;
44 using namespace ArdourSurface;
48 #include "pbd/abstract_ui.cc" // instantiate template
50 const int Push2::cols = 960;
51 const int Push2::rows = 160;
52 const int Push2::pixels_per_row = 1024;
54 #define ABLETON 0x2982
57 Push2::Push2 (ARDOUR::Session& s)
58 : ControlProtocol (s, string (X_("Ableton Push 2")))
59 , AbstractUI<Push2Request> (name())
62 , frame_buffer (Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, cols, rows))
64 context = Cairo::Context::create (frame_buffer);
65 tc_clock_layout = Pango::Layout::create (context);
66 bbt_clock_layout = Pango::Layout::create (context);
68 Pango::FontDescription fd ("Sans Bold 24");
69 tc_clock_layout->set_font_description (fd);
70 bbt_clock_layout->set_font_description (fd);
72 Pango::FontDescription fd2 ("Sans Bold 10");
73 for (int n = 0; n < 8; ++n) {
74 upper_layout[n] = Pango::Layout::create (context);
75 upper_layout[n]->set_font_description (fd2);
76 upper_layout[n]->set_text ("solo");
77 lower_layout[n] = Pango::Layout::create (context);
78 lower_layout[n]->set_font_description (fd2);
79 lower_layout[n]->set_text ("mute");
80 mid_layout[n] = Pango::Layout::create (context);
81 mid_layout[n]->set_font_description (fd2);
82 mid_layout[n]->set_text (string_compose ("Inst %1", n));
86 throw failed_constructor ();
107 if ((handle = libusb_open_device_with_vid_pid (NULL, ABLETON, PUSH2)) == 0) {
111 if ((err = libusb_claim_interface (handle, 0x00))) {
115 device_frame_buffer = new uint16_t[rows*pixels_per_row];
117 memset (device_frame_buffer, 0, sizeof (uint16_t) * rows * pixels_per_row);
119 frame_header[0] = 0xef;
120 frame_header[1] = 0xcd;
121 frame_header[2] = 0xab;
122 frame_header[3] = 0x89;
123 memset (&frame_header[4], 0, 12);
127 _async_in = AudioEngine::instance()->register_input_port (DataType::MIDI, X_("push2 in"), true);
128 _async_out = AudioEngine::instance()->register_output_port (DataType::MIDI, X_("push2 out"), true);
130 if (_async_in == 0 || _async_out == 0) {
134 _input_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_in).get();
135 _output_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_out).get();
137 connect_to_parser ();
145 AudioEngine::instance()->unregister_port (_async_in);
146 AudioEngine::instance()->unregister_port (_async_out);
148 _async_in.reset ((ARDOUR::Port*) 0);
149 _async_out.reset ((ARDOUR::Port*) 0);
153 vblank_connection.disconnect ();
154 periodic_connection.disconnect ();
155 session_connections.drop_connections ();
158 libusb_release_interface (handle, 0x00);
159 libusb_close (handle);
163 delete [] device_frame_buffer;
164 device_frame_buffer = 0;
172 libusb_device_handle *h;
175 if ((h = libusb_open_device_with_vid_pid (NULL, ABLETON, PUSH2)) == 0) {
176 DEBUG_TRACE (DEBUG::Push2, "no Push2 device found\n");
181 DEBUG_TRACE (DEBUG::Push2, "Push2 device located\n");
186 Push2::request_factory (uint32_t num_requests)
188 /* AbstractUI<T>::request_buffer_factory() is a template method only
189 instantiated in this source module. To provide something visible for
190 use in the interface/descriptor, we have this static method that is
193 return request_buffer_factory (num_requests);
197 Push2::do_request (Push2Request * req)
199 DEBUG_TRACE (DEBUG::Push2, string_compose ("doing request type %1\n", req->type));
200 if (req->type == CallSlot) {
202 call_slot (MISSING_INVALIDATOR, req->the_slot);
204 } else if (req->type == Quit) {
218 /** render host-side frame buffer (a Cairo ImageSurface) to the current
219 * device-side frame buffer. The device frame buffer will be pushed to the
220 * device on the next call to vblank()
224 Push2::bitblt_to_device_frame_buffer ()
226 /* ensure that all drawing has been done before we fetch pixel data */
228 frame_buffer->flush ();
230 const int stride = 3840; /* bytes per row for Cairo::FORMAT_ARGB32 */
231 const uint8_t* data = frame_buffer->get_data ();
233 /* fill frame buffer (320kB) */
235 uint16_t* fb = (uint16_t*) device_frame_buffer;
237 for (int row = 0; row < rows; ++row) {
239 const uint8_t* dp = data + row * stride;
241 for (int col = 0; col < cols; ++col) {
243 /* fetch r, g, b (range 0..255). Ignore alpha */
245 const int r = (*((const uint32_t*)dp) >> 16) & 0xff;
246 const int g = (*((const uint32_t*)dp) >> 8) & 0xff;
247 const int b = *((const uint32_t*)dp) & 0xff;
249 /* convert to 5 bits, 6 bits, 5 bits, respectively */
250 /* generate 16 bit BGB565 value */
252 *fb++ = (r >> 3) | ((g & 0xfc) << 3) | ((b & 0xf8) << 8);
257 /* skip 128 bytes to next line. This is filler, used to avoid line borders occuring in the middle of 512
261 fb += 64; /* 128 bytes = 64 int16_t */
270 string tc_clock_text;
271 string bbt_clock_text;
274 framepos_t audible = session->audible_frame();
276 bool negative = false;
283 session->timecode_time (audible, TC);
285 TC.negative = TC.negative || negative;
287 tc_clock_text = Timecode::timecode_format_time(TC);
289 Timecode::BBT_Time bbt = session->tempo_map().bbt_at_frame (audible);
292 #define BBT_BAR_CHAR "|"
295 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
296 bbt.bars, bbt.beats, bbt.ticks);
298 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
299 bbt.bars, bbt.beats, bbt.ticks);
302 bbt_clock_text = buf;
307 if (tc_clock_text != tc_clock_layout->get_text()) {
309 tc_clock_layout->set_text (tc_clock_text);
312 if (bbt_clock_text != tc_clock_layout->get_text()) {
314 bbt_clock_layout->set_text (bbt_clock_text);
321 context->set_source_rgb (0.764, 0.882, 0.882);
322 context->rectangle (0, 0, 960, 160);
324 context->set_source_rgb (0.23, 0.0, 0.349);
325 context->move_to (650, 25);
326 tc_clock_layout->update_from_cairo_context (context);
327 tc_clock_layout->show_in_cairo_context (context);
328 context->move_to (650, 60);
329 bbt_clock_layout->update_from_cairo_context (context);
330 bbt_clock_layout->show_in_cairo_context (context);
332 for (int n = 0; n < 8; ++n) {
333 context->move_to (10 + (n*120), 2);
334 upper_layout[n]->update_from_cairo_context (context);
335 upper_layout[n]->show_in_cairo_context (context);
338 for (int n = 0; n < 8; ++n) {
339 context->move_to (10 + (n*120), 140);
340 lower_layout[n]->update_from_cairo_context (context);
341 lower_layout[n]->show_in_cairo_context (context);
344 for (int n = 0; n < 8; ++n) {
345 context->move_to (10 + (n*120), 120);
346 context->set_source_rgb (0.0, 0.0, 0.0);
347 mid_layout[n]->update_from_cairo_context (context);
348 mid_layout[n]->show_in_cairo_context (context);
362 const int timeout_msecs = 1000;
365 if ((err = libusb_bulk_transfer (handle, 0x01, frame_header, sizeof (frame_header), &transferred, timeout_msecs))) {
371 bitblt_to_device_frame_buffer ();
374 if ((err = libusb_bulk_transfer (handle, 0x01, (uint8_t*) device_frame_buffer , 2 * rows * pixels_per_row, &transferred, timeout_msecs))) {
382 Push2::set_active (bool yn)
384 DEBUG_TRACE (DEBUG::Push2, string_compose("Push2Protocol::set_active init with yn: '%1'\n", yn));
386 if (yn == active()) {
392 /* start event loop */
397 DEBUG_TRACE (DEBUG::Push2, "device open failed\n");
402 /* Connect input port to event loop */
406 asp = dynamic_cast<AsyncMIDIPort*> (_input_port);
407 asp->xthread().set_receive_handler (sigc::bind (sigc::mem_fun (this, &Push2::midi_input_handler), _input_port));
408 asp->xthread().attach (main_loop()->get_context());
410 connect_session_signals ();
412 /* set up periodic task used to push a frame buffer to the
413 * device (25fps). The device can handle 60fps, but we don't
414 * need that frame rate.
417 Glib::RefPtr<Glib::TimeoutSource> vblank_timeout = Glib::TimeoutSource::create (40); // milliseconds
418 vblank_connection = vblank_timeout->connect (sigc::mem_fun (*this, &Push2::vblank));
419 vblank_timeout->attach (main_loop()->get_context());
422 Glib::RefPtr<Glib::TimeoutSource> periodic_timeout = Glib::TimeoutSource::create (1000); // milliseconds
423 periodic_connection = periodic_timeout->connect (sigc::mem_fun (*this, &Push2::periodic));
424 periodic_timeout->attach (main_loop()->get_context());
432 ControlProtocol::set_active (yn);
434 DEBUG_TRACE (DEBUG::Push2, string_compose("Push2Protocol::set_active done with yn: '%1'\n", yn));
440 Push2::write (const MidiByteArray& data)
442 cerr << data << endl;
443 /* immediate delivery */
444 _output_port->write (&data[0], data.size(), 0);
448 Push2::midi_input_handler (IOCondition ioc, MIDI::Port* port)
451 DEBUG_TRACE (DEBUG::Push2, "MIDI port closed\n");
457 // DEBUG_TRACE (DEBUG::Push2, string_compose ("something happend on %1\n", port->name()));
459 AsyncMIDIPort* asp = dynamic_cast<AsyncMIDIPort*>(port);
464 //DEBUG_TRACE (DEBUG::Push2, string_compose ("data available on %1\n", port->name()));
465 framepos_t now = AudioEngine::instance()->sample_time();
479 Push2::connect_to_parser ()
481 DEBUG_TRACE (DEBUG::Push2, string_compose ("Connecting to signals on port %2\n", _input_port->name()));
483 MIDI::Parser* p = _input_port->parser();
486 p->sysex.connect_same_thread (*this, boost::bind (&Push2::handle_midi_sysex, this, _1, _2, _3));
487 /* V-Pot messages are Controller */
488 p->controller.connect_same_thread (*this, boost::bind (&Push2::handle_midi_controller_message, this, _1, _2));
489 /* Button messages are NoteOn */
490 p->note_on.connect_same_thread (*this, boost::bind (&Push2::handle_midi_note_on_message, this, _1, _2));
491 /* Button messages are NoteOn but libmidi++ sends note-on w/velocity = 0 as note-off so catch them too */
492 p->note_off.connect_same_thread (*this, boost::bind (&Push2::handle_midi_note_on_message, this, _1, _2));
493 /* Fader messages are Pitchbend */
494 p->channel_pitchbend[0].connect_same_thread (*this, boost::bind (&Push2::handle_midi_pitchbend_message, this, _1, _2));
498 Push2::handle_midi_sysex (MIDI::Parser&, MIDI::byte* raw_bytes, size_t sz)
500 cerr << "sysex, " << sz << " bytes\n";
504 Push2::handle_midi_controller_message (MIDI::Parser&, MIDI::EventTwoBytes* ev)
506 CCButtonMap::iterator b = cc_button_map.find (ev->controller_number);
507 if (b != cc_button_map.end()) {
508 if (ev->value == 0) {
509 (this->*b->second->release_method)();
511 (this->*b->second->press_method)();
517 Push2::handle_midi_note_on_message (MIDI::Parser&, MIDI::EventTwoBytes* ev)
519 cerr << "note on" << (int) ev->note_number << ", velocity " << (int) ev->velocity << endl;
523 Push2::handle_midi_note_off_message (MIDI::Parser&, MIDI::EventTwoBytes* ev)
525 cerr << "note on" << (int) ev->note_number << ", velocity " << (int) ev->velocity << endl;
529 Push2::handle_midi_pitchbend_message (MIDI::Parser&, MIDI::pitchbend_t pb)
531 cerr << "pitchbend @ " << pb << endl;
541 #define MAKE_PAD(x,y,nn) \
542 pad = new Pad ((x), (y), (nn)); \
543 nn_pad_map.insert (make_pair (pad->extra(), pad)); \
544 coord_pad_map.insert (make_pair (pad->coord(), pad));
610 /* Now color buttons */
614 #define MAKE_COLOR_BUTTON(i,cc) \
615 button = new ColorButton ((i), (cc)); \
616 cc_button_map.insert (make_pair (button->controller_number(), button)); \
617 id_button_map.insert (make_pair (button->id, button));
618 #define MAKE_COLOR_BUTTON_PRESS(i,cc,p)\
619 button = new ColorButton ((i), (cc), (p)); \
620 cc_button_map.insert (make_pair (button->controller_number(), button)); \
621 id_button_map.insert (make_pair (button->id, button))
623 MAKE_COLOR_BUTTON (Upper1, 102);
624 MAKE_COLOR_BUTTON (Upper2, 103);
625 MAKE_COLOR_BUTTON (Upper3, 104);
626 MAKE_COLOR_BUTTON (Upper4, 105);
627 MAKE_COLOR_BUTTON (Upper5, 106);
628 MAKE_COLOR_BUTTON (Upper6, 107);
629 MAKE_COLOR_BUTTON (Upper7, 108);
630 MAKE_COLOR_BUTTON (Upper8, 109);
631 MAKE_COLOR_BUTTON (Lower1, 21);
632 MAKE_COLOR_BUTTON (Lower2, 22);
633 MAKE_COLOR_BUTTON (Lower3, 23);
634 MAKE_COLOR_BUTTON (Lower4, 24);
635 MAKE_COLOR_BUTTON (Lower5, 25);
636 MAKE_COLOR_BUTTON (Lower6, 26);
637 MAKE_COLOR_BUTTON (Lower7, 27);
638 MAKE_COLOR_BUTTON (Mute, 60);
639 MAKE_COLOR_BUTTON (Solo, 61);
640 MAKE_COLOR_BUTTON (Stop, 29);
641 MAKE_COLOR_BUTTON (Fwd32ndT, 43);
642 MAKE_COLOR_BUTTON (Fwd32nd,42 );
643 MAKE_COLOR_BUTTON (Fwd16thT, 41);
644 MAKE_COLOR_BUTTON (Fwd16th, 40);
645 MAKE_COLOR_BUTTON (Fwd8thT, 39 );
646 MAKE_COLOR_BUTTON (Fwd8th, 38);
647 MAKE_COLOR_BUTTON (Fwd4trT, 37);
648 MAKE_COLOR_BUTTON (Fwd4tr, 36);
649 MAKE_COLOR_BUTTON (Automate, 89);
650 MAKE_COLOR_BUTTON_PRESS (RecordEnable, 86, &Push2::button_recenable);
651 MAKE_COLOR_BUTTON_PRESS (Play, 85, &Push2::button_play);
653 #define MAKE_WHITE_BUTTON(i,cc)\
654 button = new WhiteButton ((i), (cc)); \
655 cc_button_map.insert (make_pair (button->controller_number(), button)); \
656 id_button_map.insert (make_pair (button->id, button))
657 #define MAKE_WHITE_BUTTON_PRESS(i,cc,p)\
658 button = new WhiteButton ((i), (cc), (p)); \
659 cc_button_map.insert (make_pair (button->controller_number(), button)); \
660 id_button_map.insert (make_pair (button->id, button))
662 MAKE_WHITE_BUTTON (TapTempo, 3);
663 MAKE_WHITE_BUTTON_PRESS (Metronome, 9, &Push2::button_metronome);
664 MAKE_WHITE_BUTTON (Setup, 30);
665 MAKE_WHITE_BUTTON (User, 59);
666 MAKE_WHITE_BUTTON (Delete, 118);
667 MAKE_WHITE_BUTTON (AddDevice, 52);
668 MAKE_WHITE_BUTTON (Device, 110);
669 MAKE_WHITE_BUTTON (Mix, 112);
670 MAKE_WHITE_BUTTON (Undo, 119);
671 MAKE_WHITE_BUTTON (AddTrack, 53);
672 MAKE_WHITE_BUTTON (Browse, 113);
673 MAKE_WHITE_BUTTON (Convert, 35);
674 MAKE_WHITE_BUTTON (DoubleLoop, 117);
675 MAKE_WHITE_BUTTON (Quantize, 116);
676 MAKE_WHITE_BUTTON (Duplicate, 88);
677 MAKE_WHITE_BUTTON (New, 87);
678 MAKE_WHITE_BUTTON (FixedLength, 90);
679 MAKE_WHITE_BUTTON_PRESS (Up, 46, &Push2::button_up);
680 MAKE_WHITE_BUTTON_PRESS (Right, 45, &Push2::button_right);
681 MAKE_WHITE_BUTTON_PRESS (Down, 47, &Push2::button_down);
682 MAKE_WHITE_BUTTON_PRESS (Left, 44, &Push2::button_left);
683 MAKE_WHITE_BUTTON_PRESS (Repeat, 56, &Push2::button_repeat);
684 MAKE_WHITE_BUTTON (Accent, 57);
685 MAKE_WHITE_BUTTON (Scale, 58);
686 MAKE_WHITE_BUTTON (Layout, 31);
687 MAKE_WHITE_BUTTON (OctaveUp, 55);
688 MAKE_WHITE_BUTTON (PageRight, 63);
689 MAKE_WHITE_BUTTON (OctaveDown, 54);
690 MAKE_WHITE_BUTTON (PageLeft, 62);
691 MAKE_WHITE_BUTTON (Shift, 49);
692 MAKE_WHITE_BUTTON (Select, 48);
696 Push2::thread_init ()
698 struct sched_param rtparam;
700 pthread_set_name (event_loop_name().c_str());
702 PBD::notify_event_loops_about_thread_creation (pthread_self(), event_loop_name(), 2048);
703 ARDOUR::SessionEvent::create_per_thread_pool (event_loop_name(), 128);
705 memset (&rtparam, 0, sizeof (rtparam));
706 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
708 if (pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam) != 0) {
709 // do we care? not particularly.
714 Push2::connect_session_signals()
716 // receive routes added
717 //session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&MackieControlProtocol::notify_routes_added, this, _1), this);
718 // receive VCAs added
719 //session->vca_manager().VCAAdded.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_vca_added, this, _1), this);
721 // receive record state toggled
722 session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_record_state_changed, this), this);
723 // receive transport state changed
724 session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_transport_state_changed, this), this);
725 session->TransportLooped.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_loop_state_changed, this), this);
726 // receive punch-in and punch-out
727 Config->ParameterChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_parameter_changed, this, _1), this);
728 session->config.ParameterChanged.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_parameter_changed, this, _1), this);
729 // receive rude solo changed
730 session->SoloActive.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&Push2::notify_solo_active_changed, this, _1), this);
734 Push2::notify_record_state_changed ()
736 IDButtonMap::iterator b = id_button_map.find (RecordEnable);
738 if (b == id_button_map.end()) {
742 b->second->set_color (LED::Red);
744 switch (session->record_status ()) {
745 case Session::Disabled:
746 b->second->set_state (LED::Off);
748 case Session::Enabled:
749 b->second->set_state (LED::Blinking4th);
751 case Session::Recording:
752 b->second->set_state (LED::OneShot24th);
756 write (b->second->state_msg());
760 Push2::notify_transport_state_changed ()
762 IDButtonMap::iterator b = id_button_map.find (Play);
764 if (b == id_button_map.end()) {
768 if (session->transport_rolling()) {
769 b->second->set_state (LED::OneShot24th);
770 b->second->set_color (LED::Blue);
772 b->second->set_state (LED::Off);
775 write (b->second->state_msg());
779 Push2::notify_loop_state_changed ()
784 Push2::notify_parameter_changed (std::string)
789 Push2::notify_solo_active_changed (bool yn)
791 IDButtonMap::iterator b = id_button_map.find (Solo);
793 if (b == id_button_map.end()) {
798 b->second->set_state (LED::Blinking24th);
800 b->second->set_state (LED::Off);
803 write (b->second->state_msg());
809 XMLNode& node (ControlProtocol::get_state());
812 child = new XMLNode (X_("Input"));
813 child->add_child_nocopy (_async_in->get_state());
814 node.add_child_nocopy (*child);
815 child = new XMLNode (X_("Output"));
816 child->add_child_nocopy (_async_out->get_state());
817 node.add_child_nocopy (*child);
823 Push2::set_state (const XMLNode & node, int version)
825 DEBUG_TRACE (DEBUG::Push2, string_compose ("Push2::set_state: active %1\n", active()));
829 if (ControlProtocol::set_state (node, version)) {
835 if ((child = node.child (X_("Input"))) != 0) {
836 XMLNode* portnode = child->child (Port::state_node_name.c_str());
838 _async_in->set_state (*portnode, version);
842 if ((child = node.child (X_("Output"))) != 0) {
843 XMLNode* portnode = child->child (Port::state_node_name.c_str());
845 _async_out->set_state (*portnode, version);