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 (0.251188635826, _("monitor dim level"), Controllable::Flag (0), 0.100000001490, 1.0))
40 , _solo_boost_level_ptr (new MPControl<volatile gain_t>
41 /* default is 0dB, range is 0dB to +20dB */
42 (1.0, _("monitor solo boost level"), Controllable::Flag (0), 1.0, 10.0))
44 , _dim_all_control (_dim_all_ptr)
45 , _cut_all_control (_cut_all_ptr)
46 , _mono_control (_mono_ptr)
47 , _dim_level_control (_dim_level_ptr)
48 , _solo_boost_level_control (_solo_boost_level_ptr)
50 , _dim_all (*_dim_all_ptr)
51 , _cut_all (*_cut_all_ptr)
53 , _dim_level (*_dim_level_ptr)
54 , _solo_boost_level (*_solo_boost_level_ptr)
59 MonitorProcessor::~MonitorProcessor ()
61 allocate_channels (0);
65 MonitorProcessor::allocate_channels (uint32_t size)
67 while (_channels.size() > size) {
68 if (_channels.back()->soloed) {
73 ChannelRecord* cr = _channels.back();
78 uint32_t n = _channels.size() + 1;
80 while (_channels.size() < size) {
81 _channels.push_back (new ChannelRecord (n));
86 MonitorProcessor::set_state (const XMLNode& node, int version)
88 int ret = Processor::set_state (node, version);
94 const XMLProperty* prop;
96 if ((prop = node.property (X_("type"))) == 0) {
97 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings have no type information"))
102 if (prop->value() != X_("monitor")) {
103 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor given unknown XML settings"))
108 if ((prop = node.property (X_("channels"))) == 0) {
109 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings are missing a channel cnt"))
114 allocate_channels (atoi (prop->value()));
116 if ((prop = node.property (X_("dim-level"))) != 0) {
117 gain_t val = atof (prop->value());
121 if ((prop = node.property (X_("solo-boost-level"))) != 0) {
122 gain_t val = atof (prop->value());
123 _solo_boost_level = val;
126 if ((prop = node.property (X_("cut-all"))) != 0) {
127 bool val = string_is_affirmative (prop->value());
130 if ((prop = node.property (X_("dim-all"))) != 0) {
131 bool val = string_is_affirmative (prop->value());
134 if ((prop = node.property (X_("mono"))) != 0) {
135 bool val = string_is_affirmative (prop->value());
139 for (XMLNodeList::const_iterator i = node.children().begin(); i != node.children().end(); ++i) {
141 if ((*i)->name() == X_("Channel")) {
142 if ((prop = (*i)->property (X_("id"))) == 0) {
143 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings are missing an ID"))
150 if (sscanf (prop->value().c_str(), "%u", &chn) != 1) {
151 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings has an unreadable channel ID"))
156 if (chn >= _channels.size()) {
157 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings has an illegal channel count"))
161 ChannelRecord& cr (*_channels[chn]);
163 if ((prop = (*i)->property ("cut")) != 0) {
164 if (string_is_affirmative (prop->value())){
171 if ((prop = (*i)->property ("dim")) != 0) {
172 bool val = string_is_affirmative (prop->value());
176 if ((prop = (*i)->property ("invert")) != 0) {
177 if (string_is_affirmative (prop->value())) {
184 if ((prop = (*i)->property ("solo")) != 0) {
185 bool val = string_is_affirmative (prop->value());
195 for (vector<ChannelRecord*>::const_iterator x = _channels.begin(); x != _channels.end(); ++x) {
205 MonitorProcessor::state (bool full)
207 LocaleGuard lg (X_("POSIX"));
208 XMLNode& node (Processor::state (full));
211 /* this replaces any existing "type" property */
213 node.add_property (X_("type"), X_("monitor"));
215 snprintf (buf, sizeof(buf), "%.12g", _dim_level.val());
216 node.add_property (X_("dim-level"), buf);
218 snprintf (buf, sizeof(buf), "%.12g", _solo_boost_level.val());
219 node.add_property (X_("solo-boost-level"), buf);
221 node.add_property (X_("cut-all"), (_cut_all ? "yes" : "no"));
222 node.add_property (X_("dim-all"), (_dim_all ? "yes" : "no"));
223 node.add_property (X_("mono"), (_mono ? "yes" : "no"));
225 uint32_t limit = _channels.size();
227 snprintf (buf, sizeof (buf), "%u", limit);
228 node.add_property (X_("channels"), buf);
233 for (vector<ChannelRecord*>::const_iterator x = _channels.begin(); x != _channels.end(); ++x, ++chn) {
234 chn_node = new XMLNode (X_("Channel"));
236 snprintf (buf, sizeof (buf), "%u", chn);
237 chn_node->add_property ("id", buf);
239 chn_node->add_property (X_("cut"), (*x)->cut == 1.0f ? "no" : "yes");
240 chn_node->add_property (X_("invert"), (*x)->polarity == 1.0f ? "no" : "yes");
241 chn_node->add_property (X_("dim"), (*x)->dim ? "yes" : "no");
242 chn_node->add_property (X_("solo"), (*x)->soloed ? "yes" : "no");
244 node.add_child_nocopy (*chn_node);
251 MonitorProcessor::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool /*result_required*/)
255 gain_t dim_level_this_time = _dim_level;
256 gain_t global_cut = (_cut_all ? 0.0f : 1.0f);
257 gain_t global_dim = (_dim_all ? dim_level_this_time : 1.0);
260 if (_session.listening() || _session.soloing()) {
261 solo_boost = _solo_boost_level;
266 for (BufferSet::audio_iterator b = bufs.audio_begin(); b != bufs.audio_end(); ++b) {
268 /* don't double-scale by both track dim and global dim coefficients */
270 gain_t dim_level = (global_dim == 1.0 ? (_channels[chn]->dim ? dim_level_this_time : 1.0) : 1.0);
272 if (_channels[chn]->soloed) {
273 target_gain = _channels[chn]->polarity * _channels[chn]->cut * dim_level * global_cut * global_dim * solo_boost;
276 target_gain = _channels[chn]->polarity * _channels[chn]->cut * dim_level * global_cut * global_dim * solo_boost;
282 if (target_gain != _channels[chn]->current_gain || target_gain != 1.0f) {
284 Amp::apply_gain (*b, nframes, _channels[chn]->current_gain, target_gain);
285 _channels[chn]->current_gain = target_gain;
292 DEBUG_TRACE (DEBUG::Monitor, "mono-izing\n");
294 /* chn is now the number of channels, use as a scaling factor when mixing
296 gain_t scale = 1.0/chn;
297 BufferSet::audio_iterator b = bufs.audio_begin();
298 AudioBuffer& ab (*b);
299 Sample* buf = ab.data();
301 /* scale the first channel */
303 for (pframes_t n = 0; n < nframes; ++n) {
307 /* add every other channel into the first channel's buffer */
310 for (; b != bufs.audio_end(); ++b) {
311 AudioBuffer& ob (*b);
312 Sample* obuf = ob.data ();
313 for (pframes_t n = 0; n < nframes; ++n) {
314 buf[n] += obuf[n] * scale;
318 /* copy the first channel to every other channel's buffer */
320 b = bufs.audio_begin();
322 for (; b != bufs.audio_end(); ++b) {
323 AudioBuffer& ob (*b);
324 Sample* obuf = ob.data ();
325 memcpy (obuf, buf, sizeof (Sample) * nframes);
331 MonitorProcessor::configure_io (ChanCount in, ChanCount out)
333 allocate_channels (in.n_audio());
334 return Processor::configure_io (in, out);
338 MonitorProcessor::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
345 MonitorProcessor::set_polarity (uint32_t chn, bool invert)
348 _channels[chn]->polarity = -1.0f;
350 _channels[chn]->polarity = 1.0f;
355 MonitorProcessor::set_dim (uint32_t chn, bool yn)
357 _channels[chn]->dim = yn;
361 MonitorProcessor::set_cut (uint32_t chn, bool yn)
364 _channels[chn]->cut = 0.0f;
366 _channels[chn]->cut = 1.0f;
371 MonitorProcessor::set_solo (uint32_t chn, bool solo)
373 if (solo != _channels[chn]->soloed) {
374 _channels[chn]->soloed = solo;
387 MonitorProcessor::set_mono (bool yn)
393 MonitorProcessor::set_cut_all (bool yn)
399 MonitorProcessor::set_dim_all (bool yn)
405 MonitorProcessor::display_to_user () const
411 MonitorProcessor::soloed (uint32_t chn) const
413 return _channels[chn]->soloed;
418 MonitorProcessor::inverted (uint32_t chn) const
420 return _channels[chn]->polarity < 0.0f;
425 MonitorProcessor::cut (uint32_t chn) const
427 return _channels[chn]->cut == 0.0f;
431 MonitorProcessor::dimmed (uint32_t chn) const
433 return _channels[chn]->dim;
437 MonitorProcessor::mono () const
443 MonitorProcessor::dim_all () const
449 MonitorProcessor::cut_all () const
454 boost::shared_ptr<Controllable>
455 MonitorProcessor::channel_cut_control (uint32_t chn) const
457 if (chn < _channels.size()) {
458 return _channels[chn]->cut_control;
460 return boost::shared_ptr<Controllable>();
463 boost::shared_ptr<Controllable>
464 MonitorProcessor::channel_dim_control (uint32_t chn) const
466 if (chn < _channels.size()) {
467 return _channels[chn]->dim_control;
469 return boost::shared_ptr<Controllable>();
472 boost::shared_ptr<Controllable>
473 MonitorProcessor::channel_polarity_control (uint32_t chn) const
475 if (chn < _channels.size()) {
476 return _channels[chn]->polarity_control;
478 return boost::shared_ptr<Controllable>();
481 boost::shared_ptr<Controllable>
482 MonitorProcessor::channel_solo_control (uint32_t chn) const
484 if (chn < _channels.size()) {
485 return _channels[chn]->soloed_control;
487 return boost::shared_ptr<Controllable>();
490 MonitorProcessor::ChannelRecord::ChannelRecord (uint32_t chn)
492 , cut_ptr (new MPControl<gain_t> (1.0, string_compose (_("cut control %1"), chn), PBD::Controllable::GainLike))
493 , dim_ptr (new MPControl<bool> (false, string_compose (_("dim control"), chn), PBD::Controllable::Toggle))
494 , polarity_ptr (new MPControl<gain_t> (1.0, string_compose (_("polarity control"), chn), PBD::Controllable::Toggle))
495 , soloed_ptr (new MPControl<bool> (false, string_compose (_("solo control"), chn), PBD::Controllable::Toggle))
497 , cut_control (cut_ptr)
498 , dim_control (dim_ptr)
499 , polarity_control (polarity_ptr)
500 , soloed_control (soloed_ptr)
504 , polarity (*polarity_ptr)
505 , soloed (*soloed_ptr)