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