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;
57 #define midi_ui_context() MidiControlUI::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
59 FaderPort::FaderPort (Session& s)
60 : ControlProtocol (s, _("Faderport"))
64 , connection_state (ConnectionState (0))
65 , _device_active (false)
69 boost::shared_ptr<ARDOUR::Port> inp;
70 boost::shared_ptr<ARDOUR::Port> outp;
72 inp = AudioEngine::instance()->register_input_port (DataType::MIDI, "Faderport Recv", true);
73 outp = AudioEngine::instance()->register_output_port (DataType::MIDI, "Faderport Send", true);
75 _input_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(inp);
76 _output_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(outp);
78 if (_input_port == 0 || _output_port == 0) {
79 throw failed_constructor();
83 _feedback_interval = 10 * 1000; // microseconds
84 last_feedback_time = 0;
90 /* handle device inquiry response */
91 _input_port->parser()->sysex.connect_same_thread (midi_connections, boost::bind (&FaderPort::sysex_handler, this, _1, _2, _3));
93 _input_port->parser()->poly_pressure.connect_same_thread (midi_connections, boost::bind (&FaderPort::switch_handler, this, _1, _2));
95 _input_port->parser()->pitchbend.connect_same_thread (midi_connections, boost::bind (&FaderPort::encoder_handler, this, _1, _2));
97 _input_port->parser()->controller.connect_same_thread (midi_connections, boost::bind (&FaderPort::fader_handler, this, _1, _2));
99 /* This connection means that whenever data is ready from the input
100 * port, the relevant thread will invoke our ::midi_input_handler()
101 * method, which will read the data, and invoke the parser.
104 _input_port->xthread().set_receive_handler (sigc::bind (sigc::mem_fun (this, &FaderPort::midi_input_handler), _input_port));
105 _input_port->xthread().attach (midi_ui_context()->main_loop()->get_context());
107 Session::SendFeedback.connect_same_thread (*this, boost::bind (&FaderPort::send_feedback, this));
108 //Session::SendFeedback.connect (*this, MISSING_INVALIDATOR, boost::bind (&FaderPort::send_feedback, this), midi_ui_context());;
110 /* this one is cross-thread */
112 Route::RemoteControlIDChange.connect (*this, MISSING_INVALIDATOR, boost::bind (&FaderPort::reset_controllables, this), midi_ui_context());
114 /* Catch port connections and disconnections */
115 ARDOUR::AudioEngine::instance()->PortConnectedOrDisconnected.connect (port_connection, MISSING_INVALIDATOR, boost::bind (&FaderPort::connection_handler, this, _1, _2, _3, _4, _5), midi_ui_context());
119 FaderPort::~FaderPort ()
122 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("unregistering input port %1\n", boost::shared_ptr<ARDOUR::Port>(_input_port)->name()));
123 AudioEngine::instance()->unregister_port (_input_port);
124 _input_port.reset ();
128 // _output_port->drain (10000); //ToDo: is this necessary? It hangs the shutdown, for me
129 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("unregistering output port %1\n", boost::shared_ptr<ARDOUR::Port>(_output_port)->name()));
130 AudioEngine::instance()->unregister_port (_output_port);
131 _output_port.reset ();
138 FaderPort::switch_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
140 switch (tb->controller_number) {
211 cerr << "RecEnable\n";
214 cerr << "Fader touch\n";
220 /* send feedback to turn on the LED */
224 buf[1] = tb->controller_number;
227 _output_port->write (buf, 3, 0);
231 FaderPort::encoder_handler (MIDI::Parser &, MIDI::pitchbend_t pb)
234 cerr << "Encoder right\n";
236 cerr << "Encoder left\n";
241 FaderPort::fader_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
243 bool was_fader = false;
245 if (tb->controller_number == 0x0) {
246 fader_msb = tb->value;
248 } else if (tb->controller_number == 0x20) {
249 fader_lsb = tb->value;
254 cerr << "Fader now at " << ((fader_msb<<7)|fader_lsb) << endl;
259 FaderPort::sysex_handler (MIDI::Parser &p, MIDI::byte *buf, size_t sz)
265 if (buf[2] == 0x7f &&
275 _device_active = true;
277 cerr << "FaderPort identified\n";
279 /* put it into native mode */
281 MIDI::byte native[3];
286 _output_port->write (native, 3, 0);
291 FaderPort::set_active (bool /*yn*/)
297 FaderPort::set_feedback_interval (microseconds_t ms)
299 _feedback_interval = ms;
303 FaderPort::send_feedback ()
305 /* This is executed in RT "process" context", so no blocking calls
312 microseconds_t now = get_microseconds ();
314 if (last_feedback_time != 0) {
315 if ((now - last_feedback_time) < _feedback_interval) {
320 last_feedback_time = now;
324 FaderPort::midi_input_handler (Glib::IOCondition ioc, boost::shared_ptr<ARDOUR::AsyncMIDIPort> port)
326 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("something happend on %1\n", boost::shared_ptr<MIDI::Port>(port)->name()));
338 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("data available on %1\n", boost::shared_ptr<MIDI::Port>(port)->name()));
339 framepos_t now = session->engine().sample_time();
348 FaderPort::get_state ()
350 XMLNode& node (ControlProtocol::get_state());
354 child = new XMLNode (X_("Input"));
355 child->add_child_nocopy (boost::shared_ptr<ARDOUR::Port>(_input_port)->get_state());
356 node.add_child_nocopy (*child);
359 child = new XMLNode (X_("Output"));
360 child->add_child_nocopy (boost::shared_ptr<ARDOUR::Port>(_output_port)->get_state());
361 node.add_child_nocopy (*child);
367 FaderPort::set_state (const XMLNode& node, int version)
370 XMLNodeConstIterator niter;
371 XMLNode const* child;
373 if (ControlProtocol::set_state (node, version)) {
377 if ((child = node.child (X_("Input"))) != 0) {
378 XMLNode* portnode = child->child (Port::state_node_name.c_str());
380 boost::shared_ptr<ARDOUR::Port>(_input_port)->set_state (*portnode, version);
384 if ((child = node.child (X_("Output"))) != 0) {
385 XMLNode* portnode = child->child (Port::state_node_name.c_str());
387 boost::shared_ptr<ARDOUR::Port>(_output_port)->set_state (*portnode, version);
395 FaderPort::set_feedback (bool yn)
398 last_feedback_time = 0;
403 FaderPort::get_feedback () const
409 FaderPort::set_current_bank (uint32_t b)
412 // reset_controllables ();
416 FaderPort::next_bank ()
419 // reset_controllables ();
423 FaderPort::prev_bank()
427 // reset_controllables ();
432 FaderPort::set_motorised (bool m)
438 FaderPort::set_threshold (int t)
444 FaderPort::reset_controllables ()
449 FaderPort::connection_handler (boost::weak_ptr<ARDOUR::Port>, std::string name1, boost::weak_ptr<ARDOUR::Port>, std::string name2, bool yn)
451 if (!_input_port || !_output_port) {
455 string ni = ARDOUR::AudioEngine::instance()->make_port_name_non_relative (boost::shared_ptr<ARDOUR::Port>(_input_port)->name());
456 string no = ARDOUR::AudioEngine::instance()->make_port_name_non_relative (boost::shared_ptr<ARDOUR::Port>(_output_port)->name());
458 std::cerr << "Checking " << name1 << (yn ? " + " : " - " ) << name2 << " vs. " << ni << " & " << no << std::endl;
460 if (ni == name1 || ni == name2) {
462 connection_state |= InputConnected;
464 connection_state &= ~InputConnected;
466 } else if (no == name1 || no == name2) {
468 connection_state |= OutputConnected;
470 connection_state &= ~OutputConnected;
477 if ((connection_state & (InputConnected|OutputConnected)) == (InputConnected|OutputConnected)) {
479 /* XXX this is a horrible hack. Without a short sleep here,
480 something prevents the device wakeup messages from being
481 sent and/or the responses from being received.
488 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 disconnected (input or output or both)\n", _name));
489 _device_active = false;
492 return true; /* connection status changed */
496 FaderPort::connected ()
498 std::cerr << "faderport connected\n";
500 /* send device inquiry */
511 _output_port->write (buf, 6, 0);