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;
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 */
93 Session::spp_start (Parser &, nframes_t /*timestamp*/)
95 if (Config->get_mmc_control() && (!config.get_external_sync() || config.get_sync_source() != JACK)) {
96 request_transport_speed (1.0);
101 Session::spp_continue (Parser& ignored, nframes_t timestamp)
103 spp_start (ignored, timestamp);
107 Session::spp_stop (Parser&, nframes_t /*timestamp*/)
109 if (Config->get_mmc_control()) {
115 Session::mmc_deferred_play (MIDI::MachineControl &/*mmc*/)
117 if (Config->get_mmc_control() && (!config.get_external_sync() || (config.get_sync_source() != JACK))) {
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 (!Config->get_mmc_control() || (_step_editors > 0)) {
137 /* record strobe does an implicit "Play" command */
139 if (_transport_speed != 1.0) {
141 /* start_transport() will move from Enabled->Recording, so we
142 don't need to do anything here except enable recording.
143 its not the same as maybe_enable_record() though, because
144 that *can* switch to Recording, which we do not want.
147 save_state ("", true);
148 g_atomic_int_set (&_record_status, Enabled);
149 RecordStateChanged (); /* EMIT SIGNAL */
151 request_transport_speed (1.0);
160 Session::mmc_record_exit (MIDI::MachineControl &/*mmc*/)
162 if (Config->get_mmc_control()) {
163 disable_record (false);
168 Session::mmc_stop (MIDI::MachineControl &/*mmc*/)
170 if (Config->get_mmc_control()) {
176 Session::mmc_pause (MIDI::MachineControl &/*mmc*/)
178 if (Config->get_mmc_control()) {
180 /* We support RECORD_PAUSE, so the spec says that
181 we must interpret PAUSE like RECORD_PAUSE if
185 if (actively_recording()) {
186 maybe_enable_record ();
193 static bool step_queued = false;
196 Session::mmc_step (MIDI::MachineControl &/*mmc*/, int steps)
198 if (!Config->get_mmc_control()) {
203 struct timeval diff = { 0, 0 };
205 gettimeofday (&now, 0);
207 timersub (&now, &last_mmc_step, &diff);
209 gettimeofday (&now, 0);
210 timersub (&now, &last_mmc_step, &diff);
212 if (last_mmc_step.tv_sec != 0 && (diff.tv_usec + (diff.tv_sec * 1000000)) < _engine.usecs_per_cycle()) {
216 double diff_secs = diff.tv_sec + (diff.tv_usec / 1000000.0);
217 double cur_speed = (((steps * 0.5) * timecode_frames_per_second()) / diff_secs) / timecode_frames_per_second();
219 if (_transport_speed == 0 || cur_speed * _transport_speed < 0) {
220 /* change direction */
221 step_speed = cur_speed;
223 step_speed = (0.6 * step_speed) + (0.4 * cur_speed);
229 cerr << "delta = " << diff_secs
230 << " ct = " << _transport_speed
231 << " steps = " << steps
232 << " new speed = " << cur_speed
233 << " speed = " << step_speed
237 request_transport_speed (step_speed);
241 if (midi_control_ui) {
242 RefPtr<TimeoutSource> tsrc = TimeoutSource::create (100);
243 tsrc->connect (sigc::mem_fun (*this, &Session::mmc_step_timeout));
244 tsrc->attach (midi_control_ui->main_loop()->get_context());
251 Session::mmc_rewind (MIDI::MachineControl &/*mmc*/)
253 if (Config->get_mmc_control()) {
254 request_transport_speed(-8.0f);
259 Session::mmc_fast_forward (MIDI::MachineControl &/*mmc*/)
261 if (Config->get_mmc_control()) {
262 request_transport_speed(8.0f);
267 Session::mmc_locate (MIDI::MachineControl &/*mmc*/, const MIDI::byte* mmc_tc)
269 if (!Config->get_mmc_control()) {
273 framepos_t target_frame;
274 Timecode::Time timecode;
276 timecode.hours = mmc_tc[0] & 0xf;
277 timecode.minutes = mmc_tc[1];
278 timecode.seconds = mmc_tc[2];
279 timecode.frames = mmc_tc[3];
280 timecode.rate = timecode_frames_per_second();
281 timecode.drop = timecode_drop_frames();
283 timecode_to_sample (timecode, target_frame, false /* use_subframes */);
285 if (target_frame > max_framepos) {
286 target_frame = max_framepos;
289 /* Some (all?) MTC/MMC devices do not send a full MTC frame
290 at the end of a locate, instead sending only an MMC
291 locate command. This causes the current position
292 of an MTC slave to become out of date. Catch this.
295 MTC_Slave* mtcs = dynamic_cast<MTC_Slave*> (_slave);
298 // cerr << "Locate *with* MTC slave\n";
299 mtcs->handle_locate (mmc_tc);
301 // cerr << "Locate without MTC slave\n";
302 request_locate (target_frame, false);
307 Session::mmc_shuttle (MIDI::MachineControl &/*mmc*/, float speed, bool forw)
309 if (!Config->get_mmc_control()) {
313 if (Config->get_shuttle_speed_threshold() >= 0 && speed > Config->get_shuttle_speed_threshold()) {
314 speed *= Config->get_shuttle_speed_factor();
318 request_transport_speed (speed);
320 request_transport_speed (-speed);
325 Session::mmc_record_enable (MIDI::MachineControl &mmc, size_t trk, bool enabled)
327 if (Config->get_mmc_control()) {
329 RouteList::iterator i;
330 boost::shared_ptr<RouteList> r = routes.reader();
332 for (i = r->begin(); i != r->end(); ++i) {
335 if ((at = dynamic_cast<AudioTrack*>((*i).get())) != 0) {
336 if (trk == at->remote_control_id()) {
337 at->set_record_enabled (enabled, &mmc);
345 /** Send MTC Full Frame message (complete Timecode time) for the start of this cycle.
346 * This resets the MTC code, the next quarter frame message that is sent will be
347 * the first one with the beginning of this cycle as the new start point.
348 * @param t time to send.
351 Session::send_full_time_code (framepos_t const t)
353 /* This function could easily send at a given frame offset, but would
354 * that be useful? Does ardour do sub-block accurate locating? [DR] */
357 Timecode::Time timecode;
359 _send_timecode_update = false;
361 if (!session_send_mtc || _slave) {
365 // Get timecode time for the given time
366 sample_to_timecode (t, timecode, false /* no subframes */);
368 transmitting_timecode_time = timecode;
369 outbound_mtc_timecode_frame = t;
371 // I don't understand this bit yet.. [DR]
372 if (((mtc_timecode_bits >> 5) != MIDI::MTC_25_FPS) && (transmitting_timecode_time.frames % 2)) {
373 // start MTC quarter frame transmission on an even frame
374 Timecode::increment (transmitting_timecode_time, config.get_subframes_per_frame());
375 outbound_mtc_timecode_frame += (nframes_t) _frames_per_timecode_frame;
378 // Compensate for audio latency
379 outbound_mtc_timecode_frame += _worst_output_latency;
380 next_quarter_frame_to_send = 0;
382 // Sync slave to the same Timecode time as we are on
390 msg[5] = mtc_timecode_bits | timecode.hours;
391 msg[6] = timecode.minutes;
392 msg[7] = timecode.seconds;
393 msg[8] = timecode.frames;
395 // Send message at offset 0, sent time is for the start of this cycle
396 if (MIDI::Manager::instance()->mtc_output_port()->midimsg (msg, sizeof (msg), 0)) {
397 error << _("Session: could not send full MIDI time code") << endmsg;
404 /** Send MTC (quarter-frame) messages for this cycle.
405 * Must be called exactly once per cycle from the audio thread. Realtime safe.
406 * This function assumes the state of full Timecode is sane, eg. the slave is
407 * expecting quarter frame messages and has the right frame of reference (any
408 * full MTC Timecode time messages that needed to be sent should have been sent
409 * earlier already this cycle by send_full_time_code)
412 Session::send_midi_time_code_for_cycle(nframes_t nframes)
414 if (_slave || !session_send_mtc || transmitting_timecode_time.negative || (next_quarter_frame_to_send < 0)) {
415 // cerr << "(MTC) Not sending MTC\n";
419 assert (next_quarter_frame_to_send >= 0);
420 assert (next_quarter_frame_to_send <= 7);
422 /* Duration of one quarter frame */
423 nframes_t quarter_frame_duration = ((nframes_t) _frames_per_timecode_frame) >> 2;
425 DEBUG_TRACE (DEBUG::MTC, string_compose ("TF %1 SF %2 NQ %3 FD %4\n", _transport_frame, outbound_mtc_timecode_frame,
426 next_quarter_frame_to_send, quarter_frame_duration));
428 assert((outbound_mtc_timecode_frame + (next_quarter_frame_to_send * quarter_frame_duration))
429 >= _transport_frame);
432 // Send quarter frames for this cycle
433 while (_transport_frame + nframes > (outbound_mtc_timecode_frame +
434 (next_quarter_frame_to_send * quarter_frame_duration))) {
436 DEBUG_TRACE (DEBUG::MTC, string_compose ("next frame to send: %1\n", next_quarter_frame_to_send));
438 switch (next_quarter_frame_to_send) {
440 mtc_msg[1] = 0x00 | (transmitting_timecode_time.frames & 0xf);
443 mtc_msg[1] = 0x10 | ((transmitting_timecode_time.frames & 0xf0) >> 4);
446 mtc_msg[1] = 0x20 | (transmitting_timecode_time.seconds & 0xf);
449 mtc_msg[1] = 0x30 | ((transmitting_timecode_time.seconds & 0xf0) >> 4);
452 mtc_msg[1] = 0x40 | (transmitting_timecode_time.minutes & 0xf);
455 mtc_msg[1] = 0x50 | ((transmitting_timecode_time.minutes & 0xf0) >> 4);
458 mtc_msg[1] = 0x60 | ((mtc_timecode_bits|transmitting_timecode_time.hours) & 0xf);
461 mtc_msg[1] = 0x70 | (((mtc_timecode_bits|transmitting_timecode_time.hours) & 0xf0) >> 4);
465 const nframes_t msg_time = (outbound_mtc_timecode_frame
466 + (quarter_frame_duration * next_quarter_frame_to_send));
468 // This message must fall within this block or something is broken
469 assert(msg_time >= _transport_frame);
470 assert(msg_time < _transport_frame + nframes);
472 nframes_t out_stamp = msg_time - _transport_frame;
473 assert(out_stamp < nframes);
475 if (MIDI::Manager::instance()->mtc_output_port()->midimsg (mtc_msg, 2, out_stamp)) {
476 error << string_compose(_("Session: cannot send quarter-frame MTC message (%1)"), strerror (errno))
483 DEBUG_STR_APPEND(foo,"sending ");
484 DEBUG_STR_APPEND(foo, transmitting_timecode_time);
485 DEBUG_TRACE (DEBUG::MTC, string_compose ("%1 qfm = %2, stamp = %3\n", DEBUG_STR(foo).str(), next_quarter_frame_to_send,
489 // Increment quarter frame counter
490 next_quarter_frame_to_send++;
492 if (next_quarter_frame_to_send >= 8) {
493 // Wrap quarter frame counter
494 next_quarter_frame_to_send = 0;
495 // Increment timecode time twice
496 Timecode::increment( transmitting_timecode_time, config.get_subframes_per_frame() );
497 Timecode::increment( transmitting_timecode_time, config.get_subframes_per_frame() );
498 // Re-calculate timing of first quarter frame
499 //timecode_to_sample( transmitting_timecode_time, outbound_mtc_timecode_frame, true /* use_offset */, false );
500 outbound_mtc_timecode_frame += 8 * quarter_frame_duration;
507 /***********************************************************************
509 **********************************************************************/
513 Session::mmc_step_timeout ()
518 gettimeofday (&now, 0);
520 timersub (&now, &last_mmc_step, &diff);
521 diff_usecs = diff.tv_sec * 1000000 + diff.tv_usec;
523 if (diff_usecs > 1000000.0 || fabs (_transport_speed) < 0.0000001) {
524 /* too long or too slow, stop transport */
525 request_transport_speed (0.0);
530 if (diff_usecs < 250000.0) {
531 /* too short, just keep going */
537 request_transport_speed (_transport_speed * 0.75);
541 /*---------------------------------------------------------------------------
543 ---------------------------------------------------------------------------*/
546 Session::start_midi_thread ()
548 midi_control_ui = new MidiControlUI (*this);
549 midi_control_ui->run ();
554 Session::terminate_midi_thread ()
556 if (midi_control_ui) {
557 midi_control_ui->quit ();