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 "ardour/debug.h"
28 #include "ardour/audioengine.h"
29 #include "ardour/async_midi_port.h"
30 #include "ardour/midiport_manager.h"
34 using namespace ARDOUR;
38 using namespace ArdourSurface;
42 #include "pbd/abstract_ui.cc" // instantiate template
44 const int Push2::cols = 960;
45 const int Push2::rows = 160;
46 const int Push2::pixels_per_row = 1024;
48 #define ABLETON 0x2982
51 Push2::Push2 (Session& s)
52 : ControlProtocol (s, string (X_("Ableton Push2")))
53 , AbstractUI<Push2Request> (name())
56 , frame_buffer (Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, cols, rows))
71 if ((handle = libusb_open_device_with_vid_pid (NULL, ABLETON, PUSH2)) == 0) {
75 if ((err = libusb_claim_interface (handle, 0x00))) {
79 device_frame_buffer[0] = new uint16_t[rows*pixels_per_row];
80 device_frame_buffer[1] = new uint16_t[rows*pixels_per_row];
82 memset (device_frame_buffer[0], 0, sizeof (uint16_t) * rows * pixels_per_row);
83 memset (device_frame_buffer[1], 0, sizeof (uint16_t) * rows * pixels_per_row);
85 frame_header[0] = 0xef;
86 frame_header[1] = 0xcd;
87 frame_header[2] = 0xab;
88 frame_header[3] = 0x89;
89 memset (&frame_header[4], 0, 12);
93 _async_in[0] = AudioEngine::instance()->register_input_port (DataType::MIDI, X_("push2 in1"), true);
94 _async_out[0] = AudioEngine::instance()->register_output_port (DataType::MIDI, X_("push2 out1"), true);
96 if (_async_in[0] == 0 || _async_out[0] == 0) {
100 _input_port[1] = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_in[1]).get();
101 _output_port[1] = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_out[1]).get();
103 _async_in[1] = AudioEngine::instance()->register_input_port (DataType::MIDI, X_("push2 in2"), true);
104 _async_out[1] = AudioEngine::instance()->register_output_port (DataType::MIDI, X_("push2 out2"), true);
106 if (_async_in[1] == 0 || _async_out[1] == 0) {
110 _input_port[1] = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_in[1]).get();
111 _output_port[1] = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_out[1]).get();
119 AudioEngine::instance()->unregister_port (_async_in[0]);
120 AudioEngine::instance()->unregister_port (_async_out[0]);
121 AudioEngine::instance()->unregister_port (_async_in[1]);
122 AudioEngine::instance()->unregister_port (_async_out[1]);
124 _async_in[0].reset ((ARDOUR::Port*) 0);
125 _async_out[0].reset ((ARDOUR::Port*) 0);
126 _async_in[1].reset ((ARDOUR::Port*) 0);
127 _async_out[1].reset ((ARDOUR::Port*) 0);
129 vblank_connection.disconnect ();
132 libusb_release_interface (handle, 0x00);
133 libusb_close (handle);
136 delete [] device_frame_buffer[0];
137 device_frame_buffer[0] = 0;
139 delete [] device_frame_buffer[1];
140 device_frame_buffer[1] = 0;
148 libusb_device_handle *h;
151 if ((h = libusb_open_device_with_vid_pid (NULL, ABLETON, PUSH2)) == 0) {
152 DEBUG_TRACE (DEBUG::Push2, "no Push2 device found\n");
157 DEBUG_TRACE (DEBUG::Push2, "Push2 device located\n");
162 Push2::request_factory (uint32_t num_requests)
164 /* AbstractUI<T>::request_buffer_factory() is a template method only
165 instantiated in this source module. To provide something visible for
166 use in the interface/descriptor, we have this static method that is
169 return request_buffer_factory (num_requests);
173 Push2::do_request (Push2Request * req)
175 DEBUG_TRACE (DEBUG::Push2, string_compose ("doing request type %1\n", req->type));
176 if (req->type == CallSlot) {
178 call_slot (MISSING_INVALIDATOR, req->the_slot);
180 } else if (req->type == Quit) {
195 /** render host-side frame buffer (a Cairo ImageSurface) to the current
196 * device-side frame buffer. The device frame buffer will be pushed to the
197 * device on the next call to vblank()
203 /* ensure that all drawing has been done before we fetch pixel data */
205 frame_buffer->flush ();
207 const int stride = 3840; /* bytes per row for Cairo::FORMAT_ARGB32 */
208 const uint8_t* data = frame_buffer->get_data ();
210 /* fill frame buffer (320kB) */
212 Glib::Threads::Mutex::Lock lm (fb_lock);
214 uint16_t* fb = (uint16_t*) device_frame_buffer[device_buffer];
216 for (int row = 0; row < rows; ++row) {
218 const uint8_t* dp = data + row * stride;
220 for (int col = 0; col < cols; ++col) {
222 /* fetch r, g, b (range 0..255). Ignore alpha */
224 const int r = (*((const uint32_t*)dp) >> 16) & 0xff;
225 const int g = (*((const uint32_t*)dp) >> 8) & 0xff;
226 const int b = *((const uint32_t*)dp) & 0xff;
228 /* convert to 5 bits, 6 bits, 5 bits, respectively */
229 /* generate 16 bit BGB565 value */
231 *fb++ = (r >> 3) | ((g & 0xfc) << 3) | ((b & 0xf8) << 8);
236 /* skip 128 bytes to next line. This is filler, used to avoid line borders occuring in the middle of 512
240 fb += 64; /* 128 bytes = 64 int16_t */
243 /* swap buffers (under lock protection) */
244 // device_buffer = (device_buffer ? 0 : 1);
253 const int timeout_msecs = 1000;
256 if ((err = libusb_bulk_transfer (handle, 0x01, frame_header, sizeof (frame_header), &transferred, timeout_msecs))) {
261 Glib::Threads::Mutex::Lock lm (fb_lock);
263 if ((err = libusb_bulk_transfer (handle, 0x01, (uint8_t*) device_frame_buffer[device_buffer] , 2 * rows * pixels_per_row, &transferred, timeout_msecs))) {
272 Push2::set_active (bool yn)
274 DEBUG_TRACE (DEBUG::Push2, string_compose("Push2Protocol::set_active init with yn: '%1'\n", yn));
276 if (yn == active()) {
283 DEBUG_TRACE (DEBUG::Push2, "device open failed\n");
288 /* start event loop */
292 // connect_session_signals ();
296 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (frame_buffer);
298 cerr << "Cannot create context\n";
301 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
303 cerr << "Cannot create layout\n";
307 layout->set_text ("hello, Ardour");
308 Pango::FontDescription fd ("Sans Bold 12");
309 layout->set_font_description (fd);
311 context->set_source_rgb (0.0, 1.0, 1.0);
312 context->rectangle (0, 0, 960, 160);
314 context->set_source_rgb (0.0, 0.0, 0.0);
315 context->rectangle (50, 50, 860, 60);
317 context->move_to (60, 60);
318 context->set_source_rgb ((random()%255) / 255.0, (random()%255) / 255.0, (random()%255) / 255.0);
319 layout->update_from_cairo_context (context);
320 layout->show_in_cairo_context (context);
324 /* set up periodic task used to push a frame buffer to the
325 * device (25fps). The device can handle 60fps, but we don't
326 * need that frame rate.
329 Glib::RefPtr<Glib::TimeoutSource> vblank_timeout = Glib::TimeoutSource::create (40); // milliseconds
330 vblank_connection = vblank_timeout->connect (sigc::mem_fun (*this, &Push2::vblank));
331 vblank_timeout->attach (main_loop()->get_context());
340 ControlProtocol::set_active (yn);
342 DEBUG_TRACE (DEBUG::Push2, string_compose("Push2Protocol::set_active done with yn: '%1'\n", yn));
348 Push2::write (int port, const MidiByteArray& data)
350 /* immediate delivery */
351 _output_port[port]->write (&data[0], data.size(), 0);