annotate layer/Colour3DPlotRenderer.cpp @ 1097:92ac1fce7102 spectrogram-minor-refactor

Introduce peak frequency rendering (basics of)
author Chris Cannam
date Mon, 11 Jul 2016 16:03:39 +0100
parents 6288f1b5f49b
children 5c6271734790
rev   line source
Chris@1071 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1071 2
Chris@1071 3 /*
Chris@1071 4 Sonic Visualiser
Chris@1071 5 An audio file viewer and annotation editor.
Chris@1071 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1071 7 This file copyright 2006-2016 Chris Cannam and QMUL.
Chris@1071 8
Chris@1071 9 This program is free software; you can redistribute it and/or
Chris@1071 10 modify it under the terms of the GNU General Public License as
Chris@1071 11 published by the Free Software Foundation; either version 2 of the
Chris@1071 12 License, or (at your option) any later version. See the file
Chris@1071 13 COPYING included with this distribution for more information.
Chris@1071 14 */
Chris@1071 15
Chris@1071 16 #include "Colour3DPlotRenderer.h"
Chris@1074 17 #include "RenderTimer.h"
Chris@1071 18
Chris@1075 19 #include "data/model/DenseThreeDimensionalModel.h"
Chris@1075 20 #include "data/model/Dense3DModelPeakCache.h"
Chris@1075 21 #include "data/model/FFTModel.h"
Chris@1075 22
Chris@1077 23 #include "LayerGeometryProvider.h"
Chris@1082 24 #include "VerticalBinLayer.h"
Chris@1075 25
Chris@1079 26 #include <vector>
Chris@1079 27
Chris@1094 28 //#define DEBUG_SPECTROGRAM_REPAINT 1
Chris@1094 29
Chris@1079 30 using namespace std;
Chris@1079 31
Chris@1073 32 Colour3DPlotRenderer::RenderResult
Chris@1090 33 Colour3DPlotRenderer::render(LayerGeometryProvider *v, QPainter &paint, QRect rect)
Chris@1076 34 {
Chris@1090 35 return render(v, paint, rect, false);
Chris@1076 36 }
Chris@1076 37
Chris@1076 38 Colour3DPlotRenderer::RenderResult
Chris@1090 39 Colour3DPlotRenderer::renderTimeConstrained(LayerGeometryProvider *v,
Chris@1090 40 QPainter &paint, QRect rect)
Chris@1076 41 {
Chris@1090 42 return render(v, paint, rect, true);
Chris@1076 43 }
Chris@1076 44
Chris@1096 45 QRect
Chris@1096 46 Colour3DPlotRenderer::getLargestUncachedRect()
Chris@1096 47 {
Chris@1096 48 int h = m_cache.getSize().height();
Chris@1096 49
Chris@1096 50 QRect areaLeft(0, 0, m_cache.getValidLeft(), h);
Chris@1096 51 QRect areaRight(m_cache.getValidRight(), 0,
Chris@1096 52 m_cache.getSize().width() - m_cache.getValidRight(), h);
Chris@1096 53
Chris@1096 54 if (areaRight.width() > areaLeft.width()) {
Chris@1096 55 return areaRight;
Chris@1096 56 } else {
Chris@1096 57 return areaLeft;
Chris@1096 58 }
Chris@1096 59 }
Chris@1096 60
Chris@1076 61 Colour3DPlotRenderer::RenderResult
Chris@1090 62 Colour3DPlotRenderer::render(LayerGeometryProvider *v,
Chris@1090 63 QPainter &paint, QRect rect, bool timeConstrained)
Chris@1073 64 {
Chris@1079 65 sv_frame_t startFrame = v->getStartFrame();
Chris@1079 66
Chris@1079 67 int x0 = v->getXForViewX(rect.x());
Chris@1079 68 int x1 = v->getXForViewX(rect.x() + rect.width());
Chris@1079 69 if (x0 < 0) x0 = 0;
Chris@1079 70 if (x1 > v->getPaintWidth()) x1 = v->getPaintWidth();
Chris@1079 71
Chris@1079 72 m_cache.resize(v->getPaintSize());
Chris@1079 73 m_cache.setZoomLevel(v->getZoomLevel());
Chris@1079 74
Chris@1090 75 cerr << "cache start " << m_cache.getStartFrame()
Chris@1090 76 << " valid left " << m_cache.getValidLeft()
Chris@1090 77 << " valid right " << m_cache.getValidRight()
Chris@1094 78 << endl;
Chris@1094 79 cerr << " view start " << startFrame
Chris@1090 80 << " x0 " << x0
Chris@1090 81 << " x1 " << x1
Chris@1090 82 << endl;
Chris@1090 83
Chris@1094 84 bool bufferIsBinResolution = useBinResolutionForDrawBuffer(v);
Chris@1094 85
Chris@1094 86 if (bufferIsBinResolution) {
Chris@1094 87 // Rendering should be fast in this situation because we are
Chris@1094 88 // quite well zoomed-in, and the sums are easier this
Chris@1094 89 // way. Calculating boundaries later will be fiddly for
Chris@1094 90 // partial paints otherwise.
Chris@1094 91 timeConstrained = false;
Chris@1094 92 }
Chris@1090 93
Chris@1079 94 if (m_cache.isValid()) { // some part of the cache is valid
Chris@1079 95
Chris@1079 96 if (v->getXForFrame(m_cache.getStartFrame()) ==
Chris@1079 97 v->getXForFrame(startFrame) &&
Chris@1079 98 m_cache.getValidLeft() <= x0 &&
Chris@1079 99 m_cache.getValidRight() >= x1) {
Chris@1090 100
Chris@1090 101 cerr << "cache hit" << endl;
Chris@1090 102
Chris@1079 103 // cache is valid for the complete requested area
Chris@1079 104 paint.drawImage(rect, m_cache.getImage(), rect);
Chris@1079 105 return { rect, {} };
Chris@1079 106
Chris@1079 107 } else {
Chris@1090 108 cerr << "cache partial hit" << endl;
Chris@1090 109
Chris@1079 110 // cache doesn't begin at the right frame or doesn't
Chris@1079 111 // contain the complete view, but might be scrollable or
Chris@1079 112 // partially usable
Chris@1090 113 m_cache.scrollTo(v, startFrame);
Chris@1079 114
Chris@1079 115 // if we are not time-constrained, then we want to paint
Chris@1081 116 // the whole area in one go; we don't return a partial
Chris@1081 117 // paint. To avoid providing the more complex logic to
Chris@1081 118 // handle painting discontiguous areas, if the only valid
Chris@1079 119 // part of cache is in the middle, just make the whole
Chris@1079 120 // thing invalid and start again.
Chris@1079 121 if (!timeConstrained) {
Chris@1079 122 if (m_cache.getValidLeft() > x0 &&
Chris@1079 123 m_cache.getValidRight() < x1) {
Chris@1079 124 m_cache.invalidate();
Chris@1079 125 }
Chris@1079 126 }
Chris@1079 127 }
Chris@1090 128 } else {
Chris@1090 129 // cache completely invalid
Chris@1090 130 m_cache.setStartFrame(startFrame);
Chris@1075 131 }
Chris@1075 132
Chris@1079 133 bool rightToLeft = false;
Chris@1079 134
Chris@1079 135 if (!m_cache.isValid() && timeConstrained) {
Chris@1081 136 // When rendering the whole area, in a context where we might
Chris@1081 137 // not be able to complete the work, start from somewhere near
Chris@1081 138 // the middle so that the region of interest appears first
Chris@1079 139
Chris@1079 140 //!!! (perhaps we should avoid doing this if past repaints
Chris@1079 141 //!!! have been fast enough to do the whole in one shot)
Chris@1079 142 if (x0 == 0 && x1 == v->getPaintWidth()) {
Chris@1079 143 x0 = int(x1 * 0.3);
Chris@1079 144 }
Chris@1079 145 }
Chris@1079 146
Chris@1079 147 if (m_cache.isValid()) {
Chris@1090 148 cerr << "cache somewhat valid" << endl;
Chris@1090 149
Chris@1079 150 // When rendering only a part of the cache, we need to make
Chris@1079 151 // sure that the part we're rendering is adjacent to (or
Chris@1079 152 // overlapping) a valid area of cache, if we have one. The
Chris@1079 153 // alternative is to ditch the valid area of cache and render
Chris@1079 154 // only the requested area, but that's risky because this can
Chris@1079 155 // happen when just waving the pointer over a small part of
Chris@1079 156 // the view -- if we lose the partly-built cache every time
Chris@1079 157 // the user does that, we'll never finish building it.
Chris@1079 158 int left = x0;
Chris@1079 159 int width = x1 - x0;
Chris@1079 160 bool isLeftOfValidArea = false;
Chris@1079 161 m_cache.adjustToTouchValidArea(left, width, isLeftOfValidArea);
Chris@1079 162 x0 = left;
Chris@1079 163 x1 = x0 + width;
Chris@1079 164
Chris@1079 165 // That call also told us whether we should be painting
Chris@1079 166 // sub-regions of our target region in right-to-left order in
Chris@1079 167 // order to ensure contiguity
Chris@1079 168 rightToLeft = isLeftOfValidArea;
Chris@1079 169 }
Chris@1075 170
Chris@1094 171 // Note, we always paint the full height. Smaller heights can be
Chris@1094 172 // used when painting direct from cache (outside this function),
Chris@1094 173 // but we want to ensure the cache is coherent without having to
Chris@1094 174 // worry about vertical matching of required and valid areas as
Chris@1094 175 // well as horizontal. That's why this function didn't take any
Chris@1094 176 // y/height parameters.
Chris@1094 177
Chris@1094 178 if (bufferIsBinResolution) {
Chris@1094 179 renderToCacheBinResolution(v, x0, x1 - x0);
Chris@1094 180 } else {
Chris@1094 181 renderToCachePixelResolution(v, x0, x1 - x0, rightToLeft, timeConstrained);
Chris@1094 182 }
Chris@1079 183
Chris@1079 184 QRect pr = rect & m_cache.getValidArea();
Chris@1079 185 paint.drawImage(pr.x(), pr.y(), m_cache.getImage(),
Chris@1079 186 pr.x(), pr.y(), pr.width(), pr.height());
Chris@1079 187
Chris@1079 188 if (!timeConstrained && (pr != rect)) {
Chris@1079 189 //!!! on a first cut, there is a risk that this will happen
Chris@1079 190 //!!! when we are at start/end of model -- trap, report, and
Chris@1079 191 //!!! then fix
Chris@1079 192 throw std::logic_error("internal error: failed to render entire requested rect even when not time-constrained");
Chris@1079 193 }
Chris@1079 194
Chris@1079 195 return { pr, {} };
Chris@1079 196
Chris@1073 197 //!!! todo: timing/incomplete paint
Chris@1073 198
Chris@1073 199 //!!! todo: peak frequency style
Chris@1073 200
Chris@1073 201 //!!! todo: transparent style from Colour3DPlot
Chris@1074 202
Chris@1079 203 //!!! todo: view magnitudes / normalise visible area
Chris@1079 204
Chris@1079 205 //!!! todo: alter documentation for view mag stuff (cached paints
Chris@1079 206 //!!! do not update MagnitudeRange)
Chris@1079 207
Chris@1079 208 //!!! todo, here or in caller: illuminateLocalFeatures
Chris@1079 209
Chris@1079 210 //!!! fft model scaling?
Chris@1079 211
Chris@1079 212 //!!! should we own the Dense3DModelPeakCache here? or should it persist
Chris@1073 213 }
Chris@1073 214
Chris@1094 215 bool
Chris@1094 216 Colour3DPlotRenderer::useBinResolutionForDrawBuffer(LayerGeometryProvider *v) const
Chris@1094 217 {
Chris@1094 218 DenseThreeDimensionalModel *model = m_sources.source;
Chris@1094 219 if (!model) return false;
Chris@1094 220 int binResolution = model->getResolution();
Chris@1094 221 int zoomLevel = v->getZoomLevel();
Chris@1094 222 return (binResolution > zoomLevel);
Chris@1094 223 }
Chris@1094 224
Chris@1080 225 void
Chris@1094 226 Colour3DPlotRenderer::renderToCachePixelResolution(LayerGeometryProvider *v,
Chris@1094 227 int x0, int repaintWidth,
Chris@1094 228 bool rightToLeft,
Chris@1094 229 bool timeConstrained)
Chris@1079 230 {
Chris@1094 231 cerr << "renderToCachePixelResolution" << endl;
Chris@1094 232
Chris@1094 233 // Draw to the draw buffer, and then copy from there. The draw
Chris@1094 234 // buffer is at the same resolution as the target in the cache, so
Chris@1094 235 // no extra scaling needed.
Chris@1079 236
Chris@1079 237 DenseThreeDimensionalModel *model = m_sources.source;
Chris@1079 238 if (!model || !model->isOK() || !model->isReady()) {
Chris@1079 239 throw std::logic_error("no source model provided, or model not ready");
Chris@1079 240 }
Chris@1079 241
Chris@1079 242 int h = v->getPaintHeight();
Chris@1079 243
Chris@1094 244 clearDrawBuffer(repaintWidth, h);
Chris@1079 245
Chris@1094 246 vector<int> binforx(repaintWidth);
Chris@1079 247 vector<double> binfory(h);
Chris@1079 248
Chris@1079 249 bool usePeaksCache = false;
Chris@1079 250 int binsPerPeak = 1;
Chris@1094 251 int zoomLevel = v->getZoomLevel();
Chris@1094 252 int binResolution = model->getResolution();
Chris@1079 253
Chris@1094 254 for (int x = 0; x < repaintWidth; ++x) {
Chris@1094 255 sv_frame_t f0 = v->getFrameForX(x0 + x);
Chris@1094 256 double s0 = double(f0 - model->getStartFrame()) / binResolution;
Chris@1094 257 binforx[x] = int(s0 + 0.0001);
Chris@1094 258 }
Chris@1080 259
Chris@1094 260 if (m_sources.peaks) { // peaks cache exists
Chris@1080 261
Chris@1094 262 binsPerPeak = m_sources.peaks->getColumnsPerPeak();
Chris@1094 263 usePeaksCache = (binResolution * binsPerPeak) < zoomLevel;
Chris@1094 264
Chris@1094 265 if (m_params.colourScale.getScale() ==
Chris@1094 266 ColourScale::PhaseColourScale) {
Chris@1094 267 usePeaksCache = false;
Chris@1079 268 }
Chris@1079 269 }
Chris@1082 270
Chris@1094 271 cerr << "[PIX] zoomLevel = " << zoomLevel
Chris@1094 272 << ", binResolution " << binResolution
Chris@1094 273 << ", binsPerPeak " << binsPerPeak
Chris@1094 274 << ", peak cache " << m_sources.peaks
Chris@1094 275 << ", usePeaksCache = " << usePeaksCache
Chris@1094 276 << endl;
Chris@1094 277
Chris@1080 278 for (int y = 0; y < h; ++y) {
Chris@1090 279 binfory[y] = m_sources.verticalBinLayer->getBinForY(v, h - y - 1);
Chris@1080 280 }
Chris@1079 281
Chris@1097 282 int attainedWidth;
Chris@1097 283
Chris@1097 284 if (m_params.binDisplay == PeakFrequencies) {
Chris@1097 285 attainedWidth = renderDrawBufferPeakFrequencies(v,
Chris@1097 286 repaintWidth,
Chris@1097 287 h,
Chris@1097 288 binforx,
Chris@1097 289 binfory,
Chris@1097 290 rightToLeft,
Chris@1097 291 timeConstrained);
Chris@1097 292
Chris@1097 293 } else {
Chris@1097 294 attainedWidth = renderDrawBuffer(repaintWidth,
Chris@1080 295 h,
Chris@1080 296 binforx,
Chris@1080 297 binfory,
Chris@1080 298 usePeaksCache,
Chris@1080 299 rightToLeft,
Chris@1080 300 timeConstrained);
Chris@1097 301 }
Chris@1083 302
Chris@1094 303 if (attainedWidth == 0) return;
Chris@1084 304
Chris@1094 305 // draw buffer is pixel resolution, no scaling factors or padding involved
Chris@1084 306
Chris@1084 307 int paintedLeft = x0;
Chris@1084 308 if (rightToLeft) {
Chris@1084 309 paintedLeft += (repaintWidth - attainedWidth);
Chris@1084 310 }
Chris@1084 311
Chris@1094 312 m_cache.drawImage(paintedLeft, attainedWidth,
Chris@1094 313 m_drawBuffer,
Chris@1094 314 paintedLeft - x0, attainedWidth);
Chris@1094 315 }
Chris@1084 316
Chris@1094 317 void
Chris@1094 318 Colour3DPlotRenderer::renderToCacheBinResolution(LayerGeometryProvider *v,
Chris@1094 319 int x0, int repaintWidth)
Chris@1094 320 {
Chris@1094 321 cerr << "renderToCacheBinResolution" << endl;
Chris@1094 322
Chris@1094 323 // Draw to the draw buffer, and then scale-copy from there. Draw
Chris@1094 324 // buffer is at bin resolution, i.e. buffer x == source column
Chris@1094 325 // number. We use toolkit smooth scaling for interpolation.
Chris@1084 326
Chris@1094 327 DenseThreeDimensionalModel *model = m_sources.source;
Chris@1094 328 if (!model || !model->isOK() || !model->isReady()) {
Chris@1094 329 throw std::logic_error("no source model provided, or model not ready");
Chris@1094 330 }
Chris@1094 331
Chris@1094 332 // The draw buffer will contain a fragment at bin resolution. We
Chris@1094 333 // need to ensure that it starts and ends at points where a
Chris@1094 334 // time-bin boundary occurs at an exact pixel boundary, and with a
Chris@1094 335 // certain amount of overlap across existing pixels so that we can
Chris@1094 336 // scale and draw from it without smoothing errors at the edges.
Chris@1094 337
Chris@1094 338 // If (getFrameForX(x) / increment) * increment ==
Chris@1094 339 // getFrameForX(x), then x is a time-bin boundary. We want two
Chris@1094 340 // such boundaries at either side of the draw buffer -- one which
Chris@1094 341 // we draw up to, and one which we subsequently crop at.
Chris@1094 342
Chris@1094 343 sv_frame_t leftBoundaryFrame = -1, leftCropFrame = -1;
Chris@1094 344 sv_frame_t rightBoundaryFrame = -1, rightCropFrame = -1;
Chris@1094 345
Chris@1094 346 int drawBufferWidth;
Chris@1094 347 int binResolution = model->getResolution();
Chris@1094 348
Chris@1094 349 for (int x = x0; ; --x) {
Chris@1094 350 sv_frame_t f = v->getFrameForX(x);
Chris@1094 351 if ((f / binResolution) * binResolution == f) {
Chris@1094 352 if (leftCropFrame == -1) leftCropFrame = f;
Chris@1094 353 else if (x < x0 - 2) {
Chris@1094 354 leftBoundaryFrame = f;
Chris@1094 355 break;
Chris@1094 356 }
Chris@1094 357 }
Chris@1094 358 }
Chris@1094 359 for (int x = x0 + repaintWidth; ; ++x) {
Chris@1094 360 sv_frame_t f = v->getFrameForX(x);
Chris@1094 361 if ((f / binResolution) * binResolution == f) {
Chris@1094 362 if (rightCropFrame == -1) rightCropFrame = f;
Chris@1094 363 else if (x > x0 + repaintWidth + 2) {
Chris@1094 364 rightBoundaryFrame = f;
Chris@1094 365 break;
Chris@1094 366 }
Chris@1094 367 }
Chris@1094 368 }
Chris@1094 369 drawBufferWidth = int
Chris@1094 370 ((rightBoundaryFrame - leftBoundaryFrame) / binResolution);
Chris@1094 371
Chris@1094 372 int h = v->getPaintHeight();
Chris@1094 373
Chris@1095 374 // For our purposes here, the draw buffer needs to be exactly our
Chris@1095 375 // target size (so we recreate always rather than just clear it)
Chris@1095 376
Chris@1095 377 recreateDrawBuffer(drawBufferWidth, h);
Chris@1094 378
Chris@1094 379 vector<int> binforx(drawBufferWidth);
Chris@1094 380 vector<double> binfory(h);
Chris@1094 381
Chris@1094 382 for (int x = 0; x < drawBufferWidth; ++x) {
Chris@1094 383 binforx[x] = int(leftBoundaryFrame / binResolution) + x;
Chris@1094 384 }
Chris@1094 385
Chris@1094 386 cerr << "[BIN] binResolution " << binResolution
Chris@1094 387 << endl;
Chris@1094 388
Chris@1094 389 for (int y = 0; y < h; ++y) {
Chris@1094 390 binfory[y] = m_sources.verticalBinLayer->getBinForY(v, h - y - 1);
Chris@1094 391 }
Chris@1094 392
Chris@1094 393 int attainedWidth = renderDrawBuffer(drawBufferWidth,
Chris@1094 394 h,
Chris@1094 395 binforx,
Chris@1094 396 binfory,
Chris@1094 397 false,
Chris@1094 398 false,
Chris@1094 399 false);
Chris@1094 400
Chris@1094 401 if (attainedWidth == 0) return;
Chris@1094 402
Chris@1094 403 int scaledLeft = v->getXForFrame(leftBoundaryFrame);
Chris@1094 404 int scaledRight = v->getXForFrame(rightBoundaryFrame);
Chris@1095 405
Chris@1095 406 cerr << "scaling draw buffer from width " << m_drawBuffer.width()
Chris@1095 407 << " to " << (scaledRight - scaledLeft) << " (nb drawBufferWidth = "
Chris@1095 408 << drawBufferWidth << ")" << endl;
Chris@1094 409
Chris@1094 410 QImage scaled = m_drawBuffer.scaled
Chris@1094 411 (scaledRight - scaledLeft, h,
Chris@1094 412 Qt::IgnoreAspectRatio, (m_params.interpolate ?
Chris@1094 413 Qt::SmoothTransformation :
Chris@1094 414 Qt::FastTransformation));
Chris@1084 415
Chris@1094 416 int scaledLeftCrop = v->getXForFrame(leftCropFrame);
Chris@1094 417 int scaledRightCrop = v->getXForFrame(rightCropFrame);
Chris@1094 418
Chris@1094 419 int targetLeft = scaledLeftCrop;
Chris@1094 420 if (targetLeft < 0) {
Chris@1094 421 targetLeft = 0;
Chris@1094 422 }
Chris@1094 423
Chris@1094 424 int targetWidth = scaledRightCrop - targetLeft;
Chris@1094 425 if (targetLeft + targetWidth > m_cache.getSize().width()) {
Chris@1094 426 targetWidth = m_cache.getSize().width() - targetLeft;
Chris@1094 427 }
Chris@1094 428
Chris@1094 429 int sourceLeft = targetLeft - scaledLeft;
Chris@1094 430 if (sourceLeft < 0) {
Chris@1094 431 sourceLeft = 0;
Chris@1094 432 }
Chris@1094 433
Chris@1094 434 int sourceWidth = targetWidth;
Chris@1094 435
Chris@1094 436 cerr << "repaintWidth = " << repaintWidth
Chris@1094 437 << ", targetWidth = " << targetWidth << endl;
Chris@1094 438
Chris@1094 439 if (targetWidth > 0) {
Chris@1094 440 m_cache.drawImage(targetLeft, targetWidth,
Chris@1094 441 scaled,
Chris@1094 442 sourceLeft, sourceWidth);
Chris@1084 443 }
Chris@1079 444 }
Chris@1083 445
Chris@1083 446 int
Chris@1083 447 Colour3DPlotRenderer::renderDrawBuffer(int w, int h,
Chris@1083 448 const vector<int> &binforx,
Chris@1083 449 const vector<double> &binfory,
Chris@1083 450 bool usePeaksCache,
Chris@1083 451 bool rightToLeft,
Chris@1083 452 bool timeConstrained)
Chris@1083 453 {
Chris@1083 454 // Callers must have checked that the appropriate subset of
Chris@1083 455 // Sources data members are set for the supplied flags (e.g. that
Chris@1083 456 // peaks model exists if usePeaksCache)
Chris@1083 457
Chris@1083 458 RenderTimer timer(timeConstrained ?
Chris@1083 459 RenderTimer::FastRender :
Chris@1083 460 RenderTimer::NoTimeout);
Chris@1083 461
Chris@1083 462 int minbin = int(binfory[0] + 0.0001);
Chris@1083 463 int maxbin = int(binfory[h-1]);
Chris@1083 464 if (minbin < 0) minbin = 0;
Chris@1083 465 if (maxbin < 0) maxbin = minbin+1;
Chris@1083 466
Chris@1083 467 int divisor = 1;
Chris@1083 468 DenseThreeDimensionalModel *sourceModel = m_sources.source;
Chris@1083 469 if (usePeaksCache) {
Chris@1083 470 divisor = m_sources.peaks->getColumnsPerPeak();
Chris@1083 471 sourceModel = m_sources.peaks;
Chris@1083 472 }
Chris@1083 473
Chris@1083 474 int psx = -1;
Chris@1083 475
Chris@1083 476 int start = 0;
Chris@1083 477 int finish = w;
Chris@1083 478 int step = 1;
Chris@1083 479
Chris@1083 480 if (rightToLeft) {
Chris@1083 481 start = w-1;
Chris@1083 482 finish = -1;
Chris@1083 483 step = -1;
Chris@1083 484 }
Chris@1083 485
Chris@1083 486 int columnCount = 0;
Chris@1083 487
Chris@1083 488 vector<float> preparedColumn;
Chris@1094 489
Chris@1094 490 int modelWidth = sourceModel->getWidth();
Chris@1094 491 cerr << "modelWidth " << modelWidth << endl;
Chris@1083 492
Chris@1083 493 for (int x = start; x != finish; x += step) {
Chris@1083 494
Chris@1083 495 // x is the on-canvas pixel coord; sx (later) will be the
Chris@1083 496 // source column index
Chris@1083 497
Chris@1083 498 ++columnCount;
Chris@1083 499
Chris@1083 500 if (binforx[x] < 0) continue;
Chris@1083 501
Chris@1083 502 int sx0 = binforx[x] / divisor;
Chris@1083 503 int sx1 = sx0;
Chris@1083 504 if (x+1 < w) sx1 = binforx[x+1] / divisor;
Chris@1083 505 if (sx0 < 0) sx0 = sx1 - 1;
Chris@1083 506 if (sx0 < 0) continue;
Chris@1083 507 if (sx1 <= sx0) sx1 = sx0 + 1;
Chris@1083 508
Chris@1083 509 vector<float> pixelPeakColumn;
Chris@1083 510
Chris@1083 511 for (int sx = sx0; sx < sx1; ++sx) {
Chris@1083 512
Chris@1083 513 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1094 514 cerr << "sx = " << sx << endl;
Chris@1083 515 #endif
Chris@1083 516
Chris@1094 517 if (sx < 0 || sx >= modelWidth) {
Chris@1083 518 continue;
Chris@1083 519 }
Chris@1083 520
Chris@1083 521 if (sx != psx) {
Chris@1083 522
Chris@1083 523 // order:
Chris@1083 524 // get column -> scale -> record extents ->
Chris@1083 525 // normalise -> peak pick -> apply display gain ->
Chris@1083 526 // distribute/interpolate
Chris@1083 527
Chris@1083 528 ColumnOp::Column fullColumn = sourceModel->getColumn(sx);
Chris@1090 529
Chris@1094 530 // cerr << "x " << x << ", sx " << sx << ", col height " << fullColumn.size()
Chris@1094 531 // << ", minbin " << minbin << ", maxbin " << maxbin << endl;
Chris@1090 532
Chris@1083 533 ColumnOp::Column column =
Chris@1083 534 vector<float>(fullColumn.data() + minbin,
Chris@1083 535 fullColumn.data() + maxbin + 1);
Chris@1083 536
Chris@1083 537 //!!! fft scale if (m_colourScale != PhaseColourScale) {
Chris@1083 538 // column = ColumnOp::fftScale(column, m_fftSize);
Chris@1083 539 // }
Chris@1083 540
Chris@1083 541 //!!! extents recordColumnExtents(column,
Chris@1083 542 // sx,
Chris@1083 543 // overallMag,
Chris@1083 544 // overallMagChanged);
Chris@1083 545
Chris@1083 546 // if (m_colourScale != PhaseColourScale) {
Chris@1083 547 column = ColumnOp::normalize(column, m_params.normalization);
Chris@1083 548 // }
Chris@1083 549
Chris@1083 550 if (m_params.binDisplay == PeakBins) {
Chris@1083 551 column = ColumnOp::peakPick(column);
Chris@1083 552 }
Chris@1083 553
Chris@1083 554 preparedColumn =
Chris@1083 555 ColumnOp::distribute(column, //!!! gain? ColumnOp::applyGain(column, m_gain),
Chris@1083 556 h,
Chris@1083 557 binfory,
Chris@1083 558 minbin,
Chris@1083 559 m_params.interpolate);
Chris@1083 560
Chris@1083 561 psx = sx;
Chris@1083 562 }
Chris@1083 563
Chris@1083 564 if (sx == sx0) {
Chris@1083 565 pixelPeakColumn = preparedColumn;
Chris@1083 566 } else {
Chris@1083 567 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
Chris@1083 568 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
Chris@1083 569 preparedColumn[i]);
Chris@1083 570 }
Chris@1083 571 }
Chris@1083 572 }
Chris@1083 573
Chris@1083 574 if (!pixelPeakColumn.empty()) {
Chris@1083 575 for (int y = 0; y < h; ++y) {
Chris@1083 576 m_drawBuffer.setPixel
Chris@1083 577 (x,
Chris@1083 578 h-y-1,
Chris@1083 579 m_params.colourScale.getPixel(pixelPeakColumn[y]));
Chris@1083 580 }
Chris@1083 581 }
Chris@1083 582
Chris@1083 583 double fractionComplete = double(columnCount) / double(w);
Chris@1083 584 if (timer.outOfTime(fractionComplete)) {
Chris@1083 585 return columnCount;
Chris@1083 586 }
Chris@1083 587 }
Chris@1083 588
Chris@1083 589 return columnCount;
Chris@1083 590 }
Chris@1083 591
Chris@1097 592 int
Chris@1097 593 Colour3DPlotRenderer::renderDrawBufferPeakFrequencies(LayerGeometryProvider *v,
Chris@1097 594 int w, int h,
Chris@1097 595 const vector<int> &binforx,
Chris@1097 596 const vector<double> &binfory,
Chris@1097 597 bool rightToLeft,
Chris@1097 598 bool timeConstrained)
Chris@1097 599 {
Chris@1097 600 // Callers must have checked that the appropriate subset of
Chris@1097 601 // Sources data members are set for the supplied flags (e.g. that
Chris@1097 602 // fft model exists)
Chris@1097 603
Chris@1097 604 RenderTimer timer(timeConstrained ?
Chris@1097 605 RenderTimer::FastRender :
Chris@1097 606 RenderTimer::NoTimeout);
Chris@1097 607
Chris@1097 608 int minbin = int(binfory[0] + 0.0001);
Chris@1097 609 int maxbin = int(binfory[h-1]);
Chris@1097 610 if (minbin < 0) minbin = 0;
Chris@1097 611 if (maxbin < 0) maxbin = minbin+1;
Chris@1097 612
Chris@1097 613 FFTModel *fft = m_sources.fft;
Chris@1097 614
Chris@1097 615 FFTModel::PeakSet peakfreqs;
Chris@1097 616
Chris@1097 617 int psx = -1;
Chris@1097 618
Chris@1097 619 int start = 0;
Chris@1097 620 int finish = w;
Chris@1097 621 int step = 1;
Chris@1097 622
Chris@1097 623 if (rightToLeft) {
Chris@1097 624 start = w-1;
Chris@1097 625 finish = -1;
Chris@1097 626 step = -1;
Chris@1097 627 }
Chris@1097 628
Chris@1097 629 int columnCount = 0;
Chris@1097 630
Chris@1097 631 vector<float> preparedColumn;
Chris@1097 632
Chris@1097 633 int modelWidth = fft->getWidth();
Chris@1097 634 cerr << "modelWidth " << modelWidth << endl;
Chris@1097 635
Chris@1097 636 double minFreq = (double(minbin) * fft->getSampleRate()) / fft->getFFTSize();
Chris@1097 637 double maxFreq = (double(maxbin) * fft->getSampleRate()) / fft->getFFTSize();
Chris@1097 638
Chris@1097 639 bool logarithmic = (m_params.binScale == LogBinScale);
Chris@1097 640
Chris@1097 641 for (int x = start; x != finish; x += step) {
Chris@1097 642
Chris@1097 643 // x is the on-canvas pixel coord; sx (later) will be the
Chris@1097 644 // source column index
Chris@1097 645
Chris@1097 646 ++columnCount;
Chris@1097 647
Chris@1097 648 if (binforx[x] < 0) continue;
Chris@1097 649
Chris@1097 650 int sx0 = binforx[x];
Chris@1097 651 int sx1 = sx0;
Chris@1097 652 if (x+1 < w) sx1 = binforx[x+1];
Chris@1097 653 if (sx0 < 0) sx0 = sx1 - 1;
Chris@1097 654 if (sx0 < 0) continue;
Chris@1097 655 if (sx1 <= sx0) sx1 = sx0 + 1;
Chris@1097 656
Chris@1097 657 vector<float> pixelPeakColumn;
Chris@1097 658
Chris@1097 659 for (int sx = sx0; sx < sx1; ++sx) {
Chris@1097 660
Chris@1097 661 if (sx < 0 || sx >= modelWidth) {
Chris@1097 662 continue;
Chris@1097 663 }
Chris@1097 664
Chris@1097 665 if (sx != psx) {
Chris@1097 666
Chris@1097 667 ColumnOp::Column fullColumn = fft->getColumn(sx);
Chris@1097 668
Chris@1097 669 ColumnOp::Column column =
Chris@1097 670 vector<float>(fullColumn.data() + minbin,
Chris@1097 671 fullColumn.data() + maxbin + 1);
Chris@1097 672
Chris@1097 673 //!!! fft scale if (m_colourScale != ColourScale::PhaseColourScale) {
Chris@1097 674 // column = ColumnOp::fftScale(column, getFFTSize());
Chris@1097 675 // }
Chris@1097 676
Chris@1097 677 //!!! extents recordColumnExtents(column,
Chris@1097 678 // sx,
Chris@1097 679 // overallMag,
Chris@1097 680 // overallMagChanged);
Chris@1097 681
Chris@1097 682 //!!! if (m_colourScale != ColourScale::PhaseColourScale) {
Chris@1097 683 column = ColumnOp::normalize(column, m_params.normalization);
Chris@1097 684 //!!! }
Chris@1097 685
Chris@1097 686 preparedColumn = column;
Chris@1097 687 //!!! gain? preparedColumn = ColumnOp::applyGain(column, m_params.gain);
Chris@1097 688
Chris@1097 689 psx = sx;
Chris@1097 690 }
Chris@1097 691
Chris@1097 692 if (sx == sx0) {
Chris@1097 693 pixelPeakColumn = preparedColumn;
Chris@1097 694 peakfreqs = fft->getPeakFrequencies(FFTModel::AllPeaks, sx,
Chris@1097 695 minbin, maxbin - 1);
Chris@1097 696 } else {
Chris@1097 697 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
Chris@1097 698 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
Chris@1097 699 preparedColumn[i]);
Chris@1097 700 }
Chris@1097 701 }
Chris@1097 702 }
Chris@1097 703
Chris@1097 704 if (!pixelPeakColumn.empty()) {
Chris@1097 705 for (FFTModel::PeakSet::const_iterator pi = peakfreqs.begin();
Chris@1097 706 pi != peakfreqs.end(); ++pi) {
Chris@1097 707
Chris@1097 708 int bin = pi->first;
Chris@1097 709 double freq = pi->second;
Chris@1097 710
Chris@1097 711 if (bin < minbin) continue;
Chris@1097 712 if (bin > maxbin) break;
Chris@1097 713
Chris@1097 714 double value = pixelPeakColumn[bin - minbin];
Chris@1097 715
Chris@1097 716 double y = v->getYForFrequency
Chris@1097 717 (freq, minFreq, maxFreq, logarithmic);
Chris@1097 718
Chris@1097 719 int iy = int(y + 0.5);
Chris@1097 720 if (iy < 0 || iy >= h) continue;
Chris@1097 721
Chris@1097 722 m_drawBuffer.setPixel
Chris@1097 723 (x,
Chris@1097 724 iy,
Chris@1097 725 m_params.colourScale.getPixel(value));
Chris@1097 726 }
Chris@1097 727 }
Chris@1097 728
Chris@1097 729 double fractionComplete = double(columnCount) / double(w);
Chris@1097 730 if (timer.outOfTime(fractionComplete)) {
Chris@1097 731 return columnCount;
Chris@1097 732 }
Chris@1097 733 }
Chris@1097 734
Chris@1097 735 return columnCount;
Chris@1097 736 }
Chris@1097 737
Chris@1079 738 void
Chris@1095 739 Colour3DPlotRenderer::recreateDrawBuffer(int w, int h)
Chris@1079 740 {
Chris@1095 741 m_drawBuffer = QImage(w, h, QImage::Format_Indexed8);
Chris@1079 742
Chris@1095 743 for (int pixel = 0; pixel < 256; ++pixel) {
Chris@1095 744 //!!! todo: colour rotation (here 0)
Chris@1095 745 m_drawBuffer.setColor
Chris@1095 746 ((unsigned char)pixel,
Chris@1095 747 m_params.colourScale.getColourForPixel(pixel, 0).rgb());
Chris@1079 748 }
Chris@1079 749
Chris@1079 750 m_drawBuffer.fill(0);
Chris@1079 751 }
Chris@1079 752
Chris@1095 753 void
Chris@1095 754 Colour3DPlotRenderer::clearDrawBuffer(int w, int h)
Chris@1095 755 {
Chris@1095 756 if (m_drawBuffer.width() < w || m_drawBuffer.height() != h) {
Chris@1095 757 recreateDrawBuffer(w, h);
Chris@1095 758 } else {
Chris@1095 759 m_drawBuffer.fill(0);
Chris@1095 760 }
Chris@1095 761 }
Chris@1079 762
Chris@1095 763