Chris@58: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@30: Chris@30: /* Chris@59: Sonic Visualiser Chris@59: An audio file viewer and annotation editor. Chris@59: Centre for Digital Music, Queen Mary, University of London. Chris@59: This file copyright 2006 Chris Cannam. Chris@30: Chris@59: This program is free software; you can redistribute it and/or Chris@59: modify it under the terms of the GNU General Public License as Chris@59: published by the Free Software Foundation; either version 2 of the Chris@59: License, or (at your option) any later version. See the file Chris@59: COPYING included with this distribution for more information. Chris@30: */ Chris@30: Chris@30: #include "NoteLayer.h" Chris@30: Chris@128: #include "data/model/Model.h" Chris@30: #include "base/RealTime.h" Chris@30: #include "base/Profiler.h" Chris@30: #include "base/Pitch.h" Chris@197: #include "base/LogRange.h" Chris@439: #include "base/RangeMapper.h" Chris@128: #include "view/View.h" Chris@701: Chris@1078: #include "ColourDatabase.h" Chris@692: #include "PianoScale.h" Chris@701: #include "LinearNumericalScale.h" Chris@701: #include "LogNumericalScale.h" Chris@1078: #include "PaintAssistant.h" Chris@30: Chris@128: #include "data/model/NoteModel.h" Chris@30: Chris@70: #include "widgets/ItemEditDialog.h" Chris@701: #include "widgets/TextAbbrev.h" Chris@70: Chris@30: #include Chris@30: #include Chris@30: #include Chris@316: #include Chris@360: #include Chris@30: Chris@30: #include Chris@30: #include Chris@551: #include Chris@30: Chris@665: //#define DEBUG_NOTE_LAYER 1 Chris@665: Chris@44: NoteLayer::NoteLayer() : Chris@287: SingleColourLayer(), Chris@1551: m_modelUsesHz(true), Chris@30: m_editing(false), Chris@845: m_dragPointX(0), Chris@845: m_dragPointY(0), Chris@845: m_dragStartX(0), Chris@845: m_dragStartY(0), Chris@335: m_originalPoint(0, 0.0, 0, 1.f, tr("New Point")), Chris@335: m_editingPoint(0, 0.0, 0, 1.f, tr("New Point")), Chris@1408: m_editingCommand(nullptr), Chris@1422: m_editIsOpen(false), Chris@439: m_verticalScale(AutoAlignScale), Chris@439: m_scaleMinimum(0), Chris@439: m_scaleMaximum(0) Chris@30: { Chris@1315: SVDEBUG << "constructed NoteLayer" << endl; Chris@30: } Chris@30: Chris@1470: int Chris@1470: NoteLayer::getCompletion(LayerGeometryProvider *) const Chris@1470: { Chris@1470: auto model = ModelById::get(m_model); Chris@1470: if (model) return model->getCompletion(); Chris@1470: else return 0; Chris@1470: } Chris@1470: Chris@30: void Chris@1471: NoteLayer::setModel(ModelId modelId) Chris@1471: { Chris@1471: auto newModel = ModelById::getAs(modelId); Chris@1471: Chris@1471: if (!modelId.isNone() && !newModel) { Chris@1471: throw std::logic_error("Not a NoteModel"); Chris@1471: } Chris@1471: Chris@1471: if (m_model == modelId) return; Chris@1471: m_model = modelId; Chris@30: Chris@1471: if (newModel) { Chris@1471: connectSignals(m_model); Chris@1551: Chris@1551: QString unit = newModel->getScaleUnits(); Chris@1551: m_modelUsesHz = (unit.toLower() == "hz"); Chris@1471: } Chris@1471: Chris@439: m_scaleMinimum = 0; Chris@439: m_scaleMaximum = 0; Chris@439: Chris@30: emit modelReplaced(); Chris@30: } Chris@30: Chris@30: Layer::PropertyList Chris@30: NoteLayer::getProperties() const Chris@30: { Chris@287: PropertyList list = SingleColourLayer::getProperties(); Chris@87: list.push_back("Vertical Scale"); Chris@100: list.push_back("Scale Units"); Chris@30: return list; Chris@30: } Chris@30: Chris@87: QString Chris@87: NoteLayer::getPropertyLabel(const PropertyName &name) const Chris@87: { Chris@87: if (name == "Vertical Scale") return tr("Vertical Scale"); Chris@116: if (name == "Scale Units") return tr("Scale Units"); Chris@287: return SingleColourLayer::getPropertyLabel(name); Chris@87: } Chris@87: Chris@30: Layer::PropertyType Chris@100: NoteLayer::getPropertyType(const PropertyName &name) const Chris@30: { Chris@100: if (name == "Scale Units") return UnitsProperty; Chris@287: if (name == "Vertical Scale") return ValueProperty; Chris@287: return SingleColourLayer::getPropertyType(name); Chris@30: } Chris@30: Chris@198: QString Chris@198: NoteLayer::getPropertyGroupName(const PropertyName &name) const Chris@198: { Chris@198: if (name == "Vertical Scale" || name == "Scale Units") { Chris@198: return tr("Scale"); Chris@198: } Chris@287: return SingleColourLayer::getPropertyGroupName(name); Chris@198: } Chris@198: Chris@701: QString Chris@701: NoteLayer::getScaleUnits() const Chris@701: { Chris@1551: return "Hz"; Chris@701: } Chris@701: Chris@30: int Chris@30: NoteLayer::getPropertyRangeAndValue(const PropertyName &name, Chris@216: int *min, int *max, int *deflt) const Chris@30: { Chris@216: int val = 0; Chris@30: Chris@287: if (name == "Vertical Scale") { Chris@1266: Chris@1266: if (min) *min = 0; Chris@1266: if (max) *max = 3; Chris@216: if (deflt) *deflt = int(AutoAlignScale); Chris@1266: Chris@1266: val = int(m_verticalScale); Chris@30: Chris@100: } else if (name == "Scale Units") { Chris@100: Chris@216: if (deflt) *deflt = 0; Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (model) { Chris@216: val = UnitDatabase::getInstance()->getUnitId Chris@1471: (model->getScaleUnits()); Chris@100: } Chris@100: Chris@30: } else { Chris@216: Chris@1266: val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt); Chris@30: } Chris@30: Chris@216: return val; Chris@30: } Chris@30: Chris@30: QString Chris@30: NoteLayer::getPropertyValueLabel(const PropertyName &name, Chris@287: int value) const Chris@30: { Chris@287: if (name == "Vertical Scale") { Chris@1266: switch (value) { Chris@1266: default: Chris@1266: case 0: return tr("Auto-Align"); Chris@1266: case 1: return tr("Linear"); Chris@1266: case 2: return tr("Log"); Chris@1266: case 3: return tr("MIDI Notes"); Chris@1266: } Chris@30: } Chris@287: return SingleColourLayer::getPropertyValueLabel(name, value); Chris@30: } Chris@30: Chris@30: void Chris@30: NoteLayer::setProperty(const PropertyName &name, int value) Chris@30: { Chris@287: if (name == "Vertical Scale") { Chris@1266: setVerticalScale(VerticalScale(value)); Chris@100: } else if (name == "Scale Units") { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (model) { Chris@1551: QString unit = UnitDatabase::getInstance()->getUnitById(value); Chris@1551: model->setScaleUnits(unit); Chris@1551: m_modelUsesHz = (unit.toLower() == "hz"); Chris@1481: emit modelChanged(m_model); Chris@100: } Chris@287: } else { Chris@287: return SingleColourLayer::setProperty(name, value); Chris@30: } Chris@30: } Chris@30: Chris@30: void Chris@30: NoteLayer::setVerticalScale(VerticalScale scale) Chris@30: { Chris@30: if (m_verticalScale == scale) return; Chris@30: m_verticalScale = scale; Chris@30: emit layerParametersChanged(); Chris@30: } Chris@30: Chris@30: bool Chris@918: NoteLayer::isLayerScrollable(const LayerGeometryProvider *v) const Chris@30: { Chris@30: QPoint discard; Chris@44: return !v->shouldIlluminateLocalFeatures(this, discard); Chris@30: } Chris@30: Chris@1551: double Chris@1551: NoteLayer::valueOf(const Event &e) const Chris@101: { Chris@1551: return convertValueFromEventValue(e.getValue()); Chris@1551: } Chris@1551: Chris@1551: Event Chris@1551: NoteLayer::eventWithValue(const Event &e, double value) const Chris@1551: { Chris@1551: return e.withValue(convertValueToEventValue(value)); Chris@1551: } Chris@1551: Chris@1551: double Chris@1551: NoteLayer::convertValueFromEventValue(float eventValue) const Chris@1551: { Chris@1551: if (m_modelUsesHz) { Chris@1551: return eventValue; Chris@1551: } else { Chris@1551: double v = eventValue; Chris@1551: if (v < 0) v = 0; Chris@1551: if (v > 127) v = 127; Chris@1551: int p = int(round(v)); Chris@1551: double c = 100.0 * (v - p); Chris@1551: return Pitch::getFrequencyForPitch(p, c); Chris@1551: } Chris@1551: } Chris@1551: Chris@1551: float Chris@1551: NoteLayer::convertValueToEventValue(double value) const Chris@1551: { Chris@1551: if (m_modelUsesHz) { Chris@1551: return float(value); Chris@1551: } else { Chris@1551: float c = 0; Chris@1551: int p = Pitch::getPitchForFrequency(value, &c); Chris@1551: return float(p) + c / 100.f; Chris@1551: } Chris@101: } Chris@101: Chris@101: bool Chris@905: NoteLayer::getValueExtents(double &min, double &max, Chris@101: bool &logarithmic, QString &unit) const Chris@79: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return false; Chris@101: Chris@1551: min = convertValueFromEventValue(model->getValueMinimum()); Chris@1551: max = convertValueFromEventValue(model->getValueMaximum()); Chris@1551: min /= 1.06; Chris@1551: max *= 1.06; Chris@1551: unit = "Hz"; Chris@101: Chris@1551: if (m_verticalScale != LinearScale) { Chris@1471: logarithmic = true; Chris@1471: } Chris@101: Chris@101: return true; Chris@101: } Chris@101: Chris@101: bool Chris@905: NoteLayer::getDisplayExtents(double &min, double &max) const Chris@101: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || shouldAutoAlign()) return false; Chris@101: Chris@101: if (m_verticalScale == MIDIRangeScale) { Chris@101: min = Pitch::getFrequencyForPitch(0); Chris@101: max = Pitch::getFrequencyForPitch(127); Chris@101: return true; Chris@101: } Chris@101: Chris@439: if (m_scaleMinimum == m_scaleMaximum) { Chris@1551: QString unit; Chris@1551: bool log = false; Chris@1551: getValueExtents(min, max, log, unit); Chris@455: } else { Chris@455: min = m_scaleMinimum; Chris@455: max = m_scaleMaximum; Chris@439: } Chris@439: Chris@667: #ifdef DEBUG_NOTE_LAYER Chris@1551: SVCERR << "NoteLayer::getDisplayExtents: min = " << min << ", max = " << max << " (m_scaleMinimum = " << m_scaleMinimum << ", m_scaleMaximum = " << m_scaleMaximum << ")" << endl; Chris@667: #endif Chris@667: Chris@79: return true; Chris@79: } Chris@79: Chris@439: bool Chris@905: NoteLayer::setDisplayExtents(double min, double max) Chris@439: { Chris@1471: if (m_model.isNone()) return false; Chris@439: Chris@439: if (min == max) { Chris@439: if (min == 0.f) { Chris@439: max = 1.f; Chris@439: } else { Chris@439: max = min * 1.0001; Chris@439: } Chris@439: } Chris@439: Chris@439: m_scaleMinimum = min; Chris@439: m_scaleMaximum = max; Chris@439: Chris@667: #ifdef DEBUG_NOTE_LAYER Chris@1551: SVCERR << "NoteLayer::setDisplayExtents: min = " << min << ", max = " << max << endl; Chris@667: #endif Chris@439: Chris@439: emit layerParametersChanged(); Chris@439: return true; Chris@439: } Chris@439: Chris@439: int Chris@439: NoteLayer::getVerticalZoomSteps(int &defaultStep) const Chris@439: { Chris@1471: if (shouldAutoAlign() || m_model.isNone()) return 0; Chris@439: defaultStep = 0; Chris@439: return 100; Chris@439: } Chris@439: Chris@439: int Chris@439: NoteLayer::getCurrentVerticalZoomStep() const Chris@439: { Chris@1471: if (shouldAutoAlign() || m_model.isNone()) return 0; Chris@439: Chris@439: RangeMapper *mapper = getNewVerticalZoomRangeMapper(); Chris@439: if (!mapper) return 0; Chris@439: Chris@905: double dmin, dmax; Chris@439: getDisplayExtents(dmin, dmax); Chris@439: Chris@439: int nr = mapper->getPositionForValue(dmax - dmin); Chris@439: Chris@439: delete mapper; Chris@439: Chris@439: return 100 - nr; Chris@439: } Chris@439: Chris@439: //!!! lots of duplication with TimeValueLayer Chris@439: Chris@439: void Chris@439: NoteLayer::setVerticalZoomStep(int step) Chris@439: { Chris@1471: if (shouldAutoAlign() || m_model.isNone()) return; Chris@439: Chris@439: RangeMapper *mapper = getNewVerticalZoomRangeMapper(); Chris@439: if (!mapper) return; Chris@439: Chris@905: double min, max; Chris@439: bool logarithmic; Chris@439: QString unit; Chris@439: getValueExtents(min, max, logarithmic, unit); Chris@439: Chris@905: double dmin, dmax; Chris@439: getDisplayExtents(dmin, dmax); Chris@439: Chris@905: double newdist = mapper->getValueForPosition(100 - step); Chris@439: Chris@905: double newmin, newmax; Chris@439: Chris@439: if (logarithmic) { Chris@439: Chris@439: // see SpectrogramLayer::setVerticalZoomStep Chris@439: Chris@905: newmax = (newdist + sqrt(newdist*newdist + 4*dmin*dmax)) / 2; Chris@439: newmin = newmax - newdist; Chris@439: Chris@682: // cerr << "newmin = " << newmin << ", newmax = " << newmax << endl; Chris@439: Chris@439: } else { Chris@905: double dmid = (dmax + dmin) / 2; Chris@439: newmin = dmid - newdist / 2; Chris@439: newmax = dmid + newdist / 2; Chris@439: } Chris@439: Chris@439: if (newmin < min) { Chris@439: newmax += (min - newmin); Chris@439: newmin = min; Chris@439: } Chris@439: if (newmax > max) { Chris@439: newmax = max; Chris@439: } Chris@439: Chris@667: #ifdef DEBUG_NOTE_LAYER Chris@1551: SVCERR << "NoteLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << endl; Chris@667: #endif Chris@439: Chris@439: setDisplayExtents(newmin, newmax); Chris@439: } Chris@439: Chris@439: RangeMapper * Chris@439: NoteLayer::getNewVerticalZoomRangeMapper() const Chris@439: { Chris@1471: if (m_model.isNone()) return nullptr; Chris@439: Chris@439: RangeMapper *mapper; Chris@439: Chris@905: double min, max; Chris@439: bool logarithmic; Chris@439: QString unit; Chris@439: getValueExtents(min, max, logarithmic, unit); Chris@439: Chris@1408: if (min == max) return nullptr; Chris@439: Chris@439: if (logarithmic) { Chris@439: mapper = new LogRangeMapper(0, 100, min, max, unit); Chris@439: } else { Chris@439: mapper = new LinearRangeMapper(0, 100, min, max, unit); Chris@439: } Chris@439: Chris@439: return mapper; Chris@439: } Chris@439: Chris@1424: EventVector Chris@918: NoteLayer::getLocalPoints(LayerGeometryProvider *v, int x) const Chris@30: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return {}; Chris@1424: Chris@905: sv_frame_t frame = v->getFrameForX(x); Chris@30: Chris@1471: EventVector local = model->getEventsCovering(frame); Chris@1424: if (!local.empty()) return local; Chris@30: Chris@1424: int fuzz = ViewManager::scalePixelSize(2); Chris@1424: sv_frame_t start = v->getFrameForX(x - fuzz); Chris@1424: sv_frame_t end = v->getFrameForX(x + fuzz); Chris@30: Chris@1471: local = model->getEventsStartingWithin(frame, end - frame); Chris@1424: if (!local.empty()) return local; Chris@30: Chris@1471: local = model->getEventsSpanning(start, frame - start); Chris@1424: if (!local.empty()) return local; Chris@30: Chris@1424: return {}; Chris@30: } Chris@30: Chris@550: bool Chris@1424: NoteLayer::getPointToDrag(LayerGeometryProvider *v, int x, int y, Event &point) const Chris@550: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return false; Chris@550: Chris@905: sv_frame_t frame = v->getFrameForX(x); Chris@550: Chris@1471: EventVector onPoints = model->getEventsCovering(frame); Chris@550: if (onPoints.empty()) return false; Chris@550: Chris@550: int nearestDistance = -1; Chris@1424: for (const auto &p: onPoints) { Chris@1551: int distance = getYForValue(v, valueOf(p)) - y; Chris@550: if (distance < 0) distance = -distance; Chris@550: if (nearestDistance == -1 || distance < nearestDistance) { Chris@550: nearestDistance = distance; Chris@1424: point = p; Chris@550: } Chris@550: } Chris@550: Chris@550: return true; Chris@550: } Chris@550: Chris@30: QString Chris@918: NoteLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const Chris@30: { Chris@30: int x = pos.x(); Chris@30: Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || !model->getSampleRate()) return ""; Chris@30: Chris@1424: EventVector points = getLocalPoints(v, x); Chris@30: Chris@30: if (points.empty()) { Chris@1471: if (!model->isReady()) { Chris@1266: return tr("In progress"); Chris@1266: } else { Chris@1266: return tr("No local points"); Chris@1266: } Chris@30: } Chris@30: Chris@1424: Event note; Chris@1424: EventVector::iterator i; Chris@30: Chris@30: for (i = points.begin(); i != points.end(); ++i) { Chris@30: Chris@1551: int y = getYForValue(v, valueOf(*i)); Chris@1266: int h = 3; Chris@30: Chris@1471: if (model->getValueQuantization() != 0.0) { Chris@1424: h = y - getYForValue Chris@1551: (v, convertValueFromEventValue(i->getValue() + Chris@1551: model->getValueQuantization())); Chris@1266: if (h < 3) h = 3; Chris@1266: } Chris@30: Chris@1266: if (pos.y() >= y - h && pos.y() <= y) { Chris@1266: note = *i; Chris@1266: break; Chris@1266: } Chris@30: } Chris@30: Chris@30: if (i == points.end()) return tr("No local points"); Chris@30: Chris@1424: RealTime rt = RealTime::frame2RealTime(note.getFrame(), Chris@1471: model->getSampleRate()); Chris@1424: RealTime rd = RealTime::frame2RealTime(note.getDuration(), Chris@1471: model->getSampleRate()); Chris@30: Chris@101: QString pitchText; Chris@101: Chris@1551: if (m_modelUsesHz) { Chris@1551: Chris@1551: float value = note.getValue(); Chris@1424: Chris@544: pitchText = tr("%1 Hz (%2, %3)") Chris@1424: .arg(value) Chris@1424: .arg(Pitch::getPitchLabelForFrequency(value)) Chris@1424: .arg(Pitch::getPitchForFrequency(value)); Chris@101: Chris@101: } else { Chris@1551: Chris@1551: float eventValue = note.getValue(); Chris@1551: double value = convertValueFromEventValue(eventValue); Chris@1551: Chris@1551: int mnote = int(lrint(eventValue)); Chris@1551: int cents = int(lrint((eventValue - float(mnote)) * 100)); Chris@1551: Chris@1551: pitchText = tr("%1 (%2, %3 Hz)") Chris@1551: .arg(Pitch::getPitchLabel(mnote, cents)) Chris@1551: .arg(eventValue) Chris@1551: .arg(value); Chris@101: } Chris@101: Chris@30: QString text; Chris@30: Chris@1424: if (note.getLabel() == "") { Chris@1266: text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nNo label")) Chris@1266: .arg(rt.toText(true).c_str()) Chris@1266: .arg(pitchText) Chris@1266: .arg(rd.toText(true).c_str()); Chris@30: } else { Chris@1266: text = QString(tr("Time:\t%1\nPitch:\t%2\nDuration:\t%3\nLabel:\t%4")) Chris@1266: .arg(rt.toText(true).c_str()) Chris@1266: .arg(pitchText) Chris@1266: .arg(rd.toText(true).c_str()) Chris@1424: .arg(note.getLabel()); Chris@30: } Chris@30: Chris@1551: pos = QPoint(v->getXForFrame(note.getFrame()), Chris@1551: getYForValue(v, valueOf(note))); Chris@30: return text; Chris@30: } Chris@30: Chris@30: bool Chris@918: NoteLayer::snapToFeatureFrame(LayerGeometryProvider *v, sv_frame_t &frame, Chris@1266: int &resolution, Chris@1547: SnapType snap, int ycoord) const Chris@30: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) { Chris@1547: return Layer::snapToFeatureFrame(v, frame, resolution, snap, ycoord); Chris@30: } Chris@30: Chris@1432: // SnapLeft / SnapRight: return frame of nearest feature in that Chris@1432: // direction no matter how far away Chris@1432: // Chris@1432: // SnapNeighbouring: return frame of feature that would be used in Chris@1432: // an editing operation, i.e. closest feature in either direction Chris@1432: // but only if it is "close enough" Chris@1432: Chris@1471: resolution = model->getResolution(); Chris@30: Chris@30: if (snap == SnapNeighbouring) { Chris@1432: EventVector points = getLocalPoints(v, v->getXForFrame(frame)); Chris@1266: if (points.empty()) return false; Chris@1424: frame = points.begin()->getFrame(); Chris@1266: return true; Chris@30: } Chris@30: Chris@1432: Event e; Chris@1471: if (model->getNearestEventMatching Chris@1432: (frame, Chris@1432: [](Event) { return true; }, Chris@1432: snap == SnapLeft ? EventSeries::Backward : EventSeries::Forward, Chris@1432: e)) { Chris@1432: frame = e.getFrame(); Chris@1432: return true; Chris@30: } Chris@30: Chris@1432: return false; Chris@30: } Chris@30: Chris@101: void Chris@918: NoteLayer::getScaleExtents(LayerGeometryProvider *v, double &min, double &max, bool &log) const Chris@30: { Chris@101: min = 0.0; Chris@101: max = 0.0; Chris@101: log = false; Chris@42: Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return; Chris@1471: Chris@439: if (shouldAutoAlign()) { Chris@30: Chris@1551: if (!v->getVisibleExtentsForUnit("Hz", min, max, log)) { Chris@30: Chris@1551: QString unit; Chris@1551: getValueExtents(min, max, log, unit); Chris@42: Chris@665: #ifdef DEBUG_NOTE_LAYER Chris@1551: SVCERR << "NoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << endl; Chris@665: #endif Chris@105: Chris@101: } else if (log) { Chris@101: Chris@197: LogRange::mapRange(min, max); Chris@105: Chris@665: #ifdef DEBUG_NOTE_LAYER Chris@1551: SVCERR << "NoteLayer[" << this << "]::getScaleExtents: min = " << min << ", max = " << max << ", log = " << log << endl; Chris@665: #endif Chris@101: } Chris@101: Chris@101: } else { Chris@101: Chris@439: getDisplayExtents(min, max); Chris@101: Chris@1551: if (m_verticalScale != LinearScale) { Chris@197: LogRange::mapRange(min, max); Chris@101: log = true; Chris@101: } Chris@30: } Chris@30: Chris@101: if (max == min) max = min + 1.0; Chris@101: } Chris@30: Chris@101: int Chris@918: NoteLayer::getYForValue(LayerGeometryProvider *v, double val) const Chris@101: { Chris@905: double min = 0.0, max = 0.0; Chris@101: bool logarithmic = false; Chris@918: int h = v->getPaintHeight(); Chris@101: Chris@101: getScaleExtents(v, min, max, logarithmic); Chris@101: Chris@665: #ifdef DEBUG_NOTE_LAYER Chris@1551: SVCERR << "NoteLayer[" << this << "]::getYForValue(" << val << "): min = " << min << ", max = " << max << ", log = " << logarithmic << endl; Chris@665: #endif Chris@101: Chris@101: if (logarithmic) { Chris@197: val = LogRange::map(val); Chris@665: #ifdef DEBUG_NOTE_LAYER Chris@1551: SVCERR << "logarithmic true, val now = " << val << endl; Chris@665: #endif Chris@101: } Chris@101: Chris@101: int y = int(h - ((val - min) * h) / (max - min)) - 1; Chris@665: #ifdef DEBUG_NOTE_LAYER Chris@1551: SVCERR << "y = " << y << endl; Chris@665: #endif Chris@101: return y; Chris@30: } Chris@30: Chris@905: double Chris@918: NoteLayer::getValueForY(LayerGeometryProvider *v, int y) const Chris@30: { Chris@905: double min = 0.0, max = 0.0; Chris@101: bool logarithmic = false; Chris@918: int h = v->getPaintHeight(); Chris@30: Chris@101: getScaleExtents(v, min, max, logarithmic); Chris@101: Chris@905: double val = min + (double(h - y) * double(max - min)) / h; Chris@101: Chris@101: if (logarithmic) { Chris@905: val = pow(10.0, val); Chris@101: } Chris@101: Chris@101: return val; Chris@30: } Chris@30: Chris@439: bool Chris@439: NoteLayer::shouldAutoAlign() const Chris@439: { Chris@1471: if (m_model.isNone()) return false; Chris@439: return (m_verticalScale == AutoAlignScale); Chris@439: } Chris@439: Chris@30: void Chris@916: NoteLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const Chris@30: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || !model->isOK()) return; Chris@30: Chris@1471: sv_samplerate_t sampleRate = model->getSampleRate(); Chris@30: if (!sampleRate) return; Chris@30: Chris@30: // Profiler profiler("NoteLayer::paint", true); Chris@30: Chris@1550: int x0 = rect.left(); Chris@1550: int x1 = x0 + rect.width(); Chris@1550: Chris@905: sv_frame_t frame0 = v->getFrameForX(x0); Chris@905: sv_frame_t frame1 = v->getFrameForX(x1); Chris@30: Chris@1471: EventVector points(model->getEventsSpanning(frame0, frame1 - frame0)); Chris@30: if (points.empty()) return; Chris@30: Chris@287: paint.setPen(getBaseQColor()); Chris@30: Chris@287: QColor brushColour(getBaseQColor()); Chris@30: brushColour.setAlpha(80); Chris@30: Chris@587: // SVDEBUG << "NoteLayer::paint: resolution is " Chris@1471: // << model->getResolution() << " frames" << endl; Chris@30: Chris@1551: double min = convertValueFromEventValue(model->getValueMinimum()); Chris@1551: double max = convertValueFromEventValue(model->getValueMaximum()); Chris@30: if (max == min) max = min + 1.0; Chris@30: Chris@30: QPoint localPos; Chris@1425: Event illuminatePoint; Chris@551: bool shouldIlluminate = false; Chris@30: Chris@1551: if (m_editing || m_editIsOpen) { Chris@1551: shouldIlluminate = true; Chris@1551: illuminatePoint = m_editingPoint; Chris@1551: } else if (v->shouldIlluminateLocalFeatures(this, localPos)) { Chris@551: shouldIlluminate = getPointToDrag(v, localPos.x(), localPos.y(), Chris@551: illuminatePoint); Chris@30: } Chris@30: Chris@30: paint.save(); Chris@30: paint.setRenderHint(QPainter::Antialiasing, false); Chris@30: Chris@1425: for (EventVector::const_iterator i = points.begin(); Chris@1266: i != points.end(); ++i) { Chris@30: Chris@1425: const Event &p(*i); Chris@30: Chris@1425: int x = v->getXForFrame(p.getFrame()); Chris@1551: int y = getYForValue(v, valueOf(p)); Chris@1425: int w = v->getXForFrame(p.getFrame() + p.getDuration()) - x; Chris@1266: int h = 3; Chris@1266: Chris@1471: if (model->getValueQuantization() != 0.0) { Chris@1551: h = y - getYForValue Chris@1551: (v, convertValueFromEventValue Chris@1551: (p.getValue() + model->getValueQuantization())); Chris@1266: if (h < 3) h = 3; Chris@1266: } Chris@30: Chris@1266: if (w < 1) w = 1; Chris@1266: paint.setPen(getBaseQColor()); Chris@1266: paint.setBrush(brushColour); Chris@30: Chris@1425: if (shouldIlluminate && illuminatePoint == p) { Chris@551: Chris@551: paint.setPen(v->getForeground()); Chris@551: paint.setBrush(v->getForeground()); Chris@551: Chris@1471: // Qt 5.13 deprecates QFontMetrics::width(), but its suggested Chris@1471: // replacement (horizontalAdvance) was only added in Qt 5.11 Chris@1471: // which is too new for us Chris@1471: #pragma GCC diagnostic ignored "-Wdeprecated-declarations" Chris@1471: Chris@1551: QString vlabel; Chris@1551: if (m_modelUsesHz) { Chris@1551: vlabel = QString("%1%2") Chris@1551: .arg(p.getValue()) Chris@1551: .arg(model->getScaleUnits()); Chris@1551: } else { Chris@1551: vlabel = QString("%1 %2") Chris@1551: .arg(p.getValue()) Chris@1551: .arg(model->getScaleUnits()); Chris@1551: } Chris@1551: Chris@1078: PaintAssistant::drawVisibleText(v, paint, Chris@551: x - paint.fontMetrics().width(vlabel) - 2, Chris@551: y + paint.fontMetrics().height()/2 Chris@551: - paint.fontMetrics().descent(), Chris@1078: vlabel, PaintAssistant::OutlinedText); Chris@551: Chris@551: QString hlabel = RealTime::frame2RealTime Chris@1471: (p.getFrame(), model->getSampleRate()).toText(true).c_str(); Chris@1078: PaintAssistant::drawVisibleText(v, paint, Chris@551: x, Chris@551: y - h/2 - paint.fontMetrics().descent() - 2, Chris@1078: hlabel, PaintAssistant::OutlinedText); Chris@1266: } Chris@1266: Chris@1266: paint.drawRect(x, y - h/2, w, h); Chris@30: } Chris@30: Chris@30: paint.restore(); Chris@30: } Chris@30: Chris@692: int Chris@918: NoteLayer::getVerticalScaleWidth(LayerGeometryProvider *v, bool, QPainter &paint) const Chris@692: { Chris@1471: if (m_model.isNone()) { Chris@697: return 0; Chris@1315: } Chris@1315: Chris@1315: if (shouldAutoAlign() && !valueExtentsMatchMine(v)) { Chris@1315: return 0; Chris@1315: } Chris@1315: Chris@1551: if (m_verticalScale != LinearScale) { Chris@1315: return LogNumericalScale().getWidth(v, paint) + 10; // for piano Chris@1315: } else { Chris@1315: return LinearNumericalScale().getWidth(v, paint); Chris@697: } Chris@692: } Chris@692: Chris@692: void Chris@918: NoteLayer::paintVerticalScale(LayerGeometryProvider *v, bool, QPainter &paint, QRect) const Chris@692: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || model->isEmpty()) return; Chris@701: Chris@701: QString unit; Chris@905: double min, max; Chris@701: bool logarithmic; Chris@701: Chris@701: int w = getVerticalScaleWidth(v, false, paint); Chris@918: int h = v->getPaintHeight(); Chris@701: Chris@701: getScaleExtents(v, min, max, logarithmic); Chris@701: Chris@701: if (logarithmic) { Chris@701: LogNumericalScale().paintVertical(v, this, paint, 0, min, max); Chris@701: } else { Chris@701: LinearNumericalScale().paintVertical(v, this, paint, 0, min, max); Chris@701: } Chris@701: Chris@1551: if (logarithmic) { Chris@697: PianoScale().paintPianoVertical Chris@701: (v, paint, QRect(w - 10, 0, 10, h), Chris@701: LogRange::unmap(min), Chris@701: LogRange::unmap(max)); Chris@701: paint.drawLine(w, 0, w, h); Chris@701: } Chris@701: Chris@701: if (getScaleUnits() != "") { Chris@701: int mw = w - 5; Chris@701: paint.drawText(5, Chris@701: 5 + paint.fontMetrics().ascent(), Chris@701: TextAbbrev::abbreviate(getScaleUnits(), Chris@701: paint.fontMetrics(), Chris@701: mw)); Chris@697: } Chris@692: } Chris@692: Chris@30: void Chris@918: NoteLayer::drawStart(LayerGeometryProvider *v, QMouseEvent *e) Chris@30: { Chris@587: // SVDEBUG << "NoteLayer::drawStart(" << e->x() << "," << e->y() << ")" << endl; Chris@30: Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return; Chris@30: Chris@905: sv_frame_t frame = v->getFrameForX(e->x()); Chris@30: if (frame < 0) frame = 0; Chris@1471: frame = frame / model->getResolution() * model->getResolution(); Chris@30: Chris@905: double value = getValueForY(v, e->y()); Chris@1551: float eventValue = convertValueToEventValue(value); Chris@1551: eventValue = roundf(eventValue); Chris@30: Chris@1551: m_editingPoint = Event(frame, eventValue, 0, 0.8f, tr("New Point")); Chris@30: m_originalPoint = m_editingPoint; Chris@30: Chris@376: if (m_editingCommand) finish(m_editingCommand); Chris@1471: m_editingCommand = new ChangeEventsCommand(m_model.untyped, tr("Draw Point")); Chris@1425: m_editingCommand->add(m_editingPoint); Chris@30: Chris@30: m_editing = true; Chris@30: } Chris@30: Chris@30: void Chris@918: NoteLayer::drawDrag(LayerGeometryProvider *v, QMouseEvent *e) Chris@30: { Chris@587: // SVDEBUG << "NoteLayer::drawDrag(" << e->x() << "," << e->y() << ")" << endl; Chris@30: Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || !m_editing) return; Chris@30: Chris@905: sv_frame_t frame = v->getFrameForX(e->x()); Chris@30: if (frame < 0) frame = 0; Chris@1471: frame = frame / model->getResolution() * model->getResolution(); Chris@30: Chris@905: double newValue = getValueForY(v, e->y()); Chris@1551: float newEventValue = convertValueToEventValue(newValue); Chris@1551: newEventValue = roundf(newEventValue); Chris@101: Chris@1425: sv_frame_t newFrame = m_editingPoint.getFrame(); Chris@905: sv_frame_t newDuration = frame - newFrame; Chris@101: if (newDuration < 0) { Chris@101: newFrame = frame; Chris@101: newDuration = -newDuration; Chris@101: } else if (newDuration == 0) { Chris@101: newDuration = 1; Chris@101: } Chris@30: Chris@1425: m_editingCommand->remove(m_editingPoint); Chris@1425: m_editingPoint = m_editingPoint Chris@1425: .withFrame(newFrame) Chris@1551: .withDuration(newDuration) Chris@1551: .withValue(newEventValue); Chris@1425: m_editingCommand->add(m_editingPoint); Chris@30: } Chris@30: Chris@30: void Chris@918: NoteLayer::drawEnd(LayerGeometryProvider *, QMouseEvent *) Chris@30: { Chris@587: // SVDEBUG << "NoteLayer::drawEnd(" << e->x() << "," << e->y() << ")" << endl; Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || !m_editing) return; Chris@376: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; Chris@30: m_editing = false; Chris@30: } Chris@30: Chris@30: void Chris@918: NoteLayer::eraseStart(LayerGeometryProvider *v, QMouseEvent *e) Chris@335: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return; Chris@335: Chris@550: if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return; Chris@335: Chris@335: if (m_editingCommand) { Chris@1266: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; Chris@335: } Chris@335: Chris@335: m_editing = true; Chris@335: } Chris@335: Chris@335: void Chris@918: NoteLayer::eraseDrag(LayerGeometryProvider *, QMouseEvent *) Chris@335: { Chris@335: } Chris@335: Chris@335: void Chris@918: NoteLayer::eraseEnd(LayerGeometryProvider *v, QMouseEvent *e) Chris@335: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || !m_editing) return; Chris@335: Chris@335: m_editing = false; Chris@335: Chris@1425: Event p(0); Chris@550: if (!getPointToDrag(v, e->x(), e->y(), p)) return; Chris@1425: if (p.getFrame() != m_editingPoint.getFrame() || Chris@1425: p.getValue() != m_editingPoint.getValue()) return; Chris@550: Chris@1471: m_editingCommand = new ChangeEventsCommand(m_model.untyped, tr("Erase Point")); Chris@335: Chris@1425: m_editingCommand->remove(m_editingPoint); Chris@335: Chris@376: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; Chris@335: m_editing = false; Chris@335: } Chris@335: Chris@335: void Chris@918: NoteLayer::editStart(LayerGeometryProvider *v, QMouseEvent *e) Chris@30: { Chris@587: // SVDEBUG << "NoteLayer::editStart(" << e->x() << "," << e->y() << ")" << endl; Chris@30: Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return; Chris@30: Chris@550: if (!getPointToDrag(v, e->x(), e->y(), m_editingPoint)) return; Chris@30: m_originalPoint = m_editingPoint; Chris@30: Chris@1425: m_dragPointX = v->getXForFrame(m_editingPoint.getFrame()); Chris@1551: m_dragPointY = getYForValue(v, valueOf(m_editingPoint)); Chris@551: Chris@30: if (m_editingCommand) { Chris@1266: finish(m_editingCommand); Chris@1408: m_editingCommand = nullptr; Chris@30: } Chris@30: Chris@30: m_editing = true; Chris@551: m_dragStartX = e->x(); Chris@551: m_dragStartY = e->y(); Chris@30: } Chris@30: Chris@30: void Chris@918: NoteLayer::editDrag(LayerGeometryProvider *v, QMouseEvent *e) Chris@30: { Chris@587: // SVDEBUG << "NoteLayer::editDrag(" << e->x() << "," << e->y() << ")" << endl; Chris@30: Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || !m_editing) return; Chris@30: Chris@551: int xdist = e->x() - m_dragStartX; Chris@551: int ydist = e->y() - m_dragStartY; Chris@551: int newx = m_dragPointX + xdist; Chris@551: int newy = m_dragPointY + ydist; Chris@551: Chris@905: sv_frame_t frame = v->getFrameForX(newx); Chris@30: if (frame < 0) frame = 0; Chris@1471: frame = frame / model->getResolution() * model->getResolution(); Chris@30: Chris@1551: double newValue = getValueForY(v, newy); Chris@1551: float newEventValue = convertValueToEventValue(newValue); Chris@1551: newEventValue = roundf(newEventValue); Chris@30: Chris@30: if (!m_editingCommand) { Chris@1471: m_editingCommand = new ChangeEventsCommand Chris@1471: (m_model.untyped, tr("Drag Point")); Chris@30: } Chris@30: Chris@1425: m_editingCommand->remove(m_editingPoint); Chris@1425: m_editingPoint = m_editingPoint Chris@1425: .withFrame(frame) Chris@1551: .withValue(newEventValue); Chris@1425: m_editingCommand->add(m_editingPoint); Chris@30: } Chris@30: Chris@30: void Chris@918: NoteLayer::editEnd(LayerGeometryProvider *, QMouseEvent *) Chris@30: { Chris@587: // SVDEBUG << "NoteLayer::editEnd(" << e->x() << "," << e->y() << ")" << endl; Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || !m_editing) return; Chris@30: Chris@30: if (m_editingCommand) { Chris@30: Chris@1266: QString newName = m_editingCommand->getName(); Chris@30: Chris@1425: if (m_editingPoint.getFrame() != m_originalPoint.getFrame()) { Chris@1425: if (m_editingPoint.getValue() != m_originalPoint.getValue()) { Chris@1266: newName = tr("Edit Point"); Chris@1266: } else { Chris@1266: newName = tr("Relocate Point"); Chris@1266: } Chris@1266: } else { Chris@1266: newName = tr("Change Point Value"); Chris@1266: } Chris@30: Chris@1266: m_editingCommand->setName(newName); Chris@1266: finish(m_editingCommand); Chris@30: } Chris@30: Chris@1408: m_editingCommand = nullptr; Chris@30: m_editing = false; Chris@30: } Chris@30: Chris@255: bool Chris@918: NoteLayer::editOpen(LayerGeometryProvider *v, QMouseEvent *e) Chris@70: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return false; Chris@70: Chris@1425: Event note(0); Chris@550: if (!getPointToDrag(v, e->x(), e->y(), note)) return false; Chris@550: Chris@70: ItemEditDialog *dialog = new ItemEditDialog Chris@1471: (model->getSampleRate(), Chris@70: ItemEditDialog::ShowTime | Chris@70: ItemEditDialog::ShowDuration | Chris@70: ItemEditDialog::ShowValue | Chris@1515: ItemEditDialog::ShowLevel | Chris@100: ItemEditDialog::ShowText, Chris@701: getScaleUnits()); Chris@70: Chris@1425: dialog->setFrameTime(note.getFrame()); Chris@1425: dialog->setValue(note.getValue()); Chris@1425: dialog->setFrameDuration(note.getDuration()); Chris@1425: dialog->setText(note.getLabel()); Chris@70: Chris@1422: m_editingPoint = note; Chris@1422: m_editIsOpen = true; Chris@1422: Chris@70: if (dialog->exec() == QDialog::Accepted) { Chris@70: Chris@1425: Event newNote = note Chris@1425: .withFrame(dialog->getFrameTime()) Chris@1425: .withValue(dialog->getValue()) Chris@1425: .withDuration(dialog->getFrameDuration()) Chris@1425: .withLabel(dialog->getText()); Chris@70: Chris@1427: ChangeEventsCommand *command = new ChangeEventsCommand Chris@1471: (m_model.untyped, tr("Edit Point")); Chris@1425: command->remove(note); Chris@1425: command->add(newNote); Chris@376: finish(command); Chris@70: } Chris@70: Chris@1422: m_editingPoint = 0; Chris@1422: m_editIsOpen = false; Chris@1422: Chris@70: delete dialog; Chris@255: return true; Chris@70: } Chris@70: Chris@70: void Chris@905: NoteLayer::moveSelection(Selection s, sv_frame_t newStartFrame) Chris@43: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return; Chris@99: Chris@1427: ChangeEventsCommand *command = Chris@1471: new ChangeEventsCommand(m_model.untyped, tr("Drag Selection")); Chris@43: Chris@1425: EventVector points = Chris@1471: model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); Chris@43: Chris@1425: for (Event p: points) { Chris@1425: command->remove(p); Chris@1425: Event moved = p.withFrame(p.getFrame() + Chris@1425: newStartFrame - s.getStartFrame()); Chris@1425: command->add(moved); Chris@43: } Chris@43: Chris@376: finish(command); Chris@43: } Chris@43: Chris@43: void Chris@43: NoteLayer::resizeSelection(Selection s, Selection newSize) Chris@43: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model || !s.getDuration()) return; Chris@99: Chris@1427: ChangeEventsCommand *command = Chris@1471: new ChangeEventsCommand(m_model.untyped, tr("Resize Selection")); Chris@43: Chris@1425: EventVector points = Chris@1471: model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); Chris@43: Chris@1425: double ratio = double(newSize.getDuration()) / double(s.getDuration()); Chris@1425: double oldStart = double(s.getStartFrame()); Chris@1425: double newStart = double(newSize.getStartFrame()); Chris@1425: Chris@1425: for (Event p: points) { Chris@43: Chris@1425: double newFrame = (double(p.getFrame()) - oldStart) * ratio + newStart; Chris@1425: double newDuration = double(p.getDuration()) * ratio; Chris@43: Chris@1425: Event newPoint = p Chris@1425: .withFrame(lrint(newFrame)) Chris@1425: .withDuration(lrint(newDuration)); Chris@1425: command->remove(p); Chris@1425: command->add(newPoint); Chris@43: } Chris@43: Chris@376: finish(command); Chris@43: } Chris@43: Chris@76: void Chris@76: NoteLayer::deleteSelection(Selection s) Chris@76: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return; Chris@99: Chris@1427: ChangeEventsCommand *command = Chris@1471: new ChangeEventsCommand(m_model.untyped, tr("Delete Selected Points")); Chris@76: Chris@1425: EventVector points = Chris@1471: model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); Chris@76: Chris@1425: for (Event p: points) { Chris@1425: command->remove(p); Chris@76: } Chris@76: Chris@376: finish(command); Chris@76: } Chris@76: Chris@76: void Chris@918: NoteLayer::copy(LayerGeometryProvider *v, Selection s, Clipboard &to) Chris@76: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return; Chris@99: Chris@1425: EventVector points = Chris@1471: model->getEventsStartingWithin(s.getStartFrame(), s.getDuration()); Chris@76: Chris@1425: for (Event p: points) { Chris@1425: to.addPoint(p.withReferenceFrame(alignToReference(v, p.getFrame()))); Chris@76: } Chris@76: } Chris@76: Chris@125: bool Chris@1425: NoteLayer::paste(LayerGeometryProvider *v, const Clipboard &from, Chris@1425: sv_frame_t /* frameOffset */, bool /* interactive */) Chris@76: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: if (!model) return false; Chris@99: Chris@1423: const EventVector &points = from.getPoints(); Chris@76: Chris@360: bool realign = false; Chris@360: Chris@360: if (clipboardHasDifferentAlignment(v, from)) { Chris@360: Chris@360: QMessageBox::StandardButton button = Chris@918: QMessageBox::question(v->getView(), tr("Re-align pasted items?"), Chris@360: 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: QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, Chris@360: QMessageBox::Yes); Chris@360: Chris@360: if (button == QMessageBox::Cancel) { Chris@360: return false; Chris@360: } Chris@360: Chris@360: if (button == QMessageBox::Yes) { Chris@360: realign = true; Chris@360: } Chris@360: } Chris@360: Chris@1427: ChangeEventsCommand *command = Chris@1471: new ChangeEventsCommand(m_model.untyped, tr("Paste")); Chris@76: Chris@1423: for (EventVector::const_iterator i = points.begin(); Chris@76: i != points.end(); ++i) { Chris@1425: Chris@905: sv_frame_t frame = 0; Chris@360: Chris@360: if (!realign) { Chris@360: Chris@360: frame = i->getFrame(); Chris@360: Chris@360: } else { Chris@360: Chris@1423: if (i->hasReferenceFrame()) { Chris@360: frame = i->getReferenceFrame(); Chris@360: frame = alignFromReference(v, frame); Chris@360: } else { Chris@360: frame = i->getFrame(); Chris@360: } Chris@76: } Chris@360: Chris@1533: Event p = i->withFrame(frame); Chris@1533: Chris@1425: Event newPoint = p; Chris@1425: if (!p.hasValue()) { Chris@1471: newPoint = newPoint.withValue((model->getValueMinimum() + Chris@1471: model->getValueMaximum()) / 2); Chris@1425: } Chris@1425: if (!p.hasDuration()) { Chris@905: sv_frame_t nextFrame = frame; Chris@1423: EventVector::const_iterator j = i; Chris@125: for (; j != points.end(); ++j) { Chris@125: if (j != i) break; Chris@125: } Chris@125: if (j != points.end()) { Chris@125: nextFrame = j->getFrame(); Chris@125: } Chris@125: if (nextFrame == frame) { Chris@1471: newPoint = newPoint.withDuration(model->getResolution()); Chris@125: } else { Chris@1425: newPoint = newPoint.withDuration(nextFrame - frame); Chris@125: } Chris@125: } Chris@76: Chris@1425: command->add(newPoint); Chris@76: } Chris@76: Chris@376: finish(command); Chris@125: return true; Chris@76: } Chris@76: Chris@507: void Chris@905: NoteLayer::addNoteOn(sv_frame_t frame, int pitch, int velocity) Chris@507: { Chris@1551: double value = Pitch::getFrequencyForPitch(pitch); Chris@1551: float eventValue = convertValueToEventValue(value); Chris@1551: m_pendingNoteOns.insert(Event(frame, eventValue, 0, Chris@1425: float(velocity) / 127.f, QString())); Chris@507: } Chris@507: Chris@507: void Chris@905: NoteLayer::addNoteOff(sv_frame_t frame, int pitch) Chris@507: { Chris@1471: auto model = ModelById::getAs(m_model); Chris@1471: Chris@507: for (NoteSet::iterator i = m_pendingNoteOns.begin(); Chris@507: i != m_pendingNoteOns.end(); ++i) { Chris@1425: Chris@1425: Event p = *i; Chris@1551: double value = valueOf(p); Chris@1551: int eventPitch = Pitch::getPitchForFrequency(value); Chris@1425: Chris@1551: if (eventPitch == pitch) { Chris@507: m_pendingNoteOns.erase(i); Chris@1425: Event note = p.withDuration(frame - p.getFrame()); Chris@1471: if (model) { Chris@1427: ChangeEventsCommand *c = new ChangeEventsCommand Chris@1471: (m_model.untyped, tr("Record Note")); Chris@1425: c->add(note); Chris@507: // execute and bundle: Chris@507: CommandHistory::getInstance()->addCommand(c, true, true); Chris@507: } Chris@507: break; Chris@507: } Chris@507: } Chris@507: } Chris@507: Chris@507: void Chris@507: NoteLayer::abandonNoteOns() Chris@507: { Chris@507: m_pendingNoteOns.clear(); Chris@507: } Chris@507: Chris@287: int Chris@287: NoteLayer::getDefaultColourHint(bool darkbg, bool &impose) Chris@287: { Chris@287: impose = false; Chris@287: return ColourDatabase::getInstance()->getColourIndex Chris@287: (QString(darkbg ? "White" : "Black")); Chris@287: } Chris@287: Chris@316: void Chris@316: NoteLayer::toXml(QTextStream &stream, Chris@316: QString indent, QString extraAttributes) const Chris@30: { Chris@316: SingleColourLayer::toXml(stream, indent, extraAttributes + Chris@445: QString(" verticalScale=\"%1\" scaleMinimum=\"%2\" scaleMaximum=\"%3\" ") Chris@445: .arg(m_verticalScale) Chris@445: .arg(m_scaleMinimum) Chris@445: .arg(m_scaleMaximum)); Chris@30: } Chris@30: Chris@30: void Chris@30: NoteLayer::setProperties(const QXmlAttributes &attributes) Chris@30: { Chris@287: SingleColourLayer::setProperties(attributes); Chris@30: Chris@445: bool ok, alsoOk; Chris@30: VerticalScale scale = (VerticalScale) Chris@1266: attributes.value("verticalScale").toInt(&ok); Chris@30: if (ok) setVerticalScale(scale); Chris@445: Chris@445: float min = attributes.value("scaleMinimum").toFloat(&ok); Chris@445: float max = attributes.value("scaleMaximum").toFloat(&alsoOk); Chris@667: if (ok && alsoOk && min != max) setDisplayExtents(min, max); Chris@30: } Chris@30: Chris@30: