annotate layer/TimeInstantLayer.cpp @ 25:dcdb21b62dbb

* Refactor sparse models. Previously the 1D and time-value models duplicated a lot of code; now there is a base class (SparseModel) templated on the stored point type, and the subclasses define point types with the necessary characteristics. * Add NoteModel, a new SparseModel subclass. * Reorganise local feature description display. Instead of asking the layer to draw its own, just query it for a textual description and draw that in Pane. Greatly simplifies this part of the layer code. * Add local feature descriptions to colour 3D plot and waveform layers. * Add pitch in MIDI-pitch-and-cents to spectrogram layer. * Give AudioGenerator its own mutex to shorten lock times in CallbackPlaySource. * Minor adjustments to layers menu &c
author Chris Cannam
date Thu, 02 Feb 2006 16:10:19 +0000
parents ca57f70b0e48
children 202d1dca67d2
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 A waveform viewer and audio annotation editor.
Chris@5 5 Chris Cannam, Queen Mary University of London, 2005-2006
Chris@0 6
Chris@0 7 This is experimental software. Not for distribution.
Chris@0 8 */
Chris@0 9
Chris@0 10 #include "TimeInstantLayer.h"
Chris@0 11
Chris@0 12 #include "base/Model.h"
Chris@0 13 #include "base/RealTime.h"
Chris@0 14 #include "base/View.h"
Chris@0 15 #include "base/Profiler.h"
Chris@0 16
Chris@0 17 #include "model/SparseOneDimensionalModel.h"
Chris@0 18
Chris@0 19 #include <QPainter>
Chris@17 20 #include <QMouseEvent>
Chris@0 21
Chris@0 22 #include <iostream>
Chris@0 23
Chris@0 24 TimeInstantLayer::TimeInstantLayer(View *w) :
Chris@0 25 Layer(w),
Chris@0 26 m_model(0),
Chris@18 27 m_editing(false),
Chris@17 28 m_editingPoint(0, tr("New Point")),
Chris@22 29 m_editingCommand(0),
Chris@0 30 m_colour(QColor(200, 50, 255))
Chris@0 31 {
Chris@0 32 m_view->addLayer(this);
Chris@0 33 }
Chris@0 34
Chris@0 35 void
Chris@0 36 TimeInstantLayer::setModel(SparseOneDimensionalModel *model)
Chris@0 37 {
Chris@0 38 if (m_model == model) return;
Chris@0 39 m_model = model;
Chris@0 40
Chris@0 41 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
Chris@0 42 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@0 43 this, SIGNAL(modelChanged(size_t, size_t)));
Chris@0 44
Chris@0 45 connect(m_model, SIGNAL(completionChanged()),
Chris@0 46 this, SIGNAL(modelCompletionChanged()));
Chris@0 47
Chris@0 48 std::cerr << "TimeInstantLayer::setModel(" << model << ")" << std::endl;
Chris@0 49
Chris@0 50 emit modelReplaced();
Chris@0 51 }
Chris@0 52
Chris@0 53 Layer::PropertyList
Chris@0 54 TimeInstantLayer::getProperties() const
Chris@0 55 {
Chris@0 56 PropertyList list;
Chris@0 57 list.push_back(tr("Colour"));
Chris@0 58 return list;
Chris@0 59 }
Chris@0 60
Chris@0 61 Layer::PropertyType
Chris@20 62 TimeInstantLayer::getPropertyType(const PropertyName &) const
Chris@0 63 {
Chris@0 64 return ValueProperty;
Chris@0 65 }
Chris@0 66
Chris@0 67 int
Chris@0 68 TimeInstantLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@0 69 int *min, int *max) const
Chris@0 70 {
Chris@0 71 int deft = 0;
Chris@0 72
Chris@0 73 if (name == tr("Colour")) {
Chris@0 74
Chris@10 75 if (min) *min = 0;
Chris@10 76 if (max) *max = 5;
Chris@0 77
Chris@0 78 if (m_colour == Qt::black) deft = 0;
Chris@0 79 else if (m_colour == Qt::darkRed) deft = 1;
Chris@0 80 else if (m_colour == Qt::darkBlue) deft = 2;
Chris@0 81 else if (m_colour == Qt::darkGreen) deft = 3;
Chris@0 82 else if (m_colour == QColor(200, 50, 255)) deft = 4;
Chris@0 83 else if (m_colour == QColor(255, 150, 50)) deft = 5;
Chris@0 84
Chris@0 85 } else {
Chris@0 86
Chris@0 87 deft = Layer::getPropertyRangeAndValue(name, min, max);
Chris@0 88 }
Chris@0 89
Chris@0 90 return deft;
Chris@0 91 }
Chris@0 92
Chris@0 93 QString
Chris@0 94 TimeInstantLayer::getPropertyValueLabel(const PropertyName &name,
Chris@0 95 int value) const
Chris@0 96 {
Chris@0 97 if (name == tr("Colour")) {
Chris@0 98 switch (value) {
Chris@0 99 default:
Chris@0 100 case 0: return tr("Black");
Chris@0 101 case 1: return tr("Red");
Chris@0 102 case 2: return tr("Blue");
Chris@0 103 case 3: return tr("Green");
Chris@0 104 case 4: return tr("Purple");
Chris@0 105 case 5: return tr("Orange");
Chris@0 106 }
Chris@0 107 }
Chris@0 108 return tr("<unknown>");
Chris@0 109 }
Chris@0 110
Chris@0 111 void
Chris@0 112 TimeInstantLayer::setProperty(const PropertyName &name, int value)
Chris@0 113 {
Chris@0 114 if (name == tr("Colour")) {
Chris@0 115 switch (value) {
Chris@0 116 default:
Chris@0 117 case 0: setBaseColour(Qt::black); break;
Chris@0 118 case 1: setBaseColour(Qt::darkRed); break;
Chris@0 119 case 2: setBaseColour(Qt::darkBlue); break;
Chris@0 120 case 3: setBaseColour(Qt::darkGreen); break;
Chris@0 121 case 4: setBaseColour(QColor(200, 50, 255)); break;
Chris@0 122 case 5: setBaseColour(QColor(255, 150, 50)); break;
Chris@0 123 }
Chris@0 124 }
Chris@0 125 }
Chris@0 126
Chris@0 127 void
Chris@0 128 TimeInstantLayer::setBaseColour(QColor colour)
Chris@0 129 {
Chris@0 130 if (m_colour == colour) return;
Chris@0 131 m_colour = colour;
Chris@0 132 emit layerParametersChanged();
Chris@0 133 }
Chris@0 134
Chris@0 135 bool
Chris@0 136 TimeInstantLayer::isLayerScrollable() const
Chris@0 137 {
Chris@0 138 QPoint discard;
Chris@0 139 return !m_view->shouldIlluminateLocalFeatures(this, discard);
Chris@0 140 }
Chris@0 141
Chris@0 142 SparseOneDimensionalModel::PointList
Chris@0 143 TimeInstantLayer::getLocalPoints(int x) const
Chris@0 144 {
Chris@0 145 if (!m_model) return SparseOneDimensionalModel::PointList();
Chris@0 146
Chris@20 147 long frame = getFrameForX(x);
Chris@0 148
Chris@0 149 SparseOneDimensionalModel::PointList onPoints =
Chris@0 150 m_model->getPoints(frame);
Chris@0 151
Chris@0 152 if (!onPoints.empty()) {
Chris@0 153 return onPoints;
Chris@0 154 }
Chris@0 155
Chris@0 156 SparseOneDimensionalModel::PointList prevPoints =
Chris@0 157 m_model->getPreviousPoints(frame);
Chris@0 158 SparseOneDimensionalModel::PointList nextPoints =
Chris@0 159 m_model->getNextPoints(frame);
Chris@0 160
Chris@0 161 SparseOneDimensionalModel::PointList usePoints = prevPoints;
Chris@0 162
Chris@0 163 if (prevPoints.empty()) {
Chris@0 164 usePoints = nextPoints;
Chris@20 165 } else if (prevPoints.begin()->frame < m_view->getStartFrame() &&
Chris@20 166 !(nextPoints.begin()->frame > m_view->getEndFrame())) {
Chris@0 167 usePoints = nextPoints;
Chris@0 168 } else if (nextPoints.begin()->frame - frame <
Chris@0 169 frame - prevPoints.begin()->frame) {
Chris@0 170 usePoints = nextPoints;
Chris@0 171 }
Chris@0 172
Chris@0 173 return usePoints;
Chris@0 174 }
Chris@0 175
Chris@25 176 QString
Chris@25 177 TimeInstantLayer::getFeatureDescription(QPoint &pos) const
Chris@0 178 {
Chris@25 179 int x = pos.x();
Chris@0 180
Chris@25 181 if (!m_model || !m_model->getSampleRate()) return "";
Chris@0 182
Chris@0 183 SparseOneDimensionalModel::PointList points = getLocalPoints(x);
Chris@0 184
Chris@0 185 if (points.empty()) {
Chris@0 186 if (!m_model->isReady()) {
Chris@25 187 return tr("In progress");
Chris@25 188 } else {
Chris@25 189 return tr("No local points");
Chris@0 190 }
Chris@0 191 }
Chris@0 192
Chris@0 193 long useFrame = points.begin()->frame;
Chris@0 194
Chris@0 195 RealTime rt = RealTime::frame2RealTime(useFrame, m_model->getSampleRate());
Chris@25 196
Chris@25 197 QString text;
Chris@0 198
Chris@25 199 if (points.begin()->label == "") {
Chris@25 200 text = QString(tr("Time:\t%1\nNo label"))
Chris@25 201 .arg(rt.toText(true).c_str());
Chris@25 202 } else {
Chris@25 203 text = QString(tr("Time:\t%1\nLabel:\t%2"))
Chris@25 204 .arg(rt.toText(true).c_str())
Chris@25 205 .arg(points.begin()->label);
Chris@25 206 }
Chris@0 207
Chris@25 208 pos = QPoint(getXForFrame(useFrame), pos.y());
Chris@25 209 return text;
Chris@0 210 }
Chris@0 211
Chris@13 212 int
Chris@13 213 TimeInstantLayer::getNearestFeatureFrame(int frame,
Chris@13 214 size_t &resolution,
Chris@13 215 bool snapRight) const
Chris@13 216 {
Chris@13 217 if (!m_model) {
Chris@13 218 return Layer::getNearestFeatureFrame(frame, resolution, snapRight);
Chris@13 219 }
Chris@13 220
Chris@13 221 resolution = m_model->getResolution();
Chris@13 222 SparseOneDimensionalModel::PointList points(m_model->getPoints(frame, frame));
Chris@13 223
Chris@13 224 int returnFrame = frame;
Chris@13 225
Chris@13 226 for (SparseOneDimensionalModel::PointList::const_iterator i = points.begin();
Chris@13 227 i != points.end(); ++i) {
Chris@13 228
Chris@13 229 if (snapRight) {
Chris@13 230 if (i->frame > frame) {
Chris@13 231 returnFrame = i->frame;
Chris@13 232 break;
Chris@13 233 }
Chris@13 234 } else {
Chris@13 235 if (i->frame <= frame) {
Chris@13 236 returnFrame = i->frame;
Chris@13 237 }
Chris@13 238 }
Chris@13 239 }
Chris@13 240
Chris@13 241 return returnFrame;
Chris@13 242 }
Chris@13 243
Chris@0 244 void
Chris@0 245 TimeInstantLayer::paint(QPainter &paint, QRect rect) const
Chris@0 246 {
Chris@0 247 if (!m_model || !m_model->isOK()) return;
Chris@0 248
Chris@0 249 // Profiler profiler("TimeInstantLayer::paint", true);
Chris@0 250
Chris@20 251 int x0 = rect.left(), x1 = rect.right();
Chris@0 252
Chris@20 253 long frame0 = getFrameForX(x0);
Chris@20 254 long frame1 = getFrameForX(x1);
Chris@0 255
Chris@0 256 SparseOneDimensionalModel::PointList points(m_model->getPoints
Chris@0 257 (frame0, frame1));
Chris@0 258
Chris@0 259 paint.setPen(m_colour);
Chris@0 260
Chris@0 261 QColor brushColour(m_colour);
Chris@0 262 brushColour.setAlpha(100);
Chris@0 263 paint.setBrush(brushColour);
Chris@0 264
Chris@0 265 // std::cerr << "TimeInstantLayer::paint: resolution is "
Chris@0 266 // << m_model->getResolution() << " frames" << std::endl;
Chris@0 267
Chris@0 268 QPoint localPos;
Chris@0 269 long illuminateFrame = -1;
Chris@0 270
Chris@0 271 if (m_view->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@0 272 SparseOneDimensionalModel::PointList localPoints =
Chris@0 273 getLocalPoints(localPos.x());
Chris@0 274 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
Chris@0 275 }
Chris@0 276
Chris@23 277 int prevX = -1;
Chris@23 278
Chris@0 279 for (SparseOneDimensionalModel::PointList::const_iterator i = points.begin();
Chris@0 280 i != points.end(); ++i) {
Chris@0 281
Chris@0 282 const SparseOneDimensionalModel::Point &p(*i);
Chris@17 283 SparseOneDimensionalModel::PointList::const_iterator j = i;
Chris@17 284 ++j;
Chris@0 285
Chris@20 286 int x = getXForFrame(p.frame);
Chris@23 287 if (x == prevX && p.frame != illuminateFrame) continue;
Chris@23 288
Chris@20 289 int iw = getXForFrame(p.frame + m_model->getResolution()) - x;
Chris@16 290 if (iw < 2) {
Chris@17 291 if (iw < 1) {
Chris@17 292 iw = 2;
Chris@17 293 if (j != points.end()) {
Chris@20 294 int nx = getXForFrame(j->frame);
Chris@17 295 if (nx < x + 3) iw = 1;
Chris@17 296 }
Chris@17 297 } else {
Chris@17 298 iw = 2;
Chris@17 299 }
Chris@16 300 }
Chris@20 301
Chris@0 302 if (p.frame == illuminateFrame) {
Chris@0 303 paint.setPen(Qt::black); //!!!
Chris@0 304 } else {
Chris@0 305 paint.setPen(brushColour);
Chris@0 306 }
Chris@23 307
Chris@23 308 if (iw > 1) {
Chris@23 309 paint.drawRect(x, 0, iw - 1, m_view->height() - 1);
Chris@23 310 } else {
Chris@23 311 paint.drawLine(x, 0, x, m_view->height() - 1);
Chris@23 312 }
Chris@0 313 paint.setPen(m_colour);
Chris@0 314
Chris@0 315 if (p.label != "") {
Chris@0 316
Chris@0 317 // only draw if there's enough room from here to the next point
Chris@0 318
Chris@0 319 int lw = paint.fontMetrics().width(p.label);
Chris@0 320 bool good = true;
Chris@0 321
Chris@17 322 if (j != points.end()) {
Chris@20 323 int nx = getXForFrame(j->frame);
Chris@20 324 if (nx >= x && nx - x - iw - 3 <= lw) good = false;
Chris@0 325 }
Chris@0 326
Chris@0 327 if (good) {
Chris@20 328 paint.drawText(x + iw + 2,
Chris@0 329 m_view->height() - paint.fontMetrics().height(),
Chris@0 330 p.label);
Chris@0 331 }
Chris@0 332 }
Chris@23 333
Chris@23 334 prevX = x;
Chris@0 335 }
Chris@0 336 }
Chris@0 337
Chris@17 338 void
Chris@17 339 TimeInstantLayer::drawStart(QMouseEvent *e)
Chris@17 340 {
Chris@17 341 std::cerr << "TimeInstantLayer::drawStart(" << e->x() << ")" << std::endl;
Chris@17 342
Chris@17 343 if (!m_model) return;
Chris@17 344
Chris@20 345 long frame = getFrameForX(e->x());
Chris@17 346 if (frame < 0) frame = 0;
Chris@21 347 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@22 348
Chris@17 349 m_editingPoint = SparseOneDimensionalModel::Point(frame, tr("New Point"));
Chris@22 350
Chris@22 351 if (m_editingCommand) m_editingCommand->finish();
Chris@22 352 m_editingCommand = new SparseOneDimensionalModel::EditCommand(m_model,
Chris@22 353 tr("Draw Point"));
Chris@22 354 m_editingCommand->addPoint(m_editingPoint);
Chris@22 355
Chris@18 356 m_editing = true;
Chris@17 357 }
Chris@17 358
Chris@17 359 void
Chris@17 360 TimeInstantLayer::drawDrag(QMouseEvent *e)
Chris@17 361 {
Chris@17 362 std::cerr << "TimeInstantLayer::drawDrag(" << e->x() << ")" << std::endl;
Chris@17 363
Chris@18 364 if (!m_model || !m_editing) return;
Chris@17 365
Chris@20 366 long frame = getFrameForX(e->x());
Chris@17 367 if (frame < 0) frame = 0;
Chris@21 368 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@22 369 m_editingCommand->deletePoint(m_editingPoint);
Chris@17 370 m_editingPoint.frame = frame;
Chris@22 371 m_editingCommand->addPoint(m_editingPoint);
Chris@17 372 }
Chris@17 373
Chris@17 374 void
Chris@17 375 TimeInstantLayer::drawEnd(QMouseEvent *e)
Chris@17 376 {
Chris@17 377 std::cerr << "TimeInstantLayer::drawEnd(" << e->x() << ")" << std::endl;
Chris@18 378 if (!m_model || !m_editing) return;
Chris@23 379 QString newName = tr("Add Point at %1 s")
Chris@23 380 .arg(RealTime::frame2RealTime(m_editingPoint.frame,
Chris@23 381 m_model->getSampleRate())
Chris@23 382 .toText(false).c_str());
Chris@23 383 m_editingCommand->setName(newName);
Chris@22 384 m_editingCommand->finish();
Chris@22 385 m_editingCommand = 0;
Chris@18 386 m_editing = false;
Chris@18 387 }
Chris@18 388
Chris@18 389 void
Chris@18 390 TimeInstantLayer::editStart(QMouseEvent *e)
Chris@18 391 {
Chris@18 392 std::cerr << "TimeInstantLayer::editStart(" << e->x() << ")" << std::endl;
Chris@18 393
Chris@17 394 if (!m_model) return;
Chris@18 395
Chris@18 396 SparseOneDimensionalModel::PointList points = getLocalPoints(e->x());
Chris@18 397 if (points.empty()) return;
Chris@18 398
Chris@18 399 m_editingPoint = *points.begin();
Chris@22 400
Chris@22 401 if (m_editingCommand) {
Chris@22 402 m_editingCommand->finish();
Chris@22 403 m_editingCommand = 0;
Chris@22 404 }
Chris@22 405
Chris@18 406 m_editing = true;
Chris@18 407 }
Chris@18 408
Chris@18 409 void
Chris@18 410 TimeInstantLayer::editDrag(QMouseEvent *e)
Chris@18 411 {
Chris@18 412 std::cerr << "TimeInstantLayer::editDrag(" << e->x() << ")" << std::endl;
Chris@18 413
Chris@18 414 if (!m_model || !m_editing) return;
Chris@18 415
Chris@20 416 long frame = getFrameForX(e->x());
Chris@18 417 if (frame < 0) frame = 0;
Chris@21 418 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@22 419
Chris@22 420 if (!m_editingCommand) {
Chris@22 421 m_editingCommand = new SparseOneDimensionalModel::EditCommand(m_model,
Chris@22 422 tr("Drag Point"));
Chris@22 423 }
Chris@22 424
Chris@22 425 m_editingCommand->deletePoint(m_editingPoint);
Chris@18 426 m_editingPoint.frame = frame;
Chris@22 427 m_editingCommand->addPoint(m_editingPoint);
Chris@18 428 }
Chris@18 429
Chris@18 430 void
Chris@18 431 TimeInstantLayer::editEnd(QMouseEvent *e)
Chris@18 432 {
Chris@18 433 std::cerr << "TimeInstantLayer::editEnd(" << e->x() << ")" << std::endl;
Chris@18 434 if (!m_model || !m_editing) return;
Chris@23 435 if (m_editingCommand) {
Chris@23 436 QString newName = tr("Move Point to %1 s")
Chris@23 437 .arg(RealTime::frame2RealTime(m_editingPoint.frame,
Chris@23 438 m_model->getSampleRate())
Chris@23 439 .toText(false).c_str());
Chris@23 440 m_editingCommand->setName(newName);
Chris@23 441 m_editingCommand->finish();
Chris@23 442 }
Chris@22 443 m_editingCommand = 0;
Chris@18 444 m_editing = false;
Chris@17 445 }
Chris@17 446
Chris@6 447 QString
Chris@6 448 TimeInstantLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@6 449 {
Chris@6 450 return Layer::toXmlString(indent, extraAttributes +
Chris@6 451 QString(" colour=\"%1\"").arg(encodeColour(m_colour)));
Chris@6 452 }
Chris@0 453
Chris@11 454 void
Chris@11 455 TimeInstantLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 456 {
Chris@11 457 QString colourSpec = attributes.value("colour");
Chris@11 458 if (colourSpec != "") {
Chris@11 459 QColor colour(colourSpec);
Chris@11 460 if (colour.isValid()) {
Chris@11 461 setBaseColour(QColor(colourSpec));
Chris@11 462 }
Chris@11 463 }
Chris@11 464 }
Chris@11 465
Chris@0 466 #ifdef INCLUDE_MOCFILES
Chris@0 467 #include "TimeInstantLayer.moc.cpp"
Chris@0 468 #endif
Chris@0 469