Add ogg streaming to OpenALSoundPlayer

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@642 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Charlie Groves
2008-08-21 05:33:19 +00:00
parent 81a2d3d104
commit c06de7d51f
2 changed files with 89 additions and 24 deletions
@@ -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;
}
}
@@ -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)
{