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
|
Chris@147
|
8 This program is free software; you can redistribute it and/or
|
Chris@147
|
9 modify it under the terms of the GNU General Public License as
|
Chris@147
|
10 published by the Free Software Foundation; either version 2 of the
|
Chris@147
|
11 License, or (at your option) any later version. See the file
|
Chris@147
|
12 COPYING included with this distribution for more information.
|
Chris@147
|
13 */
|
Chris@147
|
14
|
Chris@1495
|
15 #ifndef SV_NOTE_MODEL_H
|
Chris@1495
|
16 #define SV_NOTE_MODEL_H
|
Chris@147
|
17
|
Chris@1643
|
18 #include "Model.h"
|
Chris@1643
|
19 #include "TabularModel.h"
|
Chris@1648
|
20 #include "EventCommands.h"
|
Chris@1651
|
21 #include "DeferredNotifier.h"
|
Chris@1643
|
22 #include "base/UnitDatabase.h"
|
Chris@1643
|
23 #include "base/EventSeries.h"
|
Chris@1615
|
24 #include "base/NoteData.h"
|
Chris@1643
|
25 #include "base/NoteExportable.h"
|
Chris@391
|
26 #include "base/RealTime.h"
|
Chris@150
|
27 #include "base/PlayParameterRepository.h"
|
Chris@852
|
28 #include "base/Pitch.h"
|
Chris@1643
|
29 #include "system/System.h"
|
Chris@147
|
30
|
Chris@1643
|
31 #include <QMutex>
|
Chris@1643
|
32 #include <QMutexLocker>
|
Chris@441
|
33
|
Chris@1643
|
34 class NoteModel : public Model,
|
Chris@1643
|
35 public TabularModel,
|
Chris@1648
|
36 public NoteExportable,
|
Chris@1648
|
37 public EventEditable
|
Chris@147
|
38 {
|
Chris@423
|
39 Q_OBJECT
|
Chris@423
|
40
|
Chris@147
|
41 public:
|
Chris@1647
|
42 enum Subtype {
|
Chris@1647
|
43 NORMAL_NOTE,
|
Chris@1647
|
44 FLEXI_NOTE
|
Chris@1647
|
45 };
|
Chris@1647
|
46
|
Chris@1644
|
47 NoteModel(sv_samplerate_t sampleRate,
|
Chris@1644
|
48 int resolution,
|
Chris@1647
|
49 bool notifyOnAdd = true,
|
Chris@1647
|
50 Subtype subtype = NORMAL_NOTE) :
|
Chris@1647
|
51 m_subtype(subtype),
|
Chris@1643
|
52 m_sampleRate(sampleRate),
|
Chris@1643
|
53 m_resolution(resolution),
|
Chris@1643
|
54 m_valueMinimum(0.f),
|
Chris@1643
|
55 m_valueMaximum(0.f),
|
Chris@1643
|
56 m_haveExtents(false),
|
Chris@1643
|
57 m_valueQuantization(0),
|
Chris@1643
|
58 m_units(""),
|
Chris@1643
|
59 m_extendTo(0),
|
Chris@1651
|
60 m_notifier(this,
|
Chris@1651
|
61 notifyOnAdd ?
|
Chris@1651
|
62 DeferredNotifier::NOTIFY_ALWAYS :
|
Chris@1651
|
63 DeferredNotifier::NOTIFY_DEFERRED),
|
Chris@1655
|
64 m_completion(100) {
|
Chris@1647
|
65 if (subtype == FLEXI_NOTE) {
|
Chris@1647
|
66 m_valueMinimum = 33.f;
|
Chris@1647
|
67 m_valueMaximum = 88.f;
|
Chris@1647
|
68 }
|
Chris@1751
|
69 PlayParameterRepository::getInstance()->addPlayable
|
Chris@1751
|
70 (getId().untyped, this);
|
Chris@256
|
71 }
|
Chris@256
|
72
|
Chris@1040
|
73 NoteModel(sv_samplerate_t sampleRate, int resolution,
|
Chris@1429
|
74 float valueMinimum, float valueMaximum,
|
Chris@1647
|
75 bool notifyOnAdd = true,
|
Chris@1647
|
76 Subtype subtype = NORMAL_NOTE) :
|
Chris@1647
|
77 m_subtype(subtype),
|
Chris@1643
|
78 m_sampleRate(sampleRate),
|
Chris@1643
|
79 m_resolution(resolution),
|
Chris@1643
|
80 m_valueMinimum(valueMinimum),
|
Chris@1643
|
81 m_valueMaximum(valueMaximum),
|
Chris@1643
|
82 m_haveExtents(true),
|
Chris@1643
|
83 m_valueQuantization(0),
|
Chris@1643
|
84 m_units(""),
|
Chris@1643
|
85 m_extendTo(0),
|
Chris@1651
|
86 m_notifier(this,
|
Chris@1651
|
87 notifyOnAdd ?
|
Chris@1651
|
88 DeferredNotifier::NOTIFY_ALWAYS :
|
Chris@1651
|
89 DeferredNotifier::NOTIFY_DEFERRED),
|
Chris@1655
|
90 m_completion(100) {
|
Chris@1751
|
91 PlayParameterRepository::getInstance()->addPlayable
|
Chris@1751
|
92 (getId().untyped, this);
|
Chris@391
|
93 }
|
Chris@391
|
94
|
Chris@1643
|
95 virtual ~NoteModel() {
|
Chris@1751
|
96 PlayParameterRepository::getInstance()->removePlayable
|
Chris@1751
|
97 (getId().untyped);
|
Chris@147
|
98 }
|
Chris@1647
|
99
|
Chris@1643
|
100 QString getTypeName() const override { return tr("Note"); }
|
Chris@1647
|
101 Subtype getSubtype() const { return m_subtype; }
|
Chris@1692
|
102 bool isSparse() const override { return true; }
|
Chris@1659
|
103 bool isOK() const override { return true; }
|
Chris@1659
|
104
|
Chris@1659
|
105 sv_frame_t getStartFrame() const override {
|
Chris@1659
|
106 return m_events.getStartFrame();
|
Chris@1659
|
107 }
|
Chris@1725
|
108 sv_frame_t getTrueEndFrame() const override {
|
Chris@1659
|
109 if (m_events.isEmpty()) return 0;
|
Chris@1659
|
110 sv_frame_t e = m_events.getEndFrame();
|
Chris@1659
|
111 if (e % m_resolution == 0) return e;
|
Chris@1659
|
112 else return (e / m_resolution + 1) * m_resolution;
|
Chris@1659
|
113 }
|
Chris@1643
|
114
|
Chris@1643
|
115 sv_samplerate_t getSampleRate() const override { return m_sampleRate; }
|
Chris@1644
|
116 int getResolution() const { return m_resolution; }
|
Chris@1643
|
117
|
Chris@1643
|
118 bool canPlay() const override { return true; }
|
Chris@1643
|
119 QString getDefaultPlayClipId() const override {
|
Chris@1643
|
120 return "elecpiano";
|
Chris@1643
|
121 }
|
Chris@1643
|
122
|
Chris@1643
|
123 QString getScaleUnits() const { return m_units; }
|
Chris@1643
|
124 void setScaleUnits(QString units) {
|
Chris@1643
|
125 m_units = units;
|
Chris@1643
|
126 UnitDatabase::getInstance()->registerUnit(units);
|
Chris@1643
|
127 }
|
Chris@147
|
128
|
Chris@147
|
129 float getValueQuantization() const { return m_valueQuantization; }
|
Chris@147
|
130 void setValueQuantization(float q) { m_valueQuantization = q; }
|
Chris@147
|
131
|
Chris@1643
|
132 float getValueMinimum() const { return m_valueMinimum; }
|
Chris@1643
|
133 float getValueMaximum() const { return m_valueMaximum; }
|
Chris@1643
|
134
|
Chris@1671
|
135 int getCompletion() const override { return m_completion; }
|
Chris@345
|
136
|
Chris@1643
|
137 void setCompletion(int completion, bool update = true) {
|
Chris@391
|
138
|
Chris@1651
|
139 { QMutexLocker locker(&m_mutex);
|
Chris@1651
|
140 if (m_completion == completion) return;
|
Chris@1651
|
141 m_completion = completion;
|
Chris@1643
|
142 }
|
Chris@1643
|
143
|
Chris@1651
|
144 if (update) {
|
Chris@1651
|
145 m_notifier.makeDeferredNotifications();
|
Chris@1643
|
146 }
|
Chris@1651
|
147
|
Chris@1651
|
148 emit completionChanged();
|
Chris@1651
|
149
|
Chris@1651
|
150 if (completion == 100) {
|
Chris@1651
|
151 // henceforth:
|
Chris@1651
|
152 m_notifier.switchMode(DeferredNotifier::NOTIFY_ALWAYS);
|
Chris@1643
|
153 emit modelChanged();
|
Chris@1643
|
154 }
|
Chris@391
|
155 }
|
Chris@1644
|
156
|
Chris@1644
|
157 /**
|
Chris@1644
|
158 * Query methods.
|
Chris@1644
|
159 */
|
Chris@1644
|
160
|
Chris@1644
|
161 int getEventCount() const {
|
Chris@1644
|
162 return m_events.count();
|
Chris@1644
|
163 }
|
Chris@1644
|
164 bool isEmpty() const {
|
Chris@1644
|
165 return m_events.isEmpty();
|
Chris@1644
|
166 }
|
Chris@1644
|
167 bool containsEvent(const Event &e) const {
|
Chris@1644
|
168 return m_events.contains(e);
|
Chris@1644
|
169 }
|
Chris@1644
|
170 EventVector getAllEvents() const {
|
Chris@1644
|
171 return m_events.getAllEvents();
|
Chris@1644
|
172 }
|
Chris@1644
|
173 EventVector getEventsSpanning(sv_frame_t f, sv_frame_t duration) const {
|
Chris@1644
|
174 return m_events.getEventsSpanning(f, duration);
|
Chris@1644
|
175 }
|
Chris@1656
|
176 EventVector getEventsCovering(sv_frame_t f) const {
|
Chris@1656
|
177 return m_events.getEventsCovering(f);
|
Chris@1656
|
178 }
|
Chris@1644
|
179 EventVector getEventsWithin(sv_frame_t f, sv_frame_t duration) const {
|
Chris@1644
|
180 return m_events.getEventsWithin(f, duration);
|
Chris@1644
|
181 }
|
Chris@1644
|
182 EventVector getEventsStartingWithin(sv_frame_t f, sv_frame_t duration) const {
|
Chris@1644
|
183 return m_events.getEventsStartingWithin(f, duration);
|
Chris@1644
|
184 }
|
Chris@1656
|
185 EventVector getEventsStartingAt(sv_frame_t f) const {
|
Chris@1656
|
186 return m_events.getEventsStartingAt(f);
|
Chris@1644
|
187 }
|
Chris@1657
|
188 bool getNearestEventMatching(sv_frame_t startSearchAt,
|
Chris@1657
|
189 std::function<bool(Event)> predicate,
|
Chris@1657
|
190 EventSeries::Direction direction,
|
Chris@1657
|
191 Event &found) const {
|
Chris@1657
|
192 return m_events.getNearestEventMatching
|
Chris@1657
|
193 (startSearchAt, predicate, direction, found);
|
Chris@1657
|
194 }
|
Chris@1644
|
195
|
Chris@1644
|
196 /**
|
Chris@1648
|
197 * Editing methods.
|
Chris@1644
|
198 */
|
Chris@1648
|
199 void add(Event e) override {
|
Chris@1644
|
200
|
Chris@1644
|
201 bool allChange = false;
|
Chris@1644
|
202
|
Chris@1644
|
203 {
|
Chris@1644
|
204 QMutexLocker locker(&m_mutex);
|
Chris@1644
|
205 m_events.add(e);
|
Chris@1644
|
206
|
Chris@1644
|
207 float v = e.getValue();
|
Chris@1644
|
208 if (!ISNAN(v) && !ISINF(v)) {
|
Chris@1644
|
209 if (!m_haveExtents || v < m_valueMinimum) {
|
Chris@1644
|
210 m_valueMinimum = v; allChange = true;
|
Chris@1644
|
211 }
|
Chris@1644
|
212 if (!m_haveExtents || v > m_valueMaximum) {
|
Chris@1644
|
213 m_valueMaximum = v; allChange = true;
|
Chris@1644
|
214 }
|
Chris@1644
|
215 m_haveExtents = true;
|
Chris@1644
|
216 }
|
Chris@1644
|
217 }
|
Chris@1644
|
218
|
Chris@1651
|
219 m_notifier.update(e.getFrame(), e.getDuration() + m_resolution);
|
Chris@1651
|
220
|
Chris@1644
|
221 if (allChange) {
|
Chris@1644
|
222 emit modelChanged();
|
Chris@1644
|
223 }
|
Chris@1644
|
224 }
|
Chris@1644
|
225
|
Chris@1648
|
226 void remove(Event e) override {
|
Chris@1644
|
227 {
|
Chris@1644
|
228 QMutexLocker locker(&m_mutex);
|
Chris@1644
|
229 m_events.remove(e);
|
Chris@1644
|
230 }
|
Chris@1644
|
231 emit modelChangedWithin(e.getFrame(),
|
Chris@1644
|
232 e.getFrame() + e.getDuration() + m_resolution);
|
Chris@147
|
233 }
|
Chris@147
|
234
|
Chris@424
|
235 /**
|
Chris@424
|
236 * TabularModel methods.
|
Chris@424
|
237 */
|
Chris@1643
|
238
|
Chris@1643
|
239 int getRowCount() const override {
|
Chris@1643
|
240 return m_events.count();
|
Chris@1643
|
241 }
|
Chris@424
|
242
|
Chris@1643
|
243 int getColumnCount() const override {
|
Chris@424
|
244 return 6;
|
Chris@424
|
245 }
|
Chris@424
|
246
|
Chris@1643
|
247 bool isColumnTimeValue(int column) const override {
|
Chris@1643
|
248 // NB duration is not a "time value" -- that's for columns
|
Chris@1643
|
249 // whose sort ordering is exactly that of the frame time
|
Chris@1643
|
250 return (column < 2);
|
Chris@1643
|
251 }
|
Chris@1643
|
252
|
Chris@1643
|
253 sv_frame_t getFrameForRow(int row) const override {
|
Chris@1643
|
254 if (row < 0 || row >= m_events.count()) {
|
Chris@1643
|
255 return 0;
|
Chris@1643
|
256 }
|
Chris@1643
|
257 Event e = m_events.getEventByIndex(row);
|
Chris@1643
|
258 return e.getFrame();
|
Chris@1643
|
259 }
|
Chris@1643
|
260
|
Chris@1643
|
261 int getRowForFrame(sv_frame_t frame) const override {
|
Chris@1643
|
262 return m_events.getIndexForEvent(Event(frame));
|
Chris@1643
|
263 }
|
Chris@1643
|
264
|
Chris@1643
|
265 QString getHeading(int column) const override {
|
Chris@424
|
266 switch (column) {
|
Chris@424
|
267 case 0: return tr("Time");
|
Chris@424
|
268 case 1: return tr("Frame");
|
Chris@424
|
269 case 2: return tr("Pitch");
|
Chris@424
|
270 case 3: return tr("Duration");
|
Chris@424
|
271 case 4: return tr("Level");
|
Chris@424
|
272 case 5: return tr("Label");
|
Chris@424
|
273 default: return tr("Unknown");
|
Chris@424
|
274 }
|
Chris@424
|
275 }
|
Chris@424
|
276
|
Chris@1643
|
277 QVariant getData(int row, int column, int role) const override {
|
Chris@1643
|
278
|
Chris@1643
|
279 if (row < 0 || row >= m_events.count()) {
|
Chris@1643
|
280 return QVariant();
|
Chris@425
|
281 }
|
Chris@425
|
282
|
Chris@1643
|
283 Event e = m_events.getEventByIndex(row);
|
Chris@424
|
284
|
Chris@424
|
285 switch (column) {
|
Chris@1643
|
286 case 0: return adaptFrameForRole(e.getFrame(), getSampleRate(), role);
|
Chris@1643
|
287 case 1: return int(e.getFrame());
|
Chris@1643
|
288 case 2: return adaptValueForRole(e.getValue(), getScaleUnits(), role);
|
Chris@1643
|
289 case 3: return int(e.getDuration());
|
Chris@1643
|
290 case 4: return e.getLevel();
|
Chris@1643
|
291 case 5: return e.getLabel();
|
Chris@424
|
292 default: return QVariant();
|
Chris@424
|
293 }
|
Chris@424
|
294 }
|
Chris@424
|
295
|
Chris@1649
|
296 Command *getSetDataCommand(int row, int column, const QVariant &value, int role) override {
|
Chris@1649
|
297
|
Chris@1643
|
298 if (row < 0 || row >= m_events.count()) return nullptr;
|
Chris@1643
|
299 if (role != Qt::EditRole) return nullptr;
|
Chris@1643
|
300
|
Chris@1643
|
301 Event e0 = m_events.getEventByIndex(row);
|
Chris@1643
|
302 Event e1;
|
Chris@1643
|
303
|
Chris@1643
|
304 switch (column) {
|
Chris@1643
|
305 case 0: e1 = e0.withFrame(sv_frame_t(round(value.toDouble() *
|
Chris@1643
|
306 getSampleRate()))); break;
|
Chris@1643
|
307 case 1: e1 = e0.withFrame(value.toInt()); break;
|
Chris@1643
|
308 case 2: e1 = e0.withValue(float(value.toDouble())); break;
|
Chris@1643
|
309 case 3: e1 = e0.withDuration(value.toInt()); break;
|
Chris@1643
|
310 case 4: e1 = e0.withLevel(float(value.toDouble())); break;
|
Chris@1643
|
311 case 5: e1 = e0.withLabel(value.toString()); break;
|
Chris@425
|
312 }
|
Chris@425
|
313
|
Chris@1742
|
314 auto command = new ChangeEventsCommand(getId().untyped, tr("Edit Data"));
|
Chris@1644
|
315 command->remove(e0);
|
Chris@1644
|
316 command->add(e1);
|
Chris@1644
|
317 return command->finish();
|
Chris@424
|
318 }
|
Chris@424
|
319
|
Chris@1580
|
320 SortType getSortType(int column) const override
|
Chris@424
|
321 {
|
Chris@424
|
322 if (column == 5) return SortAlphabetical;
|
Chris@424
|
323 return SortNumeric;
|
Chris@424
|
324 }
|
Chris@424
|
325
|
Chris@852
|
326 /**
|
Chris@852
|
327 * NoteExportable methods.
|
Chris@852
|
328 */
|
Chris@852
|
329
|
Chris@1580
|
330 NoteList getNotes() const override {
|
Chris@1643
|
331 return getNotesStartingWithin(getStartFrame(),
|
Chris@1643
|
332 getEndFrame() - getStartFrame());
|
Chris@852
|
333 }
|
Chris@852
|
334
|
Chris@1643
|
335 NoteList getNotesActiveAt(sv_frame_t frame) const override {
|
Chris@1643
|
336
|
Chris@852
|
337 NoteList notes;
|
Chris@1643
|
338 EventVector ee = m_events.getEventsCovering(frame);
|
Chris@1643
|
339 for (const auto &e: ee) {
|
Chris@1643
|
340 notes.push_back(e.toNoteData(getSampleRate(),
|
Chris@1643
|
341 getScaleUnits() != "Hz"));
|
Chris@1643
|
342 }
|
Chris@1643
|
343 return notes;
|
Chris@1643
|
344 }
|
Chris@1643
|
345
|
Chris@1643
|
346 NoteList getNotesStartingWithin(sv_frame_t startFrame,
|
Chris@1643
|
347 sv_frame_t duration) const override {
|
Chris@852
|
348
|
Chris@1643
|
349 NoteList notes;
|
Chris@1643
|
350 EventVector ee = m_events.getEventsStartingWithin(startFrame, duration);
|
Chris@1643
|
351 for (const auto &e: ee) {
|
Chris@1643
|
352 notes.push_back(e.toNoteData(getSampleRate(),
|
Chris@1643
|
353 getScaleUnits() != "Hz"));
|
Chris@852
|
354 }
|
Chris@852
|
355 return notes;
|
Chris@852
|
356 }
|
Chris@852
|
357
|
Chris@1644
|
358 /**
|
Chris@1644
|
359 * XmlExportable methods.
|
Chris@1644
|
360 */
|
Chris@1644
|
361
|
Chris@1644
|
362 void toXml(QTextStream &out,
|
Chris@1644
|
363 QString indent = "",
|
Chris@1644
|
364 QString extraAttributes = "") const override {
|
Chris@1644
|
365
|
Chris@1644
|
366 //!!! what is valueQuantization used for?
|
Chris@1644
|
367
|
Chris@1644
|
368 Model::toXml
|
Chris@1644
|
369 (out,
|
Chris@1644
|
370 indent,
|
Chris@1644
|
371 QString("type=\"sparse\" dimensions=\"3\" resolution=\"%1\" "
|
Chris@1647
|
372 "notifyOnAdd=\"%2\" dataset=\"%3\" subtype=\"%4\" "
|
Chris@1647
|
373 "valueQuantization=\"%5\" minimum=\"%6\" maximum=\"%7\" "
|
Chris@1647
|
374 "units=\"%8\" %9")
|
Chris@1644
|
375 .arg(m_resolution)
|
Chris@1651
|
376 .arg("true") // always true after model reaches 100% -
|
Chris@1651
|
377 // subsequent events are always notified
|
Chris@1677
|
378 .arg(m_events.getExportId())
|
Chris@1647
|
379 .arg(m_subtype == FLEXI_NOTE ? "flexinote" : "note")
|
Chris@1644
|
380 .arg(m_valueQuantization)
|
Chris@1644
|
381 .arg(m_valueMinimum)
|
Chris@1644
|
382 .arg(m_valueMaximum)
|
Chris@1651
|
383 .arg(encodeEntities(m_units))
|
Chris@1644
|
384 .arg(extraAttributes));
|
Chris@1647
|
385
|
Chris@1644
|
386 m_events.toXml(out, indent, QString("dimensions=\"3\""));
|
Chris@1644
|
387 }
|
Chris@1644
|
388
|
Chris@1679
|
389 QString toDelimitedDataString(QString delimiter,
|
Chris@1679
|
390 DataExportOptions options,
|
Chris@1679
|
391 sv_frame_t startFrame,
|
Chris@1679
|
392 sv_frame_t duration) const override {
|
Chris@1679
|
393 return m_events.toDelimitedDataString
|
Chris@1679
|
394 (delimiter,
|
Chris@1679
|
395 options,
|
Chris@1679
|
396 startFrame,
|
Chris@1679
|
397 duration,
|
Chris@1679
|
398 m_sampleRate,
|
Chris@1679
|
399 m_resolution,
|
Chris@1679
|
400 Event().withValue(0.f).withDuration(0.f).withLevel(0.f));
|
Chris@1679
|
401 }
|
Chris@1679
|
402
|
Chris@147
|
403 protected:
|
Chris@1647
|
404 Subtype m_subtype;
|
Chris@1643
|
405 sv_samplerate_t m_sampleRate;
|
Chris@1643
|
406 int m_resolution;
|
Chris@1643
|
407
|
Chris@1643
|
408 float m_valueMinimum;
|
Chris@1643
|
409 float m_valueMaximum;
|
Chris@1643
|
410 bool m_haveExtents;
|
Chris@147
|
411 float m_valueQuantization;
|
Chris@1643
|
412 QString m_units;
|
Chris@1643
|
413 sv_frame_t m_extendTo;
|
Chris@1651
|
414 DeferredNotifier m_notifier;
|
Chris@1651
|
415 int m_completion;
|
Chris@1643
|
416
|
Chris@1643
|
417 EventSeries m_events;
|
Chris@1643
|
418
|
Chris@1643
|
419 mutable QMutex m_mutex;
|
Chris@1643
|
420
|
Chris@1643
|
421 //!!! do we have general docs for ownership and synchronisation of models?
|
Chris@1643
|
422 // this might be a good opportunity to stop using bare pointers to them
|
Chris@147
|
423 };
|
Chris@147
|
424
|
Chris@147
|
425 #endif
|