/* * Copyright (C) 2016-2018 Paul Davis * Copyright (C) 2017-2018 Robin Gareus * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include #include #include #include #include "pbd/compose.h" #include "pbd/convert.h" #include "pbd/debug.h" #include "pbd/failed_constructor.h" #include "pbd/file_utils.h" #include "pbd/search_path.h" #include "pbd/enumwriter.h" #include "midi++/parser.h" #include "temporal/time.h" #include "temporal/bbt_time.h" #include "ardour/amp.h" #include "ardour/async_midi_port.h" #include "ardour/audioengine.h" #include "ardour/debug.h" #include "ardour/midiport_manager.h" #include "ardour/midi_track.h" #include "ardour/midi_port.h" #include "ardour/session.h" #include "ardour/tempo.h" #include "ardour/types_convert.h" #include "gtkmm2ext/gui_thread.h" #include "gtkmm2ext/rgb_macros.h" #include "gtkmm2ext/colors.h" #include "canvas.h" #include "cues.h" #include "gui.h" #include "layout.h" #include "mix.h" #include "push2.h" #include "scale.h" #include "splash.h" #include "track_mix.h" #include "pbd/i18n.h" #ifdef PLATFORM_WINDOWS #define random() rand() #endif using namespace ARDOUR; using namespace PBD; using namespace Glib; using namespace ArdourSurface; using namespace Gtkmm2ext; #include "pbd/abstract_ui.inc.cc" // instantiate template #define ABLETON 0x2982 #define PUSH2 0x1967 static int row_interval_semitones (const Push2::RowInterval row_interval, const bool inkey) { switch (row_interval) { case Push2::Third: return 4; case Push2::Fourth: return 5; case Push2::Fifth: return 7; case Push2::Sequential: return inkey ? 12 : 8; } return 5; } bool Push2::available () { bool rv = LIBUSB_SUCCESS == libusb_init (0); if (rv) { libusb_exit (0); } return rv; } bool Push2::match_usb (uint16_t vendor, uint16_t device) { return vendor == ABLETON && device == PUSH2; } bool Push2::probe (std::string& i, std::string& o) { vector midi_inputs; vector midi_outputs; AudioEngine::instance()->get_ports ("", DataType::MIDI, PortFlags (IsOutput|IsTerminal), midi_inputs); AudioEngine::instance()->get_ports ("", DataType::MIDI, PortFlags (IsInput|IsTerminal), midi_outputs); auto has_push2 = [](string const& s) { std::string pn = AudioEngine::instance()->get_hardware_port_name_by_name (s); return pn.find ("Ableton Push 2 MIDI 1") != string::npos; }; auto pi = std::find_if (midi_inputs.begin (), midi_inputs.end (), has_push2); auto po = std::find_if (midi_outputs.begin (), midi_outputs.end (), has_push2); if (pi == midi_inputs.end () || po == midi_outputs.end ()) { return false; } i = *pi; o = *po; return true; } Push2::Push2 (ARDOUR::Session& s) : MIDISurface (s, X_("Ableton Push 2"), X_("Push 2"), true) , _handle (0) , _modifier_state (None) , _splash_start (0) , _current_layout (0) , _previous_layout (0) , _gui (0) , _mode (MusicalMode::IonianMajor) , _row_interval (Fourth) , _scale_root (0) , _root_octave (3) , _in_key (true) , _octave_shift (0) , _percussion (false) , _pressure_mode (AfterTouch) , _selection_color (LED::Green) , _contrast_color (LED::Green) , _in_range_select (false) , _stop_down (false) { /* we're going to need this */ libusb_init (NULL); build_maps (); build_color_map (); fill_color_table (); /* master cannot be removed, so no need to connect to going-away signal */ _master = session->master_out (); /* allocate graphics layouts, even though we're not using them yet */ _canvas = new Push2Canvas (*this, 960, 160); _mix_layout = new MixLayout (*this, *session, "globalmix"); _scale_layout = new ScaleLayout (*this, *session, "scale"); _track_mix_layout = new TrackMixLayout (*this, *session, "trackmix"); _cue_layout = new CueLayout (*this, *session, "cues"); _splash_layout = new SplashLayout (*this, *session, "splash"); run_event_loop (); port_setup (); std::string pn_in, pn_out; if (probe (pn_in, pn_out)) { _async_in->connect (pn_in); _async_out->connect (pn_out); } } Push2::~Push2 () { DEBUG_TRACE (DEBUG::Push2, "push2 control surface object being destroyed\n"); stop_event_loop (); MIDISurface::drop (); if (_current_layout) { _canvas->root()->remove (_current_layout); _current_layout = 0; } delete _mix_layout; _mix_layout = 0; delete _scale_layout; _scale_layout = 0; delete _splash_layout; _splash_layout = 0; delete _track_mix_layout; _track_mix_layout = 0; delete _cue_layout; _cue_layout = 0; } void Push2::run_event_loop () { DEBUG_TRACE (DEBUG::Push2, "start event loop\n"); BaseUI::run (); } void Push2::stop_event_loop () { DEBUG_TRACE (DEBUG::Push2, "stop event loop\n"); BaseUI::quit (); } int Push2::begin_using_device () { DEBUG_TRACE (DEBUG::Push2, "begin using device\n"); /* set up periodic task used to push a frame buffer to the * device (25fps). The device can handle 60fps, but we don't * need that frame rate. */ Glib::RefPtr vblank_timeout = Glib::TimeoutSource::create (40); // milliseconds _vblank_connection = vblank_timeout->connect (sigc::mem_fun (*this, &Push2::vblank)); vblank_timeout->attach (main_loop()->get_context()); init_buttons (true); init_touch_strip (false); reset_pad_colors (); splash (); /* catch current selection, if any so that we can wire up the pads if appropriate */ stripable_selection_changed (); request_pressure_mode (); return MIDISurface::begin_using_device (); } int Push2::stop_using_device () { DEBUG_TRACE (DEBUG::Push2, "stop using device\n"); if (!_in_use) { DEBUG_TRACE (DEBUG::Push2, "nothing to do, device not in use\n"); return 0; } init_buttons (false); strip_buttons_off (); for (auto & pad : _xy_pad_map) { pad->set_color (LED::Black); pad->set_state (LED::NoTransition); write (pad->state_msg()); } _vblank_connection.disconnect (); return MIDISurface::stop_using_device (); } int Push2::device_acquire () { int err; DEBUG_TRACE (DEBUG::Push2, "acquiring device\n"); if (_handle) { DEBUG_TRACE (DEBUG::Push2, "open() called with handle already set\n"); /* already open */ return 0; } if ((_handle = libusb_open_device_with_vid_pid (NULL, ABLETON, PUSH2)) == 0) { DEBUG_TRACE (DEBUG::Push2, "failed to open USB handle\n"); return -1; } if ((err = libusb_claim_interface (_handle, 0x00))) { DEBUG_TRACE (DEBUG::Push2, "failed to claim USB device\n"); libusb_close (_handle); _handle = 0; return -1; } return 0; } void Push2::device_release () { DEBUG_TRACE (DEBUG::Push2, "releasing device\n"); if (_handle) { libusb_release_interface (_handle, 0x00); libusb_close (_handle); _handle = 0; } } void Push2::strip_buttons_off () { ButtonID strip_buttons[] = { Upper1, Upper2, Upper3, Upper4, Upper5, Upper6, Upper7, Upper8, Lower1, Lower2, Lower3, Lower4, Lower5, Lower6, Lower7, Lower8, }; for (size_t n = 0; n < sizeof (strip_buttons) / sizeof (strip_buttons[0]); ++n) { std::shared_ptr