Moved the code for registering extensions and creating instances to
StreamDecoder. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@154 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
@@ -21,32 +21,18 @@
|
|||||||
|
|
||||||
package com.threerings.openal;
|
package com.threerings.openal;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import org.lwjgl.openal.AL10;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An audio stream read from one or more files.
|
* An audio stream read from one or more files.
|
||||||
*/
|
*/
|
||||||
public class FileStream extends Stream
|
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.
|
* Creates a new stream for the specified file.
|
||||||
*
|
*
|
||||||
@@ -58,7 +44,7 @@ public class FileStream extends Stream
|
|||||||
{
|
{
|
||||||
super(soundmgr);
|
super(soundmgr);
|
||||||
_file = new QueuedFile(file, loop);
|
_file = new QueuedFile(file, loop);
|
||||||
_decoder = createDecoder(file);
|
_decoder = StreamDecoder.createInstance(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -93,43 +79,13 @@ public class FileStream extends Stream
|
|||||||
if (!_queue.isEmpty()) {
|
if (!_queue.isEmpty()) {
|
||||||
_file = _queue.remove(0);
|
_file = _queue.remove(0);
|
||||||
}
|
}
|
||||||
_decoder = createDecoder(_file.file);
|
_decoder = StreamDecoder.createInstance(_file.file);
|
||||||
read = Math.max(0, read);
|
read = Math.max(0, read);
|
||||||
read += _decoder.read(buf);
|
read += _decoder.read(buf);
|
||||||
}
|
}
|
||||||
return read;
|
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. */
|
/** The file currently being played. */
|
||||||
protected QueuedFile _file;
|
protected QueuedFile _file;
|
||||||
|
|
||||||
@@ -155,11 +111,4 @@ public class FileStream extends Stream
|
|||||||
this.loop = loop;
|
this.loop = loop;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Registered decoder classes. */
|
|
||||||
protected static HashMap<String, Class> _extensions = new HashMap<String, Class>();
|
|
||||||
static {
|
|
||||||
registerExtension("ogg", OggStreamDecoder.class);
|
|
||||||
registerExtension("mp3", Mp3StreamDecoder.class);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,8 +37,7 @@ import javazoom.jl.decoder.SampleBuffer;
|
|||||||
/**
|
/**
|
||||||
* Decodes MP3 streams.
|
* Decodes MP3 streams.
|
||||||
*/
|
*/
|
||||||
public class Mp3StreamDecoder
|
public class Mp3StreamDecoder extends StreamDecoder
|
||||||
implements StreamDecoder
|
|
||||||
{
|
{
|
||||||
// documentation inherited
|
// documentation inherited
|
||||||
public void init (InputStream in)
|
public void init (InputStream in)
|
||||||
|
|||||||
@@ -33,8 +33,7 @@ import com.jmex.sound.openAL.objects.util.OggInputStream;
|
|||||||
/**
|
/**
|
||||||
* Decodes Ogg Vorbis streams.
|
* Decodes Ogg Vorbis streams.
|
||||||
*/
|
*/
|
||||||
public class OggStreamDecoder
|
public class OggStreamDecoder extends StreamDecoder
|
||||||
implements StreamDecoder
|
|
||||||
{
|
{
|
||||||
// documentation inherited
|
// documentation inherited
|
||||||
public void init (InputStream in)
|
public void init (InputStream in)
|
||||||
|
|||||||
@@ -21,6 +21,10 @@
|
|||||||
|
|
||||||
package com.threerings.openal;
|
package com.threerings.openal;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
@@ -29,8 +33,46 @@ import java.nio.ByteBuffer;
|
|||||||
/**
|
/**
|
||||||
* Decodes audio streams from data read from an {@link InputStream}.
|
* 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.
|
* Initializes the decoder with its input stream.
|
||||||
*/
|
*/
|
||||||
@@ -55,4 +97,11 @@ public interface StreamDecoder
|
|||||||
*/
|
*/
|
||||||
public abstract int read (ByteBuffer buf)
|
public abstract int read (ByteBuffer buf)
|
||||||
throws IOException;
|
throws IOException;
|
||||||
|
|
||||||
|
/** Maps file extensions to decoder classes. */
|
||||||
|
protected static HashMap<String, Class> _extensions = new HashMap<String, Class>();
|
||||||
|
static {
|
||||||
|
registerExtension("ogg", OggStreamDecoder.class);
|
||||||
|
registerExtension("mp3", Mp3StreamDecoder.class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user