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>
26 #include <pbd/enumwriter.h>
28 #include <glibmm/thread.h>
30 #include <ardour/ardour.h>
31 #include <ardour/session.h>
32 #include <ardour/timestamps.h>
33 #include <ardour/audio_diskstream.h>
34 #include <ardour/audioengine.h>
35 #include <ardour/slave.h>
36 #include <ardour/auditioner.h>
37 #include <ardour/cycles.h>
38 #include <ardour/cycle_timer.h>
42 using namespace ARDOUR
;
47 Session::process (nframes_t nframes
)
51 if (processing_blocked()) {
56 if (non_realtime_work_pending()) {
57 if (!transport_work_requested ()) {
62 (this->*process_function
) (nframes
);
65 Glib::Mutex::Lock
lm (midi_lock
, Glib::TRY_LOCK
);
66 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::fail_roll (nframes_t nframes
)
82 Port::set_port_offset (0);
83 return no_roll (nframes
);
88 Session::no_roll (nframes_t nframes
)
90 nframes_t end_frame
= _transport_frame
+ nframes
;
92 bool declick
= get_transport_declick_required();
93 boost::shared_ptr
<RouteList
> r
= routes
.reader ();
96 _click_io
->silence (nframes
);
99 for (RouteList::iterator i
= r
->begin(); i
!= r
->end(); ++i
) {
101 if ((*i
)->hidden()) {
105 (*i
)->set_pending_declick (declick
);
107 if ((*i
)->no_roll (nframes
, _transport_frame
, end_frame
, non_realtime_work_pending(),
108 actively_recording(), declick
)) {
109 error
<< string_compose(_("Session: error in no roll for %1"), (*i
)->name()) << endmsg
;
119 Session::process_routes (nframes_t nframes
)
122 int declick
= get_transport_declick_required();
123 bool rec_monitors
= get_rec_monitors_input();
124 boost::shared_ptr
<RouteList
> r
= routes
.reader ();
126 if (transport_sub_state
& StopPendingCapture
) {
127 /* force a declick out */
131 record_active
= actively_recording(); // || (get_record_enabled() && get_punch_in());
133 for (RouteList::iterator i
= r
->begin(); i
!= r
->end(); ++i
) {
137 if ((*i
)->hidden()) {
141 (*i
)->set_pending_declick (declick
);
143 if ((ret
= (*i
)->roll (nframes
, _transport_frame
, _transport_frame
+ nframes
, declick
, record_active
, rec_monitors
)) < 0) {
145 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
146 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
147 call path, so make sure we release any outstanding locks here before we return failure.
150 boost::shared_ptr
<DiskstreamList
> dsl
= diskstreams
.reader();
151 for (DiskstreamList::iterator ids
= dsl
->begin(); ids
!= dsl
->end(); ++ids
) {
164 Session::silent_process_routes (nframes_t nframes
)
166 bool record_active
= actively_recording();
167 int declick
= get_transport_declick_required();
168 bool rec_monitors
= get_rec_monitors_input();
169 boost::shared_ptr
<RouteList
> r
= routes
.reader ();
171 if (transport_sub_state
& StopPendingCapture
) {
172 /* force a declick out */
176 for (RouteList::iterator i
= r
->begin(); i
!= r
->end(); ++i
) {
180 if ((*i
)->hidden()) {
184 if ((ret
= (*i
)->silent_roll (nframes
, _transport_frame
, _transport_frame
+ nframes
, record_active
, rec_monitors
)) < 0) {
186 /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
187 and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
188 call path, so make sure we release any outstanding locks here before we return failure.
191 boost::shared_ptr
<DiskstreamList
> dsl
= diskstreams
.reader();
192 for (DiskstreamList::iterator ids
= dsl
->begin(); ids
!= dsl
->end(); ++ids
) {
205 Session::commit_diskstreams (nframes_t nframes
, bool &needs_butler
)
211 boost::shared_ptr
<DiskstreamList
> dsl
= diskstreams
.reader();
212 for (DiskstreamList::iterator i
= dsl
->begin(); i
!= dsl
->end(); ++i
) {
214 if ((*i
)->hidden()) {
218 /* force all diskstreams not handled by a Route to call do their stuff.
219 Note: the diskstreams that were handled by a route will just return zero
220 from this call, because they know they were processed. So in fact, this
221 also runs commit() for every diskstream.
224 if ((dret
= (*i
)->process (_transport_frame
, nframes
, actively_recording(), get_rec_monitors_input())) == 0) {
225 if ((*i
)->commit (nframes
)) {
229 } else if (dret
< 0) {
233 pworst
= min (pworst
, (*i
)->playback_buffer_load());
234 cworst
= min (cworst
, (*i
)->capture_buffer_load());
237 uint32_t pmin
= g_atomic_int_get (&_playback_load
);
238 uint32_t pminold
= g_atomic_int_get (&_playback_load_min
);
239 uint32_t cmin
= g_atomic_int_get (&_capture_load
);
240 uint32_t cminold
= g_atomic_int_get (&_capture_load_min
);
242 g_atomic_int_set (&_playback_load
, (uint32_t) floor (pworst
* 100.0f
));
243 g_atomic_int_set (&_capture_load
, (uint32_t) floor (cworst
* 100.0f
));
244 g_atomic_int_set (&_playback_load_min
, min (pmin
, pminold
));
245 g_atomic_int_set (&_capture_load_min
, min (cmin
, cminold
));
247 if (actively_recording()) {
253 Session::process_with_events (nframes_t nframes
)
256 nframes_t this_nframes
;
259 bool session_needs_butler
= false;
260 nframes_t stop_limit
;
265 /* make sure the auditioner is silent */
268 auditioner
->silence (nframes
);
271 /* handle any pending events */
273 while (pending_events
.read (&ev
, 1) == 1) {
274 cerr
<< "********** merge event action " << ev
->action
<< " type " << enum_2_string (ev
->type
) << " to pending\n";
278 /* if we are not in the middle of a state change,
279 and there are immediate events queued up,
283 while (!non_realtime_work_pending() && !immediate_events
.empty()) {
284 Event
*ev
= immediate_events
.front ();
285 immediate_events
.pop_front ();
286 cerr
<< "******* process immediate effect event type " << ev
->action
<< " type " << enum_2_string (ev
->type
) << endl
;
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 ();
300 if (!process_can_proceed()) {
302 cerr
<< "++PWE out 1\n";
306 if (events
.empty() || next_event
== events
.end()) {
307 process_without_events (nframes
);
308 cerr
<< "++PWE out 2\n";
312 end_frame
= _transport_frame
+ (nframes_t
)abs(floor(nframes
* _transport_speed
));
316 Events::iterator the_next_one
;
318 if (!process_can_proceed()) {
323 if (!_exporting
&& _slave
) {
324 if (!follow_slave (nframes
)) {
329 if (_transport_speed
== 0) {
331 cerr
<< "++PWE out 3\n";
335 if (actively_recording()) {
336 stop_limit
= max_frames
;
339 if (Config
->get_stop_at_session_end()) {
340 stop_limit
= current_end_frame();
342 stop_limit
= max_frames
;
346 if (maybe_stop (stop_limit
)) {
348 cerr
<< "++PWE out 4\n";
352 this_event
= *next_event
;
353 the_next_one
= next_event
;
359 this_nframes
= nframes
; /* real (jack) time relative */
360 frames_moved
= (long) floor (_transport_speed
* nframes
); /* transport relative */
362 /* running an event, position transport precisely to its time */
363 if (this_event
&& this_event
->action_frame
<= end_frame
&& this_event
->action_frame
>= _transport_frame
) {
364 /* this isn't quite right for reverse play */
365 frames_moved
= (long) (this_event
->action_frame
- _transport_frame
);
366 this_nframes
= (nframes_t
) abs( floor(frames_moved
/ _transport_speed
) );
371 click (_transport_frame
, nframes
);
373 /* now process frames between now and the first event in this block */
374 prepare_diskstreams ();
376 if (process_routes (this_nframes
)) {
378 cerr
<< "++PWE out 4\n";
382 commit_diskstreams (this_nframes
, session_needs_butler
);
384 nframes
-= this_nframes
;
386 if (frames_moved
< 0) {
387 decrement_transport_position (-frames_moved
);
389 increment_transport_position (frames_moved
);
392 maybe_stop (stop_limit
);
393 check_declick_out ();
396 /* reset port offsets so that Port::get_buffer() will fetch the correct data */
398 Port::increment_port_offset (this_nframes
);
400 /* now handle this event and all others scheduled for the same time */
402 while (this_event
&& this_event
->action_frame
== _transport_frame
) {
403 process_event (this_event
);
405 if (the_next_one
== events
.end()) {
408 this_event
= *the_next_one
;
413 /* if an event left our state changing, do the right thing */
415 if (nframes
&& non_realtime_work_pending()) {
420 /* this is necessary to handle the case of seamless looping */
421 end_frame
= _transport_frame
+ (nframes_t
) floor (nframes
* _transport_speed
);
426 } /* implicit release of route lock */
429 if (session_needs_butler
) {
433 if (!_engine
.freewheeling() && session_send_mtc
) {
434 send_midi_time_code_in_another_thread ();
437 cerr
<< "++PWE out 5\n";
442 Session::reset_slave_state ()
444 average_slave_delta
= 1800;
445 delta_accumulator_cnt
= 0;
446 have_first_delta_accumulator
= false;
447 slave_state
= Stopped
;
451 Session::transport_locked () const
455 if (!locate_pending() && ((Config
->get_slave_source() == None
) || (sl
&& sl
->ok() && sl
->locked()))) {
463 Session::follow_slave (nframes_t nframes
)
466 nframes_t slave_transport_frame
;
467 nframes_t this_delta
;
473 Config
->set_slave_source (None
);
477 _slave
->speed_and_position (slave_speed
, slave_transport_frame
);
479 if (!_slave
->locked()) {
480 // cerr << "Slave not locked, not rolling\n";
484 if (slave_transport_frame
> _transport_frame
) {
485 this_delta
= slave_transport_frame
- _transport_frame
;
488 this_delta
= _transport_frame
- slave_transport_frame
;
492 if ((starting
= _slave
->starting())) {
497 cerr
<< "delta = " << (int) (dir
* this_delta
)
498 << " speed = " << slave_speed
499 << " ts = " << _transport_speed
500 << " M@ "<< slave_transport_frame
<< " S@ " << _transport_frame
501 << " avgdelta = " << average_slave_delta
505 if (_slave
->is_always_synced() || Config
->get_timecode_source_is_synced()) {
507 /* if the TC source is synced, then we assume that its
508 speed is binary: 0.0 or 1.0
511 if (slave_speed
!= 0.0f
) {
517 /* TC source is able to drift relative to us (slave)
518 so we need to keep track of the drift and adjust
519 our speed to remain locked.
522 if (delta_accumulator_cnt
>= delta_accumulator_size
) {
523 have_first_delta_accumulator
= true;
524 delta_accumulator_cnt
= 0;
527 if (delta_accumulator_cnt
!= 0 || this_delta
< _current_frame_rate
) {
528 delta_accumulator
[delta_accumulator_cnt
++] = dir
*this_delta
;
531 if (have_first_delta_accumulator
) {
532 average_slave_delta
= 0;
533 for (int i
= 0; i
< delta_accumulator_size
; ++i
) {
534 average_slave_delta
+= delta_accumulator
[i
];
536 average_slave_delta
/= delta_accumulator_size
;
537 if (average_slave_delta
< 0) {
539 average_slave_delta
= -average_slave_delta
;
543 // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
547 if (slave_speed
!= 0.0f
) {
549 /* slave is running */
551 switch (slave_state
) {
553 if (_slave
->requires_seekahead()) {
554 slave_wait_end
= slave_transport_frame
+ _current_frame_rate
;
555 locate (slave_wait_end
, false, false);
556 slave_state
= Waiting
;
561 slave_state
= Running
;
563 Location
* al
= _locations
.auto_loop_location();
565 if (al
&& play_loop
&& (slave_transport_frame
< al
->start() || slave_transport_frame
> al
->end())) {
566 // master has moved outside the loop: stop looping
567 cerr
<< "Stop looping - master out of range\n";
568 request_play_loop(false);
571 if (slave_transport_frame
!= _transport_frame
) {
572 locate (slave_transport_frame
, false, false);
585 if (slave_state
== Waiting
) {
587 // cerr << "waiting at " << slave_transport_frame << endl;
589 if (slave_transport_frame
>= slave_wait_end
) {
590 // cerr << "\tstart at " << _transport_frame << endl;
592 slave_state
= Running
;
595 nframes_t frame_delta
= slave_transport_frame
- _transport_frame
;
597 boost::shared_ptr
<DiskstreamList
> dsl
= diskstreams
.reader();
599 for (DiskstreamList::iterator i
= dsl
->begin(); i
!= dsl
->end(); ++i
) {
600 if (!(*i
)->can_internal_playback_seek (frame_delta
)) {
607 for (DiskstreamList::iterator i
= dsl
->begin(); i
!= dsl
->end(); ++i
) {
608 (*i
)->internal_playback_seek (frame_delta
);
610 _transport_frame
+= frame_delta
;
613 // cerr << "cannot micro-seek\n";
617 memset (delta_accumulator
, 0, sizeof (nframes_t
) * delta_accumulator_size
);
618 average_slave_delta
= 0;
623 if (slave_state
== Running
&& _transport_speed
== 0.0f
) {
625 // cerr << "slave starts transport\n";
632 /* slave has stopped */
634 if (_transport_speed
!= 0.0f
) {
636 // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
637 // << " tf = " << _transport_frame
640 if (Config
->get_slave_source() == JACK
) {
641 last_stop_frame
= _transport_frame
;
647 if (slave_transport_frame
!= _transport_frame
) {
648 // cerr << "slave stopped, move to " << slave_transport_frame << endl;
649 force_locate (slave_transport_frame
, false);
652 slave_state
= Stopped
;
655 if (slave_state
== Running
&& !_slave
->is_always_synced() && !Config
->get_timecode_source_is_synced()) {
658 if (_transport_speed
!= 0.0f
) {
661 note that average_dir is +1 or -1
664 const float adjust_seconds
= 1.0f
;
667 //if (average_slave_delta == 0) {
671 // delta = average_slave_delta;
672 // delta *= average_dir;
675 float adjusted_speed
= slave_speed
+
676 (delta
/ (adjust_seconds
* _current_frame_rate
));
679 cerr
<< "adjust using " << delta
680 << " towards " << adjusted_speed
681 << " ratio = " << adjusted_speed
/ slave_speed
682 << " current = " << _transport_speed
683 << " slave @ " << slave_speed
687 request_transport_speed (adjusted_speed
);
690 if ((nframes_t
) average_slave_delta
> _slave
->resolution()) {
691 // cerr << "not locked\n";
698 if (!starting
&& !non_realtime_work_pending()) {
699 /* speed is set, we're locked, and good to go */
705 if (slave_speed
&& _transport_speed
) {
707 /* something isn't right, but we should move with the master
713 prepare_diskstreams ();
714 silent_process_routes (nframes
);
715 commit_diskstreams (nframes
, need_butler
);
721 int32_t frames_moved
= (int32_t) floor (_transport_speed
* nframes
);
723 if (frames_moved
< 0) {
724 decrement_transport_position (-frames_moved
);
726 increment_transport_position (frames_moved
);
729 nframes_t stop_limit
;
731 if (actively_recording()) {
732 stop_limit
= max_frames
;
734 if (Config
->get_stop_at_session_end()) {
735 stop_limit
= current_end_frame();
737 stop_limit
= max_frames
;
741 maybe_stop (stop_limit
);
745 /* don't move at all */
751 Session::process_without_events (nframes_t nframes
)
753 bool session_needs_butler
= false;
754 nframes_t stop_limit
;
759 if (!process_can_proceed()) {
764 if (!_exporting
&& _slave
) {
765 if (!follow_slave (nframes
)) {
766 cerr
<< "++PwE out 1\n";
771 if (_transport_speed
== 0) {
773 cerr
<< "++PwE out 2\n";
777 if (actively_recording()) {
778 stop_limit
= max_frames
;
780 if (Config
->get_stop_at_session_end()) {
781 stop_limit
= current_end_frame();
783 stop_limit
= max_frames
;
787 if (maybe_stop (stop_limit
)) {
789 cerr
<< "++PwE out 2\n";
793 if (maybe_sync_start (nframes
)) {
794 cerr
<< "++PwE out 3\n";
798 click (_transport_frame
, nframes
);
800 prepare_diskstreams ();
802 frames_moved
= (long) floor (_transport_speed
* nframes
);
804 if (process_routes (nframes
)) {
806 cerr
<< "++PwE out 4\n";
810 commit_diskstreams (nframes
, session_needs_butler
);
812 if (frames_moved
< 0) {
813 decrement_transport_position (-frames_moved
);
815 increment_transport_position (frames_moved
);
818 maybe_stop (stop_limit
);
819 check_declick_out ();
821 if (session_needs_butler
) {
825 if (!_engine
.freewheeling() && session_send_mtc
) {
826 send_midi_time_code_in_another_thread ();
829 cerr
<< "++PwE out 5\n";
834 Session::process_audition (nframes_t nframes
)
837 boost::shared_ptr
<RouteList
> r
= routes
.reader ();
839 for (RouteList::iterator i
= r
->begin(); i
!= r
->end(); ++i
) {
840 if (!(*i
)->hidden()) {
841 (*i
)->silence (nframes
);
845 /* run the auditioner, and if it says we need butler service, ask for it */
847 if (auditioner
->play_audition (nframes
) > 0) {
851 /* handle pending events */
853 while (pending_events
.read (&ev
, 1) == 1) {
857 /* if we are not in the middle of a state change,
858 and there are immediate events queued up,
862 while (!non_realtime_work_pending() && !immediate_events
.empty()) {
863 Event
*ev
= immediate_events
.front ();
864 immediate_events
.pop_front ();
868 if (!auditioner
->active()) {
869 cerr
<< "P1 pf = pwe\n";
870 process_function
= &Session::process_with_events
;
875 Session::maybe_sync_start (nframes_t
& nframes
)
877 nframes_t sync_offset
;
879 if (!waiting_for_sync_offset
) {
883 if (_engine
.get_sync_offset (sync_offset
) && sync_offset
< nframes
) {
885 /* generate silence up to the sync point, then
886 adjust nframes + offset to reflect whatever
890 no_roll (sync_offset
);
891 nframes
-= sync_offset
;
892 Port::increment_port_offset (sync_offset
);
893 waiting_for_sync_offset
= false;
896 return true; // done, nothing left to process
901 /* sync offset point is not within this process()
902 cycle, so just generate silence. and don't bother
903 with any fancy stuff here, just the minimal silence.
908 if (Config
->get_locate_while_waiting_for_sync()) {
909 if (micro_locate (nframes
)) {
910 /* XXX ERROR !!! XXX */
914 return true; // done, nothing left to process