Merge branch 'patches' of https://github.com/jdekozak/ardour
[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         ConfigurationChanged (in, in); /* EMIT SIGNAL */
141 }
142
143 void
144 PeakMeter::reset_max_channels (const ChanCount& chn)
145 {
146         uint32_t const limit = chn.n_total();
147
148         while (_peak_power.size() > limit) {
149                 _peak_power.pop_back();
150                 _visible_peak_power.pop_back();
151                 _max_peak_power.pop_back();
152         }
153
154         while (_peak_power.size() < limit) {
155                 _peak_power.push_back(0);
156                 _visible_peak_power.push_back(minus_infinity());
157                 _max_peak_power.push_back(minus_infinity());
158         }
159
160         assert(_peak_power.size() == limit);
161         assert(_visible_peak_power.size() == limit);
162         assert(_max_peak_power.size() == limit);
163 }
164
165 /** To be driven by the Meter signal from IO.
166  * Caller MUST hold its own processor_lock to prevent reconfiguration
167  * of meter size during this call.
168  */
169
170 void
171 PeakMeter::meter ()
172 {
173         if (!_active) {
174                 return;
175         }
176
177         assert(_visible_peak_power.size() == _peak_power.size());
178
179         const size_t limit = min (_peak_power.size(), (size_t) current_meters.n_total ());
180
181         for (size_t n = 0; n < limit; ++n) {
182
183                 /* grab peak since last read */
184
185                 float new_peak = _peak_power[n]; /* XXX we should use atomic exchange from here ... */
186                 _peak_power[n] = 0;              /* ... to here */
187
188                 /* compute new visible value using falloff */
189
190                 if (new_peak > 0.0) {
191                         new_peak = fast_coefficient_to_dB (new_peak);
192                 } else {
193                         new_peak = minus_infinity();
194                 }
195
196                 /* update max peak */
197
198                 _max_peak_power[n] = std::max (new_peak, _max_peak_power[n]);
199
200                 if (Config->get_meter_falloff() == 0.0f || new_peak > _visible_peak_power[n]) {
201                         _visible_peak_power[n] = new_peak;
202                 } else {
203                         // do falloff
204                         new_peak = _visible_peak_power[n] - (Config->get_meter_falloff() * 0.01f);
205                         _visible_peak_power[n] = std::max (new_peak, -INFINITY);
206                 }
207         }
208 }
209
210 XMLNode&
211 PeakMeter::state (bool full_state)
212 {
213         XMLNode& node (Processor::state (full_state));
214         node.add_property("type", "meter");
215         return node;
216 }
217
218