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