]> code.delx.au - virtualtones/blobdiff - stringinstrument.cpp
README update
[virtualtones] / stringinstrument.cpp
index b9ba3920115bb287349cd48b5e3ce6f66d967bc6..b37d8097c2ed57accd867e0614b2cc834dbc9b40 100644 (file)
@@ -1,10 +1,12 @@
 // 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)
@@ -12,10 +14,7 @@ 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);
 }
@@ -35,10 +34,7 @@ ViolaInstrument::ViolaInstrument(QWidget *parent)
 {
        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);
 }
@@ -58,10 +54,7 @@ CelloInstrument::CelloInstrument(QWidget *parent)
 {
        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);
 }
@@ -81,10 +74,7 @@ ContrabassInstrument::ContrabassInstrument(QWidget *parent)
 {
        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);
 }
@@ -128,15 +118,15 @@ StringInstrument::StringInstrument(QWidget *parent)
 : 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()
@@ -156,7 +146,7 @@ QString StringInstrument::generateHelp()
 "<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>"
 
@@ -176,12 +166,15 @@ QString StringInstrument::generateHelp()
 
 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);
        }
@@ -323,16 +316,16 @@ void StringInstrument::keyPressEvent(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();
+       QTimer::singleShot(50, this, SLOT(emitSounds()));
 }
 
 void StringInstrument::keyReleaseEvent(QKeyEvent *e)
@@ -471,56 +464,25 @@ 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