the big Route structure refactor. !!!! THIS WILL ***NOT LOAD*** PRIOR 3.0 or 2.X...
[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         // this Delivery processor is not a derived type, and thus we assume
152         // we really can modify the buffers passed in (it is almost certainly
153         // the main output stage of a Route). Contrast with Send::run_in_place()
154         // which cannot do this.
155
156         gain_t tgain = target_gain ();
157         
158         if (tgain != _current_gain) {
159                 Amp::apply_gain (bufs, nframes, _current_gain, tgain);
160                 _current_gain = tgain;
161         }
162
163         // Attach output buffers to port buffers
164
165         PortSet& ports (_output->ports());
166         output_buffers().attach_buffers (ports, nframes, _output_offset);
167
168         if (_panner && _panner->npanners() && !_panner->bypassed()) {
169
170                 // Use the panner to distribute audio to output port buffers
171
172                 _panner->run (bufs, output_buffers(), start_frame, end_frame, nframes);
173
174         } else {
175                 // Do a 1:1 copy of data to output ports
176
177                 if (bufs.count().n_audio() > 0 && ports.count().n_audio () > 0) {
178                         _output->copy_to_outputs (bufs, DataType::AUDIO, nframes, _output_offset);
179                 }
180
181                 if (bufs.count().n_midi() > 0 && ports.count().n_midi () > 0) {
182                         _output->copy_to_outputs (bufs, DataType::MIDI, nframes, _output_offset);
183                 }
184         }
185 }
186
187
188 XMLNode&
189 Delivery::state (bool full_state)
190 {
191         XMLNode& node (IOProcessor::state (full_state));
192
193         if (_role & Main) {
194                 node.add_property("type", "main-outs");
195         } else if (_role & Listen) {
196                 node.add_property("type", "listen");
197         } else {
198                 node.add_property("type", "delivery");
199         }
200
201         node.add_property("role", enum_2_string(_role));
202         node.add_child_nocopy (_panner->state (full_state));
203
204         return node;
205 }
206
207 int
208 Delivery::set_state (const XMLNode& node)
209 {
210         const XMLProperty* prop;
211
212         if (IOProcessor::set_state (node)) {
213                 return -1;
214         }
215         
216         if ((prop = node.property ("role")) != 0) {
217                 _role = Role (string_2_enum (prop->value(), _role));
218         }
219
220         if ((prop = node.property ("solo_level")) != 0) {
221                 _solo_level = 0; // needed for the reset to work
222                 mod_solo_level (atoi (prop->value()));
223         }
224
225         if ((prop = node.property ("solo-isolated")) != 0) {
226                 set_solo_isolated (prop->value() == "yes");
227         }
228
229         XMLNode* pan_node = node.child (X_("Panner"));
230         
231         if (pan_node) {
232                 cerr << _name << " reset pan state from XML\n";
233                 _panner->set_state (*pan_node);
234         } 
235
236         reset_panner ();
237
238         return 0;
239 }
240
241 void
242 Delivery::reset_panner ()
243 {
244         cerr << _name << " reset panner - plegal ? " << panners_legal << endl;
245
246         if (panners_legal) {
247                 if (!no_panner_reset) {
248                         cerr << "\treset panner with " << _output->name() << " = " << _output->n_ports()
249                              << " vs. " << pans_required () << endl;
250                         _panner->reset (_output->n_ports().n_audio(), pans_required());
251                 }
252         } else {
253                 cerr << "\tdefer pan reset till later\n";
254                 panner_legal_c.disconnect ();
255                 panner_legal_c = PannersLegal.connect (mem_fun (*this, &Delivery::panners_became_legal));
256         }
257 }
258
259 int
260 Delivery::panners_became_legal ()
261 {
262         cerr << _name << " panners now legal, outputs @ " << _output << " on " << _output->name()
263              << " = " << _output->n_ports() << " vs. " << pans_required() << endl;
264         _panner->reset (_output->n_ports().n_audio(), pans_required());
265         _panner->load (); // automation
266         panner_legal_c.disconnect ();
267         return 0;
268 }
269
270 void
271 Delivery::defer_pan_reset ()
272 {
273         no_panner_reset = true;
274 }
275
276 void
277 Delivery::allow_pan_reset ()
278 {
279         no_panner_reset = false;
280         reset_panner ();
281 }
282
283
284 int
285 Delivery::disable_panners (void)
286 {
287         panners_legal = false;
288         return 0;
289 }
290
291 int
292 Delivery::reset_panners ()
293 {
294         panners_legal = true;
295         return PannersLegal ();
296 }
297
298
299 void
300 Delivery::start_pan_touch (uint32_t which)
301 {
302         if (which < _panner->npanners()) {
303                 _panner->pan_control(which)->start_touch();
304         }
305 }
306
307 void
308 Delivery::end_pan_touch (uint32_t which)
309 {
310         if (which < _panner->npanners()) {
311                 _panner->pan_control(which)->stop_touch();
312         }
313
314 }
315
316 void
317 Delivery::transport_stopped (sframes_t frame)
318 {
319         _panner->transport_stopped (frame);
320 }
321
322 void
323 Delivery::flush (nframes_t nframes)
324 {
325         /* io_lock, not taken: function must be called from Session::process() calltree */
326         
327         PortSet& ports (_output->ports());
328
329         for (PortSet::iterator i = ports.begin(); i != ports.end(); ++i) {
330                 (*i).flush_buffers (nframes, _output_offset);
331         }
332 }
333
334 gain_t
335 Delivery::target_gain ()
336 {
337         /* if we've been told not to output because its a monitoring situation and
338            we're not monitoring, then be quiet.
339         */
340
341         if (_no_outs_cuz_we_no_monitor) {
342                 return 0.0;
343         }
344
345         gain_t desired_gain;
346         MuteMaster::MutePoint mp;
347
348         if (_solo_level) {
349                 desired_gain = 1.0;
350         } else {
351                 if (_solo_isolated) {
352
353                         switch (_role) {
354                         case Main:
355                                 mp = MuteMaster::Main;
356                                 break;
357                         case Listen:
358                                 mp = MuteMaster::Listen;
359                                 break;
360                         case Send:
361                         case Insert:
362                                 if (_placement == PreFader) {
363                                         mp = MuteMaster::PreFader;
364                                 } else {
365                                         mp = MuteMaster::PostFader;
366                                 }
367                                 break;
368                         }
369
370                         desired_gain = _mute_master->mute_gain_at (mp);
371                 } else if (_session.soloing()) {
372
373                         switch (_role) {
374                         case Main:
375                                 mp = MuteMaster::Main;
376                                 break;
377                         case Listen:
378                                 mp = MuteMaster::Listen;
379                                 break;
380                         case Send:
381                         case Insert:
382                                 if (_placement == PreFader) {
383                                         mp = MuteMaster::PreFader;
384                                 } else {
385                                         mp = MuteMaster::PostFader;
386                                 }
387                                 break;
388                         }
389
390                         desired_gain = min (Config->get_solo_mute_gain(), _mute_master->mute_gain_at (mp));
391                 } else {
392                         desired_gain = 1.0;
393                 }
394         }
395
396         return desired_gain;
397 }
398
399 void
400 Delivery::mod_solo_level (int32_t delta)
401 {
402         if (delta < 0) {
403                 if (_solo_level >= (uint32_t) delta) {
404                         _solo_level += delta;
405                 } else {
406                         _solo_level = 0;
407                 }
408         } else {
409                 _solo_level += delta;
410         }
411 }
412
413 void
414 Delivery::set_solo_isolated (bool yn)
415 {
416         _solo_isolated = yn;
417 }
418
419 void
420 Delivery::no_outs_cuz_we_no_monitor (bool yn)
421 {
422         _no_outs_cuz_we_no_monitor = yn;
423 }
424
425 bool
426 Delivery::set_name (const std::string& name)
427 {
428         bool ret = IOProcessor::set_name (name);
429
430         if (ret) {
431                 ret = _panner->set_name (name);
432         }
433
434         return ret;
435 }
436
437 void
438 Delivery::output_changed (IOChange change, void* src)
439 {
440         if (change & ARDOUR::ConfigurationChanged) {
441                 reset_panner ();
442         }
443 }