Wednesday, April 8, 2009

Thursday, April 9: Evan's at bat -- and man is he LOUD!

[Note from Mr. L: today we will be having the second in our series of student led programming classes. If you care to be the next volunteer, you know where to find me]





hello grand privateers - this is your BloodSail Admiral, Abquhazar!
Today we will be looking at a simple sound player. I attempted to make this in Visual Basic but I Epic failed.... and yet somehow, I got it in JAVA!




but any ways, we are going to learn how to play the violin today =D


lets break this down shall we?
we are gonna start this with the usual... please download the initial file at


http://www.box.net/shared/nf2gty9c0i





It has the startup file with three empty methods: init, keyDown and keyUp

The file is meant to be played as an Applet, btw.




now lets declare some things; we will do that just ABOVE the init( ) method:

AudioClip lowC, lowD, lowE, lowF, lowG, lowA, lowB, midC, midD, midE, midF, midG, midA, midB, highC; //all this stuff should go on one line!




int keyPressed;


I made enough sound files to play 2 octaves in C Major. Personally, C Major is my favorite scale but if you're not into music theory its all the same.


now we declare some booleans....you know, things that are either true or false. In this case, we wanna know if a particular note is playing....

boolean lowCPlaying,lowDPlaying,lowEPlaying,lowFPlaying, lowGPlaying, lowAPlaying, lowBPlaying, midCPlaying,midDPlaying, midEPlaying, midFPlaying,midGPlaying, midAPlaying, midBPlaying, highCPlaying;
//all this should should go on one line!!!



Once again, Iused 2 octaves of notes in a moajor scale.... thats 16 notes right there.




ok, so now get into our public void - once again, we have many sound files to load so lets get crackin'.


Fill this empty init method so that it looks like this:
public void init( )

{


try


{


lowC=getAudioClip(getCodeBase(),"lowc.mid");


lowD=getAudioClip(getCodeBase(),"lowd.mid");


lowE=getAudioClip(getCodeBase(),"lowE.mid");


lowF=getAudioClip(getCodeBase(),"lowF.mid");


lowG=getAudioClip(getCodeBase(),"lowG.mid");


lowA=getAudioClip(getCodeBase(),"lowA.mid");


lowB=getAudioClip(getCodeBase(),"lowB.mid");


midC=getAudioClip(getCodeBase(),"midC.mid");


midD=getAudioClip(getCodeBase(),"midD.mid");


midE=getAudioClip(getCodeBase(),"midE.mid");


midF=getAudioClip(getCodeBase(),"midF.mid");


midG=getAudioClip(getCodeBase(),"midG.mid");


midA=getAudioClip(getCodeBase(),"midA.mid");


midB=getAudioClip(getCodeBase(),"midB.mid");


highC=getAudioClip(getCodeBase(),"highC.mid");


}//end try



catch (Exception e){ } //end empty catch

} //end init





You have to use a "try catch" to init the sounds for the same reason you use it when loading a webpage: you have to have a "just in case' event in case the sounds aren't there.




So now after all the sounds are loaded, we will set up keydown commands to play the sound! We will put this in the boolean called "keyDown". Here we go...




INSIDE the public boolean keyDown method, just above the "return true" statement, add this:



if(key=='q'key=='Q')


{


if (lowCPlaying == false)


{


lowC.loop();


}


lowCPlaying = true;





This is just one sound. lets see if you can figue some of this out - I'll put the next post up in a few minutes to show the rest...






now as we might realize, there is no way to stop the sound once is has played... SO we are gonna have to make another boolean called keyUp. now im not going to tell you how to fix this problem just yet... lets have your brain go for a little jog...





































any one get it? Well here is the spoiler for all the n0obs who didnt try....

Put this in the keyUp method, just above the "return true"





if(key=='q'key=='Q')


{


lowC.stop();


lowCPlaying = false;


}





Save, compile, then run the program...when you do, you should be able to play a low C when you press the "Q" key.





The rest of the program is about adding in more if statements to both the keyUp and keyDown methods: other sounds connected with other keys. See if you can figure that out on your own....if not, then I'll give ya the complete set in another blog post

No comments:

Post a Comment