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@1581
|
15 #ifndef SV_SPARSE_ONE_DIMENSIONAL_MODEL_H
|
Chris@1581
|
16 #define SV_SPARSE_ONE_DIMENSIONAL_MODEL_H
|
Chris@147
|
17
|
Chris@1658
|
18 #include "EventCommands.h"
|
Chris@1658
|
19 #include "TabularModel.h"
|
Chris@1658
|
20 #include "Model.h"
|
Chris@1658
|
21 #include "DeferredNotifier.h"
|
Chris@1658
|
22
|
Chris@1615
|
23 #include "base/NoteData.h"
|
Chris@1658
|
24 #include "base/EventSeries.h"
|
Chris@1643
|
25 #include "base/NoteExportable.h"
|
Chris@150
|
26 #include "base/PlayParameterRepository.h"
|
Chris@147
|
27 #include "base/RealTime.h"
|
Chris@147
|
28
|
Chris@1658
|
29 #include "system/System.h"
|
Chris@1658
|
30
|
Chris@387
|
31 #include <QStringList>
|
Chris@387
|
32
|
Chris@1658
|
33 /**
|
Chris@1658
|
34 * A model representing a series of time instants with optional labels
|
Chris@1658
|
35 * but without values.
|
Chris@1658
|
36 */
|
Chris@1658
|
37 class SparseOneDimensionalModel : public Model,
|
Chris@1658
|
38 public TabularModel,
|
Chris@1658
|
39 public EventEditable,
|
Chris@852
|
40 public NoteExportable
|
Chris@147
|
41 {
|
Chris@423
|
42 Q_OBJECT
|
Chris@423
|
43
|
Chris@147
|
44 public:
|
Chris@1658
|
45 SparseOneDimensionalModel(sv_samplerate_t sampleRate,
|
Chris@1658
|
46 int resolution,
|
Chris@1429
|
47 bool notifyOnAdd = true) :
|
Chris@1658
|
48 m_sampleRate(sampleRate),
|
Chris@1658
|
49 m_resolution(resolution),
|
Chris@1660
|
50 m_haveTextLabels(false),
|
Chris@1658
|
51 m_notifier(this,
|
Chris@1658
|
52 notifyOnAdd ?
|
Chris@1658
|
53 DeferredNotifier::NOTIFY_ALWAYS :
|
Chris@1658
|
54 DeferredNotifier::NOTIFY_DEFERRED),
|
Chris@1658
|
55 m_completion(100) {
|
Chris@1751
|
56 PlayParameterRepository::getInstance()->addPlayable
|
Chris@1751
|
57 (getId().untyped, this);
|
Chris@391
|
58 }
|
Chris@391
|
59
|
Chris@1658
|
60 virtual ~SparseOneDimensionalModel() {
|
Chris@1751
|
61 PlayParameterRepository::getInstance()->removePlayable
|
Chris@1751
|
62 (getId().untyped);
|
Chris@391
|
63 }
|
Chris@391
|
64
|
Chris@1580
|
65 QString getTypeName() const override { return tr("Sparse 1-D"); }
|
Chris@1692
|
66 bool isSparse() const override { return true; }
|
Chris@1659
|
67 bool isOK() const override { return true; }
|
Chris@420
|
68
|
Chris@1659
|
69 sv_frame_t getStartFrame() const override {
|
Chris@1659
|
70 return m_events.getStartFrame();
|
Chris@1659
|
71 }
|
Chris@1725
|
72 sv_frame_t getTrueEndFrame() const override {
|
Chris@1659
|
73 if (m_events.isEmpty()) return 0;
|
Chris@1659
|
74 sv_frame_t e = m_events.getEndFrame() + 1;
|
Chris@1659
|
75 if (e % m_resolution == 0) return e;
|
Chris@1659
|
76 else return (e / m_resolution + 1) * m_resolution;
|
Chris@1659
|
77 }
|
Chris@1659
|
78
|
Chris@1658
|
79 sv_samplerate_t getSampleRate() const override { return m_sampleRate; }
|
Chris@1658
|
80 int getResolution() const { return m_resolution; }
|
Chris@1658
|
81
|
Chris@1658
|
82 bool canPlay() const override { return true; }
|
Chris@1658
|
83 QString getDefaultPlayClipId() const override { return "tap"; }
|
Chris@1658
|
84
|
Chris@1660
|
85 bool hasTextLabels() const { return m_haveTextLabels; }
|
Chris@1671
|
86
|
Chris@1671
|
87 int getCompletion() const override { return m_completion; }
|
Chris@1658
|
88
|
Chris@1658
|
89 void setCompletion(int completion, bool update = true) {
|
Chris@1658
|
90
|
Chris@1658
|
91 { QMutexLocker locker(&m_mutex);
|
Chris@1658
|
92 if (m_completion == completion) return;
|
Chris@1658
|
93 m_completion = completion;
|
Chris@1658
|
94 }
|
Chris@1658
|
95
|
Chris@1658
|
96 if (update) {
|
Chris@1658
|
97 m_notifier.makeDeferredNotifications();
|
Chris@1658
|
98 }
|
Chris@1658
|
99
|
Chris@1658
|
100 emit completionChanged();
|
Chris@1658
|
101
|
Chris@1658
|
102 if (completion == 100) {
|
Chris@1658
|
103 // henceforth:
|
Chris@1658
|
104 m_notifier.switchMode(DeferredNotifier::NOTIFY_ALWAYS);
|
Chris@1658
|
105 emit modelChanged();
|
Chris@1658
|
106 }
|
Chris@1658
|
107 }
|
Chris@1658
|
108
|
Chris@1658
|
109 /**
|
Chris@1658
|
110 * Query methods.
|
Chris@1658
|
111 */
|
Chris@1658
|
112
|
Chris@1658
|
113 int getEventCount() const {
|
Chris@1658
|
114 return m_events.count();
|
Chris@1658
|
115 }
|
Chris@1658
|
116 bool isEmpty() const {
|
Chris@1658
|
117 return m_events.isEmpty();
|
Chris@1658
|
118 }
|
Chris@1658
|
119 bool containsEvent(const Event &e) const {
|
Chris@1658
|
120 return m_events.contains(e);
|
Chris@1658
|
121 }
|
Chris@1658
|
122 EventVector getAllEvents() const {
|
Chris@1658
|
123 return m_events.getAllEvents();
|
Chris@1658
|
124 }
|
Chris@1658
|
125 EventVector getEventsSpanning(sv_frame_t f, sv_frame_t duration) const {
|
Chris@1658
|
126 return m_events.getEventsSpanning(f, duration);
|
Chris@1658
|
127 }
|
Chris@1658
|
128 EventVector getEventsCovering(sv_frame_t f) const {
|
Chris@1658
|
129 return m_events.getEventsCovering(f);
|
Chris@1658
|
130 }
|
Chris@1658
|
131 EventVector getEventsWithin(sv_frame_t f, sv_frame_t duration,
|
Chris@1658
|
132 int overspill = 0) const {
|
Chris@1658
|
133 return m_events.getEventsWithin(f, duration, overspill);
|
Chris@1658
|
134 }
|
Chris@1658
|
135 EventVector getEventsStartingWithin(sv_frame_t f, sv_frame_t duration) const {
|
Chris@1658
|
136 return m_events.getEventsStartingWithin(f, duration);
|
Chris@1658
|
137 }
|
Chris@1658
|
138 EventVector getEventsStartingAt(sv_frame_t f) const {
|
Chris@1658
|
139 return m_events.getEventsStartingAt(f);
|
Chris@1658
|
140 }
|
Chris@1658
|
141 bool getNearestEventMatching(sv_frame_t startSearchAt,
|
Chris@1658
|
142 std::function<bool(Event)> predicate,
|
Chris@1658
|
143 EventSeries::Direction direction,
|
Chris@1658
|
144 Event &found) const {
|
Chris@1658
|
145 return m_events.getNearestEventMatching
|
Chris@1658
|
146 (startSearchAt, predicate, direction, found);
|
Chris@1658
|
147 }
|
Chris@1658
|
148
|
Chris@1658
|
149 /**
|
Chris@1658
|
150 * Editing methods.
|
Chris@1658
|
151 */
|
Chris@1658
|
152 void add(Event e) override {
|
Chris@1658
|
153
|
Chris@1658
|
154 { QMutexLocker locker(&m_mutex);
|
Chris@1658
|
155 m_events.add(e.withoutValue().withoutDuration());
|
Chris@1660
|
156
|
Chris@1660
|
157 if (e.getLabel() != "") {
|
Chris@1660
|
158 m_haveTextLabels = true;
|
Chris@1660
|
159 }
|
Chris@1658
|
160 }
|
Chris@1658
|
161
|
Chris@1658
|
162 m_notifier.update(e.getFrame(), m_resolution);
|
Chris@1658
|
163 }
|
Chris@1658
|
164
|
Chris@1658
|
165 void remove(Event e) override {
|
Chris@1658
|
166 { QMutexLocker locker(&m_mutex);
|
Chris@1658
|
167 m_events.remove(e);
|
Chris@1658
|
168 }
|
Chris@1658
|
169 emit modelChangedWithin(e.getFrame(), e.getFrame() + m_resolution);
|
Chris@1658
|
170 }
|
Chris@1658
|
171
|
Chris@420
|
172 /**
|
Chris@420
|
173 * TabularModel methods.
|
Chris@420
|
174 */
|
Chris@420
|
175
|
Chris@1658
|
176 int getRowCount() const override {
|
Chris@1658
|
177 return m_events.count();
|
Chris@1658
|
178 }
|
Chris@1658
|
179
|
Chris@1658
|
180 int getColumnCount() const override {
|
Chris@420
|
181 return 3;
|
Chris@420
|
182 }
|
Chris@420
|
183
|
Chris@1658
|
184 bool isColumnTimeValue(int column) const override {
|
Chris@1658
|
185 return (column < 2);
|
Chris@1658
|
186 }
|
Chris@1658
|
187
|
Chris@1658
|
188 sv_frame_t getFrameForRow(int row) const override {
|
Chris@1658
|
189 if (row < 0 || row >= m_events.count()) {
|
Chris@1658
|
190 return 0;
|
Chris@1658
|
191 }
|
Chris@1658
|
192 Event e = m_events.getEventByIndex(row);
|
Chris@1658
|
193 return e.getFrame();
|
Chris@1658
|
194 }
|
Chris@1658
|
195
|
Chris@1658
|
196 int getRowForFrame(sv_frame_t frame) const override {
|
Chris@1658
|
197 return m_events.getIndexForEvent(Event(frame));
|
Chris@1658
|
198 }
|
Chris@1658
|
199
|
Chris@1658
|
200 QString getHeading(int column) const override {
|
Chris@420
|
201 switch (column) {
|
Chris@420
|
202 case 0: return tr("Time");
|
Chris@420
|
203 case 1: return tr("Frame");
|
Chris@420
|
204 case 2: return tr("Label");
|
Chris@420
|
205 default: return tr("Unknown");
|
Chris@420
|
206 }
|
Chris@420
|
207 }
|
Chris@420
|
208
|
Chris@1658
|
209 SortType getSortType(int column) const override {
|
Chris@1658
|
210 if (column == 2) return SortAlphabetical;
|
Chris@1658
|
211 return SortNumeric;
|
Chris@1658
|
212 }
|
Chris@1658
|
213
|
Chris@1658
|
214 QVariant getData(int row, int column, int role) const override {
|
Chris@1658
|
215
|
Chris@1658
|
216 if (row < 0 || row >= m_events.count()) {
|
Chris@1658
|
217 return QVariant();
|
Chris@425
|
218 }
|
Chris@425
|
219
|
Chris@1658
|
220 Event e = m_events.getEventByIndex(row);
|
Chris@420
|
221
|
Chris@420
|
222 switch (column) {
|
Chris@1658
|
223 case 0: return adaptFrameForRole(e.getFrame(), getSampleRate(), role);
|
Chris@1658
|
224 case 1: return int(e.getFrame());
|
Chris@1658
|
225 case 2: return e.getLabel();
|
Chris@420
|
226 default: return QVariant();
|
Chris@420
|
227 }
|
Chris@420
|
228 }
|
Chris@420
|
229
|
Chris@1658
|
230 Command *getSetDataCommand(int row, int column, const QVariant &value, int role) override {
|
Chris@1658
|
231 if (row < 0 || row >= m_events.count()) return nullptr;
|
Chris@1658
|
232 if (role != Qt::EditRole) return nullptr;
|
Chris@1658
|
233
|
Chris@1658
|
234 Event e0 = m_events.getEventByIndex(row);
|
Chris@1658
|
235 Event e1;
|
Chris@1658
|
236
|
Chris@1658
|
237 switch (column) {
|
Chris@1658
|
238 case 0: e1 = e0.withFrame(sv_frame_t(round(value.toDouble() *
|
Chris@1658
|
239 getSampleRate()))); break;
|
Chris@1658
|
240 case 1: e1 = e0.withFrame(value.toInt()); break;
|
Chris@1658
|
241 case 2: e1 = e0.withLabel(value.toString()); break;
|
Chris@425
|
242 }
|
Chris@425
|
243
|
Chris@1742
|
244 auto command = new ChangeEventsCommand(getId().untyped, tr("Edit Data"));
|
Chris@1658
|
245 command->remove(e0);
|
Chris@1658
|
246 command->add(e1);
|
Chris@424
|
247 return command->finish();
|
Chris@424
|
248 }
|
Chris@424
|
249
|
Chris@852
|
250 /**
|
Chris@852
|
251 * NoteExportable methods.
|
Chris@852
|
252 */
|
Chris@852
|
253
|
Chris@1580
|
254 NoteList getNotes() const override {
|
Chris@1643
|
255 return getNotesStartingWithin(getStartFrame(),
|
Chris@1643
|
256 getEndFrame() - getStartFrame());
|
Chris@852
|
257 }
|
Chris@852
|
258
|
Chris@1643
|
259 NoteList getNotesActiveAt(sv_frame_t frame) const override {
|
Chris@1643
|
260 return getNotesStartingWithin(frame, 1);
|
Chris@1643
|
261 }
|
Chris@1643
|
262
|
Chris@1643
|
263 NoteList getNotesStartingWithin(sv_frame_t startFrame,
|
Chris@1643
|
264 sv_frame_t duration) const override {
|
Chris@852
|
265
|
Chris@852
|
266 NoteList notes;
|
Chris@1658
|
267 EventVector ee = m_events.getEventsStartingWithin(startFrame, duration);
|
Chris@1658
|
268 for (const auto &e: ee) {
|
Chris@1658
|
269 notes.push_back(e.toNoteData(getSampleRate(), true));
|
Chris@852
|
270 }
|
Chris@852
|
271 return notes;
|
Chris@852
|
272 }
|
Chris@1658
|
273
|
Chris@1658
|
274 /**
|
Chris@1658
|
275 * XmlExportable methods.
|
Chris@1658
|
276 */
|
Chris@1658
|
277 void toXml(QTextStream &out,
|
Chris@1658
|
278 QString indent = "",
|
Chris@1658
|
279 QString extraAttributes = "") const override {
|
Chris@1658
|
280
|
Chris@1658
|
281 Model::toXml
|
Chris@1658
|
282 (out,
|
Chris@1658
|
283 indent,
|
Chris@1658
|
284 QString("type=\"sparse\" dimensions=\"1\" resolution=\"%1\" "
|
Chris@1658
|
285 "notifyOnAdd=\"%2\" dataset=\"%3\" %4")
|
Chris@1658
|
286 .arg(m_resolution)
|
Chris@1658
|
287 .arg("true") // always true after model reaches 100% -
|
Chris@1658
|
288 // subsequent events are always notified
|
Chris@1677
|
289 .arg(m_events.getExportId())
|
Chris@1658
|
290 .arg(extraAttributes));
|
Chris@1658
|
291
|
Chris@1658
|
292 m_events.toXml(out, indent, QString("dimensions=\"1\""));
|
Chris@1658
|
293 }
|
Chris@1679
|
294
|
Chris@1679
|
295 QString toDelimitedDataString(QString delimiter,
|
Chris@1679
|
296 DataExportOptions options,
|
Chris@1679
|
297 sv_frame_t startFrame,
|
Chris@1679
|
298 sv_frame_t duration) const override {
|
Chris@1679
|
299 return m_events.toDelimitedDataString(delimiter,
|
Chris@1679
|
300 options,
|
Chris@1679
|
301 startFrame,
|
Chris@1679
|
302 duration,
|
Chris@1679
|
303 m_sampleRate,
|
Chris@1679
|
304 m_resolution,
|
Chris@1679
|
305 Event());
|
Chris@1679
|
306 }
|
Chris@1658
|
307
|
Chris@1658
|
308 protected:
|
Chris@1658
|
309 sv_samplerate_t m_sampleRate;
|
Chris@1658
|
310 int m_resolution;
|
Chris@1658
|
311
|
Chris@1660
|
312 bool m_haveTextLabels;
|
Chris@1658
|
313 DeferredNotifier m_notifier;
|
Chris@1658
|
314 int m_completion;
|
Chris@1658
|
315
|
Chris@1658
|
316 EventSeries m_events;
|
Chris@1658
|
317
|
Chris@1658
|
318 mutable QMutex m_mutex;
|
Chris@147
|
319 };
|
Chris@147
|
320
|
Chris@147
|
321 #endif
|
Chris@147
|
322
|
Chris@147
|
323
|
Chris@147
|
324
|