annotate layer/SpectrumLayer.cpp @ 213:df791d8c8f58

* Fix real-time plugin corruption when getLatency is called for some plugins * Other minor changes
author Chris Cannam
date Thu, 01 Mar 2007 15:35:27 +0000
parents 45e995ed84d9
children 34bbbcb3c01f
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@153 134 int *min, int *max) const
Chris@153 135 {
Chris@153 136 int deft = 0;
Chris@153 137
Chris@153 138 int garbage0, garbage1;
Chris@153 139 if (!min) min = &garbage0;
Chris@153 140 if (!max) max = &garbage1;
Chris@153 141
Chris@193 142 if (name == "Window Size") {
Chris@153 143
Chris@153 144 *min = 0;
Chris@153 145 *max = 10;
Chris@153 146
Chris@153 147 deft = 0;
Chris@153 148 int ws = m_windowSize;
Chris@153 149 while (ws > 32) { ws >>= 1; deft ++; }
Chris@153 150
Chris@153 151 } else if (name == "Window Increment") {
Chris@153 152
Chris@153 153 *min = 0;
Chris@153 154 *max = 5;
Chris@153 155
Chris@153 156 deft = m_windowHopLevel;
Chris@153 157
Chris@153 158 } else {
Chris@193 159
Chris@193 160 deft = SliceLayer::getPropertyRangeAndValue(name, min, max);
Chris@153 161 }
Chris@153 162
Chris@153 163 return deft;
Chris@153 164 }
Chris@153 165
Chris@153 166 QString
Chris@153 167 SpectrumLayer::getPropertyValueLabel(const PropertyName &name,
Chris@153 168 int value) const
Chris@153 169 {
Chris@153 170 if (name == "Window Size") {
Chris@153 171 return QString("%1").arg(32 << value);
Chris@153 172 }
Chris@153 173 if (name == "Window Increment") {
Chris@153 174 switch (value) {
Chris@153 175 default:
Chris@153 176 case 0: return tr("None");
Chris@153 177 case 1: return tr("25 %");
Chris@153 178 case 2: return tr("50 %");
Chris@153 179 case 3: return tr("75 %");
Chris@153 180 case 4: return tr("87.5 %");
Chris@153 181 case 5: return tr("93.75 %");
Chris@153 182 }
Chris@153 183 }
Chris@193 184 return SliceLayer::getPropertyValueLabel(name, value);
Chris@153 185 }
Chris@153 186
Chris@167 187 RangeMapper *
Chris@167 188 SpectrumLayer::getNewPropertyRangeMapper(const PropertyName &name) const
Chris@167 189 {
Chris@193 190 return SliceLayer::getNewPropertyRangeMapper(name);
Chris@167 191 }
Chris@167 192
Chris@133 193 void
Chris@153 194 SpectrumLayer::setProperty(const PropertyName &name, int value)
Chris@133 195 {
Chris@193 196 if (name == "Window Size") {
Chris@153 197 setWindowSize(32 << value);
Chris@153 198 } else if (name == "Window Increment") {
Chris@153 199 setWindowHopLevel(value);
Chris@193 200 } else {
Chris@193 201 SliceLayer::setProperty(name, value);
Chris@153 202 }
Chris@153 203 }
Chris@153 204
Chris@153 205 void
Chris@153 206 SpectrumLayer::setWindowSize(size_t ws)
Chris@153 207 {
Chris@153 208 if (m_windowSize == ws) return;
Chris@153 209 m_windowSize = ws;
Chris@193 210 setupFFT();
Chris@153 211 emit layerParametersChanged();
Chris@153 212 }
Chris@153 213
Chris@153 214 void
Chris@153 215 SpectrumLayer::setWindowHopLevel(size_t v)
Chris@153 216 {
Chris@153 217 if (m_windowHopLevel == v) return;
Chris@153 218 m_windowHopLevel = v;
Chris@193 219 setupFFT();
Chris@153 220 emit layerParametersChanged();
Chris@153 221 }
Chris@153 222
Chris@153 223 void
Chris@153 224 SpectrumLayer::setWindowType(WindowType w)
Chris@153 225 {
Chris@153 226 if (m_windowType == w) return;
Chris@153 227 m_windowType = w;
Chris@193 228 setupFFT();
Chris@153 229 emit layerParametersChanged();
Chris@153 230 }
Chris@153 231
Chris@153 232 void
Chris@153 233 SpectrumLayer::preferenceChanged(PropertyContainer::PropertyName name)
Chris@153 234 {
Chris@153 235 if (name == "Window Type") {
Chris@153 236 setWindowType(Preferences::getInstance()->getWindowType());
Chris@153 237 return;
Chris@153 238 }
Chris@153 239 }
Chris@153 240
Chris@153 241 QString
Chris@153 242 SpectrumLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@153 243 {
Chris@153 244 QString s;
Chris@153 245
Chris@193 246 s += QString("windowSize=\"%1\" "
Chris@193 247 "windowHopLevel=\"%2\"")
Chris@153 248 .arg(m_windowSize)
Chris@193 249 .arg(m_windowHopLevel);
Chris@153 250
Chris@193 251 return SliceLayer::toXmlString(indent, extraAttributes + " " + s);
Chris@153 252 }
Chris@153 253
Chris@153 254 void
Chris@153 255 SpectrumLayer::setProperties(const QXmlAttributes &attributes)
Chris@153 256 {
Chris@153 257 bool ok = false;
Chris@153 258
Chris@153 259 size_t windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@153 260 if (ok) setWindowSize(windowSize);
Chris@153 261
Chris@153 262 size_t windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@153 263 if (ok) setWindowHopLevel(windowHopLevel);
Chris@133 264 }
Chris@133 265
Chris@133 266 bool
Chris@133 267 SpectrumLayer::getValueExtents(float &min, float &max, bool &logarithmic,
Chris@133 268 QString &units) const
Chris@133 269 {
Chris@133 270 return false;
Chris@133 271 }
Chris@133 272
Chris@199 273 QString
Chris@199 274 SpectrumLayer::getFeatureDescription(View *v, QPoint &p) const
Chris@199 275 {
Chris@199 276 if (!m_sliceableModel) return "";
Chris@199 277
Chris@199 278 int minbin = 0, maxbin = 0, range = 0;
Chris@199 279 QString genericDesc = SliceLayer::getFeatureDescription
Chris@199 280 (v, p, false, minbin, maxbin, range);
Chris@199 281
Chris@199 282 if (genericDesc == "") return "";
Chris@199 283
Chris@199 284 float minvalue = 0.f;
Chris@199 285 if (minbin < m_values.size()) minvalue = m_values[minbin];
Chris@199 286
Chris@199 287 float maxvalue = minvalue;
Chris@199 288 if (maxbin < m_values.size()) maxvalue = m_values[maxbin];
Chris@199 289
Chris@199 290 if (minvalue > maxvalue) std::swap(minvalue, maxvalue);
Chris@199 291
Chris@199 292 QString binstr;
Chris@199 293 QString hzstr;
Chris@199 294 int minfreq = lrintf((minbin * m_sliceableModel->getSampleRate()) /
Chris@199 295 m_windowSize);
Chris@199 296 int maxfreq = lrintf((std::max(maxbin, minbin+1)
Chris@199 297 * m_sliceableModel->getSampleRate()) /
Chris@199 298 m_windowSize);
Chris@199 299
Chris@199 300 if (maxbin != minbin) {
Chris@199 301 binstr = tr("%1 - %2").arg(minbin+1).arg(maxbin+1);
Chris@199 302 } else {
Chris@199 303 binstr = QString("%1").arg(minbin+1);
Chris@199 304 }
Chris@199 305 if (minfreq != maxfreq) {
Chris@199 306 hzstr = tr("%1 - %2 Hz").arg(minfreq).arg(maxfreq);
Chris@199 307 } else {
Chris@199 308 hzstr = tr("%1 Hz").arg(minfreq);
Chris@199 309 }
Chris@199 310
Chris@199 311 QString valuestr;
Chris@199 312 if (maxvalue != minvalue) {
Chris@199 313 valuestr = tr("%1 - %2").arg(minvalue).arg(maxvalue);
Chris@199 314 } else {
Chris@199 315 valuestr = QString("%1").arg(minvalue);
Chris@199 316 }
Chris@199 317
Chris@199 318 QString dbstr;
Chris@199 319 float mindb = AudioLevel::multiplier_to_dB(minvalue);
Chris@199 320 float maxdb = AudioLevel::multiplier_to_dB(maxvalue);
Chris@199 321 QString mindbstr;
Chris@199 322 QString maxdbstr;
Chris@199 323 if (mindb == AudioLevel::DB_FLOOR) {
Chris@199 324 mindbstr = tr("-Inf");
Chris@199 325 } else {
Chris@199 326 mindbstr = QString("%1").arg(lrintf(mindb));
Chris@199 327 }
Chris@199 328 if (maxdb == AudioLevel::DB_FLOOR) {
Chris@199 329 maxdbstr = tr("-Inf");
Chris@199 330 } else {
Chris@199 331 maxdbstr = QString("%1").arg(lrintf(maxdb));
Chris@199 332 }
Chris@199 333 if (lrintf(mindb) != lrintf(maxdb)) {
Chris@199 334 dbstr = tr("%1 - %2").arg(mindbstr).arg(maxdbstr);
Chris@199 335 } else {
Chris@199 336 dbstr = tr("%1").arg(mindbstr);
Chris@199 337 }
Chris@199 338
Chris@199 339 QString description;
Chris@199 340
Chris@199 341 if (range > m_sliceableModel->getResolution()) {
Chris@199 342 description = tr("%1\nBin:\t%2 (%3)\n%4 value:\t%5\ndB:\t%6")
Chris@199 343 .arg(genericDesc)
Chris@199 344 .arg(binstr)
Chris@199 345 .arg(hzstr)
Chris@199 346 .arg(m_samplingMode == NearestSample ? tr("First") :
Chris@199 347 m_samplingMode == SampleMean ? tr("Mean") : tr("Peak"))
Chris@199 348 .arg(valuestr)
Chris@199 349 .arg(dbstr);
Chris@199 350 } else {
Chris@199 351 description = tr("%1\nBin:\t%2 (%3)\nValue:\t%4\ndB:\t%5")
Chris@199 352 .arg(genericDesc)
Chris@199 353 .arg(binstr)
Chris@199 354 .arg(hzstr)
Chris@199 355 .arg(valuestr)
Chris@199 356 .arg(dbstr);
Chris@199 357 }
Chris@199 358
Chris@199 359 return description;
Chris@199 360 }
Chris@199 361
Chris@199 362
Chris@199 363