rework MIDI [processor|plugin] chain
[ardour.git] / libs / ardour / monitor_processor.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "pbd/convert.h"
21 #include "pbd/error.h"
22 #include "pbd/locale_guard.h"
23 #include "pbd/xml++.h"
24
25 #include "ardour/amp.h"
26 #include "ardour/debug.h"
27 #include "ardour/audio_buffer.h"
28 #include "ardour/monitor_processor.h"
29 #include "ardour/session.h"
30
31 #include "i18n.h"
32
33 using namespace ARDOUR;
34 using namespace PBD;
35 using namespace std;
36
37 /* specialize for bool because of set_value() semantics */
38
39 namespace ARDOUR {
40         template<> void MPControl<bool>::set_value (double v) {
41                 bool newval = fabs (v) >= 0.5;
42                 if (newval != _value) {
43                         _value = newval;
44                         Changed(); /* EMIT SIGNAL */
45                 }
46         }
47 }
48
49 MonitorProcessor::MonitorProcessor (Session& s)
50         : Processor (s, X_("MonitorOut"))
51         , solo_cnt (0)
52
53         , _dim_all_ptr (new MPControl<bool> (false, _("monitor dim"), Controllable::Toggle))
54         , _cut_all_ptr (new MPControl<bool> (false, _("monitor cut"), Controllable::Toggle))
55         , _mono_ptr (new MPControl<bool> (false, _("monitor mono"), Controllable::Toggle))
56         , _dim_level_ptr (new MPControl<volatile gain_t> 
57                           /* default is -12dB, range is -20dB to 0dB */
58                           (dB_to_coefficient(-12.0), _("monitor dim level"), Controllable::Flag (0), 
59                            dB_to_coefficient(-20.0), dB_to_coefficient (0.0)))
60         , _solo_boost_level_ptr (new MPControl<volatile gain_t> 
61                                  /* default is 0dB, range is 0dB to +20dB */
62                                  (dB_to_coefficient(0.0), _("monitor solo boost level"), Controllable::Flag (0), 
63                                   dB_to_coefficient(0.0), dB_to_coefficient(10.0)))
64         , _dim_all_control (_dim_all_ptr)
65         , _cut_all_control (_cut_all_ptr)
66         , _mono_control (_mono_ptr)
67         , _dim_level_control (_dim_level_ptr)
68         , _solo_boost_level_control (_solo_boost_level_ptr)
69
70         , _dim_all (*_dim_all_ptr)
71         , _cut_all (*_cut_all_ptr)
72         , _mono (*_mono_ptr)
73         , _dim_level (*_dim_level_ptr)
74         , _solo_boost_level (*_solo_boost_level_ptr)
75
76 {
77 }
78
79 MonitorProcessor::~MonitorProcessor ()
80 {
81         allocate_channels (0);
82 }
83
84 void
85 MonitorProcessor::allocate_channels (uint32_t size)
86 {
87         while (_channels.size() > size) {
88                 if (_channels.back()->soloed) {
89                         if (solo_cnt > 0) {
90                                 --solo_cnt;
91                         }
92                 }
93                 ChannelRecord* cr = _channels.back();
94                 _channels.pop_back();
95                 delete cr;
96         }
97
98         uint32_t n = _channels.size() + 1;
99
100         while (_channels.size() < size) {
101                 _channels.push_back (new ChannelRecord (n));
102         }
103 }
104
105 int
106 MonitorProcessor::set_state (const XMLNode& node, int version)
107 {
108         int ret = Processor::set_state (node, version);
109
110         if (ret != 0) {
111                 return ret;
112         }
113
114         const XMLProperty* prop;
115
116         if ((prop = node.property (X_("type"))) == 0) {
117                 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings have no type information"))
118                       << endmsg;
119                 return -1;
120         }
121
122         if (prop->value() != X_("monitor")) {
123                 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor given unknown XML settings"))
124                       << endmsg;
125                 return -1;
126         }
127
128         if ((prop = node.property (X_("channels"))) == 0) {
129                 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings are missing a channel cnt"))
130                       << endmsg;
131                 return -1;
132         }
133
134         allocate_channels (atoi (prop->value()));
135
136         if ((prop = node.property (X_("dim-level"))) != 0) {
137                 gain_t val = atof (prop->value());
138                 _dim_level = val;
139         }
140
141         if ((prop = node.property (X_("solo-boost-level"))) != 0) {
142                 gain_t val = atof (prop->value());
143                 _solo_boost_level = val;
144         }
145
146         if ((prop = node.property (X_("cut-all"))) != 0) {
147                 bool val = string_is_affirmative (prop->value());
148                 _cut_all = val;
149         }
150         if ((prop = node.property (X_("dim-all"))) != 0) {
151                 bool val = string_is_affirmative (prop->value());
152                 _dim_all = val;
153         }
154         if ((prop = node.property (X_("mono"))) != 0) {
155                 bool val = string_is_affirmative (prop->value());
156                 _mono = val;
157         }
158
159         for (XMLNodeList::const_iterator i = node.children().begin(); i != node.children().end(); ++i) {
160
161                 if ((*i)->name() == X_("Channel")) {
162                         if ((prop = (*i)->property (X_("id"))) == 0) {
163                                 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings are missing an ID"))
164                                       << endmsg;
165                                 return -1;
166                         }
167
168                         uint32_t chn;
169
170                         if (sscanf (prop->value().c_str(), "%u", &chn) != 1) {
171                                 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings has an unreadable channel ID"))
172                                       << endmsg;
173                                 return -1;
174                         }
175
176                         if (chn >= _channels.size()) {
177                                 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings has an illegal channel count"))
178                                       << endmsg;
179                                 return -1;
180                         }
181                         ChannelRecord& cr (*_channels[chn]);
182
183                         if ((prop = (*i)->property ("cut")) != 0) {
184                                 if (string_is_affirmative (prop->value())){
185                                         cr.cut = 0.0f;
186                                 } else {
187                                         cr.cut = 1.0f;
188                                 }
189                         }
190
191                         if ((prop = (*i)->property ("dim")) != 0) {
192                                 bool val = string_is_affirmative (prop->value());
193                                 cr.dim = val;
194                         }
195
196                         if ((prop = (*i)->property ("invert")) != 0) {
197                                 if (string_is_affirmative (prop->value())) {
198                                         cr.polarity = -1.0f;
199                                 } else {
200                                         cr.polarity = 1.0f;
201                                 }
202                         }
203
204                         if ((prop = (*i)->property ("solo")) != 0) {
205                                 bool val = string_is_affirmative (prop->value());
206                                 cr.soloed = val;
207                         }
208                 }
209         }
210
211         /* reset solo cnt */
212
213         solo_cnt = 0;
214
215         for (vector<ChannelRecord*>::const_iterator x = _channels.begin(); x != _channels.end(); ++x) {
216                 if ((*x)->soloed) {
217                         solo_cnt++;
218                 }
219         }
220
221         return 0;
222 }
223
224 XMLNode&
225 MonitorProcessor::state (bool full)
226 {
227         LocaleGuard lg (X_("POSIX"));
228         XMLNode& node (Processor::state (full));
229         char buf[64];
230
231         /* this replaces any existing "type" property */
232
233         node.add_property (X_("type"), X_("monitor"));
234
235         snprintf (buf, sizeof(buf), "%.12g", _dim_level.val());
236         node.add_property (X_("dim-level"), buf);
237
238         snprintf (buf, sizeof(buf), "%.12g", _solo_boost_level.val());
239         node.add_property (X_("solo-boost-level"), buf);
240
241         node.add_property (X_("cut-all"), (_cut_all ? "yes" : "no"));
242         node.add_property (X_("dim-all"), (_dim_all ? "yes" : "no"));
243         node.add_property (X_("mono"), (_mono ? "yes" : "no"));
244
245         uint32_t limit = _channels.size();
246
247         snprintf (buf, sizeof (buf), "%u", limit);
248         node.add_property (X_("channels"), buf);
249
250         XMLNode* chn_node;
251         uint32_t chn = 0;
252
253         for (vector<ChannelRecord*>::const_iterator x = _channels.begin(); x != _channels.end(); ++x, ++chn) {
254                 chn_node = new XMLNode (X_("Channel"));
255
256                 snprintf (buf, sizeof (buf), "%u", chn);
257                 chn_node->add_property ("id", buf);
258
259                 chn_node->add_property (X_("cut"), (*x)->cut == 1.0f ? "no" : "yes");
260                 chn_node->add_property (X_("invert"), (*x)->polarity == 1.0f ? "no" : "yes");
261                 chn_node->add_property (X_("dim"), (*x)->dim ? "yes" : "no");
262                 chn_node->add_property (X_("solo"), (*x)->soloed ? "yes" : "no");
263
264                 node.add_child_nocopy (*chn_node);
265         }
266
267         return node;
268 }
269
270 void
271 MonitorProcessor::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool /*result_required*/)
272 {
273         uint32_t chn = 0;
274         gain_t target_gain;
275         gain_t dim_level_this_time = _dim_level;
276         gain_t global_cut = (_cut_all ? 0.0f : 1.0f);
277         gain_t global_dim = (_dim_all ? dim_level_this_time : 1.0);
278         gain_t solo_boost;
279
280         if (_session.listening() || _session.soloing()) {
281                 solo_boost = _solo_boost_level;
282         } else {
283                 solo_boost = 1.0;
284         }
285
286         for (BufferSet::audio_iterator b = bufs.audio_begin(); b != bufs.audio_end(); ++b) {
287
288                 /* don't double-scale by both track dim and global dim coefficients */
289
290                 gain_t dim_level = (global_dim == 1.0 ? (_channels[chn]->dim ? dim_level_this_time : 1.0) : 1.0);
291                 
292                 if (_channels[chn]->soloed) {
293                         target_gain = _channels[chn]->polarity * _channels[chn]->cut * dim_level * global_cut * global_dim * solo_boost;
294                 } else {
295                         if (solo_cnt == 0) {
296                                 target_gain = _channels[chn]->polarity * _channels[chn]->cut * dim_level * global_cut * global_dim * solo_boost;
297                         } else {
298                                 target_gain = 0.0;
299                         }
300                 }
301
302                 if (target_gain != _channels[chn]->current_gain || target_gain != 1.0f) {
303
304                         Amp::apply_gain (*b, nframes, _channels[chn]->current_gain, target_gain);
305                         _channels[chn]->current_gain = target_gain;
306                 }
307
308                 ++chn;
309         }
310
311         if (_mono) {
312                 DEBUG_TRACE (DEBUG::Monitor, "mono-izing\n");
313
314                 /* chn is now the number of channels, use as a scaling factor when mixing
315                  */
316                 gain_t scale = 1.0/chn;
317                 BufferSet::audio_iterator b = bufs.audio_begin();
318                 AudioBuffer& ab (*b);
319                 Sample* buf = ab.data();
320
321                 /* scale the first channel */
322
323                 for (pframes_t n = 0; n < nframes; ++n) {
324                         buf[n] *= scale;
325                 }
326
327                 /* add every other channel into the first channel's buffer */
328
329                 ++b;
330                 for (; b != bufs.audio_end(); ++b) {
331                         AudioBuffer& ob (*b);
332                         Sample* obuf = ob.data ();
333                         for (pframes_t n = 0; n < nframes; ++n) {
334                                 buf[n] += obuf[n] * scale;
335                         }
336                 }
337
338                 /* copy the first channel to every other channel's buffer */
339
340                 b = bufs.audio_begin();
341                 ++b;
342                 for (; b != bufs.audio_end(); ++b) {
343                         AudioBuffer& ob (*b);
344                         Sample* obuf = ob.data ();
345                         memcpy (obuf, buf, sizeof (Sample) * nframes);
346                 }
347         }
348 }
349
350 bool
351 MonitorProcessor::configure_io (ChanCount in, ChanCount out)
352 {
353         allocate_channels (in.n_audio());
354         return Processor::configure_io (in, out);
355 }
356
357 bool
358 MonitorProcessor::can_support_io_configuration (const ChanCount& in, ChanCount& out)
359 {
360         out = in;
361         return true;
362 }
363
364 void
365 MonitorProcessor::set_polarity (uint32_t chn, bool invert)
366 {
367         if (invert) {
368                 _channels[chn]->polarity = -1.0f;
369         } else {
370                 _channels[chn]->polarity = 1.0f;
371         }
372 }
373
374 void
375 MonitorProcessor::set_dim (uint32_t chn, bool yn)
376 {
377         _channels[chn]->dim = yn;
378 }
379
380 void
381 MonitorProcessor::set_cut (uint32_t chn, bool yn)
382 {
383         if (yn) {
384                 _channels[chn]->cut = 0.0f;
385         } else {
386                 _channels[chn]->cut = 1.0f;
387         }
388 }
389
390 void
391 MonitorProcessor::set_solo (uint32_t chn, bool solo)
392 {
393         if (solo != _channels[chn]->soloed) {
394                 _channels[chn]->soloed = solo;
395
396                 if (solo) {
397                         solo_cnt++;
398                 } else {
399                         if (solo_cnt > 0) {
400                                 solo_cnt--;
401                         }
402                 }
403         }
404 }
405
406 void
407 MonitorProcessor::set_mono (bool yn)
408 {
409         _mono = yn;
410 }
411
412 void
413 MonitorProcessor::set_cut_all (bool yn)
414 {
415         _cut_all = yn;
416 }
417
418 void
419 MonitorProcessor::set_dim_all (bool yn)
420 {
421         _dim_all = yn;
422 }
423
424 bool
425 MonitorProcessor::display_to_user () const
426 {
427         return false;
428 }
429
430 bool
431 MonitorProcessor::soloed (uint32_t chn) const
432 {
433         return _channels[chn]->soloed;
434 }
435
436
437 bool
438 MonitorProcessor::inverted (uint32_t chn) const
439 {
440         return _channels[chn]->polarity < 0.0f;
441 }
442
443
444 bool
445 MonitorProcessor::cut (uint32_t chn) const
446 {
447         return _channels[chn]->cut == 0.0f;
448 }
449
450 bool
451 MonitorProcessor::dimmed (uint32_t chn) const
452 {
453         return _channels[chn]->dim;
454 }
455
456 bool
457 MonitorProcessor::mono () const
458 {
459         return _mono;
460 }
461
462 bool
463 MonitorProcessor::dim_all () const
464 {
465         return _dim_all;
466 }
467
468 bool
469 MonitorProcessor::cut_all () const
470 {
471         return _cut_all;
472 }
473
474 boost::shared_ptr<Controllable>
475 MonitorProcessor::channel_cut_control (uint32_t chn) const
476 {
477         if (chn < _channels.size()) {
478                 return _channels[chn]->cut_control;
479         }
480         return boost::shared_ptr<Controllable>();
481 }
482
483 boost::shared_ptr<Controllable>
484 MonitorProcessor::channel_dim_control (uint32_t chn) const
485 {
486         if (chn < _channels.size()) {
487                 return _channels[chn]->dim_control;
488         }
489         return boost::shared_ptr<Controllable>();
490 }
491
492 boost::shared_ptr<Controllable>
493 MonitorProcessor::channel_polarity_control (uint32_t chn) const
494 {
495         if (chn < _channels.size()) {
496                 return _channels[chn]->polarity_control;
497         }
498         return boost::shared_ptr<Controllable>();
499 }
500
501 boost::shared_ptr<Controllable>
502 MonitorProcessor::channel_solo_control (uint32_t chn) const
503 {
504         if (chn < _channels.size()) {
505                 return _channels[chn]->soloed_control;
506         }
507         return boost::shared_ptr<Controllable>();
508 }
509
510 MonitorProcessor::ChannelRecord::ChannelRecord (uint32_t chn)
511         : current_gain (1.0)
512         , cut_ptr (new MPControl<gain_t> (1.0, string_compose (_("cut control %1"), chn), PBD::Controllable::GainLike))
513         , dim_ptr (new MPControl<bool> (false, string_compose (_("dim control"), chn), PBD::Controllable::Toggle))
514         , polarity_ptr (new MPControl<gain_t> (1.0, string_compose (_("polarity control"), chn), PBD::Controllable::Toggle, -1, 1))
515         , soloed_ptr (new MPControl<bool> (false, string_compose (_("solo control"), chn), PBD::Controllable::Toggle))
516
517         , cut_control (cut_ptr)
518         , dim_control (dim_ptr)
519         , polarity_control (polarity_ptr)
520         , soloed_control (soloed_ptr)
521
522         , cut (*cut_ptr)
523         , dim (*dim_ptr)
524         , polarity (*polarity_ptr)
525         , soloed (*soloed_ptr)
526 {
527
528 }