Remove all use of nframes_t.
[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 "ardour/meter.h"
20 #include <algorithm>
21 #include <cmath>
22 #include "ardour/buffer_set.h"
23 #include "ardour/peak.h"
24 #include "ardour/dB.h"
25 #include "ardour/session.h"
26 #include "ardour/midi_buffer.h"
27 #include "ardour/audio_buffer.h"
28 #include "ardour/runtime_functions.h"
29
30 using namespace std;
31
32 using namespace ARDOUR;
33
34 PBD::Signal0<void> Metering::Meter;
35
36 /** Get peaks from @a bufs
37  * Input acceptance is lenient - the first n buffers from @a bufs will
38  * be metered, where n was set by the last call to setup(), excess meters will
39  * be set to 0.
40  */
41 void
42 PeakMeter::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool)
43 {
44         if (!_active && !_pending_active) {
45                 return;
46         }
47
48         const uint32_t n_audio = min(_configured_input.n_audio(), bufs.count().n_audio());
49         const uint32_t n_midi  = min(_configured_input.n_midi(), bufs.count().n_midi());
50
51         uint32_t n = 0;
52
53         // Meter MIDI in to the first n_midi peaks
54         for (uint32_t i = 0; i < n_midi; ++i, ++n) {
55                 float val = 0.0f;
56                 for (MidiBuffer::iterator e = bufs.get_midi(i).begin(); e != bufs.get_midi(i).end(); ++e) {
57                         const Evoral::MIDIEvent<framepos_t> ev(*e, false);
58                         if (ev.is_note_on()) {
59                                 const float this_vel = log(ev.buffer()[2] / 127.0 * (M_E*M_E-M_E) + M_E) - 1.0;
60                                 if (this_vel > val) {
61                                         val = this_vel;
62                                 }
63                         } else {
64                                 val += 1.0 / bufs.get_midi(n).capacity();
65                                 if (val > 1.0) {
66                                         val = 1.0;
67                                 }
68                         }
69                 }
70                 _peak_power[n] = max (val, _peak_power[n]);
71         }
72
73         // Meter audio in to the rest of the peaks
74         for (uint32_t i = 0; i < n_audio; ++i, ++n) {
75                 _peak_power[n] = compute_peak (bufs.get_audio(i).data(), nframes, _peak_power[n]);
76         }
77
78         // Zero any excess peaks
79         for (uint32_t i = n; i < _peak_power.size(); ++i) {
80                 _peak_power[i] = 0.0f;
81         }
82
83         _active = _pending_active;
84 }
85
86 void
87 PeakMeter::reset ()
88 {
89         for (size_t i = 0; i < _peak_power.size(); ++i) {
90                 _peak_power[i] = 0.0f;
91         }
92 }
93
94 void
95 PeakMeter::reset_max ()
96 {
97         for (size_t i = 0; i < _max_peak_power.size(); ++i) {
98                 _max_peak_power[i] = -INFINITY;
99         }
100 }
101
102 bool
103 PeakMeter::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
104 {
105         out = in;
106         return true;
107 }
108
109 bool
110 PeakMeter::configure_io (ChanCount in, ChanCount out)
111 {
112         if (out != in) { // always 1:1
113                 return false;
114         }
115
116         current_meters = in;
117
118         reset_max_channels (in);
119
120         return Processor::configure_io (in, out);
121 }
122
123 void
124 PeakMeter::reflect_inputs (const ChanCount& in)
125 {
126         current_meters = in;
127 }
128
129 void
130 PeakMeter::reset_max_channels (const ChanCount& chn)
131 {
132         uint32_t limit = chn.n_total();
133
134         while (_peak_power.size() > limit) {
135                 _peak_power.pop_back();
136                 _visible_peak_power.pop_back();
137                 _max_peak_power.pop_back();
138         }
139
140         while (_peak_power.size() < limit) {
141                 _peak_power.push_back(0);
142                 _visible_peak_power.push_back(minus_infinity());
143                 _max_peak_power.push_back(minus_infinity());
144         }
145
146         assert(_peak_power.size() == limit);
147         assert(_visible_peak_power.size() == limit);
148         assert(_max_peak_power.size() == limit);
149 }
150
151 /** To be driven by the Meter signal from IO.
152  * Caller MUST hold its own processor_lock to prevent reconfiguration
153  * of meter size during this call.
154  */
155
156 void
157 PeakMeter::meter ()
158 {
159         if (!_active) {
160                 return;
161         }
162
163         assert(_visible_peak_power.size() == _peak_power.size());
164
165         const size_t limit = min (_peak_power.size(), (size_t) current_meters.n_total ());
166
167         for (size_t n = 0; n < limit; ++n) {
168
169                 /* grab peak since last read */
170
171                 float new_peak = _peak_power[n]; /* XXX we should use atomic exchange from here ... */
172                 _peak_power[n] = 0;              /* ... to here */
173
174                 /* compute new visible value using falloff */
175
176                 if (new_peak > 0.0) {
177                         new_peak = fast_coefficient_to_dB (new_peak);
178                 } else {
179                         new_peak = minus_infinity();
180                 }
181
182                 /* update max peak */
183
184                 _max_peak_power[n] = std::max (new_peak, _max_peak_power[n]);
185
186                 if (Config->get_meter_falloff() == 0.0f || new_peak > _visible_peak_power[n]) {
187                         _visible_peak_power[n] = new_peak;
188                 } else {
189                         // do falloff
190                         new_peak = _visible_peak_power[n] - (Config->get_meter_falloff() * 0.01f);
191                         _visible_peak_power[n] = std::max (new_peak, -INFINITY);
192                 }
193         }
194 }
195
196 XMLNode&
197 PeakMeter::state (bool full_state)
198 {
199         XMLNode& node (Processor::state (full_state));
200         node.add_property("type", "meter");
201         return node;
202 }
203
204