comparison data/model/FFTModel.cpp @ 152:21792a550ec9 last-cc-copyright

* Move the current DenseThreeDimensionalModel to EditableDenseThreeDimensionalModel (wow!), and make DTDM an abstract base * Move FFTFuzzyAdapter to FFTModel as a new subclass of DTDM
author Chris Cannam
date Mon, 31 Jul 2006 17:05:18 +0000
parents
children 0ed2b2e26b44
comparison
equal deleted inserted replaced
151:3c1d5ef43baa 152:21792a550ec9
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.
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 #include "FFTModel.h"
17 #include "DenseTimeValueModel.h"
18
19 #include <cassert>
20
21 FFTModel::FFTModel(const DenseTimeValueModel *model,
22 int channel,
23 WindowType windowType,
24 size_t windowSize,
25 size_t windowIncrement,
26 size_t fftSize,
27 bool polar,
28 size_t fillFromColumn) :
29 //!!! ZoomConstraint!
30 m_server(0),
31 m_xshift(0),
32 m_yshift(0)
33 {
34 m_server = FFTDataServer::getFuzzyInstance(model,
35 channel,
36 windowType,
37 windowSize,
38 windowIncrement,
39 fftSize,
40 polar,
41 fillFromColumn);
42
43 size_t xratio = windowIncrement / m_server->getWindowIncrement();
44 size_t yratio = m_server->getFFTSize() / fftSize;
45
46 while (xratio > 1) {
47 if (xratio & 0x1) {
48 std::cerr << "ERROR: FFTModel: Window increment ratio "
49 << windowIncrement << " / "
50 << m_server->getWindowIncrement()
51 << " must be a power of two" << std::endl;
52 assert(!(xratio & 0x1));
53 }
54 ++m_xshift;
55 xratio >>= 1;
56 }
57
58 while (yratio > 1) {
59 if (yratio & 0x1) {
60 std::cerr << "ERROR: FFTModel: FFT size ratio "
61 << m_server->getFFTSize() << " / " << fftSize
62 << " must be a power of two" << std::endl;
63 assert(!(yratio & 0x1));
64 }
65 ++m_yshift;
66 yratio >>= 1;
67 }
68 }
69
70 FFTModel::~FFTModel()
71 {
72 FFTDataServer::releaseInstance(m_server);
73 }
74
75 size_t
76 FFTModel::getSampleRate() const
77 {
78 return isOK() ? m_server->getModel()->getSampleRate() : 0;
79 }
80
81 void
82 FFTModel::getBinValues(long windowStartFrame, BinValueSet &result) const
83 {
84 if (windowStartFrame < 0) windowStartFrame = 0;
85 size_t x = windowStartFrame / getResolution();
86 result.clear();
87 size_t height(getHeight());
88 for (size_t y = 0; y < height; ++y) {
89 result.push_back(const_cast<FFTModel *>(this)->getMagnitudeAt(x, y));
90 }
91 }
92
93 float
94 FFTModel::getBinValue(long windowStartFrame, size_t n) const
95 {
96 if (windowStartFrame < 0) windowStartFrame = 0;
97 size_t x = windowStartFrame / getResolution();
98 return const_cast<FFTModel *>(this)->getMagnitudeAt(x, n);
99 }
100
101 QString
102 FFTModel::getBinName(size_t n) const
103 {
104 size_t sr = getSampleRate();
105 if (!sr) return "";
106 QString name = tr("%1 Hz").arg((n * sr) / (getHeight() * 2));
107 return name;
108 }
109
110 Model *
111 FFTModel::clone() const
112 {
113 return new FFTModel(*this);
114 }
115
116 FFTModel::FFTModel(const FFTModel &model) :
117 QObject(),
118 ZoomConstraint(), //!!! want a real ZoomConstraint for this!
119 DenseThreeDimensionalModel(),
120 m_server(model.m_server),
121 m_xshift(model.m_xshift),
122 m_yshift(model.m_yshift)
123 {
124 FFTDataServer::claimInstance(m_server);
125 }
126