Committed filthy mess of a working copy solely for moving between machines.
[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     $Id$
19 */
20
21 #include <cmath>
22 #include <cerrno>
23 #include <algorithm>
24 #include <unistd.h>
25
26 #include <ardour/timestamps.h>
27
28 #include <pbd/error.h>
29 #include <pbd/atomic.h>
30 #include <pbd/lockmonitor.h>
31
32 #include <ardour/ardour.h>
33 #include <ardour/session.h>
34 #include <ardour/diskstream.h>
35 #include <ardour/audioengine.h>
36 #include <ardour/slave.h>
37 #include <ardour/auditioner.h>
38 #include <ardour/cycles.h>
39 #include <ardour/cycle_timer.h>
40
41 #include <midi++/manager.h>
42
43 #include "i18n.h"
44
45 using namespace ARDOUR;
46 //using namespace sigc;
47 using namespace std;
48
49 void
50 Session::process (jack_nframes_t nframes)
51 {
52         cerr << "CYCLE START " << _transport_frame << "-------------------" << endl;
53         
54         MIDI::Manager::instance()->cycle_start(nframes);
55
56         if (synced_to_jack() && waiting_to_start) {
57                 if ( _engine.transport_state() == AudioEngine::TransportRolling) {
58                         actually_start_transport ();
59                 }
60         }
61
62         if (non_realtime_work_pending()) {
63                 if (atomic_read (&butler_should_do_transport_work) == 0) {
64                         post_transport ();
65                 } 
66         } 
67         
68         (this->*process_function) (nframes);
69         
70         MIDI::Manager::instance()->cycle_end();
71         
72         cerr << "CYCLE END " << _transport_frame << "-----------------------" << endl;
73 }
74
75 void
76 Session::prepare_diskstreams ()
77 {
78         for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
79                 (*i)->prepare ();
80         }
81 }
82
83 int
84 Session::no_roll (jack_nframes_t nframes, jack_nframes_t offset)
85 {
86         jack_nframes_t end_frame = _transport_frame + nframes;
87         int ret = 0;
88         bool declick = get_transport_declick_required();
89
90         cerr << "[DR] no_roll\n";
91
92         if (_click_io) {
93                 _click_io->silence (nframes, offset);
94         }
95
96         /* XXX we're supposed to have the route_lock while doing this.
97            this is really bad ...
98         */
99
100         if (atomic_read (&processing_prohibited)) {
101                 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
102                         (*i)->silence (nframes, offset);
103                 }
104                 return 0;
105         }
106
107         for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
108                 
109                 if ((*i)->hidden()) {
110                         continue;
111                 }
112                 
113                 (*i)->set_pending_declick (declick);
114                 
115                 if ((*i)->no_roll (nframes, _transport_frame, end_frame, offset, non_realtime_work_pending(), 
116                                    actively_recording(), declick)) {
117                         error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
118                         ret = -1;
119                         break;
120                 }
121         }
122
123         return ret;
124 }
125
126 int
127 Session::process_routes (jack_nframes_t nframes, jack_nframes_t offset)
128 {
129         bool record_active;
130         int  declick = get_transport_declick_required();
131         bool rec_monitors = get_rec_monitors_input();
132
133         if (transport_sub_state & StopPendingCapture) {
134                 /* force a declick out */
135                 declick = -1;
136         }
137
138         record_active = actively_recording(); // || (get_record_enabled() && get_punch_in());
139
140         for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
141
142                 int ret;
143
144                 if ((*i)->hidden()) {
145                         continue;
146                 }
147
148                 (*i)->set_pending_declick (declick);
149
150                 if ((ret = (*i)->roll (nframes, _transport_frame, _transport_frame + nframes, offset, declick, record_active, rec_monitors)) < 0) {
151
152                         /* we have to do this here. Route::roll() for an AudioTrack will have called DiskStream::process(),
153                            and the DS will expect DiskStream::commit() to be called. but we're aborting from that
154                            call path, so make sure we release any outstanding locks here before we return failure.
155                         */
156
157                         for (DiskStreamList::iterator ids = diskstreams.begin(); ids != diskstreams.end(); ++ids) {
158                                 (*ids)->recover ();
159                         }
160
161                         stop_transport ();
162                         return -1;
163                 } 
164         }
165
166         return 0;
167 }
168
169 int
170 Session::silent_process_routes (jack_nframes_t nframes, jack_nframes_t offset)
171 {
172         bool record_active = actively_recording();
173         int  declick = get_transport_declick_required();
174         bool rec_monitors = get_rec_monitors_input();
175
176         if (transport_sub_state & StopPendingCapture) {
177                 /* force a declick out */
178                 declick = -1;
179         }
180
181         for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
182
183                 int ret;
184
185                 if ((*i)->hidden()) {
186                         continue;
187                 }
188
189                 if ((ret = (*i)->silent_roll (nframes, _transport_frame, _transport_frame + nframes, offset, record_active, rec_monitors)) < 0) {
190                         
191                         /* we have to do this here. Route::roll() for an AudioTrack will have called DiskStream::process(),
192                            and the DS will expect DiskStream::commit() to be called. but we're aborting from that
193                            call path, so make sure we release any outstanding locks here before we return failure.
194                         */
195
196                         for (DiskStreamList::iterator ids = diskstreams.begin(); ids != diskstreams.end(); ++ids) {
197                                 (*ids)->recover ();
198                         }
199
200                         stop_transport ();
201                         return -1;
202                 } 
203         }
204
205         return 0;
206 }
207
208 void
209 Session::commit_diskstreams (jack_nframes_t nframes, bool &needs_butler)
210 {
211         int dret;
212         float pworst = 1.0f;
213         float cworst = 1.0f;
214
215         for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
216
217                 if ((*i)->hidden()) {
218                         continue;
219                 }
220                 
221                 /* force all diskstreams not handled by a Route to call do their stuff.
222                  */
223
224                 if ((dret = (*i)->process (_transport_frame, nframes, 0, actively_recording(), get_rec_monitors_input())) == 0) {
225                         if ((*i)->commit (nframes)) {
226                                 needs_butler = true;
227                         }
228
229                 } else if (dret < 0) {
230                         (*i)->recover();
231                 }
232                 
233                 pworst = min (pworst, (*i)->playback_buffer_load());
234                 cworst = min (cworst, (*i)->capture_buffer_load());
235         }
236
237         uint32_t pmin = atomic_read (&_playback_load);
238         uint32_t pminold = atomic_read (&_playback_load_min);
239         uint32_t cmin = atomic_read (&_capture_load);
240         uint32_t cminold = atomic_read (&_capture_load_min);
241
242         atomic_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
243         atomic_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
244         atomic_set (&_playback_load_min, min (pmin, pminold));
245         atomic_set (&_capture_load_min, min (cmin, cminold));
246
247         if (actively_recording()) {
248                 set_dirty();
249         }
250 }
251
252
253 void
254 Session::process_with_events (jack_nframes_t nframes)
255 {
256         Event*         ev;
257         jack_nframes_t this_nframes;
258         jack_nframes_t end_frame;
259         jack_nframes_t offset;
260         jack_nframes_t stop_limit;
261         long           frames_moved;
262         bool           session_needs_butler = false;
263
264         cerr << "[DR] with events" << endl;
265
266         if (auditioner) {
267                 auditioner->silence (nframes, 0);
268         }
269
270         while (pending_events.read (&ev, 1) == 1) {
271                 merge_event (ev);
272         }
273
274         /* if we are not in the middle of a state change,
275            and there are immediate events queued up,
276            process them. 
277         */
278
279         while (!non_realtime_work_pending() && !immediate_events.empty()) {
280                 Event *ev = immediate_events.front ();
281                 immediate_events.pop_front ();
282                 process_event (ev);
283         }
284
285         /* Events caused a transport change and we need to send MTC
286          * [DR] FIXME: best place for this? */
287         if (_send_smpte_update) {
288                 cerr << "[DR] TIME CHANGE\n" << endl;
289                 send_full_time_code(nframes);
290         }
291
292         if (!process_can_proceed()) {
293                 no_roll (nframes, 0);
294                 return;
295         }
296                 
297         if (events.empty() || next_event == events.end()) {
298                 process_without_events (nframes);
299                 return;
300         }
301
302         end_frame = _transport_frame + nframes;
303
304         {
305                 TentativeRWLockMonitor rm (route_lock, false, __LINE__, __FILE__);
306                 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
307         
308                 Event* this_event;
309                 Events::iterator the_next_one;
310
311                 if (!rm.locked() || !dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
312                         no_roll (nframes, 0);
313                         return;
314                 }
315                 
316                 if (!_exporting && _slave) {
317                         if (!follow_slave (nframes, 0)) {
318                                 return;
319                         }
320                 } 
321
322                 if (_transport_speed == 0) {
323                         no_roll (nframes, 0);
324                         return;
325                 }
326
327                 if (actively_recording()) {
328                         stop_limit = max_frames;
329                 } else {
330
331                         if (Config->get_stop_at_session_end()) {
332                                 stop_limit = current_end_frame();
333                         } else {
334                                 stop_limit = max_frames;
335                         }
336                 }
337
338                 if (maybe_stop (stop_limit)) {
339                         no_roll (nframes, 0);
340                         return;
341                 } 
342
343                 this_event = *next_event;
344                 the_next_one = next_event;
345                 ++the_next_one;
346
347                 offset = 0;
348
349                 while (nframes) {
350
351                         if (this_event == 0 || this_event->action_frame > end_frame || this_event->action_frame < _transport_frame) {
352
353                                 this_nframes = nframes;
354                                 
355                         } else {
356                                 
357                                 /* compute nframes to next event */
358
359                                 if (this_event->action_frame < end_frame) {
360                                         this_nframes = nframes - (end_frame - this_event->action_frame);
361                                 } else {
362                                         this_nframes = nframes;
363                                 }
364                         }
365
366                         if (this_nframes) {
367                                 
368                                 click (_transport_frame, nframes, offset);
369                                 
370                                 /* now process frames between now and the first event in this block */
371                                 prepare_diskstreams ();
372
373                                 if (process_routes (this_nframes, offset)) {
374                                         no_roll (nframes, 0);
375                                         return;
376                                 }
377                                 
378                                 commit_diskstreams (this_nframes, session_needs_butler);
379
380                                 nframes -= this_nframes;
381                                 offset += this_nframes;
382                                 
383                                 frames_moved = (jack_nframes_t) floor (_transport_speed * this_nframes);
384                         
385                                 if (frames_moved < 0) {
386                                         decrement_transport_position (-frames_moved);
387                                 } else {
388                                         increment_transport_position (frames_moved);
389                                 }
390
391                                 maybe_stop (stop_limit);
392                                 check_declick_out ();
393                         }
394
395                         /* now handle this event and all others scheduled for the same time */
396                         
397                         while (this_event && this_event->action_frame == _transport_frame) {
398                                 process_event (this_event);
399
400                                 if (the_next_one == events.end()) {
401                                         this_event = 0;
402                                 } else {
403                                         this_event = *the_next_one;
404                                         ++the_next_one;
405                                 }
406                         } 
407
408                         /* if an event left our state changing, do the right thing */
409
410                         if (non_realtime_work_pending()) {
411                                 no_roll (nframes, offset);
412                                 break;
413                         }
414
415                         /* this is necessary to handle the case of seamless looping */
416                         /* not sure if it will work in conjuction with varispeed */
417                         end_frame = _transport_frame + nframes;
418                         
419                 }
420
421                 set_next_event ();
422
423         } /* implicit release of route lock */
424
425         if (session_needs_butler)
426                 summon_butler ();
427         
428         send_midi_time_code_for_cycle(nframes);
429 }
430
431 void
432 Session::reset_slave_state ()
433 {
434         average_slave_delta = 1800;
435         delta_accumulator_cnt = 0;
436         have_first_delta_accumulator = false;
437         slave_state = Stopped;
438 }
439
440 bool
441 Session::transport_locked () const
442 {
443         Slave* sl = _slave;
444
445         if (!locate_pending() && ((_slave_type == None) || (sl && sl->ok() && sl->locked()))) {
446                 return true;
447         }
448
449         return false;
450 }
451
452 bool
453 Session::follow_slave (jack_nframes_t nframes, jack_nframes_t offset)
454 {
455         float slave_speed;
456         jack_nframes_t slave_transport_frame;
457         jack_nframes_t this_delta;
458         int dir;
459         bool starting;
460
461         if (!_slave->ok()) {
462                 stop_transport ();
463                 set_slave_source (None, 0);
464                 goto noroll;
465         }
466         
467         _slave->speed_and_position (slave_speed, slave_transport_frame);
468
469         if (!_slave->locked()) {
470                 goto noroll;
471         }
472
473         if (slave_transport_frame > _transport_frame) {
474                 this_delta = slave_transport_frame - _transport_frame;
475                 dir = 1;
476         } else {
477                 this_delta = _transport_frame - slave_transport_frame;
478                 dir = -1;
479         }
480
481         if ((starting = _slave->starting())) {
482                 slave_speed = 0.0f;
483         }
484
485 #if 0
486         cerr << "delta = " << (int) (dir * this_delta)
487              << " speed = " << slave_speed 
488              << " ts = " << _transport_speed 
489              << " M@"<< slave_transport_frame << " S@" << _transport_frame 
490              << " avgdelta = " << average_slave_delta 
491              << endl;
492 #endif  
493
494         if (Config->get_timecode_source_is_synced()) {
495
496                 /* if the TC source is synced, then we assume that its 
497                    speed is binary: 0.0 or 1.0
498                 */
499
500                 if (slave_speed != 0.0f) {
501                         slave_speed = 1.0f;
502                 } 
503
504         } else {
505
506                 /* TC source is able to drift relative to us (slave)
507                    so we need to keep track of the drift and adjust
508                    our speed to remain locked.
509                 */
510
511                 if (delta_accumulator_cnt >= delta_accumulator_size) {
512                         have_first_delta_accumulator = true;
513                         delta_accumulator_cnt = 0;
514                 }
515
516                 if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
517                         delta_accumulator[delta_accumulator_cnt++] = dir*this_delta;
518                 }
519                 
520                 if (have_first_delta_accumulator) {
521                         average_slave_delta = 0;
522                         for (int i = 0; i < delta_accumulator_size; ++i) {
523                                 average_slave_delta += delta_accumulator[i];
524                         }
525                         average_slave_delta /= delta_accumulator_size;
526                         if (average_slave_delta < 0) {
527                                 average_dir = -1;
528                                 average_slave_delta = -average_slave_delta;
529                         } else {
530                                 average_dir = 1;
531                         }
532                         // cerr << "avgdelta = " << average_slave_delta*average_dir << endl;
533                 }
534         }
535
536         if (slave_speed != 0.0f) {
537
538                 /* slave is running */
539
540                 switch (slave_state) {
541                 case Stopped:
542                         if (_slave->requires_seekahead()) {
543                                 slave_wait_end = slave_transport_frame + _current_frame_rate;
544                                 locate (slave_wait_end, false, false);
545                                 slave_state = Waiting;
546                                 starting = true;
547
548                         } else {
549
550                                 slave_state = Running;
551
552                                 Location* al = _locations.auto_loop_location();
553
554                                 if (al && auto_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
555                                         // cancel looping
556                                         request_auto_loop(false);
557                                 }
558
559                                 if (slave_transport_frame != _transport_frame) {
560                                         locate (slave_transport_frame, false, false);
561                                 }
562                         }
563                         break;
564
565                 case Waiting:
566                         break;
567
568                 default:
569                         break;
570
571                 }
572
573                 if (slave_state == Waiting) {
574
575                         // cerr << "waiting at " << slave_transport_frame << endl;
576                         TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
577                                 
578                         if (dsm.locked() && slave_transport_frame >= slave_wait_end) {
579                                 // cerr << "\tstart at " << _transport_frame << endl;
580
581                                 slave_state = Running;
582
583                                 bool ok = true;
584                                 jack_nframes_t frame_delta = slave_transport_frame - _transport_frame;
585                                 
586                                 for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
587                                         if (!(*i)->can_internal_playback_seek (frame_delta)) {
588                                                 ok = false;
589                                                 break;
590                                         }
591                                 }
592
593                                 if (ok) {
594                                         for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
595                                                 (*i)->internal_playback_seek (frame_delta);
596                                         }
597                                         _transport_frame += frame_delta;
598                                        
599                                 } else {
600                                         // cerr << "cannot micro-seek\n";
601                                         /* XXX what? */
602                                 }
603
604                                 memset (delta_accumulator, 0, sizeof (jack_nframes_t) * delta_accumulator_size);
605                                 average_slave_delta = 0;
606                                 this_delta = 0;
607                         }
608                 }
609                 
610                 if (slave_state == Running && _transport_speed == 0.0f) {
611                         
612                         // cerr << "slave starts transport\n";
613                         
614                         start_transport ();
615                 } 
616
617         } else {
618
619                 /* slave has stopped */
620
621                 if (_transport_speed != 0.0f) {
622
623                         // cerr << "slave stops transport: " << slave_speed << " frame: " << slave_transport_frame 
624                         // << " tf = " << _transport_frame
625                         // << endl;
626                         
627                         if (_slave_type == JACK) {
628                                 last_stop_frame = _transport_frame;
629                         }
630
631                         stop_transport();
632                 }
633
634                 if (slave_transport_frame != _transport_frame) {
635                         // cerr << "slave stopped, move to " << slave_transport_frame << endl;
636                         force_locate (slave_transport_frame, false);
637                 }
638
639                 slave_state = Stopped;
640         }
641
642         if (slave_state == Running && !Config->get_timecode_source_is_synced()) {
643
644
645                 if (_transport_speed != 0.0f) {
646                         
647                         /* 
648                            note that average_dir is +1 or -1 
649                         */
650                         
651                         const float adjust_seconds = 1.0f;
652                         float delta;
653
654                         //if (average_slave_delta == 0) {
655                                 delta = this_delta;
656                                 delta *= dir;
657 //                      } else {
658 //                              delta = average_slave_delta;
659 //                              delta *= average_dir;
660 //                      }
661
662                         float adjusted_speed = slave_speed +
663                                 (delta / (adjust_seconds * _current_frame_rate));
664                         
665                         // cerr << "adjust using " << delta
666                         // << " towards " << adjusted_speed
667                         // << " ratio = " << adjusted_speed / slave_speed
668                         // << " current = " << _transport_speed
669                         // << " slave @ " << slave_speed
670                         // << endl;
671                         
672                         request_transport_speed (adjusted_speed);
673                         
674 #if 1
675                         if ((jack_nframes_t) average_slave_delta > _slave->resolution()) {
676                                 // cerr << "not locked\n";
677                                 goto silent_motion;
678                         }
679 #endif
680                 }
681         } 
682
683         if (!starting && !non_realtime_work_pending()) {
684                 /* speed is set, we're locked, and good to go */
685                 return true;
686         }
687
688   silent_motion:
689
690         if (slave_speed && _transport_speed) {
691
692                 /* something isn't right, but we should move with the master
693                    for now.
694                 */
695
696                 bool need_butler;
697                 
698                 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
699                 if (!dsm.locked()) {
700                         goto noroll;
701                 }
702
703                 
704                 prepare_diskstreams ();
705                 silent_process_routes (nframes, offset);
706                 commit_diskstreams (nframes, need_butler);
707
708                 if (need_butler) {
709                         summon_butler ();
710                 }
711                 
712                 jack_nframes_t frames_moved = (long) floor (_transport_speed * nframes);
713                 
714                 if (frames_moved < 0) {
715                         decrement_transport_position (-frames_moved);
716                 } else {
717                         increment_transport_position (frames_moved);
718                 }
719                 
720                 jack_nframes_t stop_limit;
721                 
722                 if (actively_recording()) {
723                         stop_limit = max_frames;
724                 } else {
725                         if (Config->get_stop_at_session_end()) {
726                                 stop_limit = current_end_frame();
727                         } else {
728                                 stop_limit = max_frames;
729                         }
730                 }
731
732                 maybe_stop (stop_limit);
733         }
734
735   noroll:
736         /* don't move at all */
737         no_roll (nframes, 0);
738         return false;
739 }
740
741 void
742 Session::process_without_events (jack_nframes_t nframes)
743 {
744         cerr << "[DR] without events" << endl;
745
746         bool session_needs_butler = false;
747         jack_nframes_t stop_limit;
748         long frames_moved;
749         
750         {
751                 TentativeRWLockMonitor rm (route_lock, false, __LINE__, __FILE__);
752                 TentativeRWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
753
754                 if (!rm.locked() || !dsm.locked() || (post_transport_work & (PostTransportLocate|PostTransportStop))) {
755                         no_roll (nframes, 0);
756                         return;
757                 }
758
759                 if (!_exporting && _slave) {
760                         if (!follow_slave (nframes, 0)) {
761                                 return;
762                         }
763                 } 
764
765                 if (_transport_speed == 0) {
766                         no_roll (nframes, 0);
767                         return;
768                 }
769                 
770                 if (actively_recording()) {
771                         stop_limit = max_frames;
772                 } else {
773                         if (Config->get_stop_at_session_end()) {
774                                 stop_limit = current_end_frame();
775                         } else {
776                                 stop_limit = max_frames;
777                         }
778                 }
779                 
780                 if (maybe_stop (stop_limit)) {
781                         no_roll (nframes, 0);
782                         return;
783                 } 
784
785                 click (_transport_frame, nframes, 0);
786
787                 prepare_diskstreams ();
788         
789                 frames_moved = (long) floor (_transport_speed * nframes);
790
791                 if (process_routes (nframes, 0)) {
792                         no_roll (nframes, 0);
793                         return;
794                 }
795
796                 commit_diskstreams (nframes, session_needs_butler);
797
798                 if (frames_moved < 0) {
799                         decrement_transport_position (-frames_moved);
800                 } else {
801                         increment_transport_position (frames_moved);
802                 }
803                 
804                 maybe_stop (stop_limit);
805                 check_declick_out ();
806
807         } /* implicit release of route lock */
808
809         send_midi_time_code_for_cycle(nframes);
810
811         if (session_needs_butler)
812                 summon_butler ();
813 }
814
815 void
816 Session::process_audition (jack_nframes_t nframes)
817 {
818         TentativeRWLockMonitor rm (route_lock, false, __LINE__, __FILE__);
819         Event* ev;
820
821         if (rm.locked()) {
822                 for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
823                         if (!(*i)->hidden()) {
824                                 (*i)->silence (nframes, 0);
825                         }
826                 }
827         }
828         
829         if (auditioner->play_audition (nframes) > 0) {
830                 summon_butler ();
831         } 
832
833         while (pending_events.read (&ev, 1) == 1) {
834                 merge_event (ev);
835         }
836
837         /* if we are not in the middle of a state change,
838            and there are immediate events queued up,
839            process them. 
840         */
841
842         while (!non_realtime_work_pending() && !immediate_events.empty()) {
843                 Event *ev = immediate_events.front ();
844                 immediate_events.pop_front ();
845                 process_event (ev);
846         }
847
848         if (!auditioner->active()) {
849                 process_function = &Session::process_with_events;
850         }
851 }
852