13
0

cleanup: unhardcode spectrum size, logscale, whitespace fixes

This commit is contained in:
Robin Gareus 2016-02-10 19:28:21 +01:00
parent 4928d53f8d
commit d8b04d3124
2 changed files with 39 additions and 25 deletions

View File

@ -55,6 +55,7 @@ class /*LIBAUDIOGRAPHER_API*/ Analyser : public ListedSource<float>, public Sink
framecnt_t _n_samples;
framecnt_t _pos;
framecnt_t _spp;
framecnt_t _fpp;
float* _bufs[2];

View File

@ -41,9 +41,15 @@ Analyser::Analyser (float sample_rate, unsigned int channels, framecnt_t bufsize
}
_bufs[0] = (float*) malloc (sizeof (float) * _bufsize);
_bufs[1] = (float*) malloc (sizeof (float) * _bufsize);
const size_t peaks = sizeof (_result.peaks) / sizeof (ARDOUR::PeakData::PeakDatum) / 2;
_spp = ceil ((_n_samples + 1.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 + 1.f) / (float) width);
_fft_data_size = _bufsize / 2;
_fft_freq_per_bin = sample_rate / _fft_data_size / 2.f;
@ -127,23 +133,30 @@ Analyser::process (ProcessContext<float> const & c)
#undef FRe
#undef FIm
// TODO: get geometry from ExportAnalysis
const framecnt_t x0 = _pos / _spp;
const framecnt_t x1 = (_pos + n_samples) / _spp;
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;
const float range = 80; // dB
const double ypb = 200.0 / _fft_data_size;
for (uint32_t i = 1; i < _fft_data_size - 1; ++i) {
const float level = fft_power_at_bin (i, i);
if (level < -range) continue;
const float pk = level > 0.0 ? 1.0 : (range + level) / range;
const uint32_t y = 200 - ceil (i * ypb); // log-y?
assert (y < 200);
#if 0 // linear
const uint32_t y0 = height - ceil (i * (float) height / _fft_data_size);
uint32_t y1= height - ceil (i * (float) height / _fft_data_size);
#else // logscale
const uint32_t y0 = height - ceilf (height * logf (1.f + .02f * i) / logf (1.f + .02f * _fft_data_size));
uint32_t y1 = height - ceilf (height * logf (1.f + .02f * (i + 1.f)) / logf (1.f + .02f * _fft_data_size));
#endif
if (y0 == y1 && y0 > 0) y1 = y0 - 1;
for (int x = x0; x < x1; ++x) {
assert (x >= 0 && x < 800);
for (uint32_t y = y0; y > y1; --y) {
if (_result.spectrum[x][y] < pk) { _result.spectrum[x][y] = pk; }
}
}
}
_pos += n_samples;