MMC related fixes
[ardour.git] / libs / ardour / session_process.cc
1 /*
2     Copyright (C) 1999-2002 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <cmath>
21 #include <cerrno>
22 #include <algorithm>
23 #include <unistd.h>
24
25 #include "pbd/error.h"
26
27 #include <glibmm/thread.h>
28
29 #include "ardour/ardour.h"
30 #include "ardour/audio_diskstream.h"
31 #include "ardour/audioengine.h"
32 #include "ardour/auditioner.h"
33 #include "ardour/butler.h"
34 #include "ardour/cycle_timer.h"
35 #include "ardour/cycles.h"
36 #include "ardour/session.h"
37 #include "ardour/slave.h"
38 #include "ardour/timestamps.h"
39
40 #include "midi++/manager.h"
41
42 #include "i18n.h"
43
44 using namespace ARDOUR;
45 using namespace PBD;
46 using namespace std;
47
48 /** Called by the audio engine when there is work to be done with JACK.
49  * @param nframes Number of frames to process.
50  */
51 void
52 Session::process (nframes_t nframes)
53 {
54         // This is no more the appropriate place to call cycle
55         // start. cycle_start needs to be called at the Route::roll()
56         // where the signals which we want to mixdown have been calculated.
57         //
58         MIDI::Manager::instance()->cycle_start(nframes);
59
60         _silent = false;
61
62         if (processing_blocked()) {
63                 _silent = true;
64                 return;
65         }
66
67         if (non_realtime_work_pending()) {
68                 if (!_butler->transport_work_requested ()) {
69                         post_transport ();
70                 }
71         }
72
73         (this->*process_function) (nframes);
74
75         // the ticker is for sending time information like MidiClock
76         nframes_t transport_frames = transport_frame();
77         BBT_Time  transport_bbt;
78         bbt_time(transport_frames, transport_bbt);
79         Timecode::Time transport_timecode;
80         timecode_time(transport_frames, transport_timecode);
81         tick (transport_frames, transport_bbt, transport_timecode); /* EMIT SIGNAL */
82
83         SendFeedback (); /* EMIT SIGNAL */
84
85         MIDI::Manager::instance()->cycle_end();
86 }
87
88 void
89 Session::prepare_diskstreams ()
90 {
91         boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
92         for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
93                 (*i)->prepare ();
94         }
95 }
96
97 int
98 Session::fail_roll (nframes_t nframes)
99 {
100         return no_roll (nframes);
101 }
102
103 int
104 Session::no_roll (nframes_t nframes)
105 {
106         nframes_t end_frame = _transport_frame + nframes; // FIXME: varispeed + no_roll ??
107         int ret = 0;
108         bool declick = get_transport_declick_required();
109         boost::shared_ptr<RouteList> r = routes.reader ();
110
111         if (_click_io) {
112                 _click_io->silence (nframes);
113         }
114
115         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
116
117                 if ((*i)->is_hidden()) {
118                         continue;
119                 }
120
121                 (*i)->set_pending_declick (declick);
122
123                 if ((*i)->no_roll (nframes, _transport_frame, end_frame, non_realtime_work_pending(),
124                                    actively_recording(), declick)) {
125                         error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
126                         ret = -1;
127                         break;
128                 }
129         }
130
131         return ret;
132 }
133
134 int
135 Session::process_routes (nframes_t nframes)
136 {
137         bool record_active;
138         int  declick = get_transport_declick_required();
139         bool rec_monitors = get_rec_monitors_input();
140         boost::shared_ptr<RouteList> r = routes.reader ();
141
142         if (transport_sub_state & StopPendingCapture) {
143                 /* force a declick out */
144                 declick = -1;
145         }
146
147         record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
148
149         const nframes_t start_frame = _transport_frame;
150         const nframes_t end_frame = _transport_frame + (nframes_t)floor(nframes * _transport_speed);
151
152         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
153
154                 int ret;
155
156                 if ((*i)->is_hidden()) {
157                         continue;
158                 }
159
160                 (*i)->set_pending_declick (declick);
161
162                 if ((ret = (*i)->roll (nframes, start_frame, end_frame, declick, record_active, rec_monitors)) < 0) {
163
164                         /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
165                            and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
166                            call path, so make sure we release any outstanding locks here before we return failure.
167                         */
168
169                         boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
170                         for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
171                                 (*ids)->recover ();
172                         }
173
174                         stop_transport ();
175                         return -1;
176                 }
177         }
178
179         return 0;
180 }
181
182 int
183 Session::silent_process_routes (nframes_t nframes)
184 {
185         bool record_active = actively_recording();
186         int  declick = get_transport_declick_required();
187         bool rec_monitors = get_rec_monitors_input();
188         boost::shared_ptr<RouteList> r = routes.reader ();
189
190         if (transport_sub_state & StopPendingCapture) {
191                 /* force a declick out */
192                 declick = -1;
193         }
194
195         const nframes_t start_frame = _transport_frame;
196         const nframes_t end_frame = _transport_frame + lrintf(nframes * _transport_speed);
197
198         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
199
200                 int ret;
201
202                 if ((*i)->is_hidden()) {
203                         continue;
204                 }
205
206                 if ((ret = (*i)->silent_roll (nframes, start_frame, end_frame, record_active, rec_monitors)) < 0) {
207
208                         /* we have to do this here. Route::roll() for an AudioTrack will have called AudioDiskstream::process(),
209                            and the DS will expect AudioDiskstream::commit() to be called. but we're aborting from that
210                            call path, so make sure we release any outstanding locks here before we return failure.
211                         */
212
213                         boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
214                         for (DiskstreamList::iterator ids = dsl->begin(); ids != dsl->end(); ++ids) {
215                                 (*ids)->recover ();
216                         }
217
218                         stop_transport ();
219                         return -1;
220                 }
221         }
222
223         return 0;
224 }
225
226 void
227 Session::commit_diskstreams (nframes_t nframes, bool &needs_butler)
228 {
229         int dret;
230         float pworst = 1.0f;
231         float cworst = 1.0f;
232
233         boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
234         for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
235
236                 if ((*i)->hidden()) {
237                         continue;
238                 }
239
240                 /* force all diskstreams not handled by a Route to call do their stuff.
241                    Note: the diskstreams that were handled by a route will just return zero
242                    from this call, because they know they were processed. So in fact, this
243                    also runs commit() for every diskstream.
244                  */
245
246                 if ((dret = (*i)->process (_transport_frame, nframes, actively_recording(), get_rec_monitors_input())) == 0) {
247                         if ((*i)->commit (nframes)) {
248                                 needs_butler = true;
249                         }
250
251                 } else if (dret < 0) {
252                         (*i)->recover();
253                 }
254
255                 pworst = min (pworst, (*i)->playback_buffer_load());
256                 cworst = min (cworst, (*i)->capture_buffer_load());
257         }
258
259         uint32_t pmin = g_atomic_int_get (&_playback_load);
260         uint32_t pminold = g_atomic_int_get (&_playback_load_min);
261         uint32_t cmin = g_atomic_int_get (&_capture_load);
262         uint32_t cminold = g_atomic_int_get (&_capture_load_min);
263
264         g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
265         g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
266         g_atomic_int_set (&_playback_load_min, min (pmin, pminold));
267         g_atomic_int_set (&_capture_load_min, min (cmin, cminold));
268
269         if (actively_recording()) {
270                 set_dirty();
271         }
272 }
273
274 /** Process callback used when the auditioner is not active */
275 void
276 Session::process_with_events (nframes_t nframes)
277 {
278         Event*         ev;
279         nframes_t      this_nframes;
280         nframes_t      end_frame;
281         bool           session_needs_butler = false;
282         nframes_t      stop_limit;
283         long           frames_moved;
284
285         /* make sure the auditioner is silent */
286
287         if (auditioner) {
288                 auditioner->silence (nframes);
289         }
290
291         /* handle any pending events */
292
293         while (pending_events.read (&ev, 1) == 1) {
294                 merge_event (ev);
295         }
296
297         /* if we are not in the middle of a state change,
298            and there are immediate events queued up,
299            process them.
300         */
301
302         while (!non_realtime_work_pending() && !immediate_events.empty()) {
303                 Event *ev = immediate_events.front ();
304                 immediate_events.pop_front ();
305                 process_event (ev);
306         }
307
308         /* Events caused a transport change, send an MTC Full Frame (Timecode) message.
309          * This is sent whether rolling or not, to give slaves an idea of ardour time
310          * on locates (and allow slow slaves to position and prepare for rolling)
311          */
312         if (_send_timecode_update) {
313                 send_full_time_code(nframes);
314                 deliver_mmc (MIDI::MachineControl::cmdLocate, _transport_frame);
315         }
316
317         if (!process_can_proceed()) {
318                 _silent = true;
319                 return;
320         }
321
322         if (events.empty() || next_event == events.end()) {
323                 process_without_events (nframes);
324                 return;
325         }
326
327         if (_transport_speed == 1.0) {
328                 frames_moved = (long) nframes;
329         } else {
330                 interpolation.set_target_speed (fabs(_target_transport_speed));
331                 interpolation.set_speed (fabs(_transport_speed));
332                 frames_moved = (long) interpolation.interpolate (0, nframes, 0, 0);
333         }
334
335         end_frame = _transport_frame + (nframes_t)frames_moved;
336
337         {
338                 Event* this_event;
339                 Events::iterator the_next_one;
340
341                 if (!process_can_proceed()) {
342                         _silent = true;
343                         return;
344                 }
345
346                 if (!_exporting && _slave) {
347                         if (!follow_slave (nframes)) {
348                                 return;
349                         }
350                 }
351
352                 if (_transport_speed == 0) {
353                         no_roll (nframes);
354                         return;
355                 }
356
357                 if (!_exporting) {
358                         send_midi_time_code_for_cycle (nframes);
359                 }
360
361                 if (actively_recording()) {
362                         stop_limit = max_frames;
363                 } else {
364
365                         if (Config->get_stop_at_session_end()) {
366                                 stop_limit = current_end_frame();
367                         } else {
368                                 stop_limit = max_frames;
369                         }
370                 }
371
372                 if (maybe_stop (stop_limit)) {
373                         no_roll (nframes);
374                         return;
375                 }
376
377                 this_event = *next_event;
378                 the_next_one = next_event;
379                 ++the_next_one;
380
381                 /* yes folks, here it is, the actual loop where we really truly
382                    process some audio
383                 */
384
385                 while (nframes) {
386
387                         this_nframes = nframes; /* real (jack) time relative */
388                         frames_moved = (long) floor (_transport_speed * nframes); /* transport relative */
389
390                         /* running an event, position transport precisely to its time */
391                         if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
392                                 /* this isn't quite right for reverse play */
393                                 frames_moved = (long) (this_event->action_frame - _transport_frame);
394                                 this_nframes = (nframes_t) abs( floor(frames_moved / _transport_speed) );
395                         }
396
397                         if (this_nframes) {
398
399                                 click (_transport_frame, this_nframes);
400
401                                 /* now process frames between now and the first event in this block */
402                                 prepare_diskstreams ();
403
404                                 if (process_routes (this_nframes)) {
405                                         fail_roll (nframes);
406                                         return;
407                                 }
408
409                                 commit_diskstreams (this_nframes, session_needs_butler);
410
411                                 nframes -= this_nframes;
412
413                                 if (frames_moved < 0) {
414                                         decrement_transport_position (-frames_moved);
415                                 } else {
416                                         increment_transport_position (frames_moved);
417                                 }
418
419                                 maybe_stop (stop_limit);
420                                 check_declick_out ();
421                         }
422
423                         _engine.split_cycle (this_nframes);
424
425                         /* now handle this event and all others scheduled for the same time */
426
427                         while (this_event && this_event->action_frame == _transport_frame) {
428                                 process_event (this_event);
429
430                                 if (the_next_one == events.end()) {
431                                         this_event = 0;
432                                 } else {
433                                         this_event = *the_next_one;
434                                         ++the_next_one;
435                                 }
436                         }
437
438                         /* if an event left our state changing, do the right thing */
439
440                         if (nframes && non_realtime_work_pending()) {
441                                 no_roll (nframes);
442                                 break;
443                         }
444
445                         /* this is necessary to handle the case of seamless looping */
446                         end_frame = _transport_frame + (nframes_t) floor (nframes * _transport_speed);
447
448                 }
449
450                 set_next_event ();
451
452         } /* implicit release of route lock */
453
454         if (session_needs_butler) {
455                 _butler->summon ();
456         }
457 }
458
459 void
460 Session::reset_slave_state ()
461 {
462         average_slave_delta = 1800;
463         delta_accumulator_cnt = 0;
464         have_first_delta_accumulator = false;
465         slave_state = Stopped;
466 }
467
468 bool
469 Session::transport_locked () const
470 {
471         Slave* sl = _slave;
472
473         if (!locate_pending() && (!config.get_external_sync() || (sl && sl->ok() && sl->locked()))) {
474                 return true;
475         }
476
477         return false;
478 }
479
480 bool
481 Session::follow_slave (nframes_t nframes)
482 {
483         double slave_speed;
484         nframes64_t slave_transport_frame;
485         nframes_t this_delta;
486         int dir;
487         bool starting;
488
489         if (!_slave->ok()) {
490                 stop_transport ();
491                 config.set_external_sync (false);
492                 goto noroll;
493         }
494
495         _slave->speed_and_position (slave_speed, slave_transport_frame);
496
497         if (!_slave->locked()) {
498                 goto noroll;
499         }
500
501         if (slave_transport_frame > _transport_frame) {
502                 this_delta = slave_transport_frame - _transport_frame;
503                 dir = 1;
504         } else {
505                 this_delta = _transport_frame - slave_transport_frame;
506                 dir = -1;
507         }
508
509         if ((starting = _slave->starting())) {
510                 slave_speed = 0.0f;
511         }
512
513         if (_slave->is_always_synced() || config.get_timecode_source_is_synced()) {
514
515                 /* if the TC source is synced, then we assume that its
516                    speed is binary: 0.0 or 1.0
517                 */
518
519                 if (slave_speed != 0.0f) {
520                         slave_speed = 1.0f;
521                 }
522
523         } else {
524
525                 /* TC source is able to drift relative to us (slave)
526                    so we need to keep track of the drift and adjust
527                    our speed to remain locked.
528                 */
529
530                 calculate_moving_average_of_slave_delta(dir, this_delta);
531         }
532
533         track_slave_state(slave_speed, slave_transport_frame, this_delta, starting);
534
535         if (slave_state == Running && !_slave->is_always_synced() && !config.get_timecode_source_is_synced()) {
536
537                 if (_transport_speed != 0.0f) {
538
539                         /*
540                            note that average_dir is +1 or -1
541                         */
542
543                         float delta;
544
545                         #ifdef USE_MOVING_AVERAGE_OF_SLAVE
546                                 if (average_slave_delta == 0) {
547                                         delta = this_delta;
548                                         delta *= dir;
549                                 } else {
550                                         delta = average_slave_delta;
551                                         delta *= average_dir;
552                                 }
553                         #else
554                                 delta = this_delta;
555                                 delta *= dir;
556                         #endif
557
558                         float adjusted_speed = slave_speed + (delta /  float(_current_frame_rate));
559
560                         if (_slave->give_slave_full_control_over_transport_speed()) {
561                                 request_transport_speed(slave_speed);
562                         } else {
563                                 request_transport_speed(adjusted_speed);
564                                 #ifdef DEBUG_SLAVES
565                                 cerr << "adjust using " << delta
566                                          << " towards " << adjusted_speed
567                                          << " ratio = " << adjusted_speed / slave_speed
568                                          << " current = " << _transport_speed
569                                          << " slave @ " << slave_speed
570                                          << endl;
571                                 #endif
572                         }
573
574                         if (abs(average_slave_delta) > (long) _slave->resolution()) {
575                                 cerr << "average slave delta greater than slave resolution, going to silent motion\n";
576                                 goto silent_motion;
577                         }
578                 }
579         }
580
581         #ifdef DEBUG_SLAVES
582         if (slave_speed != 0.0)
583         cerr << "delta = " << (int) (dir * this_delta)
584                  << " speed = " << slave_speed
585                  << " ts = " << _transport_speed
586                  << " M@ "<< slave_transport_frame << " S@ " << _transport_frame
587                  << " avgdelta = " << average_slave_delta
588                  << endl;
589         #endif
590
591         if (!starting && !non_realtime_work_pending()) {
592                 /* speed is set, we're locked, and good to go */
593                 return true;
594         }
595
596   silent_motion:
597         #ifdef DEBUG_SLAVES
598         cerr << "reached silent_motion:" <<endl;
599         #endif
600
601         follow_slave_silently (nframes, slave_speed);
602
603   noroll:
604         /* don't move at all */
605         #ifdef DEBUG_SLAVES
606         cerr << "reached no_roll:" <<endl;
607         #endif
608         no_roll (nframes);
609         return false;
610 }
611
612 void
613 Session::calculate_moving_average_of_slave_delta(int dir, nframes_t this_delta)
614 {
615         if (delta_accumulator_cnt >= delta_accumulator_size) {
616                 have_first_delta_accumulator = true;
617                 delta_accumulator_cnt = 0;
618         }
619
620         if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
621                 delta_accumulator[delta_accumulator_cnt++] = long(dir) * long(this_delta);
622         }
623
624         if (have_first_delta_accumulator) {
625                 average_slave_delta = 0L;
626                 for (int i = 0; i < delta_accumulator_size; ++i) {
627                         average_slave_delta += delta_accumulator[i];
628                 }
629                 average_slave_delta /= long(delta_accumulator_size);
630                 if (average_slave_delta < 0L) {
631                         average_dir = -1;
632                         average_slave_delta = abs(average_slave_delta);
633                 } else {
634                         average_dir = 1;
635                 }
636         }
637 }
638
639 void
640 Session::track_slave_state(
641                 float slave_speed,
642                 nframes_t slave_transport_frame,
643                 nframes_t this_delta,
644                 bool starting)
645 {
646         if (slave_speed != 0.0f) {
647
648                 /* slave is running */
649
650                 switch (slave_state) {
651                 case Stopped:
652                         if (_slave->requires_seekahead()) {
653                                 slave_wait_end = slave_transport_frame + _current_frame_rate;
654                                 locate (slave_wait_end, false, false);
655                                 slave_state = Waiting;
656                                 starting = true;
657
658                         } else {
659
660                                 slave_state = Running;
661
662                                 Location* al = _locations.auto_loop_location();
663
664                                 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
665                                         // cancel looping
666                                         request_play_loop(false);
667                                 }
668
669                                 if (slave_transport_frame != _transport_frame) {
670                                         locate (slave_transport_frame, false, false);
671                                 }
672                         }
673                         break;
674
675                 case Waiting:
676                         break;
677
678                 default:
679                         break;
680
681                 }
682
683                 if (slave_state == Waiting) {
684
685                 #ifdef DEBUG_SLAVES
686                         cerr << "waiting at " << slave_transport_frame << endl;
687                 #endif
688                         if (slave_transport_frame >= slave_wait_end) {
689 #ifdef DEBUG_SLAVES
690                                 cerr << "\tstart at " << _transport_frame << endl;
691 #endif
692                                 slave_state = Running;
693
694                                 bool ok = true;
695                                 nframes_t frame_delta = slave_transport_frame - _transport_frame;
696
697                                 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
698
699                                 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
700                                         if (!(*i)->can_internal_playback_seek (frame_delta)) {
701                                                 ok = false;
702                                                 break;
703                                         }
704                                 }
705
706                                 if (ok) {
707                                         for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
708                                                 (*i)->internal_playback_seek (frame_delta);
709                                         }
710                                         _transport_frame += frame_delta;
711
712                                 } else {
713                                         cerr << "cannot micro-seek\n";
714                                         /* XXX what? */
715                                 }
716
717                                 memset (delta_accumulator, 0, sizeof (long) * delta_accumulator_size);
718                                 average_slave_delta = 0L;
719                                 this_delta = 0;
720                         }
721                 }
722
723                 if (slave_state == Running && _transport_speed == 0.0f) {
724
725         #ifdef DEBUG_SLAVES
726                         cerr << "slave starts transport\n";
727         #endif
728                         start_transport ();
729                 }
730
731         } else { // slave_speed is 0
732
733                 /* slave has stopped */
734
735                 if (_transport_speed != 0.0f) {
736
737          #ifdef DEBUG_SLAVES
738                         cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame
739                              << " tf = " << _transport_frame << endl;
740          #endif
741
742                         stop_transport();
743                 }
744
745                 if (slave_transport_frame != _transport_frame) {
746         #ifdef DEBUG_SLAVES
747                         cerr << "slave stopped, move to " << slave_transport_frame << endl;
748         #endif
749                         force_locate (slave_transport_frame, false);
750                 }
751
752                 slave_state = Stopped;
753         }
754 }
755
756 void
757 Session::follow_slave_silently (nframes_t nframes, float slave_speed)
758 {
759         if (slave_speed && _transport_speed) {
760
761                 /* something isn't right, but we should move with the master
762                    for now.
763                 */
764
765                 bool need_butler;
766
767                 prepare_diskstreams ();
768                 silent_process_routes (nframes);
769                 commit_diskstreams (nframes, need_butler);
770
771                 if (need_butler) {
772                         _butler->summon ();
773                 }
774
775                 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
776
777                 if (frames_moved < 0) {
778                         decrement_transport_position (-frames_moved);
779                 } else {
780                         increment_transport_position (frames_moved);
781                 }
782
783                 nframes_t stop_limit;
784
785                 if (actively_recording()) {
786                         stop_limit = max_frames;
787                 } else {
788                         if (Config->get_stop_at_session_end()) {
789                                 stop_limit = current_end_frame();
790                         } else {
791                                 stop_limit = max_frames;
792                         }
793                 }
794
795                 maybe_stop (stop_limit);
796         }
797 }
798
799 void
800 Session::process_without_events (nframes_t nframes)
801 {
802         bool session_needs_butler = false;
803         nframes_t stop_limit;
804         long frames_moved;
805
806         if (!process_can_proceed()) {
807                 _silent = true;
808                 return;
809         }
810
811         if (!_exporting && _slave) {
812                 if (!follow_slave (nframes)) {
813                         return;
814                 }
815         }
816
817         if (_transport_speed == 0) {
818                 fail_roll (nframes);
819                 return;
820         }
821
822         if (!_exporting) {
823                 send_midi_time_code_for_cycle (nframes);
824         }
825
826         if (actively_recording()) {
827                 stop_limit = max_frames;
828         } else {
829                 if (Config->get_stop_at_session_end()) {
830                         stop_limit = current_end_frame();
831                 } else {
832                         stop_limit = max_frames;
833                 }
834         }
835
836         if (maybe_stop (stop_limit)) {
837                 fail_roll (nframes);
838                 return;
839         }
840
841         if (maybe_sync_start (nframes)) {
842                 return;
843         }
844
845         click (_transport_frame, nframes);
846
847         prepare_diskstreams ();
848
849         if (_transport_speed == 1.0) {
850                 frames_moved = (long) nframes;
851         } else {
852                 interpolation.set_target_speed (fabs(_target_transport_speed));
853                 interpolation.set_speed (fabs(_transport_speed));
854                 frames_moved = (long) interpolation.interpolate (0, nframes, 0, 0);
855         }
856
857         if (process_routes (nframes)) {
858                 fail_roll (nframes);
859                 return;
860         }
861
862         commit_diskstreams (nframes, session_needs_butler);
863
864         if (frames_moved < 0) {
865                 decrement_transport_position (-frames_moved);
866         } else {
867                 increment_transport_position (frames_moved);
868         }
869
870         maybe_stop (stop_limit);
871         check_declick_out ();
872
873         if (session_needs_butler) {
874                 _butler->summon ();
875         }
876 }
877
878 /** Process callback used when the auditioner is active.
879  * @param nframes number of frames to process.
880  */
881 void
882 Session::process_audition (nframes_t nframes)
883 {
884         Event* ev;
885         boost::shared_ptr<RouteList> r = routes.reader ();
886
887         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
888                 if (!(*i)->is_hidden()) {
889                         (*i)->silence (nframes);
890                 }
891         }
892
893         /* run the auditioner, and if it says we need butler service, ask for it */
894
895         if (auditioner->play_audition (nframes) > 0) {
896                 _butler->summon ();
897         }
898
899         /* handle pending events */
900
901         while (pending_events.read (&ev, 1) == 1) {
902                 merge_event (ev);
903         }
904
905         /* if we are not in the middle of a state change,
906            and there are immediate events queued up,
907            process them.
908         */
909
910         while (!non_realtime_work_pending() && !immediate_events.empty()) {
911                 Event *ev = immediate_events.front ();
912                 immediate_events.pop_front ();
913                 process_event (ev);
914         }
915
916         if (!auditioner->active()) {
917                 /* auditioner no longer active, so go back to the normal process callback */
918                 process_function = &Session::process_with_events;
919         }
920 }
921
922 bool
923 Session::maybe_sync_start (nframes_t& nframes)
924 {
925         nframes_t sync_offset;
926
927         if (!waiting_for_sync_offset) {
928                 return false;
929         }
930
931         if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
932
933                 /* generate silence up to the sync point, then
934                    adjust nframes + offset to reflect whatever
935                    is left to do.
936                 */
937
938                 no_roll (sync_offset);
939                 nframes -= sync_offset;
940                 Port::increment_port_offset (sync_offset);
941                 waiting_for_sync_offset = false;
942
943                 if (nframes == 0) {
944                         return true; // done, nothing left to process
945                 }
946
947         } else {
948
949                 /* sync offset point is not within this process()
950                    cycle, so just generate silence. and don't bother
951                    with any fancy stuff here, just the minimal silence.
952                 */
953
954                 _silent = true;
955
956                 if (Config->get_locate_while_waiting_for_sync()) {
957                         if (micro_locate (nframes)) {
958                                 /* XXX ERROR !!! XXX */
959                         }
960                 }
961
962                 return true; // done, nothing left to process
963         }
964
965         return false;
966 }
967