Follow-up to #15.
- Removed the temporary directory (Options.TMP_DIR), which is no longer needed. - Excluded original Slick2D classes that were overridden in pom.xml. - Formatting changes and documentation. Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
@@ -1,176 +1,202 @@
|
||||
/*
|
||||
Copyright (c) 2013, Slick2D
|
||||
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the Slick2D nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS <20>gAS IS<49>h AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* Copyright (c) 2013, Slick2D
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* - Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* - Neither the name of the Slick2D nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.newdawn.slick.openal;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import sun.font.EAttribute;
|
||||
import javazoom.jl.decoder.*;
|
||||
import javazoom.jl.decoder.Bitstream;
|
||||
import javazoom.jl.decoder.BitstreamException;
|
||||
import javazoom.jl.decoder.Decoder;
|
||||
import javazoom.jl.decoder.DecoderException;
|
||||
import javazoom.jl.decoder.Header;
|
||||
import javazoom.jl.decoder.SampleBuffer;
|
||||
|
||||
import org.newdawn.slick.util.Log;
|
||||
|
||||
/**
|
||||
* An input stream that can extract MP3 data.
|
||||
*
|
||||
* @author fluddokt (https://github.com/fluddokt)
|
||||
*/
|
||||
public class Mp3InputStream extends InputStream implements AudioInputStream {
|
||||
/** The MPEG audio bitstream. */
|
||||
private Bitstream bitstream;
|
||||
|
||||
Bitstream bitstream;
|
||||
Decoder decoder;
|
||||
Header header;
|
||||
SampleBuffer buf;
|
||||
|
||||
int channels;
|
||||
int sampleRate;
|
||||
|
||||
int bufLen = 0;
|
||||
boolean atEnd=false;
|
||||
int bpos; //byte pos
|
||||
|
||||
public Mp3InputStream(InputStream resourceAsStream) {
|
||||
/** The MPEG decoder. */
|
||||
private Decoder decoder;
|
||||
|
||||
/** The frame header extractor. */
|
||||
private Header header;
|
||||
|
||||
/** The buffer. */
|
||||
private SampleBuffer buf;
|
||||
|
||||
/** The number of channels. */
|
||||
private int channels;
|
||||
|
||||
/** The sample rate. */
|
||||
private int sampleRate;
|
||||
|
||||
/** The buffer length. */
|
||||
private int bufLen = 0;
|
||||
|
||||
/** True if we've reached the end of the available data. */
|
||||
private boolean endOfStream = false;
|
||||
|
||||
/** The byte position. */
|
||||
private int bpos;
|
||||
|
||||
/**
|
||||
* Create a new stream to decode MP3 data.
|
||||
* @param input the input stream from which to read the MP3 file
|
||||
*/
|
||||
public Mp3InputStream(InputStream input) {
|
||||
decoder = new Decoder();
|
||||
bitstream = new Bitstream(resourceAsStream);
|
||||
bitstream = new Bitstream(input);
|
||||
try {
|
||||
header = bitstream.readFrame();
|
||||
} catch (BitstreamException e) {
|
||||
e.printStackTrace();
|
||||
Log.error(e);
|
||||
}
|
||||
|
||||
channels = header.mode()==Header.SINGLE_CHANNEL?1:2;
|
||||
|
||||
channels = (header.mode() == Header.SINGLE_CHANNEL) ? 1 : 2;
|
||||
sampleRate = header.frequency();
|
||||
|
||||
|
||||
buf = new SampleBuffer(sampleRate, channels);
|
||||
decoder.setOutputBuffer(buf);
|
||||
//*
|
||||
|
||||
try {
|
||||
decoder.decodeFrame(header, bitstream);
|
||||
} catch (DecoderException e) {
|
||||
e.printStackTrace();
|
||||
Log.error(e);
|
||||
}
|
||||
//*/
|
||||
|
||||
bufLen = buf.getBufferLength();
|
||||
bitstream.closeFrame();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
if(atEnd())
|
||||
if (atEnd())
|
||||
return -1;
|
||||
while(bpos/2>=bufLen){
|
||||
while (bpos / 2 >= bufLen) {
|
||||
try {
|
||||
header = bitstream.readFrame();
|
||||
if(header == null){
|
||||
if (header == null) {
|
||||
buf.clear_buffer();
|
||||
|
||||
atEnd = true;
|
||||
|
||||
endOfStream = true;
|
||||
return -1;
|
||||
}
|
||||
buf.clear_buffer();
|
||||
decoder.decodeFrame(header, bitstream);
|
||||
bufLen = buf.getBufferLength();
|
||||
bitstream.closeFrame();
|
||||
} catch (DecoderException e) {
|
||||
e.printStackTrace();
|
||||
} catch (BitstreamException e) {
|
||||
e.printStackTrace();
|
||||
} catch (DecoderException | BitstreamException e) {
|
||||
Log.error(e);
|
||||
}
|
||||
bpos=0;
|
||||
bpos = 0;
|
||||
}
|
||||
int npos = bpos/2;
|
||||
int npos = bpos / 2;
|
||||
bpos++;
|
||||
|
||||
if(bpos%2==0)
|
||||
return (buf.getBuffer()[npos]>>8)&0xff;
|
||||
|
||||
if (bpos % 2 == 0)
|
||||
return (buf.getBuffer()[npos] >> 8) & 0xff;
|
||||
else
|
||||
return (buf.getBuffer()[npos])&0xff;
|
||||
return (buf.getBuffer()[npos]) & 0xff;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean atEnd() {
|
||||
return atEnd;
|
||||
}
|
||||
public boolean atEnd() { return endOfStream; }
|
||||
|
||||
@Override
|
||||
public int getChannels() {
|
||||
return channels;
|
||||
}
|
||||
public int getChannels() { return channels; }
|
||||
|
||||
@Override
|
||||
public int getRate() { return sampleRate; }
|
||||
|
||||
@Override
|
||||
public int getRate() {
|
||||
return sampleRate;
|
||||
}
|
||||
/**
|
||||
* @see java.io.InputStream#read(byte[], int, int)
|
||||
*/
|
||||
public int read(byte[] b, int off, int len) throws IOException {
|
||||
for (int i=0;i<len;i++) {
|
||||
for (int i = 0; i < len; i++) {
|
||||
try {
|
||||
int value = read();
|
||||
if (value >= 0) {
|
||||
if (value >= 0)
|
||||
b[i] = (byte) value;
|
||||
} else {
|
||||
if (i == 0) {
|
||||
return -1;
|
||||
} else {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
else
|
||||
return (i == 0) ? -1 : i;
|
||||
} catch (IOException e) {
|
||||
//Log.error(e);
|
||||
e.printStackTrace();
|
||||
Log.error(e);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see java.io.InputStream#read(byte[])
|
||||
*/
|
||||
public int read(byte[] b) throws IOException {
|
||||
return read(b, 0, b.length);
|
||||
}
|
||||
@Override
|
||||
public int read(byte[] b) throws IOException { return read(b, 0, b.length); }
|
||||
|
||||
@Override
|
||||
public long skip(long length) {
|
||||
//System.out.println("skip"+length);
|
||||
int skiped = 0;
|
||||
if(bufLen<=0){
|
||||
System.out.println("We don't know buf Length yet");
|
||||
//throw new Error("We don't know buf Length yet");
|
||||
}
|
||||
while(skiped+bufLen*2<length){
|
||||
if (bufLen <= 0)
|
||||
Log.warn("Mp3InputStream: skip: bufLen not yet determined.");
|
||||
|
||||
int skipped = 0;
|
||||
while (skipped + bufLen * 2 < length) {
|
||||
try {
|
||||
header = bitstream.readFrame();
|
||||
if(header == null){
|
||||
//System.out.println("Header is null");
|
||||
atEnd = true;
|
||||
if (header == null) {
|
||||
// Log.warn("Mp3InputStream: skip: header is null.");
|
||||
endOfStream = true;
|
||||
return -1;
|
||||
}
|
||||
//last frame that won't be skiped so better read it
|
||||
if(skiped+bufLen*2*4 >=length || bufLen<=0){
|
||||
|
||||
// last frame that won't be skipped so better read it
|
||||
if (skipped + bufLen * 2 * 4 >= length || bufLen <= 0) {
|
||||
buf.clear_buffer();
|
||||
decoder.decodeFrame(header, bitstream);
|
||||
bufLen = buf.getBufferLength();
|
||||
}
|
||||
skiped+=bufLen*2-bpos;
|
||||
|
||||
skipped += bufLen * 2 - bpos;
|
||||
|
||||
bitstream.closeFrame();
|
||||
bpos=0;
|
||||
} catch (BitstreamException e) {
|
||||
e.printStackTrace();
|
||||
}catch (DecoderException e) {
|
||||
e.printStackTrace();
|
||||
bpos = 0;
|
||||
} catch (BitstreamException | DecoderException e) {
|
||||
Log.error(e);
|
||||
}
|
||||
}
|
||||
if(bufLen*2-bpos>length-skiped){
|
||||
bpos+=length-skiped;
|
||||
skiped+=length-skiped;
|
||||
if (bufLen * 2 - bpos > length - skipped) {
|
||||
bpos += length - skipped;
|
||||
skipped += length - skipped;
|
||||
}
|
||||
|
||||
return skiped;
|
||||
|
||||
|
||||
return skipped;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user