annotate plugins/AdaptiveSpectrogram.h @ 113:d0920575b48a

* pass allocator through to sub-cuts (duh)
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 20 May 2009 08:52:11 +0000
parents be419e04899a
children 496e6d6eb413
rev   line source
c@92 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@92 2
c@92 3 /*
c@92 4 QM Vamp Plugin Set
c@92 5
c@92 6 Centre for Digital Music, Queen Mary, University of London.
c@92 7 All rights reserved.
c@92 8 */
c@92 9
c@92 10 #ifndef _ADAPTIVE_SPECTROGRAM_H_
c@92 11 #define _ADAPTIVE_SPECTROGRAM_H_
c@92 12
c@92 13 #include <vamp-sdk/Plugin.h>
c@92 14 #include <cmath>
c@92 15 #include <vector>
c@92 16
c@108 17 #include <dsp/transforms/FFT.h>
c@107 18 #include <base/Window.h>
c@105 19
c@104 20 #include "thread/Thread.h"
c@110 21 #include "thread/AsynchronousTask.h"
c@110 22 #include "thread/BlockAllocator.h"
c@104 23
c@92 24 class AdaptiveSpectrogram : public Vamp::Plugin
c@92 25 {
c@92 26 public:
c@92 27 AdaptiveSpectrogram(float inputSampleRate);
c@92 28 virtual ~AdaptiveSpectrogram();
c@92 29
c@92 30 bool initialise(size_t channels, size_t stepSize, size_t blockSize);
c@92 31 void reset();
c@92 32
c@92 33 InputDomain getInputDomain() const { return TimeDomain; }
c@92 34
c@92 35 std::string getIdentifier() const;
c@92 36 std::string getName() const;
c@92 37 std::string getDescription() const;
c@92 38 std::string getMaker() const;
c@92 39 int getPluginVersion() const;
c@92 40 std::string getCopyright() const;
c@92 41
c@92 42 size_t getPreferredStepSize() const;
c@92 43 size_t getPreferredBlockSize() const;
c@92 44
c@92 45 ParameterList getParameterDescriptors() const;
c@92 46 float getParameter(std::string id) const;
c@92 47 void setParameter(std::string id, float value);
c@92 48
c@92 49 OutputList getOutputDescriptors() const;
c@92 50
c@92 51 FeatureSet process(const float *const *inputBuffers,
c@92 52 Vamp::RealTime timestamp);
c@92 53
c@92 54 FeatureSet getRemainingFeatures();
c@92 55
c@92 56 protected:
c@92 57 int m_w;
c@92 58 int m_n;
c@109 59 bool m_threaded;
c@92 60
c@100 61 struct Spectrogram
c@100 62 {
c@100 63 int resolution;
c@100 64 int width;
c@100 65 double **data;
c@100 66
c@100 67 Spectrogram(int r, int w) :
c@100 68 resolution(r), width(w) {
c@100 69 data = new double *[width];
c@100 70 for (int i = 0; i < width; ++i) data[i] = new double[resolution];
c@100 71 }
c@100 72
c@100 73 ~Spectrogram() {
c@100 74 for (int i = 0; i < width; ++i) delete[] data[i];
c@100 75 delete[] data;
c@100 76 }
c@100 77 };
c@100 78
c@100 79 struct Spectrograms
c@100 80 {
c@100 81 int minres;
c@100 82 int maxres;
c@100 83 int n;
c@100 84 Spectrogram **spectrograms;
c@100 85
c@100 86 Spectrograms(int mn, int mx, int widthofmax) :
c@100 87 minres(mn), maxres(mx) {
c@100 88 n = log2(maxres/minres) + 1;
c@100 89 spectrograms = new Spectrogram *[n];
c@100 90 int r = mn;
c@100 91 for (int i = 0; i < n; ++i) {
c@100 92 spectrograms[i] = new Spectrogram(r, widthofmax * (mx / r));
c@100 93 r = r * 2;
c@100 94 }
c@100 95 }
c@100 96 ~Spectrograms() {
c@100 97 for (int i = 0; i < n; ++i) {
c@100 98 delete spectrograms[i];
c@100 99 }
c@100 100 delete[] spectrograms;
c@100 101 }
c@100 102 };
c@100 103
c@100 104 struct Cutting
c@100 105 {
c@100 106 enum Cut { Horizontal, Vertical, Finished };
c@100 107 Cut cut;
c@100 108 Cutting *first;
c@100 109 Cutting *second;
c@100 110 double cost;
c@100 111 double value;
c@110 112 BlockAllocator *allocator;
c@100 113
c@100 114 ~Cutting() {
c@110 115 if (first) first->erase();
c@110 116 if (second) second->erase();
c@110 117 }
c@110 118
c@110 119 void erase() {
c@110 120 if (allocator) {
c@110 121 if (first) first->erase();
c@110 122 if (second) second->erase();
c@110 123 allocator->deallocate(this);
c@110 124 } else {
c@110 125 delete this;
c@110 126 }
c@100 127 }
c@100 128 };
c@100 129
c@105 130 class FFTThread : public AsynchronousTask
c@104 131 {
c@104 132 public:
c@107 133 FFTThread(int w) :
c@107 134 m_window(HanningWindow, w) {
c@106 135 m_w = w;
c@106 136 m_fft = new FFTReal(m_w);
c@106 137 m_rin = new double[m_w];
c@106 138 m_rout = new double[m_w];
c@106 139 m_iout = new double[m_w];
c@106 140 }
c@106 141 ~FFTThread() {
c@106 142 delete[] m_rin;
c@106 143 delete[] m_rout;
c@106 144 delete[] m_iout;
c@106 145 delete m_fft;
c@106 146 }
c@106 147
c@106 148 int getW() const { return m_w; }
c@105 149
c@109 150 void startCalculation(const float *timeDomain, Spectrograms &s,
c@109 151 int res, int maxwidth) {
c@109 152 setParameters(timeDomain, s, res, maxwidth);
c@105 153 startTask();
c@105 154 }
c@105 155
c@105 156 void await() {
c@105 157 awaitTask();
c@105 158 }
c@105 159
c@109 160 void setParameters(const float *timeDomain, Spectrograms &s,
c@109 161 int res, int maxwidth) {
c@109 162 m_in = timeDomain;
c@109 163 m_s = &s;
c@109 164 m_res = res;
c@109 165 m_maxwid = maxwidth;
c@109 166 }
c@109 167
c@105 168 void performTask() {
c@105 169 for (int i = 0; i < m_maxwid / m_w; ++i) {
c@105 170 int origin = m_maxwid/4 - m_w/4; // for 50% overlap
c@105 171 for (int j = 0; j < m_w; ++j) {
c@109 172 m_rin[j] = m_in[origin + i * m_w/2 + j];
c@105 173 }
c@107 174 m_window.cut(m_rin);
c@106 175 m_fft->process(false, m_rin, m_rout, m_iout);
c@105 176 for (int j = 0; j < m_w/2; ++j) {
c@105 177 int k = j+1; // include Nyquist but not DC
c@106 178 double mag = sqrt(m_rout[k] * m_rout[k] +
c@106 179 m_iout[k] * m_iout[k]);
c@105 180 double scaled = mag / (m_w/2);
c@105 181 m_s->spectrograms[m_res]->data[i][j] = scaled;
c@105 182 }
c@105 183 }
c@105 184 }
c@105 185
c@105 186 private:
c@107 187 Window<double> m_window;
c@106 188 FFTReal *m_fft;
c@105 189 const float *m_in;
c@106 190 double *m_rin;
c@106 191 double *m_rout;
c@106 192 double *m_iout;
c@105 193 Spectrograms *m_s;
c@105 194 int m_res;
c@105 195 int m_w;
c@105 196 int m_maxwid;
c@105 197 };
c@105 198
c@106 199 typedef std::map<int, FFTThread *> FFTMap;
c@106 200 FFTMap m_fftThreads;
c@105 201
c@105 202 class CutThread : public AsynchronousTask
c@105 203 {
c@105 204 public:
c@110 205 CutThread(const AdaptiveSpectrogram *as) : m_as(as), m_result(0) {
c@110 206 m_allocator = new BlockAllocator(sizeof(Cutting));
c@110 207 }
c@110 208 ~CutThread() {
c@110 209 delete m_allocator;
c@110 210 }
c@105 211
c@104 212 void cut(const Spectrograms &s, int res, int x, int y, int h) {
c@104 213 m_s = &s;
c@104 214 m_res = res;
c@104 215 m_x = x;
c@104 216 m_y = y;
c@104 217 m_h = h;
c@105 218 startTask();
c@104 219 }
c@104 220
c@104 221 Cutting *get() {
c@105 222 awaitTask();
c@105 223 return m_result;
c@104 224 }
c@104 225
c@104 226 protected:
c@105 227 void performTask() {
c@110 228 m_result = m_as->cut(*m_s, m_res, m_x, m_y, m_h, m_allocator);
c@104 229 }
c@104 230
c@105 231 private:
c@104 232 const AdaptiveSpectrogram *m_as;
c@110 233 BlockAllocator *m_allocator;
c@104 234 const Spectrograms *m_s;
c@104 235 int m_res;
c@104 236 int m_x;
c@104 237 int m_y;
c@104 238 int m_h;
c@104 239 Cutting *m_result;
c@104 240 };
c@105 241
c@109 242 mutable std::vector<CutThread *> m_cutThreads;
c@109 243 mutable bool m_threadsInUse;
c@104 244
c@110 245 inline double xlogx(double x) const {
c@104 246 if (x == 0.0) return 0.0;
c@104 247 else return x * log(x);
c@104 248 }
c@104 249
c@110 250 inline double cost(const Spectrogram &s, int x, int y) const {
c@100 251 return xlogx(s.data[x][y]);
c@100 252 }
c@100 253
c@110 254 inline double value(const Spectrogram &s, int x, int y) const {
c@100 255 return s.data[x][y];
c@100 256 }
c@100 257
c@110 258 Cutting *cut(const Spectrograms &, int res, int x, int y, int h,
c@110 259 BlockAllocator *allocator) const;
c@100 260
c@104 261 void getSubCuts(const Spectrograms &, int res, int x, int y, int h,
c@104 262 Cutting *&top, Cutting *&bottom,
c@113 263 Cutting *&left, Cutting *&right,
c@113 264 BlockAllocator *allocator) const;
c@100 265
c@104 266 void printCutting(Cutting *, std::string) const;
c@104 267
c@104 268 void assemble(const Spectrograms &, const Cutting *,
c@104 269 std::vector<std::vector<float> > &,
c@104 270 int x, int y, int w, int h) const;
c@104 271 };
c@92 272
c@92 273
c@92 274 #endif