2 Copyright (C) 2010 Paul Davis
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.
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.
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.
20 #include "pbd/convert.h"
21 #include "pbd/error.h"
22 #include "pbd/locale_guard.h"
23 #include "pbd/xml++.h"
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"
33 using namespace ARDOUR;
37 /* specialize for bool because of set_value() semantics */
40 template<> void MPControl<bool>::set_value (double v) {
41 bool newval = fabs (v) >= 0.5;
42 if (newval != _value) {
44 Changed(); /* EMIT SIGNAL */
49 MonitorProcessor::MonitorProcessor (Session& s)
50 : Processor (s, X_("MonitorOut"))
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)
70 , _dim_all (*_dim_all_ptr)
71 , _cut_all (*_cut_all_ptr)
73 , _dim_level (*_dim_level_ptr)
74 , _solo_boost_level (*_solo_boost_level_ptr)
79 MonitorProcessor::~MonitorProcessor ()
81 allocate_channels (0);
85 MonitorProcessor::allocate_channels (uint32_t size)
87 while (_channels.size() > size) {
88 if (_channels.back()->soloed) {
93 ChannelRecord* cr = _channels.back();
98 uint32_t n = _channels.size() + 1;
100 while (_channels.size() < size) {
101 _channels.push_back (new ChannelRecord (n));
106 MonitorProcessor::set_state (const XMLNode& node, int version)
108 int ret = Processor::set_state (node, version);
114 const XMLProperty* prop;
116 if ((prop = node.property (X_("type"))) == 0) {
117 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings have no type information"))
122 if (prop->value() != X_("monitor")) {
123 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor given unknown XML settings"))
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"))
134 allocate_channels (atoi (prop->value()));
136 if ((prop = node.property (X_("dim-level"))) != 0) {
137 gain_t val = atof (prop->value());
141 if ((prop = node.property (X_("solo-boost-level"))) != 0) {
142 gain_t val = atof (prop->value());
143 _solo_boost_level = val;
146 if ((prop = node.property (X_("cut-all"))) != 0) {
147 bool val = string_is_affirmative (prop->value());
150 if ((prop = node.property (X_("dim-all"))) != 0) {
151 bool val = string_is_affirmative (prop->value());
154 if ((prop = node.property (X_("mono"))) != 0) {
155 bool val = string_is_affirmative (prop->value());
159 for (XMLNodeList::const_iterator i = node.children().begin(); i != node.children().end(); ++i) {
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"))
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"))
176 if (chn >= _channels.size()) {
177 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings has an illegal channel count"))
181 ChannelRecord& cr (*_channels[chn]);
183 if ((prop = (*i)->property ("cut")) != 0) {
184 if (string_is_affirmative (prop->value())){
191 if ((prop = (*i)->property ("dim")) != 0) {
192 bool val = string_is_affirmative (prop->value());
196 if ((prop = (*i)->property ("invert")) != 0) {
197 if (string_is_affirmative (prop->value())) {
204 if ((prop = (*i)->property ("solo")) != 0) {
205 bool val = string_is_affirmative (prop->value());
215 for (vector<ChannelRecord*>::const_iterator x = _channels.begin(); x != _channels.end(); ++x) {
225 MonitorProcessor::state (bool full)
227 LocaleGuard lg (X_("POSIX"));
228 XMLNode& node (Processor::state (full));
231 /* this replaces any existing "type" property */
233 node.add_property (X_("type"), X_("monitor"));
235 snprintf (buf, sizeof(buf), "%.12g", _dim_level.val());
236 node.add_property (X_("dim-level"), buf);
238 snprintf (buf, sizeof(buf), "%.12g", _solo_boost_level.val());
239 node.add_property (X_("solo-boost-level"), buf);
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"));
245 uint32_t limit = _channels.size();
247 snprintf (buf, sizeof (buf), "%u", limit);
248 node.add_property (X_("channels"), buf);
253 for (vector<ChannelRecord*>::const_iterator x = _channels.begin(); x != _channels.end(); ++x, ++chn) {
254 chn_node = new XMLNode (X_("Channel"));
256 snprintf (buf, sizeof (buf), "%u", chn);
257 chn_node->add_property ("id", buf);
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");
264 node.add_child_nocopy (*chn_node);
271 MonitorProcessor::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool /*result_required*/)
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);
280 if (_session.listening() || _session.soloing()) {
281 solo_boost = _solo_boost_level;
286 for (BufferSet::audio_iterator b = bufs.audio_begin(); b != bufs.audio_end(); ++b) {
288 /* don't double-scale by both track dim and global dim coefficients */
290 gain_t dim_level = (global_dim == 1.0 ? (_channels[chn]->dim ? dim_level_this_time : 1.0) : 1.0);
292 if (_channels[chn]->soloed) {
293 target_gain = _channels[chn]->polarity * _channels[chn]->cut * dim_level * global_cut * global_dim * solo_boost;
296 target_gain = _channels[chn]->polarity * _channels[chn]->cut * dim_level * global_cut * global_dim * solo_boost;
302 if (target_gain != _channels[chn]->current_gain || target_gain != 1.0f) {
304 Amp::apply_gain (*b, nframes, _channels[chn]->current_gain, target_gain);
305 _channels[chn]->current_gain = target_gain;
312 DEBUG_TRACE (DEBUG::Monitor, "mono-izing\n");
314 /* chn is now the number of channels, use as a scaling factor when mixing
316 gain_t scale = 1.0/chn;
317 BufferSet::audio_iterator b = bufs.audio_begin();
318 AudioBuffer& ab (*b);
319 Sample* buf = ab.data();
321 /* scale the first channel */
323 for (pframes_t n = 0; n < nframes; ++n) {
327 /* add every other channel into the first channel's buffer */
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;
338 /* copy the first channel to every other channel's buffer */
340 b = bufs.audio_begin();
342 for (; b != bufs.audio_end(); ++b) {
343 AudioBuffer& ob (*b);
344 Sample* obuf = ob.data ();
345 memcpy (obuf, buf, sizeof (Sample) * nframes);
351 MonitorProcessor::configure_io (ChanCount in, ChanCount out)
353 allocate_channels (in.n_audio());
354 return Processor::configure_io (in, out);
358 MonitorProcessor::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
365 MonitorProcessor::set_polarity (uint32_t chn, bool invert)
368 _channels[chn]->polarity = -1.0f;
370 _channels[chn]->polarity = 1.0f;
375 MonitorProcessor::set_dim (uint32_t chn, bool yn)
377 _channels[chn]->dim = yn;
381 MonitorProcessor::set_cut (uint32_t chn, bool yn)
384 _channels[chn]->cut = 0.0f;
386 _channels[chn]->cut = 1.0f;
391 MonitorProcessor::set_solo (uint32_t chn, bool solo)
393 if (solo != _channels[chn]->soloed) {
394 _channels[chn]->soloed = solo;
407 MonitorProcessor::set_mono (bool yn)
413 MonitorProcessor::set_cut_all (bool yn)
419 MonitorProcessor::set_dim_all (bool yn)
425 MonitorProcessor::display_to_user () const
431 MonitorProcessor::soloed (uint32_t chn) const
433 return _channels[chn]->soloed;
438 MonitorProcessor::inverted (uint32_t chn) const
440 return _channels[chn]->polarity < 0.0f;
445 MonitorProcessor::cut (uint32_t chn) const
447 return _channels[chn]->cut == 0.0f;
451 MonitorProcessor::dimmed (uint32_t chn) const
453 return _channels[chn]->dim;
457 MonitorProcessor::mono () const
463 MonitorProcessor::dim_all () const
469 MonitorProcessor::cut_all () const
474 boost::shared_ptr<Controllable>
475 MonitorProcessor::channel_cut_control (uint32_t chn) const
477 if (chn < _channels.size()) {
478 return _channels[chn]->cut_control;
480 return boost::shared_ptr<Controllable>();
483 boost::shared_ptr<Controllable>
484 MonitorProcessor::channel_dim_control (uint32_t chn) const
486 if (chn < _channels.size()) {
487 return _channels[chn]->dim_control;
489 return boost::shared_ptr<Controllable>();
492 boost::shared_ptr<Controllable>
493 MonitorProcessor::channel_polarity_control (uint32_t chn) const
495 if (chn < _channels.size()) {
496 return _channels[chn]->polarity_control;
498 return boost::shared_ptr<Controllable>();
501 boost::shared_ptr<Controllable>
502 MonitorProcessor::channel_solo_control (uint32_t chn) const
504 if (chn < _channels.size()) {
505 return _channels[chn]->soloed_control;
507 return boost::shared_ptr<Controllable>();
510 MonitorProcessor::ChannelRecord::ChannelRecord (uint32_t chn)
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))
517 , cut_control (cut_ptr)
518 , dim_control (dim_ptr)
519 , polarity_control (polarity_ptr)
520 , soloed_control (soloed_ptr)
524 , polarity (*polarity_ptr)
525 , soloed (*soloed_ptr)