annotate layer/TimeRulerLayer.cpp @ 277:8acd30ed735c

* Fix up and simplify the LayerTreeModel, removing a horrible memory leak * Move phase-unwrapped frequency estimation from SpectrogramLayer to FFTDataServer * Make the spectrum show peak phase-unwrapped frequencies as well (still needs work) * Start adding piano keyboard horizontal scale to spectrum * Debug output for id3 tags
author Chris Cannam
date Tue, 03 Jul 2007 12:46:18 +0000
parents 87e4c880b4c8
children cd2492c5fe45
rev   line source
Chris@58 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@59 4 Sonic Visualiser
Chris@59 5 An audio file viewer and annotation editor.
Chris@59 6 Centre for Digital Music, Queen Mary, University of London.
Chris@59 7 This file copyright 2006 Chris Cannam.
Chris@0 8
Chris@59 9 This program is free software; you can redistribute it and/or
Chris@59 10 modify it under the terms of the GNU General Public License as
Chris@59 11 published by the Free Software Foundation; either version 2 of the
Chris@59 12 License, or (at your option) any later version. See the file
Chris@59 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "TimeRulerLayer.h"
Chris@0 17
Chris@128 18 #include "data/model/Model.h"
Chris@0 19 #include "base/RealTime.h"
Chris@128 20 #include "view/View.h"
Chris@0 21
Chris@0 22 #include <QPainter>
Chris@0 23
Chris@0 24 #include <iostream>
Chris@271 25 #include <cmath>
Chris@0 26
Chris@0 27 using std::cerr;
Chris@0 28 using std::endl;
Chris@0 29
Chris@44 30 TimeRulerLayer::TimeRulerLayer() :
Chris@44 31 Layer(),
Chris@0 32 m_model(0),
Chris@0 33 m_colour(Qt::black),
Chris@0 34 m_labelHeight(LabelTop)
Chris@0 35 {
Chris@44 36
Chris@0 37 }
Chris@0 38
Chris@0 39 void
Chris@0 40 TimeRulerLayer::setModel(Model *model)
Chris@0 41 {
Chris@0 42 if (m_model == model) return;
Chris@0 43 m_model = model;
Chris@0 44 emit modelReplaced();
Chris@0 45 }
Chris@0 46
Chris@0 47 void
Chris@0 48 TimeRulerLayer::setBaseColour(QColor colour)
Chris@0 49 {
Chris@0 50 if (m_colour == colour) return;
Chris@0 51 m_colour = colour;
Chris@0 52 emit layerParametersChanged();
Chris@0 53 }
Chris@0 54
Chris@0 55 Layer::PropertyList
Chris@0 56 TimeRulerLayer::getProperties() const
Chris@0 57 {
Chris@0 58 PropertyList list;
Chris@87 59 list.push_back("Colour");
Chris@0 60 return list;
Chris@0 61 }
Chris@0 62
Chris@87 63 QString
Chris@87 64 TimeRulerLayer::getPropertyLabel(const PropertyName &name) const
Chris@87 65 {
Chris@87 66 if (name == "Colour") return tr("Colour");
Chris@87 67 return "";
Chris@87 68 }
Chris@87 69
Chris@0 70 Layer::PropertyType
Chris@248 71 TimeRulerLayer::getPropertyType(const PropertyName &) const
Chris@0 72 {
Chris@0 73 return ValueProperty;
Chris@0 74 }
Chris@0 75
Chris@0 76 int
Chris@0 77 TimeRulerLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 78 int *min, int *max, int *deflt) const
Chris@0 79 {
Chris@216 80 int val = 0;
Chris@0 81
Chris@87 82 if (name == "Colour") {
Chris@0 83
Chris@10 84 if (min) *min = 0;
Chris@10 85 if (max) *max = 5;
Chris@216 86 if (deflt) *deflt = 0;
Chris@0 87
Chris@216 88 if (m_colour == Qt::black) val = 0;
Chris@216 89 else if (m_colour == Qt::darkRed) val = 1;
Chris@216 90 else if (m_colour == Qt::darkBlue) val = 2;
Chris@216 91 else if (m_colour == Qt::darkGreen) val = 3;
Chris@216 92 else if (m_colour == QColor(200, 50, 255)) val = 4;
Chris@216 93 else if (m_colour == QColor(255, 150, 50)) val = 5;
Chris@0 94
Chris@0 95 } else {
Chris@0 96
Chris@216 97 val = Layer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@0 98 }
Chris@0 99
Chris@216 100 return val;
Chris@0 101 }
Chris@0 102
Chris@0 103 QString
Chris@0 104 TimeRulerLayer::getPropertyValueLabel(const PropertyName &name,
Chris@0 105 int value) const
Chris@0 106 {
Chris@87 107 if (name == "Colour") {
Chris@0 108 switch (value) {
Chris@0 109 default:
Chris@0 110 case 0: return tr("Black");
Chris@0 111 case 1: return tr("Red");
Chris@0 112 case 2: return tr("Blue");
Chris@0 113 case 3: return tr("Green");
Chris@0 114 case 4: return tr("Purple");
Chris@0 115 case 5: return tr("Orange");
Chris@0 116 }
Chris@0 117 }
Chris@0 118 return tr("<unknown>");
Chris@0 119 }
Chris@0 120
Chris@0 121 void
Chris@0 122 TimeRulerLayer::setProperty(const PropertyName &name, int value)
Chris@0 123 {
Chris@87 124 if (name == "Colour") {
Chris@0 125 switch (value) {
Chris@0 126 default:
Chris@0 127 case 0: setBaseColour(Qt::black); break;
Chris@0 128 case 1: setBaseColour(Qt::darkRed); break;
Chris@0 129 case 2: setBaseColour(Qt::darkBlue); break;
Chris@0 130 case 3: setBaseColour(Qt::darkGreen); break;
Chris@0 131 case 4: setBaseColour(QColor(200, 50, 255)); break;
Chris@0 132 case 5: setBaseColour(QColor(255, 150, 50)); break;
Chris@0 133 }
Chris@0 134 }
Chris@0 135 }
Chris@0 136
Chris@271 137 bool
Chris@271 138 TimeRulerLayer::snapToFeatureFrame(View *v, int &frame,
Chris@271 139 size_t &resolution, SnapType snap) const
Chris@271 140 {
Chris@271 141 if (!m_model) {
Chris@271 142 resolution = 1;
Chris@271 143 return false;
Chris@271 144 }
Chris@271 145
Chris@271 146 bool q;
Chris@271 147 int tick = getMajorTickSpacing(v, q);
Chris@271 148 RealTime rtick = RealTime::fromMilliseconds(tick);
Chris@271 149 int rate = m_model->getSampleRate();
Chris@271 150
Chris@271 151 RealTime rt = RealTime::frame2RealTime(frame, rate);
Chris@271 152 double ratio = rt / rtick;
Chris@271 153
Chris@272 154 int rounded = int(ratio);
Chris@271 155 RealTime rdrt = rtick * rounded;
Chris@271 156
Chris@271 157 int left = RealTime::realTime2Frame(rdrt, rate);
Chris@271 158 resolution = RealTime::realTime2Frame(rtick, rate);
Chris@271 159 int right = left + resolution;
Chris@271 160
Chris@272 161 // std::cerr << "TimeRulerLayer::snapToFeatureFrame: type "
Chris@272 162 // << int(snap) << ", frame " << frame << " (time "
Chris@272 163 // << rt << ", tick " << rtick << ", rounded " << rdrt << ") ";
Chris@272 164
Chris@271 165 switch (snap) {
Chris@271 166
Chris@271 167 case SnapLeft:
Chris@271 168 frame = left;
Chris@271 169 break;
Chris@271 170
Chris@271 171 case SnapRight:
Chris@271 172 frame = right;
Chris@271 173 break;
Chris@271 174
Chris@271 175 case SnapNearest:
Chris@271 176 {
Chris@271 177 if (abs(frame - left) > abs(right - frame)) {
Chris@271 178 frame = right;
Chris@271 179 } else {
Chris@271 180 frame = left;
Chris@271 181 }
Chris@271 182 break;
Chris@271 183 }
Chris@271 184
Chris@271 185 case SnapNeighbouring:
Chris@271 186 {
Chris@271 187 int dl = -1, dr = -1;
Chris@271 188 int x = v->getXForFrame(frame);
Chris@271 189
Chris@271 190 if (left > v->getStartFrame() &&
Chris@271 191 left < v->getEndFrame()) {
Chris@271 192 dl = abs(v->getXForFrame(left) - x);
Chris@271 193 }
Chris@271 194
Chris@271 195 if (right > v->getStartFrame() &&
Chris@271 196 right < v->getEndFrame()) {
Chris@271 197 dr = abs(v->getXForFrame(right) - x);
Chris@271 198 }
Chris@271 199
Chris@271 200 int fuzz = 2;
Chris@271 201
Chris@271 202 if (dl >= 0 && dr >= 0) {
Chris@271 203 if (dl < dr) {
Chris@271 204 if (dl <= fuzz) {
Chris@271 205 frame = left;
Chris@271 206 }
Chris@271 207 } else {
Chris@271 208 if (dr < fuzz) {
Chris@271 209 frame = right;
Chris@271 210 }
Chris@271 211 }
Chris@271 212 } else if (dl >= 0) {
Chris@271 213 if (dl <= fuzz) {
Chris@271 214 frame = left;
Chris@271 215 }
Chris@271 216 } else if (dr >= 0) {
Chris@271 217 if (dr <= fuzz) {
Chris@271 218 frame = right;
Chris@271 219 }
Chris@271 220 }
Chris@271 221 }
Chris@271 222 }
Chris@271 223
Chris@272 224 // std::cerr << " -> " << frame << " (resolution = " << resolution << ")" << std::endl;
Chris@272 225
Chris@271 226 return true;
Chris@271 227 }
Chris@271 228
Chris@271 229 int
Chris@271 230 TimeRulerLayer::getMajorTickSpacing(View *v, bool &quarterTicks) const
Chris@271 231 {
Chris@271 232 // return value is in milliseconds
Chris@271 233
Chris@271 234 if (!m_model || !v) return 1000;
Chris@271 235
Chris@271 236 int sampleRate = m_model->getSampleRate();
Chris@271 237 if (!sampleRate) return 1000;
Chris@271 238
Chris@271 239 long startFrame = v->getStartFrame();
Chris@271 240 long endFrame = v->getEndFrame();
Chris@271 241
Chris@271 242 int minPixelSpacing = 50;
Chris@271 243
Chris@271 244 RealTime rtStart = RealTime::frame2RealTime(startFrame, sampleRate);
Chris@271 245 RealTime rtEnd = RealTime::frame2RealTime(endFrame, sampleRate);
Chris@271 246
Chris@271 247 int count = v->width() / minPixelSpacing;
Chris@271 248 if (count < 1) count = 1;
Chris@271 249 RealTime rtGap = (rtEnd - rtStart) / count;
Chris@271 250
Chris@271 251 int incms;
Chris@271 252 quarterTicks = false;
Chris@271 253
Chris@271 254 if (rtGap.sec > 0) {
Chris@271 255 incms = 1000;
Chris@271 256 int s = rtGap.sec;
Chris@271 257 if (s > 0) { incms *= 5; s /= 5; }
Chris@271 258 if (s > 0) { incms *= 2; s /= 2; }
Chris@271 259 if (s > 0) { incms *= 6; s /= 6; quarterTicks = true; }
Chris@271 260 if (s > 0) { incms *= 5; s /= 5; quarterTicks = false; }
Chris@271 261 if (s > 0) { incms *= 2; s /= 2; }
Chris@271 262 if (s > 0) { incms *= 6; s /= 6; quarterTicks = true; }
Chris@271 263 while (s > 0) {
Chris@271 264 incms *= 10;
Chris@271 265 s /= 10;
Chris@271 266 quarterTicks = false;
Chris@271 267 }
Chris@271 268 } else {
Chris@271 269 incms = 1;
Chris@271 270 int ms = rtGap.msec();
Chris@271 271 if (ms > 0) { incms *= 10; ms /= 10; }
Chris@271 272 if (ms > 0) { incms *= 10; ms /= 10; }
Chris@271 273 if (ms > 0) { incms *= 5; ms /= 5; }
Chris@271 274 if (ms > 0) { incms *= 2; ms /= 2; }
Chris@271 275 }
Chris@271 276
Chris@271 277 return incms;
Chris@271 278 }
Chris@271 279
Chris@0 280 void
Chris@44 281 TimeRulerLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 282 {
Chris@0 283 // std::cerr << "TimeRulerLayer::paint (" << rect.x() << "," << rect.y()
Chris@0 284 // << ") [" << rect.width() << "x" << rect.height() << "]" << std::endl;
Chris@0 285
Chris@0 286 if (!m_model || !m_model->isOK()) return;
Chris@0 287
Chris@0 288 int sampleRate = m_model->getSampleRate();
Chris@0 289 if (!sampleRate) return;
Chris@0 290
Chris@44 291 long startFrame = v->getStartFrame();
Chris@44 292 long endFrame = v->getEndFrame();
Chris@0 293
Chris@44 294 int zoomLevel = v->getZoomLevel();
Chris@0 295
Chris@0 296 long rectStart = startFrame + (rect.x() - 100) * zoomLevel;
Chris@0 297 long rectEnd = startFrame + (rect.x() + rect.width() + 100) * zoomLevel;
Chris@0 298
Chris@0 299 // std::cerr << "TimeRulerLayer::paint: calling paint.save()" << std::endl;
Chris@0 300 paint.save();
Chris@44 301 //!!! paint.setClipRect(v->rect());
Chris@0 302
Chris@0 303 int minPixelSpacing = 50;
Chris@0 304
Chris@0 305 bool quarter = false;
Chris@271 306 int incms = getMajorTickSpacing(v, quarter);
Chris@0 307
Chris@0 308 RealTime rt = RealTime::frame2RealTime(rectStart, sampleRate);
Chris@0 309 long ms = rt.sec * 1000 + rt.msec();
Chris@0 310 ms = (ms / incms) * incms - incms;
Chris@0 311
Chris@30 312 RealTime incRt = RealTime::fromMilliseconds(incms);
Chris@0 313 long incFrame = RealTime::realTime2Frame(incRt, sampleRate);
Chris@0 314 int incX = incFrame / zoomLevel;
Chris@0 315 int ticks = 10;
Chris@0 316 if (incX < minPixelSpacing * 2) {
Chris@0 317 ticks = quarter ? 4 : 5;
Chris@0 318 }
Chris@0 319
Chris@0 320 QRect oldClipRect = rect;
Chris@0 321 QRect newClipRect(oldClipRect.x() - 25, oldClipRect.y(),
Chris@0 322 oldClipRect.width() + 50, oldClipRect.height());
Chris@0 323 paint.setClipRect(newClipRect);
Chris@55 324 paint.setClipRect(rect);
Chris@0 325
Chris@0 326 QColor greyColour(m_colour);
Chris@0 327 if (m_colour == Qt::black) {
Chris@0 328 greyColour = QColor(200,200,200);
Chris@0 329 } else {
Chris@0 330 greyColour = m_colour.light(150);
Chris@0 331 }
Chris@0 332
Chris@0 333 while (1) {
Chris@0 334
Chris@30 335 rt = RealTime::fromMilliseconds(ms);
Chris@0 336 ms += incms;
Chris@0 337
Chris@0 338 long frame = RealTime::realTime2Frame(rt, sampleRate);
Chris@0 339 if (frame >= rectEnd) break;
Chris@0 340
Chris@0 341 int x = (frame - startFrame) / zoomLevel;
Chris@0 342 if (x < rect.x() || x >= rect.x() + rect.width()) continue;
Chris@0 343
Chris@0 344 paint.setPen(greyColour);
Chris@44 345 paint.drawLine(x, 0, x, v->height());
Chris@0 346
Chris@0 347 paint.setPen(m_colour);
Chris@0 348 paint.drawLine(x, 0, x, 5);
Chris@44 349 paint.drawLine(x, v->height() - 6, x, v->height() - 1);
Chris@0 350
Chris@0 351 QString text(QString::fromStdString(rt.toText()));
Chris@0 352
Chris@0 353 int y;
Chris@0 354 QFontMetrics metrics = paint.fontMetrics();
Chris@0 355 switch (m_labelHeight) {
Chris@0 356 default:
Chris@0 357 case LabelTop:
Chris@0 358 y = 6 + metrics.ascent();
Chris@0 359 break;
Chris@0 360 case LabelMiddle:
Chris@44 361 y = v->height() / 2 - metrics.height() / 2 + metrics.ascent();
Chris@0 362 break;
Chris@0 363 case LabelBottom:
Chris@44 364 y = v->height() - metrics.height() + metrics.ascent() - 6;
Chris@0 365 }
Chris@0 366
Chris@0 367 int tw = metrics.width(text);
Chris@0 368
Chris@70 369 if (v->getViewManager() && v->getViewManager()->getOverlayMode() !=
Chris@70 370 ViewManager::NoOverlays) {
Chris@70 371
Chris@70 372 if (v->getLayer(0) == this) {
Chris@70 373 // backmost layer, don't worry about outlining the text
Chris@70 374 paint.drawText(x+2 - tw/2, y, text);
Chris@70 375 } else {
Chris@70 376 v->drawVisibleText(paint, x+2 - tw/2, y, text, View::OutlinedText);
Chris@70 377 }
Chris@70 378 }
Chris@0 379
Chris@0 380 paint.setPen(greyColour);
Chris@0 381
Chris@0 382 for (int i = 1; i < ticks; ++i) {
Chris@0 383 rt = rt + (incRt / ticks);
Chris@0 384 frame = RealTime::realTime2Frame(rt, sampleRate);
Chris@0 385 x = (frame - startFrame) / zoomLevel;
Chris@0 386 int sz = 5;
Chris@0 387 if (ticks == 10) {
Chris@0 388 if ((i % 2) == 1) {
Chris@0 389 if (i == 5) {
Chris@44 390 paint.drawLine(x, 0, x, v->height());
Chris@0 391 } else sz = 3;
Chris@0 392 } else {
Chris@0 393 sz = 7;
Chris@0 394 }
Chris@0 395 }
Chris@0 396 paint.drawLine(x, 0, x, sz);
Chris@44 397 paint.drawLine(x, v->height() - sz - 1, x, v->height() - 1);
Chris@0 398 }
Chris@0 399 }
Chris@0 400
Chris@0 401 paint.restore();
Chris@0 402 }
Chris@0 403
Chris@6 404 QString
Chris@6 405 TimeRulerLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@6 406 {
Chris@6 407 return Layer::toXmlString(indent, extraAttributes +
Chris@6 408 QString(" colour=\"%1\"").arg(encodeColour(m_colour)));
Chris@6 409 }
Chris@6 410
Chris@11 411 void
Chris@11 412 TimeRulerLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 413 {
Chris@11 414 QString colourSpec = attributes.value("colour");
Chris@11 415 if (colourSpec != "") {
Chris@11 416 QColor colour(colourSpec);
Chris@11 417 if (colour.isValid()) {
Chris@11 418 setBaseColour(QColor(colourSpec));
Chris@11 419 }
Chris@11 420 }
Chris@11 421 }
Chris@11 422