annotate layer/NoteLayer.cpp @ 507:efe0f232685f

* Make it possible to record live MIDI to a note layer. Now we're a proper MIDI sequencer. Ha, ha.
author Chris Cannam
date Wed, 25 Feb 2009 12:02:53 +0000
parents 0990b95140e3
children 1dd2cddc32eb
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@439 23 #include "base/RangeMapper.h"
Chris@376 24 #include "ColourDatabase.h"
Chris@128 25 #include "view/View.h"
Chris@30 26
Chris@128 27 #include "data/model/NoteModel.h"
Chris@30 28
Chris@70 29 #include "widgets/ItemEditDialog.h"
Chris@70 30
Chris@30 31 #include <QPainter>
Chris@30 32 #include <QPainterPath>
Chris@30 33 #include <QMouseEvent>
Chris@316 34 #include <QTextStream>
Chris@360 35 #include <QMessageBox>
Chris@30 36
Chris@30 37 #include <iostream>
Chris@30 38 #include <cmath>
Chris@30 39
Chris@44 40 NoteLayer::NoteLayer() :
Chris@287 41 SingleColourLayer(),
Chris@30 42 m_model(0),
Chris@30 43 m_editing(false),
Chris@335 44 m_originalPoint(0, 0.0, 0, 1.f, tr("New Point")),
Chris@335 45 m_editingPoint(0, 0.0, 0, 1.f, tr("New Point")),
Chris@30 46 m_editingCommand(0),
Chris@439 47 m_verticalScale(AutoAlignScale),
Chris@439 48 m_scaleMinimum(0),
Chris@439 49 m_scaleMaximum(0)
Chris@30 50 {
Chris@44 51
Chris@30 52 }
Chris@30 53
Chris@30 54 void
Chris@30 55 NoteLayer::setModel(NoteModel *model)
Chris@30 56 {
Chris@30 57 if (m_model == model) return;
Chris@30 58 m_model = model;
Chris@30 59
Chris@320 60 connectSignals(m_model);
Chris@30 61
Chris@101 62 // std::cerr << "NoteLayer::setModel(" << model << ")" << std::endl;
Chris@30 63
Chris@439 64 m_scaleMinimum = 0;
Chris@439 65 m_scaleMaximum = 0;
Chris@439 66
Chris@30 67 emit modelReplaced();
Chris@30 68 }
Chris@30 69
Chris@30 70 Layer::PropertyList
Chris@30 71 NoteLayer::getProperties() const
Chris@30 72 {
Chris@287 73 PropertyList list = SingleColourLayer::getProperties();
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 == "Vertical Scale") return tr("Vertical Scale");
Chris@116 83 if (name == "Scale Units") return tr("Scale Units");
Chris@287 84 return SingleColourLayer::getPropertyLabel(name);
Chris@87 85 }
Chris@87 86
Chris@30 87 Layer::PropertyType
Chris@100 88 NoteLayer::getPropertyType(const PropertyName &name) const
Chris@30 89 {
Chris@100 90 if (name == "Scale Units") return UnitsProperty;
Chris@287 91 if (name == "Vertical Scale") return ValueProperty;
Chris@287 92 return SingleColourLayer::getPropertyType(name);
Chris@30 93 }
Chris@30 94
Chris@198 95 QString
Chris@198 96 NoteLayer::getPropertyGroupName(const PropertyName &name) const
Chris@198 97 {
Chris@198 98 if (name == "Vertical Scale" || name == "Scale Units") {
Chris@198 99 return tr("Scale");
Chris@198 100 }
Chris@287 101 return SingleColourLayer::getPropertyGroupName(name);
Chris@198 102 }
Chris@198 103
Chris@30 104 int
Chris@30 105 NoteLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 106 int *min, int *max, int *deflt) const
Chris@30 107 {
Chris@216 108 int val = 0;
Chris@30 109
Chris@287 110 if (name == "Vertical Scale") {
Chris@30 111
Chris@30 112 if (min) *min = 0;
Chris@101 113 if (max) *max = 3;
Chris@216 114 if (deflt) *deflt = int(AutoAlignScale);
Chris@30 115
Chris@216 116 val = int(m_verticalScale);
Chris@30 117
Chris@100 118 } else if (name == "Scale Units") {
Chris@100 119
Chris@216 120 if (deflt) *deflt = 0;
Chris@100 121 if (m_model) {
Chris@216 122 val = UnitDatabase::getInstance()->getUnitId
Chris@100 123 (m_model->getScaleUnits());
Chris@100 124 }
Chris@100 125
Chris@30 126 } else {
Chris@216 127
Chris@287 128 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@30 129 }
Chris@30 130
Chris@216 131 return val;
Chris@30 132 }
Chris@30 133
Chris@30 134 QString
Chris@30 135 NoteLayer::getPropertyValueLabel(const PropertyName &name,
Chris@287 136 int value) const
Chris@30 137 {
Chris@287 138 if (name == "Vertical Scale") {
Chris@30 139 switch (value) {
Chris@30 140 default:
Chris@101 141 case 0: return tr("Auto-Align");
Chris@198 142 case 1: return tr("Linear");
Chris@198 143 case 2: return tr("Log");
Chris@198 144 case 3: return tr("MIDI Notes");
Chris@30 145 }
Chris@30 146 }
Chris@287 147 return SingleColourLayer::getPropertyValueLabel(name, value);
Chris@30 148 }
Chris@30 149
Chris@30 150 void
Chris@30 151 NoteLayer::setProperty(const PropertyName &name, int value)
Chris@30 152 {
Chris@287 153 if (name == "Vertical Scale") {
Chris@30 154 setVerticalScale(VerticalScale(value));
Chris@100 155 } else if (name == "Scale Units") {
Chris@100 156 if (m_model) {
Chris@100 157 m_model->setScaleUnits
Chris@100 158 (UnitDatabase::getInstance()->getUnitById(value));
Chris@100 159 emit modelChanged();
Chris@100 160 }
Chris@287 161 } else {
Chris@287 162 return SingleColourLayer::setProperty(name, value);
Chris@30 163 }
Chris@30 164 }
Chris@30 165
Chris@30 166 void
Chris@30 167 NoteLayer::setVerticalScale(VerticalScale scale)
Chris@30 168 {
Chris@30 169 if (m_verticalScale == scale) return;
Chris@30 170 m_verticalScale = scale;
Chris@30 171 emit layerParametersChanged();
Chris@30 172 }
Chris@30 173
Chris@30 174 bool
Chris@44 175 NoteLayer::isLayerScrollable(const View *v) const
Chris@30 176 {
Chris@30 177 QPoint discard;
Chris@44 178 return !v->shouldIlluminateLocalFeatures(this, discard);
Chris@30 179 }
Chris@30 180
Chris@79 181 bool
Chris@101 182 NoteLayer::shouldConvertMIDIToHz() const
Chris@101 183 {
Chris@101 184 QString unit = m_model->getScaleUnits();
Chris@101 185 return (unit != "Hz");
Chris@101 186 // if (unit == "" ||
Chris@101 187 // unit.startsWith("MIDI") ||
Chris@101 188 // unit.startsWith("midi")) return true;
Chris@101 189 // return false;
Chris@101 190 }
Chris@101 191
Chris@101 192 bool
Chris@101 193 NoteLayer::getValueExtents(float &min, float &max,
Chris@101 194 bool &logarithmic, QString &unit) const
Chris@79 195 {
Chris@79 196 if (!m_model) return false;
Chris@79 197 min = m_model->getValueMinimum();
Chris@79 198 max = m_model->getValueMaximum();
Chris@101 199
Chris@105 200 if (shouldConvertMIDIToHz()) {
Chris@105 201 unit = "Hz";
Chris@105 202 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@105 203 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@105 204 } else unit = m_model->getScaleUnits();
Chris@101 205
Chris@101 206 if (m_verticalScale == MIDIRangeScale ||
Chris@101 207 m_verticalScale == LogScale) logarithmic = true;
Chris@101 208
Chris@101 209 return true;
Chris@101 210 }
Chris@101 211
Chris@101 212 bool
Chris@101 213 NoteLayer::getDisplayExtents(float &min, float &max) const
Chris@101 214 {
Chris@439 215 if (!m_model || shouldAutoAlign()) return false;
Chris@101 216
Chris@101 217 if (m_verticalScale == MIDIRangeScale) {
Chris@101 218 min = Pitch::getFrequencyForPitch(0);
Chris@101 219 max = Pitch::getFrequencyForPitch(127);
Chris@101 220 return true;
Chris@101 221 }
Chris@101 222
Chris@439 223 if (m_scaleMinimum == m_scaleMaximum) {
Chris@455 224 min = m_model->getValueMinimum();
Chris@455 225 max = m_model->getValueMaximum();
Chris@455 226 } else {
Chris@455 227 min = m_scaleMinimum;
Chris@455 228 max = m_scaleMaximum;
Chris@439 229 }
Chris@439 230
Chris@101 231 if (shouldConvertMIDIToHz()) {
Chris@101 232 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@101 233 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@101 234 }
Chris@101 235
Chris@79 236 return true;
Chris@79 237 }
Chris@79 238
Chris@439 239 bool
Chris@439 240 NoteLayer::setDisplayExtents(float min, float max)
Chris@439 241 {
Chris@439 242 if (!m_model) return false;
Chris@439 243
Chris@439 244 if (min == max) {
Chris@439 245 if (min == 0.f) {
Chris@439 246 max = 1.f;
Chris@439 247 } else {
Chris@439 248 max = min * 1.0001;
Chris@439 249 }
Chris@439 250 }
Chris@439 251
Chris@439 252 m_scaleMinimum = min;
Chris@439 253 m_scaleMaximum = max;
Chris@439 254
Chris@439 255 // std::cerr << "NoteLayer::setDisplayExtents: min = " << min << ", max = " << max << std::endl;
Chris@439 256
Chris@439 257 emit layerParametersChanged();
Chris@439 258 return true;
Chris@439 259 }
Chris@439 260
Chris@439 261 int
Chris@439 262 NoteLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@439 263 {
Chris@439 264 if (shouldAutoAlign()) return 0;
Chris@439 265 if (!m_model) return 0;
Chris@439 266
Chris@439 267 defaultStep = 0;
Chris@439 268 return 100;
Chris@439 269 }
Chris@439 270
Chris@439 271 int
Chris@439 272 NoteLayer::getCurrentVerticalZoomStep() const
Chris@439 273 {
Chris@439 274 if (shouldAutoAlign()) return 0;
Chris@439 275 if (!m_model) return 0;
Chris@439 276
Chris@439 277 RangeMapper *mapper = getNewVerticalZoomRangeMapper();
Chris@439 278 if (!mapper) return 0;
Chris@439 279
Chris@439 280 float dmin, dmax;
Chris@439 281 getDisplayExtents(dmin, dmax);
Chris@439 282
Chris@439 283 int nr = mapper->getPositionForValue(dmax - dmin);
Chris@439 284
Chris@439 285 delete mapper;
Chris@439 286
Chris@439 287 return 100 - nr;
Chris@439 288 }
Chris@439 289
Chris@439 290 //!!! lots of duplication with TimeValueLayer
Chris@439 291
Chris@439 292 void
Chris@439 293 NoteLayer::setVerticalZoomStep(int step)
Chris@439 294 {
Chris@439 295 if (shouldAutoAlign()) return;
Chris@439 296 if (!m_model) return;
Chris@439 297
Chris@439 298 RangeMapper *mapper = getNewVerticalZoomRangeMapper();
Chris@439 299 if (!mapper) return;
Chris@439 300
Chris@439 301 float min, max;
Chris@439 302 bool logarithmic;
Chris@439 303 QString unit;
Chris@439 304 getValueExtents(min, max, logarithmic, unit);
Chris@439 305
Chris@439 306 float dmin, dmax;
Chris@439 307 getDisplayExtents(dmin, dmax);
Chris@439 308
Chris@439 309 float newdist = mapper->getValueForPosition(100 - step);
Chris@439 310
Chris@439 311 float newmin, newmax;
Chris@439 312
Chris@439 313 if (logarithmic) {
Chris@439 314
Chris@439 315 // see SpectrogramLayer::setVerticalZoomStep
Chris@439 316
Chris@439 317 newmax = (newdist + sqrtf(newdist*newdist + 4*dmin*dmax)) / 2;
Chris@439 318 newmin = newmax - newdist;
Chris@439 319
Chris@439 320 // std::cerr << "newmin = " << newmin << ", newmax = " << newmax << std::endl;
Chris@439 321
Chris@439 322 } else {
Chris@439 323 float dmid = (dmax + dmin) / 2;
Chris@439 324 newmin = dmid - newdist / 2;
Chris@439 325 newmax = dmid + newdist / 2;
Chris@439 326 }
Chris@439 327
Chris@439 328 if (newmin < min) {
Chris@439 329 newmax += (min - newmin);
Chris@439 330 newmin = min;
Chris@439 331 }
Chris@439 332 if (newmax > max) {
Chris@439 333 newmax = max;
Chris@439 334 }
Chris@439 335
Chris@439 336 std::cerr << "NoteLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << std::endl;
Chris@439 337
Chris@439 338 setDisplayExtents(newmin, newmax);
Chris@439 339 }
Chris@439 340
Chris@439 341 RangeMapper *
Chris@439 342 NoteLayer::getNewVerticalZoomRangeMapper() const
Chris@439 343 {
Chris@439 344 if (!m_model) return 0;
Chris@439 345
Chris@439 346 RangeMapper *mapper;
Chris@439 347
Chris@439 348 float min, max;
Chris@439 349 bool logarithmic;
Chris@439 350 QString unit;
Chris@439 351 getValueExtents(min, max, logarithmic, unit);
Chris@439 352
Chris@439 353 if (min == max) return 0;
Chris@439 354
Chris@439 355 if (logarithmic) {
Chris@439 356 mapper = new LogRangeMapper(0, 100, min, max, unit);
Chris@439 357 } else {
Chris@439 358 mapper = new LinearRangeMapper(0, 100, min, max, unit);
Chris@439 359 }
Chris@439 360
Chris@439 361 return mapper;
Chris@439 362 }
Chris@439 363
Chris@30 364 NoteModel::PointList
Chris@44 365 NoteLayer::getLocalPoints(View *v, int x) const
Chris@30 366 {
Chris@30 367 if (!m_model) return NoteModel::PointList();
Chris@30 368
Chris@44 369 long frame = v->getFrameForX(x);
Chris@30 370
Chris@30 371 NoteModel::PointList onPoints =
Chris@30 372 m_model->getPoints(frame);
Chris@30 373
Chris@30 374 if (!onPoints.empty()) {
Chris@30 375 return onPoints;
Chris@30 376 }
Chris@30 377
Chris@30 378 NoteModel::PointList prevPoints =
Chris@30 379 m_model->getPreviousPoints(frame);
Chris@30 380 NoteModel::PointList nextPoints =
Chris@30 381 m_model->getNextPoints(frame);
Chris@30 382
Chris@30 383 NoteModel::PointList usePoints = prevPoints;
Chris@30 384
Chris@30 385 if (prevPoints.empty()) {
Chris@30 386 usePoints = nextPoints;
Chris@248 387 } else if (long(prevPoints.begin()->frame) < v->getStartFrame() &&
Chris@44 388 !(nextPoints.begin()->frame > v->getEndFrame())) {
Chris@30 389 usePoints = nextPoints;
Chris@248 390 } else if (long(nextPoints.begin()->frame) - frame <
Chris@248 391 frame - long(prevPoints.begin()->frame)) {
Chris@30 392 usePoints = nextPoints;
Chris@30 393 }
Chris@30 394
Chris@30 395 if (!usePoints.empty()) {
Chris@30 396 int fuzz = 2;
Chris@44 397 int px = v->getXForFrame(usePoints.begin()->frame);
Chris@30 398 if ((px > x && px - x > fuzz) ||
Chris@30 399 (px < x && x - px > fuzz + 1)) {
Chris@30 400 usePoints.clear();
Chris@30 401 }
Chris@30 402 }
Chris@30 403
Chris@30 404 return usePoints;
Chris@30 405 }
Chris@30 406
Chris@30 407 QString
Chris@44 408 NoteLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@30 409 {
Chris@30 410 int x = pos.x();
Chris@30 411
Chris@30 412 if (!m_model || !m_model->getSampleRate()) return "";
Chris@30 413
Chris@44 414 NoteModel::PointList points = getLocalPoints(v, x);
Chris@30 415
Chris@30 416 if (points.empty()) {
Chris@30 417 if (!m_model->isReady()) {
Chris@30 418 return tr("In progress");
Chris@30 419 } else {
Chris@30 420 return tr("No local points");
Chris@30 421 }
Chris@30 422 }
Chris@30 423
Chris@30 424 Note note(0);
Chris@30 425 NoteModel::PointList::iterator i;
Chris@30 426
Chris@30 427 for (i = points.begin(); i != points.end(); ++i) {
Chris@30 428
Chris@44 429 int y = getYForValue(v, i->value);
Chris@30 430 int h = 3;
Chris@30 431
Chris@30 432 if (m_model->getValueQuantization() != 0.0) {
Chris@44 433 h = y - getYForValue(v, i->value + m_model->getValueQuantization());
Chris@30 434 if (h < 3) h = 3;
Chris@30 435 }
Chris@30 436
Chris@30 437 if (pos.y() >= y - h && pos.y() <= y) {
Chris@30 438 note = *i;
Chris@30 439 break;
Chris@30 440 }
Chris@30 441 }
Chris@30 442
Chris@30 443 if (i == points.end()) return tr("No local points");
Chris@30 444
Chris@30 445 RealTime rt = RealTime::frame2RealTime(note.frame,
Chris@30 446 m_model->getSampleRate());
Chris@30 447 RealTime rd = RealTime::frame2RealTime(note.duration,
Chris@30 448 m_model->getSampleRate());
Chris@30 449
Chris@101 450 QString pitchText;
Chris@101 451
Chris@101 452 if (shouldConvertMIDIToHz()) {
Chris@101 453
Chris@101 454 int mnote = lrintf(note.value);
Chris@101 455 int cents = lrintf((note.value - mnote) * 100);
Chris@101 456 float freq = Pitch::getFrequencyForPitch(mnote, cents);
Chris@234 457 pitchText = tr("%1 (%2 Hz)")
Chris@101 458 .arg(Pitch::getPitchLabel(mnote, cents)).arg(freq);
Chris@101 459
Chris@101 460 } else if (m_model->getScaleUnits() == "Hz") {
Chris@101 461
Chris@234 462 pitchText = tr("%1 Hz (%2)")
Chris@101 463 .arg(note.value)
Chris@101 464 .arg(Pitch::getPitchLabelForFrequency(note.value));
Chris@101 465
Chris@101 466 } else {
Chris@234 467 pitchText = tr("%1 %2")
Chris@101 468 .arg(note.value).arg(m_model->getScaleUnits());
Chris@101 469 }
Chris@101 470
Chris@30 471 QString text;
Chris@30 472
Chris@30 473 if (note.label == "") {
Chris@30 474 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nNo label"))
Chris@30 475 .arg(rt.toText(true).c_str())
Chris@101 476 .arg(pitchText)
Chris@30 477 .arg(rd.toText(true).c_str());
Chris@30 478 } else {
Chris@30 479 text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nLabel:\t%4"))
Chris@30 480 .arg(rt.toText(true).c_str())
Chris@101 481 .arg(pitchText)
Chris@30 482 .arg(rd.toText(true).c_str())
Chris@30 483 .arg(note.label);
Chris@30 484 }
Chris@30 485
Chris@44 486 pos = QPoint(v->getXForFrame(note.frame),
Chris@44 487 getYForValue(v, note.value));
Chris@30 488 return text;
Chris@30 489 }
Chris@30 490
Chris@30 491 bool
Chris@44 492 NoteLayer::snapToFeatureFrame(View *v, int &frame,
Chris@44 493 size_t &resolution,
Chris@44 494 SnapType snap) const
Chris@30 495 {
Chris@30 496 if (!m_model) {
Chris@44 497 return Layer::snapToFeatureFrame(v, frame, resolution, snap);
Chris@30 498 }
Chris@30 499
Chris@30 500 resolution = m_model->getResolution();
Chris@30 501 NoteModel::PointList points;
Chris@30 502
Chris@30 503 if (snap == SnapNeighbouring) {
Chris@30 504
Chris@44 505 points = getLocalPoints(v, v->getXForFrame(frame));
Chris@30 506 if (points.empty()) return false;
Chris@30 507 frame = points.begin()->frame;
Chris@30 508 return true;
Chris@30 509 }
Chris@30 510
Chris@30 511 points = m_model->getPoints(frame, frame);
Chris@30 512 int snapped = frame;
Chris@30 513 bool found = false;
Chris@30 514
Chris@30 515 for (NoteModel::PointList::const_iterator i = points.begin();
Chris@30 516 i != points.end(); ++i) {
Chris@30 517
Chris@30 518 if (snap == SnapRight) {
Chris@30 519
Chris@30 520 if (i->frame > frame) {
Chris@30 521 snapped = i->frame;
Chris@30 522 found = true;
Chris@30 523 break;
Chris@30 524 }
Chris@30 525
Chris@30 526 } else if (snap == SnapLeft) {
Chris@30 527
Chris@30 528 if (i->frame <= frame) {
Chris@30 529 snapped = i->frame;
Chris@30 530 found = true; // don't break, as the next may be better
Chris@30 531 } else {
Chris@30 532 break;
Chris@30 533 }
Chris@30 534
Chris@30 535 } else { // nearest
Chris@30 536
Chris@30 537 NoteModel::PointList::const_iterator j = i;
Chris@30 538 ++j;
Chris@30 539
Chris@30 540 if (j == points.end()) {
Chris@30 541
Chris@30 542 snapped = i->frame;
Chris@30 543 found = true;
Chris@30 544 break;
Chris@30 545
Chris@30 546 } else if (j->frame >= frame) {
Chris@30 547
Chris@30 548 if (j->frame - frame < frame - i->frame) {
Chris@30 549 snapped = j->frame;
Chris@30 550 } else {
Chris@30 551 snapped = i->frame;
Chris@30 552 }
Chris@30 553 found = true;
Chris@30 554 break;
Chris@30 555 }
Chris@30 556 }
Chris@30 557 }
Chris@30 558
Chris@30 559 frame = snapped;
Chris@30 560 return found;
Chris@30 561 }
Chris@30 562
Chris@101 563 void
Chris@101 564 NoteLayer::getScaleExtents(View *v, float &min, float &max, bool &log) const
Chris@30 565 {
Chris@101 566 min = 0.0;
Chris@101 567 max = 0.0;
Chris@101 568 log = false;
Chris@42 569
Chris@101 570 QString queryUnits;
Chris@101 571 if (shouldConvertMIDIToHz()) queryUnits = "Hz";
Chris@101 572 else queryUnits = m_model->getScaleUnits();
Chris@30 573
Chris@439 574 if (shouldAutoAlign()) {
Chris@30 575
Chris@101 576 if (!v->getValueExtents(queryUnits, min, max, log)) {
Chris@30 577
Chris@101 578 min = m_model->getValueMinimum();
Chris@101 579 max = m_model->getValueMaximum();
Chris@42 580
Chris@101 581 if (shouldConvertMIDIToHz()) {
Chris@101 582 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@101 583 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@101 584 }
Chris@42 585
Chris@105 586 std::cerr << "NoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << std::endl;
Chris@105 587
Chris@101 588 } else if (log) {
Chris@101 589
Chris@197 590 LogRange::mapRange(min, max);
Chris@105 591
Chris@105 592 std::cerr << "NoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << std::endl;
Chris@105 593
Chris@101 594 }
Chris@101 595
Chris@101 596 } else {
Chris@101 597
Chris@439 598 getDisplayExtents(min, max);
Chris@101 599
Chris@101 600 if (m_verticalScale == MIDIRangeScale) {
Chris@101 601 min = Pitch::getFrequencyForPitch(0);
Chris@101 602 max = Pitch::getFrequencyForPitch(127);
Chris@101 603 } else if (shouldConvertMIDIToHz()) {
Chris@101 604 min = Pitch::getFrequencyForPitch(lrintf(min));
Chris@101 605 max = Pitch::getFrequencyForPitch(lrintf(max + 1));
Chris@101 606 }
Chris@101 607
Chris@101 608 if (m_verticalScale == LogScale || m_verticalScale == MIDIRangeScale) {
Chris@197 609 LogRange::mapRange(min, max);
Chris@101 610 log = true;
Chris@101 611 }
Chris@30 612 }
Chris@30 613
Chris@101 614 if (max == min) max = min + 1.0;
Chris@101 615 }
Chris@30 616
Chris@101 617 int
Chris@101 618 NoteLayer::getYForValue(View *v, float val) const
Chris@101 619 {
Chris@101 620 float min = 0.0, max = 0.0;
Chris@101 621 bool logarithmic = false;
Chris@101 622 int h = v->height();
Chris@101 623
Chris@101 624 getScaleExtents(v, min, max, logarithmic);
Chris@101 625
Chris@101 626 // std::cerr << "NoteLayer[" << this << "]::getYForValue(" << val << "): min = " << min << ", max = " << max << ", log = " << logarithmic << std::endl;
Chris@101 627
Chris@101 628 if (shouldConvertMIDIToHz()) {
Chris@101 629 val = Pitch::getFrequencyForPitch(lrintf(val),
Chris@101 630 lrintf((val - lrintf(val)) * 100));
Chris@101 631 // std::cerr << "shouldConvertMIDIToHz true, val now = " << val << std::endl;
Chris@101 632 }
Chris@101 633
Chris@101 634 if (logarithmic) {
Chris@197 635 val = LogRange::map(val);
Chris@101 636 // std::cerr << "logarithmic true, val now = " << val << std::endl;
Chris@101 637 }
Chris@101 638
Chris@101 639 int y = int(h - ((val - min) * h) / (max - min)) - 1;
Chris@101 640 // std::cerr << "y = " << y << std::endl;
Chris@101 641 return y;
Chris@30 642 }
Chris@30 643
Chris@30 644 float
Chris@44 645 NoteLayer::getValueForY(View *v, int y) const
Chris@30 646 {
Chris@101 647 float min = 0.0, max = 0.0;
Chris@101 648 bool logarithmic = false;
Chris@44 649 int h = v->height();
Chris@30 650
Chris@101 651 getScaleExtents(v, min, max, logarithmic);
Chris@101 652
Chris@101 653 float val = min + (float(h - y) * float(max - min)) / h;
Chris@101 654
Chris@101 655 if (logarithmic) {
Chris@197 656 val = powf(10.f, val);
Chris@101 657 }
Chris@101 658
Chris@101 659 if (shouldConvertMIDIToHz()) {
Chris@101 660 val = Pitch::getPitchForFrequency(val);
Chris@101 661 }
Chris@101 662
Chris@101 663 return val;
Chris@30 664 }
Chris@30 665
Chris@439 666 bool
Chris@439 667 NoteLayer::shouldAutoAlign() const
Chris@439 668 {
Chris@439 669 if (!m_model) return false;
Chris@439 670 return (m_verticalScale == AutoAlignScale);
Chris@439 671 }
Chris@439 672
Chris@30 673 void
Chris@44 674 NoteLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@30 675 {
Chris@30 676 if (!m_model || !m_model->isOK()) return;
Chris@30 677
Chris@30 678 int sampleRate = m_model->getSampleRate();
Chris@30 679 if (!sampleRate) return;
Chris@30 680
Chris@30 681 // Profiler profiler("NoteLayer::paint", true);
Chris@30 682
Chris@30 683 int x0 = rect.left(), x1 = rect.right();
Chris@44 684 long frame0 = v->getFrameForX(x0);
Chris@44 685 long frame1 = v->getFrameForX(x1);
Chris@30 686
Chris@30 687 NoteModel::PointList points(m_model->getPoints(frame0, frame1));
Chris@30 688 if (points.empty()) return;
Chris@30 689
Chris@287 690 paint.setPen(getBaseQColor());
Chris@30 691
Chris@287 692 QColor brushColour(getBaseQColor());
Chris@30 693 brushColour.setAlpha(80);
Chris@30 694
Chris@30 695 // std::cerr << "NoteLayer::paint: resolution is "
Chris@30 696 // << m_model->getResolution() << " frames" << std::endl;
Chris@30 697
Chris@30 698 float min = m_model->getValueMinimum();
Chris@30 699 float max = m_model->getValueMaximum();
Chris@30 700 if (max == min) max = min + 1.0;
Chris@30 701
Chris@30 702 QPoint localPos;
Chris@30 703 long illuminateFrame = -1;
Chris@30 704
Chris@44 705 if (v->shouldIlluminateLocalFeatures(this, localPos)) {
Chris@30 706 NoteModel::PointList localPoints =
Chris@44 707 getLocalPoints(v, localPos.x());
Chris@30 708 if (!localPoints.empty()) illuminateFrame = localPoints.begin()->frame;
Chris@30 709 }
Chris@30 710
Chris@30 711 paint.save();
Chris@30 712 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@30 713
Chris@30 714 for (NoteModel::PointList::const_iterator i = points.begin();
Chris@30 715 i != points.end(); ++i) {
Chris@30 716
Chris@30 717 const NoteModel::Point &p(*i);
Chris@30 718
Chris@44 719 int x = v->getXForFrame(p.frame);
Chris@44 720 int y = getYForValue(v, p.value);
Chris@44 721 int w = v->getXForFrame(p.frame + p.duration) - x;
Chris@30 722 int h = 3;
Chris@30 723
Chris@30 724 if (m_model->getValueQuantization() != 0.0) {
Chris@44 725 h = y - getYForValue(v, p.value + m_model->getValueQuantization());
Chris@30 726 if (h < 3) h = 3;
Chris@30 727 }
Chris@30 728
Chris@30 729 if (w < 1) w = 1;
Chris@287 730 paint.setPen(getBaseQColor());
Chris@30 731 paint.setBrush(brushColour);
Chris@30 732
Chris@30 733 if (illuminateFrame == p.frame) {
Chris@30 734 if (localPos.y() >= y - h && localPos.y() < y) {
Chris@287 735 paint.setPen(v->getForeground());
Chris@287 736 paint.setBrush(v->getForeground());
Chris@30 737 }
Chris@30 738 }
Chris@30 739
Chris@124 740 paint.drawRect(x, y - h/2, w, h);
Chris@30 741
Chris@30 742 /// if (p.label != "") {
Chris@30 743 /// paint.drawText(x + 5, y - paint.fontMetrics().height() + paint.fontMetrics().ascent(), p.label);
Chris@30 744 /// }
Chris@30 745 }
Chris@30 746
Chris@30 747 paint.restore();
Chris@30 748 }
Chris@30 749
Chris@30 750 void
Chris@44 751 NoteLayer::drawStart(View *v, QMouseEvent *e)
Chris@30 752 {
Chris@101 753 // std::cerr << "NoteLayer::drawStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 754
Chris@30 755 if (!m_model) return;
Chris@30 756
Chris@44 757 long frame = v->getFrameForX(e->x());
Chris@30 758 if (frame < 0) frame = 0;
Chris@30 759 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 760
Chris@44 761 float value = getValueForY(v, e->y());
Chris@30 762
Chris@335 763 m_editingPoint = NoteModel::Point(frame, value, 0, 0.8, tr("New Point"));
Chris@30 764 m_originalPoint = m_editingPoint;
Chris@30 765
Chris@376 766 if (m_editingCommand) finish(m_editingCommand);
Chris@30 767 m_editingCommand = new NoteModel::EditCommand(m_model,
Chris@30 768 tr("Draw Point"));
Chris@30 769 m_editingCommand->addPoint(m_editingPoint);
Chris@30 770
Chris@30 771 m_editing = true;
Chris@30 772 }
Chris@30 773
Chris@30 774 void
Chris@44 775 NoteLayer::drawDrag(View *v, QMouseEvent *e)
Chris@30 776 {
Chris@101 777 // std::cerr << "NoteLayer::drawDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 778
Chris@30 779 if (!m_model || !m_editing) return;
Chris@30 780
Chris@44 781 long frame = v->getFrameForX(e->x());
Chris@30 782 if (frame < 0) frame = 0;
Chris@30 783 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 784
Chris@101 785 float newValue = getValueForY(v, e->y());
Chris@101 786
Chris@101 787 long newFrame = m_editingPoint.frame;
Chris@101 788 long newDuration = frame - newFrame;
Chris@101 789 if (newDuration < 0) {
Chris@101 790 newFrame = frame;
Chris@101 791 newDuration = -newDuration;
Chris@101 792 } else if (newDuration == 0) {
Chris@101 793 newDuration = 1;
Chris@101 794 }
Chris@30 795
Chris@30 796 m_editingCommand->deletePoint(m_editingPoint);
Chris@101 797 m_editingPoint.frame = newFrame;
Chris@101 798 m_editingPoint.value = newValue;
Chris@101 799 m_editingPoint.duration = newDuration;
Chris@30 800 m_editingCommand->addPoint(m_editingPoint);
Chris@30 801 }
Chris@30 802
Chris@30 803 void
Chris@248 804 NoteLayer::drawEnd(View *, QMouseEvent *)
Chris@30 805 {
Chris@101 806 // std::cerr << "NoteLayer::drawEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 807 if (!m_model || !m_editing) return;
Chris@376 808 finish(m_editingCommand);
Chris@30 809 m_editingCommand = 0;
Chris@30 810 m_editing = false;
Chris@30 811 }
Chris@30 812
Chris@30 813 void
Chris@335 814 NoteLayer::eraseStart(View *v, QMouseEvent *e)
Chris@335 815 {
Chris@335 816 if (!m_model) return;
Chris@335 817
Chris@335 818 NoteModel::PointList points = getLocalPoints(v, e->x());
Chris@335 819 if (points.empty()) return;
Chris@335 820
Chris@335 821 m_editingPoint = *points.begin();
Chris@335 822
Chris@335 823 if (m_editingCommand) {
Chris@376 824 finish(m_editingCommand);
Chris@335 825 m_editingCommand = 0;
Chris@335 826 }
Chris@335 827
Chris@335 828 m_editing = true;
Chris@335 829 }
Chris@335 830
Chris@335 831 void
Chris@335 832 NoteLayer::eraseDrag(View *v, QMouseEvent *e)
Chris@335 833 {
Chris@335 834 }
Chris@335 835
Chris@335 836 void
Chris@335 837 NoteLayer::eraseEnd(View *v, QMouseEvent *e)
Chris@335 838 {
Chris@335 839 if (!m_model || !m_editing) return;
Chris@335 840
Chris@335 841 m_editing = false;
Chris@335 842
Chris@335 843 NoteModel::PointList points = getLocalPoints(v, e->x());
Chris@335 844 if (points.empty()) return;
Chris@335 845 if (points.begin()->frame != m_editingPoint.frame ||
Chris@335 846 points.begin()->value != m_editingPoint.value) return;
Chris@335 847
Chris@335 848 m_editingCommand = new NoteModel::EditCommand
Chris@335 849 (m_model, tr("Erase Point"));
Chris@335 850
Chris@335 851 m_editingCommand->deletePoint(m_editingPoint);
Chris@335 852
Chris@376 853 finish(m_editingCommand);
Chris@335 854 m_editingCommand = 0;
Chris@335 855 m_editing = false;
Chris@335 856 }
Chris@335 857
Chris@335 858 void
Chris@44 859 NoteLayer::editStart(View *v, QMouseEvent *e)
Chris@30 860 {
Chris@101 861 // std::cerr << "NoteLayer::editStart(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 862
Chris@30 863 if (!m_model) return;
Chris@30 864
Chris@44 865 NoteModel::PointList points = getLocalPoints(v, e->x());
Chris@30 866 if (points.empty()) return;
Chris@30 867
Chris@30 868 m_editingPoint = *points.begin();
Chris@30 869 m_originalPoint = m_editingPoint;
Chris@30 870
Chris@30 871 if (m_editingCommand) {
Chris@376 872 finish(m_editingCommand);
Chris@30 873 m_editingCommand = 0;
Chris@30 874 }
Chris@30 875
Chris@30 876 m_editing = true;
Chris@30 877 }
Chris@30 878
Chris@30 879 void
Chris@44 880 NoteLayer::editDrag(View *v, QMouseEvent *e)
Chris@30 881 {
Chris@101 882 // std::cerr << "NoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 883
Chris@30 884 if (!m_model || !m_editing) return;
Chris@30 885
Chris@44 886 long frame = v->getFrameForX(e->x());
Chris@30 887 if (frame < 0) frame = 0;
Chris@30 888 frame = frame / m_model->getResolution() * m_model->getResolution();
Chris@30 889
Chris@44 890 float value = getValueForY(v, e->y());
Chris@30 891
Chris@30 892 if (!m_editingCommand) {
Chris@30 893 m_editingCommand = new NoteModel::EditCommand(m_model,
Chris@30 894 tr("Drag Point"));
Chris@30 895 }
Chris@30 896
Chris@30 897 m_editingCommand->deletePoint(m_editingPoint);
Chris@30 898 m_editingPoint.frame = frame;
Chris@30 899 m_editingPoint.value = value;
Chris@30 900 m_editingCommand->addPoint(m_editingPoint);
Chris@30 901 }
Chris@30 902
Chris@30 903 void
Chris@248 904 NoteLayer::editEnd(View *, QMouseEvent *)
Chris@30 905 {
Chris@101 906 // std::cerr << "NoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << std::endl;
Chris@30 907 if (!m_model || !m_editing) return;
Chris@30 908
Chris@30 909 if (m_editingCommand) {
Chris@30 910
Chris@30 911 QString newName = m_editingCommand->getName();
Chris@30 912
Chris@30 913 if (m_editingPoint.frame != m_originalPoint.frame) {
Chris@30 914 if (m_editingPoint.value != m_originalPoint.value) {
Chris@30 915 newName = tr("Edit Point");
Chris@30 916 } else {
Chris@30 917 newName = tr("Relocate Point");
Chris@30 918 }
Chris@30 919 } else {
Chris@30 920 newName = tr("Change Point Value");
Chris@30 921 }
Chris@30 922
Chris@30 923 m_editingCommand->setName(newName);
Chris@376 924 finish(m_editingCommand);
Chris@30 925 }
Chris@30 926
Chris@30 927 m_editingCommand = 0;
Chris@30 928 m_editing = false;
Chris@30 929 }
Chris@30 930
Chris@255 931 bool
Chris@70 932 NoteLayer::editOpen(View *v, QMouseEvent *e)
Chris@70 933 {
Chris@255 934 if (!m_model) return false;
Chris@70 935
Chris@70 936 NoteModel::PointList points = getLocalPoints(v, e->x());
Chris@255 937 if (points.empty()) return false;
Chris@70 938
Chris@70 939 NoteModel::Point note = *points.begin();
Chris@70 940
Chris@70 941 ItemEditDialog *dialog = new ItemEditDialog
Chris@70 942 (m_model->getSampleRate(),
Chris@70 943 ItemEditDialog::ShowTime |
Chris@70 944 ItemEditDialog::ShowDuration |
Chris@70 945 ItemEditDialog::ShowValue |
Chris@100 946 ItemEditDialog::ShowText,
Chris@100 947 m_model->getScaleUnits());
Chris@70 948
Chris@70 949 dialog->setFrameTime(note.frame);
Chris@70 950 dialog->setValue(note.value);
Chris@70 951 dialog->setFrameDuration(note.duration);
Chris@70 952 dialog->setText(note.label);
Chris@70 953
Chris@70 954 if (dialog->exec() == QDialog::Accepted) {
Chris@70 955
Chris@70 956 NoteModel::Point newNote = note;
Chris@70 957 newNote.frame = dialog->getFrameTime();
Chris@70 958 newNote.value = dialog->getValue();
Chris@70 959 newNote.duration = dialog->getFrameDuration();
Chris@70 960 newNote.label = dialog->getText();
Chris@70 961
Chris@70 962 NoteModel::EditCommand *command = new NoteModel::EditCommand
Chris@70 963 (m_model, tr("Edit Point"));
Chris@70 964 command->deletePoint(note);
Chris@70 965 command->addPoint(newNote);
Chris@376 966 finish(command);
Chris@70 967 }
Chris@70 968
Chris@70 969 delete dialog;
Chris@255 970 return true;
Chris@70 971 }
Chris@70 972
Chris@70 973 void
Chris@43 974 NoteLayer::moveSelection(Selection s, size_t newStartFrame)
Chris@43 975 {
Chris@99 976 if (!m_model) return;
Chris@99 977
Chris@43 978 NoteModel::EditCommand *command =
Chris@43 979 new NoteModel::EditCommand(m_model, tr("Drag Selection"));
Chris@43 980
Chris@43 981 NoteModel::PointList points =
Chris@43 982 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 983
Chris@43 984 for (NoteModel::PointList::iterator i = points.begin();
Chris@43 985 i != points.end(); ++i) {
Chris@43 986
Chris@43 987 if (s.contains(i->frame)) {
Chris@43 988 NoteModel::Point newPoint(*i);
Chris@43 989 newPoint.frame = i->frame + newStartFrame - s.getStartFrame();
Chris@43 990 command->deletePoint(*i);
Chris@43 991 command->addPoint(newPoint);
Chris@43 992 }
Chris@43 993 }
Chris@43 994
Chris@376 995 finish(command);
Chris@43 996 }
Chris@43 997
Chris@43 998 void
Chris@43 999 NoteLayer::resizeSelection(Selection s, Selection newSize)
Chris@43 1000 {
Chris@99 1001 if (!m_model) return;
Chris@99 1002
Chris@43 1003 NoteModel::EditCommand *command =
Chris@43 1004 new NoteModel::EditCommand(m_model, tr("Resize Selection"));
Chris@43 1005
Chris@43 1006 NoteModel::PointList points =
Chris@43 1007 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@43 1008
Chris@43 1009 double ratio =
Chris@43 1010 double(newSize.getEndFrame() - newSize.getStartFrame()) /
Chris@43 1011 double(s.getEndFrame() - s.getStartFrame());
Chris@43 1012
Chris@43 1013 for (NoteModel::PointList::iterator i = points.begin();
Chris@43 1014 i != points.end(); ++i) {
Chris@43 1015
Chris@43 1016 if (s.contains(i->frame)) {
Chris@43 1017
Chris@43 1018 double targetStart = i->frame;
Chris@43 1019 targetStart = newSize.getStartFrame() +
Chris@43 1020 double(targetStart - s.getStartFrame()) * ratio;
Chris@43 1021
Chris@43 1022 double targetEnd = i->frame + i->duration;
Chris@43 1023 targetEnd = newSize.getStartFrame() +
Chris@43 1024 double(targetEnd - s.getStartFrame()) * ratio;
Chris@43 1025
Chris@43 1026 NoteModel::Point newPoint(*i);
Chris@43 1027 newPoint.frame = lrint(targetStart);
Chris@43 1028 newPoint.duration = lrint(targetEnd - targetStart);
Chris@43 1029 command->deletePoint(*i);
Chris@43 1030 command->addPoint(newPoint);
Chris@43 1031 }
Chris@43 1032 }
Chris@43 1033
Chris@376 1034 finish(command);
Chris@43 1035 }
Chris@43 1036
Chris@76 1037 void
Chris@76 1038 NoteLayer::deleteSelection(Selection s)
Chris@76 1039 {
Chris@99 1040 if (!m_model) return;
Chris@99 1041
Chris@76 1042 NoteModel::EditCommand *command =
Chris@76 1043 new NoteModel::EditCommand(m_model, tr("Delete Selected Points"));
Chris@76 1044
Chris@76 1045 NoteModel::PointList points =
Chris@76 1046 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1047
Chris@76 1048 for (NoteModel::PointList::iterator i = points.begin();
Chris@76 1049 i != points.end(); ++i) {
Chris@76 1050
Chris@76 1051 if (s.contains(i->frame)) {
Chris@76 1052 command->deletePoint(*i);
Chris@76 1053 }
Chris@76 1054 }
Chris@76 1055
Chris@376 1056 finish(command);
Chris@76 1057 }
Chris@76 1058
Chris@76 1059 void
Chris@359 1060 NoteLayer::copy(View *v, Selection s, Clipboard &to)
Chris@76 1061 {
Chris@99 1062 if (!m_model) return;
Chris@99 1063
Chris@76 1064 NoteModel::PointList points =
Chris@76 1065 m_model->getPoints(s.getStartFrame(), s.getEndFrame());
Chris@76 1066
Chris@76 1067 for (NoteModel::PointList::iterator i = points.begin();
Chris@76 1068 i != points.end(); ++i) {
Chris@76 1069 if (s.contains(i->frame)) {
Chris@335 1070 Clipboard::Point point(i->frame, i->value, i->duration, i->level, i->label);
Chris@360 1071 point.setReferenceFrame(alignToReference(v, i->frame));
Chris@76 1072 to.addPoint(point);
Chris@76 1073 }
Chris@76 1074 }
Chris@76 1075 }
Chris@76 1076
Chris@125 1077 bool
Chris@359 1078 NoteLayer::paste(View *v, const Clipboard &from, int frameOffset, bool /* interactive */)
Chris@76 1079 {
Chris@125 1080 if (!m_model) return false;
Chris@99 1081
Chris@76 1082 const Clipboard::PointList &points = from.getPoints();
Chris@76 1083
Chris@360 1084 bool realign = false;
Chris@360 1085
Chris@360 1086 if (clipboardHasDifferentAlignment(v, from)) {
Chris@360 1087
Chris@360 1088 QMessageBox::StandardButton button =
Chris@360 1089 QMessageBox::question(v, tr("Re-align pasted items?"),
Chris@360 1090 tr("The items you are pasting came from a layer with different source material from this one. Do you want to re-align them in time, to match the source material for this layer?"),
Chris@360 1091 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
Chris@360 1092 QMessageBox::Yes);
Chris@360 1093
Chris@360 1094 if (button == QMessageBox::Cancel) {
Chris@360 1095 return false;
Chris@360 1096 }
Chris@360 1097
Chris@360 1098 if (button == QMessageBox::Yes) {
Chris@360 1099 realign = true;
Chris@360 1100 }
Chris@360 1101 }
Chris@360 1102
Chris@76 1103 NoteModel::EditCommand *command =
Chris@76 1104 new NoteModel::EditCommand(m_model, tr("Paste"));
Chris@76 1105
Chris@76 1106 for (Clipboard::PointList::const_iterator i = points.begin();
Chris@76 1107 i != points.end(); ++i) {
Chris@76 1108
Chris@76 1109 if (!i->haveFrame()) continue;
Chris@76 1110 size_t frame = 0;
Chris@360 1111
Chris@360 1112 if (!realign) {
Chris@360 1113
Chris@360 1114 frame = i->getFrame();
Chris@360 1115
Chris@360 1116 } else {
Chris@360 1117
Chris@360 1118 if (i->haveReferenceFrame()) {
Chris@360 1119 frame = i->getReferenceFrame();
Chris@360 1120 frame = alignFromReference(v, frame);
Chris@360 1121 } else {
Chris@360 1122 frame = i->getFrame();
Chris@360 1123 }
Chris@76 1124 }
Chris@360 1125
Chris@76 1126 NoteModel::Point newPoint(frame);
Chris@76 1127
Chris@76 1128 if (i->haveLabel()) newPoint.label = i->getLabel();
Chris@76 1129 if (i->haveValue()) newPoint.value = i->getValue();
Chris@76 1130 else newPoint.value = (m_model->getValueMinimum() +
Chris@76 1131 m_model->getValueMaximum()) / 2;
Chris@335 1132 if (i->haveLevel()) newPoint.level = i->getLevel();
Chris@76 1133 if (i->haveDuration()) newPoint.duration = i->getDuration();
Chris@125 1134 else {
Chris@125 1135 size_t nextFrame = frame;
Chris@125 1136 Clipboard::PointList::const_iterator j = i;
Chris@125 1137 for (; j != points.end(); ++j) {
Chris@125 1138 if (!j->haveFrame()) continue;
Chris@125 1139 if (j != i) break;
Chris@125 1140 }
Chris@125 1141 if (j != points.end()) {
Chris@125 1142 nextFrame = j->getFrame();
Chris@125 1143 }
Chris@125 1144 if (nextFrame == frame) {
Chris@125 1145 newPoint.duration = m_model->getResolution();
Chris@125 1146 } else {
Chris@125 1147 newPoint.duration = nextFrame - frame;
Chris@125 1148 }
Chris@125 1149 }
Chris@76 1150
Chris@76 1151 command->addPoint(newPoint);
Chris@76 1152 }
Chris@76 1153
Chris@376 1154 finish(command);
Chris@125 1155 return true;
Chris@76 1156 }
Chris@76 1157
Chris@507 1158 void
Chris@507 1159 NoteLayer::addNoteOn(long frame, int pitch, int velocity)
Chris@507 1160 {
Chris@507 1161 m_pendingNoteOns.insert(Note(frame, pitch, 0, float(velocity) / 127.0, ""));
Chris@507 1162 }
Chris@507 1163
Chris@507 1164 void
Chris@507 1165 NoteLayer::addNoteOff(long frame, int pitch)
Chris@507 1166 {
Chris@507 1167 for (NoteSet::iterator i = m_pendingNoteOns.begin();
Chris@507 1168 i != m_pendingNoteOns.end(); ++i) {
Chris@507 1169 if (lrintf((*i).value) == pitch) {
Chris@507 1170 Note note(*i);
Chris@507 1171 m_pendingNoteOns.erase(i);
Chris@507 1172 note.duration = frame - note.frame;
Chris@507 1173 if (m_model) {
Chris@507 1174 NoteModel::AddPointCommand *c = new NoteModel::AddPointCommand
Chris@507 1175 (m_model, note, tr("Record Note"));
Chris@507 1176 // execute and bundle:
Chris@507 1177 CommandHistory::getInstance()->addCommand(c, true, true);
Chris@507 1178 }
Chris@507 1179 break;
Chris@507 1180 }
Chris@507 1181 }
Chris@507 1182 }
Chris@507 1183
Chris@507 1184 void
Chris@507 1185 NoteLayer::abandonNoteOns()
Chris@507 1186 {
Chris@507 1187 m_pendingNoteOns.clear();
Chris@507 1188 }
Chris@507 1189
Chris@287 1190 int
Chris@287 1191 NoteLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@287 1192 {
Chris@287 1193 impose = false;
Chris@287 1194 return ColourDatabase::getInstance()->getColourIndex
Chris@287 1195 (QString(darkbg ? "White" : "Black"));
Chris@287 1196 }
Chris@287 1197
Chris@316 1198 void
Chris@316 1199 NoteLayer::toXml(QTextStream &stream,
Chris@316 1200 QString indent, QString extraAttributes) const
Chris@30 1201 {
Chris@316 1202 SingleColourLayer::toXml(stream, indent, extraAttributes +
Chris@445 1203 QString(" verticalScale=\"%1\" scaleMinimum=\"%2\" scaleMaximum=\"%3\" ")
Chris@445 1204 .arg(m_verticalScale)
Chris@445 1205 .arg(m_scaleMinimum)
Chris@445 1206 .arg(m_scaleMaximum));
Chris@30 1207 }
Chris@30 1208
Chris@30 1209 void
Chris@30 1210 NoteLayer::setProperties(const QXmlAttributes &attributes)
Chris@30 1211 {
Chris@287 1212 SingleColourLayer::setProperties(attributes);
Chris@30 1213
Chris@445 1214 bool ok, alsoOk;
Chris@30 1215 VerticalScale scale = (VerticalScale)
Chris@30 1216 attributes.value("verticalScale").toInt(&ok);
Chris@30 1217 if (ok) setVerticalScale(scale);
Chris@445 1218
Chris@445 1219 float min = attributes.value("scaleMinimum").toFloat(&ok);
Chris@445 1220 float max = attributes.value("scaleMaximum").toFloat(&alsoOk);
Chris@445 1221 if (ok && alsoOk) setDisplayExtents(min, max);
Chris@30 1222 }
Chris@30 1223
Chris@30 1224