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