README update
[virtualtones] / instrument.cpp
1 // instrument.cpp - An instrument widget
2 // Written by James Bunton <james@delx.net.au>
3 // Licensed under the GPL, see COPYING.txt for more details
4
5
6 #include "instrument.h"
7
8 using namespace Qt;
9
10
11 Instrument::Instrument(QWidget *parent)
12 : QWidget(parent, 0)
13 {
14 // Grab focus
15 // grabKeyboard();
16 setFocusPolicy(StrongFocus);
17 setFocus();
18
19 noteStart = 0;
20
21 // Initialise the base midi notes array
22 midnotes[0] = QString("C");
23 midnotes[1] = QString("C#");
24 midnotes[2] = QString("D");
25 midnotes[3] = QString("D#");
26 midnotes[4] = QString("E");
27 midnotes[5] = QString("F");
28 midnotes[6] = QString("F#");
29 midnotes[7] = QString("G");
30 midnotes[8] = QString("G#");
31 midnotes[9] = QString("A");
32 midnotes[10] = QString("A#");
33 midnotes[11] = QString("B");
34 }
35
36 Instrument::~Instrument()
37 {
38 releaseKeyboard();
39 }
40
41 void Instrument::displayHelp()
42 {
43 QMessageBox::information(this, "Help", generateHelp());
44 }
45
46 bool Instrument::setNoteStart(int note)
47 {
48 if(note % 12 == 0 && noteStart >= 0 && noteStart <= 127) {
49 noteStart = note;
50 return true;
51 }
52 else {
53 // printf("noteStart=%d is invalid\n", note);
54 return false;
55 }
56 }
57
58 int Instrument::getNoteStart()
59 {
60 return noteStart;
61 }
62
63 void Instrument::setStartOctave(int octave)
64 {
65 if(setNoteStart((octave) * 12) == false) {
66 QMessageBox::warning(parentWidget(), "Error", "Could not set octave. This shouldn't happen!");
67 }
68 }
69
70 void Instrument::focusOutEvent(QFocusEvent *)
71 {
72 setFocus();
73 }
74
75 bool Instrument::event(QEvent *e)
76 {
77 if(e->type() == QEvent::KeyPress) {
78 QKeyEvent *k = (QKeyEvent *)e;
79 keyPressEvent(k);
80 if(k->isAccepted() == true) {
81 return true;
82 }
83 }
84 else if(e->type() == QEvent::KeyRelease) {
85 QKeyEvent *k = (QKeyEvent *)e;
86 keyReleaseEvent(k);
87 if(k->isAccepted() == true) {
88 return true;
89 }
90 }
91
92 return QWidget::event(e);
93 }
94
95 QString Instrument::midi2string(int num)
96 {
97 int octave;
98 for(octave = 0; num >= 12; octave++) {
99 num -= 12;
100 }
101 return QString(midnotes[num] + " - Octave: " + QString::number(octave - 2)); // Middle C is then "C - Octave: 3"
102 }
103
104 bool Instrument::checkSharp(int note)
105 {
106 // This misnamed function checks to see if we are of the the following notes. Useful for string instruments
107 // These are the notes with only a half-tone gap between them
108 //1 13 25 37 49 61 73 85 97 109 121
109 //6 18 30 42 54 66 78 90 102 114 126
110 if((note - 1) % 12 == 0 || note - 1 == 0)
111 return true;
112 if((note - 6) % 12 == 0 || note - 6 == 0)
113 return true;
114
115 return false;
116 }