Path.h
Go to the documentation of this file.
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 {
28  frame(_frame), mapframe(_frame) { }
29  PathPoint(sv_frame_t _frame, sv_frame_t _mapframe) :
30  frame(_frame), mapframe(_mapframe) { }
31 
32  // "The path consists of a series of points, each with frame equal
33  // to the frame on the source model (aligned model) and mapframe
34  // equal to the frame on the target model (reference model). Both
35  // should be monotonically increasing."
36 
39 
40  void toXml(QTextStream &stream, QString indent = "",
41  QString extraAttributes = "") const {
42  stream << QString("%1<point frame=\"%2\" mapframe=\"%3\" %4/>\n")
43  .arg(indent).arg(frame).arg(mapframe).arg(extraAttributes);
44  }
45 
46  QString toDelimitedDataString(QString delimiter, DataExportOptions,
47  sv_samplerate_t sampleRate) const {
48  QStringList list;
49  list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
50  list << QString("%1").arg(mapframe);
51  return list.join(delimiter);
52  }
53 
54  bool operator<(const PathPoint &p2) const {
55  if (frame != p2.frame) return frame < p2.frame;
56  return mapframe < p2.mapframe;
57  }
58 };
59 
60 class Path : public XmlExportable
61 {
62 public:
63  Path(sv_samplerate_t sampleRate, int resolution) :
64  m_sampleRate(sampleRate),
65  m_resolution(resolution) {
66  }
67  Path(const Path &) =default;
68  Path &operator=(const Path &) =default;
69 
70  typedef std::set<PathPoint> Points;
71 
72  sv_samplerate_t getSampleRate() const { return m_sampleRate; }
73  int getResolution() const { return m_resolution; }
74 
75  int getPointCount() const {
76  return int(m_points.size());
77  }
78 
79  const Points &getPoints() const {
80  return m_points;
81  }
82 
83  void add(PathPoint p) {
84  m_points.insert(p);
85  }
86 
87  void remove(PathPoint p) {
88  m_points.erase(p);
89  }
90 
91  void clear() {
92  m_points.clear();
93  }
94 
98  void toXml(QTextStream &out,
99  QString indent = "",
100  QString extraAttributes = "") const override {
101 
102  // For historical reasons we serialise a Path as a model,
103  // although the class itself no longer is.
104 
105  // We also write start and end frames - which our API no
106  // longer exposes - just for backward compatibility
107 
108  sv_frame_t start = 0;
109  sv_frame_t end = 0;
110  if (!m_points.empty()) {
111  start = m_points.begin()->frame;
112  end = m_points.rbegin()->frame + m_resolution;
113  }
114 
115  // Our dataset doesn't have its own export ID, we just use
116  // ours. Actually any model could do that, since datasets
117  // aren't in the same id-space as models (or paths) when
118  // re-read
119 
120  out << indent;
121  out << QString("<model id=\"%1\" name=\"\" sampleRate=\"%2\" "
122  "start=\"%3\" end=\"%4\" type=\"sparse\" "
123  "dimensions=\"2\" resolution=\"%5\" "
124  "notifyOnAdd=\"true\" dataset=\"%6\" "
125  "subtype=\"path\" %7/>\n")
126  .arg(getExportId())
127  .arg(m_sampleRate)
128  .arg(start)
129  .arg(end)
130  .arg(m_resolution)
131  .arg(getExportId())
132  .arg(extraAttributes);
133 
134  out << indent << QString("<dataset id=\"%1\" dimensions=\"2\">\n")
135  .arg(getExportId());
136 
137  for (PathPoint p: m_points) {
138  p.toXml(out, indent + " ", "");
139  }
140 
141  out << indent << "</dataset>\n";
142  }
143 
144  QString toDelimitedDataString(QString delimiter,
146  sv_frame_t startFrame,
147  sv_frame_t duration) const {
148 
149  QString s;
150  for (PathPoint p: m_points) {
151  if (p.frame < startFrame) continue;
152  if (p.frame >= startFrame + duration) break;
153  s += QString("%1%2%3\n")
154  .arg(p.frame)
155  .arg(delimiter)
156  .arg(p.mapframe);
157  }
158 
159  return s;
160  }
161 
162 protected:
165  Points m_points;
166 };
167 
168 
169 #endif
bool operator<(const PathPoint &p2) const
Definition: Path.h:54
double sv_samplerate_t
Sample rate.
Definition: BaseTypes.h:51
std::set< PathPoint > Points
Definition: Path.h:70
Path(sv_samplerate_t sampleRate, int resolution)
Definition: Path.h:63
PathPoint(sv_frame_t _frame)
Definition: Path.h:27
int64_t sv_frame_t
Frame index, the unit of our time axis.
Definition: BaseTypes.h:31
static RealTime frame2RealTime(sv_frame_t frame, sv_samplerate_t sampleRate)
Convert a sample frame at the given sample rate into a RealTime.
Definition: RealTimeSV.cpp:498
void toXml(QTextStream &stream, QString indent="", QString extraAttributes="") const
Definition: Path.h:40
int m_resolution
Definition: Path.h:164
int getPointCount() const
Definition: Path.h:75
void add(PathPoint p)
Definition: Path.h:83
sv_frame_t mapframe
Definition: Path.h:38
sv_samplerate_t getSampleRate() const
Definition: Path.h:72
std::string toString(bool align=false) const
Return a human-readable debug-type string to full precision (probably not a format to show to a user ...
Definition: RealTimeSV.cpp:213
const Points & getPoints() const
Definition: Path.h:79
QString toDelimitedDataString(QString delimiter, DataExportOptions, sv_samplerate_t sampleRate) const
Definition: Path.h:46
QString toDelimitedDataString(QString delimiter, DataExportOptions, sv_frame_t startFrame, sv_frame_t duration) const
Definition: Path.h:144
Definition: Path.h:25
Definition: Path.h:60
void toXml(QTextStream &out, QString indent="", QString extraAttributes="") const override
XmlExportable methods.
Definition: Path.h:98
sv_frame_t frame
Definition: Path.h:37
void clear()
Definition: Path.h:91
int getResolution() const
Definition: Path.h:73
Points m_points
Definition: Path.h:165
sv_samplerate_t m_sampleRate
Definition: Path.h:163
PathPoint(sv_frame_t _frame, sv_frame_t _mapframe)
Definition: Path.h:29
int DataExportOptions