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