annotate layer/SpectrumLayer.cpp @ 216:34bbbcb3c01f sv1-1.0pre1

* Make getPropertyRangeAndValue return the default separately from the current value. Previously some contexts were incorrectly treating the current value as a default.
author Chris Cannam
date Fri, 02 Mar 2007 14:00:12 +0000
parents 45e995ed84d9
children 8dc50f57d480
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@195 8 This file copyright 2006-2007 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@195 37
Chris@195 38 setBinScale(LogBins);
Chris@133 39 }
Chris@133 40
Chris@133 41 SpectrumLayer::~SpectrumLayer()
Chris@133 42 {
Chris@193 43 //!!! delete parent's model
Chris@193 44 // for (size_t i = 0; i < m_fft.size(); ++i) delete m_fft[i];
Chris@133 45 }
Chris@133 46
Chris@133 47 void
Chris@133 48 SpectrumLayer::setModel(DenseTimeValueModel *model)
Chris@133 49 {
Chris@193 50 if (m_originModel == model) return;
Chris@193 51 m_originModel = model;
Chris@193 52 setupFFT();
Chris@153 53 }
Chris@153 54
Chris@153 55 void
Chris@193 56 SpectrumLayer::setupFFT()
Chris@153 57 {
Chris@193 58 FFTModel *oldFFT = dynamic_cast<FFTModel *>
Chris@193 59 (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
Chris@153 60
Chris@193 61 if (oldFFT) {
Chris@193 62 setSliceableModel(0);
Chris@193 63 delete oldFFT;
Chris@153 64 }
Chris@153 65
Chris@193 66 FFTModel *newFFT = new FFTModel(m_originModel,
Chris@193 67 m_channel,
Chris@193 68 m_windowType,
Chris@193 69 m_windowSize,
Chris@193 70 getWindowIncrement(),
Chris@193 71 m_windowSize,
Chris@193 72 true);
Chris@153 73
Chris@193 74 setSliceableModel(newFFT);
Chris@193 75
Chris@193 76 newFFT->resume();
Chris@153 77 }
Chris@153 78
Chris@153 79 void
Chris@153 80 SpectrumLayer::setChannel(int channel)
Chris@153 81 {
Chris@153 82 m_channelSet = true;
Chris@153 83
Chris@193 84 FFTModel *fft = dynamic_cast<FFTModel *>
Chris@193 85 (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
Chris@193 86
Chris@153 87 if (m_channel == channel) {
Chris@193 88 if (fft) fft->resume();
Chris@153 89 return;
Chris@153 90 }
Chris@153 91
Chris@153 92 m_channel = channel;
Chris@153 93
Chris@193 94 if (!fft) setupFFT();
Chris@153 95
Chris@153 96 emit layerParametersChanged();
Chris@133 97 }
Chris@133 98
Chris@153 99 Layer::PropertyList
Chris@153 100 SpectrumLayer::getProperties() const
Chris@153 101 {
Chris@193 102 PropertyList list = SliceLayer::getProperties();
Chris@153 103 list.push_back("Window Size");
Chris@153 104 list.push_back("Window Increment");
Chris@153 105 return list;
Chris@153 106 }
Chris@153 107
Chris@153 108 QString
Chris@153 109 SpectrumLayer::getPropertyLabel(const PropertyName &name) const
Chris@153 110 {
Chris@153 111 if (name == "Window Size") return tr("Window Size");
Chris@153 112 if (name == "Window Increment") return tr("Window Overlap");
Chris@193 113 return SliceLayer::getPropertyLabel(name);
Chris@153 114 }
Chris@153 115
Chris@153 116 Layer::PropertyType
Chris@153 117 SpectrumLayer::getPropertyType(const PropertyName &name) const
Chris@153 118 {
Chris@193 119 if (name == "Window Size") return ValueProperty;
Chris@193 120 if (name == "Window Increment") return ValueProperty;
Chris@193 121 return SliceLayer::getPropertyType(name);
Chris@153 122 }
Chris@153 123
Chris@153 124 QString
Chris@153 125 SpectrumLayer::getPropertyGroupName(const PropertyName &name) const
Chris@153 126 {
Chris@153 127 if (name == "Window Size" ||
Chris@153 128 name == "Window Increment") return tr("Window");
Chris@193 129 return SliceLayer::getPropertyGroupName(name);
Chris@153 130 }
Chris@153 131
Chris@153 132 int
Chris@153 133 SpectrumLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 134 int *min, int *max, int *deflt) const
Chris@153 135 {
Chris@216 136 int val = 0;
Chris@153 137
Chris@216 138 int garbage0, garbage1, garbage2;
Chris@153 139 if (!min) min = &garbage0;
Chris@153 140 if (!max) max = &garbage1;
Chris@216 141 if (!deflt) deflt = &garbage2;
Chris@153 142
Chris@193 143 if (name == "Window Size") {
Chris@153 144
Chris@153 145 *min = 0;
Chris@153 146 *max = 10;
Chris@216 147 *deflt = 5;
Chris@153 148
Chris@216 149 val = 0;
Chris@153 150 int ws = m_windowSize;
Chris@216 151 while (ws > 32) { ws >>= 1; val ++; }
Chris@153 152
Chris@153 153 } else if (name == "Window Increment") {
Chris@153 154
Chris@153 155 *min = 0;
Chris@153 156 *max = 5;
Chris@216 157 *deflt = 2;
Chris@153 158
Chris@216 159 val = m_windowHopLevel;
Chris@153 160
Chris@153 161 } else {
Chris@193 162
Chris@216 163 val = SliceLayer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@153 164 }
Chris@153 165
Chris@216 166 return val;
Chris@153 167 }
Chris@153 168
Chris@153 169 QString
Chris@153 170 SpectrumLayer::getPropertyValueLabel(const PropertyName &name,
Chris@153 171 int value) const
Chris@153 172 {
Chris@153 173 if (name == "Window Size") {
Chris@153 174 return QString("%1").arg(32 << value);
Chris@153 175 }
Chris@153 176 if (name == "Window Increment") {
Chris@153 177 switch (value) {
Chris@153 178 default:
Chris@153 179 case 0: return tr("None");
Chris@153 180 case 1: return tr("25 %");
Chris@153 181 case 2: return tr("50 %");
Chris@153 182 case 3: return tr("75 %");
Chris@153 183 case 4: return tr("87.5 %");
Chris@153 184 case 5: return tr("93.75 %");
Chris@153 185 }
Chris@153 186 }
Chris@193 187 return SliceLayer::getPropertyValueLabel(name, value);
Chris@153 188 }
Chris@153 189
Chris@167 190 RangeMapper *
Chris@167 191 SpectrumLayer::getNewPropertyRangeMapper(const PropertyName &name) const
Chris@167 192 {
Chris@193 193 return SliceLayer::getNewPropertyRangeMapper(name);
Chris@167 194 }
Chris@167 195
Chris@133 196 void
Chris@153 197 SpectrumLayer::setProperty(const PropertyName &name, int value)
Chris@133 198 {
Chris@193 199 if (name == "Window Size") {
Chris@153 200 setWindowSize(32 << value);
Chris@153 201 } else if (name == "Window Increment") {
Chris@153 202 setWindowHopLevel(value);
Chris@193 203 } else {
Chris@193 204 SliceLayer::setProperty(name, value);
Chris@153 205 }
Chris@153 206 }
Chris@153 207
Chris@153 208 void
Chris@153 209 SpectrumLayer::setWindowSize(size_t ws)
Chris@153 210 {
Chris@153 211 if (m_windowSize == ws) return;
Chris@153 212 m_windowSize = ws;
Chris@193 213 setupFFT();
Chris@153 214 emit layerParametersChanged();
Chris@153 215 }
Chris@153 216
Chris@153 217 void
Chris@153 218 SpectrumLayer::setWindowHopLevel(size_t v)
Chris@153 219 {
Chris@153 220 if (m_windowHopLevel == v) return;
Chris@153 221 m_windowHopLevel = v;
Chris@193 222 setupFFT();
Chris@153 223 emit layerParametersChanged();
Chris@153 224 }
Chris@153 225
Chris@153 226 void
Chris@153 227 SpectrumLayer::setWindowType(WindowType w)
Chris@153 228 {
Chris@153 229 if (m_windowType == w) return;
Chris@153 230 m_windowType = w;
Chris@193 231 setupFFT();
Chris@153 232 emit layerParametersChanged();
Chris@153 233 }
Chris@153 234
Chris@153 235 void
Chris@153 236 SpectrumLayer::preferenceChanged(PropertyContainer::PropertyName name)
Chris@153 237 {
Chris@153 238 if (name == "Window Type") {
Chris@153 239 setWindowType(Preferences::getInstance()->getWindowType());
Chris@153 240 return;
Chris@153 241 }
Chris@153 242 }
Chris@153 243
Chris@153 244 QString
Chris@153 245 SpectrumLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@153 246 {
Chris@153 247 QString s;
Chris@153 248
Chris@193 249 s += QString("windowSize=\"%1\" "
Chris@193 250 "windowHopLevel=\"%2\"")
Chris@153 251 .arg(m_windowSize)
Chris@193 252 .arg(m_windowHopLevel);
Chris@153 253
Chris@193 254 return SliceLayer::toXmlString(indent, extraAttributes + " " + s);
Chris@153 255 }
Chris@153 256
Chris@153 257 void
Chris@153 258 SpectrumLayer::setProperties(const QXmlAttributes &attributes)
Chris@153 259 {
Chris@153 260 bool ok = false;
Chris@153 261
Chris@153 262 size_t windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@153 263 if (ok) setWindowSize(windowSize);
Chris@153 264
Chris@153 265 size_t windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@153 266 if (ok) setWindowHopLevel(windowHopLevel);
Chris@133 267 }
Chris@133 268
Chris@133 269 bool
Chris@133 270 SpectrumLayer::getValueExtents(float &min, float &max, bool &logarithmic,
Chris@133 271 QString &units) const
Chris@133 272 {
Chris@133 273 return false;
Chris@133 274 }
Chris@133 275
Chris@199 276 QString
Chris@199 277 SpectrumLayer::getFeatureDescription(View *v, QPoint &p) const
Chris@199 278 {
Chris@199 279 if (!m_sliceableModel) return "";
Chris@199 280
Chris@199 281 int minbin = 0, maxbin = 0, range = 0;
Chris@199 282 QString genericDesc = SliceLayer::getFeatureDescription
Chris@199 283 (v, p, false, minbin, maxbin, range);
Chris@199 284
Chris@199 285 if (genericDesc == "") return "";
Chris@199 286
Chris@199 287 float minvalue = 0.f;
Chris@199 288 if (minbin < m_values.size()) minvalue = m_values[minbin];
Chris@199 289
Chris@199 290 float maxvalue = minvalue;
Chris@199 291 if (maxbin < m_values.size()) maxvalue = m_values[maxbin];
Chris@199 292
Chris@199 293 if (minvalue > maxvalue) std::swap(minvalue, maxvalue);
Chris@199 294
Chris@199 295 QString binstr;
Chris@199 296 QString hzstr;
Chris@199 297 int minfreq = lrintf((minbin * m_sliceableModel->getSampleRate()) /
Chris@199 298 m_windowSize);
Chris@199 299 int maxfreq = lrintf((std::max(maxbin, minbin+1)
Chris@199 300 * m_sliceableModel->getSampleRate()) /
Chris@199 301 m_windowSize);
Chris@199 302
Chris@199 303 if (maxbin != minbin) {
Chris@199 304 binstr = tr("%1 - %2").arg(minbin+1).arg(maxbin+1);
Chris@199 305 } else {
Chris@199 306 binstr = QString("%1").arg(minbin+1);
Chris@199 307 }
Chris@199 308 if (minfreq != maxfreq) {
Chris@199 309 hzstr = tr("%1 - %2 Hz").arg(minfreq).arg(maxfreq);
Chris@199 310 } else {
Chris@199 311 hzstr = tr("%1 Hz").arg(minfreq);
Chris@199 312 }
Chris@199 313
Chris@199 314 QString valuestr;
Chris@199 315 if (maxvalue != minvalue) {
Chris@199 316 valuestr = tr("%1 - %2").arg(minvalue).arg(maxvalue);
Chris@199 317 } else {
Chris@199 318 valuestr = QString("%1").arg(minvalue);
Chris@199 319 }
Chris@199 320
Chris@199 321 QString dbstr;
Chris@199 322 float mindb = AudioLevel::multiplier_to_dB(minvalue);
Chris@199 323 float maxdb = AudioLevel::multiplier_to_dB(maxvalue);
Chris@199 324 QString mindbstr;
Chris@199 325 QString maxdbstr;
Chris@199 326 if (mindb == AudioLevel::DB_FLOOR) {
Chris@199 327 mindbstr = tr("-Inf");
Chris@199 328 } else {
Chris@199 329 mindbstr = QString("%1").arg(lrintf(mindb));
Chris@199 330 }
Chris@199 331 if (maxdb == AudioLevel::DB_FLOOR) {
Chris@199 332 maxdbstr = tr("-Inf");
Chris@199 333 } else {
Chris@199 334 maxdbstr = QString("%1").arg(lrintf(maxdb));
Chris@199 335 }
Chris@199 336 if (lrintf(mindb) != lrintf(maxdb)) {
Chris@199 337 dbstr = tr("%1 - %2").arg(mindbstr).arg(maxdbstr);
Chris@199 338 } else {
Chris@199 339 dbstr = tr("%1").arg(mindbstr);
Chris@199 340 }
Chris@199 341
Chris@199 342 QString description;
Chris@199 343
Chris@199 344 if (range > m_sliceableModel->getResolution()) {
Chris@199 345 description = tr("%1\nBin:\t%2 (%3)\n%4 value:\t%5\ndB:\t%6")
Chris@199 346 .arg(genericDesc)
Chris@199 347 .arg(binstr)
Chris@199 348 .arg(hzstr)
Chris@199 349 .arg(m_samplingMode == NearestSample ? tr("First") :
Chris@199 350 m_samplingMode == SampleMean ? tr("Mean") : tr("Peak"))
Chris@199 351 .arg(valuestr)
Chris@199 352 .arg(dbstr);
Chris@199 353 } else {
Chris@199 354 description = tr("%1\nBin:\t%2 (%3)\nValue:\t%4\ndB:\t%5")
Chris@199 355 .arg(genericDesc)
Chris@199 356 .arg(binstr)
Chris@199 357 .arg(hzstr)
Chris@199 358 .arg(valuestr)
Chris@199 359 .arg(dbstr);
Chris@199 360 }
Chris@199 361
Chris@199 362 return description;
Chris@199 363 }
Chris@199 364
Chris@199 365
Chris@199 366