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