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.
25 #include <pbd/error.h>
27 #include <glibmm/thread.h>
29 #include <ardour/ardour.h>
30 #include <ardour/session.h>
31 #include <ardour/timestamps.h>
32 #include <ardour/audio_diskstream.h>
33 #include <ardour/audioengine.h>
34 #include <ardour/slave.h>
35 #include <ardour/auditioner.h>
36 #include <ardour/cycles.h>
37 #include <ardour/cycle_timer.h>
39 #include <midi++/manager.h>
43 using namespace ARDOUR;
47 /** Called by the audio engine when there is work to be done with JACK.
48 * @param nframes Number of frames to process.
51 Session::process (nframes_t nframes)
53 MIDI::Manager::instance()->cycle_start(nframes);
57 if (non_realtime_work_pending()) {
58 if (!transport_work_requested ()) {
63 (this->*process_function) (nframes);
65 MIDI::Manager::instance()->cycle_end();
67 SendFeedback (); /* EMIT SIGNAL */
71 Session::prepare_diskstreams ()
73 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
74 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
80 Session::no_roll (nframes_t nframes, nframes_t offset)
82 nframes_t end_frame = _transport_frame + nframes; // FIXME: varispeed + no_roll ??
84 bool declick = get_transport_declick_required();
85 boost::shared_ptr<RouteList> r = routes.reader ();
88 _click_io->silence (nframes, offset);
91 if (g_atomic_int_get (&processing_prohibited)) {
92 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
93 (*i)->silence (nframes, offset);
98 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
100 if ((*i)->is_hidden()) {
104 (*i)->set_pending_declick (declick);
106 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(),
107 actively_recording(), declick)) {
108 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
118 Session::process_routes (nframes_t nframes, nframes_t offset)
121 int declick = get_transport_declick_required();
122 bool rec_monitors = get_rec_monitors_input();
123 boost::shared_ptr<RouteList> r = routes.reader ();
125 if (transport_sub_state & StopPendingCapture) {
126 /* force a declick out */
130 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
132 const nframes_t start_frame = _transport_frame;
133 const nframes_t end_frame = _transport_frame + (nframes_t)floor(nframes * _transport_speed);
135 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
139 if ((*i)->is_hidden()) {
143 (*i)->set_pending_declick (declick);
145 if ((ret = (*i)->roll (nframes, start_frame, end_frame, 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 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
153 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
166 Session::silent_process_routes (nframes_t nframes, nframes_t offset)
168 bool record_active = actively_recording();
169 int declick = get_transport_declick_required();
170 bool rec_monitors = get_rec_monitors_input();
171 boost::shared_ptr<RouteList> r = routes.reader ();
173 if (transport_sub_state & StopPendingCapture) {
174 /* force a declick out */
178 const nframes_t start_frame = _transport_frame;
179 const nframes_t end_frame = _transport_frame + lrintf(nframes * _transport_speed);
181 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
185 if ((*i)->is_hidden()) {
189 if ((ret = (*i)->silent_roll (nframes, start_frame, end_frame, offset, record_active, rec_monitors)) < 0) {
191 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
192 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
193 call path, so make sure we release any outstanding locks here before we return failure.
196 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
197 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
210 Session::commit_diskstreams (nframes_t nframes, bool &needs_butler)
216 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
217 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
219 if ((*i)->hidden()) {
223 /* force all diskstreams not handled by a Route to call do their stuff.
224 Note: the diskstreams that were handled by a route will just return zero
225 from this call, because they know they were processed. So in fact, this
226 also runs commit() for every diskstream.
229 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
230 if ((*i)->commit (nframes)) {
234 } else if (dret < 0) {
238 pworst = min (pworst, (*i)->playback_buffer_load());
239 cworst = min (cworst, (*i)->capture_buffer_load());
242 uint32_t pmin = g_atomic_int_get (&_playback_load);
243 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
244 uint32_t cmin = g_atomic_int_get (&_capture_load);
245 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
247 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
248 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
249 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
250 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
252 if (actively_recording()) {
257 /** Process callback used when the auditioner is not active */
259 Session::process_with_events (nframes_t nframes)
262 nframes_t this_nframes;
265 bool session_needs_butler = false;
266 nframes_t stop_limit;
269 /* make sure the auditioner is silent */
272 auditioner->silence (nframes, 0);
275 /* handle any pending events */
277 while (pending_events.read (&ev, 1) == 1) {
281 /* if we are not in the middle of a state change,
282 and there are immediate events queued up,
286 while (!non_realtime_work_pending() && !immediate_events.empty()) {
287 Event *ev = immediate_events.front ();
288 immediate_events.pop_front ();
292 /* Events caused a transport change, send an MTC Full Frame (SMPTE) message.
293 * This is sent whether rolling or not, to give slaves an idea of ardour time
294 * on locates (and allow slow slaves to position and prepare for rolling)
296 if (_send_smpte_update) {
297 send_full_time_code(nframes);
300 if (!process_can_proceed()) {
305 if (events.empty() || next_event == events.end()) {
306 process_without_events (nframes);
310 end_frame = _transport_frame + (nframes_t)abs(floor(nframes * _transport_speed));
314 Events::iterator the_next_one;
316 if (!process_can_proceed()) {
321 if (!_exporting && _slave) {
322 if (!follow_slave (nframes, 0)) {
327 if (_transport_speed == 0) {
328 no_roll (nframes, 0);
333 send_midi_time_code_for_cycle (nframes);
336 if (actively_recording()) {
337 stop_limit = max_frames;
340 if (Config->get_stop_at_session_end()) {
341 stop_limit = current_end_frame();
343 stop_limit = max_frames;
347 if (maybe_stop (stop_limit)) {
348 no_roll (nframes, 0);
352 this_event = *next_event;
353 the_next_one = next_event;
358 /* yes folks, here it is, the actual loop where we really truly
359 process some audio */
362 this_nframes = nframes; /* real (jack) time relative */
363 frames_moved = (long) floor (_transport_speed * nframes); /* transport relative */
365 /* running an event, position transport precisely to its time */
366 if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
367 /* this isn't quite right for reverse play */
368 frames_moved = (long) (this_event->action_frame - _transport_frame);
369 this_nframes = (nframes_t) abs( floor(frames_moved / _transport_speed) );
374 click (_transport_frame, nframes, offset);
376 /* now process frames between now and the first event in this block */
377 prepare_diskstreams ();
379 if (process_routes (this_nframes, offset)) {
380 no_roll (nframes, 0);
384 commit_diskstreams (this_nframes, session_needs_butler);
386 nframes -= this_nframes;
387 offset += this_nframes;
389 if (frames_moved < 0) {
390 decrement_transport_position (-frames_moved);
392 increment_transport_position (frames_moved);
395 maybe_stop (stop_limit);
396 check_declick_out ();
399 /* now handle this event and all others scheduled for the same time */
401 while (this_event && this_event->action_frame == _transport_frame) {
402 process_event (this_event);
404 if (the_next_one == events.end()) {
407 this_event = *the_next_one;
412 /* if an event left our state changing, do the right thing */
414 if (non_realtime_work_pending()) {
415 no_roll (nframes, offset);
419 /* this is necessary to handle the case of seamless looping */
420 end_frame = _transport_frame + (nframes_t) floor (nframes * _transport_speed);
426 } /* implicit release of route lock */
428 if (session_needs_butler)
433 Session::reset_slave_state ()
435 average_slave_delta = 1800;
436 delta_accumulator_cnt = 0;
437 have_first_delta_accumulator = false;
438 slave_state = Stopped;
442 Session::transport_locked () const
446 if (!locate_pending() && ((Config->get_slave_source() == None) || (sl && sl->ok() && sl->locked()))) {
454 Session::follow_slave (nframes_t nframes, nframes_t offset)
457 nframes_t slave_transport_frame;
458 nframes_t this_delta;
464 Config->set_slave_source (None);
468 _slave->speed_and_position (slave_speed, slave_transport_frame);
470 if (!_slave->locked()) {
474 if (slave_transport_frame > _transport_frame) {
475 this_delta = slave_transport_frame - _transport_frame;
478 this_delta = _transport_frame - slave_transport_frame;
482 if ((starting = _slave->starting())) {
487 cerr << "delta = " << (int) (dir * this_delta)
488 << " speed = " << slave_speed
489 << " ts = " << _transport_speed
490 << " M@ "<< slave_transport_frame << " S@ " << _transport_frame
491 << " avgdelta = " << average_slave_delta
495 if (_slave->is_always_synced() || Config->get_timecode_source_is_synced()) {
497 /* if the TC source is synced, then we assume that its
498 speed is binary: 0.0 or 1.0
501 if (slave_speed != 0.0f) {
507 /* TC source is able to drift relative to us (slave)
508 so we need to keep track of the drift and adjust
509 our speed to remain locked.
512 if (delta_accumulator_cnt >= delta_accumulator_size) {
513 have_first_delta_accumulator = true;
514 delta_accumulator_cnt = 0;
517 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
518 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
521 if (have_first_delta_accumulator) {
522 average_slave_delta = 0;
523 for (int i = 0; i < delta_accumulator_size; ++i) {
524 average_slave_delta += delta_accumulator[i];
526 average_slave_delta /= delta_accumulator_size;
527 if (average_slave_delta < 0) {
529 average_slave_delta = -average_slave_delta;
533 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
537 if (slave_speed != 0.0f) {
539 /* slave is running */
541 switch (slave_state) {
543 if (_slave->requires_seekahead()) {
544 slave_wait_end = slave_transport_frame + _current_frame_rate;
545 locate (slave_wait_end, false, false);
546 slave_state = Waiting;
551 slave_state = Running;
553 Location* al = _locations.auto_loop_location();
555 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
557 request_play_loop(false);
560 if (slave_transport_frame != _transport_frame) {
561 locate (slave_transport_frame, false, false);
574 if (slave_state == Waiting) {
576 // cerr << "waiting at " << slave_transport_frame << endl;
577 if (slave_transport_frame >= slave_wait_end) {
578 // cerr << "\tstart at " << _transport_frame << endl;
580 slave_state = Running;
583 nframes_t frame_delta = slave_transport_frame - _transport_frame;
585 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
587 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
588 if (!(*i)->can_internal_playback_seek (frame_delta)) {
595 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
596 (*i)->internal_playback_seek (frame_delta);
598 _transport_frame += frame_delta;
601 // cerr << "cannot micro-seek\n";
605 memset (delta_accumulator, 0, sizeof (nframes_t) * delta_accumulator_size);
606 average_slave_delta = 0;
611 if (slave_state == Running && _transport_speed == 0.0f) {
613 // cerr << "slave starts transport\n";
620 /* slave has stopped */
622 if (_transport_speed != 0.0f) {
624 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
625 // << " tf = " << _transport_frame
628 if (Config->get_slave_source() == JACK) {
629 last_stop_frame = _transport_frame;
635 if (slave_transport_frame != _transport_frame) {
636 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
637 force_locate (slave_transport_frame, false);
640 slave_state = Stopped;
643 if (slave_state == Running && !_slave->is_always_synced() && !Config->get_timecode_source_is_synced()) {
646 if (_transport_speed != 0.0f) {
649 note that average_dir is +1 or -1
652 const float adjust_seconds = 1.0f;
655 //if (average_slave_delta == 0) {
659 // delta = average_slave_delta;
660 // delta *= average_dir;
663 float adjusted_speed = slave_speed +
664 (delta / (adjust_seconds * _current_frame_rate));
666 // cerr << "adjust using " << delta
667 // << " towards " << adjusted_speed
668 // << " ratio = " << adjusted_speed / slave_speed
669 // << " current = " << _transport_speed
670 // << " slave @ " << slave_speed
673 request_transport_speed (adjusted_speed);
676 if ((nframes_t) average_slave_delta > _slave->resolution()) {
677 // cerr << "not locked\n";
684 if (!starting && !non_realtime_work_pending()) {
685 /* speed is set, we're locked, and good to go */
691 if (slave_speed && _transport_speed) {
693 /* something isn't right, but we should move with the master
699 prepare_diskstreams ();
700 silent_process_routes (nframes, offset);
701 commit_diskstreams (nframes, need_butler);
707 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
709 if (frames_moved < 0) {
710 decrement_transport_position (-frames_moved);
712 increment_transport_position (frames_moved);
715 nframes_t stop_limit;
717 if (actively_recording()) {
718 stop_limit = max_frames;
720 if (Config->get_stop_at_session_end()) {
721 stop_limit = current_end_frame();
723 stop_limit = max_frames;
727 maybe_stop (stop_limit);
731 /* don't move at all */
732 no_roll (nframes, 0);
737 Session::process_without_events (nframes_t nframes)
739 bool session_needs_butler = false;
740 nframes_t stop_limit;
742 nframes_t offset = 0;
744 if (!process_can_proceed()) {
749 if (!_exporting && _slave) {
750 if (!follow_slave (nframes, 0)) {
755 if (_transport_speed == 0) {
756 no_roll (nframes, 0);
761 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 if (maybe_sync_start (nframes, offset)) {
783 click (_transport_frame, nframes, offset);
785 prepare_diskstreams ();
787 frames_moved = (long) floor (_transport_speed * nframes);
789 if (process_routes (nframes, offset)) {
790 no_roll (nframes, offset);
794 commit_diskstreams (nframes, session_needs_butler);
796 if (frames_moved < 0) {
797 decrement_transport_position (-frames_moved);
799 increment_transport_position (frames_moved);
802 maybe_stop (stop_limit);
803 check_declick_out ();
805 if (session_needs_butler)
809 /** Process callback used when the auditioner is active.
810 * @param nframes number of frames to process.
813 Session::process_audition (nframes_t nframes)
816 boost::shared_ptr<RouteList> r = routes.reader ();
818 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
819 if (!(*i)->is_hidden()) {
820 (*i)->silence (nframes, 0);
824 /* run the auditioner, and if it says we need butler service, ask for it */
826 if (auditioner->play_audition (nframes) > 0) {
830 /* handle pending events */
832 while (pending_events.read (&ev, 1) == 1) {
836 /* if we are not in the middle of a state change,
837 and there are immediate events queued up,
841 while (!non_realtime_work_pending() && !immediate_events.empty()) {
842 Event *ev = immediate_events.front ();
843 immediate_events.pop_front ();
847 if (!auditioner->active()) {
848 /* auditioner no longer active, so go back to the normal process callback */
849 process_function = &Session::process_with_events;
854 Session::maybe_sync_start (nframes_t& nframes, nframes_t& offset)
856 nframes_t sync_offset;
858 if (!waiting_for_sync_offset) {
862 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
864 /* generate silence up to the sync point, then
865 adjust nframes + offset to reflect whatever
869 no_roll (sync_offset, 0);
870 nframes -= sync_offset;
871 offset += sync_offset;
872 waiting_for_sync_offset = false;
875 return true; // done, nothing left to process
880 /* sync offset point is not within this process()
881 cycle, so just generate silence. and don't bother
882 with any fancy stuff here, just the minimal silence.
885 g_atomic_int_inc (&processing_prohibited);
886 no_roll (nframes, 0);
887 g_atomic_int_dec_and_test (&processing_prohibited);
889 if (Config->get_locate_while_waiting_for_sync()) {
890 if (micro_locate (nframes)) {
891 /* XXX ERROR !!! XXX */
895 return true; // done, nothing left to process