Fixed some things in sound classes

This commit is contained in:
Ziver Koc 2007-03-15 23:29:55 +00:00
parent a757d93f59
commit 1bb6e9e427
3 changed files with 25 additions and 17 deletions

View file

@ -32,12 +32,16 @@ public class Sound extends Entity{
/** /**
* Play this sound as a sound effect * Play this sound as a sound effect
*
* @param pitch The pitch of the play back
* @param gain The gain of the play back
*/ */
public void play(float pitch, float gain) { public void play() {
SoundLoader.getInstnace().playSound(buffer, pitch, gain, getLocation()); SoundLoader.getInstnace().playSound(buffer, getLocation(),false);
}
/**
* Loop this sound
*/
public void loop() {
SoundLoader.getInstnace().playSound(buffer, getLocation(),true);
} }
public void update() { public void update() {

View file

@ -94,7 +94,7 @@ public class SoundLoader {
* @param pitch The pitch to play at * @param pitch The pitch to play at
* @param gain The gain to play at * @param gain The gain to play at
*/ */
void playSound(int buffer,float pitch,float gain, Vector2f pos) { void playSound(int buffer, Vector2f pos, boolean loop) {
if (soundsEnabled) { if (soundsEnabled) {
nextSource++; nextSource++;
if (nextSource >= sourceCount) { if (nextSource >= sourceCount) {
@ -103,14 +103,13 @@ public class SoundLoader {
AL10.alSourceStop(sources.get(nextSource)); AL10.alSourceStop(sources.get(nextSource));
AL10.alSourcei(sources.get(nextSource), AL10.AL_BUFFER, buffer); AL10.alSourcei(sources.get(nextSource), AL10.AL_BUFFER, buffer);
AL10.alSourcef(sources.get(nextSource), AL10.AL_PITCH, pitch); AL10.alSourcef(sources.get(nextSource), AL10.AL_PITCH, 1.0f);
AL10.alSourcef(sources.get(nextSource), AL10.AL_GAIN, gain); AL10.alSourcef(sources.get(nextSource), AL10.AL_GAIN, 1.0f);
//specify where the sound is comming from by tree axes x,y and z //specify where the sound is comming from by tree axes x,y and z
AL10.alSource3f(sources.get(nextSource), AL10.AL_POSITION, AL10.alSource3f(sources.get(nextSource), AL10.AL_POSITION,
pos.getX(), pos.getY(), 0); pos.getX(), pos.getY(), 0);
//AL10.alSource(sources.get(nextSource), AL10.AL_VELOCITY, sourceVel); AL10.alSourcei(sources.get(nextSource), AL10.AL_LOOPING, (loop ? AL10.AL_TRUE : AL10.AL_FALSE) );
//AL10.alSourcei(sources.get(nextSource), AL10.AL_LOOPING, AL10.AL_TRUE );
AL10.alSourcePlay(sources.get(nextSource)); AL10.alSourcePlay(sources.get(nextSource));
} }
@ -139,13 +138,15 @@ public class SoundLoader {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(ref); InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(ref);
String extension = getFileExtension(ref); String extension = getFileExtension(ref);
AL10.alGenBuffers(buf); AL10.alGenBuffers(buf);
if(extension.equals("ogg")){ if(extension.equals("ogg")){
//loading ogg format
OggDecoder decoder = new OggDecoder(); OggDecoder decoder = new OggDecoder();
OggData ogg = decoder.getData(in); OggData ogg = decoder.getData(in);
AL10.alBufferData(buf.get(0), ogg.channels > 1 ? AL10.AL_FORMAT_STEREO16 : AL10.AL_FORMAT_MONO16, ogg.data, ogg.rate); AL10.alBufferData(buf.get(0), ogg.channels > 1 ? AL10.AL_FORMAT_STEREO16 : AL10.AL_FORMAT_MONO16, ogg.data, ogg.rate);
} }
else if(extension.equals("wav")){ else if(extension.equals("wav")){
// //loading wav format
WaveData wav = WaveData.create(in); WaveData wav = WaveData.create(in);
AL10.alBufferData(buf.get(0), wav.format, wav.data, wav.samplerate); AL10.alBufferData(buf.get(0), wav.format, wav.data, wav.samplerate);
wav.dispose(); wav.dispose();
@ -160,11 +161,9 @@ public class SoundLoader {
buffer = buf.get(0); buffer = buf.get(0);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Sys.alert("Error","Failed to load: "+ref+" - "+e.getMessage());
MultiPrintStream.out.println("Failed to load: "+ref+" - "+e.getMessage()); MultiPrintStream.out.println("Failed to load: "+ref+" - "+e.getMessage());
soundsEnabled = false; soundsEnabled = false;
return -1; return -1;
//System.exit(0);
} }
} }
@ -175,11 +174,16 @@ public class SoundLoader {
return buffer; return buffer;
} }
/**
* Returns the file extension
* @param file The path to the file
* @return The file extension
*/
private String getFileExtension(String file){ private String getFileExtension(String file){
int dotPlace = file.lastIndexOf ( '.' ); int dot = file.lastIndexOf ( '.' );
if ( dotPlace >= 0 ){ if ( dot >= 0 ){
return file.substring(dotPlace + 1, file.length()); return file.substring(dot + 1, file.length());
} }
else{ else{
return ""; return "";

View file

@ -17,7 +17,7 @@ public class InGameState extends GameState{
sprite1 = new Sprite("tank","data/units/tank.png"); sprite1 = new Sprite("tank","data/units/tank.png");
rootNode.add(sprite1); rootNode.add(sprite1);
sound1 = new Sound("sound","data/sounds/test.wav"); sound1 = new Sound("sound","data/sounds/test.wav");
sound1.play(1, 1); sound1.play();
rootNode.add(sound1); rootNode.add(sound1);
} }