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