annotate layer/PianoScale.cpp @ 893:78ae34f388f6

Tidy up layout and ranges for unit conversion dialog, highlight in red any out-of-range values
author Chris Cannam
date Mon, 15 Dec 2014 16:08:26 +0000
parents 8072264dc61f
children e0f08e108064
rev   line source
Chris@690 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@690 2
Chris@690 3 /*
Chris@690 4 Sonic Visualiser
Chris@690 5 An audio file viewer and annotation editor.
Chris@690 6 Centre for Digital Music, Queen Mary, University of London.
Chris@690 7 This file copyright 2006-2013 Chris Cannam and QMUL.
Chris@690 8
Chris@690 9 This program is free software; you can redistribute it and/or
Chris@690 10 modify it under the terms of the GNU General Public License as
Chris@690 11 published by the Free Software Foundation; either version 2 of the
Chris@690 12 License, or (at your option) any later version. See the file
Chris@690 13 COPYING included with this distribution for more information.
Chris@690 14 */
Chris@690 15
Chris@690 16 #include "PianoScale.h"
Chris@690 17
Chris@690 18 #include <QPainter>
Chris@690 19
Chris@690 20 #include <cmath>
Chris@690 21
Chris@690 22 #include "base/Pitch.h"
Chris@690 23
Chris@690 24 #include "view/View.h"
Chris@690 25
Chris@690 26 void
Chris@690 27 PianoScale::paintPianoVertical(View *v,
Chris@690 28 QPainter &paint,
Chris@690 29 QRect r,
Chris@690 30 float minf,
Chris@690 31 float maxf)
Chris@690 32 {
Chris@690 33 int x0 = r.x(), y0 = r.y(), x1 = r.x() + r.width(), y1 = r.y() + r.height();
Chris@690 34
Chris@690 35 paint.drawLine(x0, y0, x0, y1);
Chris@690 36
Chris@690 37 int py = y1, ppy = y1;
Chris@690 38 paint.setBrush(paint.pen().color());
Chris@690 39
Chris@690 40 for (int i = 0; i < 128; ++i) {
Chris@690 41
Chris@690 42 float f = Pitch::getFrequencyForPitch(i);
Chris@690 43 int y = lrintf(v->getYForFrequency(f, minf, maxf, true));
Chris@690 44
Chris@690 45 if (y < y0 - 2) break;
Chris@690 46 if (y > y1 + 2) {
Chris@690 47 continue;
Chris@690 48 }
Chris@690 49
Chris@690 50 int n = (i % 12);
Chris@690 51
Chris@690 52 if (n == 1) {
Chris@690 53 // C# -- fill the C from here
Chris@690 54 QColor col = Qt::gray;
Chris@690 55 if (i == 61) { // filling middle C
Chris@690 56 col = Qt::blue;
Chris@690 57 col = col.light(150);
Chris@690 58 }
Chris@690 59 if (ppy - y > 2) {
Chris@690 60 paint.fillRect(x0 + 1,
Chris@690 61 y,
Chris@690 62 x1 - x0,
Chris@690 63 (py + ppy) / 2 - y,
Chris@690 64 col);
Chris@690 65 }
Chris@690 66 }
Chris@690 67
Chris@690 68 if (n == 1 || n == 3 || n == 6 || n == 8 || n == 10) {
Chris@690 69 // black notes
Chris@690 70 paint.drawLine(x0 + 1, y, x1, y);
Chris@690 71 int rh = ((py - y) / 4) * 2;
Chris@690 72 if (rh < 2) rh = 2;
Chris@690 73 paint.drawRect(x0 + 1, y - (py-y)/4, (x1 - x0) / 2, rh);
Chris@690 74 } else if (n == 0 || n == 5) {
Chris@690 75 // C, F
Chris@690 76 if (py < y1) {
Chris@690 77 paint.drawLine(x0 + 1, (y + py) / 2, x1, (y + py) / 2);
Chris@690 78 }
Chris@690 79 }
Chris@690 80
Chris@690 81 ppy = py;
Chris@690 82 py = y;
Chris@690 83 }
Chris@690 84 }
Chris@690 85