possible fix for race between diskstream buffer overwrite and channel setup
[ardour2.git] / libs / ardour / session_midi.cc
blob6035a8a077e6976e7d44e7b09c69fbe84523e5cb
2 /*
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.
21 #include <string>
22 #include <cmath>
23 #include <cerrno>
24 #include <cassert>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <poll.h>
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"
51 #include "i18n.h"
53 using namespace std;
54 using namespace ARDOUR;
55 using namespace PBD;
56 using namespace MIDI;
57 using namespace Glib;
59 MachineControl::CommandSignature MMC_CommandSignature;
60 MachineControl::ResponseSignature MMC_ResponseSignature;
63 void
64 Session::midi_panic()
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());
71 if (track != 0) {
72 track->midi_panic();
78 int
79 Session::use_config_midi_ports ()
81 string port_name;
83 if (default_mmc_port) {
84 set_mmc_port (default_mmc_port->name());
85 } else {
86 set_mmc_port ("");
89 if (default_mtc_port) {
90 set_mtc_port (default_mtc_port->name());
91 } else {
92 set_mtc_port ("");
95 if (default_midi_port) {
96 set_midi_port (default_midi_port->name());
97 } else {
98 set_midi_port ("");
101 if (default_midi_clock_port) {
102 set_midi_clock_port (default_midi_clock_port->name());
103 } else {
104 set_midi_clock_port ("");
107 return 0;
111 /***********************************************************************
112 MTC, MMC, etc.
113 **********************************************************************/
116 Session::set_mtc_port (string port_tag)
118 MTC_Slave *ms;
120 if (port_tag.length() == 0) {
122 if (_slave && ((ms = dynamic_cast<MTC_Slave*> (_slave)) != 0)) {
123 error << string_compose (_("%1 is slaved to MTC - port cannot be reset"), PROGRAM_NAME) << endmsg;
124 return -1;
127 if (_mtc_port == 0) {
128 return 0;
131 _mtc_port = 0;
132 goto out;
135 MIDI::Port* port;
137 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
138 error << string_compose (_("unknown port %1 requested for MTC"), port_tag) << endl;
139 return -1;
142 _mtc_port = port;
144 if (_slave && ((ms = dynamic_cast<MTC_Slave*> (_slave)) != 0)) {
145 ms->rebind (*port);
148 Config->set_mtc_port_name (port_tag);
150 out:
151 MTC_PortChanged(); /* EMIT SIGNAL */
152 set_dirty();
153 return 0;
156 void
157 Session::set_mmc_receive_device_id (uint32_t device_id)
159 if (mmc) {
160 mmc->set_receive_device_id (device_id);
164 void
165 Session::set_mmc_send_device_id (uint32_t device_id)
167 if (mmc) {
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) {
181 return 0;
183 _mmc_port = 0;
184 goto out;
187 MIDI::Port* port;
189 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
190 return -1;
193 _mmc_port = port;
195 if (mmc) {
196 old_recv_device_id = mmc->receive_device_id();
197 old_recv_device_id = mmc->send_device_id();
198 reset_id = true;
199 delete mmc;
202 mmc = new MIDI::MachineControl (*_mmc_port, 1.0,
203 MMC_CommandSignature,
204 MMC_ResponseSignature);
206 if (reset_id) {
207 mmc->set_receive_device_id (old_recv_device_id);
208 mmc->set_send_device_id (old_send_device_id);
211 mmc->Play.connect_same_thread (*this, boost::bind (&Session::mmc_deferred_play, this, _1));
212 mmc->DeferredPlay.connect_same_thread (*this, boost::bind (&Session::mmc_deferred_play, this, _1));
213 mmc->Stop.connect_same_thread (*this, boost::bind (&Session::mmc_stop, this, _1));
214 mmc->FastForward.connect_same_thread (*this, boost::bind (&Session::mmc_fast_forward, this, _1));
215 mmc->Rewind.connect_same_thread (*this, boost::bind (&Session::mmc_rewind, this, _1));
216 mmc->Pause.connect_same_thread (*this, boost::bind (&Session::mmc_pause, this, _1));
217 mmc->RecordPause.connect_same_thread (*this, boost::bind (&Session::mmc_record_pause, this, _1));
218 mmc->RecordStrobe.connect_same_thread (*this, boost::bind (&Session::mmc_record_strobe, this, _1));
219 mmc->RecordExit.connect_same_thread (*this, boost::bind (&Session::mmc_record_exit, this, _1));
220 mmc->Locate.connect_same_thread (*this, boost::bind (&Session::mmc_locate, this, _1, _2));
221 mmc->Step.connect_same_thread (*this, boost::bind (&Session::mmc_step, this, _1, _2));
222 mmc->Shuttle.connect_same_thread (*this, boost::bind (&Session::mmc_shuttle, this, _1, _2, _3));
223 mmc->TrackRecordStatusChange.connect_same_thread (*this, boost::bind (&Session::mmc_record_enable, this, _1, _2, _3));
226 /* also handle MIDI SPP because its so common */
228 _mmc_port->input()->start.connect_same_thread (*this, boost::bind (&Session::spp_start, this, _1, _2));
229 _mmc_port->input()->contineu.connect_same_thread (*this, boost::bind (&Session::spp_continue, this, _1, _2));
230 _mmc_port->input()->stop.connect_same_thread (*this, boost::bind (&Session::spp_stop, this, _1, _2));
232 Config->set_mmc_port_name (port_tag);
234 out:
235 MMC_PortChanged(); /* EMIT SIGNAL */
236 set_dirty();
237 return 0;
241 Session::set_midi_port (string /*port_tag*/)
243 #if 0
244 if (port_tag.length() == 0) {
245 if (_midi_port == 0) {
246 return 0;
248 _midi_port = 0;
249 goto out;
252 MIDI::Port* port;
254 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
255 return -1;
258 _midi_port = port;
260 /* XXX need something to forward this to control protocols ? or just
261 use the signal below
264 Config->set_midi_port_name (port_tag);
266 out:
267 #endif
268 MIDI_PortChanged(); /* EMIT SIGNAL */
269 set_dirty();
270 return 0;
274 Session::set_midi_clock_port (string port_tag)
276 MIDIClock_Slave *ms;
278 if (port_tag.length() == 0) {
280 if (_slave && ((ms = dynamic_cast<MIDIClock_Slave*> (_slave)) != 0)) {
281 error << string_compose (_("%1 is slaved to MIDI Clock - port cannot be reset"), PROGRAM_NAME) << endmsg;
282 return -1;
285 if (_midi_clock_port == 0) {
286 return 0;
289 _midi_clock_port = 0;
290 goto out;
293 MIDI::Port* port;
295 if ((port = MIDI::Manager::instance()->port (port_tag)) == 0) {
296 error << string_compose (_("unknown port %1 requested for MIDI Clock"), port_tag) << endl;
297 return -1;
300 _midi_clock_port = port;
302 if (_slave && ((ms = dynamic_cast<MIDIClock_Slave*> (_slave)) != 0)) {
303 ms->rebind (*port);
306 Config->set_midi_clock_port_name (port_tag);
308 out:
309 MIDIClock_PortChanged(); /* EMIT SIGNAL */
310 set_dirty();
311 return 0;
314 void
315 Session::set_trace_midi_input (bool yn, MIDI::Port* port)
317 MIDI::Parser* input_parser;
319 cerr << "enabling tracing: " << yn << " for input port " << port->name() << endl;
321 if (port) {
322 if ((input_parser = port->input()) != 0) {
323 input_parser->trace (yn, &cout, "input: ");
325 } else {
327 if (_mmc_port) {
328 if ((input_parser = _mmc_port->input()) != 0) {
329 input_parser->trace (yn, &cout, "input: ");
333 if (_mtc_port && _mtc_port != _mmc_port) {
334 if ((input_parser = _mtc_port->input()) != 0) {
335 input_parser->trace (yn, &cout, "input: ");
339 if (_midi_port && _midi_port != _mmc_port && _midi_port != _mtc_port ) {
340 if ((input_parser = _midi_port->input()) != 0) {
341 input_parser->trace (yn, &cout, "input: ");
345 if (_midi_clock_port
346 && _midi_clock_port != _mmc_port
347 && _midi_clock_port != _mtc_port
348 && _midi_clock_port != _midi_port) {
349 if ((input_parser = _midi_clock_port->input()) != 0) {
350 input_parser->trace (yn, &cout, "input: ");
355 Config->set_trace_midi_input (yn);
358 void
359 Session::set_trace_midi_output (bool yn, MIDI::Port* port)
361 MIDI::Parser* output_parser;
363 if (port) {
364 if ((output_parser = port->output()) != 0) {
365 output_parser->trace (yn, &cout, "output: ");
367 } else {
368 if (_mmc_port) {
369 if ((output_parser = _mmc_port->output()) != 0) {
370 output_parser->trace (yn, &cout, "output: ");
374 if (_mtc_port && _mtc_port != _mmc_port) {
375 if ((output_parser = _mtc_port->output()) != 0) {
376 output_parser->trace (yn, &cout, "output: ");
380 if (_midi_port && _midi_port != _mmc_port && _midi_port != _mtc_port ) {
381 if ((output_parser = _midi_port->output()) != 0) {
382 output_parser->trace (yn, &cout, "output: ");
388 Config->set_trace_midi_output (yn);
391 bool
392 Session::get_trace_midi_input(MIDI::Port *port)
394 MIDI::Parser* input_parser;
395 if (port) {
396 if ((input_parser = port->input()) != 0) {
397 return input_parser->tracing();
400 else {
401 if (_mmc_port) {
402 if ((input_parser = _mmc_port->input()) != 0) {
403 return input_parser->tracing();
407 if (_mtc_port) {
408 if ((input_parser = _mtc_port->input()) != 0) {
409 return input_parser->tracing();
413 if (_midi_port) {
414 if ((input_parser = _midi_port->input()) != 0) {
415 return input_parser->tracing();
420 return false;
423 bool
424 Session::get_trace_midi_output(MIDI::Port *port)
426 MIDI::Parser* output_parser;
427 if (port) {
428 if ((output_parser = port->output()) != 0) {
429 return output_parser->tracing();
432 else {
433 if (_mmc_port) {
434 if ((output_parser = _mmc_port->output()) != 0) {
435 return output_parser->tracing();
439 if (_mtc_port) {
440 if ((output_parser = _mtc_port->output()) != 0) {
441 return output_parser->tracing();
445 if (_midi_port) {
446 if ((output_parser = _midi_port->output()) != 0) {
447 return output_parser->tracing();
452 return false;
456 void
457 Session::setup_midi_control ()
459 outbound_mtc_timecode_frame = 0;
460 next_quarter_frame_to_send = 0;
462 /* setup the MMC buffer */
464 mmc_buffer[0] = 0xf0; // SysEx
465 mmc_buffer[1] = 0x7f; // Real Time SysEx ID for MMC
466 mmc_buffer[2] = (mmc ? mmc->send_device_id() : 0x7f);
467 mmc_buffer[3] = 0x6; // MCC
469 /* Set up the qtr frame message */
471 mtc_msg[0] = 0xf1;
472 mtc_msg[2] = 0xf1;
473 mtc_msg[4] = 0xf1;
474 mtc_msg[6] = 0xf1;
475 mtc_msg[8] = 0xf1;
476 mtc_msg[10] = 0xf1;
477 mtc_msg[12] = 0xf1;
478 mtc_msg[14] = 0xf1;
481 void
482 Session::spp_start (Parser &, nframes_t /*timestamp*/)
484 if (Config->get_mmc_control() && (!config.get_external_sync() || config.get_sync_source() != JACK)) {
485 request_transport_speed (1.0);
489 void
490 Session::spp_continue (Parser& ignored, nframes_t timestamp)
492 spp_start (ignored, timestamp);
495 void
496 Session::spp_stop (Parser&, nframes_t /*timestamp*/)
498 if (Config->get_mmc_control()) {
499 request_stop ();
503 void
504 Session::mmc_deferred_play (MIDI::MachineControl &/*mmc*/)
506 if (Config->get_mmc_control() && (!config.get_external_sync() || (config.get_sync_source() != JACK))) {
507 request_transport_speed (1.0);
511 void
512 Session::mmc_record_pause (MIDI::MachineControl &/*mmc*/)
514 if (Config->get_mmc_control()) {
515 maybe_enable_record();
519 void
520 Session::mmc_record_strobe (MIDI::MachineControl &/*mmc*/)
522 if (!Config->get_mmc_control())
523 return;
525 /* record strobe does an implicit "Play" command */
527 if (_transport_speed != 1.0) {
529 /* start_transport() will move from Enabled->Recording, so we
530 don't need to do anything here except enable recording.
531 its not the same as maybe_enable_record() though, because
532 that *can* switch to Recording, which we do not want.
535 save_state ("", true);
536 g_atomic_int_set (&_record_status, Enabled);
537 RecordStateChanged (); /* EMIT SIGNAL */
539 request_transport_speed (1.0);
541 } else {
543 enable_record ();
547 void
548 Session::mmc_record_exit (MIDI::MachineControl &/*mmc*/)
550 if (Config->get_mmc_control()) {
551 disable_record (false);
555 void
556 Session::mmc_stop (MIDI::MachineControl &/*mmc*/)
558 if (Config->get_mmc_control()) {
559 request_stop ();
563 void
564 Session::mmc_pause (MIDI::MachineControl &/*mmc*/)
566 if (Config->get_mmc_control()) {
568 /* We support RECORD_PAUSE, so the spec says that
569 we must interpret PAUSE like RECORD_PAUSE if
570 recording.
573 if (actively_recording()) {
574 maybe_enable_record ();
575 } else {
576 request_stop ();
581 static bool step_queued = false;
583 void
584 Session::mmc_step (MIDI::MachineControl &/*mmc*/, int steps)
586 if (!Config->get_mmc_control()) {
587 return;
590 struct timeval now;
591 struct timeval diff = { 0, 0 };
593 gettimeofday (&now, 0);
595 timersub (&now, &last_mmc_step, &diff);
597 gettimeofday (&now, 0);
598 timersub (&now, &last_mmc_step, &diff);
600 if (last_mmc_step.tv_sec != 0 && (diff.tv_usec + (diff.tv_sec * 1000000)) < _engine.usecs_per_cycle()) {
601 return;
604 double diff_secs = diff.tv_sec + (diff.tv_usec / 1000000.0);
605 double cur_speed = (((steps * 0.5) * timecode_frames_per_second()) / diff_secs) / timecode_frames_per_second();
607 if (_transport_speed == 0 || cur_speed * _transport_speed < 0) {
608 /* change direction */
609 step_speed = cur_speed;
610 } else {
611 step_speed = (0.6 * step_speed) + (0.4 * cur_speed);
614 step_speed *= 0.25;
616 #if 0
617 cerr << "delta = " << diff_secs
618 << " ct = " << _transport_speed
619 << " steps = " << steps
620 << " new speed = " << cur_speed
621 << " speed = " << step_speed
622 << endl;
623 #endif
625 request_transport_speed (step_speed);
626 last_mmc_step = now;
628 if (!step_queued) {
629 if (midi_control_ui) {
630 RefPtr<TimeoutSource> tsrc = TimeoutSource::create (100);
631 tsrc->connect (sigc::mem_fun (*this, &Session::mmc_step_timeout));
632 tsrc->attach (midi_control_ui->main_loop()->get_context());
633 step_queued = true;
638 void
639 Session::mmc_rewind (MIDI::MachineControl &/*mmc*/)
641 if (Config->get_mmc_control()) {
642 request_transport_speed(-8.0f);
646 void
647 Session::mmc_fast_forward (MIDI::MachineControl &/*mmc*/)
649 if (Config->get_mmc_control()) {
650 request_transport_speed(8.0f);
654 void
655 Session::mmc_locate (MIDI::MachineControl &/*mmc*/, const MIDI::byte* mmc_tc)
657 if (!Config->get_mmc_control()) {
658 return;
661 nframes_t target_frame;
662 Timecode::Time timecode;
664 timecode.hours = mmc_tc[0] & 0xf;
665 timecode.minutes = mmc_tc[1];
666 timecode.seconds = mmc_tc[2];
667 timecode.frames = mmc_tc[3];
668 timecode.rate = timecode_frames_per_second();
669 timecode.drop = timecode_drop_frames();
671 // Also takes timecode offset into account:
672 timecode_to_sample( timecode, target_frame, true /* use_offset */, false /* use_subframes */ );
674 if (target_frame > max_frames) {
675 target_frame = max_frames;
678 /* Some (all?) MTC/MMC devices do not send a full MTC frame
679 at the end of a locate, instead sending only an MMC
680 locate command. This causes the current position
681 of an MTC slave to become out of date. Catch this.
684 MTC_Slave* mtcs = dynamic_cast<MTC_Slave*> (_slave);
686 if (mtcs != 0) {
687 // cerr << "Locate *with* MTC slave\n";
688 mtcs->handle_locate (mmc_tc);
689 } else {
690 // cerr << "Locate without MTC slave\n";
691 request_locate (target_frame, false);
695 void
696 Session::mmc_shuttle (MIDI::MachineControl &/*mmc*/, float speed, bool forw)
698 if (!Config->get_mmc_control()) {
699 return;
702 if (Config->get_shuttle_speed_threshold() >= 0 && speed > Config->get_shuttle_speed_threshold()) {
703 speed *= Config->get_shuttle_speed_factor();
706 if (forw) {
707 request_transport_speed (speed);
708 } else {
709 request_transport_speed (-speed);
713 void
714 Session::mmc_record_enable (MIDI::MachineControl &mmc, size_t trk, bool enabled)
716 if (Config->get_mmc_control()) {
718 RouteList::iterator i;
719 boost::shared_ptr<RouteList> r = routes.reader();
721 for (i = r->begin(); i != r->end(); ++i) {
722 AudioTrack *at;
724 if ((at = dynamic_cast<AudioTrack*>((*i).get())) != 0) {
725 if (trk == at->remote_control_id()) {
726 at->set_record_enable (enabled, &mmc);
727 break;
734 /** Send MTC Full Frame message (complete Timecode time) for the start of this cycle.
735 * This resets the MTC code, the next quarter frame message that is sent will be
736 * the first one with the beginning of this cycle as the new start point.
739 Session::send_full_time_code(nframes_t /*nframes*/)
741 /* This function could easily send at a given frame offset, but would
742 * that be useful? Does ardour do sub-block accurate locating? [DR] */
744 MIDI::byte msg[10];
745 Timecode::Time timecode;
747 _send_timecode_update = false;
749 if (_mtc_port == 0 || !session_send_mtc || _slave) {
750 return 0;
753 // Get timecode time for this transport frame
754 sample_to_timecode(_transport_frame, timecode, true /* use_offset */, false /* no subframes */);
756 transmitting_timecode_time = timecode;
757 outbound_mtc_timecode_frame = _transport_frame;
759 // I don't understand this bit yet.. [DR]
760 if (((mtc_timecode_bits >> 5) != MIDI::MTC_25_FPS) && (transmitting_timecode_time.frames % 2)) {
761 // start MTC quarter frame transmission on an even frame
762 Timecode::increment (transmitting_timecode_time, config.get_subframes_per_frame());
763 outbound_mtc_timecode_frame += (nframes_t) _frames_per_timecode_frame;
766 // Compensate for audio latency
767 outbound_mtc_timecode_frame += _worst_output_latency;
768 next_quarter_frame_to_send = 0;
770 // Sync slave to the same Timecode time as we are on
771 msg[0] = 0xf0;
772 msg[1] = 0x7f;
773 msg[2] = 0x7f;
774 msg[3] = 0x1;
775 msg[4] = 0x1;
776 msg[9] = 0xf7;
778 msg[5] = mtc_timecode_bits | timecode.hours;
779 msg[6] = timecode.minutes;
780 msg[7] = timecode.seconds;
781 msg[8] = timecode.frames;
783 // Send message at offset 0, sent time is for the start of this cycle
784 if (_mtc_port->midimsg (msg, sizeof (msg), 0)) {
785 error << _("Session: could not send full MIDI time code") << endmsg;
786 return -1;
789 return 0;
792 /** Send MTC (quarter-frame) messages for this cycle.
793 * Must be called exactly once per cycle from the audio thread. Realtime safe.
794 * This function assumes the state of full Timecode is sane, eg. the slave is
795 * expecting quarter frame messages and has the right frame of reference (any
796 * full MTC Timecode time messages that needed to be sent should have been sent
797 * earlier already this cycle by send_full_time_code)
800 Session::send_midi_time_code_for_cycle(nframes_t nframes)
802 if (_mtc_port == 0 || _slave || !session_send_mtc || transmitting_timecode_time.negative || (next_quarter_frame_to_send < 0)) {
803 // cerr << "(MTC) Not sending MTC\n";
804 return 0;
807 assert (next_quarter_frame_to_send >= 0);
808 assert (next_quarter_frame_to_send <= 7);
810 /* Duration of one quarter frame */
811 nframes_t quarter_frame_duration = ((long) _frames_per_timecode_frame) >> 2;
813 DEBUG_TRACE (DEBUG::MTC, string_compose ("TF %1 SF %2 NQ %3 FD %\4n", _transport_frame, outbound_mtc_timecode_frame,
814 next_quarter_frame_to_send, quarter_frame_duration));
816 // FIXME: this should always be true
817 //assert((outbound_mtc_timecode_frame + (next_quarter_frame_to_send * quarter_frame_duration))
818 // > _transport_frame);
821 // Send quarter frames for this cycle
822 while (_transport_frame + nframes > (outbound_mtc_timecode_frame +
823 (next_quarter_frame_to_send * quarter_frame_duration))) {
825 DEBUG_TRACE (DEBUG::MTC, string_compose ("next frame to send: %1\n", next_quarter_frame_to_send));
827 switch (next_quarter_frame_to_send) {
828 case 0:
829 mtc_msg[1] = 0x00 | (transmitting_timecode_time.frames & 0xf);
830 break;
831 case 1:
832 mtc_msg[1] = 0x10 | ((transmitting_timecode_time.frames & 0xf0) >> 4);
833 break;
834 case 2:
835 mtc_msg[1] = 0x20 | (transmitting_timecode_time.seconds & 0xf);
836 break;
837 case 3:
838 mtc_msg[1] = 0x30 | ((transmitting_timecode_time.seconds & 0xf0) >> 4);
839 break;
840 case 4:
841 mtc_msg[1] = 0x40 | (transmitting_timecode_time.minutes & 0xf);
842 break;
843 case 5:
844 mtc_msg[1] = 0x50 | ((transmitting_timecode_time.minutes & 0xf0) >> 4);
845 break;
846 case 6:
847 mtc_msg[1] = 0x60 | ((mtc_timecode_bits|transmitting_timecode_time.hours) & 0xf);
848 break;
849 case 7:
850 mtc_msg[1] = 0x70 | (((mtc_timecode_bits|transmitting_timecode_time.hours) & 0xf0) >> 4);
851 break;
854 const nframes_t msg_time = (outbound_mtc_timecode_frame
855 + (quarter_frame_duration * next_quarter_frame_to_send));
857 // This message must fall within this block or something is broken
858 assert(msg_time >= _transport_frame);
859 assert(msg_time < _transport_frame + nframes);
861 nframes_t out_stamp = msg_time - _transport_frame;
862 assert(out_stamp < nframes);
864 if (_mtc_port->midimsg (mtc_msg, 2, out_stamp)) {
865 error << string_compose(_("Session: cannot send quarter-frame MTC message (%1)"), strerror (errno))
866 << endmsg;
867 return -1;
870 #ifndef NDEBUG
871 DEBUG_STR_DECL(foo)
872 DEBUG_STR_APPEND(foo,"sending ");
873 DEBUG_STR_APPEND(foo, transmitting_timecode_time);
874 DEBUG_TRACE (DEBUG::MTC, string_compose ("%1 qfm = %2, stamp = %3\n", DEBUG_STR(foo).str(), next_quarter_frame_to_send,
875 out_stamp));
876 #endif
878 // Increment quarter frame counter
879 next_quarter_frame_to_send++;
881 if (next_quarter_frame_to_send >= 8) {
882 // Wrap quarter frame counter
883 next_quarter_frame_to_send = 0;
884 // Increment timecode time twice
885 Timecode::increment( transmitting_timecode_time, config.get_subframes_per_frame() );
886 Timecode::increment( transmitting_timecode_time, config.get_subframes_per_frame() );
887 // Re-calculate timing of first quarter frame
888 //timecode_to_sample( transmitting_timecode_time, outbound_mtc_timecode_frame, true /* use_offset */, false );
889 outbound_mtc_timecode_frame += 8 * quarter_frame_duration;
890 // Compensate for audio latency
891 outbound_mtc_timecode_frame += _worst_output_latency;
895 return 0;
898 /***********************************************************************
899 OUTBOUND MMC STUFF
900 **********************************************************************/
902 void
903 Session::deliver_mmc (MIDI::MachineControl::Command cmd, nframes_t where)
905 using namespace MIDI;
906 int nbytes = 4;
907 Timecode::Time timecode;
909 if (_mmc_port == 0 || !session_send_mmc) {
910 // cerr << "Not delivering MMC " << _mmc_port << " - " << session_send_mmc << endl;
911 return;
914 mmc_buffer[nbytes++] = cmd;
916 // cerr << "delivering MMC, cmd = " << hex << (int) cmd << dec << endl;
918 switch (cmd) {
919 case MachineControl::cmdLocate:
920 timecode_time_subframes (where, timecode);
922 mmc_buffer[nbytes++] = 0x6; // byte count
923 mmc_buffer[nbytes++] = 0x1; // "TARGET" subcommand
924 mmc_buffer[nbytes++] = timecode.hours;
925 mmc_buffer[nbytes++] = timecode.minutes;
926 mmc_buffer[nbytes++] = timecode.seconds;
927 mmc_buffer[nbytes++] = timecode.frames;
928 mmc_buffer[nbytes++] = timecode.subframes;
929 break;
931 case MachineControl::cmdStop:
932 break;
934 case MachineControl::cmdPlay:
935 /* always convert Play into Deferred Play */
936 /* Why? [DR] */
937 mmc_buffer[4] = MachineControl::cmdDeferredPlay;
938 break;
940 case MachineControl::cmdDeferredPlay:
941 break;
943 case MachineControl::cmdRecordStrobe:
944 break;
946 case MachineControl::cmdRecordExit:
947 break;
949 case MachineControl::cmdRecordPause:
950 break;
952 default:
953 nbytes = 0;
956 if (nbytes) {
958 mmc_buffer[nbytes++] = 0xf7; // terminate SysEx/MMC message
960 if (_mmc_port->midimsg (mmc_buffer, nbytes, 0)) {
961 error << string_compose(_("MMC: cannot send command %1%2%3"), &hex, cmd, &dec) << endmsg;
966 bool
967 Session::mmc_step_timeout ()
969 struct timeval now;
970 struct timeval diff;
971 double diff_usecs;
972 gettimeofday (&now, 0);
974 timersub (&now, &last_mmc_step, &diff);
975 diff_usecs = diff.tv_sec * 1000000 + diff.tv_usec;
977 if (diff_usecs > 1000000.0 || fabs (_transport_speed) < 0.0000001) {
978 /* too long or too slow, stop transport */
979 request_transport_speed (0.0);
980 step_queued = false;
981 return false;
984 if (diff_usecs < 250000.0) {
985 /* too short, just keep going */
986 return true;
989 /* slow it down */
991 request_transport_speed (_transport_speed * 0.75);
992 return true;
995 /*---------------------------------------------------------------------------
996 MIDI THREAD
997 ---------------------------------------------------------------------------*/
1000 Session::start_midi_thread ()
1002 midi_control_ui = new MidiControlUI (*this);
1003 midi_control_ui->run ();
1004 return 0;
1007 void
1008 Session::terminate_midi_thread ()
1010 if (midi_control_ui) {
1011 midi_control_ui->quit ();