package greenfoot.sound;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.UnsupportedAudioFileException;
| An implementation of GreenfootAudioInputStream that reads from a memory buffer
|
| @author neil
|
public class MemoryAudioInputStream
implements GreenfootAudioInputStream{
private byte[] sound;
private int startOffset;
private int endOffset;
private AudioFormat format;
private int markIndex;
private int curIndex;
public MemoryAudioInputStream(byte[] sound, AudioFormat format)
{
curIndex = 0;
markIndex = 0;
startOffset = 0;
endOffset = sound.length;
this.sound = sound;
this.format = format;
}
public MemoryAudioInputStream(byte[] sound, int offset, int length, AudioFormat format)
{
curIndex = offset;
markIndex = curIndex;
startOffset = offset;
endOffset = offset + length;
this.sound = sound;
this.format = format;
}
private int getFrameSize()
{
return format.getFrameSize();
}
public int available() throws IOException
{
return endOffset - curIndex;
}
public void close() throws IOException
{
}
public AudioFormat getFormat()
{
return format;
}
public String getSource()
{
return "Internal buffer";
}
public void mark(int readlimit)
{
markIndex = curIndex;
}
public boolean markSupported()
{
return true;
}
public void open() throws IOException, UnsupportedAudioFileException
{
curIndex = startOffset;
}
public int read() throws IOException
{
if (getFrameSize() != 1)
throw new IOException("Attempted to read single byte but frame size is not 1");
if (curIndex < endOffset)
return sound[curIndex++];
else{ return -1;
}
}
public int read(byte[] b) throws IOException
{
return read(b, 0, b.length);
}
public int read(byte[] b, int off, int len) throws IOException
{
if (curIndex >= endOffset)
return -1;
int maxRead = len - (len % getFrameSize());
if (curIndex + maxRead > endOffset) {
int left = endOffset - curIndex;
maxRead = left - (left % getFrameSize());
}
System.arraycopy(sound, curIndex, b, off, maxRead);
curIndex += maxRead;
return maxRead;
}
public void reset() throws IOException
{
curIndex = markIndex;
}
public void restart() throws IOException, UnsupportedAudioFileException
{
curIndex = startOffset;
}
public long skip(long n) throws IOException
{
if (curIndex + n <= endOffset) {
curIndex += n;
return n;
}
else {
int diff = endOffset - curIndex;
curIndex = endOffset;
return diff;
}
}
}
top,
use,
map,
class MemoryAudioInputStream
. MemoryAudioInputStream
. MemoryAudioInputStream
. getFrameSize
. available
. close
. getFormat
. getSource
. mark
. markSupported
. open
. read
. read
. read
. reset
. restart
. skip
176 neLoCode
+ 2 LoComm