summaryrefslogtreecommitdiff
path: root/libs/audiographer/src/general/analyser.cc
blob: 84120b6f532fe97b1660f107674362df79cbfaa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "audiographer/general/analyser.h"
#include "pbd/fastlog.h"

using namespace AudioGrapher;

const float Analyser::fft_range_db (120); // dB

Analyser::Analyser (float sample_rate, unsigned int channels, framecnt_t bufsize, framecnt_t n_samples)
	: LoudnessReader (sample_rate, channels, bufsize)
	, _n_samples (n_samples)
	, _pos (0)
{
	//printf ("NEW ANALYSER %p r:%.1f c:%d f:%ld l%ld\n", this, sample_rate, channels, bufsize, n_samples);
	assert (bufsize % channels == 0);
	assert (bufsize > 1);
	assert (_bufsize > 0);



	const size_t peaks = sizeof (_result.peaks) / sizeof (ARDOUR::PeakData::PeakDatum) / 4;
	_spp = ceil ((_n_samples + 2.f) / (float) peaks);

	const size_t swh = sizeof (_result.spectrum) / sizeof (float);
	const size_t height = sizeof (_result.spectrum[0]) / sizeof (float);
	const size_t width = swh / height;
	_fpp = ceil ((_n_samples + 2.f) / (float) width);

	_fft_data_size   = _bufsize / 2;
	_fft_freq_per_bin = sample_rate / _fft_data_size / 2.f;

	_fft_data_in  = (float *) fftwf_malloc (sizeof (float) * _bufsize);
	_fft_data_out = (float *) fftwf_malloc (sizeof (float) * _bufsize);
	_fft_power    = (float *) malloc (sizeof (float) * _fft_data_size);

	for (uint32_t i = 0; i < _fft_data_size; ++i) {
		_fft_power[i] = 0;
	}
	for (uint32_t i = 0; i < _bufsize; ++i) {
		_fft_data_out[i] = 0;
	}

	const float nyquist = (sample_rate * .5);
#if 0 // linear
#define YPOS(FREQ) rint (height * (1.0 - FREQ / nyquist))
#else
#define YPOS(FREQ) rint (height * (1 - logf (1.f + .1f * _fft_data_size * FREQ / nyquist) / logf (1.f + .1f * _fft_data_size)))
#endif

	_result.freq[0] = YPOS (50);
	_result.freq[1] = YPOS (100);
	_result.freq[2] = YPOS (500);
	_result.freq[3] = YPOS (1000);
	_result.freq[4] = YPOS (5000);
	_result.freq[5] = YPOS (10000);

	_fft_plan = fftwf_plan_r2r_1d (_bufsize, _fft_data_in, _fft_data_out, FFTW_R2HC, FFTW_MEASURE);

	_hann_window = (float *) malloc (sizeof (float) * _bufsize);
	double sum = 0.0;

	for (uint32_t i = 0; i < _bufsize; ++i) {
		_hann_window[i] = 0.5f - (0.5f * (float) cos (2.0f * M_PI * (float)i / (float)(_bufsize)));
		sum += _hann_window[i];
	}
	const double isum = 2.0 / sum;
	for (uint32_t i = 0; i < _bufsize; ++i) {
		_hann_window[i] *= isum;
	}

	if (channels == 2) {
		_result.n_channels = 2;
	} else {
		_result.n_channels = 1;
	}
}

Analyser::~Analyser ()
{
	fftwf_destroy_plan (_fft_plan);
	fftwf_free (_fft_data_in);
	fftwf_free (_fft_data_out);
	free (_fft_power);
	free (_hann_window);
}

