01ff226b5e6676509c8f9883eda61d4e85306af3
[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/delivery.h"
26 #include "ardour/audio_buffer.h"
27 #include "ardour/amp.h"
28 #include "ardour/buffer_set.h"
29 #include "ardour/configuration.h"
30 #include "ardour/io.h"
31 #include "ardour/meter.h"
32 #include "ardour/mute_master.h"
33 #include "ardour/panner.h"
34 #include "ardour/port.h"
35 #include "ardour/session.h"
36
37 #include "i18n.h"
38
39 using namespace std;
40 using namespace PBD;
41 using namespace ARDOUR;
42
43 sigc::signal<void,nframes_t> Delivery::CycleStart;
44 sigc::signal<int>            Delivery::PannersLegal;
45 bool                         Delivery::panners_legal = false;
46
47 /* deliver to an existing IO object */
48
49 Delivery::Delivery (Session& s, boost::shared_ptr<IO> io, boost::shared_ptr<MuteMaster> mm, const string& name, Role r)
50         : IOProcessor(s, boost::shared_ptr<IO>(), (role_requires_output_ports (r) ? io : boost::shared_ptr<IO>()), name)
51         , _role (r)
52         , _output_buffers (new BufferSet())
53         , _current_gain (1.0)
54         , _output_offset (0)
55         , _no_outs_cuz_we_no_monitor (false)
56         , _solo_level (0)
57         , _solo_isolated (false)
58         , _mute_master (mm)
59         , no_panner_reset (false)
60
61 {
62         _panner = boost::shared_ptr<Panner>(new Panner (_name, _session));
63         
64         if (_output) {
65                 _output->changed.connect (mem_fun (*this, &Delivery::output_changed));
66         }
67
68         CycleStart.connect (mem_fun (*this, &Delivery::cycle_start));
69 }
70
71 /* deliver to a new IO object */
72
73 Delivery::Delivery (Session& s, boost::shared_ptr<MuteMaster> mm, const string& name, Role r)
74         : IOProcessor(s, false, (role_requires_output_ports (r) ? true : false), name)
75         , _role (r)
76         , _output_buffers (new BufferSet())
77         , _current_gain (1.0)
78         , _output_offset (0)
79         , _no_outs_cuz_we_no_monitor (false)
80         , _solo_level (0)
81         , _solo_isolated (false)
82         , _mute_master (mm)
83         , no_panner_reset (false)
84 {
85         _panner = boost::shared_ptr<Panner>(new Panner (_name, _session));
86
87         if (_output) {
88                 _output->changed.connect (mem_fun (*this, &Delivery::output_changed));
89         }
90
91         CycleStart.connect (mem_fun (*this, &Delivery::cycle_start));
92 }
93
94 /* deliver to a new IO object, reconstruct from XML */
95
96 Delivery::Delivery (Session& s, boost::shared_ptr<MuteMaster> mm, const XMLNode& node)
97         : IOProcessor (s, false, true, "reset")
98         , _role (Role (0))
99         , _output_buffers (new BufferSet())
100         , _current_gain (1.0)
101         , _output_offset (0)
102         , _no_outs_cuz_we_no_monitor (false)
103         , _solo_level (0)
104         , _solo_isolated (false)
105         , _mute_master (mm)
106         , no_panner_reset (false)
107 {
108         _panner = boost::shared_ptr<Panner>(new Panner (_name, _session));
109
110         if (set_state (node)) {
111                 throw failed_constructor ();
112         }
113
114         if (_output) {
115                 _output->changed.connect (mem_fun (*this, &Delivery::output_changed));
116         }
117
118         CycleStart.connect (mem_fun (*this, &Delivery::cycle_start));
119 }
120
121 /* deliver to an existing IO object, reconstruct from XML */
122
123 Delivery::Delivery (Session& s, boost::shared_ptr<IO> out, boost::shared_ptr<MuteMaster> mm, const XMLNode& node)
124         : IOProcessor (s, boost::shared_ptr<IO>(), out, "reset")
125         , _role (Role (0))
126         , _output_buffers (new BufferSet())
127         , _current_gain (1.0)
128         , _output_offset (0)
129         , _no_outs_cuz_we_no_monitor (false)
130         , _solo_level (0)
131         , _solo_isolated (false)
132         , _mute_master (mm)
133         , no_panner_reset (false)
134 {
135         _panner = boost::shared_ptr<Panner>(new Panner (_name, _session));
136
137         if (set_state (node)) {
138                 throw failed_constructor ();
139         }
140
141         if (_output) {
142                 _output->changed.connect (mem_fun (*this, &Delivery::output_changed));
143         }
144
145         CycleStart.connect (mem_fun (*this, &Delivery::cycle_start));
146 }
147
148 std::string
149 Delivery::display_name () const
150 {
151         switch (_role) {
152         case Main:
153                 return _("main outs");
154                 break;
155         case Listen:
156                 return _("listen");
157                 break;
158         case Send:
159         case Insert:
160         default:
161                 return name();
162         }
163 }
164
165 void
166 Delivery::cycle_start (nframes_t /*nframes*/)
167 {
168         _output_offset = 0;
169         _no_outs_cuz_we_no_monitor = false;
170 }
171
172 void
173 Delivery::increment_output_offset (nframes_t n)
174 {
175         _output_offset += n;
176 }
177
178 bool
179 Delivery::visible () const
180 {
181         return true;
182 }
183
184 bool
185 Delivery::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
186 {
187         if (_role == Main) {
188
189                 /* the out buffers will be set to point to the port output buffers
190                    of our output object.
191                 */
192
193                 if (_output) { 
194                         if (_output->n_ports() != ChanCount::ZERO) {
195                                 out = _output->n_ports();
196                                 return true;
197                         } else {
198                                 /* not configured yet - we will passthru */
199                                 out = in;
200                                 return true;
201                         }
202                 } else {
203                         fatal << "programming error: this should never be reached" << endmsg;
204                         /*NOTREACHED*/
205                 }
206
207
208         } else if (_role == Insert) {
209
210                 /* the output buffers will be filled with data from the *input* ports
211                    of this Insert. 
212                 */
213
214                 if (_input) { 
215                         if (_input->n_ports() != ChanCount::ZERO) {
216                                 out = _input->n_ports();
217                                 return true;
218                         } else {
219                                 /* not configured yet - we will passthru */
220                                 out = in;
221                                 return true;
222                         }
223                 } else {
224                         fatal << "programming error: this should never be reached" << endmsg;
225                         /*NOTREACHED*/
226                 }
227
228         } else {
229                 fatal << "programming error: this should never be reached" << endmsg;
230         }
231
232         return false;
233 }
234
235 bool
236 Delivery::configure_io (ChanCount in, ChanCount out)
237 {
238         /* check configuration by comparison with our I/O port configuration, if appropriate.
239            see ::can_support_io_configuration() for comments 
240         */
241
242         if (_role == Main) {
243
244                 if (_output) { 
245                         if (_output->n_ports() != out) {
246                                 if (_output->n_ports() != ChanCount::ZERO) {
247                                         fatal << _name << " programming error: configure_io with nports = " << _output->n_ports() << " called with " << in << " and " << out << " with " << _output->n_ports() << " output ports" << endmsg;
248                                         /*NOTREACHED*/
249                                 } else {
250                                         /* I/O not yet configured */
251                                 }
252                         } 
253                 }
254
255         } else if (_role == Insert) {
256
257                 if (_input) { 
258                         if (_input->n_ports() != in) {
259                                 if (_input->n_ports() != ChanCount::ZERO) {
260                                         fatal << _name << " programming error: configure_io called with " << in << " and " << out << " with " << _input->n_ports() << " input ports" << endmsg;
261                                         /*NOTREACHED*/
262                                 } else {
263                                         /* I/O not yet configured */
264                                 }
265                         }
266                 }
267         }
268
269         if (!Processor::configure_io (in, out)) {
270                 return false;
271         }
272
273         reset_panner ();
274
275         return true;
276 }
277
278 void
279 Delivery::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
280 {
281         assert (_output);
282
283         PortSet& ports (_output->ports());
284         gain_t tgain;
285
286         if (_output->n_ports ().get (_output->default_type()) == 0) {
287                 goto out;
288         }
289
290         if (!_active && !_pending_active) {
291                 _output->silence (nframes);
292                 goto out;
293         }
294
295         /* this setup is not just for our purposes, but for anything that comes after us in the 
296            processing pathway that wants to use this->output_buffers() for some reason.
297         */
298
299         output_buffers().attach_buffers (ports, nframes, _output_offset);
300
301         // this Delivery processor is not a derived type, and thus we assume
302         // we really can modify the buffers passed in (it is almost certainly
303         // the main output stage of a Route). Contrast with Send::run()
304         // which cannot do this.
305
306         tgain = target_gain ();
307         
308         if (tgain != _current_gain) {
309                 
310                 /* target gain has changed */
311
312                 Amp::apply_gain (bufs, nframes, _current_gain, tgain);
313                 _current_gain = tgain;
314
315         } else if (tgain == 0.0) {
316
317                 /* we were quiet last time, and we're still supposed to be quiet.
318                    Silence the outputs, and make sure the buffers are quiet too,
319                 */
320                 
321                 _output->silence (nframes);
322                 Amp::apply_simple_gain (bufs, nframes, 0.0);
323                 goto out;
324
325         } else if (tgain != 1.0) {
326
327                 /* target gain has not changed, but is not unity */
328                 Amp::apply_simple_gain (bufs, nframes, tgain);
329         }
330
331         if (_panner && _panner->npanners() && !_panner->bypassed()) {
332
333                 // Use the panner to distribute audio to output port buffers
334
335                 _panner->run (bufs, output_buffers(), start_frame, end_frame, nframes);
336
337         } else {
338                 // Do a 1:1 copy of data to output ports
339
340                 if (bufs.count().n_audio() > 0 && ports.count().n_audio () > 0) {
341                         _output->copy_to_outputs (bufs, DataType::AUDIO, nframes, _output_offset);
342                 }
343
344                 if (bufs.count().n_midi() > 0 && ports.count().n_midi () > 0) {
345                         _output->copy_to_outputs (bufs, DataType::MIDI, nframes, _output_offset);
346                 }
347         }
348
349   out:
350         _active = _pending_active;
351 }
352
353 XMLNode&
354 Delivery::state (bool full_state)
355 {
356         XMLNode& node (IOProcessor::state (full_state));
357
358         if (_role & Main) {
359                 node.add_property("type", "main-outs");
360         } else if (_role & Listen) {
361                 node.add_property("type", "listen");
362         } else {
363                 node.add_property("type", "delivery");
364         }
365
366         node.add_property("role", enum_2_string(_role));
367         node.add_child_nocopy (_panner->state (full_state));
368
369         return node;
370 }
371
372 int
373 Delivery::set_state (const XMLNode& node)
374 {
375         const XMLProperty* prop;
376
377         if (IOProcessor::set_state (node)) {
378                 return -1;
379         }
380         
381         if ((prop = node.property ("role")) != 0) {
382                 _role = Role (string_2_enum (prop->value(), _role));
383                 // std::cerr << this << ' ' << _name << " set role to " << enum_2_string (_role) << std::endl;
384         } else {
385                 // std::cerr << this << ' ' << _name << " NO ROLE INFO\n";
386         }
387
388         XMLNode* pan_node = node.child (X_("Panner"));
389         
390         if (pan_node) {
391                 _panner->set_state (*pan_node);
392         } 
393
394         reset_panner ();
395
396         return 0;
397 }
398
399 void
400 Delivery::reset_panner ()
401 {
402         if (panners_legal) {
403                 if (!no_panner_reset) {
404
405                         uint32_t ntargets;
406                         
407                         if (_output) {
408                                 ntargets = _output->n_ports().n_audio();
409                         } else {
410                                 ntargets = _configured_output.n_audio();
411                         }
412
413                         _panner->reset (ntargets, pans_required());
414                 }
415         } else {
416                 panner_legal_c.disconnect ();
417                 panner_legal_c = PannersLegal.connect (mem_fun (*this, &Delivery::panners_became_legal));
418         }
419 }
420
421 int
422 Delivery::panners_became_legal ()
423 {
424         uint32_t ntargets;
425
426         if (_output) {
427                 ntargets = _output->n_ports().n_audio();
428         } else {
429                 ntargets = _configured_output.n_audio();
430         }
431
432         _panner->reset (ntargets, pans_required());
433         _panner->load (); // automation
434         panner_legal_c.disconnect ();
435         return 0;
436 }
437
438 void
439 Delivery::defer_pan_reset ()
440 {
441         no_panner_reset = true;
442 }
443
444 void
445 Delivery::allow_pan_reset ()
446 {
447         no_panner_reset = false;
448         reset_panner ();
449 }
450
451
452 int
453 Delivery::disable_panners (void)
454 {
455         panners_legal = false;
456         return 0;
457 }
458
459 int
460 Delivery::reset_panners ()
461 {
462         panners_legal = true;
463         return PannersLegal ();
464 }
465
466
467 void
468 Delivery::start_pan_touch (uint32_t which)
469 {
470         if (which < _panner->npanners()) {
471                 _panner->pan_control(which)->start_touch();
472         }
473 }
474
475 void
476 Delivery::end_pan_touch (uint32_t which)
477 {
478         if (which < _panner->npanners()) {
479                 _panner->pan_control(which)->stop_touch();
480         }
481
482 }
483
484 void
485 Delivery::transport_stopped (sframes_t frame)
486 {
487         _panner->transport_stopped (frame);
488 }
489
490 void
491 Delivery::flush (nframes_t nframes)
492 {
493         /* io_lock, not taken: function must be called from Session::process() calltree */
494         
495         PortSet& ports (_output->ports());
496
497         for (PortSet::iterator i = ports.begin(); i != ports.end(); ++i) {
498                 (*i).flush_buffers (nframes, _output_offset);
499         }
500 }
501
502 gain_t
503 Delivery::target_gain ()
504 {
505         /* if we've been requested to deactivate, our target gain is zero */
506
507         if (!_pending_active) {
508                 return 0.0;
509         }
510
511         /* if we've been told not to output because its a monitoring situation and
512            we're not monitoring, then be quiet.
513         */
514
515         if (_no_outs_cuz_we_no_monitor) {
516                 return 0.0;
517         }
518
519         gain_t desired_gain;
520
521         if (_solo_level) {
522                 desired_gain = 1.0;
523         } else {
524
525                 MuteMaster::MutePoint mp;
526                 
527                 switch (_role) {
528                 case Main:
529                         mp = MuteMaster::Main;
530                         break;
531                 case Listen:
532                         mp = MuteMaster::Listen;
533                         break;
534                 case Send:
535                 case Insert:
536                 case Aux:
537                         /* XXX FIX ME this is wrong, we need per-delivery muting */
538                         mp = MuteMaster::PreFader;
539                         break;
540                 }
541
542                 if (_solo_isolated) {
543                 
544                         /* ... but we are isolated from all that nonsense */
545                         
546                         desired_gain = _mute_master->mute_gain_at (mp);
547
548                 } else if (_session.soloing()) {
549
550                         desired_gain = min (Config->get_solo_mute_gain(), _mute_master->mute_gain_at (mp));
551
552                 } else {
553                         desired_gain = _mute_master->mute_gain_at (mp);
554                 }
555         }
556
557         return desired_gain;
558 }
559
560 void
561 Delivery::no_outs_cuz_we_no_monitor (bool yn)
562 {
563         _no_outs_cuz_we_no_monitor = yn;
564 }
565
566 bool
567 Delivery::set_name (const std::string& name)
568 {
569         bool ret = IOProcessor::set_name (name);
570
571         if (ret) {
572                 ret = _panner->set_name (name);
573         }
574
575         return ret;
576 }
577
578 void
579 Delivery::output_changed (IOChange change, void* /*src*/)
580 {
581         if (change & ARDOUR::ConfigurationChanged) {
582                 reset_panner ();
583         }
584 }
585