comparison data/model/Path.h @ 1738:4abc0f08adf9 by-id

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