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@1631
|
19 #include "XmlExportable.h"
|
Chris@1612
|
20
|
Chris@1612
|
21 #include <set>
|
Chris@1612
|
22
|
Chris@1615
|
23 //#define DEBUG_EVENT_SERIES 1
|
Chris@1614
|
24
|
Chris@1615
|
25 /**
|
Chris@1615
|
26 * Container storing a series of events, with or without durations,
|
Chris@1616
|
27 * and supporting the ability to query which events are active at a
|
Chris@1616
|
28 * given frame or within a span of frames.
|
Chris@1616
|
29 *
|
Chris@1616
|
30 * To that end, in addition to the series of events, it stores a
|
Chris@1616
|
31 * series of "seams", which are frame positions at which the set of
|
Chris@1616
|
32 * simultaneous events changes (i.e. an event of non-zero duration
|
Chris@1616
|
33 * starts or ends) associated with a set of the events that are active
|
Chris@1616
|
34 * at or from that position. These are updated when an event is added
|
Chris@1616
|
35 * or removed.
|
Chris@1618
|
36 *
|
Chris@1632
|
37 * This class is highly optimised for inserting events in increasing
|
Chris@1632
|
38 * order of start frame. Inserting (or deleting) events in the middle
|
Chris@1632
|
39 * does work, and should be acceptable in interactive use, but it is
|
Chris@1632
|
40 * very slow in bulk.
|
Chris@1615
|
41 */
|
Chris@1631
|
42 class EventSeries : public XmlExportable
|
Chris@1612
|
43 {
|
Chris@1612
|
44 public:
|
Chris@1631
|
45 EventSeries() { }
|
Chris@1616
|
46 ~EventSeries() =default;
|
Chris@1616
|
47
|
Chris@1616
|
48 EventSeries(const EventSeries &) =default;
|
Chris@1616
|
49 EventSeries &operator=(const EventSeries &) =default;
|
Chris@1616
|
50 EventSeries &operator=(EventSeries &&) =default;
|
Chris@1616
|
51
|
Chris@1616
|
52 bool operator==(const EventSeries &other) const {
|
Chris@1616
|
53 return m_events == other.m_events;
|
Chris@1616
|
54 }
|
Chris@1612
|
55
|
Chris@1631
|
56 void clear();
|
Chris@1632
|
57 void add(const Event &e);
|
Chris@1632
|
58 void remove(const Event &e);
|
Chris@1632
|
59 bool contains(const Event &e) const;
|
Chris@1631
|
60 bool isEmpty() const;
|
Chris@1631
|
61 int count() const;
|
Chris@1612
|
62
|
Chris@1612
|
63 /**
|
Chris@1635
|
64 * Retrieve all events any part of which falls within the range in
|
Chris@1616
|
65 * frames defined by the given frame f and duration d.
|
Chris@1616
|
66 *
|
Chris@1635
|
67 * - An event without duration is spanned by the range if its own
|
Chris@1635
|
68 * frame is greater than or equal to f and less than f + d.
|
Chris@1616
|
69 *
|
Chris@1635
|
70 * - An event with duration is spanned by the range if its start
|
Chris@1635
|
71 * frame is less than f + d and its start frame plus its duration
|
Chris@1635
|
72 * is greater than f.
|
Chris@1616
|
73 *
|
Chris@1619
|
74 * Note: Passing a duration of zero is seldom useful here; you
|
Chris@1619
|
75 * probably want getEventsCovering instead. getEventsSpanning(f,
|
Chris@1619
|
76 * 0) is not equivalent to getEventsCovering(f). The latter
|
Chris@1619
|
77 * includes durationless events at f and events starting at f,
|
Chris@1619
|
78 * both of which are excluded from the former.
|
Chris@1616
|
79 */
|
Chris@1617
|
80 EventVector getEventsSpanning(sv_frame_t frame,
|
Chris@1631
|
81 sv_frame_t duration) const;
|
Chris@1616
|
82
|
Chris@1616
|
83 /**
|
Chris@1635
|
84 * Retrieve all events falling wholly within the range in frames
|
Chris@1635
|
85 * defined by the given frame f and duration d.
|
Chris@1635
|
86 *
|
Chris@1635
|
87 * - An event without duration is within the range if its own
|
Chris@1635
|
88 * frame is greater than or equal to f and less than f + d.
|
Chris@1635
|
89 *
|
Chris@1635
|
90 * - An event with duration is within the range if its start frame
|
Chris@1635
|
91 * is greater than or equal to f and its start frame plus its
|
Chris@1635
|
92 * duration is less than or equal to f + d.
|
Chris@1635
|
93 */
|
Chris@1635
|
94 EventVector getEventsWithin(sv_frame_t frame,
|
Chris@1635
|
95 sv_frame_t duration) const;
|
Chris@1635
|
96
|
Chris@1635
|
97 /**
|
Chris@1616
|
98 * Retrieve all events that cover the given frame. An event without
|
Chris@1616
|
99 * duration covers a frame if its own frame is equal to it. An event
|
Chris@1616
|
100 * with duration covers a frame if its start frame is less than or
|
Chris@1612
|
101 * equal to it and its end frame (start + duration) is greater
|
Chris@1612
|
102 * than it.
|
Chris@1612
|
103 */
|
Chris@1631
|
104 EventVector getEventsCovering(sv_frame_t frame) const;
|
Chris@1635
|
105
|
Chris@1631
|
106 /**
|
Chris@1632
|
107 * If e is in the series and is not the first event in it, set
|
Chris@1632
|
108 * preceding to the event immediate preceding it according to the
|
Chris@1632
|
109 * standard event ordering and return true. Otherwise leave
|
Chris@1632
|
110 * preceding unchanged and return false.
|
Chris@1632
|
111 *
|
Chris@1632
|
112 * If there are multiple events identical to e in the series,
|
Chris@1632
|
113 * assume that the event passed in is the first one (i.e. never
|
Chris@1632
|
114 * set preceding equal to e).
|
Chris@1633
|
115 *
|
Chris@1633
|
116 * It is acceptable for preceding to alias e when this is called.
|
Chris@1632
|
117 */
|
Chris@1632
|
118 bool getEventPreceding(const Event &e, Event &preceding) const;
|
Chris@1632
|
119
|
Chris@1632
|
120 /**
|
Chris@1632
|
121 * If e is in the series and is not the final event in it, set
|
Chris@1632
|
122 * following to the event immediate following it according to the
|
Chris@1632
|
123 * standard event ordering and return true. Otherwise leave
|
Chris@1632
|
124 * following unchanged and return false.
|
Chris@1632
|
125 *
|
Chris@1632
|
126 * If there are multiple events identical to e in the series,
|
Chris@1632
|
127 * assume that the event passed in is the last one (i.e. never set
|
Chris@1632
|
128 * following equal to e).
|
Chris@1633
|
129 *
|
Chris@1633
|
130 * It is acceptable for following to alias e when this is called.
|
Chris@1632
|
131 */
|
Chris@1632
|
132 bool getEventFollowing(const Event &e, Event &following) const;
|
Chris@1632
|
133
|
Chris@1632
|
134 /**
|
Chris@1632
|
135 * Return the event at the given numerical index in the series,
|
Chris@1632
|
136 * where 0 = the first event and count()-1 = the last.
|
Chris@1632
|
137 */
|
Chris@1632
|
138 Event getEventByIndex(int index) const;
|
Chris@1632
|
139
|
Chris@1632
|
140 /**
|
Chris@1631
|
141 * Emit to XML as a dataset element.
|
Chris@1631
|
142 */
|
Chris@1631
|
143 void toXml(QTextStream &out,
|
Chris@1631
|
144 QString indent,
|
Chris@1631
|
145 QString extraAttributes) const override;
|
Chris@1631
|
146
|
Chris@1612
|
147 private:
|
Chris@1616
|
148 /**
|
Chris@1631
|
149 * This vector contains all events in the series, in the normal
|
Chris@1631
|
150 * sort order. For backward compatibility we must support series
|
Chris@1631
|
151 * containing multiple instances of identical events, so
|
Chris@1631
|
152 * consecutive events in this vector will not always be distinct.
|
Chris@1631
|
153 * The vector is used in preference to a multiset or map<Event,
|
Chris@1631
|
154 * int> in order to allow indexing by "row number" as well as by
|
Chris@1631
|
155 * properties such as frame.
|
Chris@1631
|
156 *
|
Chris@1631
|
157 * Because events are immutable, we do not have to worry about the
|
Chris@1631
|
158 * order changing once an event is inserted - we only add or
|
Chris@1631
|
159 * delete them.
|
Chris@1616
|
160 */
|
Chris@1631
|
161 typedef std::vector<Event> Events;
|
Chris@1616
|
162 Events m_events;
|
Chris@1631
|
163
|
Chris@1616
|
164 /**
|
Chris@1616
|
165 * The FrameEventMap maps from frame number to a set of events. In
|
Chris@1616
|
166 * the seam map this is used to represent the events that are
|
Chris@1616
|
167 * active at that frame, either because they begin at that frame
|
Chris@1616
|
168 * or because they are continuing from an earlier frame. There is
|
Chris@1616
|
169 * an entry here for each frame at which an event starts or ends,
|
Chris@1616
|
170 * with the event appearing in all entries from its start time
|
Chris@1616
|
171 * onward and disappearing again at its end frame.
|
Chris@1616
|
172 *
|
Chris@1616
|
173 * Only events with duration appear in this map; point events
|
Chris@1627
|
174 * appear only in m_events. Note that unlike m_events, we only
|
Chris@1627
|
175 * store one instance of each event here, even if we hold many -
|
Chris@1627
|
176 * we refer back to m_events when we need to know how many
|
Chris@1627
|
177 * identical copies of a given event we have.
|
Chris@1616
|
178 */
|
Chris@1627
|
179 typedef std::map<sv_frame_t, std::vector<Event>> FrameEventMap;
|
Chris@1616
|
180 FrameEventMap m_seams;
|
Chris@1614
|
181
|
Chris@1614
|
182 /** Create a seam at the given frame, copying from the prior seam
|
Chris@1614
|
183 * if there is one. If a seam already exists at the given frame,
|
Chris@1614
|
184 * leave it untouched.
|
Chris@1614
|
185 */
|
Chris@1614
|
186 void createSeam(sv_frame_t frame) {
|
Chris@1625
|
187 auto itr = m_seams.lower_bound(frame);
|
Chris@1625
|
188 if (itr == m_seams.end() || itr->first > frame) {
|
Chris@1614
|
189 if (itr != m_seams.begin()) {
|
Chris@1614
|
190 --itr;
|
Chris@1614
|
191 }
|
Chris@1614
|
192 }
|
Chris@1614
|
193 if (itr == m_seams.end()) {
|
Chris@1614
|
194 m_seams[frame] = {};
|
Chris@1625
|
195 } else if (itr->first < frame) {
|
Chris@1625
|
196 m_seams[frame] = itr->second;
|
Chris@1625
|
197 } else if (itr->first > frame) { // itr must be begin()
|
Chris@1614
|
198 m_seams[frame] = {};
|
Chris@1614
|
199 }
|
Chris@1614
|
200 }
|
Chris@1614
|
201
|
Chris@1627
|
202 bool seamsEqual(const std::vector<Event> &s1,
|
Chris@1627
|
203 const std::vector<Event> &s2) const {
|
Chris@1627
|
204
|
Chris@1627
|
205 if (s1.size() != s2.size()) {
|
Chris@1627
|
206 return false;
|
Chris@1627
|
207 }
|
Chris@1627
|
208
|
Chris@1627
|
209 // precondition: no event appears more than once in s1 or more
|
Chris@1627
|
210 // than once in s2
|
Chris@1627
|
211
|
Chris@1627
|
212 #ifdef DEBUG_EVENT_SERIES
|
Chris@1627
|
213 for (int i = 0; in_range_for(s1, i); ++i) {
|
Chris@1627
|
214 for (int j = i + 1; in_range_for(s1, j); ++j) {
|
Chris@1627
|
215 if (s1[i] == s1[j] || s2[i] == s2[j]) {
|
Chris@1627
|
216 throw std::runtime_error
|
Chris@1627
|
217 ("debug error: duplicate event in s1 or s2");
|
Chris@1627
|
218 }
|
Chris@1627
|
219 }
|
Chris@1627
|
220 }
|
Chris@1627
|
221 #endif
|
Chris@1627
|
222
|
Chris@1627
|
223 std::set<Event> ee;
|
Chris@1627
|
224 for (const auto &e: s1) {
|
Chris@1627
|
225 ee.insert(e);
|
Chris@1627
|
226 }
|
Chris@1627
|
227 for (const auto &e: s2) {
|
Chris@1627
|
228 if (ee.find(e) == ee.end()) {
|
Chris@1627
|
229 return false;
|
Chris@1627
|
230 }
|
Chris@1627
|
231 }
|
Chris@1627
|
232 return true;
|
Chris@1627
|
233 }
|
Chris@1627
|
234
|
Chris@1615
|
235 #ifdef DEBUG_EVENT_SERIES
|
Chris@1615
|
236 void dumpEvents() const {
|
Chris@1618
|
237 std::cerr << "EVENTS (" << m_events.size() << ") [" << std::endl;
|
Chris@1616
|
238 for (const auto &i: m_events) {
|
Chris@1631
|
239 std::cerr << " " << i.toXmlString();
|
Chris@1614
|
240 }
|
Chris@1614
|
241 std::cerr << "]" << std::endl;
|
Chris@1614
|
242 }
|
Chris@1614
|
243
|
Chris@1614
|
244 void dumpSeams() const {
|
Chris@1618
|
245 std::cerr << "SEAMS (" << m_seams.size() << ") [" << std::endl;
|
Chris@1614
|
246 for (const auto &s: m_seams) {
|
Chris@1614
|
247 std::cerr << " " << s.first << " -> {" << std::endl;
|
Chris@1614
|
248 for (const auto &p: s.second) {
|
Chris@1614
|
249 std::cerr << p.toXmlString(" ");
|
Chris@1614
|
250 }
|
Chris@1614
|
251 std::cerr << " }" << std::endl;
|
Chris@1614
|
252 }
|
Chris@1614
|
253 std::cerr << "]" << std::endl;
|
Chris@1614
|
254 }
|
Chris@1614
|
255 #endif
|
Chris@1612
|
256 };
|
Chris@1612
|
257
|
Chris@1612
|
258 #endif
|