annotate base/EventSeries.h @ 1794:71e97de5053f

Merge from branch time-frequency-boxes
author Chris Cannam
date Wed, 25 Sep 2019 13:47:46 +0100
parents 1bbea26ea21a
children 94b488d4b299
rev   line source
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@1687 22 #include <functional>
Chris@1612 23
Chris@1615 24 //#define DEBUG_EVENT_SERIES 1
Chris@1614 25
Chris@1615 26 /**
Chris@1615 27 * Container storing a series of events, with or without durations,
Chris@1616 28 * and supporting the ability to query which events are active at a
Chris@1616 29 * given frame or within a span of frames.
Chris@1616 30 *
Chris@1616 31 * To that end, in addition to the series of events, it stores a
Chris@1616 32 * series of "seams", which are frame positions at which the set of
Chris@1616 33 * simultaneous events changes (i.e. an event of non-zero duration
Chris@1616 34 * starts or ends) associated with a set of the events that are active
Chris@1616 35 * at or from that position. These are updated when an event is added
Chris@1616 36 * or removed.
Chris@1618 37 *
Chris@1632 38 * This class is highly optimised for inserting events in increasing
Chris@1632 39 * order of start frame. Inserting (or deleting) events in the middle
Chris@1632 40 * does work, and should be acceptable in interactive use, but it is
Chris@1632 41 * very slow in bulk.
Chris@1615 42 */
Chris@1631 43 class EventSeries : public XmlExportable
Chris@1612 44 {
Chris@1612 45 public:
Chris@1640 46 EventSeries() : m_finalDurationlessEventFrame(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@1679 56
Chris@1679 57 static EventSeries fromEvents(const EventVector &ee);
Chris@1612 58
Chris@1631 59 void clear();
Chris@1632 60 void add(const Event &e);
Chris@1632 61 void remove(const Event &e);
Chris@1632 62 bool contains(const Event &e) const;
Chris@1631 63 bool isEmpty() const;
Chris@1631 64 int count() const;
Chris@1612 65
Chris@1612 66 /**
Chris@1640 67 * Return the frame of the first event in the series. If there are
Chris@1640 68 * no events, return 0.
Chris@1640 69 */
Chris@1640 70 sv_frame_t getStartFrame() const;
Chris@1640 71
Chris@1640 72 /**
Chris@1640 73 * Return the frame plus duration of the event in the series that
Chris@1640 74 * ends last. If there are no events, return 0.
Chris@1640 75 */
Chris@1640 76 sv_frame_t getEndFrame() const;
Chris@1640 77
Chris@1640 78 /**
Chris@1635 79 * Retrieve all events any part of which falls within the range in
Chris@1616 80 * frames defined by the given frame f and duration d.
Chris@1616 81 *
Chris@1635 82 * - An event without duration is spanned by the range if its own
Chris@1635 83 * frame is greater than or equal to f and less than f + d.
Chris@1616 84 *
Chris@1635 85 * - An event with duration is spanned by the range if its start
Chris@1635 86 * frame is less than f + d and its start frame plus its duration
Chris@1635 87 * is greater than f.
Chris@1616 88 *
Chris@1619 89 * Note: Passing a duration of zero is seldom useful here; you
Chris@1619 90 * probably want getEventsCovering instead. getEventsSpanning(f,
Chris@1619 91 * 0) is not equivalent to getEventsCovering(f). The latter
Chris@1619 92 * includes durationless events at f and events starting at f,
Chris@1619 93 * both of which are excluded from the former.
Chris@1616 94 */
Chris@1617 95 EventVector getEventsSpanning(sv_frame_t frame,
Chris@1631 96 sv_frame_t duration) const;
Chris@1616 97
Chris@1616 98 /**
Chris@1656 99 * Retrieve all events that cover the given frame. An event without
Chris@1656 100 * duration covers a frame if its own frame is equal to it. An event
Chris@1656 101 * with duration covers a frame if its start frame is less than or
Chris@1656 102 * equal to it and its end frame (start + duration) is greater
Chris@1656 103 * than it.
Chris@1656 104 */
Chris@1656 105 EventVector getEventsCovering(sv_frame_t frame) const;
Chris@1656 106
Chris@1656 107 /**
Chris@1635 108 * Retrieve all events falling wholly within the range in frames
Chris@1635 109 * defined by the given frame f and duration d.
Chris@1635 110 *
Chris@1635 111 * - An event without duration is within the range if its own
Chris@1635 112 * frame is greater than or equal to f and less than f + d.
Chris@1635 113 *
Chris@1635 114 * - An event with duration is within the range if its start frame
Chris@1635 115 * is greater than or equal to f and its start frame plus its
Chris@1635 116 * duration is less than or equal to f + d.
Chris@1654 117 *
Chris@1654 118 * If overspill is greater than zero, also include that number of
Chris@1654 119 * additional events (where they exist) both before and after the
Chris@1654 120 * edges of the range.
Chris@1635 121 */
Chris@1635 122 EventVector getEventsWithin(sv_frame_t frame,
Chris@1654 123 sv_frame_t duration,
Chris@1654 124 int overspill = 0) const;
Chris@1635 125
Chris@1635 126 /**
Chris@1638 127 * Retrieve all events starting within the range in frames defined
Chris@1656 128 * by the given frame f and duration d. An event (regardless of
Chris@1656 129 * whether it has duration or not) starts within the range if its
Chris@1656 130 * start frame is greater than or equal to f and less than f + d.
Chris@1638 131 */
Chris@1638 132 EventVector getEventsStartingWithin(sv_frame_t frame,
Chris@1638 133 sv_frame_t duration) const;
Chris@1638 134
Chris@1638 135 /**
Chris@1656 136 * Retrieve all events starting at exactly the given frame.
Chris@1612 137 */
Chris@1656 138 EventVector getEventsStartingAt(sv_frame_t frame) const {
Chris@1656 139 return getEventsStartingWithin(frame, 1);
Chris@1656 140 }
Chris@1644 141
Chris@1644 142 /**
Chris@1644 143 * Retrieve all events, in their natural order.
Chris@1644 144 */
Chris@1644 145 EventVector getAllEvents() const;
Chris@1635 146
Chris@1631 147 /**
Chris@1632 148 * If e is in the series and is not the first event in it, set
Chris@1632 149 * preceding to the event immediate preceding it according to the
Chris@1632 150 * standard event ordering and return true. Otherwise leave
Chris@1632 151 * preceding unchanged and return false.
Chris@1632 152 *
Chris@1632 153 * If there are multiple events identical to e in the series,
Chris@1632 154 * assume that the event passed in is the first one (i.e. never
Chris@1632 155 * set preceding equal to e).
Chris@1633 156 *
Chris@1633 157 * It is acceptable for preceding to alias e when this is called.
Chris@1632 158 */
Chris@1632 159 bool getEventPreceding(const Event &e, Event &preceding) const;
Chris@1632 160
Chris@1632 161 /**
Chris@1632 162 * If e is in the series and is not the final event in it, set
Chris@1632 163 * following to the event immediate following it according to the
Chris@1632 164 * standard event ordering and return true. Otherwise leave
Chris@1632 165 * following unchanged and return false.
Chris@1632 166 *
Chris@1632 167 * If there are multiple events identical to e in the series,
Chris@1632 168 * assume that the event passed in is the last one (i.e. never set
Chris@1632 169 * following equal to e).
Chris@1633 170 *
Chris@1633 171 * It is acceptable for following to alias e when this is called.
Chris@1632 172 */
Chris@1632 173 bool getEventFollowing(const Event &e, Event &following) const;
Chris@1632 174
Chris@1653 175 enum Direction {
Chris@1653 176 Forward,
Chris@1653 177 Backward
Chris@1653 178 };
Chris@1653 179
Chris@1653 180 /**
Chris@1653 181 * Return the first event for which the given predicate returns
Chris@1653 182 * true, searching events with start frames increasingly far from
Chris@1653 183 * the given frame in the given direction. If the direction is
Chris@1653 184 * Forward then the search includes events starting at the given
Chris@1653 185 * frame, otherwise it does not.
Chris@1653 186 */
Chris@1653 187 bool getNearestEventMatching(sv_frame_t startSearchAt,
Chris@1653 188 std::function<bool(const Event &)> predicate,
Chris@1653 189 Direction direction,
Chris@1653 190 Event &found) const;
Chris@1653 191
Chris@1632 192 /**
Chris@1632 193 * Return the event at the given numerical index in the series,
Chris@1632 194 * where 0 = the first event and count()-1 = the last.
Chris@1632 195 */
Chris@1632 196 Event getEventByIndex(int index) const;
Chris@1640 197
Chris@1640 198 /**
Chris@1640 199 * Return the index of the first event in the series that does not
Chris@1640 200 * compare inferior to the given event. If there is no such event,
Chris@1640 201 * return count().
Chris@1640 202 */
Chris@1640 203 int getIndexForEvent(const Event &e) const;
Chris@1653 204
Chris@1632 205 /**
Chris@1631 206 * Emit to XML as a dataset element.
Chris@1631 207 */
Chris@1631 208 void toXml(QTextStream &out,
Chris@1631 209 QString indent,
Chris@1631 210 QString extraAttributes) const override;
Chris@1674 211
Chris@1674 212 /**
Chris@1674 213 * Emit to XML as a dataset element.
Chris@1674 214 */
Chris@1674 215 void toXml(QTextStream &out,
Chris@1674 216 QString indent,
Chris@1674 217 QString extraAttributes,
Chris@1674 218 Event::ExportNameOptions) const;
Chris@1679 219
Chris@1679 220 /**
Chris@1679 221 * Emit events starting within the given range to a delimited
Chris@1679 222 * (e.g. comma-separated) data format.
Chris@1679 223 */
Chris@1679 224 QString toDelimitedDataString(QString delimiter,
Chris@1679 225 DataExportOptions options,
Chris@1679 226 sv_frame_t startFrame,
Chris@1679 227 sv_frame_t duration,
Chris@1679 228 sv_samplerate_t sampleRate,
Chris@1679 229 sv_frame_t resolution,
Chris@1679 230 Event fillEvent) const;
Chris@1631 231
Chris@1612 232 private:
Chris@1616 233 /**
Chris@1631 234 * This vector contains all events in the series, in the normal
Chris@1631 235 * sort order. For backward compatibility we must support series
Chris@1631 236 * containing multiple instances of identical events, so
Chris@1631 237 * consecutive events in this vector will not always be distinct.
Chris@1631 238 * The vector is used in preference to a multiset or map<Event,
Chris@1631 239 * int> in order to allow indexing by "row number" as well as by
Chris@1631 240 * properties such as frame.
Chris@1631 241 *
Chris@1631 242 * Because events are immutable, we do not have to worry about the
Chris@1631 243 * order changing once an event is inserted - we only add or
Chris@1631 244 * delete them.
Chris@1616 245 */
Chris@1631 246 typedef std::vector<Event> Events;
Chris@1616 247 Events m_events;
Chris@1631 248
Chris@1616 249 /**
Chris@1616 250 * The FrameEventMap maps from frame number to a set of events. In
Chris@1616 251 * the seam map this is used to represent the events that are
Chris@1616 252 * active at that frame, either because they begin at that frame
Chris@1616 253 * or because they are continuing from an earlier frame. There is
Chris@1616 254 * an entry here for each frame at which an event starts or ends,
Chris@1616 255 * with the event appearing in all entries from its start time
Chris@1616 256 * onward and disappearing again at its end frame.
Chris@1616 257 *
Chris@1616 258 * Only events with duration appear in this map; point events
Chris@1627 259 * appear only in m_events. Note that unlike m_events, we only
Chris@1627 260 * store one instance of each event here, even if we hold many -
Chris@1627 261 * we refer back to m_events when we need to know how many
Chris@1627 262 * identical copies of a given event we have.
Chris@1616 263 */
Chris@1627 264 typedef std::map<sv_frame_t, std::vector<Event>> FrameEventMap;
Chris@1616 265 FrameEventMap m_seams;
Chris@1614 266
Chris@1640 267 /**
Chris@1640 268 * The frame of the last durationless event we have in the series.
Chris@1640 269 * This is to support a fast-ish getEndFrame(): we can easily keep
Chris@1640 270 * this up-to-date when events are added or removed, and we can
Chris@1640 271 * easily find the end frame of the last with-duration event from
Chris@1640 272 * the seam map, but it's not so easy to continuously update an
Chris@1640 273 * overall end frame or to find the last frame of all events
Chris@1640 274 * without this.
Chris@1640 275 */
Chris@1640 276 sv_frame_t m_finalDurationlessEventFrame;
Chris@1640 277
Chris@1614 278 /** Create a seam at the given frame, copying from the prior seam
Chris@1614 279 * if there is one. If a seam already exists at the given frame,
Chris@1614 280 * leave it untouched.
Chris@1614 281 */
Chris@1614 282 void createSeam(sv_frame_t frame) {
Chris@1625 283 auto itr = m_seams.lower_bound(frame);
Chris@1625 284 if (itr == m_seams.end() || itr->first > frame) {
Chris@1614 285 if (itr != m_seams.begin()) {
Chris@1614 286 --itr;
Chris@1614 287 }
Chris@1614 288 }
Chris@1614 289 if (itr == m_seams.end()) {
Chris@1614 290 m_seams[frame] = {};
Chris@1625 291 } else if (itr->first < frame) {
Chris@1625 292 m_seams[frame] = itr->second;
Chris@1625 293 } else if (itr->first > frame) { // itr must be begin()
Chris@1614 294 m_seams[frame] = {};
Chris@1614 295 }
Chris@1614 296 }
Chris@1614 297
Chris@1627 298 bool seamsEqual(const std::vector<Event> &s1,
Chris@1627 299 const std::vector<Event> &s2) const {
Chris@1627 300
Chris@1627 301 if (s1.size() != s2.size()) {
Chris@1627 302 return false;
Chris@1627 303 }
Chris@1627 304
Chris@1627 305 // precondition: no event appears more than once in s1 or more
Chris@1627 306 // than once in s2
Chris@1627 307
Chris@1627 308 #ifdef DEBUG_EVENT_SERIES
Chris@1627 309 for (int i = 0; in_range_for(s1, i); ++i) {
Chris@1627 310 for (int j = i + 1; in_range_for(s1, j); ++j) {
Chris@1627 311 if (s1[i] == s1[j] || s2[i] == s2[j]) {
Chris@1627 312 throw std::runtime_error
Chris@1627 313 ("debug error: duplicate event in s1 or s2");
Chris@1627 314 }
Chris@1627 315 }
Chris@1627 316 }
Chris@1627 317 #endif
Chris@1627 318
Chris@1627 319 std::set<Event> ee;
Chris@1627 320 for (const auto &e: s1) {
Chris@1627 321 ee.insert(e);
Chris@1627 322 }
Chris@1627 323 for (const auto &e: s2) {
Chris@1627 324 if (ee.find(e) == ee.end()) {
Chris@1627 325 return false;
Chris@1627 326 }
Chris@1627 327 }
Chris@1627 328 return true;
Chris@1627 329 }
Chris@1627 330
Chris@1615 331 #ifdef DEBUG_EVENT_SERIES
Chris@1615 332 void dumpEvents() const {
Chris@1618 333 std::cerr << "EVENTS (" << m_events.size() << ") [" << std::endl;
Chris@1616 334 for (const auto &i: m_events) {
Chris@1631 335 std::cerr << " " << i.toXmlString();
Chris@1614 336 }
Chris@1614 337 std::cerr << "]" << std::endl;
Chris@1614 338 }
Chris@1614 339
Chris@1614 340 void dumpSeams() const {
Chris@1618 341 std::cerr << "SEAMS (" << m_seams.size() << ") [" << std::endl;
Chris@1614 342 for (const auto &s: m_seams) {
Chris@1614 343 std::cerr << " " << s.first << " -> {" << std::endl;
Chris@1614 344 for (const auto &p: s.second) {
Chris@1614 345 std::cerr << p.toXmlString(" ");
Chris@1614 346 }
Chris@1614 347 std::cerr << " }" << std::endl;
Chris@1614 348 }
Chris@1614 349 std::cerr << "]" << std::endl;
Chris@1614 350 }
Chris@1614 351 #endif
Chris@1612 352 };
Chris@1612 353
Chris@1612 354 #endif