void
Analyser::process (ProcessContext<float> const & ctx)
{
	const framecnt_t n_samples = ctx.frames () / ctx.channels ();
	assert (ctx.channels () == _channels);
	assert (ctx.frames () % ctx.channels () == 0);
	assert (n_samples <= _bufsize);
	//printf ("PROC %p @%ld F: %ld, S: %ld C:%d\n", this, _pos, ctx.frames (), n_samples, ctx.channels ());

	// allow 1 sample slack for resampling
	if (_pos + n_samples > _n_samples + 1) {
		_pos += n_samples;
		ListedSource<float>::output (ctx);
		return;
	}

	float const * d = ctx.data ();
	framecnt_t s;
	const unsigned cmask = _result.n_channels - 1; // [0, 1]
	for (s = 0; s < n_samples; ++s) {
		_fft_data_in[s] = 0;
		const framecnt_t pbin = (_pos + s) / _spp;
		for (unsigned int c = 0; c < _channels; ++c) {
			const float v = *d;
			if (fabsf(v) > _result.peak) { _result.peak = fabsf(v); }
			if (c < _result.n_channels) {
				_bufs[c][s] = v;
			}
			const unsigned int cc = c & cmask;
			if (_result.peaks[cc][pbin].min > v) { _result.peaks[cc][pbin].min = *d; }
			if (_result.peaks[cc][pbin].max < v) { _result.peaks[cc][pbin].max = *d; }
			_fft_data_in[s] += v * _hann_window[s] / (float) _channels;
			++d;
		}
	}

	for (; s < _bufsize; ++s) {
		_fft_data_in[s] = 0;
		for (unsigned int c = 0; c < _result.n_channels; ++c) {
			_bufs[c][s] = 0.f;
		}
	}

	if (_ebur_plugin) {
		_ebur_plugin->process (_bufs, Vamp::RealTime::fromSeconds ((double) _pos / _sample_rate));
	}

	float const * const data = ctx.data ();
	for (unsigned int c = 0; c < _channels; ++c) {
		if (!_dbtp_plugin[c]) { continue; }
		for (s = 0; s < n_samples; ++s) {
			_bufs[0][s] = data[s * _channels + c];
		}
		_dbtp_plugin[c]->process (_bufs, Vamp::RealTime::fromSeconds ((double) _pos / _sample_rate));
	}

	fftwf_execute (_fft_plan);

	_fft_power[0] = _fft_data_out[0] * _fft_data_out[0];
#define FRe (_fft_data_out[i])
#define FIm (_fft_data_out[_bufsize - i])
	for (uint32_t i = 1; i < _fft_data_size - 1; ++i) {
		_fft_power[i] = (FRe * FRe) + (FIm * FIm);
	}
#undef FRe
#undef FIm

	const size_t height = sizeof (_result.spectrum[0]) / sizeof (float);
	const framecnt_t x0 = _pos / _fpp;
	framecnt_t x1 = (_pos + n_samples) / _fpp;
	if (x0 == x1) x1 = x0 + 1;

	for (uint32_t i = 0; i < _fft_data_size - 1; ++i) {
		const float level = fft_power_at_bin (i, i);
		if (level < -fft_range_db) continue;
		const float pk = level > 0.0 ? 1.0 : (fft_range_db + level) / fft_range_db;
#if 0 // linear
		const uint32_t y0 = floor (i * (float) height / _fft_data_size);
		uint32_t y1 = ceil ((i + 1.0) * (float) height / _fft_data_size);
#else // logscale
		const uint32_t y0 = floor (height * logf (1.f + .1f * i) / logf (1.f + .1f * _fft_data_size));
		uint32_t y1 = ceilf (height * logf (1.f + .1f * (i + 1.f)) / logf (1.f + .1f * _fft_data_size));
#endif
		assert (y0 < height);
		assert (y1 > 0 && y1 <= height);
		if (y0 == y1) y1 = y0 + 1;
		for (int x = x0; x < x1; ++x) {
			for (uint32_t y = y0; y < y1 && y < height; ++y) {
				uint32_t yy = height - 1 - y;
				if (_result.spectrum[x][yy] < pk) { _result.spectrum[x][yy] = pk; }
			}
		}
	}

	_pos += n_samples;

	/* pass audio audio through */
	ListedSource<float>::output (ctx);
}

ARDOUR::ExportAnalysisPtr
Analyser::result ()
{
	//printf ("PROCESSED %ld / %ld samples\n", _pos, _n_samples);
	if (_pos == 0 || _pos > _n_samples + 1) {
		return ARDOUR::ExportAnalysisPtr ();
	}

	if (_pos + 1 < _n_samples) {
		// crude re-bin (silence stripped version)
		const size_t peaks = sizeof (_result.peaks) / sizeof (ARDOUR::PeakData::PeakDatum) / 4;
		for (framecnt_t b = peaks - 1; b > 0; --b) {
			for (unsigned int c = 0; c < _result.n_channels; ++c) {
				const framecnt_t sb = b * _pos / _n_samples;
				_result.peaks[c][b].min = _result.peaks[c][sb].min;
				_result.peaks[c][b].max = _result.peaks[c][sb].max;
			}
		}

		const size_t swh = sizeof (_result.spectrum) / sizeof (float);
		const size_t height = sizeof (_result.spectrum[0]) / sizeof (float);
		const size_t width = swh / height;
		for (framecnt_t b = width - 1; b > 0; --b) {
			// TODO round down to prev _fft_data_size bin
			const framecnt_t sb = b * _pos / _n_samples;
			for (unsigned int y = 0; y < height; ++y) {
				_result.spectrum[b][y] = _result.spectrum[sb][y];
			}
		}
	}

	if (_ebur_plugin) {
		Vamp::Plugin::FeatureSet features = _ebur_plugin->getRemainingFeatures ();
		if (!features.empty () && features.size () == 3) {
			_result.loudness = features[0][0].values[0];
			_result.loudness_range = features[1][0].values[0];
			assert (features[2][0].values.size () == 540);
			for (int i = 0; i < 540; ++i) {
				_result.loudness_hist[i] = features[2][0].values[i];
				if (_result.loudness_hist[i] > _result.loudness_hist_max) {
					_result.loudness_hist_max = _result.loudness_hist[i]; }
			}
			_result.have_loudness = true;
		}
	}

	const unsigned cmask = _result.n_channels - 1; // [0, 1]
	for (unsigned int c = 0; c < _channels; ++c) {
		if (!_dbtp_plugin[c]) { continue; }
		Vamp::Plugin::FeatureSet features = _dbtp_plugin[c]->getRemainingFeatures ();
		if (!features.empty () && features.size () == 2) {
			_result.have_dbtp = true;
			float p = features[0][0].values[0];
			if (p > _result.truepeak) { _result.truepeak = p; }

			for (std::vector<float>::const_iterator i = features[1][0].values.begin();
					i != features[1][0].values.end(); ++i) {
				const framecnt_t pk = (*i) / _spp;
				const unsigned int cc = c & cmask;
				_result.truepeakpos[cc].insert (pk);
			}
		}
	}

	return ARDOUR::ExportAnalysisPtr (new ARDOUR::ExportAnalysis (_result));
}

float
Analyser::fft_power_at_bin (const uint32_t b, const float norm) const
{
	const float a = _fft_power[b] * norm;
	return a > 1e-12 ? 10.0 * fast_log10 (a) : -INFINITY;
}