fix crash when copy'ing latent plugins
[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 <cstring>
22
23 #include "ardour/ebur128_analysis.h"
24
25 #include "pbd/i18n.h"
26
27 using namespace Vamp;
28 using namespace ARDOUR;
29 using namespace std;
30
31 /* need a static initializer function for this */
32
33 EBUr128Analysis::EBUr128Analysis (float sr)
34         : AudioAnalyser (sr, X_("libardourvampplugins:ebur128"))
35         , _loudness (0)
36         , _loudness_range (0)
37 {
38 }
39
40 EBUr128Analysis::~EBUr128Analysis()
41 {
42 }
43
44 int
45 EBUr128Analysis::run (Readable* src)
46 {
47         int ret = -1;
48         bool done = false;
49         framecnt_t len = src->readable_length();
50         framepos_t pos = 0;
51         uint32_t n_channels = src->n_channels();
52         Plugin::FeatureSet features;
53
54         plugin->reset ();
55         if (!plugin->initialise (n_channels, stepsize, bufsize)) {
56                 return -1;
57         }
58
59         float** bufs = (float**) malloc(n_channels * sizeof(float*));
60         for (uint32_t c = 0; c < n_channels; ++c) {
61                 bufs[c] = (float*) malloc(bufsize * sizeof(float));
62         }
63
64         while (!done) {
65                 framecnt_t to_read;
66                 to_read = min ((len - pos), (framecnt_t) bufsize);
67
68                 for (uint32_t c = 0; c < n_channels; ++c) {
69                         if (src->read (bufs[c], pos, to_read, c) != to_read) {
70                                 goto out;
71                         }
72                         /* zero fill buffer if necessary */
73                         if (to_read != bufsize) {
74                                 memset (bufs[c] + to_read, 0, (bufsize - to_read) * sizeof (float));
75                         }
76                 }
77
78                 plugin->process (bufs, RealTime::fromSeconds ((double) pos / sample_rate));
79
80                 pos += min (stepsize, to_read);
81
82                 if (pos >= len) {
83                         done = true;
84                 }
85         }
86
87         features = plugin->getRemainingFeatures ();
88
89         if (use_features (features, 0)) {
90                 goto out;
91         }
92
93         ret = 0;
94
95 out:
96         for (uint32_t c = 0; c < n_channels; ++c) {
97                 free (bufs[c]);
98         }
99         free (bufs);
100
101         return ret;
102 }
103
104 int
105 EBUr128Analysis::use_features (Plugin::FeatureSet& features, ostream* out)
106 {
107         if (features.empty() || features.size() != 2) {
108                 return 0;
109         }
110         _loudness = features[0][0].values[0];
111         _loudness_range = features[1][0].values[0];
112
113         return 0;
114 }