annotate layer/PaintAssistant.cpp @ 195:4a3bdde1ef13

* beginnings of spectrum scales
author Chris Cannam
date Mon, 29 Jan 2007 18:11:20 +0000
parents
children c2ed5014d4ff
rev   line source
Chris@195 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@195 2
Chris@195 3 /*
Chris@195 4 Sonic Visualiser
Chris@195 5 An audio file viewer and annotation editor.
Chris@195 6 Centre for Digital Music, Queen Mary, University of London.
Chris@195 7 This file copyright 2006-2007 Chris Cannam and QMUL.
Chris@195 8
Chris@195 9 This program is free software; you can redistribute it and/or
Chris@195 10 modify it under the terms of the GNU General Public License as
Chris@195 11 published by the Free Software Foundation; either version 2 of the
Chris@195 12 License, or (at your option) any later version. See the file
Chris@195 13 COPYING included with this distribution for more information.
Chris@195 14 */
Chris@195 15
Chris@195 16 #include "PaintAssistant.h"
Chris@195 17
Chris@195 18 #include "base/AudioLevel.h"
Chris@195 19
Chris@195 20 #include <QPaintDevice>
Chris@195 21 #include <QPainter>
Chris@195 22
Chris@195 23 void
Chris@195 24 PaintAssistant::paintVerticalLevelScale(QPainter &paint, QRect rect,
Chris@195 25 float minVal, float maxVal,
Chris@195 26 Scale scale, std::vector<int> *vy)
Chris@195 27 {
Chris@195 28 static float meterdbs[] = { -40, -30, -20, -15, -10,
Chris@195 29 -5, -3, -2, -1, -0.5, 0 };
Chris@195 30
Chris@195 31 int h = rect.height(), w = rect.width();
Chris@195 32 int textHeight = paint.fontMetrics().height();
Chris@195 33 int toff = -textHeight/2 + paint.fontMetrics().ascent() + 1;
Chris@195 34
Chris@195 35 int lastLabelledY = -1;
Chris@195 36
Chris@195 37 int n = 10;
Chris@195 38 if (vy) vy->clear();
Chris@195 39
Chris@195 40 for (int i = 0; i <= n; ++i) {
Chris@195 41
Chris@195 42 float val = 0.0, nval = 0.0;
Chris@195 43 QString text = "";
Chris@195 44
Chris@195 45 switch (scale) {
Chris@195 46
Chris@195 47 case LinearScale:
Chris@195 48 val = minVal + ((maxVal - minVal) * i) / n;
Chris@195 49 text = QString("%1").arg(val);//float(i) / n); // ... val
Chris@195 50 /*
Chris@195 51 if (i == 0) text = "0.0";
Chris@195 52 else {
Chris@195 53 nval = -val;
Chris@195 54 if (i == n) text = "1.0";
Chris@195 55 }
Chris@195 56 */
Chris@195 57 break;
Chris@195 58
Chris@195 59 case MeterScale: // ... min, max
Chris@195 60 val = AudioLevel::dB_to_multiplier(meterdbs[i]);
Chris@195 61 text = QString("%1").arg(meterdbs[i]);
Chris@195 62 if (i == n) text = "0dB";
Chris@195 63 if (i == 0) {
Chris@195 64 text = "-Inf";
Chris@195 65 val = 0.0;
Chris@195 66 }
Chris@195 67 break;
Chris@195 68
Chris@195 69 case dBScale: // ... min, max
Chris@195 70 val = AudioLevel::dB_to_multiplier(-(10*n) + i * 10);
Chris@195 71 text = QString("%1").arg(-(10*n) + i * 10);
Chris@195 72 if (i == n) text = "0dB";
Chris@195 73 if (i == 0) {
Chris@195 74 text = "-Inf";
Chris@195 75 val = 0.0;
Chris@195 76 }
Chris@195 77 break;
Chris@195 78 }
Chris@195 79
Chris@195 80 if (val < minVal || val > maxVal) continue;
Chris@195 81
Chris@195 82 int y = getYForValue(scale, val, minVal, maxVal, rect.y(), rect.height());
Chris@195 83
Chris@195 84 int ny = y;
Chris@195 85 if (nval != 0.0) {
Chris@195 86 ny = getYForValue(scale, nval, minVal, maxVal, rect.y(), rect.height());
Chris@195 87 }
Chris@195 88
Chris@195 89 bool spaceForLabel = (i == 0 ||
Chris@195 90 abs(y - lastLabelledY) >= textHeight - 1);
Chris@195 91
Chris@195 92 if (spaceForLabel) {
Chris@195 93
Chris@195 94 int tx = 3;
Chris@195 95 if (scale != LinearScale) {
Chris@195 96 tx = w - 10 - paint.fontMetrics().width(text);
Chris@195 97 }
Chris@195 98
Chris@195 99 int ty = y;
Chris@195 100 if (ty < paint.fontMetrics().ascent()) {
Chris@195 101 ty = paint.fontMetrics().ascent();
Chris@195 102 } else if (ty > h - paint.fontMetrics().descent()) {
Chris@195 103 ty = h - paint.fontMetrics().descent();
Chris@195 104 } else {
Chris@195 105 ty += toff;
Chris@195 106 }
Chris@195 107 paint.drawText(tx, ty, text);
Chris@195 108
Chris@195 109 lastLabelledY = ty - toff;
Chris@195 110 /*
Chris@195 111 if (ny != y) {
Chris@195 112 ty = ny;
Chris@195 113 if (ty < paint.fontMetrics().ascent()) {
Chris@195 114 ty = paint.fontMetrics().ascent();
Chris@195 115 } else if (ty > h - paint.fontMetrics().descent()) {
Chris@195 116 ty = h - paint.fontMetrics().descent();
Chris@195 117 } else {
Chris@195 118 ty += toff;
Chris@195 119 }
Chris@195 120 paint.drawText(tx, ty, text);
Chris@195 121 }
Chris@195 122 */
Chris@195 123 paint.drawLine(w - 7, y, w, y);
Chris@195 124 if (vy) vy->push_back(y);
Chris@195 125
Chris@195 126 if (ny != y) {
Chris@195 127 paint.drawLine(w - 7, ny, w, ny);
Chris@195 128 if (vy) vy->push_back(ny);
Chris@195 129 }
Chris@195 130
Chris@195 131 } else {
Chris@195 132
Chris@195 133 paint.drawLine(w - 4, y, w, y);
Chris@195 134 if (vy) vy->push_back(y);
Chris@195 135
Chris@195 136 if (ny != y) {
Chris@195 137 paint.drawLine(w - 4, ny, w, ny);
Chris@195 138 if (vy) vy->push_back(ny);
Chris@195 139 }
Chris@195 140 }
Chris@195 141 }
Chris@195 142 }
Chris@195 143
Chris@195 144 static int
Chris@195 145 dBscale(float sample, int m, float maxVal, float minVal)
Chris@195 146 {
Chris@195 147 if (sample < 0.0) return dBscale(-sample, m, maxVal, minVal);
Chris@195 148 float dB = AudioLevel::multiplier_to_dB(sample);
Chris@195 149 float mindB = AudioLevel::multiplier_to_dB(minVal);
Chris@195 150 float maxdB = AudioLevel::multiplier_to_dB(maxVal);
Chris@195 151 if (dB < mindB) return 0;
Chris@195 152 if (dB > 0.0) return m;
Chris@195 153 return int(((dB - mindB) * m) / (maxdB - mindB) + 0.1);
Chris@195 154 }
Chris@195 155
Chris@195 156 int
Chris@195 157 PaintAssistant::getYForValue(Scale scale, float value,
Chris@195 158 float minVal, float maxVal,
Chris@195 159 int minY, int height)
Chris@195 160 {
Chris@195 161 int vy = 0;
Chris@195 162
Chris@195 163 // int m = height/2;
Chris@195 164 // int my = minY + m;
Chris@195 165
Chris@195 166 switch (scale) {
Chris@195 167
Chris@195 168 case LinearScale:
Chris@195 169 // vy = my - int(m * value);
Chris@195 170 vy = minY + height - int(((value - minVal) / (maxVal - minVal)) * height);
Chris@195 171 break;
Chris@195 172
Chris@195 173 case MeterScale:
Chris@195 174 // vy = my - AudioLevel::multiplier_to_preview(value, m);
Chris@195 175 vy = minY + height - AudioLevel::multiplier_to_preview
Chris@195 176 ((value - minVal) / (maxVal - minVal), height);
Chris@195 177 break;
Chris@195 178
Chris@195 179 case dBScale:
Chris@195 180 vy = minY + height - dBscale(value, height, maxVal, minVal);
Chris@195 181 break;
Chris@195 182 }
Chris@195 183
Chris@195 184 return vy;
Chris@195 185 }