svcore  1.9
FFTDataServer.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam and QMUL.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #ifndef _FFT_DATA_SERVER_H_
17 #define _FFT_DATA_SERVER_H_
18 
19 #include "base/Window.h"
20 #include "base/Thread.h"
21 #include "base/StorageAdviser.h"
22 
23 #include "FFTapi.h"
24 #include "FFTFileCacheReader.h"
25 #include "FFTFileCacheWriter.h"
26 #include "FFTMemoryCache.h"
27 
28 #include <QMutex>
29 #include <QReadWriteLock>
30 #include <QReadLocker>
31 #include <QWaitCondition>
32 #include <QString>
33 
34 #include <vector>
35 #include <deque>
36 
38 class Model;
39 
41 {
42 public:
43  static FFTDataServer *getInstance(const DenseTimeValueModel *model,
44  int channel,
45  WindowType windowType,
46  int windowSize,
47  int windowIncrement,
48  int fftSize,
49  bool polar,
50  StorageAdviser::Criteria criteria =
52  int fillFromColumn = 0);
53 
55  int channel,
56  WindowType windowType,
57  int windowSize,
58  int windowIncrement,
59  int fftSize,
60  bool polar,
61  StorageAdviser::Criteria criteria =
63  int fillFromColumn = 0);
64 
65  static void claimInstance(FFTDataServer *);
66  static void releaseInstance(FFTDataServer *);
67 
68  static void modelAboutToBeDeleted(Model *);
69 
70  const DenseTimeValueModel *getModel() const { return m_model; }
71  int getChannel() const { return m_channel; }
73  int getWindowSize() const { return m_windowSize; }
74  int getWindowIncrement() const { return m_windowIncrement; }
75  int getFFTSize() const { return m_fftSize; }
76  bool getPolar() const { return m_polar; }
77 
78  int getWidth() const { return m_width; }
79  int getHeight() const { return m_height; }
80 
81  float getMagnitudeAt(int x, int y);
82  float getNormalizedMagnitudeAt(int x, int y);
83  float getMaximumMagnitudeAt(int x);
84  float getPhaseAt(int x, int y);
85  void getValuesAt(int x, int y, float &real, float &imaginary);
86  bool isColumnReady(int x);
87 
88  bool getMagnitudesAt(int x, float *values, int minbin = 0, int count = 0, int step = 1);
89  bool getNormalizedMagnitudesAt(int x, float *values, int minbin = 0, int count = 0, int step = 1);
90  bool getPhasesAt(int x, float *values, int minbin = 0, int count = 0, int step = 1);
91  bool getValuesAt(int x, float *reals, float *imaginaries, int minbin = 0, int count = 0, int step = 1);
92 
93  void suspend();
94  void suspendWrites();
95  void resume(); // also happens automatically if new data needed
96 
97  // Convenience functions:
98 
99  bool isLocalPeak(int x, int y) {
100  float mag = getMagnitudeAt(x, y);
101  if (y > 0 && mag < getMagnitudeAt(x, y - 1)) return false;
102  if (y < getHeight()-1 && mag < getMagnitudeAt(x, y + 1)) return false;
103  return true;
104  }
105  bool isOverThreshold(int x, int y, float threshold) {
106  return getMagnitudeAt(x, y) > threshold;
107  }
108 
109  QString getError() const;
110  int getFillCompletion() const;
111  int getFillExtent() const;
112 
113 private:
114  FFTDataServer(QString fileBaseName,
115  const DenseTimeValueModel *model,
116  int channel,
117  WindowType windowType,
118  int windowSize,
119  int windowIncrement,
120  int fftSize,
121  bool polar,
122  StorageAdviser::Criteria criteria,
123  int fillFromColumn = 0);
124 
125  virtual ~FFTDataServer();
126 
127  FFTDataServer(const FFTDataServer &); // not implemented
128  FFTDataServer &operator=(const FFTDataServer &); // not implemented
129 
130  typedef float fftsample;
131 
132  QString m_fileBaseName;
135 
137 
141  bool m_polar;
142 
143  int m_width;
144  int m_height;
148 
149  struct CacheBlock {
151  typedef std::map<QThread *, FFTFileCacheReader *> ThreadReaderMap;
156  delete memoryCache;
157  while (!fileCacheReader.empty()) {
158  delete fileCacheReader.begin()->second;
159  fileCacheReader.erase(fileCacheReader.begin());
160  }
161  delete fileCacheWriter;
162  }
163  };
164 
165  typedef std::vector<CacheBlock *> CacheVector;
167  QReadWriteLock m_cacheVectorLock; // locks cache lookup, not use
168  QMutex m_cacheCreationMutex; // solely to serialise makeCache() calls
169 
170  FFTCacheReader *getCacheReader(int x, int &col) {
171  Profiler profiler("FFTDataServer::getCacheReader");
172  col = x & m_cacheWidthMask;
173  int c = x >> m_cacheWidthPower;
174  m_cacheVectorLock.lockForRead();
175  CacheBlock *cb(m_caches.at(c));
176  if (cb) {
177  if (cb->memoryCache) {
178  m_cacheVectorLock.unlock();
179  return cb->memoryCache;
180  }
181  if (cb->fileCacheWriter) {
182  QThread *me = QThread::currentThread();
183  CacheBlock::ThreadReaderMap &map = cb->fileCacheReader;
184  if (map.find(me) == map.end()) {
185  m_cacheVectorLock.unlock();
186  if (!makeCacheReader(c)) return 0;
187  return getCacheReader(x, col);
188  }
189  FFTCacheReader *reader = cb->fileCacheReader[me];
190  m_cacheVectorLock.unlock();
191  return reader;
192  }
193  // if cb exists but cb->fileCacheWriter doesn't, creation
194  // must have failed: don't try again
195  m_cacheVectorLock.unlock();
196  return 0;
197  }
198  m_cacheVectorLock.unlock();
199  if (!makeCache(c)) return 0;
200  return getCacheReader(x, col);
201  }
202 
203  FFTCacheWriter *getCacheWriter(int x, int &col) {
204  Profiler profiler("FFTDataServer::getCacheWriter");
205  col = x & m_cacheWidthMask;
206  int c = x >> m_cacheWidthPower;
207  {
208  QReadLocker locker(&m_cacheVectorLock);
209  CacheBlock *cb(m_caches.at(c));
210  if (cb) {
211  if (cb->memoryCache) return cb->memoryCache;
212  if (cb->fileCacheWriter) return cb->fileCacheWriter;
213  // if cb exists, creation must have failed: don't try again
214  return 0;
215  }
216  }
217  if (!makeCache(c)) return 0;
218  return getCacheWriter(x, col);
219  }
220 
221  bool haveCache(int x) {
222  int c = x >> m_cacheWidthPower;
223  return (m_caches.at(c) != 0);
224  }
225 
226  bool makeCache(int c);
227  bool makeCacheReader(int c);
228 
230 
231  void getStorageAdvice(int w, int h, bool &memory, bool &compact);
232 
234  QWaitCondition m_condition;
235 
238  float *m_workbuffer;
240 
241  class FillThread : public Thread
242  {
243  public:
244  FillThread(FFTDataServer &server, int fillFromColumn) :
245  m_server(server), m_extent(0), m_completion(0),
246  m_fillFrom(fillFromColumn) { }
247 
248  int getExtent() const { return m_extent; }
249  int getCompletion() const { return m_completion ? m_completion : 1; }
250  QString getError() const { return m_error; }
251  virtual void run();
252 
253  protected:
255  int m_extent;
258  QString m_error;
259  };
260 
261  bool m_exiting;
264  QString m_error;
265 
266  void deleteProcessingData();
267  void fillColumn(int x);
268  void fillComplete();
269 
270  QString generateFileBasename() const;
271  static QString generateFileBasename(const DenseTimeValueModel *model,
272  int channel,
273  WindowType windowType,
274  int windowSize,
275  int windowIncrement,
276  int fftSize,
277  bool polar);
278 
279  typedef std::pair<FFTDataServer *, int> ServerCountPair;
280  typedef std::map<QString, ServerCountPair> ServerMap;
281  typedef std::deque<FFTDataServer *> ServerQueue;
282 
284  static ServerQueue m_releasedServers; // these are still in m_servers as well, with zero refcount
285  static QMutex m_serverMapMutex;
286  static FFTDataServer *findServer(QString); // call with serverMapMutex held
287  static void purgeLimbo(int maxSize = 3); // call with serverMapMutex held
288 
289  static void claimInstance(FFTDataServer *, bool needLock);
290  static void releaseInstance(FFTDataServer *, bool needLock);
291 
292 };
293 
294 #endif
fftf_plan m_fftPlan
bool haveCache(int x)
void deleteProcessingData()
std::vector< CacheBlock * > CacheVector
FillThread * m_fillThread
static QMutex m_serverMapMutex
FFTDataServer & operator=(const FFTDataServer &)
bool getNormalizedMagnitudesAt(int x, float *values, int minbin=0, int count=0, int step=1)
bool getPolar() const
Definition: FFTDataServer.h:76
FFTCacheWriter * getCacheWriter(int x, int &col)
QString m_fileBaseName
FFTDataServer & m_server
ThreadReaderMap fileCacheReader
fftsample * m_fftInput
QReadWriteLock m_cacheVectorLock
FFTMemoryCache * memoryCache
int getChannel() const
Definition: FFTDataServer.h:71
QString getError() const
static ServerMap m_servers
Window< fftsample > m_windower
FFTDataServer(QString fileBaseName, const DenseTimeValueModel *model, int channel, WindowType windowType, int windowSize, int windowIncrement, int fftSize, bool polar, StorageAdviser::Criteria criteria, int fillFromColumn=0)
float getMaximumMagnitudeAt(int x)
const DenseTimeValueModel * getModel() const
Definition: FFTDataServer.h:70
fftf_complex * m_fftOutput
bool makeCache(int c)
static void purgeLimbo(int maxSize=3)
float getMagnitudeAt(int x, int y)
std::map< QThread *, FFTFileCacheReader * > ThreadReaderMap
std::pair< FFTDataServer *, int > ServerCountPair
void fillColumn(int x)
WindowType
Definition: Window.h:27
QWaitCondition m_condition
virtual ~FFTDataServer()
int getWindowIncrement() const
Definition: FFTDataServer.h:74
QMutex m_cacheCreationMutex
FFTFileCacheWriter * fileCacheWriter
QMutex m_fftBuffersLock
bool getMagnitudesAt(int x, float *values, int minbin=0, int count=0, int step=1)
static FFTDataServer * findServer(QString)
WindowType getWindowType() const
Definition: FFTDataServer.h:72
#define fftf_plan
Definition: FFTapi.h:26
void getStorageAdvice(int w, int h, bool &memory, bool &compact)
int getFillExtent() const
float getPhaseAt(int x, int y)
bool isLocalPeak(int x, int y)
Definition: FFTDataServer.h:99
const DenseTimeValueModel * m_model
int getHeight() const
Definition: FFTDataServer.h:79
Model is the base class for all data models that represent any sort of data on a time scale based on ...
Definition: Model.h:35
static void modelAboutToBeDeleted(Model *)
bool isColumnReady(int x)
int getFillCompletion() const
static void claimInstance(FFTDataServer *)
StorageAdviser::Criteria m_criteria
FFTCacheReader * getCacheReader(int x, int &col)
static void releaseInstance(FFTDataServer *)
void getValuesAt(int x, int y, float &real, float &imaginary)
int getFFTSize() const
Definition: FFTDataServer.h:75
Base class for models containing dense two-dimensional data (value against time).
float * m_workbuffer
std::map< QString, ServerCountPair > ServerMap
QString getError() const
Definition: Thread.h:24
bool makeCacheReader(int c)
int getWindowSize() const
Definition: FFTDataServer.h:73
float getNormalizedMagnitudeAt(int x, int y)
bool isOverThreshold(int x, int y, float threshold)
WindowType getType() const
Definition: Window.h:69
FillThread(FFTDataServer &server, int fillFromColumn)
int getWidth() const
Definition: FFTDataServer.h:78
#define fftf_complex
Definition: FFTapi.h:23
static FFTDataServer * getInstance(const DenseTimeValueModel *model, int channel, WindowType windowType, int windowSize, int windowIncrement, int fftSize, bool polar, StorageAdviser::Criteria criteria=StorageAdviser::NoCriteria, int fillFromColumn=0)
std::deque< FFTDataServer * > ServerQueue
QString generateFileBasename() const
In-memory FFT cache.
bool getPhasesAt(int x, float *values, int minbin=0, int count=0, int step=1)
static FFTDataServer * getFuzzyInstance(const DenseTimeValueModel *model, int channel, WindowType windowType, int windowSize, int windowIncrement, int fftSize, bool polar, StorageAdviser::Criteria criteria=StorageAdviser::NoCriteria, int fillFromColumn=0)
CacheVector m_caches
Profile point instance class.
Definition: Profiler.h:86
static ServerQueue m_releasedServers