libardour wrapper for EBUr128 VAMP plugin
[ardour.git] / libs / ardour / ebur128_analysis.cc
1 /*
2     Copyright (C) 2015 Robin Gareus
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <cmath>
21 #include "ardour/ebur128_analysis.h"
22
23 #include "i18n.h"
24
25 using namespace Vamp;
26 using namespace ARDOUR;
27 using namespace std;
28
29 /* need a static initializer function for this */
30
31 EBUr128Analysis::EBUr128Analysis (float sr)
32         : AudioAnalyser (sr, X_("libardourvampplugins:ebur128"))
33         , _loudness (0)
34         , _loudness_range (0)
35 {
36 }
37
38 EBUr128Analysis::~EBUr128Analysis()
39 {
40 }
41
42 int
43 EBUr128Analysis::run (Readable* src)
44 {
45         int ret = -1;
46         bool done = false;
47         framecnt_t len = src->readable_length();
48         framepos_t pos = 0;
49         uint32_t n_channels = src->n_channels();
50         Plugin::FeatureSet features;
51
52         plugin->reset ();
53         if (!plugin->initialise (n_channels, stepsize, bufsize)) {
54                 return -1;
55         }
56
57         float** bufs = (float**) malloc(n_channels * sizeof(float*));
58         for (uint32_t c = 0; c < n_channels; ++c) {
59                 bufs[c] = (float*) malloc(bufsize * sizeof(float));
60         }
61
62         while (!done) {
63                 framecnt_t to_read;
64                 to_read = min ((len - pos), (framecnt_t) bufsize);
65
66                 for (uint32_t c = 0; c < n_channels; ++c) {
67                         if (src->read (bufs[c], pos, to_read, c) != to_read) {
68                                 goto out;
69                         }
70                         /* zero fill buffer if necessary */
71                         if (to_read != bufsize) {
72                                 memset (bufs[c] + to_read, 0, (bufsize - to_read) * sizeof (float));
73                         }
74                 }
75
76                 plugin->process (bufs, RealTime::fromSeconds ((double) pos / sample_rate));
77
78                 pos += min (stepsize, to_read);
79
80                 if (pos >= len) {
81                         done = true;
82                 }
83         }
84
85         features = plugin->getRemainingFeatures ();
86
87         if (use_features (features, 0)) {
88                 goto out;
89         }
90
91         ret = 0;
92
93 out:
94         for (uint32_t c = 0; c < n_channels; ++c) {
95                 free (bufs[c]);
96         }
97         free (bufs);
98
99         return ret;
100 }
101
102 int
103 EBUr128Analysis::use_features (Plugin::FeatureSet& features, ostream* out)
104 {
105         if (features.empty() || features.size() != 2) {
106                 return 0;
107         }
108         _loudness = features[0][0].values[0];
109         _loudness_range = features[1][0].values[0];
110
111         return 0;
112 }