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;
28 MonitorProcessor::MonitorProcessor (Session& s, const XMLNode& node)
31 set_state (node, Stateful::loading_state_version);
35 MonitorProcessor::allocate_channels (uint32_t size)
37 while (_channels.size() > size) {
38 if (_channels.back().soloed) {
46 while (_channels.size() < size) {
47 _channels.push_back (ChannelRecord());
52 MonitorProcessor::set_state (const XMLNode& node, int version)
54 int ret = Processor::set_state (node, version);
60 const XMLProperty* prop;
62 if ((prop = node.property (X_("type"))) == 0) {
63 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings have no type information"))
68 if (prop->value() != X_("monitor")) {
69 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor given unknown XML settings"))
74 if ((prop = node.property (X_("channels"))) == 0) {
75 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings are missing a channel cnt"))
80 allocate_channels (atoi (prop->value()));
82 if ((prop = node.property (X_("dim-level"))) != 0) {
83 double val = atof (prop->value());
87 if ((prop = node.property (X_("solo-boost-level"))) != 0) {
88 double val = atof (prop->value());
89 _solo_boost_level = val;
92 if ((prop = node.property (X_("cut-all"))) != 0) {
93 bool val = string_is_affirmative (prop->value());
96 if ((prop = node.property (X_("dim-all"))) != 0) {
97 bool val = string_is_affirmative (prop->value());
100 if ((prop = node.property (X_("mono"))) != 0) {
101 bool val = string_is_affirmative (prop->value());
105 for (XMLNodeList::const_iterator i = node.children().begin(); i != node.children().end(); ++i) {
107 if ((*i)->name() == X_("Channel")) {
108 if ((prop = (*i)->property (X_("id"))) == 0) {
109 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings are missing an ID"))
116 if (sscanf (prop->value().c_str(), "%u", &chn) != 1) {
117 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings has an unreadable channel ID"))
122 if (chn >= _channels.size()) {
123 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings has an illegal channel count"))
127 ChannelRecord& cr (_channels[chn]);
129 if ((prop = (*i)->property ("cut")) != 0) {
130 if (string_is_affirmative (prop->value())){
137 if ((prop = (*i)->property ("dim")) != 0) {
138 bool val = string_is_affirmative (prop->value());
142 if ((prop = (*i)->property ("invert")) != 0) {
143 if (string_is_affirmative (prop->value())) {
150 if ((prop = (*i)->property ("solo")) != 0) {
151 bool val = string_is_affirmative (prop->value());
161 for (vector<ChannelRecord>::const_iterator x = _channels.begin(); x != _channels.end(); ++x) {
171 MonitorProcessor::state (bool full)
173 XMLNode& node (Processor::state (full));
176 /* this replaces any existing "type" property */
178 node.add_property (X_("type"), X_("monitor"));
180 snprintf (buf, sizeof(buf), "%.12g", _dim_level);
181 node.add_property (X_("dim-level"), buf);
183 snprintf (buf, sizeof(buf), "%.12g", _solo_boost_level);
184 node.add_property (X_("solo-boost-level"), buf);
186 node.add_property (X_("cut-all"), (_cut_all ? "yes" : "no"));
187 node.add_property (X_("dim-all"), (_dim_all ? "yes" : "no"));
188 node.add_property (X_("mono"), (_mono ? "yes" : "no"));
190 uint32_t limit = _channels.size();
192 snprintf (buf, sizeof (buf), "%u", limit);
193 node.add_property (X_("channels"), buf);
198 for (vector<ChannelRecord>::const_iterator x = _channels.begin(); x != _channels.end(); ++x, ++chn) {
199 chn_node = new XMLNode (X_("Channel"));
201 snprintf (buf, sizeof (buf), "%u", chn);
202 chn_node->add_property ("id", buf);
204 chn_node->add_property (X_("cut"), x->cut == 1.0 ? "no" : "yes");
205 chn_node->add_property (X_("invert"), x->polarity == 1.0 ? "no" : "yes");
206 chn_node->add_property (X_("dim"), x->dim ? "yes" : "no");
207 chn_node->add_property (X_("solo"), x->soloed ? "yes" : "no");
209 node.add_child_nocopy (*chn_node);
216 MonitorProcessor::run (BufferSet& bufs, sframes_t /*start_frame*/, sframes_t /*end_frame*/, nframes_t nframes, bool /*result_required*/)
220 gain_t dim_level_this_time = _dim_level;
221 gain_t global_cut = (_cut_all ? 0.0f : 1.0f);
222 gain_t global_dim = (_dim_all ? dim_level_this_time : 1.0f);
225 if (_session.listening() || _session.soloing()) {
226 solo_boost = _solo_boost_level;
231 for (BufferSet::audio_iterator b = bufs.audio_begin(); b != bufs.audio_end(); ++b) {
233 /* don't double-scale by both track dim and global dim coefficients */
235 gain_t dim_level = (global_dim == 1.0 ? (_channels[chn].dim ? dim_level_this_time : 1.0) : 1.0);
237 if (_channels[chn].soloed) {
238 target_gain = _channels[chn].polarity * _channels[chn].cut * dim_level * global_cut * global_dim * solo_boost;
241 target_gain = _channels[chn].polarity * _channels[chn].cut * dim_level * global_cut * global_dim * solo_boost;
247 DEBUG_TRACE (DEBUG::Monitor,
248 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",
255 _channels[chn].polarity,
257 _channels[chn].soloed,
261 if (target_gain != _channels[chn].current_gain || target_gain != 1.0f) {
263 Amp::apply_gain (*b, nframes, _channels[chn].current_gain, target_gain);
264 _channels[chn].current_gain = target_gain;
271 /* chn is now the number of channels, use as a scaling factor when mixing
273 gain_t scale = 1.0/chn;
274 BufferSet::audio_iterator b = bufs.audio_begin();
275 AudioBuffer& ab (*b);
276 Sample* buf = ab.data();
278 /* scale the first channel */
280 for (nframes_t n = 0; n < nframes; ++n) {
284 /* add every other channel into the first channel's buffer */
287 for (; b != bufs.audio_end(); ++b) {
288 AudioBuffer& ob (*b);
289 Sample* obuf = ob.data ();
290 for (nframes_t n = 0; n < nframes; ++n) {
291 buf[n] += obuf[n] * scale;
295 /* copy the first channel to every other channel's buffer */
297 b = bufs.audio_begin();
299 for (; b != bufs.audio_end(); ++b) {
300 AudioBuffer& ob (*b);
301 Sample* obuf = ob.data ();
302 memcpy (obuf, buf, sizeof (Sample) * nframes);
308 MonitorProcessor::configure_io (ChanCount in, ChanCount out)
310 allocate_channels (in.n_audio());
311 return Processor::configure_io (in, out);
315 MonitorProcessor::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
321 MonitorProcessor::set_polarity (uint32_t chn, bool invert)
324 _channels[chn].polarity = -1.0f;
326 _channels[chn].polarity = 1.0f;
331 MonitorProcessor::set_dim (uint32_t chn, bool yn)
333 _channels[chn].dim = yn;
337 MonitorProcessor::set_cut (uint32_t chn, bool yn)
340 _channels[chn].cut = 0.0f;
342 _channels[chn].cut = 1.0f;
347 MonitorProcessor::set_solo (uint32_t chn, bool solo)
349 if (solo != _channels[chn].soloed) {
350 _channels[chn].soloed = solo;
363 MonitorProcessor::set_mono (bool yn)
369 MonitorProcessor::set_cut_all (bool yn)
375 MonitorProcessor::set_dim_all (bool yn)
381 MonitorProcessor::display_to_user () const
387 MonitorProcessor::set_dim_level (gain_t val)
393 MonitorProcessor::set_solo_boost_level (gain_t val)
395 _solo_boost_level = val;
399 MonitorProcessor::soloed (uint32_t chn) const
401 return _channels[chn].soloed;
406 MonitorProcessor::inverted (uint32_t chn) const
408 return _channels[chn].polarity < 0.0f;
413 MonitorProcessor::cut (uint32_t chn) const
415 return _channels[chn].cut == 0.0f;
419 MonitorProcessor::dimmed (uint32_t chn) const
421 return _channels[chn].dim;
425 MonitorProcessor::mono () const
431 MonitorProcessor::dim_all () const
437 MonitorProcessor::cut_all () const