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>
41 using namespace ARDOUR;
46 Session::process (nframes_t nframes)
50 if (processing_blocked()) {
55 if (non_realtime_work_pending()) {
56 if (!transport_work_requested ()) {
61 (this->*process_function) (nframes);
64 Glib::Mutex::Lock lm (midi_lock, Glib::TRY_LOCK);
65 SendFeedback (); /* EMIT SIGNAL */
70 Session::prepare_diskstreams ()
72 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
73 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
79 Session::fail_roll (nframes_t nframes)
81 Port::set_port_offset (0);
82 return no_roll (nframes);
87 Session::no_roll (nframes_t nframes)
89 nframes_t end_frame = _transport_frame + nframes;
91 bool declick = get_transport_declick_required();
92 boost::shared_ptr<RouteList> r = routes.reader ();
95 _click_io->silence (nframes);
98 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
100 if ((*i)->hidden()) {
104 (*i)->set_pending_declick (declick);
106 if ((*i)->no_roll (nframes, _transport_frame, end_frame, 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)
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 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
136 if ((*i)->hidden()) {
140 (*i)->set_pending_declick (declick);
142 if ((ret = (*i)->roll (nframes, _transport_frame, _transport_frame + nframes, declick, record_active, rec_monitors)) < 0) {
144 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
145 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
146 call path, so make sure we release any outstanding locks here before we return failure.
149 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
150 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
163 Session::silent_process_routes (nframes_t nframes)
165 bool record_active = actively_recording();
166 int declick = get_transport_declick_required();
167 bool rec_monitors = get_rec_monitors_input();
168 boost::shared_ptr<RouteList> r = routes.reader ();
170 if (transport_sub_state & StopPendingCapture) {
171 /* force a declick out */
175 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
179 if ((*i)->hidden()) {
183 if ((ret = (*i)->silent_roll (nframes, _transport_frame, _transport_frame + nframes, record_active, rec_monitors)) < 0) {
185 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
186 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
187 call path, so make sure we release any outstanding locks here before we return failure.
190 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
191 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
204 Session::commit_diskstreams (nframes_t nframes, bool &needs_butler)
210 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
211 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
213 if ((*i)->hidden()) {
217 /* force all diskstreams not handled by a Route to call do their stuff.
218 Note: the diskstreams that were handled by a route will just return zero
219 from this call, because they know they were processed. So in fact, this
220 also runs commit() for every diskstream.
223 if ((dret = (*i)->process (_transport_frame, nframes, actively_recording(), get_rec_monitors_input())) == 0) {
224 if ((*i)->commit (nframes)) {
228 } else if (dret < 0) {
232 pworst = min (pworst, (*i)->playback_buffer_load());
233 cworst = min (cworst, (*i)->capture_buffer_load());
236 uint32_t pmin = g_atomic_int_get (&_playback_load);
237 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
238 uint32_t cmin = g_atomic_int_get (&_capture_load);
239 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
241 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
242 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
243 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
244 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
246 if (actively_recording()) {
252 Session::process_with_events (nframes_t nframes)
255 nframes_t this_nframes;
258 bool session_needs_butler = false;
259 nframes_t stop_limit;
262 /* make sure the auditioner is silent */
265 auditioner->silence (nframes);
268 /* handle any pending events */
270 while (pending_events.read (&ev, 1) == 1) {
274 /* if we are not in the middle of a state change,
275 and there are immediate events queued up,
279 while (!non_realtime_work_pending() && !immediate_events.empty()) {
280 Event *ev = immediate_events.front ();
281 immediate_events.pop_front ();
285 /* Events caused a transport change, send an MTC Full Frame (SMPTE) message.
286 * This is sent whether rolling or not, to give slaves an idea of ardour time
287 * on locates (and allow slow slaves to position and prepare for rolling)
289 if (_send_smpte_update) {
290 send_full_time_code ();
293 if (!process_can_proceed()) {
298 if (events.empty() || next_event == events.end()) {
299 process_without_events (nframes);
303 end_frame = _transport_frame + (nframes_t)abs(floor(nframes * _transport_speed));
307 Events::iterator the_next_one;
309 if (!process_can_proceed()) {
314 if (!_exporting && _slave) {
315 if (!follow_slave (nframes)) {
320 if (_transport_speed == 0) {
325 if (actively_recording()) {
326 stop_limit = max_frames;
329 if (Config->get_stop_at_session_end()) {
330 stop_limit = current_end_frame();
332 stop_limit = max_frames;
336 if (maybe_stop (stop_limit)) {
341 this_event = *next_event;
342 the_next_one = next_event;
348 this_nframes = nframes; /* real (jack) time relative */
349 frames_moved = (long) floor (_transport_speed * nframes); /* transport relative */
351 /* running an event, position transport precisely to its time */
352 if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
353 /* this isn't quite right for reverse play */
354 frames_moved = (long) (this_event->action_frame - _transport_frame);
355 this_nframes = (nframes_t) abs( floor(frames_moved / _transport_speed) );
360 click (_transport_frame, nframes);
362 /* now process frames between now and the first event in this block */
363 prepare_diskstreams ();
365 if (process_routes (this_nframes)) {
370 commit_diskstreams (this_nframes, session_needs_butler);
372 nframes -= this_nframes;
374 if (frames_moved < 0) {
375 decrement_transport_position (-frames_moved);
377 increment_transport_position (frames_moved);
380 maybe_stop (stop_limit);
381 check_declick_out ();
384 /* reset port offsets so that Port::get_buffer() will fetch the correct data */
386 Port::increment_port_offset (this_nframes);
388 /* now handle this event and all others scheduled for the same time */
390 while (this_event && this_event->action_frame == _transport_frame) {
391 process_event (this_event);
393 if (the_next_one == events.end()) {
396 this_event = *the_next_one;
401 /* if an event left our state changing, do the right thing */
403 if (nframes && non_realtime_work_pending()) {
408 /* this is necessary to handle the case of seamless looping */
409 end_frame = _transport_frame + (nframes_t) floor (nframes * _transport_speed);
414 } /* implicit release of route lock */
417 if (session_needs_butler) {
421 if (!_engine.freewheeling() && session_send_mtc) {
422 send_midi_time_code_in_another_thread ();
429 Session::reset_slave_state ()
431 average_slave_delta = 1800;
432 delta_accumulator_cnt = 0;
433 have_first_delta_accumulator = false;
434 slave_state = Stopped;
438 Session::transport_locked () const
442 if (!locate_pending() && ((Config->get_slave_source() == None) || (sl && sl->ok() && sl->locked()))) {
450 Session::follow_slave (nframes_t nframes)
453 nframes_t slave_transport_frame;
454 nframes_t this_delta;
460 Config->set_slave_source (None);
464 _slave->speed_and_position (slave_speed, slave_transport_frame);
466 if (!_slave->locked()) {
467 // cerr << "Slave not locked, not rolling\n";
471 if (slave_transport_frame > _transport_frame) {
472 this_delta = slave_transport_frame - _transport_frame;
475 this_delta = _transport_frame - slave_transport_frame;
479 if ((starting = _slave->starting())) {
484 cerr << "delta = " << (int) (dir * this_delta)
485 << " speed = " << slave_speed
486 << " ts = " << _transport_speed
487 << " M@ "<< slave_transport_frame << " S@ " << _transport_frame
488 << " avgdelta = " << average_slave_delta
492 if (_slave->is_always_synced() || Config->get_timecode_source_is_synced()) {
494 /* if the TC source is synced, then we assume that its
495 speed is binary: 0.0 or 1.0
498 if (slave_speed != 0.0f) {
504 /* TC source is able to drift relative to us (slave)
505 so we need to keep track of the drift and adjust
506 our speed to remain locked.
509 if (delta_accumulator_cnt >= delta_accumulator_size) {
510 have_first_delta_accumulator = true;
511 delta_accumulator_cnt = 0;
514 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
515 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
518 if (have_first_delta_accumulator) {
519 average_slave_delta = 0;
520 for (int i = 0; i < delta_accumulator_size; ++i) {
521 average_slave_delta += delta_accumulator[i];
523 average_slave_delta /= delta_accumulator_size;
524 if (average_slave_delta < 0) {
526 average_slave_delta = -average_slave_delta;
530 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
534 if (slave_speed != 0.0f) {
536 /* slave is running */
538 switch (slave_state) {
540 if (_slave->requires_seekahead()) {
541 slave_wait_end = slave_transport_frame + _current_frame_rate;
542 locate (slave_wait_end, false, false);
543 slave_state = Waiting;
548 slave_state = Running;
550 Location* al = _locations.auto_loop_location();
552 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
554 request_play_loop(false);
557 if (slave_transport_frame != _transport_frame) {
558 locate (slave_transport_frame, false, false);
571 if (slave_state == Waiting) {
573 // cerr << "waiting at " << slave_transport_frame << endl;
575 if (slave_transport_frame >= slave_wait_end) {
576 // cerr << "\tstart at " << _transport_frame << endl;
578 slave_state = Running;
581 nframes_t frame_delta = slave_transport_frame - _transport_frame;
583 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
585 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
586 if (!(*i)->can_internal_playback_seek (frame_delta)) {
593 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
594 (*i)->internal_playback_seek (frame_delta);
596 _transport_frame += frame_delta;
599 // cerr << "cannot micro-seek\n";
603 memset (delta_accumulator, 0, sizeof (nframes_t) * delta_accumulator_size);
604 average_slave_delta = 0;
609 if (slave_state == Running && _transport_speed == 0.0f) {
611 // cerr << "slave starts transport\n";
618 /* slave has stopped */
620 if (_transport_speed != 0.0f) {
622 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
623 // << " tf = " << _transport_frame
626 if (Config->get_slave_source() == JACK) {
627 last_stop_frame = _transport_frame;
633 if (slave_transport_frame != _transport_frame) {
634 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
635 force_locate (slave_transport_frame, false);
638 slave_state = Stopped;
641 if (slave_state == Running && !_slave->is_always_synced() && !Config->get_timecode_source_is_synced()) {
644 if (_transport_speed != 0.0f) {
647 note that average_dir is +1 or -1
650 const float adjust_seconds = 1.0f;
653 //if (average_slave_delta == 0) {
657 // delta = average_slave_delta;
658 // delta *= average_dir;
661 float adjusted_speed = slave_speed +
662 (delta / (adjust_seconds * _current_frame_rate));
665 cerr << "adjust using " << delta
666 << " towards " << adjusted_speed
667 << " ratio = " << adjusted_speed / slave_speed
668 << " current = " << _transport_speed
669 << " 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);
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 */
737 Session::process_without_events (nframes_t nframes)
739 bool session_needs_butler = false;
740 nframes_t stop_limit;
743 if (!process_can_proceed()) {
748 if (!_exporting && _slave) {
749 if (!follow_slave (nframes)) {
754 if (_transport_speed == 0) {
759 if (actively_recording()) {
760 stop_limit = max_frames;
762 if (Config->get_stop_at_session_end()) {
763 stop_limit = current_end_frame();
765 stop_limit = max_frames;
769 if (maybe_stop (stop_limit)) {
774 if (maybe_sync_start (nframes)) {
778 click (_transport_frame, nframes);
780 prepare_diskstreams ();
782 frames_moved = (long) floor (_transport_speed * nframes);
784 if (process_routes (nframes)) {
789 commit_diskstreams (nframes, session_needs_butler);
791 if (frames_moved < 0) {
792 decrement_transport_position (-frames_moved);
794 increment_transport_position (frames_moved);
797 maybe_stop (stop_limit);
798 check_declick_out ();
800 if (session_needs_butler) {
804 if (!_engine.freewheeling() && session_send_mtc) {
805 send_midi_time_code_in_another_thread ();
812 Session::process_audition (nframes_t nframes)
815 boost::shared_ptr<RouteList> r = routes.reader ();
817 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
818 if (!(*i)->hidden()) {
819 (*i)->silence (nframes);
823 /* run the auditioner, and if it says we need butler service, ask for it */
825 if (auditioner->play_audition (nframes) > 0) {
829 /* handle pending events */
831 while (pending_events.read (&ev, 1) == 1) {
835 /* if we are not in the middle of a state change,
836 and there are immediate events queued up,
840 while (!non_realtime_work_pending() && !immediate_events.empty()) {
841 Event *ev = immediate_events.front ();
842 immediate_events.pop_front ();
846 if (!auditioner->active()) {
847 process_function = &Session::process_with_events;
852 Session::maybe_sync_start (nframes_t& nframes)
854 nframes_t sync_offset;
856 if (!waiting_for_sync_offset) {
860 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
862 /* generate silence up to the sync point, then
863 adjust nframes + offset to reflect whatever
867 no_roll (sync_offset);
868 nframes -= sync_offset;
869 Port::increment_port_offset (sync_offset);
870 waiting_for_sync_offset = false;
873 return true; // done, nothing left to process
878 /* sync offset point is not within this process()
879 cycle, so just generate silence. and don't bother
880 with any fancy stuff here, just the minimal silence.
885 if (Config->get_locate_while_waiting_for_sync()) {
886 if (micro_locate (nframes)) {
887 /* XXX ERROR !!! XXX */
891 return true; // done, nothing left to process