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