/* * 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.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; } Push2::Push2 (ARDOUR::Session& s) : ControlProtocol (s, std::string (X_("Ableton Push 2"))) , AbstractUI (name()) , _handle (0) , _in_use (false) , _modifier_state (None) , _splash_start (0) , _current_layout (0) , _previous_layout (0) , _connection_state (ConnectionState (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 (); /* Ports exist for the life of this instance */ ports_acquire (); /* catch arrival and departure of Push2 itself */ ARDOUR::AudioEngine::instance()->PortRegisteredOrUnregistered.connect (port_connections, MISSING_INVALIDATOR, boost::bind (&Push2::port_registration_handler, this), this); /* Catch port connections and disconnections */ ARDOUR::AudioEngine::instance()->PortConnectedOrDisconnected.connect (port_connections, MISSING_INVALIDATOR, boost::bind (&Push2::connection_handler, this, _1, _2, _3, _4, _5), this); /* Push 2 ports might already be there */ port_registration_handler (); } Push2::~Push2 () { DEBUG_TRACE (DEBUG::Push2, "push2 control surface object being destroyed\n"); /* do this before stopping the event loop, so that we don't get any notifications */ port_connections.drop_connections (); stop_using_device (); device_release (); ports_release (); 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; stop_event_loop (); } 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()); connect_session_signals (); init_buttons (true); init_touch_strip (); 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 (); _in_use = true; return 0; } 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 (); session_connections.drop_connections (); _in_use = false; return 0; } int Push2::ports_acquire () { DEBUG_TRACE (DEBUG::Push2, "acquiring ports\n"); /* setup ports */ _async_in = AudioEngine::instance()->register_input_port (DataType::MIDI, X_("Push 2 in"), true); _async_out = AudioEngine::instance()->register_output_port (DataType::MIDI, X_("Push 2 out"), true); if (_async_in == 0 || _async_out == 0) { DEBUG_TRACE (DEBUG::Push2, "cannot register ports\n"); return -1; } /* We do not add our ports to the input/output bundles because we don't * want users wiring them by hand. They could use JACK tools if they * really insist on that (and use JACK) */ _input_port = boost::dynamic_pointer_cast(_async_in).get(); _output_port = boost::dynamic_pointer_cast(_async_out).get(); /* Create a shadow port where, depending on the state of the surface, * we will make pad note on/off events appear. The surface code will * automatically this port to the first selected MIDI track. */ boost::dynamic_pointer_cast(_async_in)->add_shadow_port (string_compose (_("%1 Pads"), X_("Push 2")), boost::bind (&Push2::pad_filter, this, _1, _2)); boost::shared_ptr shadow_port = boost::dynamic_pointer_cast(_async_in)->shadow_port(); if (shadow_port) { _output_bundle.reset (new ARDOUR::Bundle (_("Push 2 Pads"), false)); _output_bundle->add_channel ( shadow_port->name(), ARDOUR::DataType::MIDI, session->engine().make_port_name_non_relative (shadow_port->name()) ); } session->BundleAddedOrRemoved (); connect_to_parser (); /* Connect input port to event loop */ AsyncMIDIPort* asp; asp = dynamic_cast (_input_port); asp->xthread().set_receive_handler (sigc::bind (sigc::mem_fun (this, &Push2::midi_input_handler), _input_port)); asp->xthread().attach (main_loop()->get_context()); return 0; } void Push2::ports_release () { DEBUG_TRACE (DEBUG::Push2, "releasing ports\n"); /* wait for button data to be flushed */ AsyncMIDIPort* asp; asp = dynamic_cast (_output_port); asp->drain (10000, 500000); { Glib::Threads::Mutex::Lock em (AudioEngine::instance()->process_lock()); AudioEngine::instance()->unregister_port (_async_in); AudioEngine::instance()->unregister_port (_async_out); } _async_in.reset ((ARDOUR::Port*) 0); _async_out.reset ((ARDOUR::Port*) 0); _input_port = 0; _output_port = 0; } 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; } } list > Push2::bundles () { list > b; if (_output_bundle) { b.push_back (_output_bundle); } return b; } 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) { boost::shared_ptr