comparison data/model/FlexiNoteModel.h @ 773:2079abf4f0c1 tonioni

make a new FlexiNoteModel.h file by copying from NoteModel.h -- will need to change names therein
author matthiasm
date Tue, 26 Mar 2013 14:16:54 +0000
parents data/model/NoteModel.h@e22b6e89a7f7
children d3df9f50b188
comparison
equal deleted inserted replaced
772:f3df2c35e6cd 773:2079abf4f0c1
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #ifndef _NOTE_MODEL_H_
17 #define _NOTE_MODEL_H_
18
19 #include "IntervalModel.h"
20 #include "base/RealTime.h"
21 #include "base/PlayParameterRepository.h"
22
23 /**
24 * NoteModel -- a concrete IntervalModel for notes.
25 */
26
27 /**
28 * Note type for use in a sparse model. All we mean by a "note" is
29 * something that has an onset time, a single value, a duration, and a
30 * level. Like other points, it can also have a label. With this
31 * point type, the model can be thought of as representing a simple
32 * MIDI-type piano roll, except that the y coordinates (values) do not
33 * have to be discrete integers.
34 */
35
36 struct Note
37 {
38 public:
39 Note(long _frame) : frame(_frame), value(0.0f), duration(0), level(1.f) { }
40 Note(long _frame, float _value, size_t _duration, float _level, QString _label) :
41 frame(_frame), value(_value), duration(_duration), level(_level), label(_label) { }
42
43 int getDimensions() const { return 3; }
44
45 long frame;
46 float value;
47 size_t duration;
48 float level;
49 QString label;
50
51 QString getLabel() const { return label; }
52
53 void toXml(QTextStream &stream,
54 QString indent = "",
55 QString extraAttributes = "") const
56 {
57 stream <<
58 QString("%1<point frame=\"%2\" value=\"%3\" duration=\"%4\" level=\"%5\" label=\"%6\" %7/>\n")
59 .arg(indent).arg(frame).arg(value).arg(duration).arg(level)
60 .arg(XmlExportable::encodeEntities(label)).arg(extraAttributes);
61 }
62
63 QString toDelimitedDataString(QString delimiter, size_t sampleRate) const
64 {
65 QStringList list;
66 list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
67 list << QString("%1").arg(value);
68 list << RealTime::frame2RealTime(duration, sampleRate).toString().c_str();
69 list << QString("%1").arg(level);
70 if (label != "") list << label;
71 return list.join(delimiter);
72 }
73
74 struct Comparator {
75 bool operator()(const Note &p1,
76 const Note &p2) const {
77 if (p1.frame != p2.frame) return p1.frame < p2.frame;
78 if (p1.value != p2.value) return p1.value < p2.value;
79 if (p1.duration != p2.duration) return p1.duration < p2.duration;
80 if (p1.level != p2.level) return p1.level < p2.level;
81 return p1.label < p2.label;
82 }
83 };
84
85 struct OrderComparator {
86 bool operator()(const Note &p1,
87 const Note &p2) const {
88 return p1.frame < p2.frame;
89 }
90 };
91 };
92
93
94 class NoteModel : public IntervalModel<Note>
95 {
96 Q_OBJECT
97
98 public:
99 NoteModel(size_t sampleRate, size_t resolution,
100 bool notifyOnAdd = true) :
101 IntervalModel<Note>(sampleRate, resolution, notifyOnAdd),
102 m_valueQuantization(0)
103 {
104 PlayParameterRepository::getInstance()->addPlayable(this);
105 }
106
107 NoteModel(size_t sampleRate, size_t resolution,
108 float valueMinimum, float valueMaximum,
109 bool notifyOnAdd = true) :
110 IntervalModel<Note>(sampleRate, resolution,
111 valueMinimum, valueMaximum,
112 notifyOnAdd),
113 m_valueQuantization(0)
114 {
115 PlayParameterRepository::getInstance()->addPlayable(this);
116 }
117
118 virtual ~NoteModel()
119 {
120 PlayParameterRepository::getInstance()->removePlayable(this);
121 }
122
123 float getValueQuantization() const { return m_valueQuantization; }
124 void setValueQuantization(float q) { m_valueQuantization = q; }
125
126 QString getTypeName() const { return tr("Note"); }
127
128 virtual bool canPlay() const { return true; }
129
130 virtual QString getDefaultPlayPluginId() const
131 {
132 return "dssi:_builtin:sample_player";
133 }
134
135 virtual QString getDefaultPlayPluginConfiguration() const
136 {
137 return "<plugin program=\"piano\"/>";
138 }
139
140 virtual void toXml(QTextStream &out,
141 QString indent = "",
142 QString extraAttributes = "") const
143 {
144 std::cerr << "NoteModel::toXml: extraAttributes = \""
145 << extraAttributes.toStdString() << std::endl;
146
147 IntervalModel<Note>::toXml
148 (out,
149 indent,
150 QString("%1 subtype=\"note\" valueQuantization=\"%2\"")
151 .arg(extraAttributes).arg(m_valueQuantization));
152 }
153
154 /**
155 * TabularModel methods.
156 */
157
158 virtual int getColumnCount() const
159 {
160 return 6;
161 }
162
163 virtual QString getHeading(int column) const
164 {
165 switch (column) {
166 case 0: return tr("Time");
167 case 1: return tr("Frame");
168 case 2: return tr("Pitch");
169 case 3: return tr("Duration");
170 case 4: return tr("Level");
171 case 5: return tr("Label");
172 default: return tr("Unknown");
173 }
174 }
175
176 virtual QVariant getData(int row, int column, int role) const
177 {
178 if (column < 4) {
179 return IntervalModel<Note>::getData(row, column, role);
180 }
181
182 PointListConstIterator i = getPointListIteratorForRow(row);
183 if (i == m_points.end()) return QVariant();
184
185 switch (column) {
186 case 4: return i->level;
187 case 5: return i->label;
188 default: return QVariant();
189 }
190 }
191
192 virtual Command *getSetDataCommand(int row, int column, const QVariant &value, int role)
193 {
194 if (column < 4) {
195 return IntervalModel<Note>::getSetDataCommand
196 (row, column, value, role);
197 }
198
199 if (role != Qt::EditRole) return 0;
200 PointListConstIterator i = getPointListIteratorForRow(row);
201 if (i == m_points.end()) return 0;
202 EditCommand *command = new EditCommand(this, tr("Edit Data"));
203
204 Point point(*i);
205 command->deletePoint(point);
206
207 switch (column) {
208 case 4: point.level = value.toDouble(); break;
209 case 5: point.label = value.toString(); break;
210 }
211
212 command->addPoint(point);
213 return command->finish();
214 }
215
216 virtual SortType getSortType(int column) const
217 {
218 if (column == 5) return SortAlphabetical;
219 return SortNumeric;
220 }
221
222 protected:
223 float m_valueQuantization;
224 };
225
226 #endif