1 #include "pbd/convert.h"
3 #include "pbd/locale_guard.h"
6 #include "ardour/amp.h"
7 #include "ardour/debug.h"
8 #include "ardour/audio_buffer.h"
9 #include "ardour/monitor_processor.h"
10 #include "ardour/session.h"
14 using namespace ARDOUR;
18 /* specialize for bool because of set_value() semantics */
21 template<> void MPControl<bool>::set_value (double v) {
22 bool newval = fabs (v) >= 0.5;
23 if (newval != _value) {
25 Changed(); /* EMIT SIGNAL */
30 MonitorProcessor::MonitorProcessor (Session& s)
31 : Processor (s, X_("MonitorOut"))
34 , _dim_all_ptr (new MPControl<bool> (false, _("monitor dim"), Controllable::Toggle))
35 , _cut_all_ptr (new MPControl<bool> (false, _("monitor cut"), Controllable::Toggle))
36 , _mono_ptr (new MPControl<bool> (false, _("monitor mono"), Controllable::Toggle))
37 , _dim_level_ptr (new MPControl<volatile gain_t>
38 /* default is -12dB, range is -20dB to 0dB */
39 (dB_to_coefficient(-12.0), _("monitor dim level"), Controllable::Flag (0),
40 dB_to_coefficient(-20.0), dB_to_coefficient (0.0)))
41 , _solo_boost_level_ptr (new MPControl<volatile gain_t>
42 /* default is 0dB, range is 0dB to +20dB */
43 (dB_to_coefficient(0.0), _("monitor solo boost level"), Controllable::Flag (0),
44 dB_to_coefficient(0.0), dB_to_coefficient(10.0)))
45 , _dim_all_control (_dim_all_ptr)
46 , _cut_all_control (_cut_all_ptr)
47 , _mono_control (_mono_ptr)
48 , _dim_level_control (_dim_level_ptr)
49 , _solo_boost_level_control (_solo_boost_level_ptr)
51 , _dim_all (*_dim_all_ptr)
52 , _cut_all (*_cut_all_ptr)
54 , _dim_level (*_dim_level_ptr)
55 , _solo_boost_level (*_solo_boost_level_ptr)
60 MonitorProcessor::~MonitorProcessor ()
62 allocate_channels (0);
66 MonitorProcessor::allocate_channels (uint32_t size)
68 while (_channels.size() > size) {
69 if (_channels.back()->soloed) {
74 ChannelRecord* cr = _channels.back();
79 uint32_t n = _channels.size() + 1;
81 while (_channels.size() < size) {
82 _channels.push_back (new ChannelRecord (n));
87 MonitorProcessor::set_state (const XMLNode& node, int version)
89 int ret = Processor::set_state (node, version);
95 const XMLProperty* prop;
97 if ((prop = node.property (X_("type"))) == 0) {
98 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings have no type information"))
103 if (prop->value() != X_("monitor")) {
104 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor given unknown XML settings"))
109 if ((prop = node.property (X_("channels"))) == 0) {
110 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings are missing a channel cnt"))
115 allocate_channels (atoi (prop->value()));
117 if ((prop = node.property (X_("dim-level"))) != 0) {
118 gain_t val = atof (prop->value());
122 if ((prop = node.property (X_("solo-boost-level"))) != 0) {
123 gain_t val = atof (prop->value());
124 _solo_boost_level = val;
127 if ((prop = node.property (X_("cut-all"))) != 0) {
128 bool val = string_is_affirmative (prop->value());
131 if ((prop = node.property (X_("dim-all"))) != 0) {
132 bool val = string_is_affirmative (prop->value());
135 if ((prop = node.property (X_("mono"))) != 0) {
136 bool val = string_is_affirmative (prop->value());
140 for (XMLNodeList::const_iterator i = node.children().begin(); i != node.children().end(); ++i) {
142 if ((*i)->name() == X_("Channel")) {
143 if ((prop = (*i)->property (X_("id"))) == 0) {
144 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings are missing an ID"))
151 if (sscanf (prop->value().c_str(), "%u", &chn) != 1) {
152 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings has an unreadable channel ID"))
157 if (chn >= _channels.size()) {
158 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings has an illegal channel count"))
162 ChannelRecord& cr (*_channels[chn]);
164 if ((prop = (*i)->property ("cut")) != 0) {
165 if (string_is_affirmative (prop->value())){
172 if ((prop = (*i)->property ("dim")) != 0) {
173 bool val = string_is_affirmative (prop->value());
177 if ((prop = (*i)->property ("invert")) != 0) {
178 if (string_is_affirmative (prop->value())) {
185 if ((prop = (*i)->property ("solo")) != 0) {
186 bool val = string_is_affirmative (prop->value());
196 for (vector<ChannelRecord*>::const_iterator x = _channels.begin(); x != _channels.end(); ++x) {
206 MonitorProcessor::state (bool full)
208 LocaleGuard lg (X_("POSIX"));
209 XMLNode& node (Processor::state (full));
212 /* this replaces any existing "type" property */
214 node.add_property (X_("type"), X_("monitor"));
216 snprintf (buf, sizeof(buf), "%.12g", _dim_level.val());
217 node.add_property (X_("dim-level"), buf);
219 snprintf (buf, sizeof(buf), "%.12g", _solo_boost_level.val());
220 node.add_property (X_("solo-boost-level"), buf);
222 node.add_property (X_("cut-all"), (_cut_all ? "yes" : "no"));
223 node.add_property (X_("dim-all"), (_dim_all ? "yes" : "no"));
224 node.add_property (X_("mono"), (_mono ? "yes" : "no"));
226 uint32_t limit = _channels.size();
228 snprintf (buf, sizeof (buf), "%u", limit);
229 node.add_property (X_("channels"), buf);
234 for (vector<ChannelRecord*>::const_iterator x = _channels.begin(); x != _channels.end(); ++x, ++chn) {
235 chn_node = new XMLNode (X_("Channel"));
237 snprintf (buf, sizeof (buf), "%u", chn);
238 chn_node->add_property ("id", buf);
240 chn_node->add_property (X_("cut"), (*x)->cut == 1.0f ? "no" : "yes");
241 chn_node->add_property (X_("invert"), (*x)->polarity == 1.0f ? "no" : "yes");
242 chn_node->add_property (X_("dim"), (*x)->dim ? "yes" : "no");
243 chn_node->add_property (X_("solo"), (*x)->soloed ? "yes" : "no");
245 node.add_child_nocopy (*chn_node);
252 MonitorProcessor::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool /*result_required*/)
256 gain_t dim_level_this_time = _dim_level;
257 gain_t global_cut = (_cut_all ? 0.0f : 1.0f);
258 gain_t global_dim = (_dim_all ? dim_level_this_time : 1.0);
261 if (_session.listening() || _session.soloing()) {
262 solo_boost = _solo_boost_level;
267 for (BufferSet::audio_iterator b = bufs.audio_begin(); b != bufs.audio_end(); ++b) {
269 /* don't double-scale by both track dim and global dim coefficients */
271 gain_t dim_level = (global_dim == 1.0 ? (_channels[chn]->dim ? dim_level_this_time : 1.0) : 1.0);
273 if (_channels[chn]->soloed) {
274 target_gain = _channels[chn]->polarity * _channels[chn]->cut * dim_level * global_cut * global_dim * solo_boost;
277 target_gain = _channels[chn]->polarity * _channels[chn]->cut * dim_level * global_cut * global_dim * solo_boost;
283 if (target_gain != _channels[chn]->current_gain || target_gain != 1.0f) {
285 Amp::apply_gain (*b, nframes, _channels[chn]->current_gain, target_gain);
286 _channels[chn]->current_gain = target_gain;
293 DEBUG_TRACE (DEBUG::Monitor, "mono-izing\n");
295 /* chn is now the number of channels, use as a scaling factor when mixing
297 gain_t scale = 1.0/chn;
298 BufferSet::audio_iterator b = bufs.audio_begin();
299 AudioBuffer& ab (*b);
300 Sample* buf = ab.data();
302 /* scale the first channel */
304 for (pframes_t n = 0; n < nframes; ++n) {
308 /* add every other channel into the first channel's buffer */
311 for (; b != bufs.audio_end(); ++b) {
312 AudioBuffer& ob (*b);
313 Sample* obuf = ob.data ();
314 for (pframes_t n = 0; n < nframes; ++n) {
315 buf[n] += obuf[n] * scale;
319 /* copy the first channel to every other channel's buffer */
321 b = bufs.audio_begin();
323 for (; b != bufs.audio_end(); ++b) {
324 AudioBuffer& ob (*b);
325 Sample* obuf = ob.data ();
326 memcpy (obuf, buf, sizeof (Sample) * nframes);
332 MonitorProcessor::configure_io (ChanCount in, ChanCount out)
334 allocate_channels (in.n_audio());
335 return Processor::configure_io (in, out);
339 MonitorProcessor::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
346 MonitorProcessor::set_polarity (uint32_t chn, bool invert)
349 _channels[chn]->polarity = -1.0f;
351 _channels[chn]->polarity = 1.0f;
356 MonitorProcessor::set_dim (uint32_t chn, bool yn)
358 _channels[chn]->dim = yn;
362 MonitorProcessor::set_cut (uint32_t chn, bool yn)
365 _channels[chn]->cut = 0.0f;
367 _channels[chn]->cut = 1.0f;
372 MonitorProcessor::set_solo (uint32_t chn, bool solo)
374 if (solo != _channels[chn]->soloed) {
375 _channels[chn]->soloed = solo;
388 MonitorProcessor::set_mono (bool yn)
394 MonitorProcessor::set_cut_all (bool yn)
400 MonitorProcessor::set_dim_all (bool yn)
406 MonitorProcessor::display_to_user () const
412 MonitorProcessor::soloed (uint32_t chn) const
414 return _channels[chn]->soloed;
419 MonitorProcessor::inverted (uint32_t chn) const
421 return _channels[chn]->polarity < 0.0f;
426 MonitorProcessor::cut (uint32_t chn) const
428 return _channels[chn]->cut == 0.0f;
432 MonitorProcessor::dimmed (uint32_t chn) const
434 return _channels[chn]->dim;
438 MonitorProcessor::mono () const
444 MonitorProcessor::dim_all () const
450 MonitorProcessor::cut_all () const
455 boost::shared_ptr<Controllable>
456 MonitorProcessor::channel_cut_control (uint32_t chn) const
458 if (chn < _channels.size()) {
459 return _channels[chn]->cut_control;
461 return boost::shared_ptr<Controllable>();
464 boost::shared_ptr<Controllable>
465 MonitorProcessor::channel_dim_control (uint32_t chn) const
467 if (chn < _channels.size()) {
468 return _channels[chn]->dim_control;
470 return boost::shared_ptr<Controllable>();
473 boost::shared_ptr<Controllable>
474 MonitorProcessor::channel_polarity_control (uint32_t chn) const
476 if (chn < _channels.size()) {
477 return _channels[chn]->polarity_control;
479 return boost::shared_ptr<Controllable>();
482 boost::shared_ptr<Controllable>
483 MonitorProcessor::channel_solo_control (uint32_t chn) const
485 if (chn < _channels.size()) {
486 return _channels[chn]->soloed_control;
488 return boost::shared_ptr<Controllable>();
491 MonitorProcessor::ChannelRecord::ChannelRecord (uint32_t chn)
493 , cut_ptr (new MPControl<gain_t> (1.0, string_compose (_("cut control %1"), chn), PBD::Controllable::GainLike))
494 , dim_ptr (new MPControl<bool> (false, string_compose (_("dim control"), chn), PBD::Controllable::Toggle))
495 , polarity_ptr (new MPControl<gain_t> (1.0, string_compose (_("polarity control"), chn), PBD::Controllable::Toggle))
496 , soloed_ptr (new MPControl<bool> (false, string_compose (_("solo control"), chn), PBD::Controllable::Toggle))
498 , cut_control (cut_ptr)
499 , dim_control (dim_ptr)
500 , polarity_control (polarity_ptr)
501 , soloed_control (soloed_ptr)
505 , polarity (*polarity_ptr)
506 , soloed (*soloed_ptr)