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 <ardour/timestamps.h>
28 #include <pbd/error.h>
29 #include <pbd/atomic.h>
30 #include <pbd/lockmonitor.h>
32 #include <ardour/ardour.h>
33 #include <ardour/session.h>
34 #include <ardour/diskstream.h>
35 #include <ardour/audioengine.h>
36 #include <ardour/slave.h>
37 #include <ardour/auditioner.h>
38 #include <ardour/cycles.h>
39 #include <ardour/cycle_timer.h>
41 #include <midi++/manager.h>
45 using namespace ARDOUR;
46 //using namespace sigc;
50 Session::process (jack_nframes_t nframes)
52 //cerr << "CYCLE START " << _transport_frame << "-------------------" << endl;
54 MIDI::Manager::instance()->cycle_start(nframes);
56 if (synced_to_jack() && waiting_to_start) {
57 if ( _engine.transport_state() == AudioEngine::TransportRolling) {
58 actually_start_transport ();
62 if (non_realtime_work_pending()) {
63 if (atomic_read (&butler_should_do_transport_work) == 0) {
68 (this->*process_function) (nframes);
70 MIDI::Manager::instance()->cycle_end();
72 //cerr << "CYCLE END " << _transport_frame << "-----------------------" << endl;
76 Session::prepare_diskstreams ()
78 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
84 Session::no_roll (jack_nframes_t nframes, jack_nframes_t offset)
86 jack_nframes_t end_frame = _transport_frame + nframes;
88 bool declick = get_transport_declick_required();
91 _click_io->silence (nframes, offset);
94 /* XXX we're supposed to have the route_lock while doing this.
95 this is really bad ...
98 if (atomic_read (&processing_prohibited)) {
99 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
100 (*i)->silence (nframes, offset);
105 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
107 if ((*i)->hidden()) {
111 (*i)->set_pending_declick (declick);
113 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(),
114 actively_recording(), declick)) {
115 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
125 Session::process_routes (jack_nframes_t nframes, jack_nframes_t offset)
128 int declick = get_transport_declick_required();
129 bool rec_monitors = get_rec_monitors_input();
131 if (transport_sub_state & StopPendingCapture) {
132 /* force a declick out */
136 record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
138 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
142 if ((*i)->hidden()) {
146 (*i)->set_pending_declick (declick);
148 if ((ret = (*i)->roll (nframes, _transport_frame, _transport_frame + nframes, offset, declick, record_active, rec_monitors)) < 0) {
150 /* we have to do this here. Route::roll() for an AudioTrack will have called DiskStream::process(),
151 and the DS will expect DiskStream::commit() to be called. but we're aborting from that
152 call path, so make sure we release any outstanding locks here before we return failure.
155 for (DiskStreamList::iterator ids = diskstreams.begin(); ids != diskstreams.end(); ++ids) {
168 Session::silent_process_routes (jack_nframes_t nframes, jack_nframes_t offset)
170 bool record_active = actively_recording();
171 int declick = get_transport_declick_required();
172 bool rec_monitors = get_rec_monitors_input();
174 if (transport_sub_state & StopPendingCapture) {
175 /* force a declick out */
179 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
183 if ((*i)->hidden()) {
187 if ((ret = (*i)->silent_roll (nframes, _transport_frame, _transport_frame + nframes, offset, record_active, rec_monitors)) < 0) {
189 /* we have to do this here. Route::roll() for an AudioTrack will have called DiskStream::process(),
190 and the DS will expect DiskStream::commit() to be called. but we're aborting from that
191 call path, so make sure we release any outstanding locks here before we return failure.
194 for (DiskStreamList::iterator ids = diskstreams.begin(); ids != diskstreams.end(); ++ids) {
207 Session::commit_diskstreams (jack_nframes_t nframes, bool &needs_butler)
213 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
215 if ((*i)->hidden()) {
219 /* force all diskstreams not handled by a Route to call do their stuff.
222 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
223 if ((*i)->commit (nframes)) {
227 } else if (dret < 0) {
231 pworst = min (pworst, (*i)->playback_buffer_load());
232 cworst = min (cworst, (*i)->capture_buffer_load());
235 uint32_t pmin = atomic_read (&_playback_load);
236 uint32_t pminold = atomic_read (&_playback_load_min);
237 uint32_t cmin = atomic_read (&_capture_load);
238 uint32_t cminold = atomic_read (&_capture_load_min);
240 atomic_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
241 atomic_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
242 atomic_set (&_playback_load_min, min (pmin, pminold));
243 atomic_set (&_capture_load_min, min (cmin, cminold));
245 if (actively_recording()) {
252 Session::process_with_events (jack_nframes_t nframes)
255 jack_nframes_t this_nframes;
256 jack_nframes_t end_frame;
257 jack_nframes_t offset;
258 jack_nframes_t stop_limit;
260 bool session_needs_butler = false;
263 auditioner->silence (nframes, 0);
266 while (pending_events.read (&ev, 1) == 1) {
270 /* if we are not in the middle of a state change,
271 and there are immediate events queued up,
275 while (!non_realtime_work_pending() && !immediate_events.empty()) {
276 Event *ev = immediate_events.front ();
277 immediate_events.pop_front ();
281 /* Events caused a transport change, send an MTC Full Frame (SMPTE) message.
282 * This is sent whether rolling or not, to give slaves an idea of ardour time
283 * on locates (and allow slow slaves to position and prepare for rolling)
285 if (_send_smpte_update) {
286 send_full_time_code(nframes);
289 if (!process_can_proceed()) {
290 no_roll (nframes, 0);
294 if (events.empty() || next_event == events.end()) {
295 process_without_events (nframes);
299 end_frame = _transport_frame + nframes;
302 TentativeRWLockMonitor rm (route_lock, false, __LINE__, __FILE__);
303 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
306 Events::iterator the_next_one;
308 if (!rm.locked() || !dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
309 no_roll (nframes, 0);
313 if (!_exporting && _slave) {
314 if (!follow_slave (nframes, 0)) {
319 if (_transport_speed == 0) {
320 no_roll (nframes, 0);
324 send_midi_time_code_for_cycle(nframes);
326 if (actively_recording()) {
327 stop_limit = max_frames;
330 if (Config->get_stop_at_session_end()) {
331 stop_limit = current_end_frame();
333 stop_limit = max_frames;
337 if (maybe_stop (stop_limit)) {
338 no_roll (nframes, 0);
342 this_event = *next_event;
343 the_next_one = next_event;
350 if (this_event == 0 || this_event->action_frame > end_frame || this_event->action_frame < _transport_frame) {
352 this_nframes = nframes;
356 /* compute nframes to next event */
358 if (this_event->action_frame < end_frame) {
359 this_nframes = nframes - (end_frame - this_event->action_frame);
361 this_nframes = nframes;
367 click (_transport_frame, nframes, offset);
369 /* now process frames between now and the first event in this block */
370 prepare_diskstreams ();
372 if (process_routes (this_nframes, offset)) {
373 no_roll (nframes, 0);
377 commit_diskstreams (this_nframes, session_needs_butler);
379 nframes -= this_nframes;
380 offset += this_nframes;
382 frames_moved = (jack_nframes_t) floor (_transport_speed * this_nframes);
384 if (frames_moved < 0) {
385 decrement_transport_position (-frames_moved);
387 increment_transport_position (frames_moved);
390 maybe_stop (stop_limit);
391 check_declick_out ();
394 /* now handle this event and all others scheduled for the same time */
396 while (this_event && this_event->action_frame == _transport_frame) {
397 process_event (this_event);
399 if (the_next_one == events.end()) {
402 this_event = *the_next_one;
407 /* if an event left our state changing, do the right thing */
409 if (non_realtime_work_pending()) {
410 no_roll (nframes, offset);
414 /* this is necessary to handle the case of seamless looping */
415 /* not sure if it will work in conjuction with varispeed */
416 end_frame = _transport_frame + nframes;
422 } /* implicit release of route lock */
424 if (session_needs_butler)
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() && ((_slave_type == None) || (sl && sl->ok() && sl->locked()))) {
450 Session::follow_slave (jack_nframes_t nframes, jack_nframes_t offset)
453 jack_nframes_t slave_transport_frame;
454 jack_nframes_t this_delta;
460 set_slave_source (None, 0);
464 _slave->speed_and_position (slave_speed, slave_transport_frame);
466 if (!_slave->locked()) {
470 if (slave_transport_frame > _transport_frame) {
471 this_delta = slave_transport_frame - _transport_frame;
474 this_delta = _transport_frame - slave_transport_frame;
478 if ((starting = _slave->starting())) {
483 cerr << "delta = " << (int) (dir * this_delta)
484 << " speed = " << slave_speed
485 << " ts = " << _transport_speed
486 << " M@"<< slave_transport_frame << " S@" << _transport_frame
487 << " avgdelta = " << average_slave_delta
491 if (Config->get_timecode_source_is_synced()) {
493 /* if the TC source is synced, then we assume that its
494 speed is binary: 0.0 or 1.0
497 if (slave_speed != 0.0f) {
503 /* TC source is able to drift relative to us (slave)
504 so we need to keep track of the drift and adjust
505 our speed to remain locked.
508 if (delta_accumulator_cnt >= delta_accumulator_size) {
509 have_first_delta_accumulator = true;
510 delta_accumulator_cnt = 0;
513 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
514 delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
517 if (have_first_delta_accumulator) {
518 average_slave_delta = 0;
519 for (int i = 0; i < delta_accumulator_size; ++i) {
520 average_slave_delta += delta_accumulator[i];
522 average_slave_delta /= delta_accumulator_size;
523 if (average_slave_delta < 0) {
525 average_slave_delta = -average_slave_delta;
529 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
533 if (slave_speed != 0.0f) {
535 /* slave is running */
537 switch (slave_state) {
539 if (_slave->requires_seekahead()) {
540 slave_wait_end = slave_transport_frame + _current_frame_rate;
541 locate (slave_wait_end, false, false);
542 slave_state = Waiting;
547 slave_state = Running;
549 Location* al = _locations.auto_loop_location();
551 if (al && auto_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
553 request_auto_loop(false);
556 if (slave_transport_frame != _transport_frame) {
557 locate (slave_transport_frame, false, false);
570 if (slave_state == Waiting) {
572 // cerr << "waiting at " << slave_transport_frame << endl;
573 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
575 if (dsm.locked() && slave_transport_frame >= slave_wait_end) {
576 // cerr << "\tstart at " << _transport_frame << endl;
578 slave_state = Running;
581 jack_nframes_t frame_delta = slave_transport_frame - _transport_frame;
583 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
584 if (!(*i)->can_internal_playback_seek (frame_delta)) {
591 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
592 (*i)->internal_playback_seek (frame_delta);
594 _transport_frame += frame_delta;
597 // cerr << "cannot micro-seek\n";
601 memset (delta_accumulator, 0, sizeof (jack_nframes_t) * delta_accumulator_size);
602 average_slave_delta = 0;
607 if (slave_state == Running && _transport_speed == 0.0f) {
609 // cerr << "slave starts transport\n";
616 /* slave has stopped */
618 if (_transport_speed != 0.0f) {
620 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
621 // << " tf = " << _transport_frame
624 if (_slave_type == JACK) {
625 last_stop_frame = _transport_frame;
631 if (slave_transport_frame != _transport_frame) {
632 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
633 force_locate (slave_transport_frame, false);
636 slave_state = Stopped;
639 if (slave_state == Running && !Config->get_timecode_source_is_synced()) {
642 if (_transport_speed != 0.0f) {
645 note that average_dir is +1 or -1
648 const float adjust_seconds = 1.0f;
651 //if (average_slave_delta == 0) {
655 // delta = average_slave_delta;
656 // delta *= average_dir;
659 float adjusted_speed = slave_speed +
660 (delta / (adjust_seconds * _current_frame_rate));
662 // cerr << "adjust using " << delta
663 // << " towards " << adjusted_speed
664 // << " ratio = " << adjusted_speed / slave_speed
665 // << " current = " << _transport_speed
666 // << " slave @ " << slave_speed
669 request_transport_speed (adjusted_speed);
672 if ((jack_nframes_t) average_slave_delta > _slave->resolution()) {
673 // cerr << "not locked\n";
680 if (!starting && !non_realtime_work_pending()) {
681 /* speed is set, we're locked, and good to go */
687 if (slave_speed && _transport_speed) {
689 /* something isn't right, but we should move with the master
695 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
701 prepare_diskstreams ();
702 silent_process_routes (nframes, offset);
703 commit_diskstreams (nframes, need_butler);
709 jack_nframes_t frames_moved = (long) floor (_transport_speed * nframes);
711 if (frames_moved < 0) {
712 decrement_transport_position (-frames_moved);
714 increment_transport_position (frames_moved);
717 jack_nframes_t stop_limit;
719 if (actively_recording()) {
720 stop_limit = max_frames;
722 if (Config->get_stop_at_session_end()) {
723 stop_limit = current_end_frame();
725 stop_limit = max_frames;
729 maybe_stop (stop_limit);
733 /* don't move at all */
734 no_roll (nframes, 0);
739 Session::process_without_events (jack_nframes_t nframes)
741 bool session_needs_butler = false;
742 jack_nframes_t stop_limit;
746 TentativeRWLockMonitor rm (route_lock, false, __LINE__, __FILE__);
747 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
749 if (!rm.locked() || !dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
750 no_roll (nframes, 0);
754 if (!_exporting && _slave) {
755 if (!follow_slave (nframes, 0)) {
760 if (_transport_speed == 0) {
761 no_roll (nframes, 0);
765 send_midi_time_code_for_cycle(nframes);
767 if (actively_recording()) {
768 stop_limit = max_frames;
770 if (Config->get_stop_at_session_end()) {
771 stop_limit = current_end_frame();
773 stop_limit = max_frames;
777 if (maybe_stop (stop_limit)) {
778 no_roll (nframes, 0);
782 click (_transport_frame, nframes, 0);
784 prepare_diskstreams ();
786 frames_moved = (long) floor (_transport_speed * nframes);
788 if (process_routes (nframes, 0)) {
789 no_roll (nframes, 0);
793 commit_diskstreams (nframes, session_needs_butler);
795 if (frames_moved < 0) {
796 decrement_transport_position (-frames_moved);
798 increment_transport_position (frames_moved);
801 maybe_stop (stop_limit);
802 check_declick_out ();
804 } /* implicit release of route lock */
806 if (session_needs_butler)
811 Session::process_audition (jack_nframes_t nframes)
813 TentativeRWLockMonitor rm (route_lock, false, __LINE__, __FILE__);
817 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
818 if (!(*i)->hidden()) {
819 (*i)->silence (nframes, 0);
824 if (auditioner->play_audition (nframes) > 0) {
828 while (pending_events.read (&ev, 1) == 1) {
832 /* if we are not in the middle of a state change,
833 and there are immediate events queued up,
837 while (!non_realtime_work_pending() && !immediate_events.empty()) {
838 Event *ev = immediate_events.front ();
839 immediate_events.pop_front ();
843 if (!auditioner->active()) {
844 process_function = &Session::process_with_events;