From c06de7d51f7dae99db78d09ea5004ff00001f6c4 Mon Sep 17 00:00:00 2001 From: Charlie Groves Date: Thu, 21 Aug 2008 05:33:19 +0000 Subject: [PATCH] Add ogg streaming to OpenALSoundPlayer git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@642 ed5b42cb-e716-0410-a449-f6a68f950b19 --- .../threerings/media/sound/SoundLoader.java | 64 ++++++++++++------- .../threerings/openal/OpenALSoundPlayer.java | 49 ++++++++++++++ 2 files changed, 89 insertions(+), 24 deletions(-) diff --git a/src/java/com/threerings/media/sound/SoundLoader.java b/src/java/com/threerings/media/sound/SoundLoader.java index dccbd011..6459244a 100644 --- a/src/java/com/threerings/media/sound/SoundLoader.java +++ b/src/java/com/threerings/media/sound/SoundLoader.java @@ -50,6 +50,27 @@ public class SoundLoader return data; } + /** + * Attempts to load a sound stream from the given path from the given bundle and from the + * classpath. If nothing is found, a FileNotFoundException is thrown. + */ + public InputStream getSound (String bundle, String path) + throws IOException + { + InputStream rsrc; + try { + rsrc = _rmgr.getResource(bundle, path); + } catch (FileNotFoundException notFound) { + // try from the classpath + try { + rsrc = _rmgr.getResource(path); + } catch (FileNotFoundException notFoundAgain) { + throw notFound; + } + } + return rsrc; + } + public void shutdown () { _configs.clear(); @@ -84,35 +105,30 @@ public class SoundLoader { InputStream clipin = null; try { - clipin = _rmgr.getResource(bundle, path); + clipin = getSound(bundle, path); } catch (FileNotFoundException fnfe) { - // try from the classpath - try { - clipin = _rmgr.getResource(path); - } catch (FileNotFoundException fnfe2) { - // only play the default sound if we have verbose sound debugging turned on. - if (JavaSoundPlayer._verbose.getValue()) { - log.warning("Could not locate sound data", "bundle", bundle, "path", path); - if (_defaultClipPath != null) { + // only play the default sound if we have verbose sound debugging turned on. + if (JavaSoundPlayer._verbose.getValue()) { + log.warning("Could not locate sound data", "bundle", bundle, "path", path); + if (_defaultClipPath != null) { + try { + clipin = _rmgr.getResource(_defaultClipBundle, _defaultClipPath); + } catch (FileNotFoundException fnfe3) { try { - clipin = _rmgr.getResource(_defaultClipBundle, _defaultClipPath); - } catch (FileNotFoundException fnfe3) { - try { - clipin = _rmgr.getResource(_defaultClipPath); - } catch (FileNotFoundException fnfe4) { - log.warning( - "Additionally, the default fallback sound could not be located", - "bundle", _defaultClipBundle, "path", _defaultClipPath); - } + clipin = _rmgr.getResource(_defaultClipPath); + } catch (FileNotFoundException fnfe4) { + log.warning( + "Additionally, the default fallback sound could not be located", + "bundle", _defaultClipBundle, "path", _defaultClipPath); } - } else { - log.warning("No fallback default sound specified!"); } + } else { + log.warning("No fallback default sound specified!"); } - // if we couldn't load the default, rethrow - if (clipin == null) { - throw fnfe2; - } + } + // if we couldn't load the default, rethrow + if (clipin == null) { + throw fnfe; } } diff --git a/src/java/com/threerings/openal/OpenALSoundPlayer.java b/src/java/com/threerings/openal/OpenALSoundPlayer.java index 43d3b4a5..fd5e7584 100644 --- a/src/java/com/threerings/openal/OpenALSoundPlayer.java +++ b/src/java/com/threerings/openal/OpenALSoundPlayer.java @@ -7,8 +7,11 @@ import static com.threerings.media.Log.log; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; import java.util.Map; +import org.lwjgl.openal.AL10; import org.lwjgl.util.WaveData; import com.google.common.collect.Maps; @@ -97,6 +100,52 @@ public class OpenALSoundPlayer extends SoundPlayer } } + /** + * Streams ogg files from the given bundle and path. + */ + public Stream stream (final String bundle, final String path, final boolean loop) + throws IOException + { + if (!path.endsWith(".ogg")) { + log.warning("Unknown file type for streaming", "bundle", bundle, "path", path); + return null; + } + InputStream rsrc = _loader.getSound(bundle, path); + final StreamDecoder dec = new OggStreamDecoder(); + dec.init(rsrc); + return new Stream(_alSoundManager) { + + @Override + protected void update (float time) { + super.update(time); + if (_state != AL10.AL_PLAYING) { + return; + } + setGain(_clipVol); + } + + @Override + protected int getFormat () { + return dec.getFormat(); + } + + @Override + protected int getFrequency () { + return dec.getFrequency(); + } + + @Override + protected int populateBuffer (ByteBuffer buf) throws IOException { + int read = dec.read(buf); + if(buf.hasRemaining() && loop) { + dec.init(_loader.getSound(bundle, path)); + read = Math.max(0, read); + read += dec.read(buf); + } + return read; + }}; + } + @Override protected Frob loop (String pkgPath, String key, float pan) {