annotate data/model/RegionModel.h @ 441:288f45533041

* Add region model and layer; improve assignment of model types to feature extraction transforms with duration
author Chris Cannam
date Thu, 18 Sep 2008 16:08:14 +0000
parents
children 04b7fd31e1c6
rev   line source
Chris@441 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@441 2
Chris@441 3 /*
Chris@441 4 Sonic Visualiser
Chris@441 5 An audio file viewer and annotation editor.
Chris@441 6 Centre for Digital Music, Queen Mary, University of London.
Chris@441 7 This file copyright 2006 Chris Cannam.
Chris@441 8
Chris@441 9 This program is free software; you can redistribute it and/or
Chris@441 10 modify it under the terms of the GNU General Public License as
Chris@441 11 published by the Free Software Foundation; either version 2 of the
Chris@441 12 License, or (at your option) any later version. See the file
Chris@441 13 COPYING included with this distribution for more information.
Chris@441 14 */
Chris@441 15
Chris@441 16 #ifndef _REGION_MODEL_H_
Chris@441 17 #define _REGION_MODEL_H_
Chris@441 18
Chris@441 19 #include "IntervalModel.h"
Chris@441 20 #include "base/RealTime.h"
Chris@441 21
Chris@441 22 /**
Chris@441 23 * RegionModel -- a concrete IntervalModel for intervals associated
Chris@441 24 * with a value, which we call regions for no very compelling reason.
Chris@441 25 */
Chris@441 26
Chris@441 27 /**
Chris@441 28 * Region "point" type. A region is something that has an onset time,
Chris@441 29 * a single value, and a duration. Like other points, it can also
Chris@441 30 * have a label.
Chris@441 31 *
Chris@441 32 * This is called RegionRec instead of Region to avoid name collisions
Chris@441 33 * with the X11 Region struct. Bah.
Chris@441 34 */
Chris@441 35
Chris@441 36 struct RegionRec
Chris@441 37 {
Chris@441 38 public:
Chris@441 39 RegionRec(long _frame) : frame(_frame), value(0.0f), duration(0) { }
Chris@441 40 RegionRec(long _frame, float _value, size_t _duration, QString _label) :
Chris@441 41 frame(_frame), value(_value), duration(_duration), label(_label) { }
Chris@441 42
Chris@441 43 int getDimensions() const { return 3; }
Chris@441 44
Chris@441 45 long frame;
Chris@441 46 float value;
Chris@441 47 size_t duration;
Chris@441 48 QString label;
Chris@441 49
Chris@441 50 QString getLabel() const { return label; }
Chris@441 51
Chris@441 52 void toXml(QTextStream &stream,
Chris@441 53 QString indent = "",
Chris@441 54 QString extraAttributes = "") const
Chris@441 55 {
Chris@441 56 stream <<
Chris@441 57 QString("%1<point frame=\"%2\" value=\"%3\" duration=\"%4\" label=\"%5\" %6/>\n")
Chris@441 58 .arg(indent).arg(frame).arg(value).arg(duration).arg(label).arg(extraAttributes);
Chris@441 59 }
Chris@441 60
Chris@441 61 QString toDelimitedDataString(QString delimiter, size_t sampleRate) const
Chris@441 62 {
Chris@441 63 QStringList list;
Chris@441 64 list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
Chris@441 65 list << QString("%1").arg(value);
Chris@441 66 list << RealTime::frame2RealTime(duration, sampleRate).toString().c_str();
Chris@441 67 if (label != "") list << label;
Chris@441 68 return list.join(delimiter);
Chris@441 69 }
Chris@441 70
Chris@441 71 struct Comparator {
Chris@441 72 bool operator()(const RegionRec &p1,
Chris@441 73 const RegionRec &p2) const {
Chris@441 74 if (p1.frame != p2.frame) return p1.frame < p2.frame;
Chris@441 75 if (p1.value != p2.value) return p1.value < p2.value;
Chris@441 76 if (p1.duration != p2.duration) return p1.duration < p2.duration;
Chris@441 77 return p1.label < p2.label;
Chris@441 78 }
Chris@441 79 };
Chris@441 80
Chris@441 81 struct OrderComparator {
Chris@441 82 bool operator()(const RegionRec &p1,
Chris@441 83 const RegionRec &p2) const {
Chris@441 84 return p1.frame < p2.frame;
Chris@441 85 }
Chris@441 86 };
Chris@441 87 };
Chris@441 88
Chris@441 89
Chris@441 90 class RegionModel : public IntervalModel<RegionRec>
Chris@441 91 {
Chris@441 92 Q_OBJECT
Chris@441 93
Chris@441 94 public:
Chris@441 95 RegionModel(size_t sampleRate, size_t resolution,
Chris@441 96 bool notifyOnAdd = true) :
Chris@441 97 IntervalModel<RegionRec>(sampleRate, resolution, notifyOnAdd),
Chris@441 98 m_valueQuantization(0)
Chris@441 99 {
Chris@441 100 }
Chris@441 101
Chris@441 102 RegionModel(size_t sampleRate, size_t resolution,
Chris@441 103 float valueMinimum, float valueMaximum,
Chris@441 104 bool notifyOnAdd = true) :
Chris@441 105 IntervalModel<RegionRec>(sampleRate, resolution,
Chris@441 106 valueMinimum, valueMaximum,
Chris@441 107 notifyOnAdd),
Chris@441 108 m_valueQuantization(0)
Chris@441 109 {
Chris@441 110 }
Chris@441 111
Chris@441 112 virtual ~RegionModel()
Chris@441 113 {
Chris@441 114 }
Chris@441 115
Chris@441 116 float getValueQuantization() const { return m_valueQuantization; }
Chris@441 117 void setValueQuantization(float q) { m_valueQuantization = q; }
Chris@441 118
Chris@441 119 QString getTypeName() const { return tr("Region"); }
Chris@441 120
Chris@441 121 virtual void toXml(QTextStream &out,
Chris@441 122 QString indent = "",
Chris@441 123 QString extraAttributes = "") const
Chris@441 124 {
Chris@441 125 std::cerr << "RegionModel::toXml: extraAttributes = \""
Chris@441 126 << extraAttributes.toStdString() << std::endl;
Chris@441 127
Chris@441 128 IntervalModel<RegionRec>::toXml
Chris@441 129 (out,
Chris@441 130 indent,
Chris@441 131 QString("%1 valueQuantization=\"%2\"")
Chris@441 132 .arg(extraAttributes).arg(m_valueQuantization));
Chris@441 133 }
Chris@441 134
Chris@441 135 /**
Chris@441 136 * TabularModel methods.
Chris@441 137 */
Chris@441 138
Chris@441 139 virtual int getColumnCount() const
Chris@441 140 {
Chris@441 141 return 6;
Chris@441 142 }
Chris@441 143
Chris@441 144 virtual QString getHeading(int column) const
Chris@441 145 {
Chris@441 146 switch (column) {
Chris@441 147 case 0: return tr("Time");
Chris@441 148 case 1: return tr("Frame");
Chris@441 149 case 2: return tr("Value");
Chris@441 150 case 3: return tr("Duration");
Chris@441 151 case 4: return tr("Label");
Chris@441 152 default: return tr("Unknown");
Chris@441 153 }
Chris@441 154 }
Chris@441 155
Chris@441 156 virtual QVariant getData(int row, int column, int role) const
Chris@441 157 {
Chris@441 158 if (column < 4) {
Chris@441 159 return IntervalModel<RegionRec>::getData(row, column, role);
Chris@441 160 }
Chris@441 161
Chris@441 162 PointListIterator i = getPointListIteratorForRow(row);
Chris@441 163 if (i == m_points.end()) return QVariant();
Chris@441 164
Chris@441 165 switch (column) {
Chris@441 166 case 4: return i->label;
Chris@441 167 default: return QVariant();
Chris@441 168 }
Chris@441 169 }
Chris@441 170
Chris@441 171 virtual Command *getSetDataCommand(int row, int column, const QVariant &value, int role)
Chris@441 172 {
Chris@441 173 if (column < 4) {
Chris@441 174 return IntervalModel<RegionRec>::getSetDataCommand
Chris@441 175 (row, column, value, role);
Chris@441 176 }
Chris@441 177
Chris@441 178 if (role != Qt::EditRole) return false;
Chris@441 179 PointListIterator i = getPointListIteratorForRow(row);
Chris@441 180 if (i == m_points.end()) return false;
Chris@441 181 EditCommand *command = new EditCommand(this, tr("Edit Data"));
Chris@441 182
Chris@441 183 Point point(*i);
Chris@441 184 command->deletePoint(point);
Chris@441 185
Chris@441 186 switch (column) {
Chris@441 187 case 4: point.label = value.toString(); break;
Chris@441 188 }
Chris@441 189
Chris@441 190 command->addPoint(point);
Chris@441 191 return command->finish();
Chris@441 192 }
Chris@441 193
Chris@441 194 virtual SortType getSortType(int column) const
Chris@441 195 {
Chris@441 196 if (column == 5) return SortAlphabetical;
Chris@441 197 return SortNumeric;
Chris@441 198 }
Chris@441 199
Chris@441 200 protected:
Chris@441 201 float m_valueQuantization;
Chris@441 202 };
Chris@441 203
Chris@441 204 #endif