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 <pbd/error.h>
28 #include <glibmm/thread.h>
30 #include <ardour/ardour.h>
31 #include <ardour/session.h>
32 #include <ardour/timestamps.h>
33 #include <ardour/audio_diskstream.h>
34 #include <ardour/audioengine.h>
35 #include <ardour/slave.h>
36 #include <ardour/auditioner.h>
37 #include <ardour/cycles.h>
38 #include <ardour/cycle_timer.h>
40 #include <midi++/manager.h>
44 using namespace ARDOUR;
49 Session::process (jack_nframes_t nframes)
51 MIDI::Manager::instance()->cycle_start(nframes);
53 if (synced_to_jack() && waiting_to_start) {
54 if ( _engine.transport_state() == AudioEngine::TransportRolling) {
55 actually_start_transport ();
59 if (non_realtime_work_pending()) {
60 if (g_atomic_int_get (&butler_should_do_transport_work) == 0) {
65 (this->*process_function) (nframes);
67 MIDI::Manager::instance()->cycle_end();
69 SendFeedback (); /* EMIT SIGNAL */
73 Session::prepare_diskstreams ()
75 for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
81 Session::no_roll (jack_nframes_t nframes, jack_nframes_t offset)
83 jack_nframes_t end_frame = _transport_frame + nframes;
85 bool declick = get_transport_declick_required();
88 _click_io->silence (nframes, offset);
91 /* XXX we're supposed to have the route_lock while doing this.
92 this is really bad ...
95 if (g_atomic_int_get (&processing_prohibited)) {
96 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
97 (*i)->silence (nframes, offset);
102 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
104 if ((*i)->hidden()) {
108 (*i)->set_pending_declick (declick);
110 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(),
111 actively_recording(), declick)) {
112 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
122 Session::process_routes (jack_nframes_t nframes, jack_nframes_t offset)
125 int declick = get_transport_declick_required();
126 bool rec_monitors = get_rec_monitors_input();
128 if (transport_sub_state & StopPendingCapture) {
129 /* force a declick out */
133 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
135 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
139 if ((*i)->hidden()) {
143 (*i)->set_pending_declick (declick);
145 if ((ret = (*i)->roll (nframes, _transport_frame, _transport_frame + nframes, offset, declick, record_active, rec_monitors)) < 0) {
147 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
148 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
149 call path, so make sure we release any outstanding locks here before we return failure.
152 for (DiskstreamList::iterator ids = diskstreams.begin(); ids != diskstreams.end(); ++ids) {
165 Session::silent_process_routes (jack_nframes_t nframes, jack_nframes_t offset)
167 bool record_active = actively_recording();
168 int declick = get_transport_declick_required();
169 bool rec_monitors = get_rec_monitors_input();
171 if (transport_sub_state & StopPendingCapture) {
172 /* force a declick out */
176 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
180 if ((*i)->hidden()) {
184 if ((ret = (*i)->silent_roll (nframes, _transport_frame, _transport_frame + nframes, offset, record_active, rec_monitors)) < 0) {
186 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
187 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
188 call path, so make sure we release any outstanding locks here before we return failure.
191 for (DiskstreamList::iterator ids = diskstreams.begin(); ids != diskstreams.end(); ++ids) {
204 Session::commit_diskstreams (jack_nframes_t nframes, bool &needs_butler)
210 for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
212 if ((*i)->hidden()) {
216 /* force all diskstreams not handled by a Route to call do their stuff.
219 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
220 if ((*i)->commit (nframes)) {
224 } else if (dret < 0) {
228 pworst = min (pworst, (*i)->playback_buffer_load());
229 cworst = min (cworst, (*i)->capture_buffer_load());
232 uint32_t pmin = g_atomic_int_get (&_playback_load);
233 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
234 uint32_t cmin = g_atomic_int_get (&_capture_load);
235 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
237 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
238 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
239 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
240 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
242 if (actively_recording()) {
249 Session::process_with_events (jack_nframes_t nframes)
252 jack_nframes_t this_nframes;
253 jack_nframes_t end_frame;
254 jack_nframes_t offset;
255 jack_nframes_t stop_limit;
257 bool session_needs_butler = false;
260 auditioner->silence (nframes, 0);
263 while (pending_events.read (&ev, 1) == 1) {
267 /* if we are not in the middle of a state change,
268 and there are immediate events queued up,
272 while (!non_realtime_work_pending() && !immediate_events.empty()) {
273 Event *ev = immediate_events.front ();
274 immediate_events.pop_front ();
278 /* Events caused a transport change, send an MTC Full Frame (SMPTE) message.
279 * This is sent whether rolling or not, to give slaves an idea of ardour time
280 * on locates (and allow slow slaves to position and prepare for rolling)
282 if (_send_smpte_update) {
283 send_full_time_code(nframes);
286 if (!process_can_proceed()) {
287 no_roll (nframes, 0);
291 if (events.empty() || next_event == events.end()) {
292 process_without_events (nframes);
296 end_frame = _transport_frame + nframes;
299 Glib::RWLock::ReaderLock rm (route_lock, Glib::TRY_LOCK);
300 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
303 Events::iterator the_next_one;
305 if (!rm.locked() || !dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
306 no_roll (nframes, 0);
310 if (!_exporting && _slave) {
311 if (!follow_slave (nframes, 0)) {
316 if (_transport_speed == 0) {
317 no_roll (nframes, 0);
321 send_midi_time_code_for_cycle(nframes);
323 if (actively_recording()) {
324 stop_limit = max_frames;
327 if (Config->get_stop_at_session_end()) {
328 stop_limit = current_end_frame();
330 stop_limit = max_frames;
334 if (maybe_stop (stop_limit)) {
335 no_roll (nframes, 0);
339 this_event = *next_event;
340 the_next_one = next_event;
347 if (this_event == 0 || this_event->action_frame > end_frame || this_event->action_frame < _transport_frame) {
349 this_nframes = nframes;
353 /* compute nframes to next event */
355 if (this_event->action_frame < end_frame) {
356 this_nframes = nframes - (end_frame - this_event->action_frame);
358 this_nframes = nframes;
364 click (_transport_frame, nframes, offset);
366 /* now process frames between now and the first event in this block */
367 prepare_diskstreams ();
369 if (process_routes (this_nframes, offset)) {
370 no_roll (nframes, 0);
374 commit_diskstreams (this_nframes, session_needs_butler);
376 nframes -= this_nframes;
377 offset += this_nframes;
379 frames_moved = (jack_nframes_t) floor (_transport_speed * this_nframes);
381 if (frames_moved < 0) {
382 decrement_transport_position (-frames_moved);
384 increment_transport_position (frames_moved);
387 maybe_stop (stop_limit);
388 check_declick_out ();
391 /* now handle this event and all others scheduled for the same time */
393 while (this_event && this_event->action_frame == _transport_frame) {
394 process_event (this_event);
396 if (the_next_one == events.end()) {
399 this_event = *the_next_one;
404 /* if an event left our state changing, do the right thing */
406 if (non_realtime_work_pending()) {
407 no_roll (nframes, offset);
411 /* this is necessary to handle the case of seamless looping */
412 /* not sure if it will work in conjuction with varispeed */
413 end_frame = _transport_frame + nframes;
419 } /* implicit release of route lock */
421 if (session_needs_butler)
426 Session::reset_slave_state ()
428 average_slave_delta = 1800;
429 delta_accumulator_cnt = 0;
430 have_first_delta_accumulator = false;
431 slave_state = Stopped;
435 Session::transport_locked () const
439 if (!locate_pending() && ((_slave_type == None) || (sl && sl->ok() && sl->locked()))) {
447 Session::follow_slave (jack_nframes_t nframes, jack_nframes_t offset)
450 jack_nframes_t slave_transport_frame;
451 jack_nframes_t this_delta;
457 set_slave_source (None, 0);
461 _slave->speed_and_position (slave_speed, slave_transport_frame);
463 if (!_slave->locked()) {
467 if (slave_transport_frame > _transport_frame) {
468 this_delta = slave_transport_frame - _transport_frame;
471 this_delta = _transport_frame - slave_transport_frame;
475 if ((starting = _slave->starting())) {
480 cerr << "delta = " << (int) (dir * this_delta)
481 << " speed = " << slave_speed
482 << " ts = " << _transport_speed
483 << " M@"<< slave_transport_frame << " S@" << _transport_frame
484 << " avgdelta = " << average_slave_delta
488 if (Config->get_timecode_source_is_synced()) {
490 /* if the TC source is synced, then we assume that its
491 speed is binary: 0.0 or 1.0
494 if (slave_speed != 0.0f) {
500 /* TC source is able to drift relative to us (slave)
501 so we need to keep track of the drift and adjust
502 our speed to remain locked.
505 if (delta_accumulator_cnt >= delta_accumulator_size) {
506 have_first_delta_accumulator = true;
507 delta_accumulator_cnt = 0;
510 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
511 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
514 if (have_first_delta_accumulator) {
515 average_slave_delta = 0;
516 for (int i = 0; i < delta_accumulator_size; ++i) {
517 average_slave_delta += delta_accumulator[i];
519 average_slave_delta /= delta_accumulator_size;
520 if (average_slave_delta < 0) {
522 average_slave_delta = -average_slave_delta;
526 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
530 if (slave_speed != 0.0f) {
532 /* slave is running */
534 switch (slave_state) {
536 if (_slave->requires_seekahead()) {
537 slave_wait_end = slave_transport_frame + _current_frame_rate;
538 locate (slave_wait_end, false, false);
539 slave_state = Waiting;
544 slave_state = Running;
546 Location* al = _locations.auto_loop_location();
548 if (al && auto_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
550 request_auto_loop(false);
553 if (slave_transport_frame != _transport_frame) {
554 locate (slave_transport_frame, false, false);
567 if (slave_state == Waiting) {
569 // cerr << "waiting at " << slave_transport_frame << endl;
570 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
572 if (dsm.locked() && slave_transport_frame >= slave_wait_end) {
573 // cerr << "\tstart at " << _transport_frame << endl;
575 slave_state = Running;
578 jack_nframes_t frame_delta = slave_transport_frame - _transport_frame;
580 for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
581 if (!(*i)->can_internal_playback_seek (frame_delta)) {
588 for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
589 (*i)->internal_playback_seek (frame_delta);
591 _transport_frame += frame_delta;
594 // cerr << "cannot micro-seek\n";
598 memset (delta_accumulator, 0, sizeof (jack_nframes_t) * delta_accumulator_size);
599 average_slave_delta = 0;
604 if (slave_state == Running && _transport_speed == 0.0f) {
606 // cerr << "slave starts transport\n";
613 /* slave has stopped */
615 if (_transport_speed != 0.0f) {
617 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
618 // << " tf = " << _transport_frame
621 if (_slave_type == JACK) {
622 last_stop_frame = _transport_frame;
628 if (slave_transport_frame != _transport_frame) {
629 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
630 force_locate (slave_transport_frame, false);
633 slave_state = Stopped;
636 if (slave_state == Running && !Config->get_timecode_source_is_synced()) {
639 if (_transport_speed != 0.0f) {
642 note that average_dir is +1 or -1
645 const float adjust_seconds = 1.0f;
648 //if (average_slave_delta == 0) {
652 // delta = average_slave_delta;
653 // delta *= average_dir;
656 float adjusted_speed = slave_speed +
657 (delta / (adjust_seconds * _current_frame_rate));
659 // cerr << "adjust using " << delta
660 // << " towards " << adjusted_speed
661 // << " ratio = " << adjusted_speed / slave_speed
662 // << " current = " << _transport_speed
663 // << " slave @ " << slave_speed
666 request_transport_speed (adjusted_speed);
669 if ((jack_nframes_t) average_slave_delta > _slave->resolution()) {
670 // cerr << "not locked\n";
677 if (!starting && !non_realtime_work_pending()) {
678 /* speed is set, we're locked, and good to go */
684 if (slave_speed && _transport_speed) {
686 /* something isn't right, but we should move with the master
692 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
698 prepare_diskstreams ();
699 silent_process_routes (nframes, offset);
700 commit_diskstreams (nframes, need_butler);
706 jack_nframes_t frames_moved = (long) floor (_transport_speed * nframes);
708 if (frames_moved < 0) {
709 decrement_transport_position (-frames_moved);
711 increment_transport_position (frames_moved);
714 jack_nframes_t stop_limit;
716 if (actively_recording()) {
717 stop_limit = max_frames;
719 if (Config->get_stop_at_session_end()) {
720 stop_limit = current_end_frame();
722 stop_limit = max_frames;
726 maybe_stop (stop_limit);
730 /* don't move at all */
731 no_roll (nframes, 0);
736 Session::process_without_events (jack_nframes_t nframes)
738 bool session_needs_butler = false;
739 jack_nframes_t stop_limit;
743 Glib::RWLock::ReaderLock rm (route_lock, Glib::TRY_LOCK);
744 Glib::RWLock::ReaderLock dsm (diskstream_lock, Glib::TRY_LOCK);
746 if (!rm.locked() || !dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
747 no_roll (nframes, 0);
751 if (!_exporting && _slave) {
752 if (!follow_slave (nframes, 0)) {
757 if (_transport_speed == 0) {
758 no_roll (nframes, 0);
762 send_midi_time_code_for_cycle(nframes);
764 if (actively_recording()) {
765 stop_limit = max_frames;
767 if (Config->get_stop_at_session_end()) {
768 stop_limit = current_end_frame();
770 stop_limit = max_frames;
774 if (maybe_stop (stop_limit)) {
775 no_roll (nframes, 0);
779 click (_transport_frame, nframes, 0);
781 prepare_diskstreams ();
783 frames_moved = (long) floor (_transport_speed * nframes);
785 if (process_routes (nframes, 0)) {
786 no_roll (nframes, 0);
790 commit_diskstreams (nframes, session_needs_butler);
792 if (frames_moved < 0) {
793 decrement_transport_position (-frames_moved);
795 increment_transport_position (frames_moved);
798 maybe_stop (stop_limit);
799 check_declick_out ();
801 } /* implicit release of route lock */
803 if (session_needs_butler)
808 Session::process_audition (jack_nframes_t nframes)
810 Glib::RWLock::ReaderLock rm (route_lock, Glib::TRY_LOCK);
814 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
815 if (!(*i)->hidden()) {
816 (*i)->silence (nframes, 0);
821 if (auditioner->play_audition (nframes) > 0) {
825 while (pending_events.read (&ev, 1) == 1) {
829 /* if we are not in the middle of a state change,
830 and there are immediate events queued up,
834 while (!non_realtime_work_pending() && !immediate_events.empty()) {
835 Event *ev = immediate_events.front ();
836 immediate_events.pop_front ();
840 if (!auditioner->active()) {
841 process_function = &Session::process_with_events;