2 Copyright (C) 1999-2002 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.
26 #include <boost/shared_ptr.hpp>
28 #include <glibmm/main.h>
30 #include "midi++/mmc.h"
31 #include "midi++/port.h"
33 #include "pbd/error.h"
34 #include "pbd/pthread_utils.h"
35 #include "pbd/timersub.h"
36 #include "pbd/stacktrace.h"
38 #include "timecode/time.h"
40 #include "ardour/audio_track.h"
41 #include "ardour/audioengine.h"
42 #include "ardour/debug.h"
43 #include "ardour/midi_port.h"
44 #include "ardour/midi_track.h"
45 #include "ardour/midi_ui.h"
46 #include "ardour/profile.h"
47 #include "ardour/session.h"
48 #include "ardour/slave.h"
49 #include "ardour/ticker.h"
54 using namespace ARDOUR;
63 boost::shared_ptr<RouteList> r = routes.reader ();
65 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
66 MidiTrack *track = dynamic_cast<MidiTrack*>((*i).get());
75 Session::setup_midi_control ()
77 outbound_mtc_timecode_frame = 0;
78 next_quarter_frame_to_send = 0;
80 /* Set up the qtr frame message */
95 if (Config->get_mmc_control ()) {
96 request_transport_speed (1.0);
101 Session::spp_continue ()
109 if (Config->get_mmc_control ()) {
115 Session::mmc_deferred_play (MIDI::MachineControl &/*mmc*/)
117 if (Config->get_mmc_control ()) {
118 request_transport_speed (1.0);
123 Session::mmc_record_pause (MIDI::MachineControl &/*mmc*/)
125 if (Config->get_mmc_control ()) {
126 maybe_enable_record();
131 Session::mmc_record_strobe (MIDI::MachineControl &/*mmc*/)
133 if (Profile->get_trx()) {
135 /* In Tracks Live, there is no concept of punch, so we just
136 treat RecordStrobe like RecordPause. This violates the MMC
140 if (Config->get_mmc_control()) {
141 maybe_enable_record();
146 if (!Config->get_mmc_control() || (_step_editors > 0)) {
150 /* record strobe does an implicit "Play" command */
152 if (_transport_speed != 1.0) {
154 /* start_transport() will move from Enabled->Recording, so we
155 don't need to do anything here except enable recording.
156 its not the same as maybe_enable_record() though, because
157 that *can* switch to Recording, which we do not want.
160 save_state ("", true);
161 g_atomic_int_set (&_record_status, Enabled);
162 RecordStateChanged (); /* EMIT SIGNAL */
164 request_transport_speed (1.0);
173 Session::mmc_record_exit (MIDI::MachineControl &/*mmc*/)
175 if (Config->get_mmc_control ()) {
176 disable_record (false);
181 Session::mmc_stop (MIDI::MachineControl &/*mmc*/)
183 if (Config->get_mmc_control ()) {
189 Session::mmc_pause (MIDI::MachineControl &/*mmc*/)
191 if (Config->get_mmc_control ()) {
193 /* We support RECORD_PAUSE, so the spec says that
194 we must interpret PAUSE like RECORD_PAUSE if
198 if (actively_recording()) {
199 maybe_enable_record ();
206 static bool step_queued = false;
209 Session::mmc_step (MIDI::MachineControl &/*mmc*/, int steps)
211 if (!Config->get_mmc_control ()) {
216 struct timeval diff = { 0, 0 };
218 gettimeofday (&now, 0);
220 timersub (&now, &last_mmc_step, &diff);
222 gettimeofday (&now, 0);
223 timersub (&now, &last_mmc_step, &diff);
225 if (last_mmc_step.tv_sec != 0 && (diff.tv_usec + (diff.tv_sec * 1000000)) < _engine.usecs_per_cycle()) {
229 double diff_secs = diff.tv_sec + (diff.tv_usec / 1000000.0);
230 double cur_speed = (((steps * 0.5) * timecode_frames_per_second()) / diff_secs) / timecode_frames_per_second();
232 if (_transport_speed == 0 || cur_speed * _transport_speed < 0) {
233 /* change direction */
234 step_speed = cur_speed;
236 step_speed = (0.6 * step_speed) + (0.4 * cur_speed);
242 cerr << "delta = " << diff_secs
243 << " ct = " << _transport_speed
244 << " steps = " << steps
245 << " new speed = " << cur_speed
246 << " speed = " << step_speed
250 request_transport_speed_nonzero (step_speed);
254 if (midi_control_ui) {
255 RefPtr<TimeoutSource> tsrc = TimeoutSource::create (100);
256 tsrc->connect (sigc::mem_fun (*this, &Session::mmc_step_timeout));
257 tsrc->attach (midi_control_ui->main_loop()->get_context());
264 Session::mmc_rewind (MIDI::MachineControl &/*mmc*/)
266 if (Config->get_mmc_control ()) {
267 request_transport_speed(-8.0f);
272 Session::mmc_fast_forward (MIDI::MachineControl &/*mmc*/)
274 if (Config->get_mmc_control ()) {
275 request_transport_speed(8.0f);
280 Session::mmc_locate (MIDI::MachineControl &/*mmc*/, const MIDI::byte* mmc_tc)
282 if (!Config->get_mmc_control ()) {
286 framepos_t target_frame;
287 Timecode::Time timecode;
289 timecode.hours = mmc_tc[0] & 0xf;
290 timecode.minutes = mmc_tc[1];
291 timecode.seconds = mmc_tc[2];
292 timecode.frames = mmc_tc[3];
293 timecode.rate = timecode_frames_per_second();
294 timecode.drop = timecode_drop_frames();
296 // Also takes timecode offset into account:
297 timecode_to_sample( timecode, target_frame, true /* use_offset */, false /* use_subframes */ );
299 if (target_frame > max_framepos) {
300 target_frame = max_framepos;
303 /* Some (all?) MTC/MMC devices do not send a full MTC frame
304 at the end of a locate, instead sending only an MMC
305 locate command. This causes the current position
306 of an MTC slave to become out of date. Catch this.
309 MTC_Slave* mtcs = dynamic_cast<MTC_Slave*> (_slave);
312 // cerr << "Locate *with* MTC slave\n";
313 mtcs->handle_locate (mmc_tc);
315 // cerr << "Locate without MTC slave\n";
316 request_locate (target_frame, false);
321 Session::mmc_shuttle (MIDI::MachineControl &/*mmc*/, float speed, bool forw)
323 if (!Config->get_mmc_control ()) {
327 if (Config->get_shuttle_speed_threshold() >= 0 && speed > Config->get_shuttle_speed_threshold()) {
328 speed *= Config->get_shuttle_speed_factor();
332 request_transport_speed_nonzero (speed);
334 request_transport_speed_nonzero (-speed);
338 boost::shared_ptr<Route>
339 Session::get_midi_nth_route_by_id (PresentationInfo::order_t n) const
341 PresentationInfo::Flag f;
343 /* These numbers are defined by the MMC specification.
347 f = PresentationInfo::MasterOut;
348 } else if (n == 319) {
349 f = PresentationInfo::MonitorOut;
351 f = PresentationInfo::Route;
354 boost::shared_ptr<RouteList> r = routes.reader ();
355 PresentationInfo::order_t match_cnt = 0;
357 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
358 if ((*i)->presentation_info().flag_match (f)) {
359 if (match_cnt++ == n) {
365 return boost::shared_ptr<Route>();
369 Session::mmc_record_enable (MIDI::MachineControl &mmc, size_t trk, bool enabled)
371 if (!Config->get_mmc_control ()) {
375 boost::shared_ptr<Route> r = get_midi_nth_route_by_id (trk);
378 boost::shared_ptr<AudioTrack> at;
380 if ((at = boost::dynamic_pointer_cast<AudioTrack> (r))) {
381 at->rec_enable_control()->set_value (enabled, Controllable::UseGroup);
386 /** Send MTC Full Frame message (complete Timecode time) for the start of this cycle.
387 * This resets the MTC code, the next quarter frame message that is sent will be
388 * the first one with the beginning of this cycle as the new start point.
389 * @param t time to send.
392 Session::send_full_time_code (framepos_t const t, MIDI::pframes_t nframes)
394 /* This function could easily send at a given frame offset, but would
395 * that be useful? Does ardour do sub-block accurate locating? [DR] */
398 Timecode::Time timecode;
400 _send_timecode_update = false;
402 if (_engine.freewheeling() || !Config->get_send_mtc()) {
405 if (_slave && !_slave->locked()) {
409 // Get timecode time for the given time
410 sample_to_timecode (t, timecode, true /* use_offset */, false /* no subframes */);
412 // sample-align outbound to rounded (no subframes) timecode
414 timecode_to_sample(timecode, mtc_tc, true, false);
415 outbound_mtc_timecode_frame = mtc_tc;
416 transmitting_timecode_time = timecode;
418 LatencyRange mtc_out_latency = {0, 0}; // TODO cache this, update on engine().GraphReordered()
419 _midi_ports->mtc_output_port ()->get_connected_latency_range (ltc_out_latency, true);
420 frameoffset_t mtc_offset = worst_playback_latency() - mtc_out_latency.max;
422 // only if rolling.. ?
423 outbound_mtc_timecode_frame += mtc_offset;
425 // outbound_mtc_timecode_frame needs to be >= _transport_frame
426 // or a new full timecode will be queued next cycle.
427 while (outbound_mtc_timecode_frame < t) {
428 Timecode::increment (transmitting_timecode_time, config.get_subframes_per_frame());
429 outbound_mtc_timecode_frame += _frames_per_timecode_frame;
432 double const quarter_frame_duration = ((framecnt_t) _frames_per_timecode_frame) / 4.0;
433 if (ceil((t - mtc_tc) / quarter_frame_duration) > 0) {
434 Timecode::increment (transmitting_timecode_time, config.get_subframes_per_frame());
435 outbound_mtc_timecode_frame += _frames_per_timecode_frame;
438 DEBUG_TRACE (DEBUG::MTC, string_compose ("Full MTC TC %1 (off %2)\n", outbound_mtc_timecode_frame, mtc_offset));
440 // I don't understand this bit yet.. [DR]
442 // according to MTC spec 24, 30 drop and 30 non-drop TC, the frame-number represented by 8 quarter frames must be even.
443 if (((mtc_timecode_bits >> 5) != MIDI::MTC_25_FPS) && (transmitting_timecode_time.frames % 2)) {
444 // start MTC quarter frame transmission on an even frame
445 Timecode::increment (transmitting_timecode_time, config.get_subframes_per_frame());
446 outbound_mtc_timecode_frame += _frames_per_timecode_frame;
449 next_quarter_frame_to_send = 0;
451 // Sync slave to the same Timecode time as we are on
459 msg[5] = mtc_timecode_bits | (timecode.hours % 24);
460 msg[6] = timecode.minutes;
461 msg[7] = timecode.seconds;
462 msg[8] = timecode.frames;
464 // Send message at offset 0, sent time is for the start of this cycle
466 MidiBuffer& mb (_midi_ports->mtc_output_port()->get_midi_buffer (nframes));
467 mb.push_back (Port::port_offset(), sizeof (msg), msg);
469 _pframes_since_last_mtc = 0;
473 /** Send MTC (quarter-frame) messages for this cycle.
474 * Must be called exactly once per cycle from the process thread. Realtime safe.
475 * This function assumes the state of full Timecode is sane, eg. the slave is
476 * expecting quarter frame messages and has the right frame of reference (any
477 * full MTC Timecode time messages that needed to be sent should have been sent
478 * earlier already this cycle by send_full_time_code)
481 Session::send_midi_time_code_for_cycle (framepos_t start_frame, framepos_t end_frame, ARDOUR::pframes_t nframes)
483 // start_frame == start_frame for normal cycles
484 // start_frame > _transport_frame for split cycles
485 if (_engine.freewheeling() || !_send_qf_mtc || transmitting_timecode_time.negative || (next_quarter_frame_to_send < 0)) {
486 // cerr << "(MTC) Not sending MTC\n";
489 if (_slave && !_slave->locked()) {
493 if (_transport_speed < 0) {
494 // we don't support rolling backwards
498 /* MTC is max. 30 fps - assert() below will fail
499 * TODO actually limit it to 24,25,29df,30fps
500 * talk to oofus, first.
502 if (Timecode::timecode_to_frames_per_second(config.get_timecode_format()) > 30) {
506 assert (next_quarter_frame_to_send >= 0);
507 assert (next_quarter_frame_to_send <= 7);
509 /* Duration of one quarter frame */
510 double const quarter_frame_duration = _frames_per_timecode_frame / 4.0;
512 DEBUG_TRACE (DEBUG::MTC, string_compose ("TF %1 SF %2 MT %3 QF %4 QD %5\n",
513 _transport_frame, start_frame, outbound_mtc_timecode_frame,
514 next_quarter_frame_to_send, quarter_frame_duration));
516 if (rint(outbound_mtc_timecode_frame + (next_quarter_frame_to_send * quarter_frame_duration)) < _transport_frame) {
517 // send full timecode and set outbound_mtc_timecode_frame, next_quarter_frame_to_send
518 send_full_time_code (_transport_frame, nframes);
521 if (rint(outbound_mtc_timecode_frame + (next_quarter_frame_to_send * quarter_frame_duration)) < start_frame) {
522 // no QF for this cycle
526 /* Send quarter frames for this cycle */
527 while (end_frame > rint(outbound_mtc_timecode_frame + (next_quarter_frame_to_send * quarter_frame_duration))) {
529 DEBUG_TRACE (DEBUG::MTC, string_compose ("next frame to send: %1\n", next_quarter_frame_to_send));
531 switch (next_quarter_frame_to_send) {
533 mtc_msg[1] = 0x00 | (transmitting_timecode_time.frames & 0xf);
536 mtc_msg[1] = 0x10 | ((transmitting_timecode_time.frames & 0xf0) >> 4);
539 mtc_msg[1] = 0x20 | (transmitting_timecode_time.seconds & 0xf);
542 mtc_msg[1] = 0x30 | ((transmitting_timecode_time.seconds & 0xf0) >> 4);
545 mtc_msg[1] = 0x40 | (transmitting_timecode_time.minutes & 0xf);
548 mtc_msg[1] = 0x50 | ((transmitting_timecode_time.minutes & 0xf0) >> 4);
551 mtc_msg[1] = 0x60 | ((mtc_timecode_bits | transmitting_timecode_time.hours) & 0xf);
554 mtc_msg[1] = 0x70 | (((mtc_timecode_bits | transmitting_timecode_time.hours) & 0xf0) >> 4);
558 const framepos_t msg_time = rint(outbound_mtc_timecode_frame + (quarter_frame_duration * next_quarter_frame_to_send));
560 // This message must fall within this block or something is broken
561 assert (msg_time >= start_frame);
562 assert (msg_time < end_frame);
564 /* convert from session frames back to JACK frames using the transport speed */
565 ARDOUR::pframes_t const out_stamp = (msg_time - start_frame) / _transport_speed;
566 assert (out_stamp < nframes);
568 MidiBuffer& mb (_midi_ports->mtc_output_port()->get_midi_buffer(nframes));
569 if (!mb.push_back (out_stamp, 2, mtc_msg)) {
570 error << string_compose(_("Session: cannot send quarter-frame MTC message (%1)"), strerror (errno))
576 if (DEBUG_ENABLED(DEBUG::MTC)) {
578 DEBUG_STR_APPEND(foo,"sending ");
579 DEBUG_STR_APPEND(foo, transmitting_timecode_time);
580 DEBUG_TRACE (DEBUG::MTC, string_compose ("%1 qfm = %2, stamp = %3\n", DEBUG_STR(foo).str(), next_quarter_frame_to_send,
585 // Increment quarter frame counter
586 next_quarter_frame_to_send++;
588 if (next_quarter_frame_to_send >= 8) {
589 // Wrap quarter frame counter
590 next_quarter_frame_to_send = 0;
591 // Increment timecode time twice
592 Timecode::increment (transmitting_timecode_time, config.get_subframes_per_frame());
593 Timecode::increment (transmitting_timecode_time, config.get_subframes_per_frame());
594 // Increment timing of first quarter frame
595 outbound_mtc_timecode_frame += 2.0 * _frames_per_timecode_frame;
602 /***********************************************************************
604 **********************************************************************/
607 Session::send_immediate_mmc (MachineControlCommand c)
609 if (AudioEngine::instance()->in_process_thread()) {
610 _mmc->send (c, Port::port_offset());
618 Session::mmc_step_timeout ()
623 gettimeofday (&now, 0);
625 timersub (&now, &last_mmc_step, &diff);
626 diff_usecs = diff.tv_sec * 1000000 + diff.tv_usec;
628 if (diff_usecs > 1000000.0 || fabs (_transport_speed) < 0.0000001) {
629 /* too long or too slow, stop transport */
630 request_transport_speed (0.0);
635 if (diff_usecs < 250000.0) {
636 /* too short, just keep going */
642 request_transport_speed_nonzero (_transport_speed * 0.75);
646 /***********************************************************************
647 OUTBOUND SYSTEM COMMON STUFF
648 **********************************************************************/
652 Session::send_song_position_pointer (framepos_t)
655 /* Do nothing for the moment */
660 Session::start_midi_thread ()
662 if (midi_control_ui) { return 0; }
663 midi_control_ui = new MidiControlUI (*this);
664 midi_control_ui->run ();
668 boost::shared_ptr<ARDOUR::Port>
669 Session::midi_input_port () const
671 return _midi_ports->midi_input_port ();
674 boost::shared_ptr<ARDOUR::Port>
675 Session::midi_output_port () const
677 return _midi_ports->midi_output_port ();
680 boost::shared_ptr<ARDOUR::Port>
681 Session::mmc_output_port () const
683 return _midi_ports->mmc_output_port ();
686 boost::shared_ptr<ARDOUR::Port>
687 Session::mmc_input_port () const
689 return _midi_ports->mmc_input_port ();
692 boost::shared_ptr<ARDOUR::Port>
693 Session::scene_output_port () const
695 return _midi_ports->scene_output_port ();
698 boost::shared_ptr<ARDOUR::Port>
699 Session::scene_input_port () const
701 return _midi_ports->scene_input_port ();
704 boost::shared_ptr<MidiPort>
705 Session::midi_clock_output_port () const
707 return _midi_ports->midi_clock_output_port ();
710 boost::shared_ptr<MidiPort>
711 Session::midi_clock_input_port () const
713 return _midi_ports->midi_clock_input_port ();
715 boost::shared_ptr<MidiPort>
716 Session::mtc_output_port () const
718 return _midi_ports->mtc_output_port ();
720 boost::shared_ptr<MidiPort>
721 Session::mtc_input_port () const
723 return _midi_ports->mtc_input_port ();
727 Session::midi_track_presentation_info_changed (PropertyChange const& what_changed, boost::weak_ptr<MidiTrack> mt)
729 if (!Config->get_midi_input_follows_selection()) {
733 if (!what_changed.contains (Properties::selected)) {
737 boost::shared_ptr<MidiTrack> new_midi_target (mt.lock ());
739 if (new_midi_target->presentation_info().selected()) {
740 rewire_selected_midi (new_midi_target);
745 Session::rewire_selected_midi (boost::shared_ptr<MidiTrack> new_midi_target)
747 if (!new_midi_target) {
751 boost::shared_ptr<MidiTrack> old_midi_target = current_midi_target.lock ();
753 if (new_midi_target == old_midi_target) {
758 AudioEngine::instance()->get_midi_selection_ports (msp);
762 if (old_midi_target) {
763 old_midi_target->input()->disconnect (this);
766 for (vector<string>::const_iterator p = msp.begin(); p != msp.end(); ++p) {
767 /* disconnect the port from everything */
768 AudioEngine::instance()->disconnect (*p);
769 /* connect it to the new target */
770 new_midi_target->input()->connect (new_midi_target->input()->nth(0), (*p), this);
774 current_midi_target = new_midi_target;
778 Session::rewire_midi_selection_ports ()
780 if (!Config->get_midi_input_follows_selection()) {
784 boost::shared_ptr<MidiTrack> target = current_midi_target.lock();
791 AudioEngine::instance()->get_midi_selection_ports (msp);
797 target->input()->disconnect (this);
799 for (vector<string>::const_iterator p = msp.begin(); p != msp.end(); ++p) {
800 AudioEngine::instance()->disconnect (*p);
801 target->input()->connect (target->input()->nth (0), (*p), this);