Mercurial > hg > svcore
comparison data/model/NoteModel.h @ 147:3a13b0d4934e
* Reorganising code base. This revision will not compile.
author | Chris Cannam |
---|---|
date | Mon, 31 Jul 2006 11:44:37 +0000 |
parents | |
children | 4b2ea82fd0ed |
comparison
equal
deleted
inserted
replaced
146:f90fad823cea | 147:3a13b0d4934e |
---|---|
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 This file copyright 2006 Chris Cannam. | |
8 | |
9 This program is free software; you can redistribute it and/or | |
10 modify it under the terms of the GNU General Public License as | |
11 published by the Free Software Foundation; either version 2 of the | |
12 License, or (at your option) any later version. See the file | |
13 COPYING included with this distribution for more information. | |
14 */ | |
15 | |
16 #ifndef _NOTE_MODEL_H_ | |
17 #define _NOTE_MODEL_H_ | |
18 | |
19 #include "SparseValueModel.h" | |
20 #include "PlayParameterRepository.h" | |
21 #include "base/RealTime.h" | |
22 | |
23 /** | |
24 * Note type for use in a SparseModel or SparseValueModel. All we | |
25 * mean by a "note" is something that has an onset time, a single | |
26 * value, and a duration. Like other points, it can also have a | |
27 * label. With this point type, the model can be thought of as | |
28 * representing a simple MIDI-type piano roll, except that the y | |
29 * coordinates (values) do not have to be discrete integers. | |
30 */ | |
31 | |
32 struct Note | |
33 { | |
34 public: | |
35 Note(long _frame) : frame(_frame), value(0.0f), duration(0) { } | |
36 Note(long _frame, float _value, size_t _duration, QString _label) : | |
37 frame(_frame), value(_value), duration(_duration), label(_label) { } | |
38 | |
39 int getDimensions() const { return 3; } | |
40 | |
41 long frame; | |
42 float value; | |
43 size_t duration; | |
44 QString label; | |
45 | |
46 QString toXmlString(QString indent = "", | |
47 QString extraAttributes = "") const | |
48 { | |
49 return QString("%1<point frame=\"%2\" value=\"%3\" duration=\"%4\" label=\"%5\" %6/>\n") | |
50 .arg(indent).arg(frame).arg(value).arg(duration).arg(label).arg(extraAttributes); | |
51 } | |
52 | |
53 QString toDelimitedDataString(QString delimiter, size_t sampleRate) const | |
54 { | |
55 QStringList list; | |
56 list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str(); | |
57 list << QString("%1").arg(value); | |
58 list << QString("%1").arg(duration); | |
59 list << label; | |
60 return list.join(delimiter); | |
61 } | |
62 | |
63 struct Comparator { | |
64 bool operator()(const Note &p1, | |
65 const Note &p2) const { | |
66 if (p1.frame != p2.frame) return p1.frame < p2.frame; | |
67 if (p1.value != p2.value) return p1.value < p2.value; | |
68 if (p1.duration != p2.duration) return p1.duration < p2.duration; | |
69 return p1.label < p2.label; | |
70 } | |
71 }; | |
72 | |
73 struct OrderComparator { | |
74 bool operator()(const Note &p1, | |
75 const Note &p2) const { | |
76 return p1.frame < p2.frame; | |
77 } | |
78 }; | |
79 }; | |
80 | |
81 | |
82 class NoteModel : public SparseValueModel<Note> | |
83 { | |
84 public: | |
85 NoteModel(size_t sampleRate, size_t resolution, | |
86 float valueMinimum, float valueMaximum, | |
87 bool notifyOnAdd = true) : | |
88 SparseValueModel<Note>(sampleRate, resolution, | |
89 valueMinimum, valueMaximum, | |
90 notifyOnAdd), | |
91 m_valueQuantization(0) | |
92 { | |
93 PlayParameterRepository::getInstance()->addModel(this); | |
94 } | |
95 | |
96 float getValueQuantization() const { return m_valueQuantization; } | |
97 void setValueQuantization(float q) { m_valueQuantization = q; } | |
98 | |
99 /** | |
100 * Notes have a duration, so this returns all points that span any | |
101 * of the given range (as well as the usual additional few before | |
102 * and after). Consequently this can be very slow (optimised data | |
103 * structures still to be done!). | |
104 */ | |
105 virtual PointList getPoints(long start, long end) const; | |
106 | |
107 /** | |
108 * Notes have a duration, so this returns all points that span the | |
109 * given frame. Consequently this can be very slow (optimised | |
110 * data structures still to be done!). | |
111 */ | |
112 virtual PointList getPoints(long frame) const; | |
113 | |
114 virtual QString toXmlString(QString indent = "", | |
115 QString extraAttributes = "") const | |
116 { | |
117 return SparseValueModel<Note>::toXmlString | |
118 (indent, | |
119 QString("%1 valueQuantization=\"%2\"") | |
120 .arg(extraAttributes).arg(m_valueQuantization)); | |
121 } | |
122 | |
123 protected: | |
124 float m_valueQuantization; | |
125 }; | |
126 | |
127 #endif |