vamp-true-peak: collect peak locations >= -1dBTP
[ardour.git] / libs / vamp-plugins / TruePeak.h
1 /*
2  *  Copyright (C) 2013-2016 Robin Gareus <robin@gareus.org>
3  *  Copyright (C) 2006-2012 Fons Adriaensen <fons@linuxaudio.org>
4  *  Copyright (C) 2006 Chris Cannam
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef _TruePeak_PLUGIN_H_
21 #define _TruePeak_PLUGIN_H_
22
23 #include <vamp-sdk/Plugin.h>
24
25 #include <pthread.h>
26
27 namespace TruePeakMeter {
28
29 class Resampler_mutex
30 {
31 private:
32
33         friend class Resampler_table;
34
35         Resampler_mutex (void) { pthread_mutex_init (&_mutex, 0); }
36         ~Resampler_mutex (void) { pthread_mutex_destroy (&_mutex); }
37         void lock (void) { pthread_mutex_lock (&_mutex); }
38         void unlock (void) { pthread_mutex_unlock (&_mutex); }
39
40         pthread_mutex_t  _mutex;
41 };
42
43 class Resampler_table
44 {
45 private:
46
47         Resampler_table (double fr, unsigned int hl, unsigned int np);
48         ~Resampler_table (void);
49
50         friend class Resampler;
51         friend class VResampler;
52
53         Resampler_table     *_next;
54         unsigned int         _refc;
55         float               *_ctab;
56         double               _fr;
57         unsigned int         _hl;
58         unsigned int         _np;
59
60         static Resampler_table *create (double fr, unsigned int hl, unsigned int np);
61         static void destroy (Resampler_table *T);
62
63         static Resampler_table  *_list;
64         static Resampler_mutex   _mutex;
65 };
66
67 class Resampler
68 {
69 public:
70
71         Resampler (void);
72         ~Resampler (void);
73
74         int  setup (unsigned int fs_inp,
75                         unsigned int fs_out,
76                         unsigned int nchan,
77                         unsigned int hlen);
78
79         int  setup (unsigned int fs_inp,
80                         unsigned int fs_out,
81                         unsigned int nchan,
82                         unsigned int hlen,
83                         double       frel);
84
85         void   clear (void);
86         int    reset (void);
87         int    nchan (void) const { return _nchan; }
88         int    filtlen (void) const { return inpsize (); } // Deprecated
89         int    inpsize (void) const;
90         double inpdist (void) const;
91         int    process (void);
92
93         unsigned int         inp_count;
94         unsigned int         out_count;
95         float const         *inp_data;
96         float               *out_data;
97         void                *inp_list;
98         void                *out_list;
99
100 private:
101
102         Resampler_table     *_table;
103         unsigned int         _nchan;
104         unsigned int         _inmax;
105         unsigned int         _index;
106         unsigned int         _nread;
107         unsigned int         _nzero;
108         unsigned int         _phase;
109         unsigned int         _pstep;
110         float               *_buff;
111         void                *_dummy [8];
112 };
113
114 class TruePeakdsp
115 {
116 public:
117
118         TruePeakdsp (void);
119         ~TruePeakdsp (void);
120
121         void process (float const *, int n);
122         float read (void);
123         void  read (float &m, float &p);
124         void  reset (void);
125
126         void init (float fsamp);
127
128 private:
129
130         float      _m;
131         float      _p;
132         bool       _res;
133         bool       _res_peak;
134         float     *_buf;
135         Resampler  _src;
136 };
137
138 }; // namespace TruePeakMeter
139
140 class VampTruePeak : public Vamp::Plugin
141 {
142 public:
143         VampTruePeak(float inputSampleRate);
144         virtual ~VampTruePeak();
145
146         size_t getMinChannelCount() const { return 1; }
147         size_t getMaxChannelCount() const { return 1; }
148         size_t getPreferredBlockSize () const { return 1024; }
149         bool initialise(size_t channels, size_t stepSize, size_t blockSize);
150         void reset();
151
152         InputDomain getInputDomain() const { return TimeDomain; }
153
154         std::string getIdentifier() const;
155         std::string getName() const;
156         std::string getDescription() const;
157         std::string getMaker() const;
158         int getPluginVersion() const;
159         std::string getCopyright() const;
160
161         OutputList getOutputDescriptors() const;
162
163         FeatureSet process(const float *const *inputBuffers,
164                         Vamp::RealTime timestamp);
165
166         FeatureSet getRemainingFeatures();
167
168 protected:
169         size_t m_blockSize;
170
171 private:
172         TruePeakMeter::TruePeakdsp _meter;
173         Feature _above_m1;
174         unsigned int m_rate;
175 };
176
177 #endif