]>
code.delx.au - virtualtones/blob - pianoinstrument.cpp
1 // pianoinstrument.cpp - A piano simulator
2 // Written by James Bunton <james@delx.net.au>
3 // Licensed under the GPL, see COPYING.txt for more details
6 #include "pianoinstrument.h"
11 PianoInstrument::PianoInstrument(QWidget
*parent
)
14 background
.load("piano.png");
16 memset(oldNotes
, 0, sizeof(oldNotes
));
17 memset(notes
, 0, sizeof(notes
));
24 PianoInstrument::~PianoInstrument()
29 QString
PianoInstrument::generateHelp()
36 "Playing the keyboard:"
38 "<li>You can change the octave using the "Base Octave" box above. Middle C is the third octave</li>"
39 "<li>The keys - qwertyui are the top row white keys</li>"
40 "<li>The keys - 23 567 9 are the top row black keys</li>"
41 "<li>The keys - zxcvbnm, are the bottom row white keys</li>"
42 "<li>The keys - sd ghj l are the bottom row black keys</li>"
43 "<li>When you push a key, the note it corresponds to is highlighted</li>"
52 void PianoInstrument::paintEvent(QPaintEvent
*)
56 paint
.drawPixmap(0, 0, background
);
58 paint
.setPen(Qt::red
);
60 const int topBlackY
= 38;
61 const int topWhiteY
= 70;
62 const int botBlackY
= 38 + 110;
63 const int botWhiteY
= 70 + 110;
67 if(notes
[0] == true) {
68 paint
.drawEllipse(6, topWhiteY
, w
, h
);
70 if(notes
[1] == true) {
71 paint
.drawEllipse(15, topBlackY
, w
, h
);
73 if(notes
[2] == true) {
74 paint
.drawEllipse(29, topWhiteY
, w
, h
);
76 if(notes
[3] == true) {
77 paint
.drawEllipse(45, topBlackY
, w
, h
);
79 if(notes
[4] == true) {
80 paint
.drawEllipse(52, topWhiteY
, w
, h
);
82 if(notes
[5] == true) {
83 paint
.drawEllipse(75, topWhiteY
, w
, h
);
85 if(notes
[6] == true) {
86 paint
.drawEllipse(85, topBlackY
, w
, h
);
88 if(notes
[7] == true) {
89 paint
.drawEllipse(97, topWhiteY
, w
, h
);
91 if(notes
[8] == true) {
92 paint
.drawEllipse(113, topBlackY
, w
, h
);
94 if(notes
[9] == true) {
95 paint
.drawEllipse(120, topWhiteY
, w
, h
);
97 if(notes
[10] == true) {
98 paint
.drawEllipse(136, topBlackY
, w
, h
);
100 if(notes
[11] == true) {
101 paint
.drawEllipse(143, topWhiteY
, w
, h
);
103 if(notes
[12] == true) {
104 paint
.drawEllipse(166, topWhiteY
, w
, h
);
105 paint
.drawEllipse(6, botWhiteY
, w
, h
);
107 if(notes
[13] == true) {
108 paint
.drawEllipse(176, topBlackY
, w
, h
);
109 paint
.drawEllipse(15, botBlackY
, w
, h
);
111 if(notes
[14] == true) {
112 paint
.drawEllipse(29, botWhiteY
, w
, h
);
114 if(notes
[15] == true) {
115 paint
.drawEllipse(45, botBlackY
, w
, h
);
117 if(notes
[16] == true) {
118 paint
.drawEllipse(52, botWhiteY
, w
, h
);
120 if(notes
[17] == true) {
121 paint
.drawEllipse(75, botWhiteY
, w
, h
);
123 if(notes
[18] == true) {
124 paint
.drawEllipse(85, botBlackY
, w
, h
);
126 if(notes
[19] == true) {
127 paint
.drawEllipse(97, botWhiteY
, w
, h
);
129 if(notes
[20] == true) {
130 paint
.drawEllipse(113, botBlackY
, w
, h
);
132 if(notes
[21] == true) {
133 paint
.drawEllipse(120, botWhiteY
, w
, h
);
135 if(notes
[22] == true) {
136 paint
.drawEllipse(136, botBlackY
, w
, h
);
138 if(notes
[23] == true) {
139 paint
.drawEllipse(143, botWhiteY
, w
, h
);
141 if(notes
[24] == true) {
142 paint
.drawEllipse(166, botWhiteY
, w
, h
);
144 if(notes
[25] == true) {
145 paint
.drawEllipse(176, botBlackY
, w
, h
);
151 void PianoInstrument::keyPressEvent(QKeyEvent
*e
)
153 if(e
->isAutoRepeat() == true) {
158 // Make a copy of the old notes so we know what's changed
159 copyArray(notes
, oldNotes
);
163 /* First row of keys */
207 /* Second row of keys */
258 void PianoInstrument::keyReleaseEvent(QKeyEvent
*e
)
260 if(e
->isAutoRepeat() == true) {
265 // Make a copy of the old notes so we know what's changed
266 copyArray(notes
, oldNotes
);
270 /* First row of keys */
314 /* Second row of keys */
365 void PianoInstrument::copyArray(bool source
[26], bool dest
[26])
367 for(int i
= 0; i
< 26; i
++) {
372 void PianoInstrument::emitSounds()
374 for(int i
= 0; i
< 26; i
++) {
375 if(notes
[i
] == oldNotes
[i
]) continue;
377 if(notes
[i
] == false)
378 emit
stopNote(i
+ noteStart
);
380 emit
playNote(i
+ noteStart
, 120, 0);