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>
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.
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.
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.
23 #include "pbd/error.h"
24 #include "pbd/xml++.h"
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"
34 using namespace ARDOUR;
38 /* specialize for bool because of set_value() semantics */
41 template<> void MPControl<bool>::set_value (double v, PBD::Controllable::GroupControlDisposition gcd) {
42 bool newval = fabs (v) >= 0.5;
43 if (newval != _value) {
45 Changed (true, gcd); /* EMIT SIGNAL */
50 MonitorProcessor::MonitorProcessor (Session& s)
51 : Processor (s, X_("MonitorOut"))
53 , _monitor_active (false)
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)
72 , _dim_all (*_dim_all_ptr)
73 , _cut_all (*_cut_all_ptr)
75 , _dim_level (*_dim_level_ptr)
76 , _solo_boost_level (*_solo_boost_level_ptr)
81 MonitorProcessor::~MonitorProcessor ()
83 allocate_channels (0);
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 */
94 MonitorProcessor::allocate_channels (uint32_t size)
96 while (_channels.size() > size) {
97 if (_channels.back()->soloed) {
102 ChannelRecord* cr = _channels.back();
103 _channels.pop_back();
107 uint32_t n = _channels.size() + 1;
109 while (_channels.size() < size) {
110 _channels.push_back (new ChannelRecord (n));
115 MonitorProcessor::set_state (const XMLNode& node, int version)
117 int ret = Processor::set_state (node, version);
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"))
130 if (type_name != X_("monitor")) {
131 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor given unknown XML settings"))
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"))
143 allocate_channels (channels);
145 // need to check that these conversions are working as expected
147 if (node.get_property (X_("dim-level"), val)) {
151 if (node.get_property (X_("solo-boost-level"), val)) {
152 _solo_boost_level = val;
156 if (node.get_property (X_("cut-all"), bool_val)) {
160 if (node.get_property (X_("dim-all"), bool_val)) {
164 if (node.get_property (X_("mono"), bool_val)) {
168 for (XMLNodeList::const_iterator i = node.children().begin(); i != node.children().end(); ++i) {
170 if ((*i)->name() == X_("Channel")) {
173 if (!(*i)->get_property (X_("id"), chn)) {
174 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings are missing an ID"))
179 if (chn >= _channels.size()) {
180 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings has an illegal channel count"))
184 ChannelRecord& cr (*_channels[chn]);
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;
191 cr.cut = GAIN_COEFF_UNITY;
196 if ((*i)->get_property ("dim", dim)) {
200 bool invert_polarity;
201 if ((*i)->get_property ("invert", invert_polarity)) {
202 if (invert_polarity) {
210 if ((*i)->get_property ("solo", soloed)) {
220 for (vector<ChannelRecord*>::const_iterator x = _channels.begin(); x != _channels.end(); ++x) {
226 update_monitor_state ();
231 MonitorProcessor::state ()
233 XMLNode& node(Processor::state ());
235 /* this replaces any existing "type" property */
237 node.set_property (X_("type"), X_("monitor"));
239 node.set_property (X_ ("dim-level"), (float)_dim_level.val ());
240 node.set_property (X_ ("solo-boost-level"), (float)_solo_boost_level.val ());
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());
246 node.set_property (X_("channels"), (uint32_t)_channels.size ());
251 for (vector<ChannelRecord*>::const_iterator x = _channels.begin (); x != _channels.end ();
253 chn_node = new XMLNode (X_("Channel"));
255 chn_node->set_property ("id", chn);
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);
263 node.add_child_nocopy (*chn_node);
270 MonitorProcessor::run (BufferSet& bufs, samplepos_t /*start_sample*/, samplepos_t /*end_sample*/, double /*speed*/, pframes_t nframes, bool /*result_required*/)
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);
279 if (_session.listening() || _session.soloing()) {
280 solo_boost = _solo_boost_level;
282 solo_boost = GAIN_COEFF_UNITY;
285 for (BufferSet::audio_iterator b = bufs.audio_begin(); b != bufs.audio_end(); ++b) {
287 /* don't double-scale by both track dim and global dim coefficients */
289 gain_t dim_level = (global_dim == GAIN_COEFF_UNITY ? (_channels[chn]->dim ? dim_level_this_time : GAIN_COEFF_UNITY) : GAIN_COEFF_UNITY);
291 if (_channels[chn]->soloed) {
292 target_gain = _channels[chn]->polarity * _channels[chn]->cut * dim_level * global_cut * global_dim * solo_boost;
295 target_gain = _channels[chn]->polarity * _channels[chn]->cut * dim_level * global_cut * global_dim * solo_boost;
297 target_gain = GAIN_COEFF_ZERO;
301 if (target_gain != _channels[chn]->current_gain || target_gain != GAIN_COEFF_UNITY) {
303 _channels[chn]->current_gain = Amp::apply_gain (*b, _session.nominal_sample_rate(), nframes, _channels[chn]->current_gain, target_gain);
310 DEBUG_TRACE (DEBUG::Monitor, "mono-izing\n");
312 /* chn is now the number of channels, use as a scaling factor when mixing
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();
319 /* scale the first channel */
321 for (pframes_t n = 0; n < nframes; ++n) {
325 /* add every other channel into the first channel's buffer */
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;
336 /* copy the first channel to every other channel's buffer */
338 b = bufs.audio_begin();
340 for (; b != bufs.audio_end(); ++b) {
341 AudioBuffer& ob (*b);
342 Sample* obuf = ob.data ();
343 memcpy (obuf, buf, sizeof (Sample) * nframes);
349 MonitorProcessor::configure_io (ChanCount in, ChanCount out)
351 allocate_channels (in.n_audio());
352 return Processor::configure_io (in, out);
356 MonitorProcessor::can_support_io_configuration (const ChanCount& in, ChanCount& out)
363 MonitorProcessor::set_polarity (uint32_t chn, bool invert)
366 _channels[chn]->polarity = -1.0f;
368 _channels[chn]->polarity = 1.0f;
370 update_monitor_state ();
374 MonitorProcessor::set_dim (uint32_t chn, bool yn)
376 _channels[chn]->dim = yn;
377 update_monitor_state ();
381 MonitorProcessor::set_cut (uint32_t chn, bool yn)
384 _channels[chn]->cut = GAIN_COEFF_ZERO;
386 _channels[chn]->cut = GAIN_COEFF_UNITY;
388 update_monitor_state ();
392 MonitorProcessor::set_solo (uint32_t chn, bool solo)
394 if (solo != _channels[chn]->soloed) {
395 _channels[chn]->soloed = solo;
405 update_monitor_state ();
409 MonitorProcessor::set_mono (bool yn)
412 update_monitor_state ();
416 MonitorProcessor::set_cut_all (bool yn)
419 update_monitor_state ();
423 MonitorProcessor::set_dim_all (bool yn)
426 update_monitor_state ();
430 MonitorProcessor::display_to_user () const
436 MonitorProcessor::soloed (uint32_t chn) const
438 return _channels[chn]->soloed;
442 MonitorProcessor::inverted (uint32_t chn) const
444 return _channels[chn]->polarity < 0.0f;
448 MonitorProcessor::cut (uint32_t chn) const
450 return _channels[chn]->cut == GAIN_COEFF_ZERO;
454 MonitorProcessor::dimmed (uint32_t chn) const
456 return _channels[chn]->dim;
460 MonitorProcessor::mono () const
466 MonitorProcessor::dim_all () const
472 MonitorProcessor::cut_all () const
478 MonitorProcessor::update_monitor_state ()
482 if (_cut_all || _dim_all || _mono) {
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)) {
494 if (_monitor_active != en) {
495 _monitor_active = en;
496 _session.MonitorChanged();
500 boost::shared_ptr<Controllable>
501 MonitorProcessor::channel_cut_control (uint32_t chn) const
503 if (chn < _channels.size()) {
504 return _channels[chn]->cut_control;
506 return boost::shared_ptr<Controllable>();
509 boost::shared_ptr<Controllable>
510 MonitorProcessor::channel_dim_control (uint32_t chn) const
512 if (chn < _channels.size()) {
513 return _channels[chn]->dim_control;
515 return boost::shared_ptr<Controllable>();
518 boost::shared_ptr<Controllable>
519 MonitorProcessor::channel_polarity_control (uint32_t chn) const
521 if (chn < _channels.size()) {
522 return _channels[chn]->polarity_control;
524 return boost::shared_ptr<Controllable>();
527 boost::shared_ptr<Controllable>
528 MonitorProcessor::channel_solo_control (uint32_t chn) const
530 if (chn < _channels.size()) {
531 return _channels[chn]->soloed_control;
533 return boost::shared_ptr<Controllable>();
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))
543 , cut_control (cut_ptr)
544 , dim_control (dim_ptr)
545 , polarity_control (polarity_ptr)
546 , soloed_control (soloed_ptr)
550 , polarity (*polarity_ptr)
551 , soloed (*soloed_ptr)
555 MonitorProcessor::ChannelRecord::~ChannelRecord ()
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 */