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