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.
28 #include <boost/shared_ptr.hpp>
30 #include <glibmm/main.h>
32 #include "midi++/mmc.h"
33 #include "midi++/port.h"
34 #include "midi++/manager.h"
36 #include "pbd/error.h"
37 #include "pbd/pthread_utils.h"
39 #include "timecode/time.h"
41 #include "ardour/configuration.h"
42 #include "ardour/debug.h"
43 #include "ardour/audioengine.h"
44 #include "ardour/session.h"
45 #include "ardour/audio_track.h"
46 #include "ardour/midi_track.h"
47 #include "ardour/midi_ui.h"
48 #include "ardour/audio_diskstream.h"
49 #include "ardour/slave.h"
50 #include "ardour/cycles.h"
55 using namespace ARDOUR
;
64 boost::shared_ptr
<RouteList
> r
= routes
.reader ();
66 for (RouteList::iterator i
= r
->begin(); i
!= r
->end(); ++i
) {
67 MidiTrack
*track
= dynamic_cast<MidiTrack
*>((*i
).get());
76 Session::setup_midi_control ()
78 outbound_mtc_timecode_frame
= 0;
79 next_quarter_frame_to_send
= 0;
81 /* Set up the qtr frame message */
94 Session::spp_start (Parser
&, framepos_t
/*timestamp*/)
96 if (Config
->get_mmc_control ()) {
97 request_transport_speed (1.0);
102 Session::spp_continue (Parser
& ignored
, framepos_t timestamp
)
104 spp_start (ignored
, timestamp
);
108 Session::spp_stop (Parser
&, framepos_t
/*timestamp*/)
110 if (Config
->get_mmc_control ()) {
116 Session::mmc_deferred_play (MIDI::MachineControl
&/*mmc*/)
118 if (Config
->get_mmc_control ()) {
119 request_transport_speed (1.0);
124 Session::mmc_record_pause (MIDI::MachineControl
&/*mmc*/)
126 if (Config
->get_mmc_control ()) {
127 maybe_enable_record();
132 Session::mmc_record_strobe (MIDI::MachineControl
&/*mmc*/)
134 if (!Config
->get_mmc_control() || (_step_editors
> 0)) {
138 /* record strobe does an implicit "Play" command */
140 if (_transport_speed
!= 1.0) {
142 /* start_transport() will move from Enabled->Recording, so we
143 don't need to do anything here except enable recording.
144 its not the same as maybe_enable_record() though, because
145 that *can* switch to Recording, which we do not want.
148 save_state ("", true);
149 g_atomic_int_set (&_record_status
, Enabled
);
150 RecordStateChanged (); /* EMIT SIGNAL */
152 request_transport_speed (1.0);
161 Session::mmc_record_exit (MIDI::MachineControl
&/*mmc*/)
163 if (Config
->get_mmc_control ()) {
164 disable_record (false);
169 Session::mmc_stop (MIDI::MachineControl
&/*mmc*/)
171 if (Config
->get_mmc_control ()) {
177 Session::mmc_pause (MIDI::MachineControl
&/*mmc*/)
179 if (Config
->get_mmc_control ()) {
181 /* We support RECORD_PAUSE, so the spec says that
182 we must interpret PAUSE like RECORD_PAUSE if
186 if (actively_recording()) {
187 maybe_enable_record ();
194 static bool step_queued
= false;
197 Session::mmc_step (MIDI::MachineControl
&/*mmc*/, int steps
)
199 if (!Config
->get_mmc_control ()) {
204 struct timeval diff
= { 0, 0 };
206 gettimeofday (&now
, 0);
208 timersub (&now
, &last_mmc_step
, &diff
);
210 gettimeofday (&now
, 0);
211 timersub (&now
, &last_mmc_step
, &diff
);
213 if (last_mmc_step
.tv_sec
!= 0 && (diff
.tv_usec
+ (diff
.tv_sec
* 1000000)) < _engine
.usecs_per_cycle()) {
217 double diff_secs
= diff
.tv_sec
+ (diff
.tv_usec
/ 1000000.0);
218 double cur_speed
= (((steps
* 0.5) * timecode_frames_per_second()) / diff_secs
) / timecode_frames_per_second();
220 if (_transport_speed
== 0 || cur_speed
* _transport_speed
< 0) {
221 /* change direction */
222 step_speed
= cur_speed
;
224 step_speed
= (0.6 * step_speed
) + (0.4 * cur_speed
);
230 cerr
<< "delta = " << diff_secs
231 << " ct = " << _transport_speed
232 << " steps = " << steps
233 << " new speed = " << cur_speed
234 << " speed = " << step_speed
238 request_transport_speed_nonzero (step_speed
);
242 if (midi_control_ui
) {
243 RefPtr
<TimeoutSource
> tsrc
= TimeoutSource::create (100);
244 tsrc
->connect (sigc::mem_fun (*this, &Session::mmc_step_timeout
));
245 tsrc
->attach (midi_control_ui
->main_loop()->get_context());
252 Session::mmc_rewind (MIDI::MachineControl
&/*mmc*/)
254 if (Config
->get_mmc_control ()) {
255 request_transport_speed(-8.0f
);
260 Session::mmc_fast_forward (MIDI::MachineControl
&/*mmc*/)
262 if (Config
->get_mmc_control ()) {
263 request_transport_speed(8.0f
);
268 Session::mmc_locate (MIDI::MachineControl
&/*mmc*/, const MIDI::byte
* mmc_tc
)
270 if (!Config
->get_mmc_control ()) {
274 framepos_t target_frame
;
275 Timecode::Time timecode
;
277 timecode
.hours
= mmc_tc
[0] & 0xf;
278 timecode
.minutes
= mmc_tc
[1];
279 timecode
.seconds
= mmc_tc
[2];
280 timecode
.frames
= mmc_tc
[3];
281 timecode
.rate
= timecode_frames_per_second();
282 timecode
.drop
= timecode_drop_frames();
284 // Also takes timecode offset into account:
285 timecode_to_sample( timecode
, target_frame
, true /* use_offset */, false /* use_subframes */ );
287 if (target_frame
> max_framepos
) {
288 target_frame
= max_framepos
;
291 /* Some (all?) MTC/MMC devices do not send a full MTC frame
292 at the end of a locate, instead sending only an MMC
293 locate command. This causes the current position
294 of an MTC slave to become out of date. Catch this.
297 MTC_Slave
* mtcs
= dynamic_cast<MTC_Slave
*> (_slave
);
300 // cerr << "Locate *with* MTC slave\n";
301 mtcs
->handle_locate (mmc_tc
);
303 // cerr << "Locate without MTC slave\n";
304 request_locate (target_frame
, false);
309 Session::mmc_shuttle (MIDI::MachineControl
&/*mmc*/, float speed
, bool forw
)
311 if (!Config
->get_mmc_control ()) {
315 if (Config
->get_shuttle_speed_threshold() >= 0 && speed
> Config
->get_shuttle_speed_threshold()) {
316 speed
*= Config
->get_shuttle_speed_factor();
320 request_transport_speed_nonzero (speed
);
322 request_transport_speed_nonzero (-speed
);
327 Session::mmc_record_enable (MIDI::MachineControl
&mmc
, size_t trk
, bool enabled
)
329 if (!Config
->get_mmc_control ()) {
333 RouteList::iterator i
;
334 boost::shared_ptr
<RouteList
> r
= routes
.reader();
336 for (i
= r
->begin(); i
!= r
->end(); ++i
) {
339 if ((at
= dynamic_cast<AudioTrack
*>((*i
).get())) != 0) {
340 if (trk
== at
->remote_control_id()) {
341 at
->set_record_enabled (enabled
, &mmc
);
348 /** Send MTC Full Frame message (complete Timecode time) for the start of this cycle.
349 * This resets the MTC code, the next quarter frame message that is sent will be
350 * the first one with the beginning of this cycle as the new start point.
351 * @param t time to send.
354 Session::send_full_time_code (framepos_t
const t
)
356 /* This function could easily send at a given frame offset, but would
357 * that be useful? Does ardour do sub-block accurate locating? [DR] */
360 Timecode::Time timecode
;
362 _send_timecode_update
= false;
364 if (!Config
->get_send_mtc() || _slave
) {
368 // Get timecode time for the given time
369 sample_to_timecode (t
, timecode
, true /* use_offset */, false /* no subframes */);
371 transmitting_timecode_time
= timecode
;
372 outbound_mtc_timecode_frame
= t
;
374 // I don't understand this bit yet.. [DR]
375 if (((mtc_timecode_bits
>> 5) != MIDI::MTC_25_FPS
) && (transmitting_timecode_time
.frames
% 2)) {
376 // start MTC quarter frame transmission on an even frame
377 Timecode::increment (transmitting_timecode_time
, config
.get_subframes_per_frame());
378 outbound_mtc_timecode_frame
+= _frames_per_timecode_frame
;
381 // Compensate for audio latency
382 outbound_mtc_timecode_frame
+= worst_playback_latency();
383 next_quarter_frame_to_send
= 0;
385 // Sync slave to the same Timecode time as we are on
393 msg
[5] = mtc_timecode_bits
| timecode
.hours
;
394 msg
[6] = timecode
.minutes
;
395 msg
[7] = timecode
.seconds
;
396 msg
[8] = timecode
.frames
;
398 // Send message at offset 0, sent time is for the start of this cycle
399 if (MIDI::Manager::instance()->mtc_output_port()->midimsg (msg
, sizeof (msg
), 0)) {
400 error
<< _("Session: could not send full MIDI time code") << endmsg
;
404 _pframes_since_last_mtc
= 0;
408 /** Send MTC (quarter-frame) messages for this cycle.
409 * Must be called exactly once per cycle from the process thread. Realtime safe.
410 * This function assumes the state of full Timecode is sane, eg. the slave is
411 * expecting quarter frame messages and has the right frame of reference (any
412 * full MTC Timecode time messages that needed to be sent should have been sent
413 * earlier already this cycle by send_full_time_code)
416 Session::send_midi_time_code_for_cycle (framepos_t start_frame
, framepos_t end_frame
, pframes_t nframes
)
418 if (_slave
|| !_send_qf_mtc
|| transmitting_timecode_time
.negative
|| (next_quarter_frame_to_send
< 0)) {
419 // cerr << "(MTC) Not sending MTC\n";
423 assert (next_quarter_frame_to_send
>= 0);
424 assert (next_quarter_frame_to_send
<= 7);
426 /* Duration of one quarter frame */
427 framecnt_t
const quarter_frame_duration
= ((framecnt_t
) _frames_per_timecode_frame
) >> 2;
429 DEBUG_TRACE (DEBUG::MTC
, string_compose ("TF %1 SF %2 NQ %3 FD %4\n", start_frame
, outbound_mtc_timecode_frame
,
430 next_quarter_frame_to_send
, quarter_frame_duration
));
432 assert ((outbound_mtc_timecode_frame
+ (next_quarter_frame_to_send
* quarter_frame_duration
)) >= _transport_frame
);
434 /* Send quarter frames for this cycle */
435 while (end_frame
> (outbound_mtc_timecode_frame
+ (next_quarter_frame_to_send
* quarter_frame_duration
))) {
437 DEBUG_TRACE (DEBUG::MTC
, string_compose ("next frame to send: %1\n", next_quarter_frame_to_send
));
439 switch (next_quarter_frame_to_send
) {
441 mtc_msg
[1] = 0x00 | (transmitting_timecode_time
.frames
& 0xf);
444 mtc_msg
[1] = 0x10 | ((transmitting_timecode_time
.frames
& 0xf0) >> 4);
447 mtc_msg
[1] = 0x20 | (transmitting_timecode_time
.seconds
& 0xf);
450 mtc_msg
[1] = 0x30 | ((transmitting_timecode_time
.seconds
& 0xf0) >> 4);
453 mtc_msg
[1] = 0x40 | (transmitting_timecode_time
.minutes
& 0xf);
456 mtc_msg
[1] = 0x50 | ((transmitting_timecode_time
.minutes
& 0xf0) >> 4);
459 mtc_msg
[1] = 0x60 | ((mtc_timecode_bits
| transmitting_timecode_time
.hours
) & 0xf);
462 mtc_msg
[1] = 0x70 | (((mtc_timecode_bits
| transmitting_timecode_time
.hours
) & 0xf0) >> 4);
466 const framepos_t msg_time
= outbound_mtc_timecode_frame
+ (quarter_frame_duration
* next_quarter_frame_to_send
);
468 // This message must fall within this block or something is broken
469 assert (msg_time
>= start_frame
);
470 assert (msg_time
< end_frame
);
472 /* convert from session frames back to JACK frames using the transport speed */
473 pframes_t
const out_stamp
= (msg_time
- start_frame
) / _transport_speed
;
474 assert (out_stamp
< nframes
);
476 if (MIDI::Manager::instance()->mtc_output_port()->midimsg (mtc_msg
, 2, out_stamp
)) {
477 error
<< string_compose(_("Session: cannot send quarter-frame MTC message (%1)"), strerror (errno
))
484 DEBUG_STR_APPEND(foo
,"sending ");
485 DEBUG_STR_APPEND(foo
, transmitting_timecode_time
);
486 DEBUG_TRACE (DEBUG::MTC
, string_compose ("%1 qfm = %2, stamp = %3\n", DEBUG_STR(foo
).str(), next_quarter_frame_to_send
,
490 // Increment quarter frame counter
491 next_quarter_frame_to_send
++;
493 if (next_quarter_frame_to_send
>= 8) {
494 // Wrap quarter frame counter
495 next_quarter_frame_to_send
= 0;
496 // Increment timecode time twice
497 Timecode::increment (transmitting_timecode_time
, config
.get_subframes_per_frame());
498 Timecode::increment (transmitting_timecode_time
, config
.get_subframes_per_frame());
499 // Re-calculate timing of first quarter frame
500 //timecode_to_sample( transmitting_timecode_time, outbound_mtc_timecode_frame, true /* use_offset */, false );
501 outbound_mtc_timecode_frame
+= 8 * quarter_frame_duration
;
508 /***********************************************************************
510 **********************************************************************/
514 Session::mmc_step_timeout ()
519 gettimeofday (&now
, 0);
521 timersub (&now
, &last_mmc_step
, &diff
);
522 diff_usecs
= diff
.tv_sec
* 1000000 + diff
.tv_usec
;
524 if (diff_usecs
> 1000000.0 || fabs (_transport_speed
) < 0.0000001) {
525 /* too long or too slow, stop transport */
526 request_transport_speed (0.0);
531 if (diff_usecs
< 250000.0) {
532 /* too short, just keep going */
538 request_transport_speed_nonzero (_transport_speed
* 0.75);
542 /*---------------------------------------------------------------------------
544 ---------------------------------------------------------------------------*/
547 Session::start_midi_thread ()
549 midi_control_ui
= new MidiControlUI (*this);
550 midi_control_ui
->run ();
555 Session::terminate_midi_thread ()
557 if (midi_control_ui
) {
558 midi_control_ui
->quit ();