annotate layer/SpectrumLayer.cpp @ 193:57c2350a8c40

* Add slice layers (so you can display a slice of a colour 3d plot as if it were a spectrum) * Make spectrum layer a subclass of slice layer
author Chris Cannam
date Fri, 26 Jan 2007 16:59:57 +0000
parents 42118892f428
children 4a3bdde1ef13
rev   line source
Chris@133 1
Chris@133 2 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@133 3
Chris@133 4 /*
Chris@133 5 Sonic Visualiser
Chris@133 6 An audio file viewer and annotation editor.
Chris@133 7 Centre for Digital Music, Queen Mary, University of London.
Chris@182 8 This file copyright 2006 QMUL.
Chris@133 9
Chris@133 10 This program is free software; you can redistribute it and/or
Chris@133 11 modify it under the terms of the GNU General Public License as
Chris@133 12 published by the Free Software Foundation; either version 2 of the
Chris@133 13 License, or (at your option) any later version. See the file
Chris@133 14 COPYING included with this distribution for more information.
Chris@133 15 */
Chris@133 16
Chris@133 17 #include "SpectrumLayer.h"
Chris@133 18
Chris@133 19 #include "data/model/FFTModel.h"
Chris@133 20 #include "view/View.h"
Chris@153 21 #include "base/AudioLevel.h"
Chris@153 22 #include "base/Preferences.h"
Chris@167 23 #include "base/RangeMapper.h"
Chris@133 24
Chris@133 25 SpectrumLayer::SpectrumLayer() :
Chris@193 26 m_originModel(0),
Chris@153 27 m_channel(-1),
Chris@153 28 m_channelSet(false),
Chris@153 29 m_windowSize(1024),
Chris@153 30 m_windowType(HanningWindow),
Chris@153 31 m_windowHopLevel(2)
Chris@133 32 {
Chris@153 33 Preferences *prefs = Preferences::getInstance();
Chris@153 34 connect(prefs, SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@153 35 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
Chris@153 36 setWindowType(prefs->getWindowType());
Chris@133 37 }
Chris@133 38
Chris@133 39 SpectrumLayer::~SpectrumLayer()
Chris@133 40 {
Chris@193 41 //!!! delete parent's model
Chris@193 42 // for (size_t i = 0; i < m_fft.size(); ++i) delete m_fft[i];
Chris@133 43 }
Chris@133 44
Chris@133 45 void
Chris@133 46 SpectrumLayer::setModel(DenseTimeValueModel *model)
Chris@133 47 {
Chris@193 48 if (m_originModel == model) return;
Chris@193 49 m_originModel = model;
Chris@193 50 setupFFT();
Chris@153 51 }
Chris@153 52
Chris@153 53 void
Chris@193 54 SpectrumLayer::setupFFT()
Chris@153 55 {
Chris@193 56 FFTModel *oldFFT = dynamic_cast<FFTModel *>
Chris@193 57 (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
Chris@153 58
Chris@193 59 if (oldFFT) {
Chris@193 60 setSliceableModel(0);
Chris@193 61 delete oldFFT;
Chris@153 62 }
Chris@153 63
Chris@193 64 FFTModel *newFFT = new FFTModel(m_originModel,
Chris@193 65 m_channel,
Chris@193 66 m_windowType,
Chris@193 67 m_windowSize,
Chris@193 68 getWindowIncrement(),
Chris@193 69 m_windowSize,
Chris@193 70 true);
Chris@153 71
Chris@193 72 setSliceableModel(newFFT);
Chris@193 73
Chris@193 74 newFFT->resume();
Chris@153 75 }
Chris@153 76
Chris@153 77 void
Chris@153 78 SpectrumLayer::setChannel(int channel)
Chris@153 79 {
Chris@153 80 m_channelSet = true;
Chris@153 81
Chris@193 82 FFTModel *fft = dynamic_cast<FFTModel *>
Chris@193 83 (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
Chris@193 84
Chris@153 85 if (m_channel == channel) {
Chris@193 86 if (fft) fft->resume();
Chris@153 87 return;
Chris@153 88 }
Chris@153 89
Chris@153 90 m_channel = channel;
Chris@153 91
Chris@193 92 if (!fft) setupFFT();
Chris@153 93
Chris@153 94 emit layerParametersChanged();
Chris@133 95 }
Chris@133 96
Chris@153 97 Layer::PropertyList
Chris@153 98 SpectrumLayer::getProperties() const
Chris@153 99 {
Chris@193 100 PropertyList list = SliceLayer::getProperties();
Chris@153 101 list.push_back("Window Size");
Chris@153 102 list.push_back("Window Increment");
Chris@153 103 return list;
Chris@153 104 }
Chris@153 105
Chris@153 106 QString
Chris@153 107 SpectrumLayer::getPropertyLabel(const PropertyName &name) const
Chris@153 108 {
Chris@153 109 if (name == "Window Size") return tr("Window Size");
Chris@153 110 if (name == "Window Increment") return tr("Window Overlap");
Chris@193 111 return SliceLayer::getPropertyLabel(name);
Chris@153 112 }
Chris@153 113
Chris@153 114 Layer::PropertyType
Chris@153 115 SpectrumLayer::getPropertyType(const PropertyName &name) const
Chris@153 116 {
Chris@193 117 if (name == "Window Size") return ValueProperty;
Chris@193 118 if (name == "Window Increment") return ValueProperty;
Chris@193 119 return SliceLayer::getPropertyType(name);
Chris@153 120 }
Chris@153 121
Chris@153 122 QString
Chris@153 123 SpectrumLayer::getPropertyGroupName(const PropertyName &name) const
Chris@153 124 {
Chris@153 125 if (name == "Window Size" ||
Chris@153 126 name == "Window Increment") return tr("Window");
Chris@193 127 return SliceLayer::getPropertyGroupName(name);
Chris@153 128 }
Chris@153 129
Chris@153 130 int
Chris@153 131 SpectrumLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@153 132 int *min, int *max) const
Chris@153 133 {
Chris@153 134 int deft = 0;
Chris@153 135
Chris@153 136 int garbage0, garbage1;
Chris@153 137 if (!min) min = &garbage0;
Chris@153 138 if (!max) max = &garbage1;
Chris@153 139
Chris@193 140 if (name == "Window Size") {
Chris@153 141
Chris@153 142 *min = 0;
Chris@153 143 *max = 10;
Chris@153 144
Chris@153 145 deft = 0;
Chris@153 146 int ws = m_windowSize;
Chris@153 147 while (ws > 32) { ws >>= 1; deft ++; }
Chris@153 148
Chris@153 149 } else if (name == "Window Increment") {
Chris@153 150
Chris@153 151 *min = 0;
Chris@153 152 *max = 5;
Chris@153 153
Chris@153 154 deft = m_windowHopLevel;
Chris@153 155
Chris@153 156 } else {
Chris@193 157
Chris@193 158 deft = SliceLayer::getPropertyRangeAndValue(name, min, max);
Chris@153 159 }
Chris@153 160
Chris@153 161 return deft;
Chris@153 162 }
Chris@153 163
Chris@153 164 QString
Chris@153 165 SpectrumLayer::getPropertyValueLabel(const PropertyName &name,
Chris@153 166 int value) const
Chris@153 167 {
Chris@153 168 if (name == "Window Size") {
Chris@153 169 return QString("%1").arg(32 << value);
Chris@153 170 }
Chris@153 171 if (name == "Window Increment") {
Chris@153 172 switch (value) {
Chris@153 173 default:
Chris@153 174 case 0: return tr("None");
Chris@153 175 case 1: return tr("25 %");
Chris@153 176 case 2: return tr("50 %");
Chris@153 177 case 3: return tr("75 %");
Chris@153 178 case 4: return tr("87.5 %");
Chris@153 179 case 5: return tr("93.75 %");
Chris@153 180 }
Chris@153 181 }
Chris@193 182 return SliceLayer::getPropertyValueLabel(name, value);
Chris@153 183 }
Chris@153 184
Chris@167 185 RangeMapper *
Chris@167 186 SpectrumLayer::getNewPropertyRangeMapper(const PropertyName &name) const
Chris@167 187 {
Chris@193 188 return SliceLayer::getNewPropertyRangeMapper(name);
Chris@167 189 }
Chris@167 190
Chris@133 191 void
Chris@153 192 SpectrumLayer::setProperty(const PropertyName &name, int value)
Chris@133 193 {
Chris@193 194 if (name == "Window Size") {
Chris@153 195 setWindowSize(32 << value);
Chris@153 196 } else if (name == "Window Increment") {
Chris@153 197 setWindowHopLevel(value);
Chris@193 198 } else {
Chris@193 199 SliceLayer::setProperty(name, value);
Chris@153 200 }
Chris@153 201 }
Chris@153 202
Chris@153 203 void
Chris@153 204 SpectrumLayer::setWindowSize(size_t ws)
Chris@153 205 {
Chris@153 206 if (m_windowSize == ws) return;
Chris@153 207 m_windowSize = ws;
Chris@193 208 setupFFT();
Chris@153 209 emit layerParametersChanged();
Chris@153 210 }
Chris@153 211
Chris@153 212 void
Chris@153 213 SpectrumLayer::setWindowHopLevel(size_t v)
Chris@153 214 {
Chris@153 215 if (m_windowHopLevel == v) return;
Chris@153 216 m_windowHopLevel = v;
Chris@193 217 setupFFT();
Chris@153 218 emit layerParametersChanged();
Chris@153 219 }
Chris@153 220
Chris@153 221 void
Chris@153 222 SpectrumLayer::setWindowType(WindowType w)
Chris@153 223 {
Chris@153 224 if (m_windowType == w) return;
Chris@153 225 m_windowType = w;
Chris@193 226 setupFFT();
Chris@153 227 emit layerParametersChanged();
Chris@153 228 }
Chris@153 229
Chris@153 230 void
Chris@153 231 SpectrumLayer::preferenceChanged(PropertyContainer::PropertyName name)
Chris@153 232 {
Chris@153 233 if (name == "Window Type") {
Chris@153 234 setWindowType(Preferences::getInstance()->getWindowType());
Chris@153 235 return;
Chris@153 236 }
Chris@153 237 }
Chris@153 238
Chris@153 239 QString
Chris@153 240 SpectrumLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@153 241 {
Chris@153 242 QString s;
Chris@153 243
Chris@193 244 s += QString("windowSize=\"%1\" "
Chris@193 245 "windowHopLevel=\"%2\"")
Chris@153 246 .arg(m_windowSize)
Chris@193 247 .arg(m_windowHopLevel);
Chris@153 248
Chris@193 249 return SliceLayer::toXmlString(indent, extraAttributes + " " + s);
Chris@153 250 }
Chris@153 251
Chris@153 252 void
Chris@153 253 SpectrumLayer::setProperties(const QXmlAttributes &attributes)
Chris@153 254 {
Chris@153 255 bool ok = false;
Chris@153 256
Chris@153 257 size_t windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@153 258 if (ok) setWindowSize(windowSize);
Chris@153 259
Chris@153 260 size_t windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@153 261 if (ok) setWindowHopLevel(windowHopLevel);
Chris@133 262 }
Chris@133 263
Chris@133 264 bool
Chris@133 265 SpectrumLayer::getValueExtents(float &min, float &max, bool &logarithmic,
Chris@133 266 QString &units) const
Chris@133 267 {
Chris@133 268 return false;
Chris@133 269 }
Chris@133 270