Chris@1612
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@1612
|
2
|
Chris@1612
|
3 /*
|
Chris@1612
|
4 Sonic Visualiser
|
Chris@1612
|
5 An audio file viewer and annotation editor.
|
Chris@1612
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@1612
|
7
|
Chris@1612
|
8 This program is free software; you can redistribute it and/or
|
Chris@1612
|
9 modify it under the terms of the GNU General Public License as
|
Chris@1612
|
10 published by the Free Software Foundation; either version 2 of the
|
Chris@1612
|
11 License, or (at your option) any later version. See the file
|
Chris@1612
|
12 COPYING included with this distribution for more information.
|
Chris@1612
|
13 */
|
Chris@1612
|
14
|
Chris@1615
|
15 #ifndef SV_EVENT_SERIES_H
|
Chris@1615
|
16 #define SV_EVENT_SERIES_H
|
Chris@1612
|
17
|
Chris@1615
|
18 #include "Event.h"
|
Chris@1612
|
19
|
Chris@1612
|
20 #include <set>
|
Chris@1612
|
21
|
Chris@1615
|
22 //#define DEBUG_EVENT_SERIES 1
|
Chris@1614
|
23
|
Chris@1615
|
24 /**
|
Chris@1615
|
25 * Container storing a series of events, with or without durations,
|
Chris@1616
|
26 * and supporting the ability to query which events are active at a
|
Chris@1616
|
27 * given frame or within a span of frames.
|
Chris@1616
|
28 *
|
Chris@1616
|
29 * To that end, in addition to the series of events, it stores a
|
Chris@1616
|
30 * series of "seams", which are frame positions at which the set of
|
Chris@1616
|
31 * simultaneous events changes (i.e. an event of non-zero duration
|
Chris@1616
|
32 * starts or ends) associated with a set of the events that are active
|
Chris@1616
|
33 * at or from that position. These are updated when an event is added
|
Chris@1616
|
34 * or removed.
|
Chris@1618
|
35 *
|
Chris@1618
|
36 * Performance is highly dependent on the extent of overlapping events
|
Chris@1618
|
37 * and the order in which events are added. Each event (with duration)
|
Chris@1618
|
38 * that is added requires updating all the seams within the extent of
|
Chris@1618
|
39 * that event, taking a number of ordered-set updates proportional to
|
Chris@1618
|
40 * the number of events already existing within its extent. Add events
|
Chris@1618
|
41 * in order of start frame if possible.
|
Chris@1615
|
42 */
|
Chris@1615
|
43 class EventSeries
|
Chris@1612
|
44 {
|
Chris@1612
|
45 public:
|
Chris@1615
|
46 EventSeries() : m_count(0) { }
|
Chris@1616
|
47 ~EventSeries() =default;
|
Chris@1616
|
48
|
Chris@1616
|
49 EventSeries(const EventSeries &) =default;
|
Chris@1616
|
50 EventSeries &operator=(const EventSeries &) =default;
|
Chris@1616
|
51 EventSeries &operator=(EventSeries &&) =default;
|
Chris@1616
|
52
|
Chris@1616
|
53 bool operator==(const EventSeries &other) const {
|
Chris@1616
|
54 return m_events == other.m_events;
|
Chris@1616
|
55 }
|
Chris@1612
|
56
|
Chris@1615
|
57 void add(const Event &p) {
|
Chris@1612
|
58
|
Chris@1616
|
59 ++m_events[p];
|
Chris@1612
|
60 ++m_count;
|
Chris@1612
|
61
|
Chris@1626
|
62 sv_id_t id = Event::getId(p);
|
Chris@1626
|
63
|
Chris@1615
|
64 if (p.hasDuration()) {
|
Chris@1617
|
65
|
Chris@1617
|
66 const sv_frame_t frame = p.getFrame();
|
Chris@1617
|
67 const sv_frame_t endFrame = p.getFrame() + p.getDuration();
|
Chris@1612
|
68
|
Chris@1614
|
69 createSeam(frame);
|
Chris@1614
|
70 createSeam(endFrame);
|
Chris@1616
|
71
|
Chris@1616
|
72 // These calls must both succeed after calling createSeam above
|
Chris@1616
|
73 const auto i0 = m_seams.find(frame);
|
Chris@1616
|
74 const auto i1 = m_seams.find(endFrame);
|
Chris@1614
|
75
|
Chris@1614
|
76 for (auto i = i0; i != i1; ++i) {
|
Chris@1614
|
77 if (i == m_seams.end()) {
|
Chris@1615
|
78 SVCERR << "ERROR: EventSeries::add: "
|
Chris@1614
|
79 << "reached end of seam map"
|
Chris@1614
|
80 << endl;
|
Chris@1614
|
81 break;
|
Chris@1612
|
82 }
|
Chris@1626
|
83 /* bool found = false;
|
Chris@1626
|
84 for (auto eid: i->second) {
|
Chris@1626
|
85 if (eid == id) {
|
Chris@1626
|
86 found = true;
|
Chris@1626
|
87 break;
|
Chris@1626
|
88 }
|
Chris@1626
|
89 }
|
Chris@1626
|
90 if (!found) {*/
|
Chris@1626
|
91 i->second.push_back(id);
|
Chris@1626
|
92 // }
|
Chris@1612
|
93 }
|
Chris@1614
|
94 }
|
Chris@1612
|
95
|
Chris@1615
|
96 #ifdef DEBUG_EVENT_SERIES
|
Chris@1614
|
97 std::cerr << "after add:" << std::endl;
|
Chris@1615
|
98 dumpEvents();
|
Chris@1614
|
99 dumpSeams();
|
Chris@1614
|
100 #endif
|
Chris@1612
|
101 }
|
Chris@1612
|
102
|
Chris@1615
|
103 void remove(const Event &p) {
|
Chris@1612
|
104
|
Chris@1616
|
105 // If we are removing the last (unique) example of an event,
|
Chris@1616
|
106 // then we also need to remove it from the seam map. If this
|
Chris@1616
|
107 // is only one of multiple identical events, then we don't.
|
Chris@1616
|
108 bool isUnique = false;
|
Chris@1616
|
109
|
Chris@1615
|
110 auto pitr = m_events.find(p);
|
Chris@1615
|
111 if (pitr == m_events.end()) {
|
Chris@1615
|
112 return; // we don't know this event
|
Chris@1612
|
113 } else {
|
Chris@1625
|
114 if (--(pitr->second) == 0) {
|
Chris@1616
|
115 isUnique = true;
|
Chris@1616
|
116 m_events.erase(pitr);
|
Chris@1616
|
117 }
|
Chris@1612
|
118 --m_count;
|
Chris@1612
|
119 }
|
Chris@1612
|
120
|
Chris@1626
|
121 sv_id_t id = Event::getId(p);
|
Chris@1626
|
122
|
Chris@1616
|
123 if (p.hasDuration() && isUnique) {
|
Chris@1616
|
124
|
Chris@1617
|
125 const sv_frame_t frame = p.getFrame();
|
Chris@1617
|
126 const sv_frame_t endFrame = p.getFrame() + p.getDuration();
|
Chris@1612
|
127
|
Chris@1616
|
128 const auto i0 = m_seams.find(frame);
|
Chris@1616
|
129 const auto i1 = m_seams.find(endFrame);
|
Chris@1614
|
130
|
Chris@1615
|
131 #ifdef DEBUG_EVENT_SERIES
|
Chris@1615
|
132 // This should be impossible if we found p in m_events above
|
Chris@1614
|
133 if (i0 == m_seams.end() || i1 == m_seams.end()) {
|
Chris@1615
|
134 SVCERR << "ERROR: EventSeries::remove: either frame " << frame
|
Chris@1614
|
135 << " or endFrame " << endFrame
|
Chris@1615
|
136 << " for event not found in seam map: event is "
|
Chris@1612
|
137 << p.toXmlString() << endl;
|
Chris@1612
|
138 }
|
Chris@1614
|
139 #endif
|
Chris@1612
|
140
|
Chris@1614
|
141 for (auto i = i0; i != i1; ++i) {
|
Chris@1614
|
142 if (i == m_seams.end()) {
|
Chris@1615
|
143 // This can happen only if we have a negative
|
Chris@1616
|
144 // duration, which Event forbids
|
Chris@1616
|
145 throw std::logic_error("unexpectedly reached end of map");
|
Chris@1616
|
146 }
|
Chris@1626
|
147 for (size_t j = 0; j < i->second.size(); ) {
|
Chris@1626
|
148 if (i->second[j] == id) {
|
Chris@1626
|
149 i->second.erase(i->second.begin() + j);
|
Chris@1626
|
150 } else {
|
Chris@1626
|
151 ++j;
|
Chris@1626
|
152 }
|
Chris@1626
|
153 }
|
Chris@1616
|
154 }
|
Chris@1616
|
155
|
Chris@1616
|
156 // Tidy up by removing any entries that are now identical
|
Chris@1616
|
157 // to their predecessors
|
Chris@1616
|
158
|
Chris@1626
|
159 //!!! won't work as vector is not consistently ordered
|
Chris@1626
|
160
|
Chris@1616
|
161 std::vector<sv_frame_t> redundant;
|
Chris@1616
|
162
|
Chris@1616
|
163 auto pitr = m_seams.end();
|
Chris@1616
|
164 if (i0 != m_seams.begin()) {
|
Chris@1616
|
165 pitr = i0;
|
Chris@1616
|
166 --pitr;
|
Chris@1616
|
167 }
|
Chris@1616
|
168
|
Chris@1616
|
169 for (auto i = i0; i != m_seams.end(); ++i) {
|
Chris@1625
|
170 if (pitr != m_seams.end() && i->second == pitr->second) {
|
Chris@1625
|
171 redundant.push_back(i->first);
|
Chris@1616
|
172 }
|
Chris@1616
|
173 pitr = i;
|
Chris@1616
|
174 if (i == i1) {
|
Chris@1614
|
175 break;
|
Chris@1614
|
176 }
|
Chris@1612
|
177 }
|
Chris@1612
|
178
|
Chris@1616
|
179 for (sv_frame_t f: redundant) {
|
Chris@1625
|
180 m_seams.erase(f);
|
Chris@1616
|
181 }
|
Chris@1612
|
182 }
|
Chris@1614
|
183
|
Chris@1615
|
184 #ifdef DEBUG_EVENT_SERIES
|
Chris@1614
|
185 std::cerr << "after remove:" << std::endl;
|
Chris@1615
|
186 dumpEvents();
|
Chris@1614
|
187 dumpSeams();
|
Chris@1614
|
188 #endif
|
Chris@1612
|
189 }
|
Chris@1612
|
190
|
Chris@1615
|
191 bool contains(const Event &p) {
|
Chris@1615
|
192 return m_events.find(p) != m_events.end();
|
Chris@1612
|
193 }
|
Chris@1612
|
194
|
Chris@1612
|
195 int count() const {
|
Chris@1612
|
196 return m_count;
|
Chris@1612
|
197 }
|
Chris@1612
|
198
|
Chris@1612
|
199 bool isEmpty() const {
|
Chris@1612
|
200 return m_count == 0;
|
Chris@1612
|
201 }
|
Chris@1612
|
202
|
Chris@1612
|
203 void clear() {
|
Chris@1615
|
204 m_events.clear();
|
Chris@1612
|
205 m_seams.clear();
|
Chris@1612
|
206 m_count = 0;
|
Chris@1612
|
207 }
|
Chris@1612
|
208
|
Chris@1612
|
209 /**
|
Chris@1616
|
210 * Retrieve all events any part of which falls within the span in
|
Chris@1616
|
211 * frames defined by the given frame f and duration d.
|
Chris@1616
|
212 *
|
Chris@1616
|
213 * - An event without duration is within the span if its own frame
|
Chris@1616
|
214 * is greater than or equal to f and less than f + d.
|
Chris@1616
|
215 *
|
Chris@1616
|
216 * - An event with duration is within the span if its start frame
|
Chris@1616
|
217 * is less than f + d and its start frame plus its duration is
|
Chris@1616
|
218 * greater than f.
|
Chris@1616
|
219 *
|
Chris@1619
|
220 * Note: Passing a duration of zero is seldom useful here; you
|
Chris@1619
|
221 * probably want getEventsCovering instead. getEventsSpanning(f,
|
Chris@1619
|
222 * 0) is not equivalent to getEventsCovering(f). The latter
|
Chris@1619
|
223 * includes durationless events at f and events starting at f,
|
Chris@1619
|
224 * both of which are excluded from the former.
|
Chris@1616
|
225 */
|
Chris@1617
|
226 EventVector getEventsSpanning(sv_frame_t frame,
|
Chris@1617
|
227 sv_frame_t duration) const {
|
Chris@1616
|
228 EventVector span;
|
Chris@1616
|
229
|
Chris@1617
|
230 const sv_frame_t start = frame;
|
Chris@1617
|
231 const sv_frame_t end = frame + duration;
|
Chris@1616
|
232
|
Chris@1616
|
233 // first find any zero-duration events
|
Chris@1616
|
234
|
Chris@1625
|
235 auto pitr = m_events.lower_bound(Event(start, QString()));
|
Chris@1625
|
236 while (pitr != m_events.end() && pitr->first.getFrame() < end) {
|
Chris@1625
|
237 if (!pitr->first.hasDuration()) {
|
Chris@1625
|
238 for (int i = 0; i < pitr->second; ++i) {
|
Chris@1625
|
239 span.push_back(pitr->first);
|
Chris@1616
|
240 }
|
Chris@1616
|
241 }
|
Chris@1616
|
242 ++pitr;
|
Chris@1616
|
243 }
|
Chris@1616
|
244
|
Chris@1616
|
245 // now any non-zero-duration ones from the seam map
|
Chris@1616
|
246
|
Chris@1616
|
247 std::set<Event> found;
|
Chris@1625
|
248 auto sitr = m_seams.lower_bound(start);
|
Chris@1625
|
249 if (sitr == m_seams.end() || sitr->first > start) {
|
Chris@1616
|
250 if (sitr != m_seams.begin()) {
|
Chris@1616
|
251 --sitr;
|
Chris@1616
|
252 }
|
Chris@1616
|
253 }
|
Chris@1625
|
254 while (sitr != m_seams.end() && sitr->first < end) {
|
Chris@1626
|
255 for (const auto &id: sitr->second) {
|
Chris@1626
|
256 found.insert(Event::getEventForId(id));
|
Chris@1616
|
257 }
|
Chris@1616
|
258 ++sitr;
|
Chris@1616
|
259 }
|
Chris@1616
|
260 for (const auto &p: found) {
|
Chris@1625
|
261 int n = m_events.at(p);
|
Chris@1616
|
262 if (n < 1) {
|
Chris@1616
|
263 throw std::logic_error("event is in seams but not events");
|
Chris@1616
|
264 }
|
Chris@1616
|
265 for (int i = 0; i < n; ++i) {
|
Chris@1616
|
266 span.push_back(p);
|
Chris@1616
|
267 }
|
Chris@1616
|
268 }
|
Chris@1616
|
269
|
Chris@1616
|
270 return span;
|
Chris@1616
|
271 }
|
Chris@1616
|
272
|
Chris@1616
|
273 /**
|
Chris@1616
|
274 * Retrieve all events that cover the given frame. An event without
|
Chris@1616
|
275 * duration covers a frame if its own frame is equal to it. An event
|
Chris@1616
|
276 * with duration covers a frame if its start frame is less than or
|
Chris@1612
|
277 * equal to it and its end frame (start + duration) is greater
|
Chris@1612
|
278 * than it.
|
Chris@1612
|
279 */
|
Chris@1616
|
280 EventVector getEventsCovering(sv_frame_t frame) const {
|
Chris@1616
|
281 EventVector cover;
|
Chris@1614
|
282
|
Chris@1615
|
283 // first find any zero-duration events
|
Chris@1616
|
284
|
Chris@1625
|
285 auto pitr = m_events.lower_bound(Event(frame, QString()));
|
Chris@1625
|
286 while (pitr != m_events.end() && pitr->first.getFrame() == frame) {
|
Chris@1625
|
287 if (!pitr->first.hasDuration()) {
|
Chris@1625
|
288 for (int i = 0; i < pitr->second; ++i) {
|
Chris@1625
|
289 cover.push_back(pitr->first);
|
Chris@1614
|
290 }
|
Chris@1614
|
291 }
|
Chris@1616
|
292 ++pitr;
|
Chris@1614
|
293 }
|
Chris@1614
|
294
|
Chris@1625
|
295 // now any non-zero-duration ones from the seam map
|
Chris@1616
|
296
|
Chris@1626
|
297 std::set<Event> found;
|
Chris@1625
|
298 auto sitr = m_seams.lower_bound(frame);
|
Chris@1625
|
299 if (sitr == m_seams.end() || sitr->first > frame) {
|
Chris@1614
|
300 if (sitr != m_seams.begin()) {
|
Chris@1614
|
301 --sitr;
|
Chris@1614
|
302 }
|
Chris@1614
|
303 }
|
Chris@1625
|
304 if (sitr != m_seams.end() && sitr->first <= frame) {
|
Chris@1626
|
305 for (const auto &id: sitr->second) {
|
Chris@1626
|
306 found.insert(Event::getEventForId(id));
|
Chris@1626
|
307 }
|
Chris@1626
|
308 ++sitr;
|
Chris@1626
|
309 }
|
Chris@1626
|
310 for (const auto &p: found) {
|
Chris@1626
|
311 int n = m_events.at(p);
|
Chris@1626
|
312 if (n < 1) {
|
Chris@1626
|
313 throw std::logic_error("event is in seams but not events");
|
Chris@1626
|
314 }
|
Chris@1626
|
315 for (int i = 0; i < n; ++i) {
|
Chris@1626
|
316 cover.push_back(p);
|
Chris@1614
|
317 }
|
Chris@1614
|
318 }
|
Chris@1614
|
319
|
Chris@1616
|
320 return cover;
|
Chris@1612
|
321 }
|
Chris@1612
|
322
|
Chris@1612
|
323 private:
|
Chris@1616
|
324 /**
|
Chris@1616
|
325 * Total number of events in the series. Will exceed
|
Chris@1616
|
326 * m_events.size() if the series contains duplicate events.
|
Chris@1616
|
327 */
|
Chris@1612
|
328 int m_count;
|
Chris@1625
|
329
|
Chris@1616
|
330 /**
|
Chris@1616
|
331 * The (ordered) Events map acts as a list of all the events in
|
Chris@1616
|
332 * the series. For reasons of backward compatibility, we have to
|
Chris@1616
|
333 * handle series containing multiple instances of identical
|
Chris@1616
|
334 * events; the int indexed against each event records the number
|
Chris@1616
|
335 * of instances of that event. We do this in preference to using a
|
Chris@1616
|
336 * multiset, in order to support the case in which we have
|
Chris@1616
|
337 * obtained a set of distinct events (from the seam container
|
Chris@1616
|
338 * below) and then need to return the correct number of each.
|
Chris@1616
|
339 *
|
Chris@1616
|
340 * Because events are immutable, we never have to move one to a
|
Chris@1616
|
341 * different "key" in this map - we only add or delete them or
|
Chris@1616
|
342 * change their counts.
|
Chris@1616
|
343 */
|
Chris@1625
|
344 typedef std::map<Event, int> Events;
|
Chris@1616
|
345 Events m_events;
|
Chris@1614
|
346
|
Chris@1616
|
347 /**
|
Chris@1616
|
348 * The FrameEventMap maps from frame number to a set of events. In
|
Chris@1616
|
349 * the seam map this is used to represent the events that are
|
Chris@1616
|
350 * active at that frame, either because they begin at that frame
|
Chris@1616
|
351 * or because they are continuing from an earlier frame. There is
|
Chris@1616
|
352 * an entry here for each frame at which an event starts or ends,
|
Chris@1616
|
353 * with the event appearing in all entries from its start time
|
Chris@1616
|
354 * onward and disappearing again at its end frame.
|
Chris@1616
|
355 *
|
Chris@1616
|
356 * Only events with duration appear in this map; point events
|
Chris@1616
|
357 * appear only in m_events.
|
Chris@1616
|
358 */
|
Chris@1626
|
359 typedef std::map<sv_frame_t, std::vector<sv_id_t>> FrameEventMap;
|
Chris@1616
|
360 FrameEventMap m_seams;
|
Chris@1614
|
361
|
Chris@1614
|
362 /** Create a seam at the given frame, copying from the prior seam
|
Chris@1614
|
363 * if there is one. If a seam already exists at the given frame,
|
Chris@1614
|
364 * leave it untouched.
|
Chris@1614
|
365 */
|
Chris@1614
|
366 void createSeam(sv_frame_t frame) {
|
Chris@1625
|
367 auto itr = m_seams.lower_bound(frame);
|
Chris@1625
|
368 if (itr == m_seams.end() || itr->first > frame) {
|
Chris@1614
|
369 if (itr != m_seams.begin()) {
|
Chris@1614
|
370 --itr;
|
Chris@1614
|
371 }
|
Chris@1614
|
372 }
|
Chris@1614
|
373 if (itr == m_seams.end()) {
|
Chris@1614
|
374 m_seams[frame] = {};
|
Chris@1625
|
375 } else if (itr->first < frame) {
|
Chris@1625
|
376 m_seams[frame] = itr->second;
|
Chris@1625
|
377 } else if (itr->first > frame) { // itr must be begin()
|
Chris@1614
|
378 m_seams[frame] = {};
|
Chris@1614
|
379 }
|
Chris@1614
|
380 }
|
Chris@1614
|
381
|
Chris@1615
|
382 #ifdef DEBUG_EVENT_SERIES
|
Chris@1615
|
383 void dumpEvents() const {
|
Chris@1618
|
384 std::cerr << "EVENTS (" << m_events.size() << ") [" << std::endl;
|
Chris@1616
|
385 for (const auto &i: m_events) {
|
Chris@1618
|
386 std::cerr << " " << i.second << "x " << i.first.toXmlString();
|
Chris@1614
|
387 }
|
Chris@1614
|
388 std::cerr << "]" << std::endl;
|
Chris@1614
|
389 }
|
Chris@1614
|
390
|
Chris@1614
|
391 void dumpSeams() const {
|
Chris@1618
|
392 std::cerr << "SEAMS (" << m_seams.size() << ") [" << std::endl;
|
Chris@1614
|
393 for (const auto &s: m_seams) {
|
Chris@1614
|
394 std::cerr << " " << s.first << " -> {" << std::endl;
|
Chris@1614
|
395 for (const auto &p: s.second) {
|
Chris@1614
|
396 std::cerr << p.toXmlString(" ");
|
Chris@1614
|
397 }
|
Chris@1614
|
398 std::cerr << " }" << std::endl;
|
Chris@1614
|
399 }
|
Chris@1614
|
400 std::cerr << "]" << std::endl;
|
Chris@1614
|
401 }
|
Chris@1614
|
402 #endif
|
Chris@1612
|
403 };
|
Chris@1612
|
404
|
Chris@1612
|
405 #endif
|