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