Chris@152: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@152: 
Chris@152: /*
Chris@152:     Sonic Visualiser
Chris@152:     An audio file viewer and annotation editor.
Chris@152:     Centre for Digital Music, Queen Mary, University of London.
Chris@152:     This file copyright 2006 Chris Cannam.
Chris@152:     
Chris@152:     This program is free software; you can redistribute it and/or
Chris@152:     modify it under the terms of the GNU General Public License as
Chris@152:     published by the Free Software Foundation; either version 2 of the
Chris@152:     License, or (at your option) any later version.  See the file
Chris@152:     COPYING included with this distribution for more information.
Chris@152: */
Chris@152: 
Chris@152: #ifndef _FFT_MODEL_H_
Chris@152: #define _FFT_MODEL_H_
Chris@152: 
Chris@152: #include "data/fft/FFTDataServer.h"
Chris@152: #include "DenseThreeDimensionalModel.h"
Chris@152: 
Chris@275: #include <set>
Chris@275: #include <map>
Chris@275: 
Chris@254: /**
Chris@254:  * An implementation of DenseThreeDimensionalModel that makes FFT data
Chris@387:  * derived from a DenseTimeValueModel available as a generic data
Chris@387:  * grid.  The FFT data is acquired using FFTDataServer.  Note that any
Chris@387:  * of the accessor functions may throw AllocationFailed if a cache
Chris@387:  * resize fails.
Chris@254:  */
Chris@254: 
Chris@152: class FFTModel : public DenseThreeDimensionalModel
Chris@152: {
Chris@247:     Q_OBJECT
Chris@247: 
Chris@152: public:
Chris@254:     /**
Chris@254:      * Construct an FFT model derived from the given
Chris@254:      * DenseTimeValueModel, with the given window parameters and FFT
Chris@254:      * size (which may exceed the window size, for zero-padded FFTs).
Chris@254:      * 
Chris@254:      * If the model has multiple channels use only the given channel,
Chris@254:      * unless the channel is -1 in which case merge all available
Chris@254:      * channels.
Chris@254:      * 
Chris@254:      * If polar is true, the data will normally be retrieved from the
Chris@254:      * FFT model in magnitude/phase form; otherwise it will normally
Chris@254:      * be retrieved in "cartesian" real/imaginary form.  The results
Chris@254:      * should be the same either way, but a "polar" model addressed in
Chris@254:      * "cartesian" form or vice versa may suffer a performance
Chris@254:      * penalty.
Chris@254:      *
Chris@254:      * The fillFromColumn argument gives a hint that the FFT data
Chris@254:      * server should aim to start calculating FFT data at that column
Chris@254:      * number if possible, as that is likely to be requested first.
Chris@254:      */
Chris@152:     FFTModel(const DenseTimeValueModel *model,
Chris@152:              int channel,
Chris@152:              WindowType windowType,
Chris@152:              size_t windowSize,
Chris@152:              size_t windowIncrement,
Chris@152:              size_t fftSize,
Chris@152:              bool polar,
Chris@334:              StorageAdviser::Criteria criteria = StorageAdviser::NoCriteria,
Chris@152:              size_t fillFromColumn = 0);
Chris@152:     ~FFTModel();
Chris@152: 
Chris@497:     inline float getMagnitudeAt(size_t x, size_t y) {
Chris@152:         return m_server->getMagnitudeAt(x << m_xshift, y << m_yshift);
Chris@152:     }
Chris@497:     inline float getNormalizedMagnitudeAt(size_t x, size_t y) {
Chris@152:         return m_server->getNormalizedMagnitudeAt(x << m_xshift, y << m_yshift);
Chris@152:     }
Chris@497:     inline float getMaximumMagnitudeAt(size_t x) {
Chris@152:         return m_server->getMaximumMagnitudeAt(x << m_xshift);
Chris@152:     }
Chris@497:     inline float getPhaseAt(size_t x, size_t y) {
Chris@152:         return m_server->getPhaseAt(x << m_xshift, y << m_yshift);
Chris@152:     }
Chris@497:     inline void getValuesAt(size_t x, size_t y, float &real, float &imaginary) {
Chris@152:         m_server->getValuesAt(x << m_xshift, y << m_yshift, real, imaginary);
Chris@152:     }
Chris@497:     inline bool isColumnAvailable(size_t x) const {
Chris@152:         return m_server->isColumnReady(x << m_xshift);
Chris@152:     }
Chris@152: 
Chris@551:     inline bool getMagnitudesAt(size_t x, float *values, size_t minbin = 0, size_t count = 0) {
Chris@408:         return m_server->getMagnitudesAt(x << m_xshift, values, minbin << m_yshift, count, getYRatio());
Chris@408:     }
Chris@551:     inline bool getNormalizedMagnitudesAt(size_t x, float *values, size_t minbin = 0, size_t count = 0) {
Chris@408:         return m_server->getNormalizedMagnitudesAt(x << m_xshift, values, minbin << m_yshift, count, getYRatio());
Chris@408:     }
Chris@551:     inline bool getPhasesAt(size_t x, float *values, size_t minbin = 0, size_t count = 0) {
Chris@408:         return m_server->getPhasesAt(x << m_xshift, values, minbin << m_yshift, count, getYRatio());
Chris@408:     }
Chris@556:     inline bool getValuesAt(size_t x, float *reals, float *imaginaries, size_t minbin = 0, size_t count = 0) {
Chris@556:         return m_server->getValuesAt(x << m_xshift, reals, imaginaries, minbin << m_yshift, count, getYRatio());
Chris@556:     }
Chris@408: 
Chris@497:     inline size_t getFillExtent() const { return m_server->getFillExtent(); }
Chris@152: 
Chris@152:     // DenseThreeDimensionalModel and Model methods:
Chris@152:     //
Chris@497:     inline virtual size_t getWidth() const {
Chris@182:         return m_server->getWidth() >> m_xshift;
Chris@182:     }
Chris@497:     inline virtual size_t getHeight() const {
Chris@212:         // If there is no y-shift, the server's height (based on its
Chris@212:         // fftsize/2 + 1) is correct.  If there is a shift, then the
Chris@212:         // server is using a larger fft size than we want, so we shift
Chris@212:         // it right as many times as necessary, but then we need to
Chris@212:         // re-add the "+1" part (because ((fftsize*2)/2 + 1) / 2 !=
Chris@212:         // fftsize/2 + 1).
Chris@212:         return (m_server->getHeight() >> m_yshift) + (m_yshift > 0 ? 1 : 0);
Chris@182:     }
Chris@182:     virtual float getValueAt(size_t x, size_t y) const {
Chris@182:         return const_cast<FFTModel *>(this)->getMagnitudeAt(x, y);
Chris@182:     }
Chris@152:     virtual bool isOK() const {
Chris@152:         return m_server && m_server->getModel();
Chris@152:     }
Chris@152:     virtual size_t getStartFrame() const {
Chris@152:         return 0;
Chris@152:     }
Chris@152:     virtual size_t getEndFrame() const {
Chris@152:         return getWidth() * getResolution() + getResolution();
Chris@152:     }
Chris@152:     virtual size_t getSampleRate() const;
Chris@152:     virtual size_t getResolution() const {
Chris@152:         return m_server->getWindowIncrement() << m_xshift;
Chris@152:     }
Chris@152:     virtual size_t getYBinCount() const {
Chris@152:         return getHeight();
Chris@152:     }
Chris@152:     virtual float getMinimumLevel() const {
Chris@152:         return 0.f; // Can't provide
Chris@152:     }
Chris@152:     virtual float getMaximumLevel() const {
Chris@152:         return 1.f; // Can't provide
Chris@152:     }
Chris@533:     virtual Column getColumn(size_t x) const;
Chris@152:     virtual QString getBinName(size_t n) const;
Chris@152: 
Chris@478:     virtual bool shouldUseLogValueScale() const {
Chris@478:         return true; // Although obviously it's up to the user...
Chris@478:     }
Chris@478: 
Chris@275:     /**
Chris@275:      * Calculate an estimated frequency for a stable signal in this
Chris@275:      * bin, using phase unwrapping.  This will be completely wrong if
Chris@275:      * the signal is not stable here.
Chris@275:      */
Chris@275:     virtual bool estimateStableFrequency(size_t x, size_t y, float &frequency);
Chris@275: 
Chris@275:     enum PeakPickType
Chris@275:     {
Chris@275:         AllPeaks,                /// Any bin exceeding its immediate neighbours
Chris@275:         MajorPeaks,              /// Peaks picked using sliding median window
Chris@275:         MajorPitchAdaptivePeaks  /// Bigger window for higher frequencies
Chris@275:     };
Chris@275: 
Chris@551:     typedef std::set<size_t> PeakLocationSet; // bin
Chris@551:     typedef std::map<size_t, float> PeakSet; // bin -> freq
Chris@275: 
Chris@275:     /**
Chris@275:      * Return locations of peak bins in the range [ymin,ymax].  If
Chris@275:      * ymax is zero, getHeight()-1 will be used.
Chris@275:      */
Chris@275:     virtual PeakLocationSet getPeaks(PeakPickType type, size_t x,
Chris@275:                                      size_t ymin = 0, size_t ymax = 0);
Chris@275: 
Chris@275:     /**
Chris@275:      * Return locations and estimated stable frequencies of peak bins.
Chris@275:      */
Chris@275:     virtual PeakSet getPeakFrequencies(PeakPickType type, size_t x,
Chris@275:                                        size_t ymin = 0, size_t ymax = 0);
Chris@273: 
Chris@152:     virtual int getCompletion() const { return m_server->getFillCompletion(); }
Chris@152: 
Chris@152:     virtual Model *clone() const;
Chris@152: 
Chris@154:     virtual void suspend() { m_server->suspend(); }
Chris@155:     virtual void suspendWrites() { m_server->suspendWrites(); }
Chris@154:     virtual void resume() { m_server->resume(); }
Chris@154: 
Chris@345:     QString getTypeName() const { return tr("FFT"); }
Chris@345: 
Chris@360: public slots:
Chris@360:     void sourceModelAboutToBeDeleted();
Chris@360: 
Chris@152: private:
Chris@297:     FFTModel(const FFTModel &); // not implemented
Chris@152:     FFTModel &operator=(const FFTModel &); // not implemented
Chris@152: 
Chris@152:     FFTDataServer *m_server;
Chris@152:     int m_xshift;
Chris@152:     int m_yshift;
Chris@275: 
Chris@297:     FFTDataServer *getServer(const DenseTimeValueModel *,
Chris@297:                              int, WindowType, size_t, size_t, size_t,
Chris@334:                              bool, StorageAdviser::Criteria, size_t);
Chris@297: 
Chris@280:     size_t getPeakPickWindowSize(PeakPickType type, size_t sampleRate,
Chris@280:                                  size_t bin, float &percentile) const;
Chris@408: 
Chris@408:     size_t getYRatio() {
Chris@408:         size_t ys = m_yshift;
Chris@408:         size_t r = 1;
Chris@408:         while (ys) { --ys; r <<= 1; }
Chris@408:         return r;
Chris@408:     }
Chris@152: };
Chris@152: 
Chris@152: #endif