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