annotate layer/SpectrumLayer.cpp @ 220:8dc50f57d480

* Make the vertical scale a bit less thtoopid for many linear scale spectra * Don't forget to reload slice layer properties when loading a session, as well as spectrum layer (i.e. subclass) properties
author Chris Cannam
date Mon, 05 Mar 2007 13:04:52 +0000
parents 34bbbcb3c01f
children 28c8e8e3c537
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@133 244 bool
Chris@133 245 SpectrumLayer::getValueExtents(float &min, float &max, bool &logarithmic,
Chris@133 246 QString &units) const
Chris@133 247 {
Chris@133 248 return false;
Chris@133 249 }
Chris@133 250
Chris@199 251 QString
Chris@199 252 SpectrumLayer::getFeatureDescription(View *v, QPoint &p) const
Chris@199 253 {
Chris@199 254 if (!m_sliceableModel) return "";
Chris@199 255
Chris@199 256 int minbin = 0, maxbin = 0, range = 0;
Chris@199 257 QString genericDesc = SliceLayer::getFeatureDescription
Chris@199 258 (v, p, false, minbin, maxbin, range);
Chris@199 259
Chris@199 260 if (genericDesc == "") return "";
Chris@199 261
Chris@199 262 float minvalue = 0.f;
Chris@199 263 if (minbin < m_values.size()) minvalue = m_values[minbin];
Chris@199 264
Chris@199 265 float maxvalue = minvalue;
Chris@199 266 if (maxbin < m_values.size()) maxvalue = m_values[maxbin];
Chris@199 267
Chris@199 268 if (minvalue > maxvalue) std::swap(minvalue, maxvalue);
Chris@199 269
Chris@199 270 QString binstr;
Chris@199 271 QString hzstr;
Chris@199 272 int minfreq = lrintf((minbin * m_sliceableModel->getSampleRate()) /
Chris@199 273 m_windowSize);
Chris@199 274 int maxfreq = lrintf((std::max(maxbin, minbin+1)
Chris@199 275 * m_sliceableModel->getSampleRate()) /
Chris@199 276 m_windowSize);
Chris@199 277
Chris@199 278 if (maxbin != minbin) {
Chris@199 279 binstr = tr("%1 - %2").arg(minbin+1).arg(maxbin+1);
Chris@199 280 } else {
Chris@199 281 binstr = QString("%1").arg(minbin+1);
Chris@199 282 }
Chris@199 283 if (minfreq != maxfreq) {
Chris@199 284 hzstr = tr("%1 - %2 Hz").arg(minfreq).arg(maxfreq);
Chris@199 285 } else {
Chris@199 286 hzstr = tr("%1 Hz").arg(minfreq);
Chris@199 287 }
Chris@199 288
Chris@199 289 QString valuestr;
Chris@199 290 if (maxvalue != minvalue) {
Chris@199 291 valuestr = tr("%1 - %2").arg(minvalue).arg(maxvalue);
Chris@199 292 } else {
Chris@199 293 valuestr = QString("%1").arg(minvalue);
Chris@199 294 }
Chris@199 295
Chris@199 296 QString dbstr;
Chris@199 297 float mindb = AudioLevel::multiplier_to_dB(minvalue);
Chris@199 298 float maxdb = AudioLevel::multiplier_to_dB(maxvalue);
Chris@199 299 QString mindbstr;
Chris@199 300 QString maxdbstr;
Chris@199 301 if (mindb == AudioLevel::DB_FLOOR) {
Chris@199 302 mindbstr = tr("-Inf");
Chris@199 303 } else {
Chris@199 304 mindbstr = QString("%1").arg(lrintf(mindb));
Chris@199 305 }
Chris@199 306 if (maxdb == AudioLevel::DB_FLOOR) {
Chris@199 307 maxdbstr = tr("-Inf");
Chris@199 308 } else {
Chris@199 309 maxdbstr = QString("%1").arg(lrintf(maxdb));
Chris@199 310 }
Chris@199 311 if (lrintf(mindb) != lrintf(maxdb)) {
Chris@199 312 dbstr = tr("%1 - %2").arg(mindbstr).arg(maxdbstr);
Chris@199 313 } else {
Chris@199 314 dbstr = tr("%1").arg(mindbstr);
Chris@199 315 }
Chris@199 316
Chris@199 317 QString description;
Chris@199 318
Chris@199 319 if (range > m_sliceableModel->getResolution()) {
Chris@199 320 description = tr("%1\nBin:\t%2 (%3)\n%4 value:\t%5\ndB:\t%6")
Chris@199 321 .arg(genericDesc)
Chris@199 322 .arg(binstr)
Chris@199 323 .arg(hzstr)
Chris@199 324 .arg(m_samplingMode == NearestSample ? tr("First") :
Chris@199 325 m_samplingMode == SampleMean ? tr("Mean") : tr("Peak"))
Chris@199 326 .arg(valuestr)
Chris@199 327 .arg(dbstr);
Chris@199 328 } else {
Chris@199 329 description = tr("%1\nBin:\t%2 (%3)\nValue:\t%4\ndB:\t%5")
Chris@199 330 .arg(genericDesc)
Chris@199 331 .arg(binstr)
Chris@199 332 .arg(hzstr)
Chris@199 333 .arg(valuestr)
Chris@199 334 .arg(dbstr);
Chris@199 335 }
Chris@199 336
Chris@199 337 return description;
Chris@199 338 }
Chris@199 339
Chris@199 340
Chris@220 341 QString
Chris@220 342 SpectrumLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@220 343 {
Chris@220 344 QString s;
Chris@199 345
Chris@220 346 s += QString("windowSize=\"%1\" "
Chris@220 347 "windowHopLevel=\"%2\"")
Chris@220 348 .arg(m_windowSize)
Chris@220 349 .arg(m_windowHopLevel);
Chris@220 350
Chris@220 351 return SliceLayer::toXmlString(indent, extraAttributes + " " + s);
Chris@220 352 }
Chris@220 353
Chris@220 354 void
Chris@220 355 SpectrumLayer::setProperties(const QXmlAttributes &attributes)
Chris@220 356 {
Chris@220 357 SliceLayer::setProperties(attributes);
Chris@220 358
Chris@220 359 bool ok = false;
Chris@220 360
Chris@220 361 size_t windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@220 362 if (ok) setWindowSize(windowSize);
Chris@220 363
Chris@220 364 size_t windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@220 365 if (ok) setWindowHopLevel(windowHopLevel);
Chris@220 366 }
Chris@220 367
Chris@220 368