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