annotate 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
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@1738 99
Chris@1677 100 // Our dataset doesn't have its own export ID, we just use
Chris@1677 101 // ours. Actually any model could do that, since datasets
Chris@1738 102 // aren't in the same id-space as models (or paths) when
Chris@1738 103 // re-read
Chris@1662 104
Chris@1738 105 out << indent;
Chris@1738 106 out << QString("<model id=\"%1\" name=\"\" sampleRate=\"%2\" "
Chris@1738 107 "type=\"sparse\" dimensions=\"2\" resolution=\"%3\" "
Chris@1738 108 "notifyOnAdd=\"true\" dataset=\"%4\" "
Chris@1738 109 "subtype=\"path\" %5/>\n")
Chris@1738 110 .arg(getExportId())
Chris@1738 111 .arg(m_sampleRate)
Chris@1738 112 .arg(m_resolution)
Chris@1738 113 .arg(getExportId())
Chris@1738 114 .arg(extraAttributes);
Chris@1662 115
Chris@1675 116 out << indent << QString("<dataset id=\"%1\" dimensions=\"2\">\n")
Chris@1677 117 .arg(getExportId());
Chris@1662 118
Chris@1675 119 for (PathPoint p: m_points) {
Chris@1675 120 p.toXml(out, indent + " ", "");
Chris@1675 121 }
Chris@1675 122
Chris@1675 123 out << indent << "</dataset>\n";
Chris@1662 124 }
Chris@1679 125
Chris@1679 126 QString toDelimitedDataString(QString delimiter,
Chris@1679 127 DataExportOptions,
Chris@1679 128 sv_frame_t startFrame,
Chris@1738 129 sv_frame_t duration) const {
Chris@1679 130
Chris@1679 131 QString s;
Chris@1679 132 for (PathPoint p: m_points) {
Chris@1679 133 if (p.frame < startFrame) continue;
Chris@1679 134 if (p.frame >= startFrame + duration) break;
Chris@1679 135 s += QString("%1%2%3\n")
Chris@1679 136 .arg(p.frame)
Chris@1679 137 .arg(delimiter)
Chris@1679 138 .arg(p.mapframe);
Chris@1679 139 }
Chris@1679 140
Chris@1679 141 return s;
Chris@1679 142 }
Chris@1662 143
Chris@1662 144 protected:
Chris@1662 145 sv_samplerate_t m_sampleRate;
Chris@1662 146 int m_resolution;
Chris@1738 147 Points m_points;
Chris@407 148 };
Chris@407 149
Chris@407 150
Chris@407 151 #endif