Chris@133
|
1
|
Chris@133
|
2 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@133
|
3
|
Chris@133
|
4 /*
|
Chris@133
|
5 Sonic Visualiser
|
Chris@133
|
6 An audio file viewer and annotation editor.
|
Chris@133
|
7 Centre for Digital Music, Queen Mary, University of London.
|
Chris@195
|
8 This file copyright 2006-2007 QMUL.
|
Chris@133
|
9
|
Chris@133
|
10 This program is free software; you can redistribute it and/or
|
Chris@133
|
11 modify it under the terms of the GNU General Public License as
|
Chris@133
|
12 published by the Free Software Foundation; either version 2 of the
|
Chris@133
|
13 License, or (at your option) any later version. See the file
|
Chris@133
|
14 COPYING included with this distribution for more information.
|
Chris@133
|
15 */
|
Chris@133
|
16
|
Chris@193
|
17 #include "SliceLayer.h"
|
Chris@133
|
18
|
Chris@133
|
19 #include "view/View.h"
|
Chris@153
|
20 #include "base/AudioLevel.h"
|
Chris@167
|
21 #include "base/RangeMapper.h"
|
Chris@195
|
22 #include "base/RealTime.h"
|
Chris@195
|
23
|
Chris@197
|
24 #include "ColourMapper.h"
|
Chris@195
|
25 #include "PaintAssistant.h"
|
Chris@133
|
26
|
Chris@133
|
27 #include <QPainter>
|
Chris@133
|
28 #include <QPainterPath>
|
Chris@133
|
29
|
Chris@193
|
30 SliceLayer::SliceLayer() :
|
Chris@193
|
31 m_sliceableModel(0),
|
Chris@153
|
32 m_colour(Qt::darkBlue),
|
Chris@197
|
33 m_colourMap(0),
|
Chris@153
|
34 m_energyScale(dBScale),
|
Chris@198
|
35 m_samplingMode(SampleMean),
|
Chris@195
|
36 m_plotStyle(PlotSteps),
|
Chris@193
|
37 m_binScale(LinearBins),
|
Chris@153
|
38 m_normalize(false),
|
Chris@198
|
39 m_gain(1.0),
|
Chris@198
|
40 m_currentf0(0),
|
Chris@198
|
41 m_currentf1(0)
|
Chris@133
|
42 {
|
Chris@133
|
43 }
|
Chris@133
|
44
|
Chris@193
|
45 SliceLayer::~SliceLayer()
|
Chris@133
|
46 {
|
Chris@193
|
47
|
Chris@133
|
48 }
|
Chris@133
|
49
|
Chris@133
|
50 void
|
Chris@193
|
51 SliceLayer::setSliceableModel(const Model *model)
|
Chris@133
|
52 {
|
Chris@193
|
53 const DenseThreeDimensionalModel *sliceable =
|
Chris@193
|
54 dynamic_cast<const DenseThreeDimensionalModel *>(model);
|
Chris@193
|
55
|
Chris@193
|
56 if (model && !sliceable) {
|
Chris@193
|
57 std::cerr << "WARNING: SliceLayer::setSliceableModel(" << model
|
Chris@193
|
58 << "): model is not a DenseThreeDimensionalModel" << std::endl;
|
Chris@193
|
59 }
|
Chris@193
|
60
|
Chris@193
|
61 if (m_sliceableModel == sliceable) return;
|
Chris@193
|
62
|
Chris@193
|
63 m_sliceableModel = sliceable;
|
Chris@193
|
64
|
Chris@193
|
65 connect(m_sliceableModel, SIGNAL(modelChanged()),
|
Chris@193
|
66 this, SIGNAL(modelChanged()));
|
Chris@193
|
67
|
Chris@193
|
68 connect(m_sliceableModel, SIGNAL(modelChanged(size_t, size_t)),
|
Chris@193
|
69 this, SIGNAL(modelChanged(size_t, size_t)));
|
Chris@193
|
70
|
Chris@193
|
71 connect(m_sliceableModel, SIGNAL(completionChanged()),
|
Chris@193
|
72 this, SIGNAL(modelCompletionChanged()));
|
Chris@193
|
73
|
Chris@193
|
74 emit modelReplaced();
|
Chris@153
|
75 }
|
Chris@153
|
76
|
Chris@153
|
77 void
|
Chris@193
|
78 SliceLayer::sliceableModelReplaced(const Model *orig, const Model *replacement)
|
Chris@153
|
79 {
|
Chris@193
|
80 std::cerr << "SliceLayer::sliceableModelReplaced(" << orig << ", " << replacement << ")" << std::endl;
|
Chris@153
|
81
|
Chris@193
|
82 if (orig == m_sliceableModel) {
|
Chris@193
|
83 setSliceableModel
|
Chris@193
|
84 (dynamic_cast<const DenseThreeDimensionalModel *>(replacement));
|
Chris@153
|
85 }
|
Chris@153
|
86 }
|
Chris@153
|
87
|
Chris@153
|
88 void
|
Chris@193
|
89 SliceLayer::modelAboutToBeDeleted(Model *m)
|
Chris@153
|
90 {
|
Chris@193
|
91 std::cerr << "SliceLayer::modelAboutToBeDeleted(" << m << ")" << std::endl;
|
Chris@153
|
92
|
Chris@193
|
93 if (m == m_sliceableModel) {
|
Chris@193
|
94 setSliceableModel(0);
|
Chris@153
|
95 }
|
Chris@133
|
96 }
|
Chris@133
|
97
|
Chris@198
|
98 QString
|
Chris@198
|
99 SliceLayer::getFeatureDescription(View *v, QPoint &p) const
|
Chris@198
|
100 {
|
Chris@199
|
101 int minbin, maxbin, range;
|
Chris@199
|
102 return getFeatureDescription(v, p, true, minbin, maxbin, range);
|
Chris@199
|
103 }
|
Chris@199
|
104
|
Chris@199
|
105 QString
|
Chris@199
|
106 SliceLayer::getFeatureDescription(View *v, QPoint &p,
|
Chris@199
|
107 bool includeBinDescription,
|
Chris@199
|
108 int &minbin, int &maxbin, int &range) const
|
Chris@199
|
109 {
|
Chris@199
|
110 minbin = 0;
|
Chris@199
|
111 maxbin = 0;
|
Chris@198
|
112 if (!m_sliceableModel) return "";
|
Chris@198
|
113
|
Chris@198
|
114 int xorigin = m_xorigins[v];
|
Chris@198
|
115 int w = v->width() - xorigin - 1;
|
Chris@198
|
116
|
Chris@198
|
117 int mh = m_sliceableModel->getHeight();
|
Chris@199
|
118 minbin = getBinForX(p.x() - xorigin, mh, w);
|
Chris@199
|
119 maxbin = getBinForX(p.x() - xorigin + 1, mh, w);
|
Chris@199
|
120
|
Chris@199
|
121 if (minbin >= mh) minbin = mh - 1;
|
Chris@199
|
122 if (maxbin >= mh) maxbin = mh - 1;
|
Chris@199
|
123 if (minbin < 0) minbin = 0;
|
Chris@199
|
124 if (maxbin < 0) maxbin = 0;
|
Chris@198
|
125
|
Chris@198
|
126 int sampleRate = m_sliceableModel->getSampleRate();
|
Chris@198
|
127
|
Chris@198
|
128 size_t f0 = m_currentf0;
|
Chris@198
|
129 size_t f1 = m_currentf1;
|
Chris@198
|
130
|
Chris@198
|
131 RealTime rt0 = RealTime::frame2RealTime(f0, sampleRate);
|
Chris@198
|
132 RealTime rt1 = RealTime::frame2RealTime(f1, sampleRate);
|
Chris@198
|
133
|
Chris@199
|
134 range = f1 - f0 + 1;
|
Chris@198
|
135
|
Chris@199
|
136 if (includeBinDescription) {
|
Chris@198
|
137
|
Chris@199
|
138 float minvalue = 0.f;
|
Chris@248
|
139 if (minbin < int(m_values.size())) minvalue = m_values[minbin];
|
Chris@198
|
140
|
Chris@199
|
141 float maxvalue = minvalue;
|
Chris@248
|
142 if (maxbin < int(m_values.size())) maxvalue = m_values[maxbin];
|
Chris@199
|
143
|
Chris@199
|
144 if (minvalue > maxvalue) std::swap(minvalue, maxvalue);
|
Chris@199
|
145
|
Chris@199
|
146 QString binstr;
|
Chris@199
|
147 if (maxbin != minbin) {
|
Chris@199
|
148 binstr = tr("%1 - %2").arg(minbin+1).arg(maxbin+1);
|
Chris@199
|
149 } else {
|
Chris@199
|
150 binstr = QString("%1").arg(minbin+1);
|
Chris@199
|
151 }
|
Chris@199
|
152
|
Chris@199
|
153 QString valuestr;
|
Chris@199
|
154 if (maxvalue != minvalue) {
|
Chris@199
|
155 valuestr = tr("%1 - %2").arg(minvalue).arg(maxvalue);
|
Chris@199
|
156 } else {
|
Chris@199
|
157 valuestr = QString("%1").arg(minvalue);
|
Chris@199
|
158 }
|
Chris@199
|
159
|
Chris@199
|
160 QString description = tr("Time:\t%1 - %2\nRange:\t%3 samples\nBin:\t%4\n%5 value:\t%6")
|
Chris@199
|
161 .arg(QString::fromStdString(rt0.toText(true)))
|
Chris@199
|
162 .arg(QString::fromStdString(rt1.toText(true)))
|
Chris@199
|
163 .arg(range)
|
Chris@199
|
164 .arg(binstr)
|
Chris@199
|
165 .arg(m_samplingMode == NearestSample ? tr("First") :
|
Chris@199
|
166 m_samplingMode == SampleMean ? tr("Mean") : tr("Peak"))
|
Chris@199
|
167 .arg(valuestr);
|
Chris@199
|
168
|
Chris@199
|
169 return description;
|
Chris@199
|
170
|
Chris@199
|
171 } else {
|
Chris@199
|
172
|
Chris@199
|
173 QString description = tr("Time:\t%1 - %2\nRange:\t%3 samples")
|
Chris@199
|
174 .arg(QString::fromStdString(rt0.toText(true)))
|
Chris@199
|
175 .arg(QString::fromStdString(rt1.toText(true)))
|
Chris@199
|
176 .arg(range);
|
Chris@199
|
177
|
Chris@199
|
178 return description;
|
Chris@199
|
179 }
|
Chris@198
|
180 }
|
Chris@198
|
181
|
Chris@198
|
182 float
|
Chris@198
|
183 SliceLayer::getXForBin(int bin, int count, float w) const
|
Chris@198
|
184 {
|
Chris@198
|
185 float x = 0;
|
Chris@198
|
186
|
Chris@198
|
187 switch (m_binScale) {
|
Chris@198
|
188
|
Chris@198
|
189 case LinearBins:
|
Chris@198
|
190 x = (float(w) * bin) / count;
|
Chris@198
|
191 break;
|
Chris@198
|
192
|
Chris@198
|
193 case LogBins:
|
Chris@198
|
194 x = (float(w) * log10f(bin + 1)) / log10f(count + 1);
|
Chris@198
|
195 break;
|
Chris@198
|
196
|
Chris@198
|
197 case InvertedLogBins:
|
Chris@198
|
198 x = w - (float(w) * log10f(count - bin - 1)) / log10f(count);
|
Chris@198
|
199 break;
|
Chris@198
|
200 }
|
Chris@198
|
201
|
Chris@198
|
202 return x;
|
Chris@198
|
203 }
|
Chris@198
|
204
|
Chris@198
|
205 int
|
Chris@198
|
206 SliceLayer::getBinForX(float x, int count, float w) const
|
Chris@198
|
207 {
|
Chris@198
|
208 int bin = 0;
|
Chris@198
|
209
|
Chris@198
|
210 switch (m_binScale) {
|
Chris@198
|
211
|
Chris@198
|
212 case LinearBins:
|
Chris@198
|
213 bin = int((x * count) / w + 0.0001);
|
Chris@198
|
214 break;
|
Chris@198
|
215
|
Chris@198
|
216 case LogBins:
|
Chris@198
|
217 bin = int(powf(10.f, (x * log10f(count + 1)) / w) - 1 + 0.0001);
|
Chris@198
|
218 break;
|
Chris@198
|
219
|
Chris@198
|
220 case InvertedLogBins:
|
Chris@198
|
221 bin = count + 1 - int(powf(10.f, (log10f(count) * (w - x)) / float(w)) + 0.0001);
|
Chris@198
|
222 break;
|
Chris@198
|
223 }
|
Chris@198
|
224
|
Chris@198
|
225 return bin;
|
Chris@198
|
226 }
|
Chris@198
|
227
|
Chris@133
|
228 void
|
Chris@193
|
229 SliceLayer::paint(View *v, QPainter &paint, QRect rect) const
|
Chris@133
|
230 {
|
Chris@254
|
231 if (!m_sliceableModel || !m_sliceableModel->isOK() ||
|
Chris@254
|
232 !m_sliceableModel->isReady()) return;
|
Chris@133
|
233
|
Chris@195
|
234 paint.save();
|
Chris@195
|
235 paint.setRenderHint(QPainter::Antialiasing, false);
|
Chris@195
|
236
|
Chris@195
|
237 if (v->getViewManager() && v->getViewManager()->shouldShowScaleGuides()) {
|
Chris@195
|
238 if (!m_scalePoints.empty()) {
|
Chris@195
|
239 paint.setPen(QColor(240, 240, 240)); //!!! and dark background?
|
Chris@195
|
240 for (size_t i = 0; i < m_scalePoints.size(); ++i) {
|
Chris@195
|
241 paint.drawLine(0, m_scalePoints[i], rect.width(), m_scalePoints[i]);
|
Chris@195
|
242 }
|
Chris@195
|
243 }
|
Chris@195
|
244 }
|
Chris@195
|
245
|
Chris@195
|
246 paint.setPen(m_colour);
|
Chris@195
|
247
|
Chris@195
|
248 // int w = (v->width() * 2) / 3;
|
Chris@195
|
249 int xorigin = getVerticalScaleWidth(v, paint) + 1; //!!! (v->width() / 2) - (w / 2);
|
Chris@195
|
250 int w = v->width() - xorigin - 1;
|
Chris@198
|
251
|
Chris@198
|
252 m_xorigins[v] = xorigin; // for use in getFeatureDescription
|
Chris@133
|
253
|
Chris@195
|
254 int yorigin = v->height() - 20 - paint.fontMetrics().height() - 7;
|
Chris@195
|
255 int h = yorigin - paint.fontMetrics().height() - 8;
|
Chris@195
|
256 if (h < 0) return;
|
Chris@133
|
257
|
Chris@195
|
258 // int h = (v->height() * 3) / 4;
|
Chris@195
|
259 // int yorigin = (v->height() / 2) + (h / 2);
|
Chris@133
|
260
|
Chris@133
|
261 QPainterPath path;
|
Chris@133
|
262 float thresh = -80.f;
|
Chris@133
|
263
|
Chris@248
|
264 size_t mh = m_sliceableModel->getHeight();
|
Chris@133
|
265
|
Chris@193
|
266 int divisor = 0;
|
Chris@193
|
267
|
Chris@198
|
268 m_values.clear();
|
Chris@193
|
269 for (size_t bin = 0; bin < mh; ++bin) {
|
Chris@198
|
270 m_values.push_back(0.f);
|
Chris@193
|
271 }
|
Chris@193
|
272
|
Chris@193
|
273 size_t f0 = v->getCentreFrame();
|
Chris@193
|
274 int f0x = v->getXForFrame(f0);
|
Chris@195
|
275 f0 = v->getFrameForX(f0x);
|
Chris@193
|
276 size_t f1 = v->getFrameForX(f0x + 1);
|
Chris@195
|
277 if (f1 > f0) --f1;
|
Chris@193
|
278
|
Chris@193
|
279 size_t col0 = f0 / m_sliceableModel->getResolution();
|
Chris@193
|
280 size_t col1 = col0;
|
Chris@193
|
281 if (m_samplingMode != NearestSample) {
|
Chris@193
|
282 col1 = f1 / m_sliceableModel->getResolution();
|
Chris@193
|
283 }
|
Chris@195
|
284 f0 = col0 * m_sliceableModel->getResolution();
|
Chris@195
|
285 f1 = (col1 + 1) * m_sliceableModel->getResolution() - 1;
|
Chris@193
|
286
|
Chris@198
|
287 m_currentf0 = f0;
|
Chris@198
|
288 m_currentf1 = f1;
|
Chris@198
|
289
|
Chris@254
|
290 BiasCurve curve;
|
Chris@254
|
291 getBiasCurve(curve);
|
Chris@254
|
292 size_t cs = curve.size();
|
Chris@254
|
293
|
Chris@195
|
294 for (size_t col = col0; col <= col1; ++col) {
|
Chris@193
|
295 for (size_t bin = 0; bin < mh; ++bin) {
|
Chris@193
|
296 float value = m_sliceableModel->getValueAt(col, bin);
|
Chris@254
|
297 if (bin < cs) value *= curve[bin];
|
Chris@193
|
298 if (m_samplingMode == SamplePeak) {
|
Chris@198
|
299 if (value > m_values[bin]) m_values[bin] = value;
|
Chris@193
|
300 } else {
|
Chris@198
|
301 m_values[bin] += value;
|
Chris@193
|
302 }
|
Chris@153
|
303 }
|
Chris@193
|
304 ++divisor;
|
Chris@193
|
305 }
|
Chris@193
|
306
|
Chris@193
|
307 float max = 0.f;
|
Chris@193
|
308 for (size_t bin = 0; bin < mh; ++bin) {
|
Chris@198
|
309 if (m_samplingMode == SampleMean) m_values[bin] /= divisor;
|
Chris@198
|
310 if (m_values[bin] > max) max = m_values[bin];
|
Chris@193
|
311 }
|
Chris@193
|
312 if (max != 0.f && m_normalize) {
|
Chris@193
|
313 for (size_t bin = 0; bin < mh; ++bin) {
|
Chris@198
|
314 m_values[bin] /= max;
|
Chris@193
|
315 }
|
Chris@193
|
316 }
|
Chris@193
|
317
|
Chris@193
|
318 float py = 0;
|
Chris@193
|
319 float nx = xorigin;
|
Chris@193
|
320
|
Chris@197
|
321 ColourMapper mapper(m_colourMap, 0, 1);
|
Chris@197
|
322
|
Chris@193
|
323 for (size_t bin = 0; bin < mh; ++bin) {
|
Chris@193
|
324
|
Chris@198
|
325 float x = nx;
|
Chris@198
|
326 nx = xorigin + getXForBin(bin + 1, mh, w);
|
Chris@193
|
327
|
Chris@198
|
328 float value = m_values[bin];
|
Chris@193
|
329
|
Chris@193
|
330 value *= m_gain;
|
Chris@197
|
331 float norm = 0.f;
|
Chris@153
|
332 float y = 0.f;
|
Chris@153
|
333
|
Chris@153
|
334 switch (m_energyScale) {
|
Chris@153
|
335
|
Chris@153
|
336 case dBScale:
|
Chris@153
|
337 {
|
Chris@153
|
338 float db = thresh;
|
Chris@193
|
339 if (value > 0.f) db = 10.f * log10f(value);
|
Chris@153
|
340 if (db < thresh) db = thresh;
|
Chris@197
|
341 norm = (db - thresh) / -thresh;
|
Chris@197
|
342 y = yorigin - (float(h) * norm);
|
Chris@153
|
343 break;
|
Chris@153
|
344 }
|
Chris@153
|
345
|
Chris@153
|
346 case MeterScale:
|
Chris@197
|
347 y = AudioLevel::multiplier_to_preview(value, h);
|
Chris@197
|
348 norm = float(y) / float(h);
|
Chris@197
|
349 y = yorigin - y;
|
Chris@153
|
350 break;
|
Chris@153
|
351
|
Chris@153
|
352 default:
|
Chris@197
|
353 norm = value;
|
Chris@193
|
354 y = yorigin - (float(h) * value);
|
Chris@153
|
355 break;
|
Chris@153
|
356 }
|
Chris@133
|
357
|
Chris@193
|
358 if (m_plotStyle == PlotLines) {
|
Chris@193
|
359
|
Chris@193
|
360 if (bin == 0) {
|
Chris@193
|
361 path.moveTo(x, y);
|
Chris@193
|
362 } else {
|
Chris@193
|
363 path.lineTo(x, y);
|
Chris@193
|
364 }
|
Chris@193
|
365
|
Chris@193
|
366 } else if (m_plotStyle == PlotSteps) {
|
Chris@193
|
367
|
Chris@193
|
368 if (bin == 0) {
|
Chris@193
|
369 path.moveTo(x, y);
|
Chris@193
|
370 } else {
|
Chris@193
|
371 path.lineTo(x, y);
|
Chris@193
|
372 }
|
Chris@193
|
373 path.lineTo(nx, y);
|
Chris@193
|
374
|
Chris@193
|
375 } else if (m_plotStyle == PlotBlocks) {
|
Chris@193
|
376
|
Chris@193
|
377 path.moveTo(x, yorigin);
|
Chris@133
|
378 path.lineTo(x, y);
|
Chris@193
|
379 path.lineTo(nx, y);
|
Chris@193
|
380 path.lineTo(nx, yorigin);
|
Chris@193
|
381 path.lineTo(x, yorigin);
|
Chris@197
|
382
|
Chris@197
|
383 } else if (m_plotStyle == PlotFilledBlocks) {
|
Chris@197
|
384
|
Chris@197
|
385 paint.fillRect(QRectF(x, y, nx - x, yorigin - y), mapper.map(norm));
|
Chris@133
|
386 }
|
Chris@193
|
387
|
Chris@193
|
388 py = y;
|
Chris@133
|
389 }
|
Chris@133
|
390
|
Chris@197
|
391 if (m_plotStyle != PlotFilledBlocks) {
|
Chris@197
|
392 paint.drawPath(path);
|
Chris@197
|
393 }
|
Chris@133
|
394 paint.restore();
|
Chris@198
|
395 /*
|
Chris@197
|
396 QPoint discard;
|
Chris@197
|
397
|
Chris@197
|
398 if (v->getViewManager() && v->getViewManager()->shouldShowFrameCount() &&
|
Chris@197
|
399 v->shouldIlluminateLocalFeatures(this, discard)) {
|
Chris@195
|
400
|
Chris@195
|
401 int sampleRate = m_sliceableModel->getSampleRate();
|
Chris@195
|
402
|
Chris@195
|
403 QString startText = QString("%1 / %2")
|
Chris@195
|
404 .arg(QString::fromStdString
|
Chris@195
|
405 (RealTime::frame2RealTime
|
Chris@195
|
406 (f0, sampleRate).toText(true)))
|
Chris@195
|
407 .arg(f0);
|
Chris@195
|
408
|
Chris@195
|
409 QString endText = QString(" %1 / %2")
|
Chris@195
|
410 .arg(QString::fromStdString
|
Chris@195
|
411 (RealTime::frame2RealTime
|
Chris@195
|
412 (f1, sampleRate).toText(true)))
|
Chris@195
|
413 .arg(f1);
|
Chris@195
|
414
|
Chris@195
|
415 QString durationText = QString("(%1 / %2) ")
|
Chris@195
|
416 .arg(QString::fromStdString
|
Chris@195
|
417 (RealTime::frame2RealTime
|
Chris@195
|
418 (f1 - f0 + 1, sampleRate).toText(true)))
|
Chris@195
|
419 .arg(f1 - f0 + 1);
|
Chris@195
|
420
|
Chris@195
|
421 v->drawVisibleText
|
Chris@195
|
422 (paint, xorigin + 5,
|
Chris@195
|
423 paint.fontMetrics().ascent() + 5,
|
Chris@195
|
424 startText, View::OutlinedText);
|
Chris@195
|
425
|
Chris@195
|
426 v->drawVisibleText
|
Chris@195
|
427 (paint, xorigin + 5,
|
Chris@195
|
428 paint.fontMetrics().ascent() + paint.fontMetrics().height() + 10,
|
Chris@195
|
429 endText, View::OutlinedText);
|
Chris@195
|
430
|
Chris@195
|
431 v->drawVisibleText
|
Chris@195
|
432 (paint, xorigin + 5,
|
Chris@195
|
433 paint.fontMetrics().ascent() + 2*paint.fontMetrics().height() + 15,
|
Chris@195
|
434 durationText, View::OutlinedText);
|
Chris@195
|
435 }
|
Chris@195
|
436 */
|
Chris@195
|
437 }
|
Chris@195
|
438
|
Chris@195
|
439 int
|
Chris@248
|
440 SliceLayer::getVerticalScaleWidth(View *, QPainter &paint) const
|
Chris@195
|
441 {
|
Chris@195
|
442 if (m_energyScale == LinearScale) {
|
Chris@220
|
443 return std::max(paint.fontMetrics().width("0.0") + 13,
|
Chris@220
|
444 paint.fontMetrics().width("x10-10"));
|
Chris@195
|
445 } else {
|
Chris@195
|
446 return std::max(paint.fontMetrics().width(tr("0dB")),
|
Chris@195
|
447 paint.fontMetrics().width(tr("-Inf"))) + 13;
|
Chris@195
|
448 }
|
Chris@195
|
449 }
|
Chris@195
|
450
|
Chris@195
|
451 void
|
Chris@195
|
452 SliceLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
|
Chris@195
|
453 {
|
Chris@195
|
454 float thresh = 0;
|
Chris@195
|
455 if (m_energyScale != LinearScale) {
|
Chris@195
|
456 thresh = AudioLevel::dB_to_multiplier(-80); //!!! thresh
|
Chris@195
|
457 }
|
Chris@195
|
458
|
Chris@195
|
459 // int h = (rect.height() * 3) / 4;
|
Chris@195
|
460 // int y = (rect.height() / 2) - (h / 2);
|
Chris@195
|
461
|
Chris@195
|
462 int yorigin = v->height() - 20 - paint.fontMetrics().height() - 6;
|
Chris@195
|
463 int h = yorigin - paint.fontMetrics().height() - 8;
|
Chris@195
|
464 if (h < 0) return;
|
Chris@195
|
465
|
Chris@195
|
466 QRect actual(rect.x(), rect.y() + yorigin - h, rect.width(), h);
|
Chris@195
|
467
|
Chris@220
|
468 int mult = 1;
|
Chris@220
|
469
|
Chris@195
|
470 PaintAssistant::paintVerticalLevelScale
|
Chris@195
|
471 (paint, actual, thresh, 1.0 / m_gain,
|
Chris@195
|
472 PaintAssistant::Scale(m_energyScale),
|
Chris@220
|
473 mult,
|
Chris@195
|
474 const_cast<std::vector<int> *>(&m_scalePoints));
|
Chris@220
|
475
|
Chris@220
|
476 if (mult != 1 && mult != 0) {
|
Chris@220
|
477 int log = lrintf(log10f(mult));
|
Chris@220
|
478 QString a = tr("x10");
|
Chris@220
|
479 QString b = QString("%1").arg(-log);
|
Chris@220
|
480 paint.drawText(3, 8 + paint.fontMetrics().ascent(), a);
|
Chris@220
|
481 paint.drawText(3 + paint.fontMetrics().width(a),
|
Chris@220
|
482 3 + paint.fontMetrics().ascent(), b);
|
Chris@220
|
483 }
|
Chris@133
|
484 }
|
Chris@133
|
485
|
Chris@153
|
486 Layer::PropertyList
|
Chris@193
|
487 SliceLayer::getProperties() const
|
Chris@153
|
488 {
|
Chris@153
|
489 PropertyList list;
|
Chris@153
|
490 list.push_back("Colour");
|
Chris@193
|
491 list.push_back("Plot Type");
|
Chris@198
|
492 // list.push_back("Sampling Mode");
|
Chris@153
|
493 list.push_back("Scale");
|
Chris@153
|
494 list.push_back("Normalize");
|
Chris@153
|
495 list.push_back("Gain");
|
Chris@193
|
496 list.push_back("Bin Scale");
|
Chris@153
|
497
|
Chris@153
|
498 return list;
|
Chris@153
|
499 }
|
Chris@153
|
500
|
Chris@153
|
501 QString
|
Chris@193
|
502 SliceLayer::getPropertyLabel(const PropertyName &name) const
|
Chris@153
|
503 {
|
Chris@153
|
504 if (name == "Colour") return tr("Colour");
|
Chris@193
|
505 if (name == "Plot Type") return tr("Plot Type");
|
Chris@153
|
506 if (name == "Energy Scale") return tr("Scale");
|
Chris@153
|
507 if (name == "Normalize") return tr("Normalize");
|
Chris@153
|
508 if (name == "Gain") return tr("Gain");
|
Chris@193
|
509 if (name == "Sampling Mode") return tr("Sampling Mode");
|
Chris@193
|
510 if (name == "Bin Scale") return tr("Plot X Scale");
|
Chris@153
|
511 return "";
|
Chris@153
|
512 }
|
Chris@153
|
513
|
Chris@153
|
514 Layer::PropertyType
|
Chris@193
|
515 SliceLayer::getPropertyType(const PropertyName &name) const
|
Chris@153
|
516 {
|
Chris@153
|
517 if (name == "Gain") return RangeProperty;
|
Chris@153
|
518 if (name == "Normalize") return ToggleProperty;
|
Chris@153
|
519 return ValueProperty;
|
Chris@153
|
520 }
|
Chris@153
|
521
|
Chris@153
|
522 QString
|
Chris@193
|
523 SliceLayer::getPropertyGroupName(const PropertyName &name) const
|
Chris@153
|
524 {
|
Chris@153
|
525 if (name == "Scale" ||
|
Chris@153
|
526 name == "Normalize" ||
|
Chris@193
|
527 name == "Sampling Mode" ||
|
Chris@193
|
528 name == "Gain") return tr("Scale");
|
Chris@193
|
529 if (name == "Plot Type" ||
|
Chris@193
|
530 name == "Bin Scale") return tr("Plot Type");
|
Chris@153
|
531 return QString();
|
Chris@153
|
532 }
|
Chris@153
|
533
|
Chris@153
|
534 int
|
Chris@193
|
535 SliceLayer::getPropertyRangeAndValue(const PropertyName &name,
|
Chris@216
|
536 int *min, int *max, int *deflt) const
|
Chris@153
|
537 {
|
Chris@216
|
538 int val = 0;
|
Chris@153
|
539
|
Chris@216
|
540 int garbage0, garbage1, garbage2;
|
Chris@153
|
541 if (!min) min = &garbage0;
|
Chris@153
|
542 if (!max) max = &garbage1;
|
Chris@248
|
543 if (!deflt) deflt = &garbage2;
|
Chris@153
|
544
|
Chris@153
|
545 if (name == "Gain") {
|
Chris@153
|
546
|
Chris@153
|
547 *min = -50;
|
Chris@153
|
548 *max = 50;
|
Chris@216
|
549 *deflt = 0;
|
Chris@153
|
550
|
Chris@193
|
551 std::cerr << "gain is " << m_gain << ", mode is " << m_samplingMode << std::endl;
|
Chris@193
|
552
|
Chris@216
|
553 val = lrint(log10(m_gain) * 20.0);
|
Chris@216
|
554 if (val < *min) val = *min;
|
Chris@216
|
555 if (val > *max) val = *max;
|
Chris@153
|
556
|
Chris@153
|
557 } else if (name == "Normalize") {
|
Chris@153
|
558
|
Chris@216
|
559 val = (m_normalize ? 1 : 0);
|
Chris@216
|
560 *deflt = 0;
|
Chris@153
|
561
|
Chris@153
|
562 } else if (name == "Colour") {
|
Chris@153
|
563
|
Chris@197
|
564 if (m_plotStyle == PlotFilledBlocks) {
|
Chris@197
|
565
|
Chris@197
|
566 *min = 0;
|
Chris@197
|
567 *max = ColourMapper::getColourMapCount() - 1;
|
Chris@216
|
568 *deflt = 0;
|
Chris@153
|
569
|
Chris@216
|
570 val = m_colourMap;
|
Chris@197
|
571
|
Chris@197
|
572 } else {
|
Chris@197
|
573
|
Chris@197
|
574 *min = 0;
|
Chris@197
|
575 *max = 5;
|
Chris@216
|
576 *deflt = 0;
|
Chris@197
|
577
|
Chris@216
|
578 if (m_colour == Qt::black) val = 0;
|
Chris@216
|
579 else if (m_colour == Qt::darkRed) val = 1;
|
Chris@216
|
580 else if (m_colour == Qt::darkBlue) val = 2;
|
Chris@216
|
581 else if (m_colour == Qt::darkGreen) val = 3;
|
Chris@216
|
582 else if (m_colour == QColor(200, 50, 255)) val = 4;
|
Chris@216
|
583 else if (m_colour == QColor(255, 150, 50)) val = 5;
|
Chris@197
|
584 }
|
Chris@153
|
585
|
Chris@153
|
586 } else if (name == "Scale") {
|
Chris@153
|
587
|
Chris@153
|
588 *min = 0;
|
Chris@153
|
589 *max = 2;
|
Chris@216
|
590 *deflt = (int)dBScale;
|
Chris@153
|
591
|
Chris@216
|
592 val = (int)m_energyScale;
|
Chris@153
|
593
|
Chris@193
|
594 } else if (name == "Sampling Mode") {
|
Chris@153
|
595
|
Chris@153
|
596 *min = 0;
|
Chris@193
|
597 *max = 2;
|
Chris@216
|
598 *deflt = (int)SampleMean;
|
Chris@193
|
599
|
Chris@216
|
600 val = (int)m_samplingMode;
|
Chris@153
|
601
|
Chris@193
|
602 } else if (name == "Plot Type") {
|
Chris@193
|
603
|
Chris@193
|
604 *min = 0;
|
Chris@197
|
605 *max = 3;
|
Chris@216
|
606 *deflt = (int)PlotSteps;
|
Chris@193
|
607
|
Chris@216
|
608 val = (int)m_plotStyle;
|
Chris@193
|
609
|
Chris@193
|
610 } else if (name == "Bin Scale") {
|
Chris@193
|
611
|
Chris@193
|
612 *min = 0;
|
Chris@198
|
613 *max = 2;
|
Chris@216
|
614 *deflt = (int)LinearBins;
|
Chris@198
|
615 // *max = 1; // I don't think we really do want to offer inverted log
|
Chris@193
|
616
|
Chris@216
|
617 val = (int)m_binScale;
|
Chris@193
|
618
|
Chris@153
|
619 } else {
|
Chris@216
|
620 val = Layer::getPropertyRangeAndValue(name, min, max, deflt);
|
Chris@153
|
621 }
|
Chris@153
|
622
|
Chris@216
|
623 return val;
|
Chris@153
|
624 }
|
Chris@153
|
625
|
Chris@153
|
626 QString
|
Chris@193
|
627 SliceLayer::getPropertyValueLabel(const PropertyName &name,
|
Chris@153
|
628 int value) const
|
Chris@153
|
629 {
|
Chris@153
|
630 if (name == "Colour") {
|
Chris@197
|
631 if (m_plotStyle == PlotFilledBlocks) {
|
Chris@197
|
632 return ColourMapper::getColourMapName(value);
|
Chris@197
|
633 } else {
|
Chris@197
|
634 switch (value) {
|
Chris@197
|
635 default:
|
Chris@197
|
636 case 0: return tr("Black");
|
Chris@197
|
637 case 1: return tr("Red");
|
Chris@197
|
638 case 2: return tr("Blue");
|
Chris@197
|
639 case 3: return tr("Green");
|
Chris@197
|
640 case 4: return tr("Purple");
|
Chris@197
|
641 case 5: return tr("Orange");
|
Chris@197
|
642 }
|
Chris@153
|
643 }
|
Chris@153
|
644 }
|
Chris@153
|
645 if (name == "Scale") {
|
Chris@153
|
646 switch (value) {
|
Chris@153
|
647 default:
|
Chris@153
|
648 case 0: return tr("Linear");
|
Chris@153
|
649 case 1: return tr("Meter");
|
Chris@153
|
650 case 2: return tr("dB");
|
Chris@153
|
651 }
|
Chris@153
|
652 }
|
Chris@193
|
653 if (name == "Sampling Mode") {
|
Chris@153
|
654 switch (value) {
|
Chris@153
|
655 default:
|
Chris@193
|
656 case 0: return tr("Any");
|
Chris@193
|
657 case 1: return tr("Mean");
|
Chris@193
|
658 case 2: return tr("Peak");
|
Chris@193
|
659 }
|
Chris@193
|
660 }
|
Chris@193
|
661 if (name == "Plot Type") {
|
Chris@193
|
662 switch (value) {
|
Chris@193
|
663 default:
|
Chris@193
|
664 case 0: return tr("Lines");
|
Chris@193
|
665 case 1: return tr("Steps");
|
Chris@193
|
666 case 2: return tr("Blocks");
|
Chris@197
|
667 case 3: return tr("Colours");
|
Chris@193
|
668 }
|
Chris@193
|
669 }
|
Chris@193
|
670 if (name == "Bin Scale") {
|
Chris@193
|
671 switch (value) {
|
Chris@193
|
672 default:
|
Chris@198
|
673 case 0: return tr("Linear Bins");
|
Chris@198
|
674 case 1: return tr("Log Bins");
|
Chris@198
|
675 case 2: return tr("Rev Log Bins");
|
Chris@153
|
676 }
|
Chris@153
|
677 }
|
Chris@153
|
678 return tr("<unknown>");
|
Chris@153
|
679 }
|
Chris@153
|
680
|
Chris@167
|
681 RangeMapper *
|
Chris@193
|
682 SliceLayer::getNewPropertyRangeMapper(const PropertyName &name) const
|
Chris@167
|
683 {
|
Chris@167
|
684 if (name == "Gain") {
|
Chris@167
|
685 return new LinearRangeMapper(-50, 50, -25, 25, tr("dB"));
|
Chris@167
|
686 }
|
Chris@167
|
687 return 0;
|
Chris@167
|
688 }
|
Chris@167
|
689
|
Chris@133
|
690 void
|
Chris@193
|
691 SliceLayer::setProperty(const PropertyName &name, int value)
|
Chris@133
|
692 {
|
Chris@153
|
693 if (name == "Gain") {
|
Chris@153
|
694 setGain(pow(10, float(value)/20.0));
|
Chris@153
|
695 } else if (name == "Colour") {
|
Chris@197
|
696 if (m_plotStyle == PlotFilledBlocks) {
|
Chris@197
|
697 setFillColourMap(value);
|
Chris@197
|
698 } else {
|
Chris@197
|
699 switch (value) {
|
Chris@197
|
700 default:
|
Chris@197
|
701 case 0: setBaseColour(Qt::black); break;
|
Chris@197
|
702 case 1: setBaseColour(Qt::darkRed); break;
|
Chris@197
|
703 case 2: setBaseColour(Qt::darkBlue); break;
|
Chris@197
|
704 case 3: setBaseColour(Qt::darkGreen); break;
|
Chris@197
|
705 case 4: setBaseColour(QColor(200, 50, 255)); break;
|
Chris@197
|
706 case 5: setBaseColour(QColor(255, 150, 50)); break;
|
Chris@197
|
707 }
|
Chris@153
|
708 }
|
Chris@153
|
709 } else if (name == "Scale") {
|
Chris@153
|
710 switch (value) {
|
Chris@153
|
711 default:
|
Chris@153
|
712 case 0: setEnergyScale(LinearScale); break;
|
Chris@153
|
713 case 1: setEnergyScale(MeterScale); break;
|
Chris@153
|
714 case 2: setEnergyScale(dBScale); break;
|
Chris@153
|
715 }
|
Chris@193
|
716 } else if (name == "Plot Type") {
|
Chris@193
|
717 setPlotStyle(PlotStyle(value));
|
Chris@193
|
718 } else if (name == "Sampling Mode") {
|
Chris@193
|
719 switch (value) {
|
Chris@193
|
720 default:
|
Chris@193
|
721 case 0: setSamplingMode(NearestSample); break;
|
Chris@193
|
722 case 1: setSamplingMode(SampleMean); break;
|
Chris@193
|
723 case 2: setSamplingMode(SamplePeak); break;
|
Chris@193
|
724 }
|
Chris@193
|
725 } else if (name == "Bin Scale") {
|
Chris@193
|
726 switch (value) {
|
Chris@193
|
727 default:
|
Chris@193
|
728 case 0: setBinScale(LinearBins); break;
|
Chris@193
|
729 case 1: setBinScale(LogBins); break;
|
Chris@193
|
730 case 2: setBinScale(InvertedLogBins); break;
|
Chris@193
|
731 }
|
Chris@153
|
732 } else if (name == "Normalize") {
|
Chris@153
|
733 setNormalize(value ? true : false);
|
Chris@153
|
734 }
|
Chris@153
|
735 }
|
Chris@153
|
736
|
Chris@153
|
737 void
|
Chris@193
|
738 SliceLayer::setBaseColour(QColor colour)
|
Chris@153
|
739 {
|
Chris@153
|
740 if (m_colour == colour) return;
|
Chris@153
|
741 m_colour = colour;
|
Chris@153
|
742 emit layerParametersChanged();
|
Chris@153
|
743 }
|
Chris@153
|
744
|
Chris@153
|
745 void
|
Chris@197
|
746 SliceLayer::setFillColourMap(int map)
|
Chris@197
|
747 {
|
Chris@197
|
748 if (m_colourMap == map) return;
|
Chris@197
|
749 m_colourMap = map;
|
Chris@197
|
750 emit layerParametersChanged();
|
Chris@197
|
751 }
|
Chris@197
|
752
|
Chris@197
|
753 void
|
Chris@193
|
754 SliceLayer::setEnergyScale(EnergyScale scale)
|
Chris@153
|
755 {
|
Chris@153
|
756 if (m_energyScale == scale) return;
|
Chris@153
|
757 m_energyScale = scale;
|
Chris@153
|
758 emit layerParametersChanged();
|
Chris@153
|
759 }
|
Chris@153
|
760
|
Chris@153
|
761 void
|
Chris@193
|
762 SliceLayer::setSamplingMode(SamplingMode mode)
|
Chris@153
|
763 {
|
Chris@193
|
764 if (m_samplingMode == mode) return;
|
Chris@193
|
765 m_samplingMode = mode;
|
Chris@153
|
766 emit layerParametersChanged();
|
Chris@153
|
767 }
|
Chris@153
|
768
|
Chris@153
|
769 void
|
Chris@193
|
770 SliceLayer::setPlotStyle(PlotStyle style)
|
Chris@153
|
771 {
|
Chris@193
|
772 if (m_plotStyle == style) return;
|
Chris@197
|
773 bool colourTypeChanged = (style == PlotFilledBlocks ||
|
Chris@197
|
774 m_plotStyle == PlotFilledBlocks);
|
Chris@193
|
775 m_plotStyle = style;
|
Chris@197
|
776 if (colourTypeChanged) {
|
Chris@197
|
777 emit layerParameterRangesChanged();
|
Chris@197
|
778 }
|
Chris@153
|
779 emit layerParametersChanged();
|
Chris@153
|
780 }
|
Chris@153
|
781
|
Chris@153
|
782 void
|
Chris@193
|
783 SliceLayer::setBinScale(BinScale scale)
|
Chris@153
|
784 {
|
Chris@193
|
785 if (m_binScale == scale) return;
|
Chris@193
|
786 m_binScale = scale;
|
Chris@153
|
787 emit layerParametersChanged();
|
Chris@153
|
788 }
|
Chris@153
|
789
|
Chris@153
|
790 void
|
Chris@193
|
791 SliceLayer::setNormalize(bool n)
|
Chris@153
|
792 {
|
Chris@153
|
793 if (m_normalize == n) return;
|
Chris@153
|
794 m_normalize = n;
|
Chris@153
|
795 emit layerParametersChanged();
|
Chris@153
|
796 }
|
Chris@153
|
797
|
Chris@153
|
798 void
|
Chris@193
|
799 SliceLayer::setGain(float gain)
|
Chris@153
|
800 {
|
Chris@153
|
801 if (m_gain == gain) return;
|
Chris@153
|
802 m_gain = gain;
|
Chris@153
|
803 emit layerParametersChanged();
|
Chris@153
|
804 }
|
Chris@153
|
805
|
Chris@153
|
806 QString
|
Chris@193
|
807 SliceLayer::toXmlString(QString indent, QString extraAttributes) const
|
Chris@153
|
808 {
|
Chris@153
|
809 QString s;
|
Chris@153
|
810
|
Chris@153
|
811 s += QString("colour=\"%1\" "
|
Chris@197
|
812 "colourScheme=\"%2\" "
|
Chris@197
|
813 "energyScale=\"%3\" "
|
Chris@197
|
814 "samplingMode=\"%4\" "
|
Chris@197
|
815 "gain=\"%5\" "
|
Chris@197
|
816 "normalize=\"%6\"")
|
Chris@153
|
817 .arg(encodeColour(m_colour))
|
Chris@197
|
818 .arg(m_colourMap)
|
Chris@153
|
819 .arg(m_energyScale)
|
Chris@193
|
820 .arg(m_samplingMode)
|
Chris@153
|
821 .arg(m_gain)
|
Chris@153
|
822 .arg(m_normalize ? "true" : "false");
|
Chris@153
|
823
|
Chris@153
|
824 return Layer::toXmlString(indent, extraAttributes + " " + s);
|
Chris@153
|
825 }
|
Chris@153
|
826
|
Chris@153
|
827 void
|
Chris@193
|
828 SliceLayer::setProperties(const QXmlAttributes &attributes)
|
Chris@153
|
829 {
|
Chris@153
|
830 bool ok = false;
|
Chris@153
|
831
|
Chris@153
|
832 QString colourSpec = attributes.value("colour");
|
Chris@153
|
833 if (colourSpec != "") {
|
Chris@153
|
834 QColor colour(colourSpec);
|
Chris@153
|
835 if (colour.isValid()) {
|
Chris@153
|
836 setBaseColour(QColor(colourSpec));
|
Chris@153
|
837 }
|
Chris@153
|
838 }
|
Chris@153
|
839
|
Chris@153
|
840 EnergyScale scale = (EnergyScale)
|
Chris@153
|
841 attributes.value("energyScale").toInt(&ok);
|
Chris@153
|
842 if (ok) setEnergyScale(scale);
|
Chris@153
|
843
|
Chris@193
|
844 SamplingMode mode = (SamplingMode)
|
Chris@193
|
845 attributes.value("samplingMode").toInt(&ok);
|
Chris@193
|
846 if (ok) setSamplingMode(mode);
|
Chris@153
|
847
|
Chris@197
|
848 int colourMap = attributes.value("colourScheme").toInt(&ok);
|
Chris@197
|
849 if (ok) setFillColourMap(colourMap);
|
Chris@197
|
850
|
Chris@153
|
851 float gain = attributes.value("gain").toFloat(&ok);
|
Chris@153
|
852 if (ok) setGain(gain);
|
Chris@153
|
853
|
Chris@153
|
854 bool normalize = (attributes.value("normalize").trimmed() == "true");
|
Chris@153
|
855 setNormalize(normalize);
|
Chris@133
|
856 }
|
Chris@133
|
857
|
Chris@133
|
858 bool
|
Chris@248
|
859 SliceLayer::getValueExtents(float &, float &, bool &, QString &) const
|
Chris@133
|
860 {
|
Chris@133
|
861 return false;
|
Chris@133
|
862 }
|
Chris@133
|
863
|