FFTModel.h

Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 Chris Cannam.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #ifndef _FFT_MODEL_H_
00017 #define _FFT_MODEL_H_
00018 
00019 #include "data/fft/FFTDataServer.h"
00020 #include "DenseThreeDimensionalModel.h"
00021 
00022 #include <set>
00023 #include <map>
00024 
00031 class FFTModel : public DenseThreeDimensionalModel
00032 {
00033     Q_OBJECT
00034 
00035 public:
00056     FFTModel(const DenseTimeValueModel *model,
00057              int channel,
00058              WindowType windowType,
00059              size_t windowSize,
00060              size_t windowIncrement,
00061              size_t fftSize,
00062              bool polar,
00063              StorageAdviser::Criteria criteria = StorageAdviser::NoCriteria,
00064              size_t fillFromColumn = 0);
00065     ~FFTModel();
00066 
00067     float getMagnitudeAt(size_t x, size_t y) {
00068         return m_server->getMagnitudeAt(x << m_xshift, y << m_yshift);
00069     }
00070     float getNormalizedMagnitudeAt(size_t x, size_t y) {
00071         return m_server->getNormalizedMagnitudeAt(x << m_xshift, y << m_yshift);
00072     }
00073     float getMaximumMagnitudeAt(size_t x) {
00074         return m_server->getMaximumMagnitudeAt(x << m_xshift);
00075     }
00076     float getPhaseAt(size_t x, size_t y) {
00077         return m_server->getPhaseAt(x << m_xshift, y << m_yshift);
00078     }
00079     void getValuesAt(size_t x, size_t y, float &real, float &imaginary) {
00080         m_server->getValuesAt(x << m_xshift, y << m_yshift, real, imaginary);
00081     }
00082     bool isColumnAvailable(size_t x) const {
00083         return m_server->isColumnReady(x << m_xshift);
00084     }
00085 
00086     size_t getFillExtent() const { return m_server->getFillExtent(); }
00087 
00088     // DenseThreeDimensionalModel and Model methods:
00089     //
00090     virtual size_t getWidth() const {
00091         return m_server->getWidth() >> m_xshift;
00092     }
00093     virtual size_t getHeight() const {
00094         // If there is no y-shift, the server's height (based on its
00095         // fftsize/2 + 1) is correct.  If there is a shift, then the
00096         // server is using a larger fft size than we want, so we shift
00097         // it right as many times as necessary, but then we need to
00098         // re-add the "+1" part (because ((fftsize*2)/2 + 1) / 2 !=
00099         // fftsize/2 + 1).
00100         return (m_server->getHeight() >> m_yshift) + (m_yshift > 0 ? 1 : 0);
00101     }
00102     virtual float getValueAt(size_t x, size_t y) const {
00103         return const_cast<FFTModel *>(this)->getMagnitudeAt(x, y);
00104     }
00105     virtual bool isOK() const {
00106         return m_server && m_server->getModel();
00107     }
00108     virtual size_t getStartFrame() const {
00109         return 0;
00110     }
00111     virtual size_t getEndFrame() const {
00112         return getWidth() * getResolution() + getResolution();
00113     }
00114     virtual size_t getSampleRate() const;
00115     virtual size_t getResolution() const {
00116         return m_server->getWindowIncrement() << m_xshift;
00117     }
00118     virtual size_t getYBinCount() const {
00119         return getHeight();
00120     }
00121     virtual float getMinimumLevel() const {
00122         return 0.f; // Can't provide
00123     }
00124     virtual float getMaximumLevel() const {
00125         return 1.f; // Can't provide
00126     }
00127     virtual void getColumn(size_t x, Column &result) const;
00128     virtual QString getBinName(size_t n) const;
00129 
00135     virtual bool estimateStableFrequency(size_t x, size_t y, float &frequency);
00136 
00137     enum PeakPickType
00138     {
00139         AllPeaks,                
00140         MajorPeaks,              
00141         MajorPitchAdaptivePeaks  
00142     };
00143 
00144     typedef std::set<size_t> PeakLocationSet;
00145     typedef std::map<size_t, float> PeakSet;
00146 
00151     virtual PeakLocationSet getPeaks(PeakPickType type, size_t x,
00152                                      size_t ymin = 0, size_t ymax = 0);
00153 
00157     virtual PeakSet getPeakFrequencies(PeakPickType type, size_t x,
00158                                        size_t ymin = 0, size_t ymax = 0);
00159 
00160     virtual int getCompletion() const { return m_server->getFillCompletion(); }
00161 
00162     virtual Model *clone() const;
00163 
00164     virtual void suspend() { m_server->suspend(); }
00165     virtual void suspendWrites() { m_server->suspendWrites(); }
00166     virtual void resume() { m_server->resume(); }
00167 
00168     QString getTypeName() const { return tr("FFT"); }
00169 
00170 public slots:
00171     void sourceModelAboutToBeDeleted();
00172 
00173 private:
00174     FFTModel(const FFTModel &); // not implemented
00175     FFTModel &operator=(const FFTModel &); // not implemented
00176 
00177     FFTDataServer *m_server;
00178     int m_xshift;
00179     int m_yshift;
00180 
00181     FFTDataServer *getServer(const DenseTimeValueModel *,
00182                              int, WindowType, size_t, size_t, size_t,
00183                              bool, StorageAdviser::Criteria, size_t);
00184 
00185     size_t getPeakPickWindowSize(PeakPickType type, size_t sampleRate,
00186                                  size_t bin, float &percentile) const;
00187 };
00188 
00189 #endif

Generated on Wed Feb 20 15:45:25 2008 for SonicVisualiser by  doxygen 1.5.1