annotate layer/NoteLayer.cpp @ 197:6b023411087b

* Work on harmonising colour and scale ranges between types of layer * Add normalize options to colour 3d plot layer
author Chris Cannam
date Thu, 01 Feb 2007 14:31:28 +0000
parents 33929e0c3c6b
children c2ed5014d4ff
rev   line source
Chris@58 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@30 2
Chris@30 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@30 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@30 14 */
Chris@30 15
Chris@30 16 #include "NoteLayer.h"
Chris@30 17
Chris@128 18 #include "data/model/Model.h"
Chris@30 19 #include "base/RealTime.h"
Chris@30 20 #include "base/Profiler.h"
Chris@30 21 #include "base/Pitch.h"
Chris@197 22 #include "base/LogRange.h"
Chris@128 23 #include "view/View.h"
Chris@30 24
Chris@128 25 #include "data/model/NoteModel.h"
Chris@30 26
Chris@70 27 #include "widgets/ItemEditDialog.h"
Chris@70 28
Chris@42 29 #include "SpectrogramLayer.h" // for optional frequency alignment
Chris@42 30
Chris@30 31 #include <QPainter>
Chris@30 32 #include <QPainterPath>
Chris@30 33 #include <QMouseEvent>
Chris@30 34
Chris@30 35 #include <iostream>
Chris@30 36 #include <cmath>
Chris@30 37
Chris@44 38 NoteLayer::NoteLayer() :
Chris@44 39 Layer(),
Chris@30 40 m_model(0),
Chris@30 41 m_editing(false),
Chris@30 42 m_originalPoint(0, 0.0, 0, tr("New Point")),
Chris@30 43 m_editingPoint(0, 0.0, 0, tr("New Point")),
Chris@30 44 m_editingCommand(0),
Chris@30 45 m_colour(Qt::black),
Chris@101 46 m_verticalScale(AutoAlignScale)
Chris@30 47 {
Chris@44 48
Chris@30 49 }
Chris@30 50
Chris@30 51 void
Chris@30 52 NoteLayer::setModel(NoteModel *model)
Chris@30 53 {
Chris@30 54 if (m_model == model) return;
Chris@30 55 m_model = model;
Chris@30 56
Chris@30 57 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
Chris@30 58 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@30 59 this, SIGNAL(modelChanged(size_t, size_t)));
Chris@30 60
Chris@30 61 connect(m_model, SIGNAL(completionChanged()),
Chris@30 62 this, SIGNAL(modelCompletionChanged()));
Chris@30 63
Chris@101 64 // std::cerr << "NoteLayer::setModel(" << model << ")" << std::endl;
Chris@30 65
Chris@30 66 emit modelReplaced();
Chris@30 67 }
Chris@30 68
Chris@30 69 Layer::PropertyList
Chris@30 70 NoteLayer::getProperties() const
Chris@30 71 {
Chris@30 72 PropertyList list;
Chris@87 73 list.push_back("Colour");
Chris@87 74 list.push_back("Vertical Scale");
Chris@100 75 list.push_back("Scale Units");
Chris@30 76 return list;
Chris@30 77 }
Chris@30 78
Chris@87 79 QString
Chris@87 80 NoteLayer::getPropertyLabel(const PropertyName &name) const
Chris@87 81 {
Chris@87 82 if (name == "Colour") return tr("Colour");
Chris@87 83 if (name == "Vertical Scale") return tr("Vertical Scale");
Chris@116 84 if (name == "Scale Units") return tr("Scale Units");
Chris@87 85 return "";
Chris@87 86 }
Chris@87 87
Chris@30 88 Layer::PropertyType
Chris@100 89 NoteLayer::getPropertyType(const PropertyName &name) const
Chris@30 90 {
Chris@100 91 if (name == "Scale Units") return UnitsProperty;
Chris@30 92 return ValueProperty;
Chris@30 93 }
Chris@30 94
Chris@30 95 int
Chris@30 96 NoteLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@30 97 int *min, int *max) const
Chris@30 98 {
Chris@30 99 //!!! factor this colour handling stuff out into a colour manager class
Chris@30 100
Chris@30 101 int deft = 0;
Chris@30 102
Chris@87 103 if (name == "Colour") {
Chris@30 104
Chris@30 105 if (min) *min = 0;
Chris@30 106 if (max) *max = 5;
Chris@30 107
Chris@30 108 if (m_colour == Qt::black) deft = 0;
Chris@30 109 else if (m_colour == Qt::darkRed) deft = 1;
Chris@30 110 else if (m_colour == Qt::darkBlue) deft = 2;
Chris@30 111 else if (m_colour == Qt::darkGreen) deft = 3;
Chris@30 112 else if (m_colour == QColor(200, 50, 255)) deft = 4;
Chris@30 113 else if (m_colour == QColor(255, 150, 50)) deft = 5;
Chris@30 114
Chris@87 115 } else if (name == "Vertical Scale") {
Chris@30 116
Chris@30 117 if (min) *min = 0;
Chris@101 118 if (max) *max = 3;
Chris@30 119
Chris@30 120 deft = int(m_verticalScale);
Chris@30 121
Chris@100 122 } else if (name == "Scale Units") {
Chris@100 123
Chris@100 124 if (m_model) {
Chris@100 125 deft = UnitDatabase::getInstance()->getUnitId
Chris@100 126 (m_model->getScaleUnits());
Chris@100 127 }
Chris@100 128
Chris@30 129 } else {
Chris@30 130
Chris@30 131 deft = Layer::getPropertyRangeAndValue(name, min, max);
Chris@30 132 }
Chris@30 133
Chris@30 134 return deft;
Chris@30 135 }
Chris@30 136
Chris@30 137 QString
Chris@30 138 NoteLayer::getPropertyValueLabel(const PropertyName &name,
Chris@30 139 int value) const
Chris@30 140 {
Chris@87 141 if (name == "Colour") {
Chris@30 142 switch (value) {
Chris@30 143 default:
Chris@30 144 case 0: return tr("Black");
Chris@30 145 case 1: return tr("Red");
Chris@30 146 case 2: return tr("Blue");
Chris@30 147 case 3: return tr("Green");
Chris@30 148 case 4: return tr("Purple");
Chris@30 149 case 5: return tr("Orange");
Chris@30 150 }
Chris@87 151 } else if (name == "Vertical Scale") {
Chris@30 152 switch (value) {
Chris@30 153 default:
Chris@101 154 case 0: return tr("Auto-Align");
Chris@101 155 case 1: return tr("Linear Scale");
Chris@101 156 case 2: return tr("Log Scale");
Chris@101 157 case 3: return tr("MIDI Note Range");
Chris@30 158 }
Chris@30 159 }
Chris@30 160 return tr("<unknown>");
Chris@30 161 }
Chris@30 162
Chris@30 163 void
Chris@30 164 NoteLayer::setProperty(const PropertyName &name, int value)
Chris@30 165 {
Chris@87 166 if (name == "Colour") {
Chris@30 167 switch (value) {
Chris@30 168 default:
Chris@30 169 case 0: setBaseColour(Qt::black); break;
Chris@30 170 case 1: setBaseColour(Qt::darkRed); break;
Chris@30 171 case 2: setBaseColour(Qt::darkBlue); break;
Chris@30 172 case 3: setBaseColour(Qt::darkGreen); break;
Chris@30 173 case 4: setBaseColour(QColor(200, 50, 255)); break;
Chris@30 174 case 5: setBaseColour(QColor(255, 150, 50)); break;
Chris@30 175 }
Chris@87 176 } else if (name == "Vertical Scale") {
Chris@30 177 setVerticalScale(VerticalScale(value));
Chris@100 178 } else if (name == "Scale Units") {
Chris@100 179 if (m_model) {
Chris@100 180 m_model->setScaleUnits
Chris@100 181 (UnitDatabase::getInstance()->getUnitById(value));
Chris@100 182 emit modelChanged();
Chris@100 183 }
Chris@30 184 }
Chris@30 185 }
Chris@30 186
Chris@30 187 void
Chris@30 188 NoteLayer::setBaseColour(QColor colour)
Chris@30 189 {
Chris@30 190 if (m_colour == colour) return;
Chris@30 191 m_colour = colour;
Chris@30 192 emit layerParametersChanged();
Chris@30 193 }
Chris@30 194
Chris@30 195 void
Chris@30 196 NoteLayer::setVerticalScale(VerticalScale scale)
Chris@30 197 {
Chris@30 198 if (m_verticalScale == scale) return;
Chris@30 199 m_verticalScale = scale;
Chris@30 200 emit layerParametersChanged();
Chris@30 201 }
Chris@30 202
Chris@30 203 bool
Chris@44 204 NoteLayer::isLayerScrollable(const View *v) const
Chris@30 205 {
Chris@30 206 QPoint discard;
Chris@44 207 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@30 208 }
Chris@30 209
Chris@79 210 bool
Chris@101 211 NoteLayer::shouldConvertMIDIToHz() const
Chris@101 212 {
Chris@101 213 QString unit = m_model->getScaleUnits();
Chris@101 214 return (unit != "Hz");
Chris@101 215 // if (unit == "" ||
Chris@101 216 // unit.startsWith("MIDI") ||
Chris@101 217 // unit.startsWith("midi")) return true;
Chris@101 218 // return false;
Chris@101 219 }
Chris@101 220
Chris@101 221 bool
Chris@101 222 NoteLayer::getValueExtents(float &min, float &max,
Chris@101 223 bool &logarithmic, QString &unit) const
Chris@79 224 {
Chris@79 225 if (!m_model) return false;
Chris@79 226 min = m_model->getValueMinimum();
Chris@79 227 max = m_model->getValueMaximum();
Chris@101 228
Chris@105 229 if (shouldConvertMIDIToHz()) {
Chris@105 230 unit = "Hz";
Chris@105 231 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@105 232 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@105 233 } else unit = m_model->getScaleUnits();
Chris@101 234
Chris@101 235 if (m_verticalScale == MIDIRangeScale ||
Chris@101 236 m_verticalScale == LogScale) logarithmic = true;
Chris@101 237
Chris@101 238 return true;
Chris@101 239 }
Chris@101 240
Chris@101 241 bool
Chris@101 242 NoteLayer::getDisplayExtents(float &min, float &max) const
Chris@101 243 {
Chris@101 244 if (!m_model || m_verticalScale == AutoAlignScale) return false;
Chris@101 245
Chris@101 246 if (m_verticalScale == MIDIRangeScale) {
Chris@101 247 min = Pitch::getFrequencyForPitch(0);
Chris@101 248 max = Pitch::getFrequencyForPitch(127);
Chris@101 249 return true;
Chris@101 250 }
Chris@101 251
Chris@101 252 min = m_model->getValueMinimum();
Chris@101 253 max = m_model->getValueMaximum();
Chris@101 254
Chris@101 255 if (shouldConvertMIDIToHz()) {
Chris@101 256 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@101 257 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@101 258 }
Chris@101 259
Chris@79 260 return true;
Chris@79 261 }
Chris@79 262
Chris@30 263 NoteModel::PointList
Chris@44 264 NoteLayer::getLocalPoints(View *v, int x) const
Chris@30 265 {
Chris@30 266 if (!m_model) return NoteModel::PointList();
Chris@30 267
Chris@44 268 long frame = v->getFrameForX(x);
Chris@30 269
Chris@30 270 NoteModel::PointList onPoints =
Chris@30 271 m_model->getPoints(frame);
Chris@30 272
Chris@30 273 if (!onPoints.empty()) {
Chris@30 274 return onPoints;
Chris@30 275 }
Chris@30 276
Chris@30 277 NoteModel::PointList prevPoints =
Chris@30 278 m_model->getPreviousPoints(frame);
Chris@30 279 NoteModel::PointList nextPoints =
Chris@30 280 m_model->getNextPoints(frame);
Chris@30 281
Chris@30 282 NoteModel::PointList usePoints = prevPoints;
Chris@30 283
Chris@30 284 if (prevPoints.empty()) {
Chris@30 285 usePoints = nextPoints;
Chris@44 286 } else if (prevPoints.begin()->frame < v->getStartFrame() &&
Chris@44 287 !(nextPoints.begin()->frame > v->getEndFrame())) {
Chris@30 288 usePoints = nextPoints;
Chris@30 289 } else if (nextPoints.begin()->frame - frame <
Chris@30 290 frame - prevPoints.begin()->frame) {
Chris@30 291 usePoints = nextPoints;
Chris@30 292 }
Chris@30 293
Chris@30 294 if (!usePoints.empty()) {
Chris@30 295 int fuzz = 2;
Chris@44 296 int px = v->getXForFrame(usePoints.begin()->frame);
Chris@30 297 if ((px > x && px - x > fuzz) ||
Chris@30 298 (px < x && x - px > fuzz + 1)) {
Chris@30 299 usePoints.clear();
Chris@30 300 }
Chris@30 301 }
Chris@30 302
Chris@30 303 return usePoints;
Chris@30 304 }
Chris@30 305
Chris@30 306 QString
Chris@44 307 NoteLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@30 308 {
Chris@30 309 int x = pos.x();
Chris@30 310
Chris@30 311 if (!m_model || !m_model->getSampleRate()) return "";
Chris@30 312
Chris@44 313 NoteModel::PointList points = getLocalPoints(v, x);
Chris@30 314
Chris@30 315 if (points.empty()) {
Chris@30 316 if (!m_model->isReady()) {
Chris@30 317 return tr("In progress");
Chris@30 318 } else {
Chris@30 319 return tr("No local points");
Chris@30 320 }
Chris@30 321 }
Chris@30 322
Chris@30 323 Note note(0);
Chris@30 324 NoteModel::PointList::iterator i;
Chris@30 325
Chris@30 326 for (i = points.begin(); i != points.end(); ++i) {
Chris@30 327
Chris@44 328 int y = getYForValue(v, i->value);
Chris@30 329 int h = 3;
Chris@30 330
Chris@30 331 if (m_model->getValueQuantization() != 0.0) {
Chris@44 332 h = y - getYForValue(v, i->value + m_model->getValueQuantization());
Chris@30 333 if (h < 3) h = 3;
Chris@30 334 }
Chris@30 335
Chris@30 336 if (pos.y() >= y - h && pos.y() <= y) {
Chris@30 337 note = *i;
Chris@30 338 break;
Chris@30 339 }
Chris@30 340 }
Chris@30 341
Chris@30 342 if (i == points.end()) return tr("No local points");
Chris@30 343
Chris@30 344 RealTime rt = RealTime::frame2RealTime(note.frame,
Chris@30 345 m_model->getSampleRate());
Chris@30 346 RealTime rd = RealTime::frame2RealTime(note.duration,
Chris@30 347 m_model->getSampleRate());
Chris@30 348
Chris@101 349 QString pitchText;
Chris@101 350
Chris@101 351 if (shouldConvertMIDIToHz()) {
Chris@101 352
Chris@101 353 int mnote = lrintf(note.value);
Chris@101 354 int cents = lrintf((note.value - mnote) * 100);
Chris@101 355 float freq = Pitch::getFrequencyForPitch(mnote, cents);
Chris@101 356 pitchText = QString("%1 (%2 Hz)")
Chris@101 357 .arg(Pitch::getPitchLabel(mnote, cents)).arg(freq);
Chris@101 358
Chris@101 359 } else if (m_model->getScaleUnits() == "Hz") {
Chris@101 360
Chris@101 361 pitchText = QString("%1 Hz (%2)")
Chris@101 362 .arg(note.value)
Chris@101 363 .arg(Pitch::getPitchLabelForFrequency(note.value));
Chris@101 364
Chris@101 365 } else {
Chris@101 366 pitchText = QString("%1 %2")
Chris@101 367 .arg(note.value).arg(m_model->getScaleUnits());
Chris@101 368 }
Chris@101 369
Chris@30 370 QString text;
Chris@30 371
Chris@30 372 if (note.label == "") {
Chris@30 373 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nNo label"))
Chris@30 374 .arg(rt.toText(true).c_str())
Chris@101 375 .arg(pitchText)
Chris@30 376 .arg(rd.toText(true).c_str());
Chris@30 377 } else {
Chris@30 378 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nLabel:\t%4"))
Chris@30 379 .arg(rt.toText(true).c_str())
Chris@101 380 .arg(pitchText)
Chris@30 381 .arg(rd.toText(true).c_str())
Chris@30 382 .arg(note.label);
Chris@30 383 }
Chris@30 384
Chris@44 385 pos = QPoint(v->getXForFrame(note.frame),
Chris@44 386 getYForValue(v, note.value));
Chris@30 387 return text;
Chris@30 388 }
Chris@30 389
Chris@30 390 bool
Chris@44 391 NoteLayer::snapToFeatureFrame(View *v, int &frame,
Chris@44 392 size_t &resolution,
Chris@44 393 SnapType snap) const
Chris@30 394 {
Chris@30 395 if (!m_model) {
Chris@44 396 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@30 397 }
Chris@30 398
Chris@30 399 resolution = m_model->getResolution();
Chris@30 400 NoteModel::PointList points;
Chris@30 401
Chris@30 402 if (snap == SnapNeighbouring) {
Chris@30 403
Chris@44 404 points = getLocalPoints(v, v->getXForFrame(frame));
Chris@30 405 if (points.empty()) return false;
Chris@30 406 frame = points.begin()->frame;
Chris@30 407 return true;
Chris@30 408 }
Chris@30 409
Chris@30 410 points = m_model->getPoints(frame, frame);
Chris@30 411 int snapped = frame;
Chris@30 412 bool found = false;
Chris@30 413
Chris@30 414 for (NoteModel::PointList::const_iterator i = points.begin();
Chris@30 415 i != points.end(); ++i) {
Chris@30 416
Chris@30 417 if (snap == SnapRight) {
Chris@30 418
Chris@30 419 if (i->frame > frame) {
Chris@30 420 snapped = i->frame;
Chris@30 421 found = true;
Chris@30 422 break;
Chris@30 423 }
Chris@30 424
Chris@30 425 } else if (snap == SnapLeft) {
Chris@30 426
Chris@30 427 if (i->frame <= frame) {
Chris@30 428 snapped = i->frame;
Chris@30 429 found = true; // don't break, as the next may be better
Chris@30 430 } else {
Chris@30 431 break;
Chris@30 432 }
Chris@30 433
Chris@30 434 } else { // nearest
Chris@30 435
Chris@30 436 NoteModel::PointList::const_iterator j = i;
Chris@30 437 ++j;
Chris@30 438
Chris@30 439 if (j == points.end()) {
Chris@30 440
Chris@30 441 snapped = i->frame;
Chris@30 442 found = true;
Chris@30 443 break;
Chris@30 444
Chris@30 445 } else if (j->frame >= frame) {
Chris@30 446
Chris@30 447 if (j->frame - frame < frame - i->frame) {
Chris@30 448 snapped = j->frame;
Chris@30 449 } else {
Chris@30 450 snapped = i->frame;
Chris@30 451 }
Chris@30 452 found = true;
Chris@30 453 break;
Chris@30 454 }
Chris@30 455 }
Chris@30 456 }
Chris@30 457
Chris@30 458 frame = snapped;
Chris@30 459 return found;
Chris@30 460 }
Chris@30 461
Chris@101 462 void
Chris@101 463 NoteLayer::getScaleExtents(View *v, float &min, float &max, bool &log) const
Chris@30 464 {
Chris@101 465 min = 0.0;
Chris@101 466 max = 0.0;
Chris@101 467 log = false;
Chris@42 468
Chris@101 469 QString queryUnits;
Chris@101 470 if (shouldConvertMIDIToHz()) queryUnits = "Hz";
Chris@101 471 else queryUnits = m_model->getScaleUnits();
Chris@30 472
Chris@101 473 if (m_verticalScale == AutoAlignScale) {
Chris@30 474
Chris@101 475 if (!v->getValueExtents(queryUnits, min, max, log)) {
Chris@30 476
Chris@101 477 min = m_model->getValueMinimum();
Chris@101 478 max = m_model->getValueMaximum();
Chris@42 479
Chris@101 480 if (shouldConvertMIDIToHz()) {
Chris@101 481 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@101 482 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@101 483 }
Chris@42 484
Chris@105 485 std::cerr << "NoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << std::endl;
Chris@105 486
Chris@101 487 } else if (log) {
Chris@101 488
Chris@197 489 LogRange::mapRange(min, max);
Chris@105 490
Chris@105 491 std::cerr << "NoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << std::endl;
Chris@105 492
Chris@101 493 }
Chris@101 494
Chris@101 495 } else {
Chris@101 496
Chris@101 497 min = m_model->getValueMinimum();
Chris@101 498 max = m_model->getValueMaximum();
Chris@101 499
Chris@101 500 if (m_verticalScale == MIDIRangeScale) {
Chris@101 501 min = Pitch::getFrequencyForPitch(0);
Chris@101 502 max = Pitch::getFrequencyForPitch(127);
Chris@101 503 } else if (shouldConvertMIDIToHz()) {
Chris@101 504 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@101 505 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@101 506 }
Chris@101 507
Chris@101 508 if (m_verticalScale == LogScale || m_verticalScale == MIDIRangeScale) {
Chris@197 509 LogRange::mapRange(min, max);
Chris@101 510 log = true;
Chris@101 511 }
Chris@30 512 }
Chris@30 513
Chris@101 514 if (max == min) max = min + 1.0;
Chris@101 515 }
Chris@30 516
Chris@101 517 int
Chris@101 518 NoteLayer::getYForValue(View *v, float val) const
Chris@101 519 {
Chris@101 520 float min = 0.0, max = 0.0;
Chris@101 521 bool logarithmic = false;
Chris@101 522 int h = v->height();
Chris@101 523
Chris@101 524 getScaleExtents(v, min, max, logarithmic);
Chris@101 525
Chris@101 526 // std::cerr << "NoteLayer[" << this << "]::getYForValue(" << val << "): min = " << min << ", max = " << max << ", log = " << logarithmic << std::endl;
Chris@101 527
Chris@101 528 if (shouldConvertMIDIToHz()) {
Chris@101 529 val = Pitch::getFrequencyForPitch(lrintf(val),
Chris@101 530 lrintf((val - lrintf(val)) * 100));
Chris@101 531 // std::cerr << "shouldConvertMIDIToHz true, val now = " << val << std::endl;
Chris@101 532 }
Chris@101 533
Chris@101 534 if (logarithmic) {
Chris@197 535 val = LogRange::map(val);
Chris@101 536 // std::cerr << "logarithmic true, val now = " << val << std::endl;
Chris@101 537 }
Chris@101 538
Chris@101 539 int y = int(h - ((val - min) * h) / (max - min)) - 1;
Chris@101 540 // std::cerr << "y = " << y << std::endl;
Chris@101 541 return y;
Chris@30 542 }
Chris@30 543
Chris@30 544 float
Chris@44 545 NoteLayer::getValueForY(View *v, int y) const
Chris@30 546 {
Chris@101 547 float min = 0.0, max = 0.0;
Chris@101 548 bool logarithmic = false;
Chris@44 549 int h = v->height();
Chris@30 550
Chris@101 551 getScaleExtents(v, min, max, logarithmic);
Chris@101 552
Chris@101 553 float val = min + (float(h - y) * float(max - min)) / h;
Chris@101 554
Chris@101 555 if (logarithmic) {
Chris@197 556 val = powf(10.f, val);
Chris@101 557 }
Chris@101 558
Chris@101 559 if (shouldConvertMIDIToHz()) {
Chris@101 560 val = Pitch::getPitchForFrequency(val);
Chris@101 561 }
Chris@101 562
Chris@101 563 return val;
Chris@30 564 }
Chris@30 565
Chris@30 566 void
Chris@44 567 NoteLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@30 568 {
Chris@30 569 if (!m_model || !m_model->isOK()) return;
Chris@30 570
Chris@30 571 int sampleRate = m_model->getSampleRate();
Chris@30 572 if (!sampleRate) return;
Chris@30 573
Chris@30 574 // Profiler profiler("NoteLayer::paint", true);
Chris@30 575
Chris@30 576 int x0 = rect.left(), x1 = rect.right();
Chris@44 577 long frame0 = v->getFrameForX(x0);
Chris@44 578 long frame1 = v->getFrameForX(x1);
Chris@30 579
Chris@30 580 NoteModel::PointList points(m_model->getPoints(frame0, frame1));
Chris@30 581 if (points.empty()) return;
Chris@30 582
Chris@30 583 paint.setPen(m_colour);
Chris@30 584
Chris@30 585 QColor brushColour(m_colour);
Chris@30 586 brushColour.setAlpha(80);
Chris@30 587
Chris@30 588 // std::cerr << "NoteLayer::paint: resolution is "
Chris@30 589 // << m_model->getResolution() << " frames" << std::endl;
Chris@30 590
Chris@30 591 float min = m_model->getValueMinimum();
Chris@30 592 float max = m_model->getValueMaximum();
Chris@30 593 if (max == min) max = min + 1.0;
Chris@30 594
Chris@44 595 int origin = int(nearbyint(v->height() -
Chris@44 596 (-min * v->height()) / (max - min)));
Chris@30 597
Chris@30 598 QPoint localPos;
Chris@30 599 long illuminateFrame = -1;
Chris@30 600
Chris@44 601 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@30 602 NoteModel::PointList localPoints =
Chris@44 603 getLocalPoints(v, localPos.x());
Chris@30 604 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
Chris@30 605 }
Chris@30 606
Chris@30 607 paint.save();
Chris@30 608 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@30 609
Chris@30 610 for (NoteModel::PointList::const_iterator i = points.begin();
Chris@30 611 i != points.end(); ++i) {
Chris@30 612
Chris@30 613 const NoteModel::Point &p(*i);
Chris@30 614
Chris@44 615 int x = v->getXForFrame(p.frame);
Chris@44 616 int y = getYForValue(v, p.value);
Chris@44 617 int w = v->getXForFrame(p.frame + p.duration) - x;
Chris@30 618 int h = 3;
Chris@30 619
Chris@30 620 if (m_model->getValueQuantization() != 0.0) {
Chris@44 621 h = y - getYForValue(v, p.value + m_model->getValueQuantization());
Chris@30 622 if (h < 3) h = 3;
Chris@30 623 }
Chris@30 624
Chris@30 625 if (w < 1) w = 1;
Chris@30 626 paint.setPen(m_colour);
Chris@30 627 paint.setBrush(brushColour);
Chris@30 628
Chris@30 629 if (illuminateFrame == p.frame) {
Chris@30 630 if (localPos.y() >= y - h && localPos.y() < y) {
Chris@30 631 paint.setPen(Qt::black);//!!!
Chris@30 632 paint.setBrush(Qt::black);//!!!
Chris@30 633 }
Chris@30 634 }
Chris@30 635
Chris@124 636 paint.drawRect(x, y - h/2, w, h);
Chris@30 637
Chris@30 638 /// if (p.label != "") {
Chris@30 639 /// paint.drawText(x + 5, y - paint.fontMetrics().height() + paint.fontMetrics().ascent(), p.label);
Chris@30 640 /// }
Chris@30 641 }
Chris@30 642
Chris@30 643 paint.restore();
Chris@30 644 }
Chris@30 645
Chris@30 646 void
Chris@44 647 NoteLayer::drawStart(View *v, QMouseEvent *e)
Chris@30 648 {
Chris@101 649 // std::cerr << "NoteLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 650
Chris@30 651 if (!m_model) return;
Chris@30 652
Chris@44 653 long frame = v->getFrameForX(e->x());
Chris@30 654 if (frame < 0) frame = 0;
Chris@30 655 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 656
Chris@44 657 float value = getValueForY(v, e->y());
Chris@30 658
Chris@30 659 m_editingPoint = NoteModel::Point(frame, value, 0, tr("New Point"));
Chris@30 660 m_originalPoint = m_editingPoint;
Chris@30 661
Chris@30 662 if (m_editingCommand) m_editingCommand->finish();
Chris@30 663 m_editingCommand = new NoteModel::EditCommand(m_model,
Chris@30 664 tr("Draw Point"));
Chris@30 665 m_editingCommand->addPoint(m_editingPoint);
Chris@30 666
Chris@30 667 m_editing = true;
Chris@30 668 }
Chris@30 669
Chris@30 670 void
Chris@44 671 NoteLayer::drawDrag(View *v, QMouseEvent *e)
Chris@30 672 {
Chris@101 673 // std::cerr << "NoteLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 674
Chris@30 675 if (!m_model || !m_editing) return;
Chris@30 676
Chris@44 677 long frame = v->getFrameForX(e->x());
Chris@30 678 if (frame < 0) frame = 0;
Chris@30 679 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 680
Chris@101 681 float newValue = getValueForY(v, e->y());
Chris@101 682
Chris@101 683 long newFrame = m_editingPoint.frame;
Chris@101 684 long newDuration = frame - newFrame;
Chris@101 685 if (newDuration < 0) {
Chris@101 686 newFrame = frame;
Chris@101 687 newDuration = -newDuration;
Chris@101 688 } else if (newDuration == 0) {
Chris@101 689 newDuration = 1;
Chris@101 690 }
Chris@30 691
Chris@30 692 m_editingCommand->deletePoint(m_editingPoint);
Chris@101 693 m_editingPoint.frame = newFrame;
Chris@101 694 m_editingPoint.value = newValue;
Chris@101 695 m_editingPoint.duration = newDuration;
Chris@30 696 m_editingCommand->addPoint(m_editingPoint);
Chris@30 697 }
Chris@30 698
Chris@30 699 void
Chris@44 700 NoteLayer::drawEnd(View *v, QMouseEvent *e)
Chris@30 701 {
Chris@101 702 // std::cerr << "NoteLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 703 if (!m_model || !m_editing) return;
Chris@30 704 m_editingCommand->finish();
Chris@30 705 m_editingCommand = 0;
Chris@30 706 m_editing = false;
Chris@30 707 }
Chris@30 708
Chris@30 709 void
Chris@44 710 NoteLayer::editStart(View *v, QMouseEvent *e)
Chris@30 711 {
Chris@101 712 // std::cerr << "NoteLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 713
Chris@30 714 if (!m_model) return;
Chris@30 715
Chris@44 716 NoteModel::PointList points = getLocalPoints(v, e->x());
Chris@30 717 if (points.empty()) return;
Chris@30 718
Chris@30 719 m_editingPoint = *points.begin();
Chris@30 720 m_originalPoint = m_editingPoint;
Chris@30 721
Chris@30 722 if (m_editingCommand) {
Chris@30 723 m_editingCommand->finish();
Chris@30 724 m_editingCommand = 0;
Chris@30 725 }
Chris@30 726
Chris@30 727 m_editing = true;
Chris@30 728 }
Chris@30 729
Chris@30 730 void
Chris@44 731 NoteLayer::editDrag(View *v, QMouseEvent *e)
Chris@30 732 {
Chris@101 733 // std::cerr << "NoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 734
Chris@30 735 if (!m_model || !m_editing) return;
Chris@30 736
Chris@44 737 long frame = v->getFrameForX(e->x());
Chris@30 738 if (frame < 0) frame = 0;
Chris@30 739 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 740
Chris@44 741 float value = getValueForY(v, e->y());
Chris@30 742
Chris@30 743 if (!m_editingCommand) {
Chris@30 744 m_editingCommand = new NoteModel::EditCommand(m_model,
Chris@30 745 tr("Drag Point"));
Chris@30 746 }
Chris@30 747
Chris@30 748 m_editingCommand->deletePoint(m_editingPoint);
Chris@30 749 m_editingPoint.frame = frame;
Chris@30 750 m_editingPoint.value = value;
Chris@30 751 m_editingCommand->addPoint(m_editingPoint);
Chris@30 752 }
Chris@30 753
Chris@30 754 void
Chris@44 755 NoteLayer::editEnd(View *v, QMouseEvent *e)
Chris@30 756 {
Chris@101 757 // std::cerr << "NoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 758 if (!m_model || !m_editing) return;
Chris@30 759
Chris@30 760 if (m_editingCommand) {
Chris@30 761
Chris@30 762 QString newName = m_editingCommand->getName();
Chris@30 763
Chris@30 764 if (m_editingPoint.frame != m_originalPoint.frame) {
Chris@30 765 if (m_editingPoint.value != m_originalPoint.value) {
Chris@30 766 newName = tr("Edit Point");
Chris@30 767 } else {
Chris@30 768 newName = tr("Relocate Point");
Chris@30 769 }
Chris@30 770 } else {
Chris@30 771 newName = tr("Change Point Value");
Chris@30 772 }
Chris@30 773
Chris@30 774 m_editingCommand->setName(newName);
Chris@30 775 m_editingCommand->finish();
Chris@30 776 }
Chris@30 777
Chris@30 778 m_editingCommand = 0;
Chris@30 779 m_editing = false;
Chris@30 780 }
Chris@30 781
Chris@43 782 void
Chris@70 783 NoteLayer::editOpen(View *v, QMouseEvent *e)
Chris@70 784 {
Chris@70 785 if (!m_model) return;
Chris@70 786
Chris@70 787 NoteModel::PointList points = getLocalPoints(v, e->x());
Chris@70 788 if (points.empty()) return;
Chris@70 789
Chris@70 790 NoteModel::Point note = *points.begin();
Chris@70 791
Chris@70 792 ItemEditDialog *dialog = new ItemEditDialog
Chris@70 793 (m_model->getSampleRate(),
Chris@70 794 ItemEditDialog::ShowTime |
Chris@70 795 ItemEditDialog::ShowDuration |
Chris@70 796 ItemEditDialog::ShowValue |
Chris@100 797 ItemEditDialog::ShowText,
Chris@100 798 m_model->getScaleUnits());
Chris@70 799
Chris@70 800 dialog->setFrameTime(note.frame);
Chris@70 801 dialog->setValue(note.value);
Chris@70 802 dialog->setFrameDuration(note.duration);
Chris@70 803 dialog->setText(note.label);
Chris@70 804
Chris@70 805 if (dialog->exec() == QDialog::Accepted) {
Chris@70 806
Chris@70 807 NoteModel::Point newNote = note;
Chris@70 808 newNote.frame = dialog->getFrameTime();
Chris@70 809 newNote.value = dialog->getValue();
Chris@70 810 newNote.duration = dialog->getFrameDuration();
Chris@70 811 newNote.label = dialog->getText();
Chris@70 812
Chris@70 813 NoteModel::EditCommand *command = new NoteModel::EditCommand
Chris@70 814 (m_model, tr("Edit Point"));
Chris@70 815 command->deletePoint(note);
Chris@70 816 command->addPoint(newNote);
Chris@70 817 command->finish();
Chris@70 818 }
Chris@70 819
Chris@70 820 delete dialog;
Chris@70 821 }
Chris@70 822
Chris@70 823 void
Chris@43 824 NoteLayer::moveSelection(Selection s, size_t newStartFrame)
Chris@43 825 {
Chris@99 826 if (!m_model) return;
Chris@99 827
Chris@43 828 NoteModel::EditCommand *command =
Chris@43 829 new NoteModel::EditCommand(m_model, tr("Drag Selection"));
Chris@43 830
Chris@43 831 NoteModel::PointList points =
Chris@43 832 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 833
Chris@43 834 for (NoteModel::PointList::iterator i = points.begin();
Chris@43 835 i != points.end(); ++i) {
Chris@43 836
Chris@43 837 if (s.contains(i->frame)) {
Chris@43 838 NoteModel::Point newPoint(*i);
Chris@43 839 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
Chris@43 840 command->deletePoint(*i);
Chris@43 841 command->addPoint(newPoint);
Chris@43 842 }
Chris@43 843 }
Chris@43 844
Chris@43 845 command->finish();
Chris@43 846 }
Chris@43 847
Chris@43 848 void
Chris@43 849 NoteLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 850 {
Chris@99 851 if (!m_model) return;
Chris@99 852
Chris@43 853 NoteModel::EditCommand *command =
Chris@43 854 new NoteModel::EditCommand(m_model, tr("Resize Selection"));
Chris@43 855
Chris@43 856 NoteModel::PointList points =
Chris@43 857 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 858
Chris@43 859 double ratio =
Chris@43 860 double(newSize.getEndFrame() - newSize.getStartFrame()) /
Chris@43 861 double(s.getEndFrame() - s.getStartFrame());
Chris@43 862
Chris@43 863 for (NoteModel::PointList::iterator i = points.begin();
Chris@43 864 i != points.end(); ++i) {
Chris@43 865
Chris@43 866 if (s.contains(i->frame)) {
Chris@43 867
Chris@43 868 double targetStart = i->frame;
Chris@43 869 targetStart = newSize.getStartFrame() +
Chris@43 870 double(targetStart - s.getStartFrame()) * ratio;
Chris@43 871
Chris@43 872 double targetEnd = i->frame + i->duration;
Chris@43 873 targetEnd = newSize.getStartFrame() +
Chris@43 874 double(targetEnd - s.getStartFrame()) * ratio;
Chris@43 875
Chris@43 876 NoteModel::Point newPoint(*i);
Chris@43 877 newPoint.frame = lrint(targetStart);
Chris@43 878 newPoint.duration = lrint(targetEnd - targetStart);
Chris@43 879 command->deletePoint(*i);
Chris@43 880 command->addPoint(newPoint);
Chris@43 881 }
Chris@43 882 }
Chris@43 883
Chris@43 884 command->finish();
Chris@43 885 }
Chris@43 886
Chris@76 887 void
Chris@76 888 NoteLayer::deleteSelection(Selection s)
Chris@76 889 {
Chris@99 890 if (!m_model) return;
Chris@99 891
Chris@76 892 NoteModel::EditCommand *command =
Chris@76 893 new NoteModel::EditCommand(m_model, tr("Delete Selected Points"));
Chris@76 894
Chris@76 895 NoteModel::PointList points =
Chris@76 896 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 897
Chris@76 898 for (NoteModel::PointList::iterator i = points.begin();
Chris@76 899 i != points.end(); ++i) {
Chris@76 900
Chris@76 901 if (s.contains(i->frame)) {
Chris@76 902 command->deletePoint(*i);
Chris@76 903 }
Chris@76 904 }
Chris@76 905
Chris@76 906 command->finish();
Chris@76 907 }
Chris@76 908
Chris@76 909 void
Chris@76 910 NoteLayer::copy(Selection s, Clipboard &to)
Chris@76 911 {
Chris@99 912 if (!m_model) return;
Chris@99 913
Chris@76 914 NoteModel::PointList points =
Chris@76 915 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 916
Chris@76 917 for (NoteModel::PointList::iterator i = points.begin();
Chris@76 918 i != points.end(); ++i) {
Chris@76 919 if (s.contains(i->frame)) {
Chris@83 920 Clipboard::Point point(i->frame, i->value, i->duration, i->label);
Chris@76 921 to.addPoint(point);
Chris@76 922 }
Chris@76 923 }
Chris@76 924 }
Chris@76 925
Chris@125 926 bool
Chris@125 927 NoteLayer::paste(const Clipboard &from, int frameOffset, bool interactive)
Chris@76 928 {
Chris@125 929 if (!m_model) return false;
Chris@99 930
Chris@76 931 const Clipboard::PointList &points = from.getPoints();
Chris@76 932
Chris@76 933 NoteModel::EditCommand *command =
Chris@76 934 new NoteModel::EditCommand(m_model, tr("Paste"));
Chris@76 935
Chris@76 936 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@76 937 i != points.end(); ++i) {
Chris@76 938
Chris@76 939 if (!i->haveFrame()) continue;
Chris@76 940 size_t frame = 0;
Chris@76 941 if (frameOffset > 0 || -frameOffset < i->getFrame()) {
Chris@76 942 frame = i->getFrame() + frameOffset;
Chris@76 943 }
Chris@76 944 NoteModel::Point newPoint(frame);
Chris@76 945
Chris@76 946 if (i->haveLabel()) newPoint.label = i->getLabel();
Chris@76 947 if (i->haveValue()) newPoint.value = i->getValue();
Chris@76 948 else newPoint.value = (m_model->getValueMinimum() +
Chris@76 949 m_model->getValueMaximum()) / 2;
Chris@76 950 if (i->haveDuration()) newPoint.duration = i->getDuration();
Chris@125 951 else {
Chris@125 952 size_t nextFrame = frame;
Chris@125 953 Clipboard::PointList::const_iterator j = i;
Chris@125 954 for (; j != points.end(); ++j) {
Chris@125 955 if (!j->haveFrame()) continue;
Chris@125 956 if (j != i) break;
Chris@125 957 }
Chris@125 958 if (j != points.end()) {
Chris@125 959 nextFrame = j->getFrame();
Chris@125 960 }
Chris@125 961 if (nextFrame == frame) {
Chris@125 962 newPoint.duration = m_model->getResolution();
Chris@125 963 } else {
Chris@125 964 newPoint.duration = nextFrame - frame;
Chris@125 965 }
Chris@125 966 }
Chris@76 967
Chris@76 968 command->addPoint(newPoint);
Chris@76 969 }
Chris@76 970
Chris@76 971 command->finish();
Chris@125 972 return true;
Chris@76 973 }
Chris@76 974
Chris@30 975 QString
Chris@30 976 NoteLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@30 977 {
Chris@30 978 return Layer::toXmlString(indent, extraAttributes +
Chris@30 979 QString(" colour=\"%1\" verticalScale=\"%2\"")
Chris@30 980 .arg(encodeColour(m_colour)).arg(m_verticalScale));
Chris@30 981 }
Chris@30 982
Chris@30 983 void
Chris@30 984 NoteLayer::setProperties(const QXmlAttributes &attributes)
Chris@30 985 {
Chris@30 986 QString colourSpec = attributes.value("colour");
Chris@30 987 if (colourSpec != "") {
Chris@30 988 QColor colour(colourSpec);
Chris@30 989 if (colour.isValid()) {
Chris@30 990 setBaseColour(QColor(colourSpec));
Chris@30 991 }
Chris@30 992 }
Chris@30 993
Chris@30 994 bool ok;
Chris@30 995 VerticalScale scale = (VerticalScale)
Chris@30 996 attributes.value("verticalScale").toInt(&ok);
Chris@30 997 if (ok) setVerticalScale(scale);
Chris@30 998 }
Chris@30 999
Chris@30 1000