f8bba4c8e71bf58ca054971d2779c3eb2b332d63
[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>(), (r == Listen ? boost::shared_ptr<IO>() : 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, (r == Listen ? false : true), 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 void
149 Delivery::cycle_start (nframes_t nframes)
150 {
151         _output_offset = 0;
152         _no_outs_cuz_we_no_monitor = false;
153 }
154
155 void
156 Delivery::increment_output_offset (nframes_t n)
157 {
158         _output_offset += n;
159 }
160
161 bool
162 Delivery::visible () const
163 {
164         if (_role & Main) {
165                 return false;
166         }
167
168         return true;
169 }
170
171 bool
172 Delivery::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
173 {
174         out = in;
175         return true;
176 }
177
178 bool
179 Delivery::configure_io (ChanCount in, ChanCount out)
180 {
181         if (out != in) { // always 1:1
182                 return false;
183         }
184         
185         if (!Processor::configure_io (in, out)) {
186                 return false;
187         }
188
189         reset_panner ();
190
191         return true;
192 }
193
194 void
195 Delivery::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
196 {
197         if (_output->n_ports ().get (_output->default_type()) == 0) {
198                 return;
199         }
200
201         /* this setup is not just for our purposes, but for anything that comes after us in the 
202            processing pathway that wants to use this->output_buffers() for some reason.
203         */
204
205         PortSet& ports (_output->ports());
206         output_buffers().attach_buffers (ports, nframes, _output_offset);
207
208         // this Delivery processor is not a derived type, and thus we assume
209         // we really can modify the buffers passed in (it is almost certainly
210         // the main output stage of a Route). Contrast with Send::run()
211         // which cannot do this.
212
213         gain_t tgain = target_gain ();
214         
215         if (tgain != _current_gain) {
216                 
217                 /* target gain has changed */
218
219                 Amp::apply_gain (bufs, nframes, _current_gain, tgain);
220                 _current_gain = tgain;
221
222         } else if (tgain == 0.0) {
223
224                 /* we were quiet last time, and we're still supposed to be quiet.
225                    Silence the outputs, and make sure the buffers are quiet too,
226                 */
227
228                 _output->silence (nframes);
229                 Amp::apply_simple_gain (bufs, nframes, 0.0);
230                 
231                 return;
232
233         } else if (tgain != 1.0) {
234
235                 /* target gain has not changed, but is not unity */
236                 Amp::apply_simple_gain (bufs, nframes, tgain);
237         }
238
239         // Attach output buffers to port buffers
240
241         if (_panner && _panner->npanners() && !_panner->bypassed()) {
242
243                 // Use the panner to distribute audio to output port buffers
244
245                 _panner->run (bufs, output_buffers(), start_frame, end_frame, nframes);
246
247         } else {
248                 // Do a 1:1 copy of data to output ports
249
250                 if (bufs.count().n_audio() > 0 && ports.count().n_audio () > 0) {
251                         _output->copy_to_outputs (bufs, DataType::AUDIO, nframes, _output_offset);
252                 }
253
254                 if (bufs.count().n_midi() > 0 && ports.count().n_midi () > 0) {
255                         _output->copy_to_outputs (bufs, DataType::MIDI, nframes, _output_offset);
256                 }
257         }
258 }
259
260
261 XMLNode&
262 Delivery::state (bool full_state)
263 {
264         XMLNode& node (IOProcessor::state (full_state));
265
266         if (_role & Main) {
267                 node.add_property("type", "main-outs");
268         } else if (_role & Listen) {
269                 node.add_property("type", "listen");
270         } else {
271                 node.add_property("type", "delivery");
272         }
273
274         node.add_property("role", enum_2_string(_role));
275         node.add_child_nocopy (_panner->state (full_state));
276
277         return node;
278 }
279
280 int
281 Delivery::set_state (const XMLNode& node)
282 {
283         const XMLProperty* prop;
284
285         if (IOProcessor::set_state (node)) {
286                 return -1;
287         }
288         
289         if ((prop = node.property ("role")) != 0) {
290                 _role = Role (string_2_enum (prop->value(), _role));
291                 // std::cerr << this << ' ' << _name << " set role to " << enum_2_string (_role) << std::endl;
292         } else {
293                 // std::cerr << this << ' ' << _name << " NO ROLE INFO\n";
294         }
295
296         XMLNode* pan_node = node.child (X_("Panner"));
297         
298         if (pan_node) {
299                 _panner->set_state (*pan_node);
300         } 
301
302         reset_panner ();
303
304         return 0;
305 }
306
307 void
308 Delivery::reset_panner ()
309 {
310         if (panners_legal) {
311                 if (!no_panner_reset) {
312
313                         uint32_t ntargets;
314                         
315                         if (_output) {
316                                 ntargets = _output->n_ports().n_audio();
317                         } else {
318                                 ntargets = _configured_output.n_audio();
319                         }
320
321                         _panner->reset (ntargets, pans_required());
322                 }
323         } else {
324                 panner_legal_c.disconnect ();
325                 panner_legal_c = PannersLegal.connect (mem_fun (*this, &Delivery::panners_became_legal));
326         }
327 }
328
329 int
330 Delivery::panners_became_legal ()
331 {
332         uint32_t ntargets;
333
334         if (_output) {
335                 ntargets = _output->n_ports().n_audio();
336         } else {
337                 ntargets = _configured_output.n_audio();
338         }
339
340         _panner->reset (ntargets, pans_required());
341         _panner->load (); // automation
342         panner_legal_c.disconnect ();
343         return 0;
344 }
345
346 void
347 Delivery::defer_pan_reset ()
348 {
349         no_panner_reset = true;
350 }
351
352 void
353 Delivery::allow_pan_reset ()
354 {
355         no_panner_reset = false;
356         reset_panner ();
357 }
358
359
360 int
361 Delivery::disable_panners (void)
362 {
363         panners_legal = false;
364         return 0;
365 }
366
367 int
368 Delivery::reset_panners ()
369 {
370         panners_legal = true;
371         return PannersLegal ();
372 }
373
374
375 void
376 Delivery::start_pan_touch (uint32_t which)
377 {
378         if (which < _panner->npanners()) {
379                 _panner->pan_control(which)->start_touch();
380         }
381 }
382
383 void
384 Delivery::end_pan_touch (uint32_t which)
385 {
386         if (which < _panner->npanners()) {
387                 _panner->pan_control(which)->stop_touch();
388         }
389
390 }
391
392 void
393 Delivery::transport_stopped (sframes_t frame)
394 {
395         _panner->transport_stopped (frame);
396 }
397
398 void
399 Delivery::flush (nframes_t nframes)
400 {
401         /* io_lock, not taken: function must be called from Session::process() calltree */
402         
403         PortSet& ports (_output->ports());
404
405         for (PortSet::iterator i = ports.begin(); i != ports.end(); ++i) {
406                 (*i).flush_buffers (nframes, _output_offset);
407         }
408 }
409
410 gain_t
411 Delivery::target_gain ()
412 {
413         /* if we've been told not to output because its a monitoring situation and
414            we're not monitoring, then be quiet.
415         */
416
417         if (_no_outs_cuz_we_no_monitor) {
418                 return 0.0;
419         }
420
421         gain_t desired_gain;
422
423         if (_solo_level) {
424                 desired_gain = 1.0;
425         } else {
426
427                 MuteMaster::MutePoint mp;
428                 
429                 switch (_role) {
430                 case Main:
431                         mp = MuteMaster::Main;
432                         break;
433                 case Listen:
434                         mp = MuteMaster::Listen;
435                         break;
436                 case Send:
437                 case Insert:
438                         /* XXX FIX ME this is wrong, we need per-delivery muting */
439                         mp = MuteMaster::PreFader;
440                         break;
441                 }
442
443                 if (_solo_isolated) {
444                 
445                         /* ... but we are isolated from all that nonsense */
446                         
447                         desired_gain = _mute_master->mute_gain_at (mp);
448
449                 } else if (_session.soloing()) {
450
451                         desired_gain = min (Config->get_solo_mute_gain(), _mute_master->mute_gain_at (mp));
452
453                 } else {
454                         desired_gain = _mute_master->mute_gain_at (mp);
455                 }
456         }
457
458         return desired_gain;
459 }
460
461 void
462 Delivery::no_outs_cuz_we_no_monitor (bool yn)
463 {
464         _no_outs_cuz_we_no_monitor = yn;
465 }
466
467 bool
468 Delivery::set_name (const std::string& name)
469 {
470         bool ret = IOProcessor::set_name (name);
471
472         if (ret) {
473                 ret = _panner->set_name (name);
474         }
475
476         return ret;
477 }
478
479 void
480 Delivery::output_changed (IOChange change, void* src)
481 {
482         if (change & ARDOUR::ConfigurationChanged) {
483                 reset_panner ();
484         }
485 }
486