reset meters when changing metering-point or ports
[ardour.git] / libs / ardour / meter.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify it
5     under the terms of the GNU General Public License as published by the Free
6     Software Foundation; either version 2 of the License, or (at your option)
7     any later version.
8
9     This program is distributed in the hope that it will be useful, but WITHOUT
10     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12     for more details.
13
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <algorithm>
20 #include <cmath>
21
22 #include "pbd/compose.h"
23
24 #include "ardour/audio_buffer.h"
25 #include "ardour/buffer_set.h"
26 #include "ardour/dB.h"
27 #include "ardour/meter.h"
28 #include "ardour/midi_buffer.h"
29 #include "ardour/rc_configuration.h"
30 #include "ardour/runtime_functions.h"
31
32 using namespace std;
33
34 using namespace ARDOUR;
35
36 PBD::Signal0<void> Metering::Meter;
37
38 PeakMeter::PeakMeter (Session& s, const std::string& name)
39     : Processor (s, string_compose ("meter-%1", name)) 
40 {
41 }
42
43
44 /** Get peaks from @a bufs
45  * Input acceptance is lenient - the first n buffers from @a bufs will
46  * be metered, where n was set by the last call to setup(), excess meters will
47  * be set to 0.
48  */
49 void
50 PeakMeter::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool)
51 {
52         if (!_active && !_pending_active) {
53                 return;
54         }
55
56         // cerr << "meter " << name() << " runs with " << bufs.available() << " inputs\n";
57
58         const uint32_t n_audio = min (current_meters.n_audio(), bufs.count().n_audio());
59         const uint32_t n_midi  = min (current_meters.n_midi(), bufs.count().n_midi());
60
61         uint32_t n = 0;
62
63         // Meter MIDI in to the first n_midi peaks
64         for (uint32_t i = 0; i < n_midi; ++i, ++n) {
65                 float val = 0.0f;
66                 MidiBuffer& buf (bufs.get_midi(i));
67                 
68                 for (MidiBuffer::iterator e = buf.begin(); e != buf.end(); ++e) {
69                         const Evoral::MIDIEvent<framepos_t> ev(*e, false);
70                         if (ev.is_note_on()) {
71                                 const float this_vel = log(ev.buffer()[2] / 127.0 * (M_E*M_E-M_E) + M_E) - 1.0;
72                                 if (this_vel > val) {
73                                         val = this_vel;
74                                 }
75                         } else {
76                                 val += 1.0 / bufs.get_midi(n).capacity();
77                                 if (val > 1.0) {
78                                         val = 1.0;
79                                 }
80                         }
81                 }
82                 _peak_power[n] = max (val, _peak_power[n]);
83         }
84
85         // Meter audio in to the rest of the peaks
86         for (uint32_t i = 0; i < n_audio; ++i, ++n) {
87                 _peak_power[n] = compute_peak (bufs.get_audio(i).data(), nframes, _peak_power[n]);
88         }
89
90         // Zero any excess peaks
91         for (uint32_t i = n; i < _peak_power.size(); ++i) {
92                 _peak_power[i] = 0.0f;
93         }
94
95         _active = _pending_active;
96 }
97
98 void
99 PeakMeter::reset ()
100 {
101         for (size_t i = 0; i < _peak_power.size(); ++i) {
102                 _peak_power[i] = 0.0f;
103         }
104 }
105
106 void
107 PeakMeter::reset_max ()
108 {
109         for (size_t i = 0; i < _max_peak_power.size(); ++i) {
110                 _max_peak_power[i] = -INFINITY;
111         }
112 }
113
114 bool
115 PeakMeter::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
116 {
117         out = in;
118         return true;
119 }
120
121 bool
122 PeakMeter::configure_io (ChanCount in, ChanCount out)
123 {
124         if (out != in) { // always 1:1
125                 return false;
126         }
127
128         current_meters = in;
129
130         reset_max_channels (in);
131
132         return Processor::configure_io (in, out);
133 }
134
135 void
136 PeakMeter::reflect_inputs (const ChanCount& in)
137 {
138         current_meters = in;
139
140         const size_t limit = min (_peak_power.size(), (size_t) current_meters.n_total ());
141         const size_t n_midi  = min (_peak_power.size(), (size_t) current_meters.n_midi());
142
143         for (size_t n = 0; n < limit; ++n) {
144                 if (n < n_midi) {
145                         _visible_peak_power[n] = 0;
146                 } else {
147                         _visible_peak_power[n] = -INFINITY;
148                 }
149         }
150         reset_max();
151
152         ConfigurationChanged (in, in); /* EMIT SIGNAL */
153 }
154
155 void
156 PeakMeter::reset_max_channels (const ChanCount& chn)
157 {
158         uint32_t const limit = chn.n_total();
159
160         while (_peak_power.size() > limit) {
161                 _peak_power.pop_back();
162                 _visible_peak_power.pop_back();
163                 _max_peak_power.pop_back();
164         }
165
166         while (_peak_power.size() < limit) {
167                 _peak_power.push_back(0);
168                 _visible_peak_power.push_back(minus_infinity());
169                 _max_peak_power.push_back(minus_infinity());
170         }
171
172         assert(_peak_power.size() == limit);
173         assert(_visible_peak_power.size() == limit);
174         assert(_max_peak_power.size() == limit);
175 }
176
177 /** To be driven by the Meter signal from IO.
178  * Caller MUST hold its own processor_lock to prevent reconfiguration
179  * of meter size during this call.
180  */
181
182 void
183 PeakMeter::meter ()
184 {
185         if (!_active) {
186                 return;
187         }
188
189         assert(_visible_peak_power.size() == _peak_power.size());
190
191         const size_t limit = min (_peak_power.size(), (size_t) current_meters.n_total ());
192
193         for (size_t n = 0; n < limit; ++n) {
194
195                 /* grab peak since last read */
196
197                 float new_peak = _peak_power[n]; /* XXX we should use atomic exchange from here ... */
198                 _peak_power[n] = 0;              /* ... to here */
199
200                 /* compute new visible value using falloff */
201
202                 if (new_peak > 0.0) {
203                         new_peak = fast_coefficient_to_dB (new_peak);
204                 } else {
205                         new_peak = minus_infinity();
206                 }
207
208                 /* update max peak */
209
210                 _max_peak_power[n] = std::max (new_peak, _max_peak_power[n]);
211
212                 if (Config->get_meter_falloff() == 0.0f || new_peak > _visible_peak_power[n]) {
213                         _visible_peak_power[n] = new_peak;
214                 } else {
215                         // do falloff
216                         new_peak = _visible_peak_power[n] - (Config->get_meter_falloff() * 0.01f);
217                         _visible_peak_power[n] = std::max (new_peak, -INFINITY);
218                 }
219         }
220 }
221
222 XMLNode&
223 PeakMeter::state (bool full_state)
224 {
225         XMLNode& node (Processor::state (full_state));
226         node.add_property("type", "meter");
227         return node;
228 }
229
230