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