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 (synced_to_jack() && waiting_to_start) {
51 if ( _engine.transport_state() == AudioEngine::TransportRolling) {
52 actually_start_transport ();
56 if (non_realtime_work_pending()) {
57 if (!transport_work_requested ()) {
62 (this->*process_function) (nframes);
64 SendFeedback (); /* EMIT SIGNAL */
68 Session::prepare_diskstreams ()
70 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
71 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
77 Session::no_roll (nframes_t nframes, nframes_t offset)
79 nframes_t end_frame = _transport_frame + nframes;
81 bool declick = get_transport_declick_required();
82 boost::shared_ptr<RouteList> r = routes.reader ();
85 _click_io->silence (nframes, offset);
88 if (g_atomic_int_get (&processing_prohibited)) {
89 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
90 (*i)->silence (nframes, offset);
95 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
101 (*i)->set_pending_declick (declick);
103 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(),
104 actively_recording(), declick)) {
105 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
115 Session::process_routes (nframes_t nframes, nframes_t offset)
118 int declick = get_transport_declick_required();
119 bool rec_monitors = get_rec_monitors_input();
120 boost::shared_ptr<RouteList> r = routes.reader ();
122 if (transport_sub_state & StopPendingCapture) {
123 /* force a declick out */
127 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
129 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
133 if ((*i)->hidden()) {
137 (*i)->set_pending_declick (declick);
139 if ((ret = (*i)->roll (nframes, _transport_frame, _transport_frame + nframes, offset, declick, record_active, rec_monitors)) < 0) {
141 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
142 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
143 call path, so make sure we release any outstanding locks here before we return failure.
146 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
147 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
160 Session::silent_process_routes (nframes_t nframes, nframes_t offset)
162 bool record_active = actively_recording();
163 int declick = get_transport_declick_required();
164 bool rec_monitors = get_rec_monitors_input();
165 boost::shared_ptr<RouteList> r = routes.reader ();
167 if (transport_sub_state & StopPendingCapture) {
168 /* force a declick out */
172 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
176 if ((*i)->hidden()) {
180 if ((ret = (*i)->silent_roll (nframes, _transport_frame, _transport_frame + nframes, offset, record_active, rec_monitors)) < 0) {
182 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
183 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
184 call path, so make sure we release any outstanding locks here before we return failure.
187 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
188 for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
201 Session::commit_diskstreams (nframes_t nframes, bool &needs_butler)
207 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
208 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
210 if ((*i)->hidden()) {
214 /* force all diskstreams not handled by a Route to call do their stuff.
215 Note: the diskstreams that were handled by a route will just return zero
216 from this call, because they know they were processed. So in fact, this
217 also runs commit() for every diskstream.
220 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
221 if ((*i)->commit (nframes)) {
225 } else if (dret < 0) {
229 pworst = min (pworst, (*i)->playback_buffer_load());
230 cworst = min (cworst, (*i)->capture_buffer_load());
233 uint32_t pmin = g_atomic_int_get (&_playback_load);
234 uint32_t pminold = g_atomic_int_get (&_playback_load_min);
235 uint32_t cmin = g_atomic_int_get (&_capture_load);
236 uint32_t cminold = g_atomic_int_get (&_capture_load_min);
238 g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
239 g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
240 g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
241 g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
243 if (actively_recording()) {
249 Session::process_with_events (nframes_t nframes)
252 nframes_t this_nframes;
255 bool session_needs_butler = false;
256 nframes_t stop_limit;
259 /* make sure the auditioner is silent */
262 auditioner->silence (nframes, 0);
265 /* handle any pending events */
267 while (pending_events.read (&ev, 1) == 1) {
271 /* if we are not in the middle of a state change,
272 and there are immediate events queued up,
276 while (!non_realtime_work_pending() && !immediate_events.empty()) {
277 Event *ev = immediate_events.front ();
278 immediate_events.pop_front ();
282 if (!process_can_proceed()) {
287 if (events.empty() || next_event == events.end()) {
288 process_without_events (nframes);
292 end_frame = _transport_frame + (nframes_t)abs(floor(nframes * _transport_speed));
296 Events::iterator the_next_one;
298 if (!process_can_proceed()) {
303 if (!_exporting && _slave) {
304 if (!follow_slave (nframes, 0)) {
309 if (_transport_speed == 0) {
310 no_roll (nframes, 0);
314 if (actively_recording()) {
315 stop_limit = max_frames;
318 if (Config->get_stop_at_session_end()) {
319 stop_limit = current_end_frame();
321 stop_limit = max_frames;
325 if (maybe_stop (stop_limit)) {
326 no_roll (nframes, 0);
330 this_event = *next_event;
331 the_next_one = next_event;
338 this_nframes = nframes; /* real (jack) time relative */
339 frames_moved = (long) floor (_transport_speed * nframes); /* transport relative */
341 /* running an event, position transport precisely to its time */
342 if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
343 /* this isn't quite right for reverse play */
344 frames_moved = (long) (this_event->action_frame - _transport_frame);
345 this_nframes = (nframes_t) abs( floor(frames_moved / _transport_speed) );
350 click (_transport_frame, nframes, offset);
352 /* now process frames between now and the first event in this block */
353 prepare_diskstreams ();
355 if (process_routes (this_nframes, offset)) {
356 no_roll (nframes, 0);
360 commit_diskstreams (this_nframes, session_needs_butler);
362 nframes -= this_nframes;
363 offset += this_nframes;
365 if (frames_moved < 0) {
366 decrement_transport_position (-frames_moved);
368 increment_transport_position (frames_moved);
371 maybe_stop (stop_limit);
372 check_declick_out ();
375 /* now handle this event and all others scheduled for the same time */
377 while (this_event && this_event->action_frame == _transport_frame) {
378 process_event (this_event);
380 if (the_next_one == events.end()) {
383 this_event = *the_next_one;
388 /* if an event left our state changing, do the right thing */
390 if (non_realtime_work_pending()) {
391 no_roll (nframes, offset);
395 /* this is necessary to handle the case of seamless looping */
396 end_frame = _transport_frame + (nframes_t) floor (nframes * _transport_speed);
402 } /* implicit release of route lock */
405 if (session_needs_butler) {
409 if (!_engine.freewheeling() && session_send_mtc) {
410 send_midi_time_code_in_another_thread ();
417 Session::reset_slave_state ()
419 average_slave_delta = 1800;
420 delta_accumulator_cnt = 0;
421 have_first_delta_accumulator = false;
422 slave_state = Stopped;
426 Session::transport_locked () const
430 if (!locate_pending() && ((Config->get_slave_source() == None) || (sl && sl->ok() && sl->locked()))) {
438 Session::follow_slave (nframes_t nframes, nframes_t offset)
441 nframes_t slave_transport_frame;
442 nframes_t this_delta;
448 Config->set_slave_source (None);
452 _slave->speed_and_position (slave_speed, slave_transport_frame);
454 if (!_slave->locked()) {
455 cerr << "Slave not locked, not rolling\n";
459 if (slave_transport_frame > _transport_frame) {
460 this_delta = slave_transport_frame - _transport_frame;
463 this_delta = _transport_frame - slave_transport_frame;
467 if ((starting = _slave->starting())) {
472 cerr << "delta = " << (int) (dir * this_delta)
473 << " speed = " << slave_speed
474 << " ts = " << _transport_speed
475 << " M@ "<< slave_transport_frame << " S@ " << _transport_frame
476 << " avgdelta = " << average_slave_delta
480 if (_slave->is_always_synced() || Config->get_timecode_source_is_synced()) {
482 /* if the TC source is synced, then we assume that its
483 speed is binary: 0.0 or 1.0
486 if (slave_speed != 0.0f) {
492 /* TC source is able to drift relative to us (slave)
493 so we need to keep track of the drift and adjust
494 our speed to remain locked.
497 if (delta_accumulator_cnt >= delta_accumulator_size) {
498 have_first_delta_accumulator = true;
499 delta_accumulator_cnt = 0;
502 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
503 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
506 if (have_first_delta_accumulator) {
507 average_slave_delta = 0;
508 for (int i = 0; i < delta_accumulator_size; ++i) {
509 average_slave_delta += delta_accumulator[i];
511 average_slave_delta /= delta_accumulator_size;
512 if (average_slave_delta < 0) {
514 average_slave_delta = -average_slave_delta;
518 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
522 if (slave_speed != 0.0f) {
524 /* slave is running */
526 switch (slave_state) {
528 if (_slave->requires_seekahead()) {
529 slave_wait_end = slave_transport_frame + _current_frame_rate;
530 locate (slave_wait_end, false, false);
531 slave_state = Waiting;
536 slave_state = Running;
538 Location* al = _locations.auto_loop_location();
540 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
542 request_play_loop(false);
545 if (slave_transport_frame != _transport_frame) {
546 locate (slave_transport_frame, false, false);
559 if (slave_state == Waiting) {
561 // cerr << "waiting at " << slave_transport_frame << endl;
563 if (slave_transport_frame >= slave_wait_end) {
564 // cerr << "\tstart at " << _transport_frame << endl;
566 slave_state = Running;
569 nframes_t frame_delta = slave_transport_frame - _transport_frame;
571 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
573 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
574 if (!(*i)->can_internal_playback_seek (frame_delta)) {
581 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
582 (*i)->internal_playback_seek (frame_delta);
584 _transport_frame += frame_delta;
587 // cerr << "cannot micro-seek\n";
591 memset (delta_accumulator, 0, sizeof (nframes_t) * delta_accumulator_size);
592 average_slave_delta = 0;
597 if (slave_state == Running && _transport_speed == 0.0f) {
599 // cerr << "slave starts transport\n";
606 /* slave has stopped */
608 if (_transport_speed != 0.0f) {
610 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
611 // << " tf = " << _transport_frame
614 if (Config->get_slave_source() == JACK) {
615 last_stop_frame = _transport_frame;
621 if (slave_transport_frame != _transport_frame) {
622 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
623 force_locate (slave_transport_frame, false);
626 slave_state = Stopped;
629 if (slave_state == Running && !_slave->is_always_synced() && !Config->get_timecode_source_is_synced()) {
632 if (_transport_speed != 0.0f) {
635 note that average_dir is +1 or -1
638 const float adjust_seconds = 1.0f;
641 //if (average_slave_delta == 0) {
645 // delta = average_slave_delta;
646 // delta *= average_dir;
649 float adjusted_speed = slave_speed +
650 (delta / (adjust_seconds * _current_frame_rate));
652 // cerr << "adjust using " << delta
653 // << " towards " << adjusted_speed
654 // << " ratio = " << adjusted_speed / slave_speed
655 // << " current = " << _transport_speed
656 // << " slave @ " << slave_speed
659 request_transport_speed (adjusted_speed);
662 if ((nframes_t) average_slave_delta > _slave->resolution()) {
663 // cerr << "not locked\n";
670 if (!starting && !non_realtime_work_pending()) {
671 /* speed is set, we're locked, and good to go */
677 if (slave_speed && _transport_speed) {
679 /* something isn't right, but we should move with the master
685 prepare_diskstreams ();
686 silent_process_routes (nframes, offset);
687 commit_diskstreams (nframes, need_butler);
693 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
695 if (frames_moved < 0) {
696 decrement_transport_position (-frames_moved);
698 increment_transport_position (frames_moved);
701 nframes_t stop_limit;
703 if (actively_recording()) {
704 stop_limit = max_frames;
706 if (Config->get_stop_at_session_end()) {
707 stop_limit = current_end_frame();
709 stop_limit = max_frames;
713 maybe_stop (stop_limit);
717 /* don't move at all */
718 no_roll (nframes, 0);
723 Session::process_without_events (nframes_t nframes)
725 bool session_needs_butler = false;
726 nframes_t stop_limit;
728 nframes_t offset = 0;
730 if (!process_can_proceed()) {
735 if (!_exporting && _slave) {
736 if (!follow_slave (nframes, 0)) {
741 if (_transport_speed == 0) {
742 no_roll (nframes, 0);
746 if (actively_recording()) {
747 stop_limit = max_frames;
749 if (Config->get_stop_at_session_end()) {
750 stop_limit = current_end_frame();
752 stop_limit = max_frames;
756 if (maybe_stop (stop_limit)) {
757 no_roll (nframes, 0);
761 if (maybe_sync_start (nframes, offset)) {
765 click (_transport_frame, nframes, offset);
767 prepare_diskstreams ();
769 frames_moved = (long) floor (_transport_speed * nframes);
771 if (process_routes (nframes, offset)) {
772 no_roll (nframes, offset);
776 commit_diskstreams (nframes, session_needs_butler);
778 if (frames_moved < 0) {
779 decrement_transport_position (-frames_moved);
781 increment_transport_position (frames_moved);
784 maybe_stop (stop_limit);
785 check_declick_out ();
787 if (session_needs_butler) {
791 if (!_engine.freewheeling() && session_send_mtc) {
792 send_midi_time_code_in_another_thread ();
799 Session::process_audition (nframes_t nframes)
802 boost::shared_ptr<RouteList> r = routes.reader ();
804 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
805 if (!(*i)->hidden()) {
806 (*i)->silence (nframes, 0);
810 /* run the auditioner, and if it says we need butler service, ask for it */
812 if (auditioner->play_audition (nframes) > 0) {
816 /* handle pending events */
818 while (pending_events.read (&ev, 1) == 1) {
822 /* if we are not in the middle of a state change,
823 and there are immediate events queued up,
827 while (!non_realtime_work_pending() && !immediate_events.empty()) {
828 Event *ev = immediate_events.front ();
829 immediate_events.pop_front ();
833 if (!auditioner->active()) {
834 process_function = &Session::process_with_events;
839 Session::maybe_sync_start (nframes_t& nframes, nframes_t& offset)
841 nframes_t sync_offset;
843 if (!waiting_for_sync_offset) {
847 if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
849 no_roll (sync_offset, 0);
850 nframes -= sync_offset;
851 offset += sync_offset;
852 waiting_for_sync_offset = false;
859 no_roll (nframes, 0);