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