annotate data/model/FFTModel.cpp @ 153:8d92611901d3

...
author Chris Cannam
date Wed, 02 Aug 2006 16:42:17 +0000
parents 21792a550ec9
children 0ed2b2e26b44
rev   line source
Chris@152 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@152 2
Chris@152 3 /*
Chris@152 4 Sonic Visualiser
Chris@152 5 An audio file viewer and annotation editor.
Chris@152 6 Centre for Digital Music, Queen Mary, University of London.
Chris@152 7 This file copyright 2006 Chris Cannam.
Chris@152 8
Chris@152 9 This program is free software; you can redistribute it and/or
Chris@152 10 modify it under the terms of the GNU General Public License as
Chris@152 11 published by the Free Software Foundation; either version 2 of the
Chris@152 12 License, or (at your option) any later version. See the file
Chris@152 13 COPYING included with this distribution for more information.
Chris@152 14 */
Chris@152 15
Chris@152 16 #include "FFTModel.h"
Chris@152 17 #include "DenseTimeValueModel.h"
Chris@152 18
Chris@152 19 #include <cassert>
Chris@152 20
Chris@152 21 FFTModel::FFTModel(const DenseTimeValueModel *model,
Chris@152 22 int channel,
Chris@152 23 WindowType windowType,
Chris@152 24 size_t windowSize,
Chris@152 25 size_t windowIncrement,
Chris@152 26 size_t fftSize,
Chris@152 27 bool polar,
Chris@152 28 size_t fillFromColumn) :
Chris@152 29 //!!! ZoomConstraint!
Chris@152 30 m_server(0),
Chris@152 31 m_xshift(0),
Chris@152 32 m_yshift(0)
Chris@152 33 {
Chris@152 34 m_server = FFTDataServer::getFuzzyInstance(model,
Chris@152 35 channel,
Chris@152 36 windowType,
Chris@152 37 windowSize,
Chris@152 38 windowIncrement,
Chris@152 39 fftSize,
Chris@152 40 polar,
Chris@152 41 fillFromColumn);
Chris@152 42
Chris@152 43 size_t xratio = windowIncrement / m_server->getWindowIncrement();
Chris@152 44 size_t yratio = m_server->getFFTSize() / fftSize;
Chris@152 45
Chris@152 46 while (xratio > 1) {
Chris@152 47 if (xratio & 0x1) {
Chris@152 48 std::cerr << "ERROR: FFTModel: Window increment ratio "
Chris@152 49 << windowIncrement << " / "
Chris@152 50 << m_server->getWindowIncrement()
Chris@152 51 << " must be a power of two" << std::endl;
Chris@152 52 assert(!(xratio & 0x1));
Chris@152 53 }
Chris@152 54 ++m_xshift;
Chris@152 55 xratio >>= 1;
Chris@152 56 }
Chris@152 57
Chris@152 58 while (yratio > 1) {
Chris@152 59 if (yratio & 0x1) {
Chris@152 60 std::cerr << "ERROR: FFTModel: FFT size ratio "
Chris@152 61 << m_server->getFFTSize() << " / " << fftSize
Chris@152 62 << " must be a power of two" << std::endl;
Chris@152 63 assert(!(yratio & 0x1));
Chris@152 64 }
Chris@152 65 ++m_yshift;
Chris@152 66 yratio >>= 1;
Chris@152 67 }
Chris@152 68 }
Chris@152 69
Chris@152 70 FFTModel::~FFTModel()
Chris@152 71 {
Chris@152 72 FFTDataServer::releaseInstance(m_server);
Chris@152 73 }
Chris@152 74
Chris@152 75 size_t
Chris@152 76 FFTModel::getSampleRate() const
Chris@152 77 {
Chris@152 78 return isOK() ? m_server->getModel()->getSampleRate() : 0;
Chris@152 79 }
Chris@152 80
Chris@152 81 void
Chris@152 82 FFTModel::getBinValues(long windowStartFrame, BinValueSet &result) const
Chris@152 83 {
Chris@152 84 if (windowStartFrame < 0) windowStartFrame = 0;
Chris@152 85 size_t x = windowStartFrame / getResolution();
Chris@152 86 result.clear();
Chris@152 87 size_t height(getHeight());
Chris@152 88 for (size_t y = 0; y < height; ++y) {
Chris@152 89 result.push_back(const_cast<FFTModel *>(this)->getMagnitudeAt(x, y));
Chris@152 90 }
Chris@152 91 }
Chris@152 92
Chris@152 93 float
Chris@152 94 FFTModel::getBinValue(long windowStartFrame, size_t n) const
Chris@152 95 {
Chris@152 96 if (windowStartFrame < 0) windowStartFrame = 0;
Chris@152 97 size_t x = windowStartFrame / getResolution();
Chris@152 98 return const_cast<FFTModel *>(this)->getMagnitudeAt(x, n);
Chris@152 99 }
Chris@152 100
Chris@152 101 QString
Chris@152 102 FFTModel::getBinName(size_t n) const
Chris@152 103 {
Chris@152 104 size_t sr = getSampleRate();
Chris@152 105 if (!sr) return "";
Chris@152 106 QString name = tr("%1 Hz").arg((n * sr) / (getHeight() * 2));
Chris@152 107 return name;
Chris@152 108 }
Chris@152 109
Chris@152 110 Model *
Chris@152 111 FFTModel::clone() const
Chris@152 112 {
Chris@152 113 return new FFTModel(*this);
Chris@152 114 }
Chris@152 115
Chris@152 116 FFTModel::FFTModel(const FFTModel &model) :
Chris@152 117 QObject(),
Chris@152 118 ZoomConstraint(), //!!! want a real ZoomConstraint for this!
Chris@152 119 DenseThreeDimensionalModel(),
Chris@152 120 m_server(model.m_server),
Chris@152 121 m_xshift(model.m_xshift),
Chris@152 122 m_yshift(model.m_yshift)
Chris@152 123 {
Chris@152 124 FFTDataServer::claimInstance(m_server);
Chris@152 125 }
Chris@152 126