Use shared_ptr for Port in the AudioEngine; improves thread-safety of the audio engin...
[ardour.git] / libs / ardour / delivery.cc
1 /*
2     Copyright (C) 2009 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify it
5     under the terms of the GNU General Public License as published by the Free
6     Software Foundation; either version 2 of the License, or (at your option)
7     any later version.
8
9     This program is distributed in the hope that it will be useful, but WITHOUT
10     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12     for more details.
13
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <cmath>
20 #include <algorithm>
21
22 #include "pbd/enumwriter.h"
23 #include "pbd/convert.h"
24
25 #include "ardour/midi_buffer.h"
26
27 #include "ardour/debug.h"
28 #include "ardour/delivery.h"
29 #include "ardour/audio_buffer.h"
30 #include "ardour/audio_port.h"
31 #include "ardour/amp.h"
32 #include "ardour/buffer_set.h"
33 #include "ardour/configuration.h"
34 #include "ardour/io.h"
35 #include "ardour/meter.h"
36 #include "ardour/mute_master.h"
37 #include "ardour/panner.h"
38 #include "ardour/panner_shell.h"
39 #include "ardour/pannable.h"
40 #include "ardour/port.h"
41 #include "ardour/session.h"
42 #include "ardour/audioengine.h"
43
44 #include "i18n.h"
45
46 using namespace std;
47 using namespace PBD;
48 using namespace ARDOUR;
49
50 PBD::Signal1<void, pframes_t> Delivery::CycleStart;
51 PBD::Signal0<int>             Delivery::PannersLegal;
52 bool                          Delivery::panners_legal = false;
53
54 /* deliver to an existing IO object */
55
56 Delivery::Delivery (Session& s, boost::shared_ptr<IO> io, boost::shared_ptr<Pannable> pannable,
57                     boost::shared_ptr<MuteMaster> mm, const string& name, Role r)
58         : IOProcessor(s, boost::shared_ptr<IO>(), (role_requires_output_ports (r) ? io : boost::shared_ptr<IO>()), name)
59         , _role (r)
60         , _output_buffers (new BufferSet())
61         , _current_gain (1.0)
62         , _no_outs_cuz_we_no_monitor (false)
63         , _mute_master (mm)
64         , no_panner_reset (false)
65         , scnt (0)
66 {
67         if (pannable) {
68                 _panshell = boost::shared_ptr<PannerShell>(new PannerShell (_name, _session, pannable));
69         }
70
71         _display_to_user = false;
72
73         if (_output) {
74                 _output->changed.connect_same_thread (*this, boost::bind (&Delivery::output_changed, this, _1, _2));
75         }
76
77         CycleStart.connect_same_thread (*this, boost::bind (&Delivery::cycle_start, this, _1));
78 }
79
80 /* deliver to a new IO object */
81
82 Delivery::Delivery (Session& s, boost::shared_ptr<Pannable> pannable, boost::shared_ptr<MuteMaster> mm, const string& name, Role r)
83         : IOProcessor(s, false, (role_requires_output_ports (r) ? true : false), name)
84         , _role (r)
85         , _output_buffers (new BufferSet())
86         , _current_gain (1.0)
87         , _no_outs_cuz_we_no_monitor (false)
88         , _mute_master (mm)
89         , no_panner_reset (false)
90         , scnt (0)
91 {
92         if (pannable) {
93                 _panshell = boost::shared_ptr<PannerShell>(new PannerShell (_name, _session, pannable));
94         }
95
96         _display_to_user = false;
97
98         if (_output) {
99                 _output->changed.connect_same_thread (*this, boost::bind (&Delivery::output_changed, this, _1, _2));
100         }
101
102         CycleStart.connect_same_thread (*this, boost::bind (&Delivery::cycle_start, this, _1));
103 }
104
105
106 Delivery::~Delivery()
107 {
108         DEBUG_TRACE (DEBUG::Destruction, string_compose ("delivery %1 destructor\n", _name));
109         delete _output_buffers;
110 }
111
112 std::string
113 Delivery::display_name () const
114 {
115         switch (_role) {
116         case Main:
117                 return _("main outs");
118                 break;
119         case Listen:
120                 return _("listen");
121                 break;
122         case Send:
123         case Insert:
124         default:
125                 return name();
126         }
127 }
128
129 void
130 Delivery::cycle_start (pframes_t /*nframes*/)
131 {
132         _no_outs_cuz_we_no_monitor = false;
133 }
134
135 bool
136 Delivery::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
137 {
138         if (_role == Main) {
139
140                 /* the out buffers will be set to point to the port output buffers
141                    of our output object.
142                 */
143
144                 if (_output) {
145                         if (_output->n_ports() != ChanCount::ZERO) {
146                                 /* increase number of output ports if the processor chain requires it */
147                                 out = ChanCount::max (_output->n_ports(), in);
148                                 return true;
149                         } else {
150                                 /* not configured yet - we will passthru */
151                                 out = in;
152                                 return true;
153                         }
154                 } else {
155                         fatal << "programming error: this should never be reached" << endmsg;
156                         /*NOTREACHED*/
157                 }
158
159
160         } else if (_role == Insert) {
161
162                 /* the output buffers will be filled with data from the *input* ports
163                    of this Insert.
164                 */
165
166                 if (_input) {
167                         if (_input->n_ports() != ChanCount::ZERO) {
168                                 out = _input->n_ports();
169                                 return true;
170                         } else {
171                                 /* not configured yet - we will passthru */
172                                 out = in;
173                                 return true;
174                         }
175                 } else {
176                         fatal << "programming error: this should never be reached" << endmsg;
177                         /*NOTREACHED*/
178                 }
179
180         } else {
181                 fatal << "programming error: this should never be reached" << endmsg;
182         }
183
184         return false;
185 }
186
187 /** Caller must hold process lock */
188 bool
189 Delivery::configure_io (ChanCount in, ChanCount out)
190 {
191         assert (!AudioEngine::instance()->process_lock().trylock());
192
193         /* check configuration by comparison with our I/O port configuration, if appropriate.
194            see ::can_support_io_configuration() for comments
195         */
196
197         if (_role == Main) {
198
199                 if (_output) {
200                         if (_output->n_ports() != out) {
201                                 if (_output->n_ports() != ChanCount::ZERO) {
202                                         _output->ensure_io (out, false, this);
203                                 } else {
204                                         /* I/O not yet configured */
205                                 }
206                         }
207                 }
208
209         } else if (_role == Insert) {
210
211                 if (_input) {
212                         if (_input->n_ports() != in) {
213                                 if (_input->n_ports() != ChanCount::ZERO) {
214                                         fatal << _name << " programming error: configure_io called with " << in << " and " << out << " with " << _input->n_ports() << " input ports" << endmsg;
215                                         /*NOTREACHED*/
216                                 } else {
217                                         /* I/O not yet configured */
218                                 }
219                         }
220                 }
221
222         }
223
224         if (!Processor::configure_io (in, out)) {
225                 return false;
226         }
227
228         reset_panner ();
229
230         return true;
231 }
232
233 void
234 Delivery::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes, bool result_required)
235 {
236         assert (_output);
237
238         PortSet& ports (_output->ports());
239         gain_t tgain;
240
241         if (_output->n_ports ().get (_output->default_type()) == 0) {
242                 goto out;
243         }
244
245         if (!_active && !_pending_active) {
246                 _output->silence (nframes);
247                 goto out;
248         }
249
250         /* this setup is not just for our purposes, but for anything that comes after us in the
251            processing pathway that wants to use this->output_buffers() for some reason.
252         */
253
254         output_buffers().get_jack_port_addresses (ports, nframes);
255
256         // this Delivery processor is not a derived type, and thus we assume
257         // we really can modify the buffers passed in (it is almost certainly
258         // the main output stage of a Route). Contrast with Send::run()
259         // which cannot do this.
260
261         tgain = target_gain ();
262
263         if (tgain != _current_gain) {
264                 /* target gain has changed */
265
266                 Amp::apply_gain (bufs, nframes, _current_gain, tgain);
267                 _current_gain = tgain;
268
269         } else if (tgain == 0.0) {
270
271                 /* we were quiet last time, and we're still supposed to be quiet.
272                    Silence the outputs, and make sure the buffers are quiet too,
273                 */
274
275                 _output->silence (nframes);
276                 if (result_required) {
277                         bufs.set_count (output_buffers().count ());
278                         Amp::apply_simple_gain (bufs, nframes, 0.0);
279                 }
280                 goto out;
281
282         } else if (tgain != 1.0) {
283
284                 /* target gain has not changed, but is not unity */
285                 Amp::apply_simple_gain (bufs, nframes, tgain);
286         }
287
288         if (_panshell && !_panshell->bypassed() && _panshell->panner()) {
289
290                 // Use the panner to distribute audio to output port buffers
291
292                 _panshell->run (bufs, output_buffers(), start_frame, end_frame, nframes);
293
294                 // MIDI data will not have been delivered by the panner
295
296                 if (bufs.count().n_midi() > 0 && ports.count().n_midi () > 0) {
297                         _output->copy_to_outputs (bufs, DataType::MIDI, nframes, 0);
298                 }
299
300         } else {
301
302                 // Do a 1:1 copy of data to output ports
303
304                 if (bufs.count().n_audio() > 0 && ports.count().n_audio () > 0) {
305                         _output->copy_to_outputs (bufs, DataType::AUDIO, nframes, 0);
306                 }
307
308                 if (bufs.count().n_midi() > 0 && ports.count().n_midi () > 0) {
309                         _output->copy_to_outputs (bufs, DataType::MIDI, nframes, 0);
310                 }
311         }
312
313         if (result_required) {
314                 bufs.read_from (output_buffers (), nframes);
315         }
316
317   out:
318         _active = _pending_active;
319 }
320
321 XMLNode&
322 Delivery::state (bool full_state)
323 {
324         XMLNode& node (IOProcessor::state (full_state));
325
326         if (_role & Main) {
327                 node.add_property("type", "main-outs");
328         } else if (_role & Listen) {
329                 node.add_property("type", "listen");
330         } else {
331                 node.add_property("type", "delivery");
332         }
333
334         node.add_property("role", enum_2_string(_role));
335
336         if (_panshell) {
337                 node.add_child_nocopy (_panshell->get_state ());
338         }
339
340         return node;
341 }
342
343 int
344 Delivery::set_state (const XMLNode& node, int version)
345 {
346         const XMLProperty* prop;
347
348         if (IOProcessor::set_state (node, version)) {
349                 return -1;
350         }
351
352         if ((prop = node.property ("role")) != 0) {
353                 _role = Role (string_2_enum (prop->value(), _role));
354                 // std::cerr << this << ' ' << _name << " set role to " << enum_2_string (_role) << std::endl;
355         } else {
356                 // std::cerr << this << ' ' << _name << " NO ROLE INFO\n";
357         }
358
359         XMLNode* pan_node = node.child (X_("PannerShell"));
360
361         if (pan_node && _panshell) {
362                 _panshell->set_state (*pan_node, version);
363         }
364
365         reset_panner ();
366
367         return 0;
368 }
369
370 void
371 Delivery::unpan ()
372 {
373         /* caller must hold process lock */
374
375         _panshell.reset ();
376 }
377
378 uint32_t
379 Delivery::pan_outs () const
380 {
381         if (_output) {
382                 return _output->n_ports().n_audio();
383         } 
384
385         return _configured_output.n_audio();
386 }
387
388 void
389 Delivery::reset_panner ()
390 {
391         if (panners_legal) {
392                 if (!no_panner_reset) {
393
394                         if (_panshell) {
395                                 _panshell->configure_io (ChanCount (DataType::AUDIO, pans_required()), ChanCount (DataType::AUDIO, pan_outs()));
396                                 
397                                 if (_role == Main) {
398                                         _panshell->pannable()->set_panner (_panshell->panner());
399                                 }
400                         }
401                 }
402
403         } else {
404                 panner_legal_c.disconnect ();
405                 PannersLegal.connect_same_thread (panner_legal_c, boost::bind (&Delivery::panners_became_legal, this));
406         }
407 }
408
409 int
410 Delivery::panners_became_legal ()
411 {
412         if (_panshell) {
413                 _panshell->configure_io (ChanCount (DataType::AUDIO, pans_required()), ChanCount (DataType::AUDIO, pan_outs()));
414                 
415                 if (_role == Main) {
416                         _panshell->pannable()->set_panner (_panshell->panner());
417                 }
418         }
419
420         panner_legal_c.disconnect ();
421         return 0;
422 }
423
424 void
425 Delivery::defer_pan_reset ()
426 {
427         no_panner_reset = true;
428 }
429
430 void
431 Delivery::allow_pan_reset ()
432 {
433         no_panner_reset = false;
434         reset_panner ();
435 }
436
437
438 int
439 Delivery::disable_panners (void)
440 {
441         panners_legal = false;
442         return 0;
443 }
444
445 int
446 Delivery::reset_panners ()
447 {
448         panners_legal = true;
449         return *PannersLegal ();
450 }
451
452 void
453 Delivery::flush_buffers (framecnt_t nframes, framepos_t time)
454 {
455         /* io_lock, not taken: function must be called from Session::process() calltree */
456
457         if (!_output) {
458                 return;
459         }
460         
461         PortSet& ports (_output->ports());
462
463         for (PortSet::iterator i = ports.begin(); i != ports.end(); ++i) {
464                 i->flush_buffers (nframes, time);
465         }
466 }
467
468 void
469 Delivery::transport_stopped (framepos_t now)
470 {
471         Processor::transport_stopped (now);
472
473         if (_panshell) {
474                 _panshell->pannable()->transport_stopped (now);
475         }
476
477         if (_output) {
478                 PortSet& ports (_output->ports());
479
480                 for (PortSet::iterator i = ports.begin(); i != ports.end(); ++i) {
481                         i->transport_stopped ();
482                 }
483         }
484 }
485
486 void
487 Delivery::realtime_locate ()
488 {
489         if (_output) {
490                 PortSet& ports (_output->ports());
491
492                 for (PortSet::iterator i = ports.begin(); i != ports.end(); ++i) {
493                         i->realtime_locate ();
494                 }
495         }
496 }
497
498 gain_t
499 Delivery::target_gain ()
500 {
501         /* if we've been requested to deactivate, our target gain is zero */
502
503         if (!_pending_active) {
504                 return 0.0;
505         }
506
507         /* if we've been told not to output because its a monitoring situation and
508            we're not monitoring, then be quiet.
509         */
510
511         if (_no_outs_cuz_we_no_monitor) {
512                 return 0.0;
513         }
514
515         MuteMaster::MutePoint mp = MuteMaster::Main; // stupid gcc uninit warning
516
517         switch (_role) {
518         case Main:
519                 mp = MuteMaster::Main;
520                 break;
521         case Listen:
522                 mp = MuteMaster::Listen;
523                 break;
524         case Send:
525         case Insert:
526         case Aux:
527                 if (_pre_fader) {
528                         mp = MuteMaster::PreFader;
529                 } else {
530                         mp = MuteMaster::PostFader;
531                 }
532                 break;
533         }
534
535         gain_t desired_gain = _mute_master->mute_gain_at (mp);
536
537         if (_role == Listen && _session.monitor_out() && !_session.listening()) {
538
539                 /* nobody is soloed, and this delivery is a listen-send to the
540                    control/monitor/listen bus, we should be silent since
541                    it gets its signal from the master out.
542                 */
543
544                 desired_gain = 0.0;
545
546         }
547
548         return desired_gain;
549 }
550
551 void
552 Delivery::no_outs_cuz_we_no_monitor (bool yn)
553 {
554         _no_outs_cuz_we_no_monitor = yn;
555 }
556
557 bool
558 Delivery::set_name (const std::string& name)
559 {
560         bool ret = IOProcessor::set_name (name);
561
562         if (ret) {
563                 ret = _panshell->set_name (name);
564         }
565
566         return ret;
567 }
568
569 bool ignore_output_change = false;
570
571 void
572 Delivery::output_changed (IOChange change, void* /*src*/)
573 {
574         if (change.type & IOChange::ConfigurationChanged) {
575                 reset_panner ();
576                 _output_buffers->attach_buffers (_output->ports ());
577         }
578 }
579
580 boost::shared_ptr<Panner>
581 Delivery::panner () const
582 {
583         if (_panshell) {
584                 return _panshell->panner();
585         } else {
586                 return boost::shared_ptr<Panner>();
587         }
588 }
589