Widening, modernization

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@620 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Charlie Groves
2008-08-11 04:34:50 +00:00
parent d657cf9f3b
commit 7266e22085
4 changed files with 70 additions and 89 deletions
@@ -209,21 +209,19 @@ public class ClipBuffer
*/
protected boolean bind (Clip clip)
{
AL10.alBufferData(
_bufferId.get(0), clip.format, clip.data, clip.frequency);
AL10.alBufferData(_bufferId.get(0), clip.format, clip.data, clip.frequency);
int errno = AL10.alGetError();
if (errno != AL10.AL_NO_ERROR) {
log.warning("Failed to bind clip [key=" + getKey() +
", errno=" + errno + "].");
log.warning("Failed to bind clip", "key", getKey(), "errno", errno);
failed();
return false;
}
_state = LOADED;
_size = AL10.alGetBufferi(_bufferId.get(0), AL10.AL_SIZE);
_observers.apply(new ObserverList.ObserverOp() {
public boolean apply (Object observer) {
((Observer)observer).clipLoaded(ClipBuffer.this);
_observers.apply(new ObserverList.ObserverOp<Observer>() {
public boolean apply (Observer observer) {
observer.clipLoaded(ClipBuffer.this);
return true;
}
});
+53 -65
View File
@@ -27,19 +27,21 @@ import org.lwjgl.BufferUtils;
import org.lwjgl.openal.AL10;
/**
* Represents an instance of a sound clip which can be positioned in 3D
* space, gain and pitch adjusted and played or looped.
* Represents an instance of a sound clip which can be positioned in 3D space, gain and pitch
* adjusted and played or looped.
*/
public class Sound
{
/** Used to await notification of the starting of a sound which may be
* delayed in loading. */
/**
* Used to await notification of the starting of a sound which may be delayed in loading.
*/
public interface StartObserver
{
/** Called when the specified sound has started playing. If
* sound is null then the sound failed to play but soundStarted
* was called anyway to perform whatever actions were waiting
* on the sound. */
/**
* Called when the specified sound has started playing. If sound is null then the sound
* failed to play but soundStarted was called anyway to perform whatever actions were
* waiting on the sound.
*/
public void soundStarted (Sound sound);
}
@@ -52,9 +54,8 @@ public class Sound
}
/**
* Configures the location of this sound in 3D space. This will not
* affect an already playing sound but will take effect the next time
* it is played.
* Configures the location of this sound in 3D space. This will not affect an already playing
* sound but will take effect the next time it is played.
*/
public void setLocation (float x, float y, float z)
{
@@ -66,10 +67,9 @@ public class Sound
}
/**
* Configures the velocity of this sound in 3D space, in delta
* location per second (see {@link #setLocation}). This will not
* affect an already playing sound but will take effect the next time
* it is played.
* Configures the velocity of this sound in 3D space, in delta location per second (see
* {@link #setLocation}). This will not affect an already playing sound but will take effect
* the next time it is played.
*/
public void setVelocity (float dx, float dy, float dz)
{
@@ -81,9 +81,8 @@ public class Sound
}
/**
* Configures the sounds's pitch. This value can range from 0.5 to
* 2.0. This will not affect an already playing sound but will take
* effect the next time it is played.
* Configures the sounds's pitch. This value can range from 0.5 to 2.0. This will not affect
* an already playing sound but will take effect the next time it is played.
*/
public void setPitch (float pitch)
{
@@ -91,15 +90,13 @@ public class Sound
}
/**
* Configures the sound's gain (volume). This value can range from 0.0
* to 1.0 with 1.0 meaning no attenuation, each division by two
* corresponding to a -6db attentuation and each multiplication by two
* corresponding to +6db amplification. This will not affect an
* already playing sound but will take effect the next time it is
* played.
* Configures the sound's gain (volume). This value can range from 0.0 to 1.0 with 1.0 meaning
* no attenuation, each division by two corresponding to a -6db attentuation and each
* multiplication by two corresponding to +6db amplification. This will not affect an already
* playing sound but will take effect the next time it is played.
*
* <p><em>Note:</em> this value is multiplied by the base gain configured
* in the sound manager.
* <p>
* <em>Note:</em> this value is multiplied by the base gain configured in the sound manager.
*/
public void setGain (float gain)
{
@@ -107,19 +104,17 @@ public class Sound
}
/**
* Plays this sound from the beginning. While the sound is playing, an
* audio channel will be locked and then freed when the sound
* completes.
* Plays this sound from the beginning. While the sound is playing, an audio channel will be
* locked and then freed when the sound completes.
*
* @param allowDefer if false, the sound will be played immediately or
* not at all. If true, the sound will be queued up for loading if it
* is currently flushed from the cache and played once loaded.
* @param allowDefer if false, the sound will be played immediately or not at all. If true,
* the sound will be queued up for loading if it is currently flushed from the cache and
* played once loaded.
*
* @return true if the sound could be played and was started (or
* queued up to be loaded and played ASAP if it was specified as
* deferrable) or false if the sound could not be played either
* because it was not ready and deferral was not allowed or because
* too many other sounds were playing concurrently.
* @return true if the sound could be played and was started (or queued up to be loaded and
* played ASAP if it was specified as deferrable) or false if the sound could not be played
* either because it was not ready and deferral was not allowed or because too many other
* sounds were playing concurrently.
*/
public boolean play (boolean allowDefer)
{
@@ -127,12 +122,12 @@ public class Sound
}
/**
* Loops this sound, starting from the beginning of the audio data. It
* will continue to loop until {@link #pause}d or {@link #stop}ped.
* While the sound is playing an audio channel will be locked.
* Loops this sound, starting from the beginning of the audio data. It will continue to loop
* until {@link #pause}d or {@link #stop}ped. While the sound is playing an audio channel will
* be locked.
*
* @return true if a channel could be obtained to play the sound (and
* the sound was thus started) or false if no channels were available.
* @return true if a channel could be obtained to play the sound (and the sound was thus
* started) or false if no channels were available.
*/
public boolean loop (boolean allowDefer)
{
@@ -140,8 +135,7 @@ public class Sound
}
/**
* Plays this sound from the beginning, notifying the supplied observer
* when the audio starts.
* Plays this sound from the beginning, notifying the supplied observer when the audio starts.
*
* @param loop whether or not to loop the sampe until {@link #stop}ped.
*/
@@ -151,9 +145,9 @@ public class Sound
}
/**
* Pauses this sound. A subsequent call to {@link #play} will resume
* the sound from the precise position that it left off. While the
* sound is paused, its audio channel will remain locked.
* Pauses this sound. A subsequent call to {@link #play} will resume the sound from the
* precise position that it left off. While the sound is paused, its audio channel will remain
* locked.
*/
public void pause ()
{
@@ -163,8 +157,8 @@ public class Sound
}
/**
* Stops this sound and rewinds to its beginning. The audio channel
* being used to play the sound will be released.
* Stops this sound and rewinds to its beginning. The audio channel being used to play the
* sound will be released.
*/
public void stop ()
{
@@ -188,8 +182,7 @@ public class Sound
_buffer = buffer;
}
protected boolean play (
boolean allowDefer, final boolean loop, final StartObserver obs)
protected boolean play (boolean allowDefer, final boolean loop, final StartObserver obs)
{
// if we were unable to get our buffer, fail immediately
if (_buffer == null) {
@@ -202,15 +195,14 @@ public class Sound
// if we're not ready to go...
if (!_buffer.isPlayable()) {
if (allowDefer) {
// resolve the buffer and instruct it to play once it is
// resolved
// resolve the buffer and instruct it to play once it is resolved
_buffer.resolve(new ClipBuffer.Observer() {
public void clipLoaded (ClipBuffer buffer) {
play(false, loop, obs);
}
public void clipFailed (ClipBuffer buffer) {
// well, let's pretend like the sound started so that
// the observer isn't left hanging
// well, let's pretend like the sound started so that the observer isn't
// left hanging
if (obs != null) {
obs.soundStarted(Sound.this);
}
@@ -226,8 +218,7 @@ public class Sound
}
}
// let the observer know that (as far as they're concerned), we're
// started
// let the observer know that (as far as they're concerned), we're started
if (obs != null) {
obs.soundStarted(this);
}
@@ -255,8 +246,7 @@ public class Sound
}
// configure whether or not we should loop
AL10.alSourcei(_sourceId, AL10.AL_LOOPING,
loop ? AL10.AL_TRUE : AL10.AL_FALSE);
AL10.alSourcei(_sourceId, AL10.AL_LOOPING, loop ? AL10.AL_TRUE : AL10.AL_FALSE);
// and start that damned thing up!
AL10.alSourcePlay(_sourceId);
@@ -265,17 +255,15 @@ public class Sound
}
/**
* Called by the {@link SoundGroup} when it wants to reclaim our
* source.
* Called by the {@link SoundGroup} when it wants to reclaim our source.
*
* @return false if we have no source to reclaim or if we're still busy
* playing our sound, true if we gave up our source.
* @return false if we have no source to reclaim or if we're still busy playing our sound,
* true if we gave up our source.
*/
protected boolean reclaim ()
{
if (_sourceId != -1 &&
AL10.alGetSourcei(_sourceId, AL10.AL_SOURCE_STATE) ==
AL10.AL_STOPPED) {
AL10.alGetSourcei(_sourceId, AL10.AL_SOURCE_STATE) == AL10.AL_STOPPED) {
AL10.alSourcei(_sourceId, AL10.AL_BUFFER, 0);
_buffer.sourceUnbound();
_sourceId = -1;
+9 -14
View File
@@ -30,9 +30,8 @@ import org.lwjgl.openal.AL10;
import static com.threerings.openal.Log.log;
/**
* Manages a group of sounds, binding them to OpenAL sources as they are
* played and freeing up those sources for use by other sounds when the
* sounds are finished.
* Manages a group of sounds, binding them to OpenAL sources as they are played and freeing up
* those sources for use by other sounds when the sounds are finished.
*/
public class SoundGroup
{
@@ -47,8 +46,8 @@ public class SoundGroup
}
/**
* Obtains an "instance" of the specified sound which can be
* positioned, played, looped and otherwise used to make noise.
* Obtains an "instance" of the specified sound which can be positioned, played, looped and
* otherwise used to make noise.
*/
public Sound getSound (String path)
{
@@ -60,9 +59,8 @@ public class SoundGroup
}
/**
* Disposes this sound group, freeing up the OpenAL sources with which
* it is associated. All sounds obtained from this group will no
* longer be usable and should be discarded.
* Disposes this sound group, freeing up the OpenAL sources with which it is associated. All
* sounds obtained from this group will no longer be usable and should be discarded.
*/
public void dispose ()
{
@@ -75,8 +73,7 @@ public class SoundGroup
}
/**
* Stops and reclaims all sounds from this sound group but does not
* free the sources.
* Stops and reclaims all sounds from this sound group but does not free the sources.
*/
public void reclaimAll ()
{
@@ -92,8 +89,7 @@ public class SoundGroup
}
}
protected SoundGroup (
SoundManager manager, ClipProvider provider, int sources)
protected SoundGroup (SoundManager manager, ClipProvider provider, int sources)
{
_manager = manager;
_provider = provider;
@@ -125,8 +121,7 @@ public class SoundGroup
}
/**
* Called by a {@link Sound} when it wants to obtain a source on which
* to play its clip.
* Called by a {@link Sound} when it wants to obtain a source on which to play its clip.
*/
protected int acquireSource (Sound acquirer)
{
@@ -308,11 +308,11 @@ public class SoundManager
protected float _baseGain = 1;
/** Contains a mapping of all currently-loading clips. */
protected HashMap<Comparable,ClipBuffer> _loading = Maps.newHashMap();
protected HashMap<Comparable, ClipBuffer> _loading = Maps.newHashMap();
/** Contains a mapping of all loaded clips. */
protected LRUHashMap<Comparable,ClipBuffer> _clips =
new LRUHashMap<Comparable,ClipBuffer>(DEFAULT_CACHE_SIZE, _sizer);
protected LRUHashMap<Comparable, ClipBuffer> _clips =
new LRUHashMap<Comparable, ClipBuffer>(DEFAULT_CACHE_SIZE, _sizer);
/** Contains a queue of clip buffers waiting to be loaded. */
protected Queue<ClipBuffer> _toLoad;