]> code.delx.au - virtualtones/blob - mainwin.cpp
Remove unused include
[virtualtones] / mainwin.cpp
1 // mainwin.cpp - Displays the MIDI keyboard and instrument selector
2 // Written by James Bunton <james@delx.net.au>
3 // Licensed under the GPL, see COPYING.txt for more details
4
5
6 #include "mainwin.h"
7
8 using namespace Qt;
9
10
11 MainWin::MainWin()
12 : QWidget(0,0)
13 {
14 setWindowTitle("Virtual Tones");
15
16 // Setup the MIDI output
17 midi = new MidiReal();
18 if(midi->initSuccess() == false) {
19 QMessageBox::critical(this, "MIDI Error", midi->getError());
20 qApp->quit();
21 exit(1);
22 }
23 midiFile = 0;
24
25 // Create the sound selector
26 soundSelection = new QComboBox(this);
27 soundSelection->setToolTip("Select the MIDI instrument to play with");
28 fillSounds();
29 connect(soundSelection, SIGNAL(activated(int)), midi, SLOT(setInstrument(int)));
30 soundSelectionLabel = new QLabel("Sound:", this);
31
32 // Setup the interface selector
33 interfaceSelection = new QComboBox(this);
34 interfaceSelection->setToolTip("Select the interface to play with");
35 interfaceSelection->insertItem(0, "Piano");
36 interfaceSelection->insertItem(1, "Violin");
37 interfaceSelection->insertItem(2, "Viola");
38 interfaceSelection->insertItem(3, "Cello");
39 interfaceSelection->insertItem(4, "Contrabass");
40 connect(interfaceSelection, SIGNAL(activated(int)), this, SLOT(interfaceSelectionSlot(int)));
41 instrument = 0;
42 interfaceSelectionLabel = new QLabel("Interface:", this);
43
44 // Setup the octave selector
45 octaveSelection = new QComboBox(this);
46 octaveSelection->setToolTip("Select the starting octave");
47 for(int i = 0; i < 11; i++) {
48 octaveSelection->insertItem(i, "Octave " + QString::number(i - 2));
49 }
50 octaveSelectionLabel = new QLabel("Base Octave:", this);
51
52 // Create the help & quit button
53 helpBtn = new QPushButton("Help", this);
54 quitBtn = new QPushButton("Quit", this);
55 connect(quitBtn, SIGNAL(clicked()), qApp, SLOT(quit()));
56
57 // MIDI recording buttons
58 recordBtn = new QPushButton("Record", this);
59 recordBtn->setToolTip("Starts a MIDI recording of the notes played");
60 recordBtn->setEnabled(true);
61 connect(recordBtn, SIGNAL(clicked()), SLOT(setupRecord()));
62 stopBtn = new QPushButton("Stop", this);
63 stopBtn->setToolTip("Stops the MIDI recording");
64 stopBtn->setEnabled(false);
65 connect(stopBtn, SIGNAL(clicked()), SLOT(finishRecord()));
66
67
68 // The about label
69 aboutLabel = new QLabel("Written for Julieanne...", this);
70
71 // Setup the layouts
72 soundSelectionLayout = new QVBoxLayout();
73 soundSelectionLayout->addWidget(soundSelectionLabel);
74 soundSelectionLayout->addWidget(soundSelection);
75
76 interfaceSelectionLayout = new QVBoxLayout();
77 interfaceSelectionLayout->addWidget(interfaceSelectionLabel);
78 interfaceSelectionLayout->addWidget(interfaceSelection);
79
80 octaveSelectionLayout = new QVBoxLayout();
81 octaveSelectionLayout->addWidget(octaveSelectionLabel);
82 octaveSelectionLayout->addWidget(octaveSelection);
83
84 midiBtnLayout = new QVBoxLayout();
85 midiBtnLayout->addWidget(recordBtn);
86 midiBtnLayout->addSpacing(5);
87 midiBtnLayout->addWidget(stopBtn);
88
89 buttonsLayout = new QVBoxLayout();
90 buttonsLayout->addWidget(helpBtn);
91 buttonsLayout->addSpacing(5);
92 buttonsLayout->addWidget(quitBtn);
93
94 optionsLayout = new QHBoxLayout();
95 optionsLayout->addSpacing(5);
96 optionsLayout->addLayout(interfaceSelectionLayout);
97 optionsLayout->addSpacing(5);
98 optionsLayout->addLayout(soundSelectionLayout);
99 optionsLayout->addSpacing(5);
100 optionsLayout->addLayout(octaveSelectionLayout);
101 optionsLayout->addSpacing(5);
102 optionsLayout->addLayout(midiBtnLayout);
103 optionsLayout->addSpacing(5);
104 optionsLayout->addLayout(buttonsLayout);
105 optionsLayout->addSpacing(5);
106
107 vLayout = new QVBoxLayout(this);
108 vLayout->insertSpacing(0, 5);
109 vLayout->insertLayout(1, optionsLayout);
110 vLayout->insertSpacing(2, 5);
111 vLayout->insertSpacing(3, 1);// instrument goes here
112 vLayout->insertSpacing(4, 5);
113 vLayout->insertWidget(5, aboutLabel);
114 vLayout->insertSpacing(6, 5);
115
116 interfaceSelectionSlot(0);
117
118 show();
119 }
120
121 MainWin::~MainWin()
122 {
123 delete midi;
124 if(midiFile != 0)
125 delete midiFile;
126 }
127
128 void MainWin::setupRecord() {
129 if(midiFile == 0) {
130 QString filename = QFileDialog::getSaveFileName(NULL, "MIDI files (*.mid *.midi)");
131 if(filename.isNull() || filename.isEmpty())
132 return;
133 midiFile = new MidiFile(filename);
134 if(midiFile->initSuccess() == false) {
135 QMessageBox::warning(this, "MIDI recording error", midiFile->getError());
136 delete midiFile;
137 return;
138 }
139 connect(soundSelection, SIGNAL(activated(int)), midiFile, SLOT(setInstrument(int)));
140 connect(instrument, SIGNAL(playNote(int, int, int)), midiFile, SLOT(playNote(int, int, int)));
141 connect(instrument, SIGNAL(stopNote(int)), midiFile, SLOT(stopNote(int)));
142 }
143
144 recordBtn->setEnabled(false);
145 stopBtn->setEnabled(true);
146 }
147
148 void MainWin::finishRecord() {
149 if(midiFile != 0) {
150 delete midiFile;
151 midiFile = 0;
152 }
153
154 recordBtn->setEnabled(true);
155 stopBtn->setEnabled(false);
156 }
157
158 void MainWin::interfaceSelectionSlot(int num)
159 {
160 // Right now interfaces 0-4 exist
161
162 if(instrument != 0) {
163 // if(vLayout->findWidget(instrument)) {
164 // vLayout->remove(instrument);
165 // }
166 delete instrument;
167 instrument = 0;
168 }
169
170 switch(num) {
171 // Create the instrument
172
173 default:
174 case 0: {
175 PianoInstrument *p = new PianoInstrument(this);
176 instrument = p;
177 // Select piano as the default
178 soundSelection->setCurrentIndex(0);
179 midi->setInstrument(0);
180 if(midiFile != 0)
181 midiFile->setInstrument(0);
182 break;
183 }
184 case 1: {
185 ViolinInstrument *p = new ViolinInstrument(this);
186 instrument = p;
187 // Select violin as the default
188 soundSelection->setCurrentIndex(40);
189 midi->setInstrument(40);
190 if(midiFile != 0)
191 midiFile->setInstrument(40);
192 break;
193 }
194 case 2: {
195 ViolaInstrument *p = new ViolaInstrument(this);
196 instrument = p;
197 // Select violin as the default
198 soundSelection->setCurrentIndex(41);
199 midi->setInstrument(41);
200 if(midiFile != 0)
201 midiFile->setInstrument(41);
202 break;
203 }
204 case 3: {
205 CelloInstrument *p = new CelloInstrument(this);
206 instrument = p;
207 // Select violin as the default
208 soundSelection->setCurrentIndex(42);
209 midi->setInstrument(42);
210 if(midiFile != 0)
211 midiFile->setInstrument(42);
212 break;
213 }
214 case 4: {
215 ContrabassInstrument *p = new ContrabassInstrument(this);
216 instrument = p;
217 // Select violin as the default
218 soundSelection->setCurrentIndex(43);
219 midi->setInstrument(43);
220 if(midiFile != 0)
221 midiFile->setInstrument(43);
222 break;
223 }
224 }
225
226 // Relayout
227 vLayout->insertWidget(3, instrument);
228 setFixedSize(640, 640);
229 instrument->show();
230
231 // Connect signals
232 connect(instrument, SIGNAL(playNote(int, int, int)), midi, SLOT(playNote(int, int, int)));
233 connect(instrument, SIGNAL(stopNote(int)), midi, SLOT(stopNote(int)));
234 if(midiFile != 0) {
235 connect(instrument, SIGNAL(playNote(int, int, int)), midiFile, SLOT(playNote(int, int, int)));
236 connect(instrument, SIGNAL(stopNote(int)), midiFile, SLOT(stopNote(int)));
237 }
238 connect(helpBtn, SIGNAL(clicked()), instrument, SLOT(displayHelp()));
239 connect(octaveSelection, SIGNAL(activated(int)), instrument, SLOT(setStartOctave(int)));
240
241 // Set the octave widget
242 octaveSelection->setCurrentIndex(instrument->getNoteStart() / 12);
243 }
244
245 void MainWin::fillSounds()
246 {
247 soundSelection->insertItem(0, "Acoustic Grand Piano");
248 soundSelection->insertItem(1, "Bright Acoustic Piano");
249 soundSelection->insertItem(2, "Electric Grand Piano");
250 soundSelection->insertItem(3, "Honky-tonk Piano");
251 soundSelection->insertItem(4, "Rhodes Piano");
252 soundSelection->insertItem(5, "Chorused Piano");
253 soundSelection->insertItem(6, "Harpsichord");
254 soundSelection->insertItem(7, "Clavinet");
255 soundSelection->insertItem(8, "Celesta");
256 soundSelection->insertItem(9, "Glockenspiel");
257 soundSelection->insertItem(10, "Music Box");
258 soundSelection->insertItem(11, "Vibraphone");
259 soundSelection->insertItem(12, "Marimba");
260 soundSelection->insertItem(13, "Xylophone");
261 soundSelection->insertItem(14, "Tubular bells");
262 soundSelection->insertItem(15, "Dulcimer");
263 soundSelection->insertItem(16, "Draw Organ");
264 soundSelection->insertItem(17, "Percussive Organ");
265 soundSelection->insertItem(18, "Rock Organ");
266 soundSelection->insertItem(19, "Church Organ");
267 soundSelection->insertItem(20, "Reed Organ");
268 soundSelection->insertItem(21, "Accordion");
269 soundSelection->insertItem(22, "Harmonica");
270 soundSelection->insertItem(23, "Tango Accordion");
271 soundSelection->insertItem(24, "Acoustic Nylon Guitar");
272 soundSelection->insertItem(25, "Acoustic Steel Guitar");
273 soundSelection->insertItem(26, "Electric Jazz Guitar");
274 soundSelection->insertItem(27, "Electric clean Guitar");
275 soundSelection->insertItem(28, "Electric Guitar muted");
276 soundSelection->insertItem(29, "Overdriven Guitar");
277 soundSelection->insertItem(30, "Distortion Guitar");
278 soundSelection->insertItem(31, "Guitar Harmonics");
279 soundSelection->insertItem(32, "Wood Bass");
280 soundSelection->insertItem(33, "Electric Bass Fingered");
281 soundSelection->insertItem(34, "Electric Bass Picked");
282 soundSelection->insertItem(35, "Fretless Bass");
283 soundSelection->insertItem(36, "Slap Bass 1");
284 soundSelection->insertItem(37, "Slap Bass 2");
285 soundSelection->insertItem(38, "Synth Bass 1");
286 soundSelection->insertItem(39, "Synth Bass 2");
287 soundSelection->insertItem(40, "Violin");
288 soundSelection->insertItem(41, "Viola");
289 soundSelection->insertItem(42, "Cello");
290 soundSelection->insertItem(43, "Contrabass");
291 soundSelection->insertItem(44, "Tremolo Strings");
292 soundSelection->insertItem(45, "Pizzicato Strings");
293 soundSelection->insertItem(46, "Orchestral Harp");
294 soundSelection->insertItem(47, "Timpani");
295 soundSelection->insertItem(48, "Acoustic String Ensemble 1");
296 soundSelection->insertItem(49, "Acoustic String Ensemble 2");
297 soundSelection->insertItem(50, "Synth Strings 1");
298 soundSelection->insertItem(51, "Synth Strings 2");
299 soundSelection->insertItem(52, "Aah Choir");
300 soundSelection->insertItem(53, "Ooh Choir");
301 soundSelection->insertItem(54, "Synvox");
302 soundSelection->insertItem(55, "Orchestra Hit");
303 soundSelection->insertItem(56, "Trumpet");
304 soundSelection->insertItem(57, "Trombone");
305 soundSelection->insertItem(58, "Tuba");
306 soundSelection->insertItem(59, "Muted Trumpet");
307 soundSelection->insertItem(60, "French Horn");
308 soundSelection->insertItem(61, "Brass Section");
309 soundSelection->insertItem(62, "Synth Brass 1");
310 soundSelection->insertItem(63, "Synth Brass 2");
311 soundSelection->insertItem(64, "Soprano Sax");
312 soundSelection->insertItem(65, "Alto Sax");
313 soundSelection->insertItem(66, "Tenor Sax");
314 soundSelection->insertItem(67, "Baritone Sax");
315 soundSelection->insertItem(68, "Oboe");
316 soundSelection->insertItem(69, "English Horn");
317 soundSelection->insertItem(70, "Bassoon");
318 soundSelection->insertItem(71, "Clarinet");
319 soundSelection->insertItem(72, "Piccolo");
320 soundSelection->insertItem(73, "Flute");
321 soundSelection->insertItem(74, "Recorder");
322 soundSelection->insertItem(75, "Pan Flute");
323 soundSelection->insertItem(76, "Bottle blow");
324 soundSelection->insertItem(77, "Shakuhachi");
325 soundSelection->insertItem(78, "Whistle");
326 soundSelection->insertItem(79, "Ocarina");
327 soundSelection->insertItem(80, "Square Lead");
328 soundSelection->insertItem(81, "Saw Lead");
329 soundSelection->insertItem(82, "Calliope");
330 soundSelection->insertItem(83, "Chiffer");
331 soundSelection->insertItem(84, "Synth Lead 5");
332 soundSelection->insertItem(85, "Synth Lead 6");
333 soundSelection->insertItem(86, "Synth Lead 7");
334 soundSelection->insertItem(87, "Synth Lead 8");
335 soundSelection->insertItem(88, "Synth Pad 1");
336 soundSelection->insertItem(89, "Synth Pad 2");
337 soundSelection->insertItem(90, "Synth Pad 3");
338 soundSelection->insertItem(91, "Synth Pad 4");
339 soundSelection->insertItem(92, "Synth Pad 5");
340 soundSelection->insertItem(93, "Synth Pad 6");
341 soundSelection->insertItem(94, "Synth Pad 7");
342 soundSelection->insertItem(95, "Synth Pad 8");
343 soundSelection->insertItem(96, "Ice Rain");
344 soundSelection->insertItem(97, "Soundtracks");
345 soundSelection->insertItem(98, "Crystal");
346 soundSelection->insertItem(99, "Atmosphere");
347 soundSelection->insertItem(100, "Bright");
348 soundSelection->insertItem(101, "Goblin");
349 soundSelection->insertItem(102, "Echoes");
350 soundSelection->insertItem(103, "Space");
351 soundSelection->insertItem(104, "Sitar");
352 soundSelection->insertItem(105, "Banjo");
353 soundSelection->insertItem(106, "Shamisen");
354 soundSelection->insertItem(107, "Koto");
355 soundSelection->insertItem(108, "Kalimba");
356 soundSelection->insertItem(109, "Bagpipe");
357 soundSelection->insertItem(110, "Fiddle");
358 soundSelection->insertItem(111, "Shanai");
359 soundSelection->insertItem(112, "Tinkle bell");
360 soundSelection->insertItem(113, "Agogo");
361 soundSelection->insertItem(114, "Steel Drums");
362 soundSelection->insertItem(115, "Woodblock");
363 soundSelection->insertItem(116, "Taiko Drum");
364 soundSelection->insertItem(117, "Melodic Tom");
365 soundSelection->insertItem(118, "Synth Tom");
366 soundSelection->insertItem(119, "Reverse Cymbal");
367 soundSelection->insertItem(120, "Guitar Fret Noise");
368 soundSelection->insertItem(121, "Breath Noise");
369 soundSelection->insertItem(122, "Seashore");
370 soundSelection->insertItem(123, "Bird Tweet");
371 soundSelection->insertItem(124, "Telephone Ring");
372 soundSelection->insertItem(125, "Helicopter");
373 soundSelection->insertItem(126, "Applause");
374 soundSelection->insertItem(127, "Gunshot");
375
376 }