Added ability to perform simple file queueing.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4112 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Andrzej Kapolka
2006-05-11 02:09:54 +00:00
parent 790ddafb30
commit a9567278ea
@@ -28,27 +28,40 @@ import java.io.InputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.ArrayList;
import org.lwjgl.openal.AL10; import org.lwjgl.openal.AL10;
import com.jmex.sound.openAL.objects.util.OggInputStream; import com.jmex.sound.openAL.objects.util.OggInputStream;
/** /**
* An audio stream read from an Ogg Vorbis file. * An audio stream read from one or more Ogg Vorbis files.
*/ */
public class OggFileStream extends Stream public class OggFileStream extends Stream
{ {
/** /**
* Creates a new Ogg stream for the specified Ogg file. * Creates a new Ogg stream for the specified file.
* *
* @param loop whether or not to play the file in a continuous loop * @param loop whether or not to play the file in a continuous loop
* if there's nothing on the queue
*/ */
public OggFileStream (SoundManager soundmgr, File file, boolean loop) public OggFileStream (SoundManager soundmgr, File file, boolean loop)
throws IOException throws IOException
{ {
super(soundmgr); super(soundmgr);
_file = file; _file = new OggFile(file, loop);
_istream = new OggInputStream(new FileInputStream(_file)); _istream = new OggInputStream(new FileInputStream(file));
_loop = loop; }
/**
* Adds a file to the queue of files to play.
*
* @param loop if true, play this file in a loop if there's nothing else
* on the queue
*/
public void queueFile (File file, boolean loop)
{
_queue.add(new OggFile(file, loop));
} }
// documentation inherited // documentation inherited
@@ -69,21 +82,40 @@ public class OggFileStream extends Stream
throws IOException throws IOException
{ {
int read = _istream.read(buf, buf.position(), buf.remaining()); int read = _istream.read(buf, buf.position(), buf.remaining());
while (buf.hasRemaining() && _loop) { while (buf.hasRemaining() && (!_queue.isEmpty() || _file.loop)) {
_istream = new OggInputStream(new FileInputStream(_file)); if (!_queue.isEmpty()) {
_file = _queue.remove(0);
}
_istream = new OggInputStream(new FileInputStream(_file.file));
read = Math.max(0, read); read = Math.max(0, read);
read += _istream.read(buf, buf.position(), buf.remaining()); read += _istream.read(buf, buf.position(), buf.remaining());
} }
return read; return read;
} }
/** The Ogg file from which to read. */ /** The file currently being played. */
protected File _file; protected OggFile _file;
/** The underlying Ogg input stream. */ /** The underlying Ogg input stream for the current file. */
protected OggInputStream _istream; protected OggInputStream _istream;
/** Whether or not to loop back to the beginning of the file when we've /** The queue of files to play after the current one. */
* reached the end. */ protected ArrayList<OggFile> _queue = new ArrayList<OggFile>();
protected boolean _loop;
/** A file queued for play. */
protected class OggFile
{
/** The file to play. */
public File file;
/** Whether or not to play the file in a loop when there's nothing
* in the queue. */
public boolean loop;
public OggFile (File file, boolean loop)
{
this.file = file;
this.loop = loop;
}
}
} }