Merged with trunk R992.
[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 audio 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 (BufferSet& bufs, jack_nframes_t nframes, jack_nframes_t offset)
37 {
38         size_t meterable = std::min(bufs.count().get(DataType::AUDIO), _peak_power.size());
39
40         // Meter what we have
41         for (size_t n = 0; n < meterable; ++n) {
42                 _peak_power[n] = compute_peak (bufs.get_audio(n).data(nframes, offset), nframes, _peak_power[n]); 
43         }
44
45         // Zero any excess peaks
46         for (size_t n = meterable; n < _peak_power.size(); ++n) {
47                 _peak_power[n] = 0;
48         }
49 }
50
51 void
52 PeakMeter::reset ()
53 {
54         for (size_t i = 0; i < _peak_power.size(); ++i) {
55                 _peak_power[i] = 0;
56         }
57 }
58
59 void
60 PeakMeter::setup (const ChanCount& in)
61 {
62         uint32_t limit = in.get(DataType::AUDIO);
63
64         while (_peak_power.size() > limit) {
65                 _peak_power.pop_back();
66                 _visible_peak_power.pop_back();
67         }
68
69         while (_peak_power.size() < limit) {
70                 _peak_power.push_back (0);
71                 _visible_peak_power.push_back (0);
72         }
73
74         assert(_peak_power.size() == limit);
75         assert(_visible_peak_power.size() == limit);
76 }
77
78 /** To be driven by the Meter signal from IO.
79  * Caller MUST hold io_lock!
80  */
81 void
82 PeakMeter::meter ()
83 {
84         assert(_visible_peak_power.size() == _peak_power.size());
85
86         const size_t limit = _peak_power.size();
87
88         for (size_t n = 0; n < limit; ++n) {
89
90                 /* XXX we should use atomic exchange here */
91
92                 /* grab peak since last read */
93
94                 float new_peak = _peak_power[n];
95                 _peak_power[n] = 0;
96                 
97                 /* compute new visible value using falloff */
98
99                 if (new_peak > 0.0) {
100                         new_peak = coefficient_to_dB (new_peak);
101                 } else {
102                         new_peak = minus_infinity();
103                 }
104                 
105                 if (Config->get_meter_falloff() == 0.0f || new_peak > _visible_peak_power[n]) {
106                         _visible_peak_power[n] = new_peak;
107                 } else {
108                         // do falloff
109                         new_peak = _visible_peak_power[n] - Config->get_meter_falloff();
110                         _visible_peak_power[n] = std::max (new_peak, -INFINITY);
111                 }
112         }
113 }
114
115 } // namespace ARDOUR