Chris@690: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@690: Chris@690: /* Chris@690: Sonic Visualiser Chris@690: An audio file viewer and annotation editor. Chris@690: Centre for Digital Music, Queen Mary, University of London. Chris@690: This file copyright 2006-2013 Chris Cannam and QMUL. Chris@690: Chris@690: This program is free software; you can redistribute it and/or Chris@690: modify it under the terms of the GNU General Public License as Chris@690: published by the Free Software Foundation; either version 2 of the Chris@690: License, or (at your option) any later version. See the file Chris@690: COPYING included with this distribution for more information. Chris@690: */ Chris@690: Chris@690: #include "PianoScale.h" Chris@690: Chris@690: #include Chris@690: Chris@690: #include Chris@690: Chris@690: #include "base/Pitch.h" Chris@690: Chris@1077: #include "LayerGeometryProvider.h" Chris@1276: #include "HorizontalScaleProvider.h" Chris@690: Chris@1238: #include Chris@1238: using namespace std; Chris@1238: Chris@690: void Chris@918: PianoScale::paintPianoVertical(LayerGeometryProvider *v, Chris@1266: QPainter &paint, Chris@1266: QRect r, Chris@1266: double minf, Chris@1266: double maxf) Chris@690: { Chris@690: int x0 = r.x(), y0 = r.y(), x1 = r.x() + r.width(), y1 = r.y() + r.height(); Chris@690: Chris@690: paint.drawLine(x0, y0, x0, y1); Chris@690: Chris@690: int py = y1, ppy = y1; Chris@690: paint.setBrush(paint.pen().color()); Chris@690: Chris@690: for (int i = 0; i < 128; ++i) { Chris@690: Chris@1266: double f = Pitch::getFrequencyForPitch(i); Chris@1266: int y = int(lrint(v->getYForFrequency(f, minf, maxf, true))); Chris@690: Chris@1266: if (y < y0 - 2) break; Chris@1266: if (y > y1 + 2) { Chris@1266: continue; Chris@1266: } Chris@1266: Chris@1266: int n = (i % 12); Chris@1266: Chris@1266: if (n == 1) { Chris@1266: // C# -- fill the C from here Chris@1266: QColor col = Qt::gray; Chris@1266: if (i == 61) { // filling middle C Chris@1266: col = Qt::blue; Chris@1266: col = col.light(150); Chris@1266: } Chris@1266: if (ppy - y > 2) { Chris@1266: paint.fillRect(x0 + 1, Chris@1266: y, Chris@1266: x1 - x0, Chris@1266: (py + ppy) / 2 - y, Chris@1266: col); Chris@1266: } Chris@1266: } Chris@1266: Chris@1266: if (n == 1 || n == 3 || n == 6 || n == 8 || n == 10) { Chris@1266: // black notes Chris@1266: paint.drawLine(x0 + 1, y, x1, y); Chris@1266: int rh = ((py - y) / 4) * 2; Chris@1266: if (rh < 2) rh = 2; Chris@1266: paint.drawRect(x0 + 1, y - (py-y)/4, (x1 - x0) / 2, rh); Chris@1266: } else if (n == 0 || n == 5) { Chris@1266: // C, F Chris@1266: if (py < y1) { Chris@1266: paint.drawLine(x0 + 1, (y + py) / 2, x1, (y + py) / 2); Chris@1266: } Chris@1266: } Chris@1266: Chris@1266: ppy = py; Chris@1266: py = y; Chris@690: } Chris@690: } Chris@690: Chris@1238: void Chris@1238: PianoScale::paintPianoHorizontal(LayerGeometryProvider *v, Chris@1238: const HorizontalScaleProvider *p, Chris@1238: QPainter &paint, Chris@1238: QRect r) Chris@1238: { Chris@1238: int x0 = r.x(), y0 = r.y(), x1 = r.x() + r.width(), y1 = r.y() + r.height(); Chris@1238: Chris@1238: paint.drawLine(x0, y0, x1, y0); Chris@1238: Chris@1238: int px = x0, ppx = x0; Chris@1238: paint.setBrush(paint.pen().color()); Chris@1238: Chris@1238: for (int i = 0; i < 128; ++i) { Chris@1238: Chris@1238: double f = Pitch::getFrequencyForPitch(i); Chris@1238: int x = int(lrint(p->getXForFrequency(v, f))); Chris@1238: Chris@1238: if (i == 0) { Chris@1238: px = ppx = x; Chris@1238: } Chris@1238: if (i == 1) { Chris@1238: ppx = px - (x - px); Chris@1238: } Chris@1238: Chris@1238: if (x < x0) { Chris@1238: ppx = px; Chris@1238: px = x; Chris@1238: continue; Chris@1238: } Chris@1238: Chris@1238: if (x > x1) { Chris@1238: break; Chris@1238: } Chris@1238: Chris@1238: int n = (i % 12); Chris@1238: Chris@1238: if (n == 1) { Chris@1238: // C# -- fill the C from here Chris@1238: QColor col = Qt::gray; Chris@1238: if (i == 61) { // filling middle C Chris@1238: col = Qt::blue; Chris@1238: col = col.light(150); Chris@1238: } Chris@1238: if (x - ppx > 2) { Chris@1238: paint.fillRect((px + ppx) / 2 + 1, Chris@1238: y0 + 1, Chris@1238: x - (px + ppx) / 2 - 1, Chris@1238: y1 - y0, Chris@1238: col); Chris@1238: } Chris@1238: } Chris@1238: Chris@1238: if (n == 1 || n == 3 || n == 6 || n == 8 || n == 10) { Chris@1238: // black notes Chris@1238: paint.drawLine(x, y0, x, y1); Chris@1238: int rw = int(lrint(double(x - px) / 4) * 2); Chris@1238: if (rw < 2) rw = 2; Chris@1238: paint.drawRect(x - rw/2, (y0 + y1) / 2, rw, (y1 - y0) / 2); Chris@1238: } else if (n == 0 || n == 5) { Chris@1238: // C, F Chris@1238: if (px < x1) { Chris@1238: paint.drawLine((x + px) / 2, y0, (x + px) / 2, y1); Chris@1238: } Chris@1238: } Chris@1238: Chris@1238: ppx = px; Chris@1238: px = x; Chris@1238: } Chris@1238: } Chris@1238: Chris@1238: