annotate data/model/SparseOneDimensionalModel.h @ 1612:23a29e5dc0e9 single-point

Start implementing & testing PointSeries
author Chris Cannam
date Wed, 06 Mar 2019 16:24:23 +0000
parents 560453546749
children 24dc8cb42755
rev   line source
Chris@147 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@147 2
Chris@147 3 /*
Chris@147 4 Sonic Visualiser
Chris@147 5 An audio file viewer and annotation editor.
Chris@147 6 Centre for Digital Music, Queen Mary, University of London.
Chris@147 7 This file copyright 2006 Chris Cannam.
Chris@147 8
Chris@147 9 This program is free software; you can redistribute it and/or
Chris@147 10 modify it under the terms of the GNU General Public License as
Chris@147 11 published by the Free Software Foundation; either version 2 of the
Chris@147 12 License, or (at your option) any later version. See the file
Chris@147 13 COPYING included with this distribution for more information.
Chris@147 14 */
Chris@147 15
Chris@1581 16 #ifndef SV_SPARSE_ONE_DIMENSIONAL_MODEL_H
Chris@1581 17 #define SV_SPARSE_ONE_DIMENSIONAL_MODEL_H
Chris@147 18
Chris@147 19 #include "SparseModel.h"
Chris@852 20 #include "NoteData.h"
Chris@150 21 #include "base/PlayParameterRepository.h"
Chris@147 22 #include "base/RealTime.h"
Chris@147 23
Chris@387 24 #include <QStringList>
Chris@387 25
Chris@147 26 struct OneDimensionalPoint
Chris@147 27 {
Chris@147 28 public:
Chris@1038 29 OneDimensionalPoint(sv_frame_t _frame) : frame(_frame) { }
Chris@1038 30 OneDimensionalPoint(sv_frame_t _frame, QString _label) : frame(_frame), label(_label) { }
Chris@147 31
Chris@147 32 int getDimensions() const { return 1; }
Chris@147 33
Chris@1038 34 sv_frame_t frame;
Chris@147 35 QString label;
Chris@338 36
Chris@338 37 QString getLabel() const { return label; }
Chris@338 38
Chris@314 39 void toXml(QTextStream &stream,
Chris@314 40 QString indent = "",
Chris@314 41 QString extraAttributes = "") const
Chris@147 42 {
Chris@314 43 stream << QString("%1<point frame=\"%2\" label=\"%3\" %4/>\n")
Chris@1429 44 .arg(indent).arg(frame).arg(XmlExportable::encodeEntities(label))
Chris@603 45 .arg(extraAttributes);
Chris@147 46 }
Chris@147 47
Chris@1060 48 QString toDelimitedDataString(QString delimiter, DataExportOptions, sv_samplerate_t sampleRate) const
Chris@147 49 {
Chris@147 50 QStringList list;
Chris@147 51 list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
Chris@318 52 if (label != "") list << label;
Chris@147 53 return list.join(delimiter);
Chris@147 54 }
Chris@147 55
Chris@147 56 struct Comparator {
Chris@1429 57 bool operator()(const OneDimensionalPoint &p1,
Chris@1429 58 const OneDimensionalPoint &p2) const {
Chris@1429 59 if (p1.frame != p2.frame) return p1.frame < p2.frame;
Chris@1429 60 return p1.label < p2.label;
Chris@1429 61 }
Chris@147 62 };
Chris@147 63
Chris@147 64 struct OrderComparator {
Chris@1429 65 bool operator()(const OneDimensionalPoint &p1,
Chris@1429 66 const OneDimensionalPoint &p2) const {
Chris@1429 67 return p1.frame < p2.frame;
Chris@1429 68 }
Chris@147 69 };
Chris@1609 70
Chris@1609 71 bool operator==(const OneDimensionalPoint &p) const {
Chris@1609 72 return (frame == p.frame && label == p.label);
Chris@1609 73 }
Chris@147 74 };
Chris@147 75
Chris@147 76
Chris@852 77 class SparseOneDimensionalModel : public SparseModel<OneDimensionalPoint>,
Chris@852 78 public NoteExportable
Chris@147 79 {
Chris@423 80 Q_OBJECT
Chris@423 81
Chris@147 82 public:
Chris@1040 83 SparseOneDimensionalModel(sv_samplerate_t sampleRate, int resolution,
Chris@1429 84 bool notifyOnAdd = true) :
Chris@1429 85 SparseModel<OneDimensionalPoint>(sampleRate, resolution, notifyOnAdd)
Chris@147 86 {
Chris@1429 87 PlayParameterRepository::getInstance()->addPlayable(this);
Chris@391 88 }
Chris@391 89
Chris@391 90 virtual ~SparseOneDimensionalModel()
Chris@391 91 {
Chris@391 92 PlayParameterRepository::getInstance()->removePlayable(this);
Chris@391 93 }
Chris@391 94
Chris@1580 95 bool canPlay() const override { return true; }
Chris@391 96
Chris@1580 97 QString getDefaultPlayClipId() const override
Chris@391 98 {
Chris@864 99 return "tap";
Chris@147 100 }
Chris@147 101
Chris@420 102 int getIndexOf(const Point &point)
Chris@420 103 {
Chris@1429 104 // slow
Chris@1429 105 int i = 0;
Chris@1429 106 Point::Comparator comparator;
Chris@1429 107 for (PointList::const_iterator j = m_points.begin();
Chris@1429 108 j != m_points.end(); ++j, ++i) {
Chris@1429 109 if (!comparator(*j, point) && !comparator(point, *j)) return i;
Chris@1429 110 }
Chris@1429 111 return -1;
Chris@147 112 }
Chris@345 113
Chris@1580 114 QString getTypeName() const override { return tr("Sparse 1-D"); }
Chris@420 115
Chris@420 116 /**
Chris@420 117 * TabularModel methods.
Chris@420 118 */
Chris@420 119
Chris@1580 120 int getColumnCount() const override
Chris@420 121 {
Chris@420 122 return 3;
Chris@420 123 }
Chris@420 124
Chris@1580 125 QString getHeading(int column) const override
Chris@420 126 {
Chris@420 127 switch (column) {
Chris@420 128 case 0: return tr("Time");
Chris@420 129 case 1: return tr("Frame");
Chris@420 130 case 2: return tr("Label");
Chris@420 131 default: return tr("Unknown");
Chris@420 132 }
Chris@420 133 }
Chris@420 134
Chris@1580 135 QVariant getData(int row, int column, int role) const override
Chris@420 136 {
Chris@425 137 if (column < 2) {
Chris@425 138 return SparseModel<OneDimensionalPoint>::getData
Chris@425 139 (row, column, role);
Chris@425 140 }
Chris@425 141
Chris@606 142 PointListConstIterator i = getPointListIteratorForRow(row);
Chris@420 143 if (i == m_points.end()) return QVariant();
Chris@420 144
Chris@420 145 switch (column) {
Chris@422 146 case 2: return i->label;
Chris@420 147 default: return QVariant();
Chris@420 148 }
Chris@420 149 }
Chris@420 150
Chris@1580 151 Command *getSetDataCommand(int row, int column, const QVariant &value, int role) override
Chris@424 152 {
Chris@425 153 if (column < 2) {
Chris@425 154 return SparseModel<OneDimensionalPoint>::getSetDataCommand
Chris@425 155 (row, column, value, role);
Chris@425 156 }
Chris@425 157
Chris@740 158 if (role != Qt::EditRole) return 0;
Chris@606 159 PointListConstIterator i = getPointListIteratorForRow(row);
Chris@740 160 if (i == m_points.end()) return 0;
Chris@424 161 EditCommand *command = new EditCommand(this, tr("Edit Data"));
Chris@424 162
Chris@424 163 Point point(*i);
Chris@424 164 command->deletePoint(point);
Chris@424 165
Chris@424 166 switch (column) {
Chris@424 167 case 2: point.label = value.toString(); break;
Chris@424 168 }
Chris@424 169
Chris@424 170 command->addPoint(point);
Chris@424 171 return command->finish();
Chris@424 172 }
Chris@424 173
Chris@427 174
Chris@1580 175 bool isColumnTimeValue(int column) const override
Chris@420 176 {
Chris@420 177 return (column < 2);
Chris@420 178 }
Chris@422 179
Chris@1580 180 SortType getSortType(int column) const override
Chris@422 181 {
Chris@422 182 if (column == 2) return SortAlphabetical;
Chris@422 183 return SortNumeric;
Chris@422 184 }
Chris@852 185
Chris@852 186 /**
Chris@852 187 * NoteExportable methods.
Chris@852 188 */
Chris@852 189
Chris@1580 190 NoteList getNotes() const override {
Chris@929 191 return getNotesWithin(getStartFrame(), getEndFrame());
Chris@852 192 }
Chris@852 193
Chris@1580 194 NoteList getNotesWithin(sv_frame_t startFrame, sv_frame_t endFrame) const override {
Chris@852 195
Chris@1429 196 PointList points = getPoints(startFrame, endFrame);
Chris@852 197 NoteList notes;
Chris@852 198
Chris@1429 199 for (PointList::iterator pli =
Chris@1429 200 points.begin(); pli != points.end(); ++pli) {
Chris@852 201
Chris@852 202 notes.push_back
Chris@852 203 (NoteData(pli->frame,
Chris@1040 204 sv_frame_t(getSampleRate() / 6), // arbitrary short duration
Chris@852 205 64, // default pitch
Chris@852 206 100)); // default velocity
Chris@852 207 }
Chris@852 208
Chris@852 209 return notes;
Chris@852 210 }
Chris@147 211 };
Chris@147 212
Chris@147 213 #endif
Chris@147 214
Chris@147 215
Chris@147 216