merge with master, including manual merge conflict resolution
[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 #include "pbd/enumwriter.h"
27
28 #include <glibmm/threads.h>
29
30 #include "ardour/audioengine.h"
31 #include "ardour/auditioner.h"
32 #include "ardour/butler.h"
33 #include "ardour/cycle_timer.h"
34 #include "ardour/debug.h"
35 #include "ardour/graph.h"
36 #include "ardour/port.h"
37 #include "ardour/process_thread.h"
38 #include "ardour/session.h"
39 #include "ardour/slave.h"
40 #include "ardour/ticker.h"
41 #include "ardour/types.h"
42
43 #include "midi++/manager.h"
44 #include "midi++/mmc.h"
45
46 #include "i18n.h"
47
48 #include <xmmintrin.h>
49
50 using namespace ARDOUR;
51 using namespace PBD;
52 using namespace std;
53
54 /** Called by the audio engine when there is work to be done with JACK.
55  * @param nframes Number of frames to process.
56  */
57
58 void
59 Session::process (pframes_t nframes)
60 {
61         framepos_t transport_at_start = _transport_frame;
62
63         _silent = false;
64
65         if (processing_blocked()) {
66                 _silent = true;
67                 return;
68         }
69
70         if (non_realtime_work_pending()) {
71                 if (!_butler->transport_work_requested ()) {
72                         post_transport ();
73                 }
74         }
75
76         _engine.main_thread()->get_buffers ();
77
78         (this->*process_function) (nframes);
79
80         _engine.main_thread()->drop_buffers ();
81
82         /* deliver MIDI clock. Note that we need to use the transport frame
83          * position at the start of process(), not the value at the end of
84          * it. We may already have ticked() because of a transport state
85          * change, for example.
86          */
87
88         try {
89                 if (!_engine.freewheeling() && Config->get_send_midi_clock() && transport_speed() == 1.0f && midi_clock->has_midi_port()) {
90                         midi_clock->tick (transport_at_start);
91                 }
92         } catch (...) {
93                 /* don't bother with a message */
94         }
95
96         SendFeedback (); /* EMIT SIGNAL */
97 }
98
99 int
100 Session::fail_roll (pframes_t nframes)
101 {
102         return no_roll (nframes);
103 }
104
105 int
106 Session::no_roll (pframes_t nframes)
107 {
108         PT_TIMING_CHECK (4);
109         
110         framepos_t end_frame = _transport_frame + nframes; // FIXME: varispeed + no_roll ??
111         int ret = 0;
112         int declick = get_transport_declick_required();
113         boost::shared_ptr<RouteList> r = routes.reader ();
114
115         if (_click_io) {
116                 _click_io->silence (nframes);
117         }
118
119         ltc_tx_send_time_code_for_cycle (_transport_frame, end_frame, _target_transport_speed, _transport_speed, nframes);
120
121         if (_process_graph) {
122                 DEBUG_TRACE(DEBUG::ProcessThreads,"calling graph/no-roll\n");
123                 _process_graph->routes_no_roll( nframes, _transport_frame, end_frame, non_realtime_work_pending(), declick);
124         } else {
125                 PT_TIMING_CHECK (10);
126                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
127
128                         if ((*i)->is_auditioner()) {
129                                 continue;
130                         }
131
132                         (*i)->set_pending_declick (declick);
133
134                         if ((*i)->no_roll (nframes, _transport_frame, end_frame, non_realtime_work_pending())) {
135                                 error << string_compose(_("Session: error in no roll for %1"), (*i)->name()) << endmsg;
136                                 ret = -1;
137                                 break;
138                         }
139                 }
140                 PT_TIMING_CHECK (11);
141         }
142
143         PT_TIMING_CHECK (5);
144         return ret;
145 }
146
147 /** @param need_butler to be set to true by this method if it needs the butler,
148  *  otherwise it must be left alone.
149  */
150 int
151 Session::process_routes (pframes_t nframes, bool& need_butler)
152 {
153         int  declick = get_transport_declick_required();
154         boost::shared_ptr<RouteList> r = routes.reader ();
155
156         if (transport_sub_state & StopPendingCapture) {
157                 /* force a declick out */
158                 declick = -1;
159         }
160
161         const framepos_t start_frame = _transport_frame;
162         const framepos_t end_frame = _transport_frame + floor (nframes * _transport_speed);
163         
164         if (_process_graph) {
165                 DEBUG_TRACE(DEBUG::ProcessThreads,"calling graph/process-routes\n");
166                 _process_graph->process_routes (nframes, start_frame, end_frame, declick, need_butler);
167         } else {
168
169                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
170
171                         int ret;
172
173                         if ((*i)->is_auditioner()) {
174                                 continue;
175                         }
176
177                         (*i)->set_pending_declick (declick);
178
179                         bool b = false;
180
181                         if ((ret = (*i)->roll (nframes, start_frame, end_frame, declick, b)) < 0) {
182                                 stop_transport ();
183                                 return -1;
184                         }
185
186                         if (b) {
187                                 need_butler = true;
188                         }
189                 }
190         }
191
192         return 0;
193 }
194
195 /** @param need_butler to be set to true by this method if it needs the butler,
196  *  otherwise it must be left alone.
197  */
198 int
199 Session::silent_process_routes (pframes_t nframes, bool& need_butler)
200 {
201         boost::shared_ptr<RouteList> r = routes.reader ();
202
203         const framepos_t start_frame = _transport_frame;
204         const framepos_t end_frame = _transport_frame + lrintf(nframes * _transport_speed);
205
206         if (_process_graph) {
207                 _process_graph->silent_process_routes (nframes, start_frame, end_frame, need_butler);
208         } else {
209                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
210
211                         int ret;
212
213                         if ((*i)->is_auditioner()) {
214                                 continue;
215                         }
216
217                         bool b = false;
218
219                         if ((ret = (*i)->silent_roll (nframes, start_frame, end_frame, b)) < 0) {
220                                 stop_transport ();
221                                 return -1;
222                         }
223
224                         if (b) {
225                                 need_butler = true;
226                         }
227                 }
228         }
229
230         return 0;
231 }
232
233 void
234 Session::get_track_statistics ()
235 {
236         float pworst = 1.0f;
237         float cworst = 1.0f;
238
239         boost::shared_ptr<RouteList> rl = routes.reader();
240         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
241
242                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
243
244                 if (!tr || tr->hidden()) {
245                         continue;
246                 }
247
248                 pworst = min (pworst, tr->playback_buffer_load());
249                 cworst = min (cworst, tr->capture_buffer_load());
250         }
251
252         g_atomic_int_set (&_playback_load, (uint32_t) floor (pworst * 100.0f));
253         g_atomic_int_set (&_capture_load, (uint32_t) floor (cworst * 100.0f));
254
255         if (actively_recording()) {
256                 set_dirty();
257         }
258 }
259
260 /** Process callback used when the auditioner is not active */
261 void
262 Session::process_with_events (pframes_t nframes)
263 {
264         PT_TIMING_CHECK (3);
265         
266         SessionEvent*  ev;
267         pframes_t      this_nframes;
268         framepos_t     end_frame;
269         bool           session_needs_butler = false;
270         framecnt_t     frames_moved;
271
272         /* make sure the auditioner is silent */
273
274         if (auditioner) {
275                 auditioner->silence (nframes);
276         }
277
278         /* handle any pending events */
279
280         while (pending_events.read (&ev, 1) == 1) {
281                 merge_event (ev);
282         }
283
284         /* if we are not in the middle of a state change,
285            and there are immediate events queued up,
286            process them.
287         */
288
289         while (!non_realtime_work_pending() && !immediate_events.empty()) {
290                 SessionEvent *ev = immediate_events.front ();
291                 immediate_events.pop_front ();
292                 process_event (ev);
293         }
294
295         /* Decide on what to do with quarter-frame MTC during this cycle */
296
297         bool const was_sending_qf_mtc = _send_qf_mtc;
298         double const tolerance = Config->get_mtc_qf_speed_tolerance() / 100.0;
299
300         if (_transport_speed != 0) {
301                 _send_qf_mtc = (
302                         Config->get_send_mtc () &&
303                         _transport_speed >= (1 - tolerance) &&
304                         _transport_speed <= (1 + tolerance)
305                         );
306
307                 if (_send_qf_mtc && !was_sending_qf_mtc) {
308                         /* we will re-start quarter-frame MTC this cycle, so send a full update to set things up */
309                         _send_timecode_update = true;
310                 }
311
312                 if (Config->get_send_mtc() && !_send_qf_mtc && _pframes_since_last_mtc > (frame_rate () / 4)) {
313                         /* we're sending MTC, but we're not sending QF MTC at the moment, and it's been
314                            a quarter of a second since we sent anything at all, so send a full MTC update
315                            this cycle.
316                         */
317                         _send_timecode_update = true;
318                 }
319
320                 _pframes_since_last_mtc += nframes;
321         }
322
323         /* Events caused a transport change (or we re-started sending
324          * MTC), so send an MTC Full Frame (Timecode) message.  This
325          * is sent whether rolling or not, to give slaves an idea of
326          * ardour time on locates (and allow slow slaves to position
327          * and prepare for rolling)
328          */
329         if (_send_timecode_update) {
330                 send_full_time_code (_transport_frame);
331         }
332
333         if (!process_can_proceed()) {
334                 _silent = true;
335                 return;
336         }
337
338         if (events.empty() || next_event == events.end()) {
339                 process_without_events (nframes);
340                 return;
341         }
342
343         if (_transport_speed == 1.0) {
344                 frames_moved = (framecnt_t) nframes;
345         } else {
346                 interpolation.set_target_speed (_target_transport_speed);
347                 interpolation.set_speed (_transport_speed);
348                 frames_moved = (framecnt_t) interpolation.interpolate (0, nframes, 0, 0);
349         }
350
351         end_frame = _transport_frame + frames_moved;
352
353         {
354                 SessionEvent* this_event;
355                 Events::iterator the_next_one;
356
357                 if (!process_can_proceed()) {
358                         _silent = true;
359                         return;
360                 }
361
362                 if (!_exporting && _slave) {
363                         if (!follow_slave (nframes)) {
364                                 return;
365                         }
366                 }
367
368                 if (_transport_speed == 0) {
369                         no_roll (nframes);
370                         return;
371                 }
372
373                 if (!_exporting && !timecode_transmission_suspended()) {
374                         send_midi_time_code_for_cycle (_transport_frame, end_frame, nframes);
375                 }
376
377                 framepos_t stop_limit = compute_stop_limit ();
378
379                 if (maybe_stop (stop_limit)) {
380                         no_roll (nframes);
381                         return;
382                 }
383
384                 this_event = *next_event;
385                 the_next_one = next_event;
386                 ++the_next_one;
387
388                 /* yes folks, here it is, the actual loop where we really truly
389                    process some audio
390                 */
391
392                 while (nframes) {
393
394                         this_nframes = nframes; /* real (jack) time relative */
395                         frames_moved = (framecnt_t) floor (_transport_speed * nframes); /* transport relative */
396
397                         /* running an event, position transport precisely to its time */
398                         if (this_event && this_event->action_frame <= end_frame && this_event->action_frame >= _transport_frame) {
399                                 /* this isn't quite right for reverse play */
400                                 frames_moved = (framecnt_t) (this_event->action_frame - _transport_frame);
401                                 this_nframes = abs (floor(frames_moved / _transport_speed));
402                         }
403
404                         if (this_nframes) {
405
406                                 click (_transport_frame, this_nframes);
407
408                                 if (process_routes (this_nframes, session_needs_butler)) {
409                                         fail_roll (nframes);
410                                         return;
411                                 }
412
413                                 get_track_statistics ();
414
415                                 nframes -= this_nframes;
416
417                                 if (frames_moved < 0) {
418                                         decrement_transport_position (-frames_moved);
419                                 } else {
420                                         increment_transport_position (frames_moved);
421                                 }
422
423                                 maybe_stop (stop_limit);
424                                 check_declick_out ();
425                         }
426
427                         _engine.split_cycle (this_nframes);
428
429                         /* now handle this event and all others scheduled for the same time */
430
431                         while (this_event && this_event->action_frame == _transport_frame) {
432                                 process_event (this_event);
433
434                                 if (the_next_one == events.end()) {
435                                         this_event = 0;
436                                 } else {
437                                         this_event = *the_next_one;
438                                         ++the_next_one;
439                                 }
440                         }
441
442                         /* if an event left our state changing, do the right thing */
443
444                         if (nframes && non_realtime_work_pending()) {
445                                 no_roll (nframes);
446                                 break;
447                         }
448
449                         /* this is necessary to handle the case of seamless looping */
450                         end_frame = _transport_frame + floor (nframes * _transport_speed);
451                 }
452
453                 set_next_event ();
454
455         } /* implicit release of route lock */
456
457         if (session_needs_butler) {
458                 _butler->summon ();
459         }
460 }
461
462 void
463 Session::reset_slave_state ()
464 {
465         average_slave_delta = 1800;
466         delta_accumulator_cnt = 0;
467         have_first_delta_accumulator = false;
468         _slave_state = Stopped;
469 }
470
471 bool
472 Session::transport_locked () const
473 {
474         Slave* sl = _slave;
475
476         if (!locate_pending() && (!config.get_external_sync() || (sl && sl->ok() && sl->locked()))) {
477                 return true;
478         }
479
480         return false;
481 }
482
483 bool
484 Session::follow_slave (pframes_t nframes)
485 {
486         double slave_speed;
487         framepos_t slave_transport_frame;
488         framecnt_t this_delta;
489         int dir;
490
491         if (!_slave->ok()) {
492                 stop_transport ();
493                 config.set_external_sync (false);
494                 goto noroll;
495         }
496
497         _slave->speed_and_position (slave_speed, slave_transport_frame);
498
499         DEBUG_TRACE (DEBUG::Slave, string_compose ("Slave position %1 speed %2\n", slave_transport_frame, slave_speed));
500
501         if (!_slave->locked()) {
502                 DEBUG_TRACE (DEBUG::Slave, "slave not locked\n");
503                 goto noroll;
504         }
505
506         if (slave_transport_frame > _transport_frame) {
507                 this_delta = slave_transport_frame - _transport_frame;
508                 dir = 1;
509         } else {
510                 this_delta = _transport_frame - slave_transport_frame;
511                 dir = -1;
512         }
513
514         if (_slave->starting()) {
515                 slave_speed = 0.0f;
516         }
517
518         if (_slave->is_always_synced() ||
519                         (Config->get_timecode_source_is_synced() && (dynamic_cast<TimecodeSlave*>(_slave)) != 0)
520                         ) {
521
522                 /* if the TC source is synced, then we assume that its
523                    speed is binary: 0.0 or 1.0
524                 */
525
526                 if (slave_speed != 0.0f) {
527                         slave_speed = 1.0f;
528                 }
529
530         } else {
531
532                 /* if we are chasing and the average delta between us and the
533                    master gets too big, we want to switch to silent
534                    motion. so keep track of that here.
535                 */
536
537                 if (_slave_state == Running) {
538                         calculate_moving_average_of_slave_delta(dir, this_delta);
539                 }
540         }
541
542         track_slave_state (slave_speed, slave_transport_frame, this_delta);
543
544         DEBUG_TRACE (DEBUG::Slave, string_compose ("slave state %1 @ %2 speed %3 cur delta %4 avg delta %5\n",
545                                                    _slave_state, slave_transport_frame, slave_speed, this_delta, average_slave_delta));
546
547
548         if (_slave_state == Running && !_slave->is_always_synced() &&
549                         !(Config->get_timecode_source_is_synced() && (dynamic_cast<TimecodeSlave*>(_slave)) != 0)
550                         ) {
551
552                 if (_transport_speed != 0.0f) {
553
554                         /*
555                            note that average_dir is +1 or -1
556                         */
557
558                         float delta;
559
560                         if (average_slave_delta == 0) {
561                                 delta = this_delta;
562                                 delta *= dir;
563                         } else {
564                                 delta = average_slave_delta;
565                                 delta *= average_dir;
566                         }
567
568 #ifndef NDEBUG
569                         if (slave_speed != 0.0) {
570                                 DEBUG_TRACE (DEBUG::Slave, string_compose ("delta = %1 speed = %2 ts = %3 M@%4 S@%5 avgdelta %6\n",
571                                                                            (int) (dir * this_delta),
572                                                                            slave_speed,
573                                                                            _transport_speed,
574                                                                            _transport_frame,
575                                                                            slave_transport_frame,
576                                                                            average_slave_delta));
577                         }
578 #endif
579
580                         if (_slave->give_slave_full_control_over_transport_speed()) {
581                                 set_transport_speed (slave_speed, false, false);
582                                 //std::cout << "set speed = " << slave_speed << "\n";
583                         } else {
584                                 float adjusted_speed = slave_speed + (1.5 * (delta /  float(_current_frame_rate)));
585                                 request_transport_speed (adjusted_speed);
586                                 DEBUG_TRACE (DEBUG::Slave, string_compose ("adjust using %1 towards %2 ratio %3 current %4 slave @ %5\n",
587                                                                            delta, adjusted_speed, adjusted_speed/slave_speed, _transport_speed,
588                                                                            slave_speed));
589                         }
590
591 #if 1
592                         if (!actively_recording() && (framecnt_t) abs(average_slave_delta) > _slave->resolution()) {
593                                 cerr << "average slave delta greater than slave resolution (" << _slave->resolution() << "), going to silent motion\n";
594                                 goto silent_motion;
595                         }
596 #endif
597                 }
598         }
599
600
601         if (_slave_state == Running && !non_realtime_work_pending()) {
602                 /* speed is set, we're locked, and good to go */
603                 return true;
604         }
605
606   silent_motion:
607         DEBUG_TRACE (DEBUG::Slave, "silent motion\n")
608         follow_slave_silently (nframes, slave_speed);
609
610   noroll:
611         /* don't move at all */
612         DEBUG_TRACE (DEBUG::Slave, "no roll\n")
613         no_roll (nframes);
614         return false;
615 }
616
617 void
618 Session::calculate_moving_average_of_slave_delta (int dir, framecnt_t this_delta)
619 {
620         if (delta_accumulator_cnt >= delta_accumulator_size) {
621                 have_first_delta_accumulator = true;
622                 delta_accumulator_cnt = 0;
623         }
624
625         if (delta_accumulator_cnt != 0 || this_delta < _current_frame_rate) {
626                 delta_accumulator[delta_accumulator_cnt++] = (framecnt_t) dir *  (framecnt_t) this_delta;
627         }
628
629         if (have_first_delta_accumulator) {
630                 average_slave_delta = 0L;
631                 for (int i = 0; i < delta_accumulator_size; ++i) {
632                         average_slave_delta += delta_accumulator[i];
633                 }
634                 average_slave_delta /= (int32_t) delta_accumulator_size;
635                 if (average_slave_delta < 0L) {
636                         average_dir = -1;
637                         average_slave_delta = abs(average_slave_delta);
638                 } else {
639                         average_dir = 1;
640                 }
641         }
642 }
643
644 void
645 Session::track_slave_state (float slave_speed, framepos_t slave_transport_frame, framecnt_t /*this_delta*/)
646 {
647         if (slave_speed != 0.0f) {
648
649                 /* slave is running */
650
651                 switch (_slave_state) {
652                 case Stopped:
653                         if (_slave->requires_seekahead()) {
654                                 slave_wait_end = slave_transport_frame + _slave->seekahead_distance ();
655                                 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave stopped, but running, requires seekahead to %1\n", slave_wait_end));
656                                 /* we can call locate() here because we are in process context */
657                                 locate (slave_wait_end, false, false);
658                                 _slave_state = Waiting;
659
660                         } else {
661
662                                 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave stopped -> running at %1\n", slave_transport_frame));
663
664                                 memset (delta_accumulator, 0, sizeof (int32_t) * delta_accumulator_size);
665                                 average_slave_delta = 0L;
666
667                                 Location* al = _locations->auto_loop_location();
668
669                                 if (al && play_loop && (slave_transport_frame < al->start() || slave_transport_frame > al->end())) {
670                                         // cancel looping
671                                         request_play_loop(false);
672                                 }
673
674                                 if (slave_transport_frame != _transport_frame) {
675                                         DEBUG_TRACE (DEBUG::Slave, string_compose ("require locate to run. eng: %1 -> sl: %2\n", _transport_frame, slave_transport_frame));
676                                         locate (slave_transport_frame, false, false);
677                                 }
678                                 _slave_state = Running;
679                         }
680                         break;
681
682                 case Waiting:
683                 default:
684                         break;
685                 }
686
687                 if (_slave_state == Waiting) {
688
689                         DEBUG_TRACE (DEBUG::Slave, string_compose ("slave waiting at %1\n", slave_transport_frame));
690
691                         if (slave_transport_frame >= slave_wait_end) {
692
693                                 DEBUG_TRACE (DEBUG::Slave, string_compose ("slave start at %1 vs %2\n", slave_transport_frame, _transport_frame));
694
695                                 _slave_state = Running;
696
697                                 /* now perform a "micro-seek" within the disk buffers to realign ourselves
698                                    precisely with the master.
699                                 */
700
701
702                                 bool ok = true;
703                                 framecnt_t frame_delta = slave_transport_frame - _transport_frame;
704
705                                 boost::shared_ptr<RouteList> rl = routes.reader();
706                                 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
707                                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
708                                         if (tr && !tr->can_internal_playback_seek (frame_delta)) {
709                                                 ok = false;
710                                                 break;
711                                         }
712                                 }
713
714                                 if (ok) {
715                                         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
716                                                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
717                                                 if (tr) {
718                                                         tr->internal_playback_seek (frame_delta);
719                                                 }
720                                         }
721                                         _transport_frame += frame_delta;
722
723                                 } else {
724                                         cerr << "cannot micro-seek\n";
725                                         /* XXX what? */
726                                 }
727                         }
728                 }
729
730                 if (_slave_state == Running && _transport_speed == 0.0f) {
731                         DEBUG_TRACE (DEBUG::Slave, "slave starts transport\n");
732                         start_transport ();
733                 }
734
735         } else { // slave_speed is 0
736
737                 /* slave has stopped */
738
739                 if (_transport_speed != 0.0f) {
740                         DEBUG_TRACE (DEBUG::Slave, string_compose ("slave stops transport: %1 frame %2 tf %3\n", slave_speed, slave_transport_frame, _transport_frame));
741                         stop_transport ();
742                 }
743
744                 if (slave_transport_frame != _transport_frame) {
745                         DEBUG_TRACE (DEBUG::Slave, string_compose ("slave stopped, move to %1\n", slave_transport_frame));
746                         force_locate (slave_transport_frame, false);
747                 }
748
749                 reset_slave_state();
750         }
751 }
752
753 void
754 Session::follow_slave_silently (pframes_t nframes, float slave_speed)
755 {
756         if (slave_speed && _transport_speed) {
757
758                 /* something isn't right, but we should move with the master
759                    for now.
760                 */
761
762                 bool need_butler;
763
764                 silent_process_routes (nframes, need_butler);
765
766                 get_track_statistics ();
767
768                 if (need_butler) {
769                         _butler->summon ();
770                 }
771
772                 int32_t frames_moved = (int32_t) floor (_transport_speed * nframes);
773
774                 if (frames_moved < 0) {
775                         decrement_transport_position (-frames_moved);
776                 } else {
777                         increment_transport_position (frames_moved);
778                 }
779
780                 framepos_t const stop_limit = compute_stop_limit ();
781                 maybe_stop (stop_limit);
782         }
783 }
784
785 void
786 Session::process_without_events (pframes_t nframes)
787 {
788         bool session_needs_butler = false;
789         framecnt_t frames_moved;
790
791         if (!process_can_proceed()) {
792                 _silent = true;
793                 return;
794         }
795
796         if (!_exporting && _slave) {
797                 if (!follow_slave (nframes)) {
798                         ltc_tx_send_time_code_for_cycle (_transport_frame, _transport_frame, 0, 0 , nframes);
799                         return;
800                 }
801         }
802
803         if (_transport_speed == 0) {
804                 fail_roll (nframes);
805                 return;
806         }
807
808         if (_transport_speed == 1.0) {
809                 frames_moved = (framecnt_t) nframes;
810         } else {
811                 interpolation.set_target_speed (_target_transport_speed);
812                 interpolation.set_speed (_transport_speed);
813                 frames_moved = (framecnt_t) interpolation.interpolate (0, nframes, 0, 0);
814         }
815
816         if (!_exporting && !timecode_transmission_suspended()) {
817                 send_midi_time_code_for_cycle (_transport_frame, _transport_frame + frames_moved, nframes);
818         }
819
820         ltc_tx_send_time_code_for_cycle (_transport_frame, _transport_frame + frames_moved, _target_transport_speed, _transport_speed, nframes);
821
822         framepos_t const stop_limit = compute_stop_limit ();
823
824         if (maybe_stop (stop_limit)) {
825                 fail_roll (nframes);
826                 return;
827         }
828
829         if (maybe_sync_start (nframes)) {
830                 return;
831         }
832
833         click (_transport_frame, nframes);
834
835         if (process_routes (nframes, session_needs_butler)) {
836                 fail_roll (nframes);
837                 return;
838         }
839
840         get_track_statistics ();
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                 _butler->summon ();
853         }
854 }
855
856 /** Process callback used when the auditioner is active.
857  * @param nframes number of frames to process.
858  */
859 void
860 Session::process_audition (pframes_t nframes)
861 {
862         SessionEvent* ev;
863         boost::shared_ptr<RouteList> r = routes.reader ();
864
865         for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
866                 if (!(*i)->is_auditioner()) {
867                         (*i)->silence (nframes);
868                 }
869         }
870
871         /* run the auditioner, and if it says we need butler service, ask for it */
872
873         if (auditioner->play_audition (nframes) > 0) {
874                 _butler->summon ();
875         }
876
877         /* if using a monitor section, run it because otherwise we don't hear anything */
878
879         if (auditioner->needs_monitor()) {
880                 _monitor_out->monitor_run (_transport_frame, _transport_frame + nframes, nframes, false);
881         }
882
883         /* handle pending events */
884
885         while (pending_events.read (&ev, 1) == 1) {
886                 merge_event (ev);
887         }
888
889         /* if we are not in the middle of a state change,
890            and there are immediate events queued up,
891            process them.
892         */
893
894         while (!non_realtime_work_pending() && !immediate_events.empty()) {
895                 SessionEvent *ev = immediate_events.front ();
896                 immediate_events.pop_front ();
897                 process_event (ev);
898         }
899
900         if (!auditioner->auditioning()) {
901                 /* auditioner no longer active, so go back to the normal process callback */
902                 process_function = &Session::process_with_events;
903         }
904 }
905
906 bool
907 Session::maybe_sync_start (pframes_t & nframes)
908 {
909         pframes_t sync_offset;
910
911         if (!waiting_for_sync_offset) {
912                 return false;
913         }
914
915         if (_engine.get_sync_offset (sync_offset) && sync_offset < nframes) {
916
917                 /* generate silence up to the sync point, then
918                    adjust nframes + offset to reflect whatever
919                    is left to do.
920                 */
921
922                 no_roll (sync_offset);
923                 nframes -= sync_offset;
924                 Port::increment_global_port_buffer_offset (sync_offset);
925                 waiting_for_sync_offset = false;
926
927                 if (nframes == 0) {
928                         return true; // done, nothing left to process
929                 }
930
931         } else {
932
933                 /* sync offset point is not within this process()
934                    cycle, so just generate silence. and don't bother
935                    with any fancy stuff here, just the minimal silence.
936                 */
937
938                 _silent = true;
939
940                 if (Config->get_locate_while_waiting_for_sync()) {
941                         if (micro_locate (nframes)) {
942                                 /* XXX ERROR !!! XXX */
943                         }
944                 }
945
946                 return true; // done, nothing left to process
947         }
948
949         return false;
950 }
951
952 void
953 Session::queue_event (SessionEvent* ev)
954 {
955         if (_state_of_the_state & Deletion) {
956                 return;
957         } else if (_state_of_the_state & Loading) {
958                 merge_event (ev);
959         } else {
960                 pending_events.write (&ev, 1);
961         }
962 }
963
964 void
965 Session::set_next_event ()
966 {
967         if (events.empty()) {
968                 next_event = events.end();
969                 return;
970         }
971
972         if (next_event == events.end()) {
973                 next_event = events.begin();
974         }
975
976         if ((*next_event)->action_frame > _transport_frame) {
977                 next_event = events.begin();
978         }
979
980         for (; next_event != events.end(); ++next_event) {
981                 if ((*next_event)->action_frame >= _transport_frame) {
982                         break;
983                 }
984         }
985 }
986
987 void
988 Session::process_event (SessionEvent* ev)
989 {
990         bool remove = true;
991         bool del = true;
992
993         /* if we're in the middle of a state change (i.e. waiting
994            for the butler thread to complete the non-realtime
995            part of the change), we'll just have to queue this
996            event for a time when the change is complete.
997         */
998
999         if (non_realtime_work_pending()) {
1000
1001                 /* except locates, which we have the capability to handle */
1002
1003                 if (ev->type != SessionEvent::Locate) {
1004                         immediate_events.insert (immediate_events.end(), ev);
1005                         _remove_event (ev);
1006                         return;
1007                 }
1008         }
1009
1010         DEBUG_TRACE (DEBUG::SessionEvents, string_compose ("Processing event: %1 @ %2\n", enum_2_string (ev->type), _transport_frame));
1011
1012         switch (ev->type) {
1013         case SessionEvent::SetLoop:
1014                 set_play_loop (ev->yes_or_no);
1015                 break;
1016
1017         case SessionEvent::AutoLoop:
1018                 if (play_loop) {
1019                         /* roll after locate, do not flush, set "with loop"
1020                            true only if we are seamless looping
1021                         */
1022                         start_locate (ev->target_frame, true, false, Config->get_seamless_loop());
1023                 }
1024                 remove = false;
1025                 del = false;
1026                 break;
1027
1028         case SessionEvent::AutoLoopDeclick:
1029                 if (play_loop) {
1030                         /* Request a declick fade-out and a fade-in; the fade-out will happen
1031                            at the end of the loop, and the fade-in at the start.
1032                         */
1033                         transport_sub_state |= (PendingLoopDeclickOut | PendingLoopDeclickIn);
1034                 }
1035                 remove = false;
1036                 del = false;
1037                 break;
1038
1039         case SessionEvent::Locate:
1040                 if (ev->yes_or_no) {
1041                         /* args: do not roll after locate, do flush, not with loop */
1042                         locate (ev->target_frame, false, true, false);
1043                 } else {
1044                         /* args: do not roll after locate, do flush, not with loop */
1045                         start_locate (ev->target_frame, false, true, false);
1046                 }
1047                 _send_timecode_update = true;
1048                 break;
1049
1050         case SessionEvent::LocateRoll:
1051                 if (ev->yes_or_no) {
1052                         /* args: roll after locate, do flush, not with loop */
1053                         locate (ev->target_frame, true, true, false);
1054                 } else {
1055                         /* args: roll after locate, do flush, not with loop */
1056                         start_locate (ev->target_frame, true, true, false);
1057                 }
1058                 _send_timecode_update = true;
1059                 break;
1060
1061         case SessionEvent::LocateRollLocate:
1062                 // locate is handled by ::request_roll_at_and_return()
1063                 _requested_return_frame = ev->target_frame;
1064                 request_locate (ev->target2_frame, true);
1065                 break;
1066
1067
1068         case SessionEvent::SetTransportSpeed:
1069                 set_transport_speed (ev->speed, ev->yes_or_no, ev->second_yes_or_no, ev->third_yes_or_no);
1070                 break;
1071
1072         case SessionEvent::PunchIn:
1073                 // cerr << "PunchIN at " << transport_frame() << endl;
1074                 if (config.get_punch_in() && record_status() == Enabled) {
1075                         enable_record ();
1076                 }
1077                 remove = false;
1078                 del = false;
1079                 break;
1080
1081         case SessionEvent::PunchOut:
1082                 // cerr << "PunchOUT at " << transport_frame() << endl;
1083                 if (config.get_punch_out()) {
1084                         step_back_from_record ();
1085                 }
1086                 remove = false;
1087                 del = false;
1088                 break;
1089
1090         case SessionEvent::StopOnce:
1091                 if (!non_realtime_work_pending()) {
1092                         stop_transport (ev->yes_or_no);
1093                         _clear_event_type (SessionEvent::StopOnce);
1094                 }
1095                 remove = false;
1096                 del = false;
1097                 break;
1098
1099         case SessionEvent::RangeStop:
1100                 if (!non_realtime_work_pending()) {
1101                         stop_transport (ev->yes_or_no);
1102                 }
1103                 remove = false;
1104                 del = false;
1105                 break;
1106
1107         case SessionEvent::RangeLocate:
1108                 /* args: roll after locate, do flush, not with loop */
1109                 start_locate (ev->target_frame, true, true, false);
1110                 remove = false;
1111                 del = false;
1112                 break;
1113
1114         case SessionEvent::Overwrite:
1115                 overwrite_some_buffers (static_cast<Track*>(ev->ptr));
1116                 break;
1117
1118         case SessionEvent::SetTrackSpeed:
1119                 set_track_speed (static_cast<Track*> (ev->ptr), ev->speed);
1120                 break;
1121
1122         case SessionEvent::SetSyncSource:
1123                 DEBUG_TRACE (DEBUG::Slave, "seen request for new slave\n");
1124                 use_sync_source (ev->slave);
1125                 break;
1126
1127         case SessionEvent::Audition:
1128                 set_audition (ev->region);
1129                 // drop reference to region
1130                 ev->region.reset ();
1131                 break;
1132
1133         case SessionEvent::InputConfigurationChange:
1134                 add_post_transport_work (PostTransportInputChange);
1135                 _butler->schedule_transport_work ();
1136                 break;
1137
1138         case SessionEvent::SetPlayAudioRange:
1139                 set_play_range (ev->audio_range, (ev->speed == 1.0f));
1140                 break;
1141
1142         case SessionEvent::RealTimeOperation:
1143                 process_rtop (ev);
1144                 del = false; // other side of RT request needs to clean up
1145                 break;
1146
1147         case SessionEvent::AdjustPlaybackBuffering:
1148                 schedule_playback_buffering_adjustment ();
1149                 break;
1150
1151         case SessionEvent::AdjustCaptureBuffering:
1152                 schedule_capture_buffering_adjustment ();
1153                 break;
1154
1155         case SessionEvent::SetTimecodeTransmission:
1156                 g_atomic_int_set (&_suspend_timecode_transmission, ev->yes_or_no ? 0 : 1);
1157                 break;
1158
1159         default:
1160           fatal << string_compose(_("Programming error: illegal event type in process_event (%1)"), ev->type) << endmsg;
1161                 /*NOTREACHED*/
1162                 break;
1163         };
1164
1165         if (remove) {
1166                 del = del && !_remove_event (ev);
1167         }
1168
1169         if (del) {
1170                 delete ev;
1171         }
1172 }
1173
1174 framepos_t
1175 Session::compute_stop_limit () const
1176 {
1177         if (!Config->get_stop_at_session_end ()) {
1178                 return max_framepos;
1179         }
1180
1181         if (_slave) {
1182                 return max_framepos;
1183         }
1184
1185         
1186         bool const punching_in = (config.get_punch_in () && _locations->auto_punch_location());
1187         bool const punching_out = (config.get_punch_out () && _locations->auto_punch_location());
1188
1189         if (actively_recording ()) {
1190                 /* permanently recording */
1191                 return max_framepos;
1192         } else if (punching_in && !punching_out) {
1193                 /* punching in but never out */
1194                 return max_framepos;
1195         } else if (punching_in && punching_out && _locations->auto_punch_location()->end() > current_end_frame()) {
1196                 /* punching in and punching out after session end */
1197                 return max_framepos;
1198         }
1199
1200         return current_end_frame ();
1201 }