/**********
 * LegoMusic has methods for playing musical tunes
 * on the RCX Lego brick.
 *
 * @author Jim Mahoney
 * @version 1.0, October 13, 2002
 *********/
import josx.platform.rcx.*;

class LegoMusic {

    // Frequencies of notes and sharped notes, in Hz.
    final public static int C1 =  131;
    final public static int Cs1 = 139;
    final public static int D1 =  147;
    final public static int Ds1 = 156;
    final public static int E1 =  165;
    final public static int F1 =  175;
    final public static int Fs1 = 185;
    final public static int G1 =  196;
    final public static int Gs1 = 208;
    final public static int A1 =  220;
    final public static int As1 = 233;
    final public static int B1 =  247;
    final public static int C2 =  262;
    final public static int Cs2 = 278;
    final public static int D2 =  294;
    final public static int Ds2 = 311;
    final public static int E2 =  330;
    final public static int F2 =  349;
    final public static int Fs2 = 370;
    final public static int G2 =  392;
    final public static int Gs2 = 415;
    final public static int A2 =  440;
    final public static int As2 = 466;
    final public static int B2 =  494;
    final public static int C3 =  523;
    final public static int Cs3 = 554;
    final public static int D3 =  587;
    final public static int Ds3 = 622;
    final public static int E3 =  659;
    final public static int F3 =  698;
    final public static int Fs3 = 740;
    final public static int G3 =  784;
    final public static int Gs3 = 831;
    final public static int A3 =  880;
    final public static int As3 = 932;
    final public static int B3 =  989;
    final public static int C4 =  1046;
    final public static int Cs4 = 1109;
    final public static int D4 =  1175;
    final public static int Ds4 = 1245;
    final public static int E4 =  1318;
    final public static int F4 =  1397;
    final public static int Fs4 = 1480;
    final public static int G4 =  1568;
    final public static int Gs4 = 1661;
    final public static int A4 =  1760;
    final public static int As4 = 1865;
    final public static int B4 =  1976;
    final public static int C5 =  2093;

    final public static int bugle1 = A2;
    final public static int bugle2 = D3;
    final public static int bugle3 = Fs3;
    final public static int bugle4 = A3;

    final private static int NOTE_PAUSE = 2; // centi-seconds between notes.
    private static double beatTime;	// time for one beat, in centi-seconds.

    public static void setTempo(double theTempo){  // tempo in beats per minute
	beatTime = 100.0 * 60.0/theTempo;
    }
    
    public static void play(int whichNote, int beats){
	play(whichNote,(double)beats);
    }
    public static void play(int whichNote, double beats){
	int timeInCents = (int)(beats*beatTime);
	Sound.playTone(whichNote, timeInCents - NOTE_PAUSE );
	waitCents(timeInCents);
    }

    public static void waitCents(int cents){
	try {
	    Thread.sleep( (long)(10*cents) );
	}
	catch (InterruptedException e1){
	}
    }

    public static void playSoundTheCharge(){
	setTempo(3*120.0);
	play(bugle1, 1);
	play(bugle2, 1);
	play(bugle3, 1);
	play(bugle4, 2);
	play(bugle3, 1);
	play(bugle4, 3);
    }

    public static void playStartTheRace(){
	setTempo(3*100.0);
	play(bugle1, 1);
	play(bugle2, 1);
	play(bugle3, 1);
	play(bugle4, 1);
	play(bugle4, 1./3.);
	play(bugle4, 1./3.);
	play(bugle4, 1./3.);
	play(bugle3, 1);
	play(bugle3, 1./3.);
	play(bugle3, 1./3.);
	play(bugle3, 1./3.);
	play(bugle2, 1);
	play(bugle3, 1);
	play(bugle2, 1);
	play(bugle1, 3);
    }


}


syntax highlighted by Code2HTML, v. 0.9.1