"Listen" delivery processors (i.e. monitor out) never get their own panner; Route...
[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/delivery.h"
28 #include "ardour/audio_buffer.h"
29 #include "ardour/amp.h"
30 #include "ardour/buffer_set.h"
31 #include "ardour/configuration.h"
32 #include "ardour/io.h"
33 #include "ardour/meter.h"
34 #include "ardour/mute_master.h"
35 #include "ardour/panner.h"
36 #include "ardour/panner_shell.h"
37 #include "ardour/pannable.h"
38 #include "ardour/port.h"
39 #include "ardour/session.h"
40 #include "ardour/audioengine.h"
41
42 #include "i18n.h"
43
44 using namespace std;
45 using namespace PBD;
46 using namespace ARDOUR;
47
48 PBD::Signal1<void, pframes_t> Delivery::CycleStart;
49 PBD::Signal0<int>             Delivery::PannersLegal;
50 bool                          Delivery::panners_legal = false;
51
52 /* deliver to an existing IO object */
53
54 Delivery::Delivery (Session& s, boost::shared_ptr<IO> io, boost::shared_ptr<Pannable> pannable, 
55                     boost::shared_ptr<MuteMaster> mm, const string& name, Role r)
56         : IOProcessor(s, boost::shared_ptr<IO>(), (role_requires_output_ports (r) ? io : boost::shared_ptr<IO>()), name)
57         , _role (r)
58         , _output_buffers (new BufferSet())
59         , _current_gain (1.0)
60         , _output_offset (0)
61         , _no_outs_cuz_we_no_monitor (false)
62         , _mute_master (mm)
63         , no_panner_reset (false)
64 {
65         _panshell = boost::shared_ptr<PannerShell>(new PannerShell (_name, _session, pannable));
66         _display_to_user = false;
67
68         if (_output) {
69                 _output->changed.connect_same_thread (*this, boost::bind (&Delivery::output_changed, this, _1, _2));
70         }
71
72         CycleStart.connect_same_thread (*this, boost::bind (&Delivery::cycle_start, this, _1));
73 }
74
75 /* deliver to a new IO object */
76
77 Delivery::Delivery (Session& s, boost::shared_ptr<Pannable> pannable, boost::shared_ptr<MuteMaster> mm, const string& name, Role r)
78         : IOProcessor(s, false, (role_requires_output_ports (r) ? true : false), name)
79         , _role (r)
80         , _output_buffers (new BufferSet())
81         , _current_gain (1.0)
82         , _output_offset (0)
83         , _no_outs_cuz_we_no_monitor (false)
84         , _mute_master (mm)
85         , no_panner_reset (false)
86 {
87         _panshell = boost::shared_ptr<PannerShell>(new PannerShell (_name, _session, pannable));
88         _display_to_user = false;
89
90         if (_output) {
91                 _output->changed.connect_same_thread (*this, boost::bind (&Delivery::output_changed, this, _1, _2));
92         }
93
94         CycleStart.connect_same_thread (*this, boost::bind (&Delivery::cycle_start, this, _1));
95 }
96
97
98 Delivery::~Delivery()
99 {
100         delete _output_buffers;
101 }
102
103 std::string
104 Delivery::display_name () const
105 {
106         switch (_role) {
107         case Main:
108                 return _("main outs");
109                 break;
110         case Listen:
111                 return _("listen");
112                 break;
113         case Send:
114         case Insert:
115         default:
116                 return name();
117         }
118 }
119
120 void
121 Delivery::cycle_start (pframes_t /*nframes*/)
122 {
123         _output_offset = 0;
124         _no_outs_cuz_we_no_monitor = false;
125 }
126
127 void
128 Delivery::increment_output_offset (framecnt_t n)
129 {
130         _output_offset += n;
131 }
132
133 bool
134 Delivery::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
135 {
136         if (_role == Main) {
137
138                 /* the out buffers will be set to point to the port output buffers
139                    of our output object.
140                 */
141
142                 if (_output) {
143                         if (_output->n_ports() != ChanCount::ZERO) {
144                                 /* increase number of output ports if the processor chain requires it */
145                                 out = ChanCount::max (_output->n_ports(), in);
146                                 return true;
147                         } else {
148                                 /* not configured yet - we will passthru */
149                                 out = in;
150                                 return true;
151                         }
152                 } else {
153                         fatal << "programming error: this should never be reached" << endmsg;
154                         /*NOTREACHED*/
155                 }
156
157
158         } else if (_role == Insert) {
159
160                 /* the output buffers will be filled with data from the *input* ports
161                    of this Insert.
162                 */
163
164                 if (_input) {
165                         if (_input->n_ports() != ChanCount::ZERO) {
166                                 out = _input->n_ports();
167                                 return true;
168                         } else {
169                                 /* not configured yet - we will passthru */
170                                 out = in;
171                                 return true;
172                         }
173                 } else {
174                         fatal << "programming error: this should never be reached" << endmsg;
175                         /*NOTREACHED*/
176                 }
177
178         } else {
179                 fatal << "programming error: this should never be reached" << endmsg;
180         }
181
182         return false;
183 }
184
185 /** Caller must hold process lock */
186 bool
187 Delivery::configure_io (ChanCount in, ChanCount out)
188 {
189         assert (!AudioEngine::instance()->process_lock().trylock());
190
191         /* check configuration by comparison with our I/O port configuration, if appropriate.
192            see ::can_support_io_configuration() for comments
193         */
194
195         if (_role == Main) {
196
197                 if (_output) {
198                         if (_output->n_ports() != out) {
199                                 if (_output->n_ports() != ChanCount::ZERO) {
200                                         _output->ensure_io (out, false, this);
201                                 } else {
202                                         /* I/O not yet configured */
203                                 }
204                         }
205                 }
206
207         } else if (_role == Insert) {
208
209                 if (_input) {
210                         if (_input->n_ports() != in) {
211                                 if (_input->n_ports() != ChanCount::ZERO) {
212                                         fatal << _name << " programming error: configure_io called with " << in << " and " << out << " with " << _input->n_ports() << " input ports" << endmsg;
213                                         /*NOTREACHED*/
214                                 } else {
215                                         /* I/O not yet configured */
216                                 }
217                         }
218                 }
219
220         }
221         
222         if (!Processor::configure_io (in, out)) {
223                 return false;
224         }
225
226         reset_panner ();
227
228         return true;
229 }
230
231 void
232 Delivery::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes, bool result_required)
233 {
234         boost::shared_ptr<Panner> panner;
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, _output_offset);
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                         Amp::apply_simple_gain (bufs, nframes, 0.0);
278                 }
279                 goto out;
280
281         } else if (tgain != 1.0) {
282
283                 /* target gain has not changed, but is not unity */
284                 Amp::apply_simple_gain (bufs, nframes, tgain);
285         }
286
287         panner = _panshell->panner();
288
289         if (panner && !panner->bypassed()) {
290
291                 // Use the panner to distribute audio to output port buffers
292
293                 _panshell->run (bufs, output_buffers(), start_frame, end_frame, nframes);
294
295                 if (result_required) {
296                         bufs.read_from (output_buffers (), nframes);
297                 }
298
299         } else {
300
301                 // Do a 1:1 copy of data to output ports
302
303                 if (bufs.count().n_audio() > 0 && ports.count().n_audio () > 0) {
304                         _output->copy_to_outputs (bufs, DataType::AUDIO, nframes, _output_offset);
305                 }
306
307                 if (bufs.count().n_midi() > 0 && ports.count().n_midi () > 0) {
308                         _output->copy_to_outputs (bufs, DataType::MIDI, nframes, _output_offset);
309                 }
310                 
311                         
312         }
313
314   out:
315         _active = _pending_active;
316 }
317
318 XMLNode&
319 Delivery::state (bool full_state)
320 {
321         XMLNode& node (IOProcessor::state (full_state));
322
323         if (_role & Main) {
324                 node.add_property("type", "main-outs");
325         } else if (_role & Listen) {
326                 node.add_property("type", "listen");
327         } else {
328                 node.add_property("type", "delivery");
329         }
330
331         node.add_property("role", enum_2_string(_role));
332         node.add_child_nocopy (_panshell->state (full_state));
333
334         return node;
335 }
336
337 int
338 Delivery::set_state (const XMLNode& node, int version)
339 {
340         const XMLProperty* prop;
341
342         if (IOProcessor::set_state (node, version)) {
343                 return -1;
344         }
345
346         if ((prop = node.property ("role")) != 0) {
347                 _role = Role (string_2_enum (prop->value(), _role));
348                 // std::cerr << this << ' ' << _name << " set role to " << enum_2_string (_role) << std::endl;
349         } else {
350                 // std::cerr << this << ' ' << _name << " NO ROLE INFO\n";
351         }
352
353         XMLNode* pan_node = node.child (X_("Panner"));
354
355         if (pan_node) {
356                 _panshell->set_state (*pan_node, version);
357         }
358
359         reset_panner ();
360
361         return 0;
362 }
363
364 void
365 Delivery::reset_panner ()
366 {
367         if (_role == Listen) {
368                 /* monitor out gets no panner */
369                 return;
370         }
371
372         if (panners_legal) {
373                 if (!no_panner_reset) {
374
375                         uint32_t ntargets;
376
377                         if (_output) {
378                                 ntargets = _output->n_ports().n_audio();
379                         } else {
380                                 ntargets = _configured_output.n_audio();
381                         }
382
383                         _panshell->configure_io (ChanCount (DataType::AUDIO, pans_required()), ChanCount (DataType::AUDIO, ntargets));
384                 }
385         } else {
386                 panner_legal_c.disconnect ();
387                 PannersLegal.connect_same_thread (panner_legal_c, boost::bind (&Delivery::panners_became_legal, this));
388         }
389 }
390
391 int
392 Delivery::panners_became_legal ()
393 {
394         uint32_t ntargets;
395
396         if (_output) {
397                 ntargets = _output->n_ports().n_audio();
398         } else {
399                 ntargets = _configured_output.n_audio();
400         }
401
402         _panshell->configure_io (ChanCount (DataType::AUDIO, pans_required()), ChanCount (DataType::AUDIO, ntargets));
403         panner_legal_c.disconnect ();
404         return 0;
405 }
406
407 void
408 Delivery::defer_pan_reset ()
409 {
410         no_panner_reset = true;
411 }
412
413 void
414 Delivery::allow_pan_reset ()
415 {
416         no_panner_reset = false;
417         reset_panner ();
418 }
419
420
421 int
422 Delivery::disable_panners (void)
423 {
424         panners_legal = false;
425         return 0;
426 }
427
428 int
429 Delivery::reset_panners ()
430 {
431         panners_legal = true;
432         return *PannersLegal ();
433 }
434
435 void
436 Delivery::flush_buffers (framecnt_t nframes, framepos_t time)
437 {
438         /* io_lock, not taken: function must be called from Session::process() calltree */
439
440         PortSet& ports (_output->ports());
441         
442         for (PortSet::iterator i = ports.begin(); i != ports.end(); ++i) {
443                 (*i).flush_buffers (nframes, time, _output_offset);
444         }
445 }
446
447 void
448 Delivery::transport_stopped (framepos_t now)
449 {
450         Processor::transport_stopped (now);
451         _panshell->pannable()->transport_stopped (now);
452
453         if (_output) {
454                 PortSet& ports (_output->ports());
455                 
456                 for (PortSet::iterator i = ports.begin(); i != ports.end(); ++i) {
457                         (*i).transport_stopped ();
458                 }
459         }
460 }
461
462 gain_t
463 Delivery::target_gain ()
464 {
465         /* if we've been requested to deactivate, our target gain is zero */
466
467         if (!_pending_active) {
468                 return 0.0;
469         }
470
471         /* if we've been told not to output because its a monitoring situation and
472            we're not monitoring, then be quiet.
473         */
474
475         if (_no_outs_cuz_we_no_monitor) {
476                 return 0.0;
477         }
478
479         MuteMaster::MutePoint mp = MuteMaster::Main; // stupid gcc uninit warning
480         
481         switch (_role) {
482         case Main:
483                 mp = MuteMaster::Main;
484                 break;
485         case Listen:
486                 mp = MuteMaster::Listen;
487                 break;
488         case Send:
489         case Insert:
490         case Aux:
491                 if (_pre_fader) {
492                         mp = MuteMaster::PreFader;
493                 } else {
494                         mp = MuteMaster::PostFader;
495                 }
496                 break;
497         }
498
499         gain_t desired_gain = _mute_master->mute_gain_at (mp);
500         
501         if (_role == Listen && _session.monitor_out() && !_session.listening()) {
502                 
503                 /* nobody is soloed, and this delivery is a listen-send to the
504                    control/monitor/listen bus, we should be silent since
505                    it gets its signal from the master out.
506                 */
507                 
508                 desired_gain = 0.0;
509                 
510         } 
511
512         return desired_gain;
513 }
514
515 void
516 Delivery::no_outs_cuz_we_no_monitor (bool yn)
517 {
518         _no_outs_cuz_we_no_monitor = yn;
519 }
520
521 bool
522 Delivery::set_name (const std::string& name)
523 {
524         bool ret = IOProcessor::set_name (name);
525
526         if (ret) {
527                 ret = _panshell->set_name (name);
528         }
529
530         return ret;
531 }
532
533 void
534 Delivery::output_changed (IOChange change, void* /*src*/)
535 {
536         if (change.type & IOChange::ConfigurationChanged) {
537                 reset_panner ();
538                 _output_buffers->attach_buffers (_output->ports ());
539         }
540 }
541
542 boost::shared_ptr<Panner>
543 Delivery::panner () const
544 {
545         return _panshell->panner();
546 }