// stringinstrument.cpp - A stringed instrument simulator
-// Written by James Bunton <james@delx.cjb.net>
+// Written by James Bunton <james@delx.net.au>
// Licensed under the GPL, see COPYING.txt for more details
#include "stringinstrument.h"
+using namespace Qt;
+
// Images from http://www.asinari.it/bassoeng.htm
ViolinInstrument::ViolinInstrument(QWidget *parent)
{
noteStart = 48;
- // Set us up to look pretty
- setPaletteBackgroundPixmap(QPixmap("violin.png"));
- setFixedSize(637, 384);
- parentWidget()->setFixedSize(637,384);
+ background.load("violin.png");
setNotes(stringnote);
}
{
noteStart = 48;
- // Set us up to look pretty
- setPaletteBackgroundPixmap(QPixmap("viola.png"));
- setFixedSize(638, 392);
- parentWidget()->setFixedSize(638,392);
+ background.load("viola.png");
setNotes(stringnote);
}
{
noteStart = 36;
- // Set us up to look pretty
- setPaletteBackgroundPixmap(QPixmap("cello.png"));
- setFixedSize(639, 391);
- parentWidget()->setFixedSize(639,391);
+ background.load("cello.png");
setNotes(stringnote);
}
{
noteStart = 24;
- // Set us up to look pretty
- setPaletteBackgroundPixmap(QPixmap("contrabass.png"));
- setFixedSize(638, 388);
- parentWidget()->setFixedSize(637,384);
+ background.load("contrabass.png");
setNotes(stringnote);
}
: Instrument(parent)
{
// Set all the keys to false
- zeroArray(down);
- zeroArray(downFudge);
- zeroArray(bow);
- zeroArray(oldVolume);
- zeroArray(oldNote);
- zeroArray(note);
- zeroArray(volume);
-
- emitSounds();
+ memset(down, 0, sizeof(down));
+ memset(downFudge, 0, sizeof(downFudge));
+ memset(bow, 0, sizeof(bow));
+ memset(oldVolume, 0, sizeof(oldVolume));
+ memset(oldNote, 0, sizeof(oldNote));
+ memset(note, 0, sizeof(note));
+ memset(volume, 0, sizeof(volume));
+
+ QTimer::singleShot(50, this, SLOT(emitSounds()));
}
StringInstrument::~StringInstrument()
"<li>The row 'q' 'w' 'e' 'r' is the A string</li>"
"<li>The row 'a' 's' 'd' 'f' is the D string</li>"
"<li>The row 'z' 'x' 'c' 'v' is the G string</li>"
-"<li>The further right the key is, the higher the note</li>"
+"<li>The further to the right the key is, the higher the note</li>"
"<li>Try pressing multiple keys on the same string to get sharp notes. On the A,D,G strings try the Tab, Caps and Shift keys for sharp notes</li>"
"</ul>"
void StringInstrument::paintEvent(QPaintEvent *) {
QPainter paint(this);
+
+ paint.drawPixmap(0, 0, background);
+
paint.setPen(Qt::black);
for(int i = 0; i < 4; i++) {
QString text;
text += "Playing note: ";
- text += midi2string(note[i]);
+ text += midi2string(noteStart + note[i]);
text += " - Volume: " + QString::number(volume[i] / 30);
paint.drawText(45, (i+1)*20, text);
}
break;
case Key_F12:
- zeroArray(down);
- zeroArray(bow);
- break;
+ memset(down, 0, sizeof(down));
+ memset(bow, 0, sizeof(bow));
+ break;
default:
e->ignore();
return;
}
e->accept();
- emitSounds();
+ QTimer::singleShot(50, this, SLOT(emitSounds()));
}
void StringInstrument::keyReleaseEvent(QKeyEvent *e)
break;
case Key_F12:
- zeroArray(down);
- zeroArray(bow);
- break;
+ memset(down, 0, sizeof(down));
+ memset(bow, 0, sizeof(bow));
+ break;
default:
e->ignore();
return;
}
e->accept();
- emitSounds();
-}
-
-void StringInstrument::zeroArray(bool array[4][4])
-{
- for(int i = 0; i < 4; i++) {
- for(int j = 0; j < 4; j++) {
- array[i][j] = false;
- }
- }
-}
-
-void StringInstrument::zeroArray(bool array[4])
-{
- for(int i = 0; i < 4; i++) {
- array[i] = false;
- }
-
-}
-
-void StringInstrument::zeroArray(int array[4])
-{
- for(int i = 0; i < 4; i++) {
- array[i] = 0;
- }
-}
-
-void StringInstrument::copyArray(int source[4], int dest[4])
-{
- for(int i = 0; i < 4; i++) {
- dest[i] = source[i];
- }
+ QTimer::singleShot(50, this, SLOT(emitSounds()));
}
void StringInstrument::emitSounds()
{
- copyArray(volume, oldVolume);
- copyArray(note, oldNote);
+ memcpy(oldVolume, volume, sizeof(oldVolume));
+ memcpy(oldNote, note, sizeof(oldNote));
- zeroArray(volume);
- copyArray(stringnote, note); // Fudge for setNotes(note);
+ memset(volume, 0, sizeof(volume));
+ memcpy(note, stringnote, sizeof(note)); // Fudge for setNotes(note);
// Need to find the differences in oldVolume, volume and oldNote, note
// Then stop changed notes, and start new ones