1 #include "pbd/convert.h"
5 #include "ardour/amp.h"
7 #include "ardour/audio_buffer.h"
8 #include "ardour/monitor_processor.h"
9 #include "ardour/session.h"
13 using namespace ARDOUR;
17 MonitorProcessor::MonitorProcessor (Session& s)
18 : Processor (s, X_("MonitorOut"))
24 _solo_boost_level = 1.0;
27 MonitorProcessor::MonitorProcessor (Session& s, const XMLNode& node)
30 set_state (node, Stateful::loading_state_version);
34 MonitorProcessor::allocate_channels (uint32_t size)
36 while (_channels.size() > size) {
37 if (_channels.back().soloed) {
45 while (_channels.size() < size) {
46 _channels.push_back (ChannelRecord());
51 MonitorProcessor::set_state (const XMLNode& node, int version)
53 int ret = Processor::set_state (node, version);
59 const XMLProperty* prop;
61 if ((prop = node.property (X_("type"))) == 0) {
62 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings have no type information"))
67 if (prop->value() != X_("monitor")) {
68 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor given unknown XML settings"))
73 if ((prop = node.property (X_("channels"))) == 0) {
74 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings are missing a channel cnt"))
79 allocate_channels (atoi (prop->value()));
81 if ((prop = node.property (X_("dim-level"))) != 0) {
82 double val = atof (prop->value());
86 if ((prop = node.property (X_("solo-boost-level"))) != 0) {
87 double val = atof (prop->value());
88 _solo_boost_level = val;
91 if ((prop = node.property (X_("cut-all"))) != 0) {
92 bool val = string_is_affirmative (prop->value());
95 if ((prop = node.property (X_("dim-all"))) != 0) {
96 bool val = string_is_affirmative (prop->value());
99 if ((prop = node.property (X_("mono"))) != 0) {
100 bool val = string_is_affirmative (prop->value());
104 for (XMLNodeList::const_iterator i = node.children().begin(); i != node.children().end(); ++i) {
106 if ((*i)->name() == X_("Channel")) {
107 if ((prop = (*i)->property (X_("id"))) == 0) {
108 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings are missing an ID"))
115 if (sscanf (prop->value().c_str(), "%u", &chn) != 1) {
116 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings has an unreadable channel ID"))
121 if (chn >= _channels.size()) {
122 error << string_compose (X_("programming error: %1"), X_("MonitorProcessor XML settings has an illegal channel count"))
126 ChannelRecord& cr (_channels[chn]);
128 if ((prop = (*i)->property ("cut")) != 0) {
129 if (string_is_affirmative (prop->value())){
136 if ((prop = (*i)->property ("dim")) != 0) {
137 bool val = string_is_affirmative (prop->value());
141 if ((prop = (*i)->property ("invert")) != 0) {
142 if (string_is_affirmative (prop->value())) {
149 if ((prop = (*i)->property ("solo")) != 0) {
150 bool val = string_is_affirmative (prop->value());
160 for (vector<ChannelRecord>::const_iterator x = _channels.begin(); x != _channels.end(); ++x) {
170 MonitorProcessor::state (bool full)
172 XMLNode& node (Processor::state (full));
175 /* this replaces any existing "type" property */
177 node.add_property (X_("type"), X_("monitor"));
179 snprintf (buf, sizeof(buf), "%.12g", _dim_level);
180 node.add_property (X_("dim-level"), buf);
182 snprintf (buf, sizeof(buf), "%.12g", _solo_boost_level);
183 node.add_property (X_("solo-boost-level"), buf);
185 node.add_property (X_("cut-all"), (_cut_all ? "yes" : "no"));
186 node.add_property (X_("dim-all"), (_dim_all ? "yes" : "no"));
187 node.add_property (X_("mono"), (_mono ? "yes" : "no"));
189 uint32_t limit = _channels.size();
191 snprintf (buf, sizeof (buf), "%u", limit);
192 node.add_property (X_("channels"), buf);
197 for (vector<ChannelRecord>::const_iterator x = _channels.begin(); x != _channels.end(); ++x, ++chn) {
198 chn_node = new XMLNode (X_("Channel"));
200 snprintf (buf, sizeof (buf), "%u", chn);
201 chn_node->add_property ("id", buf);
203 chn_node->add_property (X_("cut"), x->cut == 1.0 ? "no" : "yes");
204 chn_node->add_property (X_("invert"), x->polarity == 1.0 ? "yes" : "no");
205 chn_node->add_property (X_("dim"), x->dim ? "yes" : "no");
206 chn_node->add_property (X_("solo"), x->soloed ? "yes" : "no");
208 node.add_child_nocopy (*chn_node);
215 MonitorProcessor::run (BufferSet& bufs, sframes_t /*start_frame*/, sframes_t /*end_frame*/, nframes_t nframes, bool /*result_required*/)
219 gain_t dim_level_this_time = _dim_level;
220 gain_t global_cut = (_cut_all ? 0.0f : 1.0f);
221 gain_t global_dim = (_dim_all ? dim_level_this_time : 1.0f);
224 if (_session.listening() || _session.soloing()) {
225 solo_boost = _solo_boost_level;
230 for (BufferSet::audio_iterator b = bufs.audio_begin(); b != bufs.audio_end(); ++b) {
232 /* don't double-scale by both track dim and global dim coefficients */
234 gain_t dim_level = (global_dim == 1.0 ? (_channels[chn].dim ? dim_level_this_time : 1.0) : 1.0);
236 if (_channels[chn].soloed) {
237 target_gain = _channels[chn].polarity * _channels[chn].cut * dim_level * global_cut * global_dim * solo_boost;
240 target_gain = _channels[chn].polarity * _channels[chn].cut * dim_level * global_cut * global_dim * solo_boost;
246 if (target_gain != _channels[chn].current_gain || target_gain != 1.0f) {
248 Amp::apply_gain (*b, nframes, _channels[chn].current_gain, target_gain);
249 _channels[chn].current_gain = target_gain;
256 /* chn is now the number of channels, use as a scaling factor when mixing
258 gain_t scale = 1.0/chn;
259 BufferSet::audio_iterator b = bufs.audio_begin();
260 AudioBuffer& ab (*b);
261 Sample* buf = ab.data();
263 /* scale the first channel */
265 for (nframes_t n = 0; n < nframes; ++n) {
269 /* add every other channel into the first channel's buffer */
272 for (; b != bufs.audio_end(); ++b) {
273 AudioBuffer& ob (*b);
274 Sample* obuf = ob.data ();
275 for (nframes_t n = 0; n < nframes; ++n) {
276 buf[n] += obuf[n] * scale;
280 /* copy the first channel to every other channel's buffer */
282 b = bufs.audio_begin();
284 for (; b != bufs.audio_end(); ++b) {
285 AudioBuffer& ob (*b);
286 Sample* obuf = ob.data ();
287 memcpy (obuf, buf, sizeof (Sample) * nframes);
293 MonitorProcessor::configure_io (ChanCount in, ChanCount out)
295 allocate_channels (in.n_audio());
296 return Processor::configure_io (in, out);
300 MonitorProcessor::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
306 MonitorProcessor::set_polarity (uint32_t chn, bool invert)
309 _channels[chn].polarity = -1.0f;
311 _channels[chn].polarity = 1.0f;
316 MonitorProcessor::set_dim (uint32_t chn, bool yn)
318 _channels[chn].dim = yn;
322 MonitorProcessor::set_cut (uint32_t chn, bool yn)
325 _channels[chn].cut = 0.0f;
327 _channels[chn].cut = 1.0f;
332 MonitorProcessor::set_solo (uint32_t chn, bool solo)
334 _channels[chn].soloed = solo;
346 MonitorProcessor::set_mono (bool yn)
352 MonitorProcessor::set_cut_all (bool yn)
358 MonitorProcessor::set_dim_all (bool yn)
364 MonitorProcessor::display_to_user () const
370 MonitorProcessor::set_dim_level (gain_t val)
376 MonitorProcessor::set_solo_boost_level (gain_t val)
378 _solo_boost_level = val;
382 MonitorProcessor::soloed (uint32_t chn) const
384 return _channels[chn].soloed;
389 MonitorProcessor::inverted (uint32_t chn) const
391 return _channels[chn].polarity < 0.0f;
396 MonitorProcessor::cut (uint32_t chn) const
398 return _channels[chn].cut == 0.0f;
402 MonitorProcessor::dimmed (uint32_t chn) const
404 return _channels[chn].dim;
408 MonitorProcessor::mono () const