annotate plugins/AdaptiveSpectrogram.h @ 135:dcf5800f0f00

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