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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
/* TIATracker, (c) 2016 Andre "Kylearan" Wichmann.
* Website: https://bitbucket.org/kylearan/tiatracker
* Email: andre.wichmann@gmx.de
* See the file "license.txt" for information on usage and redistribution
* of this file.
*/
#include <QPainter>
#include <QMouseEvent>
#include <iostream>
#include "guidekeyboard.h"
#include "mainwindow.h"
// Fixed key traits (black yes/no, position index)
const GuideKeyboard::KeyGfxTrait GuideKeyboard::octaveTraits[] = {
{false, 0}, // C
{true, 1}, // Cis
{false, 1}, // D
{true, 2}, // Dis
{false, 2}, // E
{false, 3}, // F
{true, 4}, // Fis
{false, 4}, // G
{true, 5}, // Gis
{false, 5}, // A
{true, 6}, // Ais
{false, 6} // H
};
GuideKeyboard::GuideKeyboard(QWidget *parent) : QWidget(parent)
{
keyFont.setPixelSize(keyFontSize);
QFontMetrics fontMetrics(keyFont);
keyFontHeight = fontMetrics.height();
keyInfoRectHeight = keyFontHeight;
setFixedWidth(keyboardWidth+1);
setFixedHeight(keyboardHeight+1);
for (int i = 0; i < numKeys; ++i) {
keyInfo[i].isEnabled = false;
keyInfo[i].frequency = -1;
}
}
/*************************************************************************/
void GuideKeyboard::setInstrumentPitchGuide(TiaSound::InstrumentPitchGuide *pitchGuide, int threshold) {
offThreshold = threshold;
removeGuide();
for (int freq = 0; freq < pitchGuide->getNumFrequencies(); ++freq) {
TiaSound::Note note = pitchGuide->getNote(freq);
if (note != TiaSound::Note::NotANote) {
int iNote = TiaSound::getIntFromNote(note);
keyInfo[iNote].frequency = freq;
keyInfo[iNote].off = pitchGuide->getPercentOff(freq);
}
}
}
/*************************************************************************/
void GuideKeyboard::removeGuide() {
for (int i = 0; i < numKeys; ++i) {
keyInfo[i].frequency = -1;
}
}
/*************************************************************************/
void GuideKeyboard::paintEvent(QPaintEvent *) {
QPainter painter(this);
// White keys
painter.setPen(MainWindow::dark);
for (int key = 0; key < numKeys; ++key) {
if (!octaveTraits[key%numKeysPerOctave].isBlack) {
const int xPos = calcWhiteKeyXPos(key);
if (keyInfo[key].isEnabled) {
painter.fillRect(xPos, 0, keyWidth, keyHeight, MainWindow::violet);
} else {
painter.fillRect(xPos, 0, keyWidth, keyHeight, MainWindow::light);
}
painter.drawRect(xPos, 0, keyWidth, keyHeight);
}
}
// Octave number hints
painter.setFont(keyFont);
painter.setPen(MainWindow::contentDark);
keyFont.setBold(false);
for (int octave = 0; octave < numOctaves; ++octave) {
const int xPos = octave*numWhiteKeysPerOctave*keyWidth - 2;
const int yPos = keyHeight/2 - 4*keyFontHeight;
painter.drawText(xPos, yPos, keyWidth, keyFontHeight, Qt::AlignHCenter, QString::number(octave + 1));
}
keyFont.setBold(false);
painter.setFont(keyFont);
// Black keys
for (int key = 0; key < numKeys; ++key) {
if (octaveTraits[key%numKeysPerOctave].isBlack) {
const int xPos = calcBlackKeyXPos(key);
// First draw black frame, then colored interior
painter.fillRect(xPos, 0, blackKeyWidth, blackKeyHeight, MainWindow::darkHighlighted);
if (keyInfo[key].isEnabled) {
painter.fillRect(xPos + 1, 1, blackKeyWidth - 2, blackKeyHeight - 2, MainWindow::violet);
} else {
painter.fillRect(xPos + 1, 1, blackKeyWidth - 2, blackKeyHeight - 2, MainWindow::darkHighlighted);
}
}
}
// Note labels and hints
painter.setFont(keyFont);
for (int key = 0; key < numKeys; ++key) {
int xPos;
int rectWidth;
int rectHeight;
if (octaveTraits[key%numKeysPerOctave].isBlack) {
painter.setPen(MainWindow::light);
xPos = calcBlackKeyXPos(key);
rectWidth = blackKeyWidth;
rectHeight = blackKeyHeight;
} else {
painter.setPen(MainWindow::dark);
xPos = calcWhiteKeyXPos(key);
rectWidth = keyWidth;
rectHeight = keyHeight;
}
if (keyInfo[key].frequency != -1) {
if (std::abs(keyInfo[key].off) >= std::abs(offThreshold)) {
painter.setPen(MainWindow::red);
}
painter.drawText(xPos, rectHeight - 3*keyInfoRectHeight, rectWidth, keyInfoRectHeight, Qt::AlignHCenter|Qt::AlignBottom, QString::number(keyInfo[key].frequency));
painter.drawText(xPos, rectHeight - 2*keyInfoRectHeight, rectWidth, keyInfoRectHeight, Qt::AlignHCenter|Qt::AlignBottom, QString::number(keyInfo[key].off));
}
TiaSound::Note curNote = TiaSound::getNoteFromInt(key);
painter.drawText(xPos, rectHeight - 1*keyInfoRectHeight, rectWidth, keyInfoRectHeight, Qt::AlignHCenter|Qt::AlignBottom, TiaSound::getNoteName(curNote));
}
}
/*************************************************************************/
void GuideKeyboard::mousePressEvent(QMouseEvent *event) {
int octave = int(event->x()/(keyWidth*numWhiteKeysPerOctave));
int keyIndex;
if (event->y() < blackKeyHeight) {
// Potential black key
// TODO: More intelligent calculation instead of brute force
int octaveBaseKey = octave*12;
int xPos = event->x();
keyIndex = -1;
for (int key = 0; key < 12; ++key) {
if (octaveTraits[key].isBlack) {
int xPosCandidate = calcBlackKeyXPos(octaveBaseKey + key);
if (xPos >= xPosCandidate && xPos < xPosCandidate + blackKeyWidth) {
keyIndex = octaveBaseKey + key;
}
}
}
if (keyIndex == -1) {
// Click is between black keys: White key instead
keyIndex = calcKeyIndexForWhiteKey(xPos);
}
} else {
// White key
keyIndex = calcKeyIndexForWhiteKey(event->x());
}
keyInfo[keyIndex].isEnabled = !keyInfo[keyIndex].isEnabled;
update();
}
/*************************************************************************/
int GuideKeyboard::calcWhiteKeyXPos(int key) {
int keyInOctave = key%numKeysPerOctave;
int octave = int(key/numKeysPerOctave);
int octaveXPos = octave*keyWidth*numWhiteKeysPerOctave;
return octaveXPos + octaveTraits[keyInOctave].posIndex*keyWidth;
}
/*************************************************************************/
int GuideKeyboard::calcBlackKeyXPos(int key) {
int keyInOctave = key%numKeysPerOctave;
int octave = int(key/numKeysPerOctave);
int octaveXPos = octave*keyWidth*numWhiteKeysPerOctave;
return octaveXPos + octaveTraits[keyInOctave].posIndex*keyWidth - blackKeyWidth/2;
}
/*************************************************************************/
int GuideKeyboard::calcKeyIndexForWhiteKey(int xPos) {
// Maps index of a white key to overall key index including black keys,
// which for example can be used with octaveTraits[].
static const int whiteKeyIndexToKeyIndex[] = {0, 2, 4, 5, 7, 9, 11};
int octave = int(xPos/(keyWidth*numWhiteKeysPerOctave));
int xPosInOctave = xPos%(keyWidth*numWhiteKeysPerOctave);
int whiteKeyIndex = int(xPosInOctave/keyWidth);
int keyIndex = octave*12 + whiteKeyIndexToKeyIndex[whiteKeyIndex];
return keyIndex;
}
|