3 Copyright (C) 1999-2002 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <boost/shared_ptr.hpp>
31 #include <glibmm/main.h>
33 #include "midi++/mmc.h"
34 #include "midi++/port.h"
35 #include "midi++/manager.h"
36 #include "pbd/error.h"
37 #include "pbd/pthread_utils.h"
39 #include "ardour/configuration.h"
40 #include "ardour/debug.h"
41 #include "ardour/audioengine.h"
42 #include "ardour/session.h"
43 #include "ardour/audio_track.h"
44 #include "ardour/midi_track.h"
45 #include "ardour/midi_ui.h"
46 #include "ardour/audio_diskstream.h"
47 #include "ardour/slave.h"
48 #include "ardour/cycles.h"
49 #include "ardour/timecode.h"
54 using namespace ARDOUR;
59 MachineControl::CommandSignature MMC_CommandSignature;
60 MachineControl::ResponseSignature MMC_ResponseSignature;
67 boost::shared_ptr<RouteList> r = routes.reader ();
69 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
70 MidiTrack *track = dynamic_cast<MidiTrack*>((*i).get());
79 Session::use_config_midi_ports ()
83 if (default_mmc_port) {
84 set_mmc_port (default_mmc_port->name());
89 if (default_mtc_port) {
90 set_mtc_port (default_mtc_port->name());
95 if (default_midi_port) {
96 set_midi_port (default_midi_port->name());
101 if (default_midi_clock_port) {
102 set_midi_clock_port (default_midi_clock_port->name());
104 set_midi_clock_port ("");
111 /***********************************************************************
113 **********************************************************************/
116 Session::set_mtc_port (string port_tag)
120 if (port_tag.length() == 0) {
122 if (_slave && ((ms = dynamic_cast<MTC_Slave*> (_slave)) != 0)) {
123 error << _("Ardour is slaved to MTC - port cannot be reset") << endmsg;
127 if (_mtc_port == 0) {
137 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
138 error << string_compose (_("unknown port %1 requested for MTC"), port_tag) << endl;
144 if (_slave && ((ms = dynamic_cast<MTC_Slave*> (_slave)) != 0)) {
148 Config->set_mtc_port_name (port_tag);
151 MTC_PortChanged(); /* EMIT SIGNAL */
157 Session::set_mmc_receive_device_id (uint32_t device_id)
160 mmc->set_receive_device_id (device_id);
165 Session::set_mmc_send_device_id (uint32_t device_id)
168 mmc->set_send_device_id (device_id);
173 Session::set_mmc_port (string port_tag)
175 MIDI::byte old_recv_device_id = 0;
176 MIDI::byte old_send_device_id = 0;
177 bool reset_id = false;
179 if (port_tag.length() == 0) {
180 if (_mmc_port == 0) {
189 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
196 old_recv_device_id = mmc->receive_device_id();
197 old_recv_device_id = mmc->send_device_id();
202 mmc = new MIDI::MachineControl (*_mmc_port, 1.0,
203 MMC_CommandSignature,
204 MMC_ResponseSignature);
207 mmc->set_receive_device_id (old_recv_device_id);
208 mmc->set_send_device_id (old_send_device_id);
212 (sigc::mem_fun (*this, &Session::mmc_deferred_play));
213 mmc->DeferredPlay.connect
214 (sigc::mem_fun (*this, &Session::mmc_deferred_play));
216 (sigc::mem_fun (*this, &Session::mmc_stop));
217 mmc->FastForward.connect
218 (sigc::mem_fun (*this, &Session::mmc_fast_forward));
220 (sigc::mem_fun (*this, &Session::mmc_rewind));
222 (sigc::mem_fun (*this, &Session::mmc_pause));
223 mmc->RecordPause.connect
224 (sigc::mem_fun (*this, &Session::mmc_record_pause));
225 mmc->RecordStrobe.connect
226 (sigc::mem_fun (*this, &Session::mmc_record_strobe));
227 mmc->RecordExit.connect
228 (sigc::mem_fun (*this, &Session::mmc_record_exit));
230 (sigc::mem_fun (*this, &Session::mmc_locate));
232 (sigc::mem_fun (*this, &Session::mmc_step));
234 (sigc::mem_fun (*this, &Session::mmc_shuttle));
235 mmc->TrackRecordStatusChange.connect
236 (sigc::mem_fun (*this, &Session::mmc_record_enable));
239 /* also handle MIDI SPP because its so common */
241 _mmc_port->input()->start.connect (sigc::mem_fun (*this, &Session::spp_start));
242 _mmc_port->input()->contineu.connect (sigc::mem_fun (*this, &Session::spp_continue));
243 _mmc_port->input()->stop.connect (sigc::mem_fun (*this, &Session::spp_stop));
245 Config->set_mmc_port_name (port_tag);
248 MMC_PortChanged(); /* EMIT SIGNAL */
254 Session::set_midi_port (string /*port_tag*/)
257 if (port_tag.length() == 0) {
258 if (_midi_port == 0) {
267 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
273 /* XXX need something to forward this to control protocols ? or just
277 Config->set_midi_port_name (port_tag);
281 MIDI_PortChanged(); /* EMIT SIGNAL */
287 Session::set_midi_clock_port (string port_tag)
291 if (port_tag.length() == 0) {
293 if (_slave && ((ms = dynamic_cast<MIDIClock_Slave*> (_slave)) != 0)) {
294 error << _("Ardour is slaved to MIDI Clock - port cannot be reset") << endmsg;
298 if (_midi_clock_port == 0) {
302 _midi_clock_port = 0;
308 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
309 error << string_compose (_("unknown port %1 requested for MIDI Clock"), port_tag) << endl;
313 _midi_clock_port = port;
315 if (_slave && ((ms = dynamic_cast<MIDIClock_Slave*> (_slave)) != 0)) {
319 Config->set_midi_clock_port_name (port_tag);
322 MIDIClock_PortChanged(); /* EMIT SIGNAL */
328 Session::set_trace_midi_input (bool yn, MIDI::Port* port)
330 MIDI::Parser* input_parser;
332 cerr << "enabling tracing: " << yn << " for input port " << port->name() << endl;
335 if ((input_parser = port->input()) != 0) {
336 input_parser->trace (yn, &cout, "input: ");
341 if ((input_parser = _mmc_port->input()) != 0) {
342 input_parser->trace (yn, &cout, "input: ");
346 if (_mtc_port && _mtc_port != _mmc_port) {
347 if ((input_parser = _mtc_port->input()) != 0) {
348 input_parser->trace (yn, &cout, "input: ");
352 if (_midi_port && _midi_port != _mmc_port && _midi_port != _mtc_port ) {
353 if ((input_parser = _midi_port->input()) != 0) {
354 input_parser->trace (yn, &cout, "input: ");
359 && _midi_clock_port != _mmc_port
360 && _midi_clock_port != _mtc_port
361 && _midi_clock_port != _midi_port) {
362 if ((input_parser = _midi_clock_port->input()) != 0) {
363 input_parser->trace (yn, &cout, "input: ");
368 Config->set_trace_midi_input (yn);
372 Session::set_trace_midi_output (bool yn, MIDI::Port* port)
374 MIDI::Parser* output_parser;
377 if ((output_parser = port->output()) != 0) {
378 output_parser->trace (yn, &cout, "output: ");
382 if ((output_parser = _mmc_port->output()) != 0) {
383 output_parser->trace (yn, &cout, "output: ");
387 if (_mtc_port && _mtc_port != _mmc_port) {
388 if ((output_parser = _mtc_port->output()) != 0) {
389 output_parser->trace (yn, &cout, "output: ");
393 if (_midi_port && _midi_port != _mmc_port && _midi_port != _mtc_port ) {
394 if ((output_parser = _midi_port->output()) != 0) {
395 output_parser->trace (yn, &cout, "output: ");
401 Config->set_trace_midi_output (yn);
405 Session::get_trace_midi_input(MIDI::Port *port)
407 MIDI::Parser* input_parser;
409 if ((input_parser = port->input()) != 0) {
410 return input_parser->tracing();
415 if ((input_parser = _mmc_port->input()) != 0) {
416 return input_parser->tracing();
421 if ((input_parser = _mtc_port->input()) != 0) {
422 return input_parser->tracing();
427 if ((input_parser = _midi_port->input()) != 0) {
428 return input_parser->tracing();
437 Session::get_trace_midi_output(MIDI::Port *port)
439 MIDI::Parser* output_parser;
441 if ((output_parser = port->output()) != 0) {
442 return output_parser->tracing();
447 if ((output_parser = _mmc_port->output()) != 0) {
448 return output_parser->tracing();
453 if ((output_parser = _mtc_port->output()) != 0) {
454 return output_parser->tracing();
459 if ((output_parser = _midi_port->output()) != 0) {
460 return output_parser->tracing();
470 Session::setup_midi_control ()
472 outbound_mtc_timecode_frame = 0;
473 next_quarter_frame_to_send = 0;
475 /* setup the MMC buffer */
477 mmc_buffer[0] = 0xf0; // SysEx
478 mmc_buffer[1] = 0x7f; // Real Time SysEx ID for MMC
479 mmc_buffer[2] = (mmc ? mmc->send_device_id() : 0x7f);
480 mmc_buffer[3] = 0x6; // MCC
482 /* Set up the qtr frame message */
495 Session::spp_start (Parser &, nframes_t /*timestamp*/)
497 if (Config->get_mmc_control() && (!config.get_external_sync() || config.get_sync_source() != JACK)) {
498 request_transport_speed (1.0);
503 Session::spp_continue (Parser& ignored, nframes_t timestamp)
505 spp_start (ignored, timestamp);
509 Session::spp_stop (Parser&, nframes_t /*timestamp*/)
511 if (Config->get_mmc_control()) {
517 Session::mmc_deferred_play (MIDI::MachineControl &/*mmc*/)
519 if (Config->get_mmc_control() && (!config.get_external_sync() || (config.get_sync_source() != JACK))) {
520 request_transport_speed (1.0);
525 Session::mmc_record_pause (MIDI::MachineControl &/*mmc*/)
527 if (Config->get_mmc_control()) {
528 maybe_enable_record();
533 Session::mmc_record_strobe (MIDI::MachineControl &/*mmc*/)
535 if (!Config->get_mmc_control())
538 /* record strobe does an implicit "Play" command */
540 if (_transport_speed != 1.0) {
542 /* start_transport() will move from Enabled->Recording, so we
543 don't need to do anything here except enable recording.
544 its not the same as maybe_enable_record() though, because
545 that *can* switch to Recording, which we do not want.
548 save_state ("", true);
549 g_atomic_int_set (&_record_status, Enabled);
550 RecordStateChanged (); /* EMIT SIGNAL */
552 request_transport_speed (1.0);
561 Session::mmc_record_exit (MIDI::MachineControl &/*mmc*/)
563 if (Config->get_mmc_control()) {
564 disable_record (false);
569 Session::mmc_stop (MIDI::MachineControl &/*mmc*/)
571 if (Config->get_mmc_control()) {
577 Session::mmc_pause (MIDI::MachineControl &/*mmc*/)
579 if (Config->get_mmc_control()) {
581 /* We support RECORD_PAUSE, so the spec says that
582 we must interpret PAUSE like RECORD_PAUSE if
586 if (actively_recording()) {
587 maybe_enable_record ();
594 static bool step_queued = false;
597 Session::mmc_step (MIDI::MachineControl &/*mmc*/, int steps)
599 if (!Config->get_mmc_control()) {
604 struct timeval diff = { 0, 0 };
606 gettimeofday (&now, 0);
608 timersub (&now, &last_mmc_step, &diff);
610 gettimeofday (&now, 0);
611 timersub (&now, &last_mmc_step, &diff);
613 if (last_mmc_step.tv_sec != 0 && (diff.tv_usec + (diff.tv_sec * 1000000)) < _engine.usecs_per_cycle()) {
617 double diff_secs = diff.tv_sec + (diff.tv_usec / 1000000.0);
618 double cur_speed = (((steps * 0.5) * timecode_frames_per_second()) / diff_secs) / timecode_frames_per_second();
620 if (_transport_speed == 0 || cur_speed * _transport_speed < 0) {
621 /* change direction */
622 step_speed = cur_speed;
624 step_speed = (0.6 * step_speed) + (0.4 * cur_speed);
630 cerr << "delta = " << diff_secs
631 << " ct = " << _transport_speed
632 << " steps = " << steps
633 << " new speed = " << cur_speed
634 << " speed = " << step_speed
638 request_transport_speed (step_speed);
642 if (midi_control_ui) {
643 RefPtr<TimeoutSource> tsrc = TimeoutSource::create (100);
644 tsrc->connect (sigc::mem_fun (*this, &Session::mmc_step_timeout));
645 tsrc->attach (midi_control_ui->main_loop()->get_context());
652 Session::mmc_rewind (MIDI::MachineControl &/*mmc*/)
654 if (Config->get_mmc_control()) {
655 request_transport_speed(-8.0f);
660 Session::mmc_fast_forward (MIDI::MachineControl &/*mmc*/)
662 if (Config->get_mmc_control()) {
663 request_transport_speed(8.0f);
668 Session::mmc_locate (MIDI::MachineControl &/*mmc*/, const MIDI::byte* mmc_tc)
670 if (!Config->get_mmc_control()) {
674 nframes_t target_frame;
675 Timecode::Time timecode;
677 timecode.hours = mmc_tc[0] & 0xf;
678 timecode.minutes = mmc_tc[1];
679 timecode.seconds = mmc_tc[2];
680 timecode.frames = mmc_tc[3];
681 timecode.rate = timecode_frames_per_second();
682 timecode.drop = timecode_drop_frames();
684 // Also takes timecode offset into account:
685 timecode_to_sample( timecode, target_frame, true /* use_offset */, false /* use_subframes */ );
687 if (target_frame > max_frames) {
688 target_frame = max_frames;
691 /* Some (all?) MTC/MMC devices do not send a full MTC frame
692 at the end of a locate, instead sending only an MMC
693 locate command. This causes the current position
694 of an MTC slave to become out of date. Catch this.
697 MTC_Slave* mtcs = dynamic_cast<MTC_Slave*> (_slave);
700 // cerr << "Locate *with* MTC slave\n";
701 mtcs->handle_locate (mmc_tc);
703 // cerr << "Locate without MTC slave\n";
704 request_locate (target_frame, false);
709 Session::mmc_shuttle (MIDI::MachineControl &/*mmc*/, float speed, bool forw)
711 if (!Config->get_mmc_control()) {
715 if (Config->get_shuttle_speed_threshold() >= 0 && speed > Config->get_shuttle_speed_threshold()) {
716 speed *= Config->get_shuttle_speed_factor();
720 request_transport_speed (speed);
722 request_transport_speed (-speed);
727 Session::mmc_record_enable (MIDI::MachineControl &mmc, size_t trk, bool enabled)
729 if (Config->get_mmc_control()) {
731 RouteList::iterator i;
732 boost::shared_ptr<RouteList> r = routes.reader();
734 for (i = r->begin(); i != r->end(); ++i) {
737 if ((at = dynamic_cast<AudioTrack*>((*i).get())) != 0) {
738 if (trk == at->remote_control_id()) {
739 at->set_record_enable (enabled, &mmc);
747 /** Send MTC Full Frame message (complete Timecode time) for the start of this cycle.
748 * This resets the MTC code, the next quarter frame message that is sent will be
749 * the first one with the beginning of this cycle as the new start point.
752 Session::send_full_time_code(nframes_t /*nframes*/)
754 /* This function could easily send at a given frame offset, but would
755 * that be useful? Does ardour do sub-block accurate locating? [DR] */
758 Timecode::Time timecode;
760 _send_timecode_update = false;
762 if (_mtc_port == 0 || !session_send_mtc) {
766 // Get timecode time for this transport frame
767 sample_to_timecode(_transport_frame, timecode, true /* use_offset */, false /* no subframes */);
769 transmitting_timecode_time = timecode;
770 outbound_mtc_timecode_frame = _transport_frame;
772 // I don't understand this bit yet.. [DR]
773 if (((mtc_timecode_bits >> 5) != MIDI::MTC_25_FPS) && (transmitting_timecode_time.frames % 2)) {
774 // start MTC quarter frame transmission on an even frame
775 Timecode::increment( transmitting_timecode_time, config.get_subframes_per_frame() );
776 outbound_mtc_timecode_frame += (nframes_t) _frames_per_timecode_frame;
779 // Compensate for audio latency
780 outbound_mtc_timecode_frame += _worst_output_latency;
782 next_quarter_frame_to_send = 0;
784 // Sync slave to the same Timecode time as we are on
792 msg[5] = mtc_timecode_bits | timecode.hours;
793 msg[6] = timecode.minutes;
794 msg[7] = timecode.seconds;
795 msg[8] = timecode.frames;
797 cerr << "MTC: Sending full time code at " << outbound_mtc_timecode_frame << endl;
799 // Send message at offset 0, sent time is for the start of this cycle
800 if (_mtc_port->midimsg (msg, sizeof (msg), 0)) {
801 error << _("Session: could not send full MIDI time code") << endmsg;
808 /** Send MTC (quarter-frame) messages for this cycle.
809 * Must be called exactly once per cycle from the audio thread. Realtime safe.
810 * This function assumes the state of full Timecode is sane, eg. the slave is
811 * expecting quarter frame messages and has the right frame of reference (any
812 * full MTC Timecode time messages that needed to be sent should have been sent
813 * earlier already this cycle by send_full_time_code)
816 Session::send_midi_time_code_for_cycle(nframes_t nframes)
818 assert (next_quarter_frame_to_send >= 0);
819 assert (next_quarter_frame_to_send <= 7);
821 if (_mtc_port == 0 || !session_send_mtc || transmitting_timecode_time.negative
822 /*|| (next_quarter_frame_to_send < 0)*/ ) {
823 // cerr << "(MTC) Not sending MTC\n";
827 /* Duration of one quarter frame */
828 nframes_t quarter_frame_duration = ((long) _frames_per_timecode_frame) >> 2;
830 DEBUG_TRACE (DEBUG::MTC, string_compose ("TF %1 SF %2 NQ %3 FD %\4n", _transport_frame, outbound_mtc_timecode_frame,
831 next_quarter_frame_to_send, quarter_frame_duration));
833 // FIXME: this should always be true
834 //assert((outbound_mtc_timecode_frame + (next_quarter_frame_to_send * quarter_frame_duration))
835 // > _transport_frame);
838 // Send quarter frames for this cycle
839 while (_transport_frame + nframes > (outbound_mtc_timecode_frame +
840 (next_quarter_frame_to_send * quarter_frame_duration))) {
842 DEBUG_TRACE (DEBUG::MTC, string_compose ("next frame to send: %1\n", next_quarter_frame_to_send));
844 switch (next_quarter_frame_to_send) {
846 mtc_msg[1] = 0x00 | (transmitting_timecode_time.frames & 0xf);
849 mtc_msg[1] = 0x10 | ((transmitting_timecode_time.frames & 0xf0) >> 4);
852 mtc_msg[1] = 0x20 | (transmitting_timecode_time.seconds & 0xf);
855 mtc_msg[1] = 0x30 | ((transmitting_timecode_time.seconds & 0xf0) >> 4);
858 mtc_msg[1] = 0x40 | (transmitting_timecode_time.minutes & 0xf);
861 mtc_msg[1] = 0x50 | ((transmitting_timecode_time.minutes & 0xf0) >> 4);
864 mtc_msg[1] = 0x60 | ((mtc_timecode_bits|transmitting_timecode_time.hours) & 0xf);
867 mtc_msg[1] = 0x70 | (((mtc_timecode_bits|transmitting_timecode_time.hours) & 0xf0) >> 4);
871 const nframes_t msg_time = (outbound_mtc_timecode_frame
872 + (quarter_frame_duration * next_quarter_frame_to_send));
874 // This message must fall within this block or something is broken
875 assert(msg_time >= _transport_frame);
876 assert(msg_time < _transport_frame + nframes);
878 nframes_t out_stamp = msg_time - _transport_frame;
879 assert(out_stamp < nframes);
881 if (_mtc_port->midimsg (mtc_msg, 2, out_stamp)) {
882 error << string_compose(_("Session: cannot send quarter-frame MTC message (%1)"), strerror (errno))
888 DEBUG_STR_SET(foo,"sending ");
889 DEBUG_STR(foo) << transmitting_timecode_time;
890 DEBUG_TRACE (DEBUG::MTC, string_compose ("%1 qfm = %2, stamp = %3\n", DEBUG_STR(foo).str(), next_quarter_frame_to_send,
894 // Increment quarter frame counter
895 next_quarter_frame_to_send++;
897 if (next_quarter_frame_to_send >= 8) {
898 // Wrap quarter frame counter
899 next_quarter_frame_to_send = 0;
900 // Increment timecode time twice
901 Timecode::increment( transmitting_timecode_time, config.get_subframes_per_frame() );
902 Timecode::increment( transmitting_timecode_time, config.get_subframes_per_frame() );
903 // Re-calculate timing of first quarter frame
904 //timecode_to_sample( transmitting_timecode_time, outbound_mtc_timecode_frame, true /* use_offset */, false );
905 outbound_mtc_timecode_frame += 8 * quarter_frame_duration;
906 // Compensate for audio latency
907 outbound_mtc_timecode_frame += _worst_output_latency;
914 /***********************************************************************
916 **********************************************************************/
919 Session::deliver_mmc (MIDI::MachineControl::Command cmd, nframes_t where)
921 using namespace MIDI;
923 Timecode::Time timecode;
925 if (_mmc_port == 0 || !session_send_mmc) {
926 // cerr << "Not delivering MMC " << _mmc_port << " - " << session_send_mmc << endl;
930 mmc_buffer[nbytes++] = cmd;
932 // cerr << "delivering MMC, cmd = " << hex << (int) cmd << dec << endl;
935 case MachineControl::cmdLocate:
936 timecode_time_subframes (where, timecode);
938 mmc_buffer[nbytes++] = 0x6; // byte count
939 mmc_buffer[nbytes++] = 0x1; // "TARGET" subcommand
940 mmc_buffer[nbytes++] = timecode.hours;
941 mmc_buffer[nbytes++] = timecode.minutes;
942 mmc_buffer[nbytes++] = timecode.seconds;
943 mmc_buffer[nbytes++] = timecode.frames;
944 mmc_buffer[nbytes++] = timecode.subframes;
947 case MachineControl::cmdStop:
950 case MachineControl::cmdPlay:
951 /* always convert Play into Deferred Play */
953 mmc_buffer[4] = MachineControl::cmdDeferredPlay;
956 case MachineControl::cmdDeferredPlay:
959 case MachineControl::cmdRecordStrobe:
962 case MachineControl::cmdRecordExit:
965 case MachineControl::cmdRecordPause:
974 mmc_buffer[nbytes++] = 0xf7; // terminate SysEx/MMC message
976 if (_mmc_port->midimsg (mmc_buffer, nbytes, 0)) {
977 error << string_compose(_("MMC: cannot send command %1%2%3"), &hex, cmd, &dec) << endmsg;
983 Session::mmc_step_timeout ()
988 gettimeofday (&now, 0);
990 timersub (&now, &last_mmc_step, &diff);
991 diff_usecs = diff.tv_sec * 1000000 + diff.tv_usec;
993 if (diff_usecs > 1000000.0 || fabs (_transport_speed) < 0.0000001) {
994 /* too long or too slow, stop transport */
995 request_transport_speed (0.0);
1000 if (diff_usecs < 250000.0) {
1001 /* too short, just keep going */
1007 request_transport_speed (_transport_speed * 0.75);
1011 /*---------------------------------------------------------------------------
1013 ---------------------------------------------------------------------------*/
1016 Session::start_midi_thread ()
1018 midi_control_ui = new MidiControlUI (*this);
1019 midi_control_ui->run ();
1024 Session::terminate_midi_thread ()
1026 if (midi_control_ui) {
1027 midi_control_ui->quit ();