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@1618
|
37 * Performance is highly dependent on the extent of overlapping events
|
Chris@1618
|
38 * and the order in which events are added. Each event (with duration)
|
Chris@1618
|
39 * that is added requires updating all the seams within the extent of
|
Chris@1618
|
40 * that event, taking a number of ordered-set updates proportional to
|
Chris@1618
|
41 * the number of events already existing within its extent. Add events
|
Chris@1618
|
42 * in order of start frame if possible.
|
Chris@1615
|
43 */
|
Chris@1631
|
44 class EventSeries : public XmlExportable
|
Chris@1612
|
45 {
|
Chris@1612
|
46 public:
|
Chris@1631
|
47 EventSeries() { }
|
Chris@1616
|
48 ~EventSeries() =default;
|
Chris@1616
|
49
|
Chris@1616
|
50 EventSeries(const EventSeries &) =default;
|
Chris@1616
|
51 EventSeries &operator=(const EventSeries &) =default;
|
Chris@1616
|
52 EventSeries &operator=(EventSeries &&) =default;
|
Chris@1616
|
53
|
Chris@1616
|
54 bool operator==(const EventSeries &other) const {
|
Chris@1616
|
55 return m_events == other.m_events;
|
Chris@1616
|
56 }
|
Chris@1612
|
57
|
Chris@1631
|
58 void clear();
|
Chris@1631
|
59 void add(const Event &p);
|
Chris@1631
|
60 void remove(const Event &p);
|
Chris@1631
|
61 bool contains(const Event &p) const;
|
Chris@1631
|
62 bool isEmpty() const;
|
Chris@1631
|
63 int count() const;
|
Chris@1612
|
64
|
Chris@1612
|
65 /**
|
Chris@1616
|
66 * Retrieve all events any part of which falls within the span in
|
Chris@1616
|
67 * frames defined by the given frame f and duration d.
|
Chris@1616
|
68 *
|
Chris@1616
|
69 * - An event without duration is within the span if its own frame
|
Chris@1616
|
70 * is greater than or equal to f and less than f + d.
|
Chris@1616
|
71 *
|
Chris@1616
|
72 * - An event with duration is within the span if its start frame
|
Chris@1616
|
73 * is less than f + d and its start frame plus its duration is
|
Chris@1616
|
74 * greater than f.
|
Chris@1616
|
75 *
|
Chris@1619
|
76 * Note: Passing a duration of zero is seldom useful here; you
|
Chris@1619
|
77 * probably want getEventsCovering instead. getEventsSpanning(f,
|
Chris@1619
|
78 * 0) is not equivalent to getEventsCovering(f). The latter
|
Chris@1619
|
79 * includes durationless events at f and events starting at f,
|
Chris@1619
|
80 * both of which are excluded from the former.
|
Chris@1616
|
81 */
|
Chris@1617
|
82 EventVector getEventsSpanning(sv_frame_t frame,
|
Chris@1631
|
83 sv_frame_t duration) const;
|
Chris@1616
|
84
|
Chris@1616
|
85 /**
|
Chris@1616
|
86 * Retrieve all events that cover the given frame. An event without
|
Chris@1616
|
87 * duration covers a frame if its own frame is equal to it. An event
|
Chris@1616
|
88 * with duration covers a frame if its start frame is less than or
|
Chris@1612
|
89 * equal to it and its end frame (start + duration) is greater
|
Chris@1612
|
90 * than it.
|
Chris@1612
|
91 */
|
Chris@1631
|
92 EventVector getEventsCovering(sv_frame_t frame) const;
|
Chris@1614
|
93
|
Chris@1631
|
94 /**
|
Chris@1631
|
95 * Emit to XML as a dataset element.
|
Chris@1631
|
96 */
|
Chris@1631
|
97 void toXml(QTextStream &out,
|
Chris@1631
|
98 QString indent,
|
Chris@1631
|
99 QString extraAttributes) const override;
|
Chris@1631
|
100
|
Chris@1612
|
101 private:
|
Chris@1616
|
102 /**
|
Chris@1631
|
103 * This vector contains all events in the series, in the normal
|
Chris@1631
|
104 * sort order. For backward compatibility we must support series
|
Chris@1631
|
105 * containing multiple instances of identical events, so
|
Chris@1631
|
106 * consecutive events in this vector will not always be distinct.
|
Chris@1631
|
107 * The vector is used in preference to a multiset or map<Event,
|
Chris@1631
|
108 * int> in order to allow indexing by "row number" as well as by
|
Chris@1631
|
109 * properties such as frame.
|
Chris@1631
|
110 *
|
Chris@1631
|
111 * Because events are immutable, we do not have to worry about the
|
Chris@1631
|
112 * order changing once an event is inserted - we only add or
|
Chris@1631
|
113 * delete them.
|
Chris@1616
|
114 */
|
Chris@1631
|
115 typedef std::vector<Event> Events;
|
Chris@1616
|
116 Events m_events;
|
Chris@1631
|
117
|
Chris@1616
|
118 /**
|
Chris@1616
|
119 * The FrameEventMap maps from frame number to a set of events. In
|
Chris@1616
|
120 * the seam map this is used to represent the events that are
|
Chris@1616
|
121 * active at that frame, either because they begin at that frame
|
Chris@1616
|
122 * or because they are continuing from an earlier frame. There is
|
Chris@1616
|
123 * an entry here for each frame at which an event starts or ends,
|
Chris@1616
|
124 * with the event appearing in all entries from its start time
|
Chris@1616
|
125 * onward and disappearing again at its end frame.
|
Chris@1616
|
126 *
|
Chris@1616
|
127 * Only events with duration appear in this map; point events
|
Chris@1627
|
128 * appear only in m_events. Note that unlike m_events, we only
|
Chris@1627
|
129 * store one instance of each event here, even if we hold many -
|
Chris@1627
|
130 * we refer back to m_events when we need to know how many
|
Chris@1627
|
131 * identical copies of a given event we have.
|
Chris@1616
|
132 */
|
Chris@1627
|
133 typedef std::map<sv_frame_t, std::vector<Event>> FrameEventMap;
|
Chris@1616
|
134 FrameEventMap m_seams;
|
Chris@1614
|
135
|
Chris@1614
|
136 /** Create a seam at the given frame, copying from the prior seam
|
Chris@1614
|
137 * if there is one. If a seam already exists at the given frame,
|
Chris@1614
|
138 * leave it untouched.
|
Chris@1614
|
139 */
|
Chris@1614
|
140 void createSeam(sv_frame_t frame) {
|
Chris@1625
|
141 auto itr = m_seams.lower_bound(frame);
|
Chris@1625
|
142 if (itr == m_seams.end() || itr->first > frame) {
|
Chris@1614
|
143 if (itr != m_seams.begin()) {
|
Chris@1614
|
144 --itr;
|
Chris@1614
|
145 }
|
Chris@1614
|
146 }
|
Chris@1614
|
147 if (itr == m_seams.end()) {
|
Chris@1614
|
148 m_seams[frame] = {};
|
Chris@1625
|
149 } else if (itr->first < frame) {
|
Chris@1625
|
150 m_seams[frame] = itr->second;
|
Chris@1625
|
151 } else if (itr->first > frame) { // itr must be begin()
|
Chris@1614
|
152 m_seams[frame] = {};
|
Chris@1614
|
153 }
|
Chris@1614
|
154 }
|
Chris@1614
|
155
|
Chris@1627
|
156 bool seamsEqual(const std::vector<Event> &s1,
|
Chris@1627
|
157 const std::vector<Event> &s2) const {
|
Chris@1627
|
158
|
Chris@1627
|
159 if (s1.size() != s2.size()) {
|
Chris@1627
|
160 return false;
|
Chris@1627
|
161 }
|
Chris@1627
|
162
|
Chris@1627
|
163 // precondition: no event appears more than once in s1 or more
|
Chris@1627
|
164 // than once in s2
|
Chris@1627
|
165
|
Chris@1627
|
166 #ifdef DEBUG_EVENT_SERIES
|
Chris@1627
|
167 for (int i = 0; in_range_for(s1, i); ++i) {
|
Chris@1627
|
168 for (int j = i + 1; in_range_for(s1, j); ++j) {
|
Chris@1627
|
169 if (s1[i] == s1[j] || s2[i] == s2[j]) {
|
Chris@1627
|
170 throw std::runtime_error
|
Chris@1627
|
171 ("debug error: duplicate event in s1 or s2");
|
Chris@1627
|
172 }
|
Chris@1627
|
173 }
|
Chris@1627
|
174 }
|
Chris@1627
|
175 #endif
|
Chris@1627
|
176
|
Chris@1627
|
177 std::set<Event> ee;
|
Chris@1627
|
178 for (const auto &e: s1) {
|
Chris@1627
|
179 ee.insert(e);
|
Chris@1627
|
180 }
|
Chris@1627
|
181 for (const auto &e: s2) {
|
Chris@1627
|
182 if (ee.find(e) == ee.end()) {
|
Chris@1627
|
183 return false;
|
Chris@1627
|
184 }
|
Chris@1627
|
185 }
|
Chris@1627
|
186 return true;
|
Chris@1627
|
187 }
|
Chris@1627
|
188
|
Chris@1615
|
189 #ifdef DEBUG_EVENT_SERIES
|
Chris@1615
|
190 void dumpEvents() const {
|
Chris@1618
|
191 std::cerr << "EVENTS (" << m_events.size() << ") [" << std::endl;
|
Chris@1616
|
192 for (const auto &i: m_events) {
|
Chris@1631
|
193 std::cerr << " " << i.toXmlString();
|
Chris@1614
|
194 }
|
Chris@1614
|
195 std::cerr << "]" << std::endl;
|
Chris@1614
|
196 }
|
Chris@1614
|
197
|
Chris@1614
|
198 void dumpSeams() const {
|
Chris@1618
|
199 std::cerr << "SEAMS (" << m_seams.size() << ") [" << std::endl;
|
Chris@1614
|
200 for (const auto &s: m_seams) {
|
Chris@1614
|
201 std::cerr << " " << s.first << " -> {" << std::endl;
|
Chris@1614
|
202 for (const auto &p: s.second) {
|
Chris@1614
|
203 std::cerr << p.toXmlString(" ");
|
Chris@1614
|
204 }
|
Chris@1614
|
205 std::cerr << " }" << std::endl;
|
Chris@1614
|
206 }
|
Chris@1614
|
207 std::cerr << "]" << std::endl;
|
Chris@1614
|
208 }
|
Chris@1614
|
209 #endif
|
Chris@1612
|
210 };
|
Chris@1612
|
211
|
Chris@1612
|
212 #endif
|