26 #include <boost/shared_ptr.hpp>
28 #include <glibmm/main.h>
30 #include "midi++/mmc.h"
31 #include "midi++/port.h"
38 #include "timecode/time.h"
42 #include "ardour/debug.h"
64 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
74 Session::setup_midi_control ()
76 outbound_mtc_timecode_frame = 0;
77 next_quarter_frame_to_send = 0;
94 if (
Config->get_mmc_control ()) {
95 request_transport_speed (1.0);
100 Session::spp_continue ()
108 if (
Config->get_mmc_control ()) {
114 Session::mmc_deferred_play (MIDI::MachineControl &)
116 if (
Config->get_mmc_control ()) {
117 request_transport_speed (1.0);
122 Session::mmc_record_pause (MIDI::MachineControl &)
124 if (
Config->get_mmc_control ()) {
125 maybe_enable_record();
130 Session::mmc_record_strobe (MIDI::MachineControl &)
132 if (!
Config->get_mmc_control() || (_step_editors > 0)) {
138 if (_transport_speed != 1.0) {
146 save_state (
"",
true);
147 g_atomic_int_set (&_record_status, Enabled);
148 RecordStateChanged ();
150 request_transport_speed (1.0);
159 Session::mmc_record_exit (MIDI::MachineControl &)
161 if (
Config->get_mmc_control ()) {
162 disable_record (
false);
167 Session::mmc_stop (MIDI::MachineControl &)
169 if (
Config->get_mmc_control ()) {
175 Session::mmc_pause (MIDI::MachineControl &)
177 if (
Config->get_mmc_control ()) {
184 if (actively_recording()) {
185 maybe_enable_record ();
195 Session::mmc_step (MIDI::MachineControl &,
int steps)
197 if (!
Config->get_mmc_control ()) {
202 struct timeval diff = { 0, 0 };
204 gettimeofday (&now, 0);
206 timersub (&now, &last_mmc_step, &diff);
208 gettimeofday (&now, 0);
209 timersub (&now, &last_mmc_step, &diff);
211 if (last_mmc_step.tv_sec != 0 && (diff.tv_usec + (diff.tv_sec * 1000000)) < _engine.usecs_per_cycle()) {
215 double diff_secs = diff.tv_sec + (diff.tv_usec / 1000000.0);
216 double cur_speed = (((steps * 0.5) * timecode_frames_per_second()) / diff_secs) / timecode_frames_per_second();
218 if (_transport_speed == 0 || cur_speed * _transport_speed < 0) {
220 step_speed = cur_speed;
222 step_speed = (0.6 * step_speed) + (0.4 * cur_speed);
228 cerr <<
"delta = " << diff_secs
229 <<
" ct = " << _transport_speed
230 <<
" steps = " << steps
231 <<
" new speed = " << cur_speed
232 <<
" speed = " << step_speed
236 request_transport_speed_nonzero (step_speed);
240 if (midi_control_ui) {
241 RefPtr<TimeoutSource> tsrc = TimeoutSource::create (100);
242 tsrc->connect (sigc::mem_fun (*
this, &Session::mmc_step_timeout));
243 tsrc->attach (midi_control_ui->main_loop()->get_context());
250 Session::mmc_rewind (MIDI::MachineControl &)
252 if (
Config->get_mmc_control ()) {
253 request_transport_speed(-8.0
f);
258 Session::mmc_fast_forward (MIDI::MachineControl &)
260 if (
Config->get_mmc_control ()) {
261 request_transport_speed(8.0
f);
266 Session::mmc_locate (MIDI::MachineControl &,
const MIDI::byte* mmc_tc)
268 if (!
Config->get_mmc_control ()) {
273 Timecode::Time timecode;
275 timecode.hours = mmc_tc[0] & 0xf;
276 timecode.minutes = mmc_tc[1];
277 timecode.seconds = mmc_tc[2];
278 timecode.frames = mmc_tc[3];
279 timecode.rate = timecode_frames_per_second();
280 timecode.drop = timecode_drop_frames();
283 timecode_to_sample( timecode, target_frame,
true ,
false );
302 request_locate (target_frame,
false);
307 Session::mmc_shuttle (MIDI::MachineControl &,
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_nonzero (speed);
320 request_transport_speed_nonzero (-speed);
325 Session::mmc_record_enable (MIDI::MachineControl &mmc,
size_t trk,
bool enabled)
327 if (!
Config->get_mmc_control ()) {
331 RouteList::iterator i;
334 for (i = r->begin(); i != r->end(); ++i) {
337 if ((at = dynamic_cast<AudioTrack*>((*i).get())) != 0) {
358 Timecode::Time timecode;
360 _send_timecode_update =
false;
362 if (_engine.freewheeling() || !
Config->get_send_mtc()) {
365 if (_slave && !_slave->locked()) {
370 sample_to_timecode (t, timecode,
true ,
false );
374 timecode_to_sample(timecode, mtc_tc,
true,
false);
375 outbound_mtc_timecode_frame = mtc_tc;
377 transmitting_timecode_time = timecode;
379 double const quarter_frame_duration = ((
framecnt_t) _frames_per_timecode_frame) / 4.0;
380 if (ceil((t - mtc_tc) / quarter_frame_duration) > 0) {
381 Timecode::increment (transmitting_timecode_time, config.get_subframes_per_frame());
382 outbound_mtc_timecode_frame += _frames_per_timecode_frame;
390 if (((mtc_timecode_bits >> 5) != MIDI::MTC_25_FPS) && (transmitting_timecode_time.frames % 2)) {
392 Timecode::increment (transmitting_timecode_time, config.get_subframes_per_frame());
393 outbound_mtc_timecode_frame += _frames_per_timecode_frame;
396 #if 0 // compensate for audio latency -- disabled [rg]
409 outbound_mtc_timecode_frame += worst_playback_latency();
411 next_quarter_frame_to_send = 0;
421 msg[5] = mtc_timecode_bits | (timecode.hours % 24);
422 msg[6] = timecode.minutes;
423 msg[7] = timecode.seconds;
424 msg[8] = timecode.frames;
428 MidiBuffer& mb (_midi_ports->mtc_output_port()->get_midi_buffer (nframes));
429 mb.
push_back (Port::port_offset(),
sizeof (msg), msg);
431 _pframes_since_last_mtc = 0;
445 if (_engine.freewheeling() || !_send_qf_mtc || transmitting_timecode_time.negative || (next_quarter_frame_to_send < 0)) {
449 if (_slave && !_slave->locked()) {
457 if (Timecode::timecode_to_frames_per_second(config.get_timecode_format()) > 30) {
461 assert (next_quarter_frame_to_send >= 0);
462 assert (next_quarter_frame_to_send <= 7);
465 double const quarter_frame_duration = _frames_per_timecode_frame / 4.0;
468 _transport_frame, start_frame, outbound_mtc_timecode_frame,
469 next_quarter_frame_to_send, quarter_frame_duration));
471 if (rint(outbound_mtc_timecode_frame + (next_quarter_frame_to_send * quarter_frame_duration)) < _transport_frame) {
472 send_full_time_code (_transport_frame, nframes);
477 while (end_frame > rint(outbound_mtc_timecode_frame + (next_quarter_frame_to_send * quarter_frame_duration))) {
481 switch (next_quarter_frame_to_send) {
483 mtc_msg[1] = 0x00 | (transmitting_timecode_time.frames & 0xf);
486 mtc_msg[1] = 0x10 | ((transmitting_timecode_time.frames & 0xf0) >> 4);
489 mtc_msg[1] = 0x20 | (transmitting_timecode_time.seconds & 0xf);
492 mtc_msg[1] = 0x30 | ((transmitting_timecode_time.seconds & 0xf0) >> 4);
495 mtc_msg[1] = 0x40 | (transmitting_timecode_time.minutes & 0xf);
498 mtc_msg[1] = 0x50 | ((transmitting_timecode_time.minutes & 0xf0) >> 4);
501 mtc_msg[1] = 0x60 | ((mtc_timecode_bits | transmitting_timecode_time.hours) & 0xf);
504 mtc_msg[1] = 0x70 | (((mtc_timecode_bits | transmitting_timecode_time.hours) & 0xf0) >> 4);
508 const framepos_t msg_time = rint(outbound_mtc_timecode_frame + (quarter_frame_duration * next_quarter_frame_to_send));
511 assert (msg_time >= start_frame);
512 assert (msg_time < end_frame);
516 assert (out_stamp < nframes);
518 MidiBuffer& mb (_midi_ports->mtc_output_port()->get_midi_buffer(nframes));
519 if (!mb.push_back (out_stamp, 2, mtc_msg)) {
520 error <<
string_compose(
_(
"Session: cannot send quarter-frame MTC message (%1)"), strerror (errno))
534 next_quarter_frame_to_send++;
536 if (next_quarter_frame_to_send >= 8) {
538 next_quarter_frame_to_send = 0;
540 Timecode::increment (transmitting_timecode_time, config.get_subframes_per_frame());
541 Timecode::increment (transmitting_timecode_time, config.get_subframes_per_frame());
544 outbound_mtc_timecode_frame += 2.0 * _frames_per_timecode_frame;
556 Session::send_immediate_mmc (MachineControlCommand c)
558 if (AudioEngine::instance()->in_process_thread()) {
559 _mmc->send (c, Port::port_offset());
567 Session::mmc_step_timeout ()
572 gettimeofday (&now, 0);
574 timersub (&now, &last_mmc_step, &diff);
575 diff_usecs = diff.tv_sec * 1000000 + diff.tv_usec;
577 if (diff_usecs > 1000000.0 || fabs (_transport_speed) < 0.0000001) {
579 request_transport_speed (0.0);
584 if (diff_usecs < 250000.0) {
591 request_transport_speed_nonzero (_transport_speed * 0.75);
609 Session::start_midi_thread ()
611 if (midi_control_ui) {
return 0; }
613 midi_control_ui->run ();
618 Session::midi_input_port ()
const
620 return _midi_ports->midi_input_port ();
623 Session::midi_output_port ()
const
625 return _midi_ports->midi_output_port ();
628 Session::midi_clock_output_port ()
const
630 return _midi_ports->midi_clock_output_port ();
633 Session::midi_clock_input_port ()
const
635 return _midi_ports->midi_clock_input_port ();
638 Session::mtc_output_port ()
const
640 return _midi_ports->mtc_output_port ();
643 Session::mtc_input_port ()
const
645 return _midi_ports->mtc_input_port ();
649 Session::mmc_output_port ()
const
651 return _midi_ports->mmc_output_port ();
655 Session::mmc_input_port ()
const
657 return _midi_ports->mmc_input_port ();
661 Session::scene_output_port ()
const
663 return _midi_ports->scene_output_port ();
667 Session::scene_input_port ()
const
669 return _midi_ports->scene_input_port ();
673 Session::scene_in ()
const
675 return _midi_ports->scene_in ();
679 Session::scene_out ()
const
681 return _midi_ports->scene_out ();
void set_record_enabled(bool yn, void *src)
LIBPBD_API Transmitter error
std::ostream & endmsg(std::ostream &ostr)
LIBARDOUR_API RCConfiguration * Config
#define DEBUG_TRACE(bits, str)
#define DEBUG_STR_APPEND(id, s)
bool push_back(const Evoral::MIDIEvent< TimeType > &event)
LIBARDOUR_API uint64_t MTC
static const framepos_t max_framepos
uint32_t remote_control_id() const
#define timersub(a, b, result)
#define DEBUG_STR_DECL(id)
void handle_locate(const MIDI::byte *)
std::string string_compose(const std::string &fmt, const T1 &o1)