File: PianoScale.cpp

package info (click to toggle)
sonic-visualiser 5.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,744 kB
  • sloc: cpp: 158,888; ansic: 11,920; sh: 1,785; makefile: 517; xml: 64; perl: 31
file content (170 lines) | stat: -rw-r--r-- 4,723 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.
    Centre for Digital Music, Queen Mary, University of London.
    This file copyright 2006-2013 Chris Cannam and QMUL.
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#include "PianoScale.h"

#include <QPainter>

#include <cmath>

#include "base/Pitch.h"
#include "base/Debug.h"
#include "base/UnitDatabase.h"

#include "LayerGeometryProvider.h"
#include "HorizontalScaleProvider.h"

#include <iostream>
using namespace std;

namespace sv {

void
PianoScale::paintPianoVertical(LayerGeometryProvider *v,
                               QPainter &paint,
                               QRect r,
                               const CoordinateScale &scale)
{
    int x0 = r.x(), y0 = r.y(), x1 = r.x() + r.width(), y1 = r.y() + r.height();

    paint.drawLine(x0, y0, x0, y1);

    int py = y1, ppy = y1;
    paint.setBrush(paint.pen().color());

    if (UnitDatabase::asCommonUnit(scale.getUnit()) != "Hz") {
        SVDEBUG << "WARNING: PianoScale::paintPianoVertical: CoordinateScale unit is not Hz (it is: \"" << scale.getUnit() << "\")" << endl;
    }
    
    for (int i = 0; i < 128; ++i) {

        double f = Pitch::getFrequencyForPitch(i);
        int y = scale.getCoordForValueRounded(v, f);

        if (y < y0 - 2) break;
        if (y > y1 + 2) {
            continue;
        }
        
        int n = (i % 12);
        
        if (n == 1) {
            // C# -- fill the C from here
            QColor col = Qt::gray;
            if (i == 61) { // filling middle C
                col = Qt::blue;
                col = col.lighter(150);
            }
            if (ppy - y > 2) {
                paint.fillRect(x0 + 1,
                               y,
                               x1 - x0,
                               (py + ppy) / 2 - y,
                               col);
            }
        }
        
        if (n == 1 || n == 3 || n == 6 || n == 8 || n == 10) {
            // black notes
            paint.drawLine(x0 + 1, y, x1, y);
            int rh = ((py - y) / 4) * 2;
            if (rh < 2) rh = 2;
            paint.drawRect(x0 + 1, y - (py-y)/4, (x1 - x0) / 2, rh);
        } else if (n == 0 || n == 5) {
            // C, F
            if (py < y1) {
                paint.drawLine(x0 + 1, (y + py) / 2, x1, (y + py) / 2);
            }
        }
        
        ppy = py;
        py = y;
    }
}

void
PianoScale::paintPianoHorizontal(LayerGeometryProvider *v,
                                 const HorizontalScaleProvider *p,
                                 QPainter &paint,
                                 QRect r)
{
    int x0 = r.x(), y0 = r.y(), x1 = r.x() + r.width(), y1 = r.y() + r.height();

    paint.drawLine(x0, y0, x1, y0);

    int px = x0, ppx = x0;
    paint.setBrush(paint.pen().color());

    for (int i = 0; i < 128; ++i) {

        double f = Pitch::getFrequencyForPitch(i);
        int x = int(lrint(p->getXForFrequency(v, f)));

        if (i == 0) {
            px = ppx = x;
        }
        if (i == 1) {
            ppx = px - (x - px);
        }
        
        if (x < x0) {
            ppx = px;
            px = x;
            continue;
        }
        
        if (x > x1) {
            break;
        }

        int n = (i % 12);

        if (n == 1) {
            // C# -- fill the C from here
            QColor col = Qt::gray;
            if (i == 61) { // filling middle C
                col = Qt::blue;
                col = col.lighter(150);
            }
            if (x - ppx > 2) {
                paint.fillRect((px + ppx) / 2 + 1,
                               y0 + 1,
                               x - (px + ppx) / 2 - 1,
                               y1 - y0,
                               col);
            }
        }

        if (n == 1 || n == 3 || n == 6 || n == 8 || n == 10) {
            // black notes
            paint.drawLine(x, y0, x, y1);
            int rw = int(lrint(double(x - px) / 4) * 2);
            if (rw < 2) rw = 2;
            paint.drawRect(x - rw/2, (y0 + y1) / 2, rw, (y1 - y0) / 2);
        } else if (n == 0 || n == 5) {
            // C, F
            if (px < x1) {
                paint.drawLine((x + px) / 2, y0, (x + px) / 2, y1);
            }
        }
        
        ppx = px;
        px = x;
    }
}


} // end namespace sv