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 if (!process_can_proceed()) {
290 if (events.empty() || next_event == events.end()) {
291 process_without_events (nframes);
295 end_frame = _transport_frame + (nframes_t)abs(floor(nframes * _transport_speed));
299 Events::iterator the_next_one;
301 if (!process_can_proceed()) {
306 if (!_exporting && _slave) {
307 if (!follow_slave (nframes)) {
312 if (_transport_speed == 0) {
317 if (actively_recording()) {
318 stop_limit = max_frames;
321 if (Config->get_stop_at_session_end()) {
322 stop_limit = current_end_frame();
324 stop_limit = max_frames;
328 if (maybe_stop (stop_limit)) {
333 this_event = *next_event;
334 the_next_one = next_event;
340 this_nframes = nframes; /* real (jack) time relative */
341 frames_moved = (long) floor (_transport_speed * nframes); /* transport relative */
343 /* running an event, position transport precisely to its time */
344 if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
345 /* this isn't quite right for reverse play */
346 frames_moved = (long) (this_event->action_frame - _transport_frame);
347 this_nframes = (nframes_t) abs( floor(frames_moved / _transport_speed) );
352 click (_transport_frame, nframes);
354 /* now process frames between now and the first event in this block */
355 prepare_diskstreams ();
357 if (process_routes (this_nframes)) {
362 commit_diskstreams (this_nframes, session_needs_butler);
364 nframes -= this_nframes;
366 if (frames_moved < 0) {
367 decrement_transport_position (-frames_moved);
369 increment_transport_position (frames_moved);
372 maybe_stop (stop_limit);
373 check_declick_out ();
376 /* reset port offsets so that Port::get_buffer() will fetch the correct data */
378 Port::increment_port_offset (this_nframes);
380 /* now handle this event and all others scheduled for the same time */
382 while (this_event && this_event->action_frame == _transport_frame) {
383 process_event (this_event);
385 if (the_next_one == events.end()) {
388 this_event = *the_next_one;
393 /* if an event left our state changing, do the right thing */
395 if (nframes && non_realtime_work_pending()) {
400 /* this is necessary to handle the case of seamless looping */
401 end_frame = _transport_frame + (nframes_t) floor (nframes * _transport_speed);
406 } /* implicit release of route lock */
409 if (session_needs_butler) {
413 if (!_engine.freewheeling() && session_send_mtc) {
414 send_midi_time_code_in_another_thread ();
421 Session::reset_slave_state ()
423 average_slave_delta = 1800;
424 delta_accumulator_cnt = 0;
425 have_first_delta_accumulator = false;
426 slave_state = Stopped;
430 Session::transport_locked () const
434 if (!locate_pending() && ((Config->get_slave_source() == None) || (sl && sl->ok() && sl->locked()))) {
442 Session::follow_slave (nframes_t nframes)
445 nframes_t slave_transport_frame;
446 nframes_t this_delta;
452 Config->set_slave_source (None);
456 _slave->speed_and_position (slave_speed, slave_transport_frame);
458 if (!_slave->locked()) {
459 // cerr << "Slave not locked, not rolling\n";
463 if (slave_transport_frame > _transport_frame) {
464 this_delta = slave_transport_frame - _transport_frame;
467 this_delta = _transport_frame - slave_transport_frame;
471 if ((starting = _slave->starting())) {
476 cerr << "delta = " << (int) (dir * this_delta)
477 << " speed = " << slave_speed
478 << " ts = " << _transport_speed
479 << " M@ "<< slave_transport_frame << " S@ " << _transport_frame
480 << " avgdelta = " << average_slave_delta
484 if (_slave->is_always_synced() || Config->get_timecode_source_is_synced()) {
486 /* if the TC source is synced, then we assume that its
487 speed is binary: 0.0 or 1.0
490 if (slave_speed != 0.0f) {
496 /* TC source is able to drift relative to us (slave)
497 so we need to keep track of the drift and adjust
498 our speed to remain locked.
501 if (delta_accumulator_cnt >= delta_accumulator_size) {
502 have_first_delta_accumulator = true;
503 delta_accumulator_cnt = 0;
506 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
507 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
510 if (have_first_delta_accumulator) {
511 average_slave_delta = 0;
512 for (int i = 0; i < delta_accumulator_size; ++i) {
513 average_slave_delta += delta_accumulator[i];
515 average_slave_delta /= delta_accumulator_size;
516 if (average_slave_delta < 0) {
518 average_slave_delta = -average_slave_delta;
522 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
526 if (slave_speed != 0.0f) {
528 /* slave is running */
530 switch (slave_state) {
532 if (_slave->requires_seekahead()) {
533 slave_wait_end = slave_transport_frame + _current_frame_rate;
534 locate (slave_wait_end, false, false);
535 slave_state = Waiting;
540 slave_state = Running;
542 Location* al = _locations.auto_loop_location();
544 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
546 request_play_loop(false);
549 if (slave_transport_frame != _transport_frame) {
550 locate (slave_transport_frame, false, false);
563 if (slave_state == Waiting) {
565 // cerr << "waiting at " << slave_transport_frame << endl;
567 if (slave_transport_frame >= slave_wait_end) {
568 // cerr << "\tstart at " << _transport_frame << endl;
570 slave_state = Running;
573 nframes_t frame_delta = slave_transport_frame - _transport_frame;
575 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
577 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
578 if (!(*i)->can_internal_playback_seek (frame_delta)) {
585 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
586 (*i)->internal_playback_seek (frame_delta);
588 _transport_frame += frame_delta;
591 // cerr << "cannot micro-seek\n";
595 memset (delta_accumulator, 0, sizeof (nframes_t) * delta_accumulator_size);
596 average_slave_delta = 0;
601 if (slave_state == Running && _transport_speed == 0.0f) {
603 // cerr << "slave starts transport\n";
610 /* slave has stopped */
612 if (_transport_speed != 0.0f) {
614 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
615 // << " tf = " << _transport_frame
618 if (Config->get_slave_source() == JACK) {
619 last_stop_frame = _transport_frame;
625 if (slave_transport_frame != _transport_frame) {
626 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
627 force_locate (slave_transport_frame, false);
630 slave_state = Stopped;
633 if (slave_state == Running && !_slave->is_always_synced() && !Config->get_timecode_source_is_synced()) {
636 if (_transport_speed != 0.0f) {
639 note that average_dir is +1 or -1
642 const float adjust_seconds = 1.0f;
645 //if (average_slave_delta == 0) {
649 // delta = average_slave_delta;
650 // delta *= average_dir;
653 float adjusted_speed = slave_speed +
654 (delta / (adjust_seconds * _current_frame_rate));
657 cerr << "adjust using " << delta
658 << " towards " << adjusted_speed
659 << " ratio = " << adjusted_speed / slave_speed
660 << " current = " << _transport_speed
661 << " slave @ " << slave_speed
665 request_transport_speed (adjusted_speed);
668 if ((nframes_t) average_slave_delta > _slave->resolution()) {
669 // cerr << "not locked\n";
676 if (!starting && !non_realtime_work_pending()) {
677 /* speed is set, we're locked, and good to go */
683 if (slave_speed && _transport_speed) {
685 /* something isn't right, but we should move with the master
691 prepare_diskstreams ();
692 silent_process_routes (nframes);
693 commit_diskstreams (nframes, need_butler);
699 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
701 if (frames_moved < 0) {
702 decrement_transport_position (-frames_moved);
704 increment_transport_position (frames_moved);
707 nframes_t stop_limit;
709 if (actively_recording()) {
710 stop_limit = max_frames;
712 if (Config->get_stop_at_session_end()) {
713 stop_limit = current_end_frame();
715 stop_limit = max_frames;
719 maybe_stop (stop_limit);
723 /* don't move at all */
729 Session::process_without_events (nframes_t nframes)
731 bool session_needs_butler = false;
732 nframes_t stop_limit;
735 if (!process_can_proceed()) {
740 if (!_exporting && _slave) {
741 if (!follow_slave (nframes)) {
746 if (_transport_speed == 0) {
751 if (actively_recording()) {
752 stop_limit = max_frames;
754 if (Config->get_stop_at_session_end()) {
755 stop_limit = current_end_frame();
757 stop_limit = max_frames;
761 if (maybe_stop (stop_limit)) {
766 if (maybe_sync_start (nframes)) {
770 click (_transport_frame, nframes);
772 prepare_diskstreams ();
774 frames_moved = (long) floor (_transport_speed * nframes);
776 if (process_routes (nframes)) {
781 commit_diskstreams (nframes, session_needs_butler);
783 if (frames_moved < 0) {
784 decrement_transport_position (-frames_moved);
786 increment_transport_position (frames_moved);
789 maybe_stop (stop_limit);
790 check_declick_out ();
792 if (session_needs_butler) {
796 if (!_engine.freewheeling() && session_send_mtc) {
797 send_midi_time_code_in_another_thread ();
804 Session::process_audition (nframes_t nframes)
807 boost::shared_ptr<RouteList> r = routes.reader ();
809 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
810 if (!(*i)->hidden()) {
811 (*i)->silence (nframes);
815 /* run the auditioner, and if it says we need butler service, ask for it */
817 if (auditioner->play_audition (nframes) > 0) {
821 /* handle pending events */
823 while (pending_events.read (&ev, 1) == 1) {
827 /* if we are not in the middle of a state change,
828 and there are immediate events queued up,
832 while (!non_realtime_work_pending() && !immediate_events.empty()) {
833 Event *ev = immediate_events.front ();
834 immediate_events.pop_front ();
838 if (!auditioner->active()) {
839 process_function = &Session::process_with_events;
844 Session::maybe_sync_start (nframes_t& nframes)
846 nframes_t sync_offset;
848 if (!waiting_for_sync_offset) {
852 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
854 /* generate silence up to the sync point, then
855 adjust nframes + offset to reflect whatever
859 no_roll (sync_offset);
860 nframes -= sync_offset;
861 Port::increment_port_offset (sync_offset);
862 waiting_for_sync_offset = false;
865 return true; // done, nothing left to process
870 /* sync offset point is not within this process()
871 cycle, so just generate silence. and don't bother
872 with any fancy stuff here, just the minimal silence.
877 if (Config->get_locate_while_waiting_for_sync()) {
878 if (micro_locate (nframes)) {
879 /* XXX ERROR !!! XXX */
883 return true; // done, nothing left to process