annotate layer/LogColourScale.cpp @ 1386:fc3d89f88690 spectrogramparam

Use log-frequency rather than log-bin for calculating x coord in spectrum. This has the advantage that frequency positions don't move when we change the window size or oversampling ratio, but it does give us an unhelpfully large amount of space for very low frequencies - to be considered
author Chris Cannam
date Mon, 12 Nov 2018 11:34:34 +0000
parents a34a2a25907c
children f2525e6cbdf1
rev   line source
Chris@699 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@699 2
Chris@699 3 /*
Chris@699 4 Sonic Visualiser
Chris@699 5 An audio file viewer and annotation editor.
Chris@699 6 Centre for Digital Music, Queen Mary, University of London.
Chris@699 7 This file copyright 2006-2013 Chris Cannam and QMUL.
Chris@699 8
Chris@699 9 This program is free software; you can redistribute it and/or
Chris@699 10 modify it under the terms of the GNU General Public License as
Chris@699 11 published by the Free Software Foundation; either version 2 of the
Chris@699 12 License, or (at your option) any later version. See the file
Chris@699 13 COPYING included with this distribution for more information.
Chris@699 14 */
Chris@699 15
Chris@699 16 #include "LogColourScale.h"
Chris@699 17 #include "ColourScaleLayer.h"
Chris@699 18
Chris@699 19 #include "base/LogRange.h"
Chris@699 20
Chris@699 21 #include <QPainter>
Chris@699 22
Chris@699 23 #include <cmath>
Chris@699 24
Chris@1077 25 #include "LayerGeometryProvider.h"
Chris@699 26
Chris@699 27 int
Chris@918 28 LogColourScale::getWidth(LayerGeometryProvider *,
Chris@1266 29 QPainter &paint)
Chris@699 30 {
Chris@699 31 return paint.fontMetrics().width("-000.00") + 15;
Chris@699 32 }
Chris@699 33
Chris@699 34 void
Chris@918 35 LogColourScale::paintVertical(LayerGeometryProvider *v,
Chris@1266 36 const ColourScaleLayer *layer,
Chris@1266 37 QPainter &paint,
Chris@1266 38 int /* x0 */,
Chris@1266 39 double minlog,
Chris@1266 40 double maxlog)
Chris@699 41 {
Chris@918 42 int h = v->getPaintHeight();
Chris@699 43
Chris@699 44 int n = 10;
Chris@699 45
Chris@905 46 double val = minlog;
Chris@905 47 double inc = (maxlog - val) / n;
Chris@699 48
Chris@864 49 const int buflen = 40;
Chris@864 50 char buffer[buflen];
Chris@699 51
Chris@699 52 int boxx = 5, boxy = 5;
Chris@699 53 if (layer->getScaleUnits() != "") {
Chris@699 54 boxy += paint.fontMetrics().height();
Chris@699 55 }
Chris@699 56 int boxw = 10, boxh = h - boxy - 5;
Chris@699 57
Chris@699 58 int tx = 5 + boxx + boxw;
Chris@699 59 paint.drawRect(boxx, boxy, boxw, boxh);
Chris@699 60
Chris@699 61 paint.save();
Chris@699 62 for (int y = 0; y < boxh; ++y) {
Chris@1266 63 double val = ((boxh - y) * (maxlog - minlog)) / boxh + minlog;
Chris@1266 64 paint.setPen(layer->getColourForValue(v, LogRange::unmap(val)));
Chris@1266 65 paint.drawLine(boxx + 1, y + boxy + 1, boxx + boxw, y + boxy + 1);
Chris@699 66 }
Chris@699 67 paint.restore();
Chris@699 68
Chris@699 69 int dp = 0;
Chris@699 70 if (inc > 0) {
Chris@905 71 int prec = int(trunc(log10(inc)));
Chris@699 72 prec -= 1;
Chris@699 73 if (prec < 0) dp = -prec;
Chris@699 74 }
Chris@699 75
Chris@699 76 for (int i = 0; i < n; ++i) {
Chris@699 77
Chris@1266 78 int y, ty;
Chris@699 79
Chris@1266 80 y = boxy + int(boxh - ((val - minlog) * boxh) / (maxlog - minlog));
Chris@699 81
Chris@1266 82 ty = y - paint.fontMetrics().height() +
Chris@1266 83 paint.fontMetrics().ascent() + 2;
Chris@699 84
Chris@1266 85 double dv = LogRange::unmap(val);
Chris@1266 86 int digits = int(trunc(log10(dv)));
Chris@1266 87 int sf = dp + (digits > 0 ? digits : 0);
Chris@1266 88 if (sf < 2) sf = 2;
Chris@1266 89 snprintf(buffer, buflen, "%.*g", sf, dv);
Chris@699 90
Chris@1266 91 QString label = QString(buffer);
Chris@699 92
Chris@1266 93 paint.drawLine(boxx + boxw - boxw/3, y, boxx + boxw, y);
Chris@1266 94 paint.drawText(tx, ty, label);
Chris@699 95
Chris@1266 96 val += inc;
Chris@699 97 }
Chris@699 98 }