Logarithmic MIDI metering.
[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_events.h>
27
28 namespace ARDOUR {
29
30
31 /** Get peaks from @a bufs
32  * Input acceptance is lenient - the first n buffers from @a bufs will
33  * be metered, where n was set by the last call to setup(), excess meters will
34  * be set to 0.
35  */
36 void
37 PeakMeter::run (BufferSet& bufs, nframes_t nframes, nframes_t offset)
38 {
39         size_t meterable = std::min(bufs.count().n_total(), _peak_power.size());
40
41         size_t n = 0;
42
43         // Meter what we have (midi)
44         for ( ; n < meterable && n < bufs.count().n_midi(); ++n) {
45                 
46                 float val = 0;
47                 
48                 // GUI needs a better MIDI meter, not much information can be
49                 // expressed through peaks alone
50                 const unsigned n_events = bufs.get_midi(n).size();
51                 for (size_t i=0; i < n_events; ++i) {
52                         const MidiEvent& ev = bufs.get_midi(n)[i];
53                         if ((ev.buffer[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
54                                 const float this_vel = log(ev.buffer[2] / 127.0 * (M_E*M_E-M_E) + M_E) - 1.0;
55                                 //printf("V %d -> %f\n", (int)((Byte)ev.buffer[2]), this_vel);
56                                 if (this_vel > val)
57                                         val = this_vel;
58                         } else {
59                                 val += 1.0 / bufs.get_midi(n).capacity();
60                                 if (val > 1.0)
61                                         val = 1.0;
62                         }
63                 }
64                         
65                 _peak_power[n] = val;
66
67         }
68         
69         // Meter what we have (audio)
70         for ( ; n < meterable && n < bufs.count().n_audio(); ++n) {
71                 _peak_power[n] = compute_peak (bufs.get_audio(n).data(nframes, offset), nframes, _peak_power[n]); 
72         }
73
74         // Zero any excess peaks
75         for (size_t n = meterable; n < _peak_power.size(); ++n) {
76                 _peak_power[n] = 0;
77         }
78 }
79
80 void
81 PeakMeter::reset ()
82 {
83         for (size_t i = 0; i < _peak_power.size(); ++i) {
84                 _peak_power[i] = 0;
85         }
86 }
87
88 void
89 PeakMeter::reset_max ()
90 {
91         for (size_t i = 0; i < _max_peak_power.size(); ++i) {
92                 _max_peak_power[i] = -INFINITY;
93         }
94 }
95
96 void
97 PeakMeter::setup (const ChanCount& in)
98 {
99         uint32_t limit = in.n_total();
100
101         while (_peak_power.size() > limit) {
102                 _peak_power.pop_back();
103                 _visible_peak_power.pop_back();
104                 _max_peak_power.pop_back();
105         }
106
107         while (_peak_power.size() < limit) {
108                 _peak_power.push_back(0);
109                 _visible_peak_power.push_back(minus_infinity());
110                 _max_peak_power.push_back(minus_infinity());
111         }
112
113         assert(_peak_power.size() == limit);
114         assert(_visible_peak_power.size() == limit);
115         assert(_max_peak_power.size() == limit);
116 }
117
118 /** To be driven by the Meter signal from IO.
119  * Caller MUST hold io_lock!
120  */
121 void
122 PeakMeter::meter ()
123 {
124         assert(_visible_peak_power.size() == _peak_power.size());
125
126         const size_t limit = _peak_power.size();
127
128         for (size_t n = 0; n < limit; ++n) {
129
130                 /* XXX we should use atomic exchange here */
131
132                 /* grab peak since last read */
133
134                 float new_peak = _peak_power[n];
135                 _peak_power[n] = 0;
136                 
137                 /* compute new visible value using falloff */
138
139                 if (new_peak > 0.0) {
140                         new_peak = coefficient_to_dB (new_peak);
141                 } else {
142                         new_peak = minus_infinity();
143                 }
144                 
145                 /* update max peak */
146                 
147                 _max_peak_power[n] = std::max (new_peak, _max_peak_power[n]);
148                 
149                 if (Config->get_meter_falloff() == 0.0f || new_peak > _visible_peak_power[n]) {
150                         _visible_peak_power[n] = new_peak;
151                 } else {
152                         // do falloff
153                         new_peak = _visible_peak_power[n] - (Config->get_meter_falloff() * 0.01f);
154                         _visible_peak_power[n] = std::max (new_peak, -INFINITY);
155                 }
156         }
157 }
158
159 } // namespace ARDOUR