File: Path.h

package info (click to toggle)
sonic-visualiser 5.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 24,640 kB
  • sloc: cpp: 158,888; ansic: 11,920; sh: 1,785; makefile: 517; xml: 64; perl: 31
file content (174 lines) | stat: -rw-r--r-- 5,125 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.
    Centre for Digital Music, Queen Mary, University of London.
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#ifndef SV_PATH_H
#define SV_PATH_H

#include "base/XmlExportable.h"
#include "base/RealTime.h"
#include "base/BaseTypes.h"

#include <QStringList>
#include <QTextStream>
#include <set>

namespace sv {

struct PathPoint
{
    PathPoint(sv_frame_t _frame) :
        frame(_frame), mapframe(_frame) { }
    PathPoint(sv_frame_t _frame, sv_frame_t _mapframe) :
        frame(_frame), mapframe(_mapframe) { }

    // "The path consists of a series of points, each with frame equal
    // to the frame on the source model (aligned model) and mapframe
    // equal to the frame on the target model (reference model).  Both
    // should be monotonically increasing."

    sv_frame_t frame;
    sv_frame_t mapframe;

    void toXml(QTextStream &stream, QString indent = "",
               QString extraAttributes = "") const {
        stream << QString("%1<point frame=\"%2\" mapframe=\"%3\" %4/>\n")
            .arg(indent).arg(frame).arg(mapframe).arg(extraAttributes);
    }
        
    QString toDelimitedDataString(QString delimiter, DataExportOptions,
                                  sv_samplerate_t sampleRate) const {
        QStringList list;
        list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
        list << QString("%1").arg(mapframe);
        return list.join(delimiter);
    }

    bool operator<(const PathPoint &p2) const {
        if (frame != p2.frame) return frame < p2.frame;
        return mapframe < p2.mapframe;
    }
};

class Path : public XmlExportable
{
public:
    Path(sv_samplerate_t sampleRate, int resolution) :
        m_sampleRate(sampleRate),
        m_resolution(resolution) {
    }
    Path(const Path &) =default;
    Path &operator=(const Path &) =default;

    typedef std::set<PathPoint> Points;

    sv_samplerate_t getSampleRate() const { return m_sampleRate; }
    int getResolution() const { return m_resolution; }

    int getPointCount() const {
        return int(m_points.size());
    }

    const Points &getPoints() const {
        return m_points;
    }

    void add(PathPoint p) {
        m_points.insert(p);
    }
    
    void remove(PathPoint p) {
        m_points.erase(p);
    }

    void clear() {
        m_points.clear();
    }

    /**
     * XmlExportable methods.
     */
    void toXml(QTextStream &out,
               QString indent = "",
               QString extraAttributes = "") const override {

        // For historical reasons we serialise a Path as a model,
        // although the class itself no longer is.

        // We also write start and end frames - which our API no
        // longer exposes - just for backward compatibility

        sv_frame_t start = 0;
        sv_frame_t end = 0;
        if (!m_points.empty()) {
            start = m_points.begin()->frame;
            end = m_points.rbegin()->frame + m_resolution;
        }
        
        // Our dataset doesn't have its own export ID, we just use
        // ours. Actually any model could do that, since datasets
        // aren't in the same id-space as models (or paths) when
        // re-read
        
        out << indent;
        out << QString("<model id=\"%1\" name=\"\" sampleRate=\"%2\" "
                       "start=\"%3\" end=\"%4\" type=\"sparse\" "
                       "dimensions=\"2\" resolution=\"%5\" "
                       "notifyOnAdd=\"true\" dataset=\"%6\" "
                       "subtype=\"path\" %7/>\n")
            .arg(getExportId())
            .arg(m_sampleRate)
            .arg(start)
            .arg(end)
            .arg(m_resolution)
            .arg(getExportId())
            .arg(extraAttributes);

        out << indent << QString("<dataset id=\"%1\" dimensions=\"2\">\n")
            .arg(getExportId());
        
        for (PathPoint p: m_points) {
            p.toXml(out, indent + "  ", "");
        }

        out << indent << "</dataset>\n";
    }

    QString toDelimitedDataString(QString delimiter,
                                  DataExportOptions,
                                  sv_frame_t startFrame,
                                  sv_frame_t duration) const {

        QString s;
        for (PathPoint p: m_points) {
            if (p.frame < startFrame) continue;
            if (p.frame >= startFrame + duration) break;
            s += QString("%1%2%3\n")
                .arg(p.frame)
                .arg(delimiter)
                .arg(p.mapframe);
        }

        return s;
    }
    
protected:
    sv_samplerate_t m_sampleRate;
    int m_resolution;
    Points m_points;
};


} // end namespace sv

#endif