Hey,
I'm starting to use the OpenAL wrapper which is great. However, it doesn't give any controls over the OpenAL sources. I mean, I understand it's designed to hide everything under the hood and it's nice, but in my case, I'd like to get controls over sources for controlling SFX. Actually, I'd like to have a single SFX channel per player, so that some SFX gets "overwritten" by the preceding one.
Of course, I'd like to leverage what has been done in the SoundSStore. I think changing the following would do the job:
- Changing the visibility of "isPlaying" to public,
- Changing the visibility of "stopSource" to public,
- Adding a new playAsSoundAt method with a source parameter,
- Adding a convenience method playAsSound wih the source parameter.
Here is the modified code:
Code:
/**
* Stop a particular sound source
*
* @param index The index of the source to stop
*/
public void stopSource(int index) {
AL10.alSourceStop(sources.get(index));
}
/**
* Play the specified buffer as a sound effect with the specified
* pitch and gain.
*
* @param buffer The ID of the buffer to play
* @param pitch The pitch to play at
* @param gain The gain to play at
* @param loop True if the sound should loop
* @return source The source that will be used
*/
int playAsSound(int buffer,float pitch,float gain,boolean loop) {
return playAsSoundAt(buffer, pitch, gain, loop, 0, 0, 0, 0);
}
/**
* Play the specified buffer as a sound effect with the specified
* pitch and gain.
*
* @param buffer The ID of the buffer to play
* @param pitch The pitch to play at
* @param gain The gain to play at
* @param loop True if the sound should loop
* @param source The source to use, 0 to find a free source
* @return source The source that will be used
*/
int playAsSound(int buffer,float pitch,float gain,boolean loop, int source) {
return playAsSoundAt(buffer, pitch, gain, loop, 0, 0, 0, source);
}
/**
* Play the specified buffer as a sound effect with the specified
* pitch and gain.
*
* @param buffer The ID of the buffer to play
* @param pitch The pitch to play at
* @param gain The gain to play at
* @param loop True if the sound should loop
* @param x The x position to play the sound from
* @param y The y position to play the sound from
* @param z The z position to play the sound from
* @return source The source that will be used
*/
int playAsSoundAt(int buffer,float pitch,float gain,boolean loop,float x, float y, float z) {
return playAsSoundAt(buffer, pitch, gain, loop, x, y, z, 0);
}
/**
* Play the specified buffer as a sound effect with the specified
* pitch and gain.
*
* @param buffer The ID of the buffer to play
* @param pitch The pitch to play at
* @param gain The gain to play at
* @param loop True if the sound should loop
* @param x The x position to play the sound from
* @param y The y position to play the sound from
* @param z The z position to play the sound from
* @param source The source to use, 0 to find a free source
* @return source The source that will be used
*/
int playAsSoundAt(int buffer,float pitch,float gain,boolean loop,float x, float y, float z, int source) {
gain *= soundVolume;
if (gain == 0) {
gain = 0.001f;
}
if (soundWorks) {
if (sounds) {
int nextSource = source > 0 ? sources.get(source) : findFreeSource(); // the source 0 is reserved for music
if (nextSource == -1) {
return -1;
}
AL10.alSourceStop(sources.get(nextSource));
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_GAIN, gain);
AL10.alSourcei(sources.get(nextSource), AL10.AL_LOOPING, loop ? AL10.AL_TRUE : AL10.AL_FALSE);
sourcePos.clear();
sourceVel.clear();
sourceVel.put(new float[] { 0, 0, 0 });
sourcePos.put(new float[] { x, y, z });
sourcePos.flip();
sourceVel.flip();
AL10.alSource(sources.get(nextSource), AL10.AL_POSITION, sourcePos);
AL10.alSource(sources.get(nextSource), AL10.AL_VELOCITY, sourceVel);
AL10.alSourcePlay(sources.get(nextSource));
return nextSource;
}
}
return -1;
}
/**
* Check if a particular source is playing
*
* @param index The index of the source to check
* @return True if the source is playing
*/
boolean isPlaying(int index) {
int state = AL10.alGetSourcei(sources.get(index), AL10.AL_SOURCE_STATE);
return (state == AL10.AL_PLAYING);
}
Tell me what you think of this. Do you think it's something you can add to Slick ?
Thanks,
TsuG