1 #include "pbd/convert.h"
5 #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> /* units in dB */
38 (-12.0, _("monitor dim level"), Controllable::Flag (0), -20.0f, 0.0f))
39 , _solo_boost_level_ptr (new MPControl<volatile gain_t> /* units in dB */
40 (0.0, _("monitor solo boost level"), Controllable::Flag (0), 0.0, 20.0))
42 , _dim_all_control (_dim_all_ptr)
43 , _cut_all_control (_cut_all_ptr)
44 , _mono_control (_mono_ptr)
45 , _dim_level_control (_dim_level_ptr)
46 , _solo_boost_level_control (_solo_boost_level_ptr)
48 , _dim_all (*_dim_all_ptr)
49 , _cut_all (*_cut_all_ptr)
51 , _dim_level (*_dim_level_ptr)
52 , _solo_boost_level (*_solo_boost_level_ptr)
57 MonitorProcessor::~MonitorProcessor ()
59 allocate_channels (0);
63 MonitorProcessor::allocate_channels (uint32_t size)
65 while (_channels.size() > size) {
66 if (_channels.back()->soloed) {
71 ChannelRecord* cr = _channels.back();
76 uint32_t n = _channels.size() + 1;
78 while (_channels.size() < size) {
79 _channels.push_back (new ChannelRecord (n));
84 MonitorProcessor::set_state (const XMLNode& node, int version)
86 int ret = Processor::set_state (node, version);
92 const XMLProperty* prop;
94 if ((prop = node.property (X_("type"))) == 0) {
95 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings have no type information"))
100 if (prop->value() != X_("monitor")) {
101 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor given unknown XML settings"))
106 if ((prop = node.property (X_("channels"))) == 0) {
107 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings are missing a channel cnt"))
112 allocate_channels (atoi (prop->value()));
114 if ((prop = node.property (X_("dim-level"))) != 0) {
115 gain_t val = atof (prop->value());
119 if ((prop = node.property (X_("solo-boost-level"))) != 0) {
120 gain_t val = atof (prop->value());
121 _solo_boost_level = val;
124 if ((prop = node.property (X_("cut-all"))) != 0) {
125 bool val = string_is_affirmative (prop->value());
128 if ((prop = node.property (X_("dim-all"))) != 0) {
129 bool val = string_is_affirmative (prop->value());
132 if ((prop = node.property (X_("mono"))) != 0) {
133 bool val = string_is_affirmative (prop->value());
137 for (XMLNodeList::const_iterator i = node.children().begin(); i != node.children().end(); ++i) {
139 if ((*i)->name() == X_("Channel")) {
140 if ((prop = (*i)->property (X_("id"))) == 0) {
141 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings are missing an ID"))
148 if (sscanf (prop->value().c_str(), "%u", &chn) != 1) {
149 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings has an unreadable channel ID"))
154 if (chn >= _channels.size()) {
155 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings has an illegal channel count"))
159 ChannelRecord& cr (*_channels[chn]);
161 if ((prop = (*i)->property ("cut")) != 0) {
162 if (string_is_affirmative (prop->value())){
169 if ((prop = (*i)->property ("dim")) != 0) {
170 bool val = string_is_affirmative (prop->value());
174 if ((prop = (*i)->property ("invert")) != 0) {
175 if (string_is_affirmative (prop->value())) {
182 if ((prop = (*i)->property ("solo")) != 0) {
183 bool val = string_is_affirmative (prop->value());
193 for (vector<ChannelRecord*>::const_iterator x = _channels.begin(); x != _channels.end(); ++x) {
203 MonitorProcessor::state (bool full)
205 XMLNode& node (Processor::state (full));
208 /* this replaces any existing "type" property */
210 node.add_property (X_("type"), X_("monitor"));
212 snprintf (buf, sizeof(buf), "%.12g", _dim_level.val());
213 node.add_property (X_("dim-level"), buf);
215 snprintf (buf, sizeof(buf), "%.12g", _solo_boost_level.val());
216 node.add_property (X_("solo-boost-level"), buf);
218 node.add_property (X_("cut-all"), (_cut_all ? "yes" : "no"));
219 node.add_property (X_("dim-all"), (_dim_all ? "yes" : "no"));
220 node.add_property (X_("mono"), (_mono ? "yes" : "no"));
222 uint32_t limit = _channels.size();
224 snprintf (buf, sizeof (buf), "%u", limit);
225 node.add_property (X_("channels"), buf);
230 for (vector<ChannelRecord*>::const_iterator x = _channels.begin(); x != _channels.end(); ++x, ++chn) {
231 chn_node = new XMLNode (X_("Channel"));
233 snprintf (buf, sizeof (buf), "%u", chn);
234 chn_node->add_property ("id", buf);
236 chn_node->add_property (X_("cut"), (*x)->cut == 1.0f ? "no" : "yes");
237 chn_node->add_property (X_("invert"), (*x)->polarity == 1.0f ? "no" : "yes");
238 chn_node->add_property (X_("dim"), (*x)->dim ? "yes" : "no");
239 chn_node->add_property (X_("solo"), (*x)->soloed ? "yes" : "no");
241 node.add_child_nocopy (*chn_node);
248 MonitorProcessor::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool /*result_required*/)
252 gain_t dim_level_this_time = _dim_level;
253 gain_t global_cut = (_cut_all ? 0.0f : 1.0f);
254 gain_t global_dim = (_dim_all ? dim_level_this_time : 1.0f);
257 if (_session.listening() || _session.soloing()) {
258 /* solo boost controller is in dB */
259 solo_boost = dB_to_coefficient (_solo_boost_level);
264 for (BufferSet::audio_iterator b = bufs.audio_begin(); b != bufs.audio_end(); ++b) {
266 /* don't double-scale by both track dim and global dim coefficients */
268 gain_t dim_level = (global_dim == 1.0 ? (_channels[chn]->dim ? dim_level_this_time : 1.0) : 1.0);
270 /* dim level is in dB */
272 dim_level = dB_to_coefficient (dim_level);
274 if (_channels[chn]->soloed) {
275 target_gain = _channels[chn]->polarity * _channels[chn]->cut * dim_level * global_cut * global_dim * solo_boost;
278 target_gain = _channels[chn]->polarity * _channels[chn]->cut * dim_level * global_cut * global_dim * solo_boost;
284 if (target_gain != _channels[chn]->current_gain || target_gain != 1.0f) {
286 Amp::apply_gain (*b, nframes, _channels[chn]->current_gain, target_gain);
287 _channels[chn]->current_gain = target_gain;
294 DEBUG_TRACE (DEBUG::Monitor, "mono-izing\n");
296 /* chn is now the number of channels, use as a scaling factor when mixing
298 gain_t scale = 1.0/chn;
299 BufferSet::audio_iterator b = bufs.audio_begin();
300 AudioBuffer& ab (*b);
301 Sample* buf = ab.data();
303 /* scale the first channel */
305 for (pframes_t n = 0; n < nframes; ++n) {
309 /* add every other channel into the first channel's buffer */
312 for (; b != bufs.audio_end(); ++b) {
313 AudioBuffer& ob (*b);
314 Sample* obuf = ob.data ();
315 for (pframes_t n = 0; n < nframes; ++n) {
316 buf[n] += obuf[n] * scale;
320 /* copy the first channel to every other channel's buffer */
322 b = bufs.audio_begin();
324 for (; b != bufs.audio_end(); ++b) {
325 AudioBuffer& ob (*b);
326 Sample* obuf = ob.data ();
327 memcpy (obuf, buf, sizeof (Sample) * nframes);
333 MonitorProcessor::configure_io (ChanCount in, ChanCount out)
335 allocate_channels (in.n_audio());
336 return Processor::configure_io (in, out);
340 MonitorProcessor::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
347 MonitorProcessor::set_polarity (uint32_t chn, bool invert)
350 _channels[chn]->polarity = -1.0f;
352 _channels[chn]->polarity = 1.0f;
357 MonitorProcessor::set_dim (uint32_t chn, bool yn)
359 _channels[chn]->dim = yn;
363 MonitorProcessor::set_cut (uint32_t chn, bool yn)
366 _channels[chn]->cut = 0.0f;
368 _channels[chn]->cut = 1.0f;
373 MonitorProcessor::set_solo (uint32_t chn, bool solo)
375 if (solo != _channels[chn]->soloed) {
376 _channels[chn]->soloed = solo;
389 MonitorProcessor::set_mono (bool yn)
395 MonitorProcessor::set_cut_all (bool yn)
401 MonitorProcessor::set_dim_all (bool yn)
407 MonitorProcessor::display_to_user () const
413 MonitorProcessor::soloed (uint32_t chn) const
415 return _channels[chn]->soloed;
420 MonitorProcessor::inverted (uint32_t chn) const
422 return _channels[chn]->polarity < 0.0f;
427 MonitorProcessor::cut (uint32_t chn) const
429 return _channels[chn]->cut == 0.0f;
433 MonitorProcessor::dimmed (uint32_t chn) const
435 return _channels[chn]->dim;
439 MonitorProcessor::mono () const
445 MonitorProcessor::dim_all () const
451 MonitorProcessor::cut_all () const
456 boost::shared_ptr<Controllable>
457 MonitorProcessor::channel_cut_control (uint32_t chn) const
459 if (chn < _channels.size()) {
460 return _channels[chn]->cut_control;
462 return boost::shared_ptr<Controllable>();
465 boost::shared_ptr<Controllable>
466 MonitorProcessor::channel_dim_control (uint32_t chn) const
468 if (chn < _channels.size()) {
469 return _channels[chn]->dim_control;
471 return boost::shared_ptr<Controllable>();
474 boost::shared_ptr<Controllable>
475 MonitorProcessor::channel_polarity_control (uint32_t chn) const
477 if (chn < _channels.size()) {
478 return _channels[chn]->polarity_control;
480 return boost::shared_ptr<Controllable>();
483 boost::shared_ptr<Controllable>
484 MonitorProcessor::channel_solo_control (uint32_t chn) const
486 if (chn < _channels.size()) {
487 return _channels[chn]->soloed_control;
489 return boost::shared_ptr<Controllable>();
492 MonitorProcessor::ChannelRecord::ChannelRecord (uint32_t chn)
494 , cut_ptr (new MPControl<gain_t> (1.0, string_compose (_("cut control %1"), chn), PBD::Controllable::GainLike))
495 , dim_ptr (new MPControl<bool> (false, string_compose (_("dim control"), chn), PBD::Controllable::Toggle))
496 , polarity_ptr (new MPControl<gain_t> (1.0, string_compose (_("polarity control"), chn), PBD::Controllable::Toggle))
497 , soloed_ptr (new MPControl<bool> (false, string_compose (_("solo control"), chn), PBD::Controllable::Toggle))
499 , cut_control (cut_ptr)
500 , dim_control (dim_ptr)
501 , polarity_control (polarity_ptr)
502 , soloed_control (soloed_ptr)
506 , polarity (*polarity_ptr)
507 , soloed (*soloed_ptr)