diff --git a/src/java/com/threerings/openal/FileStream.java b/src/java/com/threerings/openal/FileStream.java index 9ef73813..c8c355f7 100644 --- a/src/java/com/threerings/openal/FileStream.java +++ b/src/java/com/threerings/openal/FileStream.java @@ -21,32 +21,18 @@ package com.threerings.openal; -import java.util.HashMap; - import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; import java.nio.ByteBuffer; import java.util.ArrayList; -import org.lwjgl.openal.AL10; - /** * An audio stream read from one or more files. */ public class FileStream extends Stream { - /** - * Registers a class of {@link StreamDecoder} for the specified file extension. - */ - public static void registerExtension (String extension, Class clazz) - { - _extensions.put(extension, clazz); - } - /** * Creates a new stream for the specified file. * @@ -58,7 +44,7 @@ public class FileStream extends Stream { super(soundmgr); _file = new QueuedFile(file, loop); - _decoder = createDecoder(file); + _decoder = StreamDecoder.createInstance(file); } /** @@ -93,43 +79,13 @@ public class FileStream extends Stream if (!_queue.isEmpty()) { _file = _queue.remove(0); } - _decoder = createDecoder(_file.file); + _decoder = StreamDecoder.createInstance(_file.file); read = Math.max(0, read); read += _decoder.read(buf); } return read; } - /** - * Creates and initializes a stream decoder for the specified file. - */ - protected StreamDecoder createDecoder (File file) - throws IOException - { - String path = file.getPath(); - int idx = path.lastIndexOf('.'); - if (idx == -1) { - Log.warning("Missing extension for file [file=" + path + "]."); - return null; - } - String extension = path.substring(idx+1); - Class clazz = _extensions.get(extension); - if (clazz == null) { - Log.warning("No decoder registered for extension [extension=" + extension + - ", file=" + path + "]."); - return null; - } - StreamDecoder decoder; - try { - decoder = (StreamDecoder)clazz.newInstance(); - } catch (Exception e) { - Log.warning("Error instantiating decoder [file=" + path + ", error=" + e + "]."); - return null; - } - decoder.init(new FileInputStream(file)); - return decoder; - } - /** The file currently being played. */ protected QueuedFile _file; @@ -155,11 +111,4 @@ public class FileStream extends Stream this.loop = loop; } } - - /** Registered decoder classes. */ - protected static HashMap _extensions = new HashMap(); - static { - registerExtension("ogg", OggStreamDecoder.class); - registerExtension("mp3", Mp3StreamDecoder.class); - } } diff --git a/src/java/com/threerings/openal/Mp3StreamDecoder.java b/src/java/com/threerings/openal/Mp3StreamDecoder.java index 225229c5..c0522458 100644 --- a/src/java/com/threerings/openal/Mp3StreamDecoder.java +++ b/src/java/com/threerings/openal/Mp3StreamDecoder.java @@ -37,8 +37,7 @@ import javazoom.jl.decoder.SampleBuffer; /** * Decodes MP3 streams. */ -public class Mp3StreamDecoder - implements StreamDecoder +public class Mp3StreamDecoder extends StreamDecoder { // documentation inherited public void init (InputStream in) diff --git a/src/java/com/threerings/openal/OggStreamDecoder.java b/src/java/com/threerings/openal/OggStreamDecoder.java index 5cafa6d9..8c89be96 100644 --- a/src/java/com/threerings/openal/OggStreamDecoder.java +++ b/src/java/com/threerings/openal/OggStreamDecoder.java @@ -33,8 +33,7 @@ import com.jmex.sound.openAL.objects.util.OggInputStream; /** * Decodes Ogg Vorbis streams. */ -public class OggStreamDecoder - implements StreamDecoder +public class OggStreamDecoder extends StreamDecoder { // documentation inherited public void init (InputStream in) diff --git a/src/java/com/threerings/openal/StreamDecoder.java b/src/java/com/threerings/openal/StreamDecoder.java index 858918ce..b7758af6 100644 --- a/src/java/com/threerings/openal/StreamDecoder.java +++ b/src/java/com/threerings/openal/StreamDecoder.java @@ -21,6 +21,10 @@ package com.threerings.openal; +import java.util.HashMap; + +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; @@ -29,8 +33,46 @@ import java.nio.ByteBuffer; /** * Decodes audio streams from data read from an {@link InputStream}. */ -public interface StreamDecoder +public abstract class StreamDecoder { + /** + * Registers a class of {@link StreamDecoder} for the specified file extension. + */ + public static void registerExtension (String extension, Class clazz) + { + _extensions.put(extension, clazz); + } + + /** + * Creates and initializes a stream decoder for the specified file. + */ + public static StreamDecoder createInstance (File file) + throws IOException + { + String path = file.getPath(); + int idx = path.lastIndexOf('.'); + if (idx == -1) { + Log.warning("Missing extension for file [file=" + path + "]."); + return null; + } + String extension = path.substring(idx+1); + Class clazz = _extensions.get(extension); + if (clazz == null) { + Log.warning("No decoder registered for extension [extension=" + extension + + ", file=" + path + "]."); + return null; + } + StreamDecoder decoder; + try { + decoder = (StreamDecoder)clazz.newInstance(); + } catch (Exception e) { + Log.warning("Error instantiating decoder [file=" + path + ", error=" + e + "]."); + return null; + } + decoder.init(new FileInputStream(file)); + return decoder; + } + /** * Initializes the decoder with its input stream. */ @@ -55,4 +97,11 @@ public interface StreamDecoder */ public abstract int read (ByteBuffer buf) throws IOException; + + /** Maps file extensions to decoder classes. */ + protected static HashMap _extensions = new HashMap(); + static { + registerExtension("ogg", OggStreamDecoder.class); + registerExtension("mp3", Mp3StreamDecoder.class); + } }