2 Copyright (C) 2006 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.
25 #include <glibmm/fileutils.h>
26 #include <glibmm/miscutils.h>
28 #include "pbd/controllable_descriptor.h"
29 #include "pbd/error.h"
30 #include "pbd/failed_constructor.h"
31 #include "pbd/file_utils.h"
32 #include "pbd/xml++.h"
33 #include "pbd/compose.h"
35 #include "midi++/port.h"
37 #include "ardour/audioengine.h"
38 #include "ardour/filesystem_paths.h"
39 #include "ardour/session.h"
40 #include "ardour/route.h"
41 #include "ardour/midi_ui.h"
42 #include "ardour/midi_port.h"
43 #include "ardour/rc_configuration.h"
44 #include "ardour/midiport_manager.h"
45 #include "ardour/debug.h"
46 #include "ardour/async_midi_port.h"
48 #include "faderport.h"
50 using namespace ARDOUR;
51 using namespace ArdourSurface;
58 #define midi_ui_context() MidiControlUI::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
60 FaderPort::FaderPort (Session& s)
61 : ControlProtocol (s, _("Faderport"))
65 , connection_state (ConnectionState (0))
66 , _device_active (false)
70 boost::shared_ptr<ARDOUR::Port> inp;
71 boost::shared_ptr<ARDOUR::Port> outp;
73 inp = AudioEngine::instance()->register_input_port (DataType::MIDI, "Faderport Recv", true);
74 outp = AudioEngine::instance()->register_output_port (DataType::MIDI, "Faderport Send", true);
76 _input_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(inp);
77 _output_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(outp);
79 if (_input_port == 0 || _output_port == 0) {
80 throw failed_constructor();
84 _feedback_interval = 10 * 1000; // microseconds
85 last_feedback_time = 0;
91 /* handle device inquiry response */
92 _input_port->parser()->sysex.connect_same_thread (midi_connections, boost::bind (&FaderPort::sysex_handler, this, _1, _2, _3));
94 _input_port->parser()->poly_pressure.connect_same_thread (midi_connections, boost::bind (&FaderPort::switch_handler, this, _1, _2));
96 _input_port->parser()->pitchbend.connect_same_thread (midi_connections, boost::bind (&FaderPort::encoder_handler, this, _1, _2));
98 _input_port->parser()->controller.connect_same_thread (midi_connections, boost::bind (&FaderPort::fader_handler, this, _1, _2));
100 /* This connection means that whenever data is ready from the input
101 * port, the relevant thread will invoke our ::midi_input_handler()
102 * method, which will read the data, and invoke the parser.
105 _input_port->xthread().set_receive_handler (sigc::bind (sigc::mem_fun (this, &FaderPort::midi_input_handler), _input_port));
106 _input_port->xthread().attach (midi_ui_context()->main_loop()->get_context());
108 Session::SendFeedback.connect_same_thread (*this, boost::bind (&FaderPort::send_feedback, this));
109 //Session::SendFeedback.connect (*this, MISSING_INVALIDATOR, boost::bind (&FaderPort::send_feedback, this), midi_ui_context());;
111 /* this one is cross-thread */
113 Route::RemoteControlIDChange.connect (*this, MISSING_INVALIDATOR, boost::bind (&FaderPort::reset_controllables, this), midi_ui_context());
115 /* Catch port connections and disconnections */
116 ARDOUR::AudioEngine::instance()->PortConnectedOrDisconnected.connect (port_connection, MISSING_INVALIDATOR, boost::bind (&FaderPort::connection_handler, this, _1, _2, _3, _4, _5), midi_ui_context());
118 buttons.insert (std::make_pair (18, ButtonID (_("Mute"), 18, 21)));
119 buttons.insert (std::make_pair (17, ButtonID (_("Solo"), 17, 22)));
120 buttons.insert (std::make_pair (16, ButtonID (_("Rec"), 16, 23)));
121 buttons.insert (std::make_pair (19, ButtonID (_("Left"), 19, 20)));
122 buttons.insert (std::make_pair (20, ButtonID (_("Bank"), 20, 19)));
123 buttons.insert (std::make_pair (21, ButtonID (_("Right"), 21, 18)));
124 buttons.insert (std::make_pair (22, ButtonID (_("Output"), 22, 17)));
125 buttons.insert (std::make_pair (10, ButtonID (_("Read"), 10, 13)));
126 buttons.insert (std::make_pair (9, ButtonID (_("Write"), 9, 14)));
127 buttons.insert (std::make_pair (8, ButtonID (_("Touch"), 8, 15)));
128 buttons.insert (std::make_pair (23, ButtonID (_("Off"), 23, 16)));
129 buttons.insert (std::make_pair (11, ButtonID (_("Mix"), 11, 12)));
130 buttons.insert (std::make_pair (12, ButtonID (_("Proj"), 12, 11)));
131 buttons.insert (std::make_pair (13, ButtonID (_("Trns"), 13, 10)));
132 buttons.insert (std::make_pair (14, ButtonID (_("Undo"), 14, 9)));
133 buttons.insert (std::make_pair (2, ButtonID (_("Shift"), 2, 5)));
134 buttons.insert (std::make_pair (1, ButtonID (_("Punch"), 1, 6)));
135 buttons.insert (std::make_pair (0, ButtonID (_("User"), 0, 7)));
136 buttons.insert (std::make_pair (15, ButtonID (_("Loop"), 15, 8)));
137 buttons.insert (std::make_pair (3, ButtonID (_("Rewind"), 3, 4)));
138 buttons.insert (std::make_pair (4, ButtonID (_("Ffwd"), 4, 3)));
139 buttons.insert (std::make_pair (5, ButtonID (_("Stop"), 5, 2)));
140 buttons.insert (std::make_pair (6, ButtonID (_("Play"), 6, 1)));
141 buttons.insert (std::make_pair (7, ButtonID (_("RecEnable"), 7, 0)));
142 buttons.insert (std::make_pair (127, ButtonID (_("Fader (touch)"), 127, -1)));
145 FaderPort::~FaderPort ()
148 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("unregistering input port %1\n", boost::shared_ptr<ARDOUR::Port>(_input_port)->name()));
149 AudioEngine::instance()->unregister_port (_input_port);
150 _input_port.reset ();
154 // _output_port->drain (10000); //ToDo: is this necessary? It hangs the shutdown, for me
155 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("unregistering output port %1\n", boost::shared_ptr<ARDOUR::Port>(_output_port)->name()));
156 AudioEngine::instance()->unregister_port (_output_port);
157 _output_port.reset ();
164 FaderPort::switch_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
166 map<int,ButtonID>::const_iterator b = buttons.find (tb->controller_number);
168 if (b != buttons.end()) {
170 cerr << b->second.name << endl;
172 if (b->second.out >= 0) {
173 /* send feedback to turn on the LED */
177 buf[1] = b->second.out;
180 _output_port->write (buf, 3, 0);
186 FaderPort::encoder_handler (MIDI::Parser &, MIDI::pitchbend_t pb)
189 cerr << "Encoder right\n";
191 cerr << "Encoder left\n";
196 FaderPort::fader_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
198 bool was_fader = false;
200 if (tb->controller_number == 0x0) {
201 fader_msb = tb->value;
203 } else if (tb->controller_number == 0x20) {
204 fader_lsb = tb->value;
209 cerr << "Fader now at " << ((fader_msb<<7)|fader_lsb) << endl;
214 FaderPort::sysex_handler (MIDI::Parser &p, MIDI::byte *buf, size_t sz)
220 if (buf[2] == 0x7f &&
230 _device_active = true;
232 cerr << "FaderPort identified\n";
234 /* put it into native mode */
236 MIDI::byte native[3];
241 _output_port->write (native, 3, 0);
246 FaderPort::set_active (bool /*yn*/)
252 FaderPort::set_feedback_interval (microseconds_t ms)
254 _feedback_interval = ms;
258 FaderPort::send_feedback ()
260 /* This is executed in RT "process" context", so no blocking calls
267 microseconds_t now = get_microseconds ();
269 if (last_feedback_time != 0) {
270 if ((now - last_feedback_time) < _feedback_interval) {
275 last_feedback_time = now;
279 FaderPort::midi_input_handler (Glib::IOCondition ioc, boost::shared_ptr<ARDOUR::AsyncMIDIPort> port)
281 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("something happend on %1\n", boost::shared_ptr<MIDI::Port>(port)->name()));
293 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("data available on %1\n", boost::shared_ptr<MIDI::Port>(port)->name()));
294 framepos_t now = session->engine().sample_time();
303 FaderPort::get_state ()
305 XMLNode& node (ControlProtocol::get_state());
309 child = new XMLNode (X_("Input"));
310 child->add_child_nocopy (boost::shared_ptr<ARDOUR::Port>(_input_port)->get_state());
311 node.add_child_nocopy (*child);
314 child = new XMLNode (X_("Output"));
315 child->add_child_nocopy (boost::shared_ptr<ARDOUR::Port>(_output_port)->get_state());
316 node.add_child_nocopy (*child);
322 FaderPort::set_state (const XMLNode& node, int version)
325 XMLNodeConstIterator niter;
326 XMLNode const* child;
328 if (ControlProtocol::set_state (node, version)) {
332 if ((child = node.child (X_("Input"))) != 0) {
333 XMLNode* portnode = child->child (Port::state_node_name.c_str());
335 boost::shared_ptr<ARDOUR::Port>(_input_port)->set_state (*portnode, version);
339 if ((child = node.child (X_("Output"))) != 0) {
340 XMLNode* portnode = child->child (Port::state_node_name.c_str());
342 boost::shared_ptr<ARDOUR::Port>(_output_port)->set_state (*portnode, version);
350 FaderPort::set_feedback (bool yn)
353 last_feedback_time = 0;
358 FaderPort::get_feedback () const
364 FaderPort::set_current_bank (uint32_t b)
367 // reset_controllables ();
371 FaderPort::next_bank ()
374 // reset_controllables ();
378 FaderPort::prev_bank()
382 // reset_controllables ();
387 FaderPort::set_motorised (bool m)
393 FaderPort::set_threshold (int t)
399 FaderPort::reset_controllables ()
404 FaderPort::connection_handler (boost::weak_ptr<ARDOUR::Port>, std::string name1, boost::weak_ptr<ARDOUR::Port>, std::string name2, bool yn)
406 if (!_input_port || !_output_port) {
410 string ni = ARDOUR::AudioEngine::instance()->make_port_name_non_relative (boost::shared_ptr<ARDOUR::Port>(_input_port)->name());
411 string no = ARDOUR::AudioEngine::instance()->make_port_name_non_relative (boost::shared_ptr<ARDOUR::Port>(_output_port)->name());
413 if (ni == name1 || ni == name2) {
415 connection_state |= InputConnected;
417 connection_state &= ~InputConnected;
419 } else if (no == name1 || no == name2) {
421 connection_state |= OutputConnected;
423 connection_state &= ~OutputConnected;
430 if ((connection_state & (InputConnected|OutputConnected)) == (InputConnected|OutputConnected)) {
432 /* XXX this is a horrible hack. Without a short sleep here,
433 something prevents the device wakeup messages from being
434 sent and/or the responses from being received.
441 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 disconnected (input or output or both)\n", _name));
442 _device_active = false;
445 return true; /* connection status changed */
449 FaderPort::connected ()
451 std::cerr << "faderport connected\n";
453 /* send device inquiry */
464 _output_port->write (buf, 6, 0);