Chris@407: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@407: Chris@407: /* Chris@407: Sonic Visualiser Chris@407: An audio file viewer and annotation editor. Chris@407: Centre for Digital Music, Queen Mary, University of London. Chris@407: Chris@407: This program is free software; you can redistribute it and/or Chris@407: modify it under the terms of the GNU General Public License as Chris@407: published by the Free Software Foundation; either version 2 of the Chris@407: License, or (at your option) any later version. See the file Chris@407: COPYING included with this distribution for more information. Chris@407: */ Chris@407: Chris@1738: #ifndef SV_PATH_H Chris@1738: #define SV_PATH_H Chris@407: Chris@1662: #include "base/XmlExportable.h" Chris@1662: #include "base/RealTime.h" Chris@1738: #include "base/BaseTypes.h" Chris@1662: Chris@407: #include Chris@1662: #include Chris@407: Chris@407: struct PathPoint Chris@407: { Chris@1662: PathPoint(sv_frame_t _frame) : Chris@1662: frame(_frame), mapframe(_frame) { } Chris@1040: PathPoint(sv_frame_t _frame, sv_frame_t _mapframe) : Chris@407: frame(_frame), mapframe(_mapframe) { } Chris@407: Chris@1040: sv_frame_t frame; Chris@1040: sv_frame_t mapframe; Chris@407: Chris@407: void toXml(QTextStream &stream, QString indent = "", Chris@407: QString extraAttributes = "") const { Chris@407: stream << QString("%1\n") Chris@407: .arg(indent).arg(frame).arg(mapframe).arg(extraAttributes); Chris@407: } Chris@407: Chris@1060: QString toDelimitedDataString(QString delimiter, DataExportOptions, Chris@1040: sv_samplerate_t sampleRate) const { Chris@407: QStringList list; Chris@407: list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str(); Chris@407: list << QString("%1").arg(mapframe); Chris@407: return list.join(delimiter); Chris@407: } Chris@407: Chris@1662: bool operator<(const PathPoint &p2) const { Chris@1662: if (frame != p2.frame) return frame < p2.frame; Chris@1662: return mapframe < p2.mapframe; Chris@1662: } Chris@407: }; Chris@407: Chris@1738: class Path : public XmlExportable Chris@407: { Chris@407: public: Chris@1738: Path(sv_samplerate_t sampleRate, int resolution) : Chris@1738: m_sampleRate(sampleRate), Chris@1738: m_resolution(resolution) { Chris@1738: } Chris@1738: Path(const Path &) =default; Chris@1738: Path &operator=(const Path &) =default; Chris@407: Chris@1738: typedef std::set Points; Chris@1662: Chris@1738: sv_samplerate_t getSampleRate() const { return m_sampleRate; } Chris@1738: int getResolution() const { return m_resolution; } Chris@1662: Chris@1670: int getPointCount() const { Chris@1670: return int(m_points.size()); Chris@1670: } Chris@1738: Chris@1738: const Points &getPoints() const { Chris@1662: return m_points; Chris@1662: } Chris@425: Chris@1662: void add(PathPoint p) { Chris@1738: m_points.insert(p); Chris@1662: } Chris@1662: Chris@1662: void remove(PathPoint p) { Chris@1738: m_points.erase(p); Chris@1662: } Chris@1662: Chris@1662: void clear() { Chris@1738: m_points.clear(); Chris@1662: } Chris@1662: Chris@1662: /** Chris@1662: * XmlExportable methods. Chris@1662: */ Chris@1662: void toXml(QTextStream &out, Chris@1738: QString indent = "", Chris@1738: QString extraAttributes = "") const override { Chris@1677: Chris@1738: // For historical reasons we serialise a Path as a model, Chris@1738: // although the class itself no longer is. Chris@1738: Chris@1677: // Our dataset doesn't have its own export ID, we just use Chris@1677: // ours. Actually any model could do that, since datasets Chris@1738: // aren't in the same id-space as models (or paths) when Chris@1738: // re-read Chris@1662: Chris@1738: out << indent; Chris@1738: out << QString("\n") Chris@1738: .arg(getExportId()) Chris@1738: .arg(m_sampleRate) Chris@1738: .arg(m_resolution) Chris@1738: .arg(getExportId()) Chris@1738: .arg(extraAttributes); Chris@1662: Chris@1675: out << indent << QString("\n") Chris@1677: .arg(getExportId()); Chris@1662: Chris@1675: for (PathPoint p: m_points) { Chris@1675: p.toXml(out, indent + " ", ""); Chris@1675: } Chris@1675: Chris@1675: out << indent << "\n"; Chris@1662: } Chris@1679: Chris@1679: QString toDelimitedDataString(QString delimiter, Chris@1679: DataExportOptions, Chris@1679: sv_frame_t startFrame, Chris@1738: sv_frame_t duration) const { Chris@1679: Chris@1679: QString s; Chris@1679: for (PathPoint p: m_points) { Chris@1679: if (p.frame < startFrame) continue; Chris@1679: if (p.frame >= startFrame + duration) break; Chris@1679: s += QString("%1%2%3\n") Chris@1679: .arg(p.frame) Chris@1679: .arg(delimiter) Chris@1679: .arg(p.mapframe); Chris@1679: } Chris@1679: Chris@1679: return s; Chris@1679: } Chris@1662: Chris@1662: protected: Chris@1662: sv_samplerate_t m_sampleRate; Chris@1662: int m_resolution; Chris@1738: Points m_points; Chris@407: }; Chris@407: Chris@407: Chris@407: #endif