annotate data/model/Path.h @ 1794:71e97de5053f

Merge from branch time-frequency-boxes
author Chris Cannam
date Wed, 25 Sep 2019 13:47:46 +0100
parents 9d82b164f264
children 14bf9bf5ac28
rev   line source
Chris@407 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@407 2
Chris@407 3 /*
Chris@407 4 Sonic Visualiser
Chris@407 5 An audio file viewer and annotation editor.
Chris@407 6 Centre for Digital Music, Queen Mary, University of London.
Chris@407 7
Chris@407 8 This program is free software; you can redistribute it and/or
Chris@407 9 modify it under the terms of the GNU General Public License as
Chris@407 10 published by the Free Software Foundation; either version 2 of the
Chris@407 11 License, or (at your option) any later version. See the file
Chris@407 12 COPYING included with this distribution for more information.
Chris@407 13 */
Chris@407 14
Chris@1738 15 #ifndef SV_PATH_H
Chris@1738 16 #define SV_PATH_H
Chris@407 17
Chris@1662 18 #include "base/XmlExportable.h"
Chris@1662 19 #include "base/RealTime.h"
Chris@1738 20 #include "base/BaseTypes.h"
Chris@1662 21
Chris@407 22 #include <QStringList>
Chris@1662 23 #include <set>
Chris@407 24
Chris@407 25 struct PathPoint
Chris@407 26 {
Chris@1662 27 PathPoint(sv_frame_t _frame) :
Chris@1662 28 frame(_frame), mapframe(_frame) { }
Chris@1040 29 PathPoint(sv_frame_t _frame, sv_frame_t _mapframe) :
Chris@407 30 frame(_frame), mapframe(_mapframe) { }
Chris@407 31
Chris@1040 32 sv_frame_t frame;
Chris@1040 33 sv_frame_t mapframe;
Chris@407 34
Chris@407 35 void toXml(QTextStream &stream, QString indent = "",
Chris@407 36 QString extraAttributes = "") const {
Chris@407 37 stream << QString("%1<point frame=\"%2\" mapframe=\"%3\" %4/>\n")
Chris@407 38 .arg(indent).arg(frame).arg(mapframe).arg(extraAttributes);
Chris@407 39 }
Chris@407 40
Chris@1060 41 QString toDelimitedDataString(QString delimiter, DataExportOptions,
Chris@1040 42 sv_samplerate_t sampleRate) const {
Chris@407 43 QStringList list;
Chris@407 44 list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
Chris@407 45 list << QString("%1").arg(mapframe);
Chris@407 46 return list.join(delimiter);
Chris@407 47 }
Chris@407 48
Chris@1662 49 bool operator<(const PathPoint &p2) const {
Chris@1662 50 if (frame != p2.frame) return frame < p2.frame;
Chris@1662 51 return mapframe < p2.mapframe;
Chris@1662 52 }
Chris@407 53 };
Chris@407 54
Chris@1738 55 class Path : public XmlExportable
Chris@407 56 {
Chris@407 57 public:
Chris@1738 58 Path(sv_samplerate_t sampleRate, int resolution) :
Chris@1738 59 m_sampleRate(sampleRate),
Chris@1738 60 m_resolution(resolution) {
Chris@1738 61 }
Chris@1738 62 Path(const Path &) =default;
Chris@1738 63 Path &operator=(const Path &) =default;
Chris@407 64
Chris@1738 65 typedef std::set<PathPoint> Points;
Chris@1662 66
Chris@1738 67 sv_samplerate_t getSampleRate() const { return m_sampleRate; }
Chris@1738 68 int getResolution() const { return m_resolution; }
Chris@1662 69
Chris@1670 70 int getPointCount() const {
Chris@1670 71 return int(m_points.size());
Chris@1670 72 }
Chris@1738 73
Chris@1738 74 const Points &getPoints() const {
Chris@1662 75 return m_points;
Chris@1662 76 }
Chris@425 77
Chris@1662 78 void add(PathPoint p) {
Chris@1738 79 m_points.insert(p);
Chris@1662 80 }
Chris@1662 81
Chris@1662 82 void remove(PathPoint p) {
Chris@1738 83 m_points.erase(p);
Chris@1662 84 }
Chris@1662 85
Chris@1662 86 void clear() {
Chris@1738 87 m_points.clear();
Chris@1662 88 }
Chris@1662 89
Chris@1662 90 /**
Chris@1662 91 * XmlExportable methods.
Chris@1662 92 */
Chris@1662 93 void toXml(QTextStream &out,
Chris@1738 94 QString indent = "",
Chris@1738 95 QString extraAttributes = "") const override {
Chris@1677 96
Chris@1738 97 // For historical reasons we serialise a Path as a model,
Chris@1738 98 // although the class itself no longer is.
Chris@1741 99
Chris@1741 100 // We also write start and end frames - which our API no
Chris@1741 101 // longer exposes - just for backward compatibility
Chris@1741 102
Chris@1741 103 sv_frame_t start = 0;
Chris@1741 104 sv_frame_t end = 0;
Chris@1741 105 if (!m_points.empty()) {
Chris@1741 106 start = m_points.begin()->frame;
Chris@1741 107 end = m_points.rbegin()->frame + m_resolution;
Chris@1741 108 }
Chris@1738 109
Chris@1677 110 // Our dataset doesn't have its own export ID, we just use
Chris@1677 111 // ours. Actually any model could do that, since datasets
Chris@1738 112 // aren't in the same id-space as models (or paths) when
Chris@1738 113 // re-read
Chris@1662 114
Chris@1738 115 out << indent;
Chris@1738 116 out << QString("<model id=\"%1\" name=\"\" sampleRate=\"%2\" "
Chris@1741 117 "start=\"%3\" end=\"%4\" type=\"sparse\" "
Chris@1741 118 "dimensions=\"2\" resolution=\"%5\" "
Chris@1741 119 "notifyOnAdd=\"true\" dataset=\"%6\" "
Chris@1741 120 "subtype=\"path\" %7/>\n")
Chris@1738 121 .arg(getExportId())
Chris@1738 122 .arg(m_sampleRate)
Chris@1741 123 .arg(start)
Chris@1741 124 .arg(end)
Chris@1738 125 .arg(m_resolution)
Chris@1738 126 .arg(getExportId())
Chris@1738 127 .arg(extraAttributes);
Chris@1662 128
Chris@1675 129 out << indent << QString("<dataset id=\"%1\" dimensions=\"2\">\n")
Chris@1677 130 .arg(getExportId());
Chris@1662 131
Chris@1675 132 for (PathPoint p: m_points) {
Chris@1675 133 p.toXml(out, indent + " ", "");
Chris@1675 134 }
Chris@1675 135
Chris@1675 136 out << indent << "</dataset>\n";
Chris@1662 137 }
Chris@1679 138
Chris@1679 139 QString toDelimitedDataString(QString delimiter,
Chris@1679 140 DataExportOptions,
Chris@1679 141 sv_frame_t startFrame,
Chris@1738 142 sv_frame_t duration) const {
Chris@1679 143
Chris@1679 144 QString s;
Chris@1679 145 for (PathPoint p: m_points) {
Chris@1679 146 if (p.frame < startFrame) continue;
Chris@1679 147 if (p.frame >= startFrame + duration) break;
Chris@1679 148 s += QString("%1%2%3\n")
Chris@1679 149 .arg(p.frame)
Chris@1679 150 .arg(delimiter)
Chris@1679 151 .arg(p.mapframe);
Chris@1679 152 }
Chris@1679 153
Chris@1679 154 return s;
Chris@1679 155 }
Chris@1662 156
Chris@1662 157 protected:
Chris@1662 158 sv_samplerate_t m_sampleRate;
Chris@1662 159 int m_resolution;
Chris@1738 160 Points m_points;
Chris@407 161 };
Chris@407 162
Chris@407 163
Chris@407 164 #endif