2 Copyright (C) 2006,2007 John Anderson
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.
18 #include "mackie_port.h"
20 #include "mackie_control_exception.h"
21 #include "mackie_control_protocol.h"
22 #include "mackie_midi_builder.h"
26 #include <glibmm/main.h>
28 #include <boost/shared_array.hpp>
30 #include "midi++/types.h"
31 #include "midi++/port.h"
33 #include "ardour/debug.h"
34 #include "ardour/rc_configuration.h"
41 using namespace Mackie;
42 using namespace ARDOUR;
45 // The MCU sysex header
46 MidiByteArray mackie_sysex_hdr (5, MIDI::sysex, 0x0, 0x0, 0x66, 0x10);
48 // The MCU extender sysex header
49 MidiByteArray mackie_sysex_hdr_xt (5, MIDI::sysex, 0x0, 0x0, 0x66, 0x11);
51 MackiePort::MackiePort (MackieControlProtocol & mcp, MIDI::Port & input_port, MIDI::Port & output_port, int number, port_type_t port_type)
52 : SurfacePort (input_port, output_port, number)
54 , _port_type (port_type)
56 , _initialising (true)
59 DEBUG_TRACE (DEBUG::MackieControl, "MackiePort::MackiePort\n");
62 MackiePort::~MackiePort()
64 DEBUG_TRACE (DEBUG::MackieControl, "MackiePort::~MackiePort\n");
66 DEBUG_TRACE (DEBUG::MackieControl, "~MackiePort finished\n");
69 int MackiePort::strips() const
71 if (_port_type == mcu)
75 // BCF2000 only has 8 faders, so reserve one for master
76 case bcf2000: return 7;
77 case mackie: return 8;
80 throw MackieControlException ("MackiePort::strips: don't know what emulation we're using");
85 // must be an extender, ie no master fader
90 // should really be in MackiePort
91 void MackiePort::open()
93 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackiePort::open %1\n", *this));
95 input_port().parser()->sysex.connect_same_thread (sysex_connection, boost::bind (&MackiePort::handle_midi_sysex, this, _1, _2, _3));
97 // make sure the device is connected
101 void MackiePort::close()
103 DEBUG_TRACE (DEBUG::MackieControl, "MackiePort::close\n");
105 // disconnect signals
107 sysex_connection.disconnect();
108 ScopedConnectionList::drop_connections ();
111 // TODO emit a "closing" signal?
114 const MidiByteArray & MackiePort::sysex_hdr() const
118 case mcu: return mackie_sysex_hdr;
119 case ext: return mackie_sysex_hdr_xt;
121 cout << "MackiePort::sysex_hdr _port_type not known" << endl;
122 return mackie_sysex_hdr;
125 MidiByteArray calculate_challenge_response (MidiByteArray::iterator begin, MidiByteArray::iterator end)
128 back_insert_iterator<MidiByteArray> back (l);
129 copy (begin, end, back);
131 MidiByteArray retval;
133 // this is how to calculate the response to the challenge.
134 // from the Logic docs.
135 retval << (0x7f & (l[0] + (l[1] ^ 0xa) - l[3]));
136 retval << (0x7f & ( (l[2] >> l[3]) ^ (l[0] + l[3])));
137 retval << (0x7f & ((l[3] - (l[2] << 2)) ^ (l[0] | l[1])));
138 retval << (0x7f & (l[1] - l[2] + (0xf0 ^ (l[3] << 4))));
143 // not used right now
144 MidiByteArray MackiePort::host_connection_query (MidiByteArray & bytes)
146 MidiByteArray response;
148 // handle host connection query
149 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("host connection query: %1\n", bytes));
151 if (bytes.size() != 18) {
152 finalise_init (false);
153 cerr << "expecting 18 bytes, read " << bytes << " from " << input_port().name() << endl;
157 // build and send host connection reply
159 copy (bytes.begin() + 6, bytes.begin() + 6 + 7, back_inserter (response));
160 response << calculate_challenge_response (bytes.begin() + 6 + 7, bytes.begin() + 6 + 7 + 4);
164 // not used right now
165 MidiByteArray MackiePort::host_connection_confirmation (const MidiByteArray & bytes)
167 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("host_connection_confirmation: %1\n", bytes));
169 // decode host connection confirmation
170 if (bytes.size() != 14) {
171 finalise_init (false);
173 os << "expecting 14 bytes, read " << bytes << " from " << input_port().name();
174 throw MackieControlException (os.str());
177 // send version request
178 return MidiByteArray (2, 0x13, 0x00);
181 void MackiePort::probe_emulation (const MidiByteArray &)
184 cout << "MackiePort::probe_emulation: " << bytes.size() << ", " << bytes << endl;
186 MidiByteArray version_string;
187 for (int i = 6; i < 11; ++i) version_string << bytes[i];
188 cout << "version_string: " << version_string << endl;
191 // TODO investigate using serial number. Also, possibly size of bytes might
192 // give an indication. Also, apparently MCU sends non-documented messages
196 //cout << "MackiePort::probe_emulation out of sequence." << endl;
200 finalise_init (true);
203 void MackiePort::init()
205 DEBUG_TRACE (DEBUG::MackieControl, "MackiePort::init\n");
208 _initialising = true;
210 DEBUG_TRACE (DEBUG::MackieControl, "MackiePort::init lock acquired\n");
212 // emit pre-init signal
215 // kick off initialisation. See docs in header file for init()
217 // bypass the init sequence because sometimes the first
218 // message doesn't get to the unit, and there's no way
219 // to do a timed lock in Glib.
220 //write_sysex (MidiByteArray (2, 0x13, 0x00));
222 finalise_init (true);
225 void MackiePort::finalise_init (bool yn)
227 DEBUG_TRACE (DEBUG::MackieControl, "MackiePort::finalise_init\n");
229 bool emulation_ok = false;
231 // probing doesn't work very well, so just use a config variable
232 // to set the emulation mode
233 // TODO This might have to be specified on a per-port basis
234 // in the config file
235 // if an mcu and a bcf are needed to work as one surface
236 if (_emulation == none) {
238 // TODO same as code in mackie_control_protocol.cc
239 if (ARDOUR::Config->get_mackie_emulation() == "bcf") {
240 _emulation = bcf2000;
242 } else if (ARDOUR::Config->get_mackie_emulation() == "mcu") {
246 cout << "unknown mackie emulation: " << ARDOUR::Config->get_mackie_emulation() << endl;
247 emulation_ok = false;
251 yn = yn && emulation_ok;
253 SurfacePort::active (yn);
258 // start handling messages from controls
259 connect_to_signals ();
262 _initialising = false;
266 DEBUG_TRACE (DEBUG::MackieControl, "MackiePort::finalise_init lock released\n");
269 void MackiePort::connect_to_signals ()
273 MIDI::Parser* p = input_port().parser();
275 /* V-Pot messages are Controller */
276 p->controller.connect_same_thread (*this, boost::bind (&MackiePort::handle_midi_controller_message, this, _1, _2));
277 /* Button messages are NoteOn */
278 p->note_on.connect_same_thread (*this, boost::bind (&MackiePort::handle_midi_note_on_message, this, _1, _2));
279 /* Fader messages are Pitchbend */
280 p->channel_pitchbend[0].connect_same_thread (*this, boost::bind (&MackiePort::handle_midi_pitchbend_message, this, _1, _2, 0U));
281 p->channel_pitchbend[1].connect_same_thread (*this, boost::bind (&MackiePort::handle_midi_pitchbend_message, this, _1, _2, 1U));
282 p->channel_pitchbend[2].connect_same_thread (*this, boost::bind (&MackiePort::handle_midi_pitchbend_message, this, _1, _2, 2U));
283 p->channel_pitchbend[3].connect_same_thread (*this, boost::bind (&MackiePort::handle_midi_pitchbend_message, this, _1, _2, 3U));
284 p->channel_pitchbend[4].connect_same_thread (*this, boost::bind (&MackiePort::handle_midi_pitchbend_message, this, _1, _2, 4U));
285 p->channel_pitchbend[5].connect_same_thread (*this, boost::bind (&MackiePort::handle_midi_pitchbend_message, this, _1, _2, 5U));
286 p->channel_pitchbend[6].connect_same_thread (*this, boost::bind (&MackiePort::handle_midi_pitchbend_message, this, _1, _2, 6U));
287 p->channel_pitchbend[7].connect_same_thread (*this, boost::bind (&MackiePort::handle_midi_pitchbend_message, this, _1, _2, 7U));
293 bool MackiePort::wait_for_init()
295 Glib::Mutex::Lock lock (init_mutex);
296 while (_initialising) {
297 DEBUG_TRACE (DEBUG::MackieControl, "MackiePort::wait_for_active waiting\n");
298 init_cond.wait (init_mutex);
299 DEBUG_TRACE (DEBUG::MackieControl, "MackiePort::wait_for_active released\n");
301 DEBUG_TRACE (DEBUG::MackieControl, "MackiePort::wait_for_active returning\n");
302 return SurfacePort::active();
305 void MackiePort::handle_midi_sysex (MIDI::Parser &, MIDI::byte * raw_bytes, size_t count)
307 MidiByteArray bytes (count, raw_bytes);
309 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("handle_midi_sysex: %1\n", bytes));
314 write_sysex (host_connection_query (bytes));
317 // not used right now
318 write_sysex (host_connection_confirmation (bytes));
322 cout << "host connection error" << bytes << endl;
325 probe_emulation (bytes);
328 cout << "unknown sysex: " << bytes << endl;
333 MackiePort::handle_midi_pitchbend_message (MIDI::Parser&, MIDI::pitchbend_t pb, uint32_t fader_id)
335 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("handle_midi pitchbend on port %3 (number %4), fader = %1 value = %2\n",
336 (8*number()) + fader_id, pb, *this, number()));
338 Control* control = _mcp.surface().faders[(8*number()) + fader_id];
341 float midi_pos = pb >> 4; // only the top 10 bytes are used
342 _mcp.handle_control_event (*this, *control, midi_pos / 1023.0);
344 DEBUG_TRACE (DEBUG::MackieControl, "fader not found\n");
349 MackiePort::handle_midi_note_on_message (MIDI::Parser &, MIDI::EventTwoBytes* ev)
351 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackiePort::handle_note_on %1 = %2\n", ev->note_number, ev->velocity));
353 Control* control = _mcp.surface().buttons[(8*number()) + ev->note_number];
356 ControlState control_state (ev->velocity == 0x7f ? press : release);
357 control->set_in_use (control_state.button_state == press);
358 control_event (*this, *control, control_state);
360 DEBUG_TRACE (DEBUG::MackieControl, "button not found\n");
365 MackiePort::handle_midi_controller_message (MIDI::Parser &, MIDI::EventTwoBytes* ev)
367 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackiePort::handle_midi_controller %1 = %2\n", ev->controller_number, ev->value));
369 Control* control = _mcp.surface().pots[(8*number()) + ev->controller_number];
371 if (!control && ev->controller_number == Control::jog_base_id) {
372 control = _mcp.surface().controls_by_name["jog"];
378 // bytes[2] & 0b01000000 (0x40) give sign
379 state.sign = (ev->value & 0x40) == 0 ? 1 : -1;
380 // bytes[2] & 0b00111111 (0x3f) gives delta
381 state.ticks = (ev->value & 0x3f);
382 if (state.ticks == 0) {
383 /* euphonix and perhaps other devices send zero
384 when they mean 1, we think.
388 state.delta = float (state.ticks) / float (0x3f);
390 /* Pots only emit events when they move, not when they
391 stop moving. So to get a stop event, we need to use a timeout.
394 control->set_in_use (true);
395 _mcp.add_in_use_timeout (*this, *control, control);
397 control_event (*this, *control, state);
399 DEBUG_TRACE (DEBUG::MackieControl, "pot not found\n");
404 MackiePort::control_event (SurfacePort& sp, Control& c, const ControlState& cs)
406 _mcp.handle_control_event (sp, c, cs);