Mercurial > hg > svcore
comparison data/model/DenseThreeDimensionalModel.cpp @ 147:3a13b0d4934e
* Reorganising code base. This revision will not compile.
author | Chris Cannam |
---|---|
date | Mon, 31 Jul 2006 11:44:37 +0000 |
parents | |
children |
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 #include "DenseThreeDimensionalModel.h" | |
17 | |
18 #include <QTextStream> | |
19 | |
20 DenseThreeDimensionalModel::DenseThreeDimensionalModel(size_t sampleRate, | |
21 size_t windowSize, | |
22 size_t yBinCount, | |
23 bool notifyOnAdd) : | |
24 m_sampleRate(sampleRate), | |
25 m_windowSize(windowSize), | |
26 m_yBinCount(yBinCount), | |
27 m_minimum(0.0), | |
28 m_maximum(0.0), | |
29 m_notifyOnAdd(notifyOnAdd), | |
30 m_sinceLastNotifyMin(-1), | |
31 m_sinceLastNotifyMax(-1), | |
32 m_completion(100) | |
33 { | |
34 } | |
35 | |
36 bool | |
37 DenseThreeDimensionalModel::isOK() const | |
38 { | |
39 return true; | |
40 } | |
41 | |
42 size_t | |
43 DenseThreeDimensionalModel::getSampleRate() const | |
44 { | |
45 return m_sampleRate; | |
46 } | |
47 | |
48 size_t | |
49 DenseThreeDimensionalModel::getStartFrame() const | |
50 { | |
51 return 0; | |
52 } | |
53 | |
54 size_t | |
55 DenseThreeDimensionalModel::getEndFrame() const | |
56 { | |
57 return m_windowSize * m_data.size() + (m_windowSize - 1); | |
58 } | |
59 | |
60 Model * | |
61 DenseThreeDimensionalModel::clone() const | |
62 { | |
63 DenseThreeDimensionalModel *model = new DenseThreeDimensionalModel | |
64 (m_sampleRate, m_windowSize, m_yBinCount); | |
65 | |
66 model->m_minimum = m_minimum; | |
67 model->m_maximum = m_maximum; | |
68 | |
69 for (size_t i = 0; i < m_data.size(); ++i) { | |
70 model->setBinValues(i * m_windowSize, m_data[i]); | |
71 } | |
72 | |
73 return model; | |
74 } | |
75 | |
76 size_t | |
77 DenseThreeDimensionalModel::getWindowSize() const | |
78 { | |
79 return m_windowSize; | |
80 } | |
81 | |
82 void | |
83 DenseThreeDimensionalModel::setWindowSize(size_t sz) | |
84 { | |
85 m_windowSize = sz; | |
86 } | |
87 | |
88 size_t | |
89 DenseThreeDimensionalModel::getYBinCount() const | |
90 { | |
91 return m_yBinCount; | |
92 } | |
93 | |
94 void | |
95 DenseThreeDimensionalModel::setYBinCount(size_t sz) | |
96 { | |
97 m_yBinCount = sz; | |
98 } | |
99 | |
100 float | |
101 DenseThreeDimensionalModel::getMinimumLevel() const | |
102 { | |
103 return m_minimum; | |
104 } | |
105 | |
106 void | |
107 DenseThreeDimensionalModel::setMinimumLevel(float level) | |
108 { | |
109 m_minimum = level; | |
110 } | |
111 | |
112 float | |
113 DenseThreeDimensionalModel::getMaximumLevel() const | |
114 { | |
115 return m_maximum; | |
116 } | |
117 | |
118 void | |
119 DenseThreeDimensionalModel::setMaximumLevel(float level) | |
120 { | |
121 m_maximum = level; | |
122 } | |
123 | |
124 void | |
125 DenseThreeDimensionalModel::getBinValues(long windowStart, | |
126 BinValueSet &result) const | |
127 { | |
128 QMutexLocker locker(&m_mutex); | |
129 | |
130 long index = windowStart / m_windowSize; | |
131 | |
132 if (index >= 0 && index < long(m_data.size())) { | |
133 result = m_data[index]; | |
134 } else { | |
135 result.clear(); | |
136 } | |
137 | |
138 while (result.size() < m_yBinCount) result.push_back(m_minimum); | |
139 } | |
140 | |
141 float | |
142 DenseThreeDimensionalModel::getBinValue(long windowStart, | |
143 size_t n) const | |
144 { | |
145 QMutexLocker locker(&m_mutex); | |
146 | |
147 long index = windowStart / m_windowSize; | |
148 | |
149 if (index >= 0 && index < long(m_data.size())) { | |
150 const BinValueSet &s = m_data[index]; | |
151 if (n < s.size()) return s[n]; | |
152 } | |
153 | |
154 return m_minimum; | |
155 } | |
156 | |
157 void | |
158 DenseThreeDimensionalModel::setBinValues(long windowStart, | |
159 const BinValueSet &values) | |
160 { | |
161 QMutexLocker locker(&m_mutex); | |
162 | |
163 long index = windowStart / m_windowSize; | |
164 | |
165 while (index >= long(m_data.size())) { | |
166 m_data.push_back(BinValueSet()); | |
167 } | |
168 | |
169 bool newExtents = (m_data.empty() && (m_minimum == m_maximum)); | |
170 bool allChange = false; | |
171 | |
172 for (size_t i = 0; i < values.size(); ++i) { | |
173 if (newExtents || values[i] < m_minimum) { | |
174 m_minimum = values[i]; | |
175 allChange = true; | |
176 } | |
177 if (newExtents || values[i] > m_maximum) { | |
178 m_maximum = values[i]; | |
179 allChange = true; | |
180 } | |
181 } | |
182 | |
183 m_data[index] = values; | |
184 | |
185 if (m_notifyOnAdd) { | |
186 if (allChange) { | |
187 emit modelChanged(); | |
188 } else { | |
189 emit modelChanged(windowStart, windowStart + m_windowSize); | |
190 } | |
191 } else { | |
192 if (allChange) { | |
193 m_sinceLastNotifyMin = -1; | |
194 m_sinceLastNotifyMax = -1; | |
195 emit modelChanged(); | |
196 } else { | |
197 if (m_sinceLastNotifyMin == -1 || | |
198 windowStart < m_sinceLastNotifyMin) { | |
199 m_sinceLastNotifyMin = windowStart; | |
200 } | |
201 if (m_sinceLastNotifyMax == -1 || | |
202 windowStart > m_sinceLastNotifyMax) { | |
203 m_sinceLastNotifyMax = windowStart; | |
204 } | |
205 } | |
206 } | |
207 } | |
208 | |
209 QString | |
210 DenseThreeDimensionalModel::getBinName(size_t n) const | |
211 { | |
212 if (m_binNames.size() > n) return m_binNames[n]; | |
213 else return ""; | |
214 } | |
215 | |
216 void | |
217 DenseThreeDimensionalModel::setBinName(size_t n, QString name) | |
218 { | |
219 while (m_binNames.size() <= n) m_binNames.push_back(""); | |
220 m_binNames[n] = name; | |
221 emit modelChanged(); | |
222 } | |
223 | |
224 void | |
225 DenseThreeDimensionalModel::setBinNames(std::vector<QString> names) | |
226 { | |
227 m_binNames = names; | |
228 emit modelChanged(); | |
229 } | |
230 | |
231 void | |
232 DenseThreeDimensionalModel::setCompletion(int completion) | |
233 { | |
234 if (m_completion != completion) { | |
235 m_completion = completion; | |
236 | |
237 if (completion == 100) { | |
238 | |
239 m_notifyOnAdd = true; // henceforth | |
240 emit modelChanged(); | |
241 | |
242 } else if (!m_notifyOnAdd) { | |
243 | |
244 if (m_sinceLastNotifyMin >= 0 && | |
245 m_sinceLastNotifyMax >= 0) { | |
246 emit modelChanged(m_sinceLastNotifyMin, | |
247 m_sinceLastNotifyMax + m_windowSize); | |
248 m_sinceLastNotifyMin = m_sinceLastNotifyMax = -1; | |
249 } else { | |
250 emit completionChanged(); | |
251 } | |
252 } else { | |
253 emit completionChanged(); | |
254 } | |
255 } | |
256 } | |
257 | |
258 void | |
259 DenseThreeDimensionalModel::toXml(QTextStream &out, | |
260 QString indent, | |
261 QString extraAttributes) const | |
262 { | |
263 out << Model::toXmlString | |
264 (indent, QString("type=\"dense\" dimensions=\"3\" windowSize=\"%1\" yBinCount=\"%2\" minimum=\"%3\" maximum=\"%4\" dataset=\"%5\" %6") | |
265 .arg(m_windowSize) | |
266 .arg(m_yBinCount) | |
267 .arg(m_minimum) | |
268 .arg(m_maximum) | |
269 .arg(getObjectExportId(&m_data)) | |
270 .arg(extraAttributes)); | |
271 | |
272 out << indent; | |
273 out << QString("<dataset id=\"%1\" dimensions=\"3\" separator=\" \">\n") | |
274 .arg(getObjectExportId(&m_data)); | |
275 | |
276 for (size_t i = 0; i < m_binNames.size(); ++i) { | |
277 if (m_binNames[i] != "") { | |
278 out << indent + " "; | |
279 out << QString("<bin number=\"%1\" name=\"%2\"/>\n") | |
280 .arg(i).arg(m_binNames[i]); | |
281 } | |
282 } | |
283 | |
284 for (size_t i = 0; i < m_data.size(); ++i) { | |
285 out << indent + " "; | |
286 out << QString("<row n=\"%1\">").arg(i); | |
287 for (size_t j = 0; j < m_data[i].size(); ++j) { | |
288 if (j > 0) out << " "; | |
289 out << m_data[i][j]; | |
290 } | |
291 out << QString("</row>\n"); | |
292 } | |
293 | |
294 out << indent + "</dataset>\n"; | |
295 } | |
296 | |
297 QString | |
298 DenseThreeDimensionalModel::toXmlString(QString indent, | |
299 QString extraAttributes) const | |
300 { | |
301 QString s; | |
302 | |
303 { | |
304 QTextStream out(&s); | |
305 toXml(out, indent, extraAttributes); | |
306 } | |
307 | |
308 return s; | |
309 } | |
310 | |
311 #ifdef INCLUDE_MOCFILES | |
312 #include "DenseThreeDimensionalModel.moc.cpp" | |
313 #endif | |
314 |