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