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 MonitorProcessor::MonitorProcessor (Session& s)
19 : Processor (s, X_("MonitorOut"))
25 _solo_boost_level = 1.0;
29 MonitorProcessor::allocate_channels (uint32_t size)
31 while (_channels.size() > size) {
32 if (_channels.back().soloed) {
40 while (_channels.size() < size) {
41 _channels.push_back (ChannelRecord());
46 MonitorProcessor::set_state (const XMLNode& node, int version)
48 int ret = Processor::set_state (node, version);
54 const XMLProperty* prop;
56 if ((prop = node.property (X_("type"))) == 0) {
57 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings have no type information"))
62 if (prop->value() != X_("monitor")) {
63 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor given unknown XML settings"))
68 if ((prop = node.property (X_("channels"))) == 0) {
69 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings are missing a channel cnt"))
74 allocate_channels (atoi (prop->value()));
76 if ((prop = node.property (X_("dim-level"))) != 0) {
77 double val = atof (prop->value());
81 if ((prop = node.property (X_("solo-boost-level"))) != 0) {
82 double val = atof (prop->value());
83 _solo_boost_level = val;
86 if ((prop = node.property (X_("cut-all"))) != 0) {
87 bool val = string_is_affirmative (prop->value());
90 if ((prop = node.property (X_("dim-all"))) != 0) {
91 bool val = string_is_affirmative (prop->value());
94 if ((prop = node.property (X_("mono"))) != 0) {
95 bool val = string_is_affirmative (prop->value());
99 for (XMLNodeList::const_iterator i = node.children().begin(); i != node.children().end(); ++i) {
101 if ((*i)->name() == X_("Channel")) {
102 if ((prop = (*i)->property (X_("id"))) == 0) {
103 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings are missing an ID"))
110 if (sscanf (prop->value().c_str(), "%u", &chn) != 1) {
111 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings has an unreadable channel ID"))
116 if (chn >= _channels.size()) {
117 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings has an illegal channel count"))
121 ChannelRecord& cr (_channels[chn]);
123 if ((prop = (*i)->property ("cut")) != 0) {
124 if (string_is_affirmative (prop->value())){
131 if ((prop = (*i)->property ("dim")) != 0) {
132 bool val = string_is_affirmative (prop->value());
136 if ((prop = (*i)->property ("invert")) != 0) {
137 if (string_is_affirmative (prop->value())) {
144 if ((prop = (*i)->property ("solo")) != 0) {
145 bool val = string_is_affirmative (prop->value());
155 for (vector<ChannelRecord>::const_iterator x = _channels.begin(); x != _channels.end(); ++x) {
165 MonitorProcessor::state (bool full)
167 XMLNode& node (Processor::state (full));
170 /* this replaces any existing "type" property */
172 node.add_property (X_("type"), X_("monitor"));
174 snprintf (buf, sizeof(buf), "%.12g", _dim_level);
175 node.add_property (X_("dim-level"), buf);
177 snprintf (buf, sizeof(buf), "%.12g", _solo_boost_level);
178 node.add_property (X_("solo-boost-level"), buf);
180 node.add_property (X_("cut-all"), (_cut_all ? "yes" : "no"));
181 node.add_property (X_("dim-all"), (_dim_all ? "yes" : "no"));
182 node.add_property (X_("mono"), (_mono ? "yes" : "no"));
184 uint32_t limit = _channels.size();
186 snprintf (buf, sizeof (buf), "%u", limit);
187 node.add_property (X_("channels"), buf);
192 for (vector<ChannelRecord>::const_iterator x = _channels.begin(); x != _channels.end(); ++x, ++chn) {
193 chn_node = new XMLNode (X_("Channel"));
195 snprintf (buf, sizeof (buf), "%u", chn);
196 chn_node->add_property ("id", buf);
198 chn_node->add_property (X_("cut"), x->cut == 1.0 ? "no" : "yes");
199 chn_node->add_property (X_("invert"), x->polarity == 1.0 ? "no" : "yes");
200 chn_node->add_property (X_("dim"), x->dim ? "yes" : "no");
201 chn_node->add_property (X_("solo"), x->soloed ? "yes" : "no");
203 node.add_child_nocopy (*chn_node);
210 MonitorProcessor::run (BufferSet& bufs, sframes_t /*start_frame*/, sframes_t /*end_frame*/, nframes_t nframes, bool /*result_required*/)
214 gain_t dim_level_this_time = _dim_level;
215 gain_t global_cut = (_cut_all ? 0.0f : 1.0f);
216 gain_t global_dim = (_dim_all ? dim_level_this_time : 1.0f);
219 if (_session.listening() || _session.soloing()) {
220 solo_boost = _solo_boost_level;
225 for (BufferSet::audio_iterator b = bufs.audio_begin(); b != bufs.audio_end(); ++b) {
227 /* don't double-scale by both track dim and global dim coefficients */
229 gain_t dim_level = (global_dim == 1.0 ? (_channels[chn].dim ? dim_level_this_time : 1.0) : 1.0);
231 if (_channels[chn].soloed) {
232 target_gain = _channels[chn].polarity * _channels[chn].cut * dim_level * global_cut * global_dim * solo_boost;
235 target_gain = _channels[chn].polarity * _channels[chn].cut * dim_level * global_cut * global_dim * solo_boost;
241 DEBUG_TRACE (DEBUG::Monitor,
242 string_compose("channel %1 sb %2 gc %3 gd %4 cd %5 dl %6 cp %7 cc %8 cs %9 sc %10 TG %11\n",
249 _channels[chn].polarity,
251 _channels[chn].soloed,
255 if (target_gain != _channels[chn].current_gain || target_gain != 1.0f) {
257 Amp::apply_gain (*b, nframes, _channels[chn].current_gain, target_gain);
258 _channels[chn].current_gain = target_gain;
265 DEBUG_TRACE (DEBUG::Monitor, "mono-izing\n");
267 /* chn is now the number of channels, use as a scaling factor when mixing
269 gain_t scale = 1.0/chn;
270 BufferSet::audio_iterator b = bufs.audio_begin();
271 AudioBuffer& ab (*b);
272 Sample* buf = ab.data();
274 /* scale the first channel */
276 for (nframes_t n = 0; n < nframes; ++n) {
280 /* add every other channel into the first channel's buffer */
283 for (; b != bufs.audio_end(); ++b) {
284 AudioBuffer& ob (*b);
285 Sample* obuf = ob.data ();
286 for (nframes_t n = 0; n < nframes; ++n) {
287 buf[n] += obuf[n] * scale;
291 /* copy the first channel to every other channel's buffer */
293 b = bufs.audio_begin();
295 for (; b != bufs.audio_end(); ++b) {
296 AudioBuffer& ob (*b);
297 Sample* obuf = ob.data ();
298 memcpy (obuf, buf, sizeof (Sample) * nframes);
304 MonitorProcessor::configure_io (ChanCount in, ChanCount out)
306 allocate_channels (in.n_audio());
307 return Processor::configure_io (in, out);
311 MonitorProcessor::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
317 MonitorProcessor::set_polarity (uint32_t chn, bool invert)
320 _channels[chn].polarity = -1.0f;
322 _channels[chn].polarity = 1.0f;
327 MonitorProcessor::set_dim (uint32_t chn, bool yn)
329 _channels[chn].dim = yn;
333 MonitorProcessor::set_cut (uint32_t chn, bool yn)
336 _channels[chn].cut = 0.0f;
338 _channels[chn].cut = 1.0f;
343 MonitorProcessor::set_solo (uint32_t chn, bool solo)
345 if (solo != _channels[chn].soloed) {
346 _channels[chn].soloed = solo;
359 MonitorProcessor::set_mono (bool yn)
365 MonitorProcessor::set_cut_all (bool yn)
371 MonitorProcessor::set_dim_all (bool yn)
377 MonitorProcessor::display_to_user () const
383 MonitorProcessor::set_dim_level (gain_t val)
389 MonitorProcessor::set_solo_boost_level (gain_t val)
391 _solo_boost_level = val;
395 MonitorProcessor::soloed (uint32_t chn) const
397 return _channels[chn].soloed;
402 MonitorProcessor::inverted (uint32_t chn) const
404 return _channels[chn].polarity < 0.0f;
409 MonitorProcessor::cut (uint32_t chn) const
411 return _channels[chn].cut == 0.0f;
415 MonitorProcessor::dimmed (uint32_t chn) const
417 return _channels[chn].dim;
421 MonitorProcessor::mono () const
427 MonitorProcessor::dim_all () const
433 MonitorProcessor::cut_all () const