-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmidix_piano.cc
129 lines (107 loc) · 3.53 KB
/
xmidix_piano.cc
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
#include "xmidix_piano.h"
#include <spdlog/spdlog.h>
#include <map>
#include <QPainter>
#include <QVBoxLayout>
#define TRIGGER_COLOR Qt::red
#define WHITE_COLOR Qt::white
#define BLACK_COLOR Qt::black
#define OUTLINE_COLOR Qt::black
bool is_white_key(int idx) {
switch (idx % 12) {
case 1:
case 3:
case 6:
case 8:
case 10:
return false;
default:
return true;
}
}
xmidix_piano::xmidix_piano(QWidget *parent, std::shared_ptr<QImage> grad)
: QWidget{parent}, gradient{grad} {
setMinimumHeight(30);
setMinimumWidth(128 * 6);
// setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
}
void xmidix_piano::paintEvent(QPaintEvent *event) {
QPainter painter{this};
painter.setPen(OUTLINE_COLOR);
double white_width = (width() - 1) / 75.0;
double black_width = white_width * 0.60;
double black_adjust = black_width / 2.0;
double x = 0;
std::map<uint8_t, double> black_offsets;
// draw white keys
for (int i = 0; i < 128; i++) {
auto velocity = key_state[i];
auto color = velocity == 0 ? WHITE_COLOR : gradient->pixelColor(velocity, 0);
if (is_white_key(i)) {
QRectF r(x, 0, white_width, height() - 1);
// painter.fillRect(r, on ? TRIGGER_COLOR : WHITE_COLOR);
painter.fillRect(r, color);
painter.drawRect(r);
x += white_width;
} else {
// save x position for next loop
black_offsets[i] = x - black_adjust;
}
}
// draw black keys using offsets
for (const auto&[i, offset] : black_offsets) {
auto velocity = key_state[i];
auto color = velocity == 0 ? BLACK_COLOR : gradient->pixelColor(velocity, 0);
// QRectF r(offset, 0, black_width, height() / 2.0);
QRectF r(offset, 0, black_width, height() * 0.6);
// painter.fillRect(r, on ? TRIGGER_COLOR : BLACK_COLOR);
painter.fillRect(r, color);
painter.drawRect(r);
}
}
xmidix_piano_array::xmidix_piano_array(QWidget *parent)
: QDialog{parent} {
setAttribute(Qt::WA_X11NetWmWindowTypeUtility);
setWindowTitle("XMIDIXPIANOX");
// setFixedHeight(518);
// setFixedWidth(600);
// setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
setLayout(new QVBoxLayout(this));
QLinearGradient grad(QPoint(0, 0), QPoint(127, 0));
grad.setColorAt(1, Qt::red);
grad.setColorAt(0.75, Qt::yellow);
grad.setColorAt(0, Qt::green);
auto img = std::make_shared<QImage>(128, 1, QImage::Format_RGB32);
QPainter painter{img.get()};
painter.fillRect(img->rect(), QBrush(grad));
painter.end();
for (int i = 0; i < 16; i++) {
auto piano = std::make_shared<xmidix_piano>(this, img);
layout()->addWidget(piano.get());
pianos.emplace_back(piano);
}
updateGeometry();
}
void xmidix_piano_array::midi_event(const snd_seq_event_t &event) {
auto type = event.type;
if (type == SND_SEQ_EVENT_NOTEOFF || type == SND_SEQ_EVENT_NOTEON || type == SND_SEQ_EVENT_KEYPRESS) {
auto data = event.data.note;
auto chan = data.channel;
if (chan < 0 || chan >= 16) {
spdlog::warn("got invalid channel = {}", chan);
return;
}
uint8_t velocity = type != SND_SEQ_EVENT_NOTEOFF ? data.velocity : 0;
pianos[data.channel]->set_key(data.note, velocity);
}
}
void xmidix_piano_array::hideEvent(QHideEvent *event) {
QWidget::hideEvent(event);
position = pos();
}
void xmidix_piano_array::showEvent(QShowEvent *event) {
QDialog::showEvent(event);
move(position);
}