2015-02-09 05:48:55 +01:00
|
|
|
/*
|
2015-02-12 09:52:19 +01:00
|
|
|
* 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.
|
2015-02-19 21:05:55 +01:00
|
|
|
*/
|
2015-02-12 09:52:19 +01:00
|
|
|
|
2015-02-09 05:48:55 +01:00
|
|
|
package org.newdawn.slick.openal;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
2015-02-12 09:52:19 +01:00
|
|
|
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;
|
2015-02-09 05:48:55 +01:00
|
|
|
|
2015-02-12 09:52:19 +01:00
|
|
|
/**
|
|
|
|
* An input stream that can extract MP3 data.
|
|
|
|
*
|
|
|
|
* @author fluddokt (https://github.com/fluddokt)
|
|
|
|
*/
|
2015-02-09 05:48:55 +01:00
|
|
|
public class Mp3InputStream extends InputStream implements AudioInputStream {
|
2015-02-12 09:52:19 +01:00
|
|
|
/** The MPEG audio bitstream. */
|
|
|
|
private Bitstream bitstream;
|
|
|
|
|
|
|
|
/** 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;
|
2015-02-09 05:48:55 +01:00
|
|
|
|
2015-02-12 09:52:19 +01:00
|
|
|
/** 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
|
2015-08-26 16:29:28 +02:00
|
|
|
* @throws IOException failure to read the header from the input stream
|
2015-02-12 09:52:19 +01:00
|
|
|
*/
|
2015-08-26 16:29:28 +02:00
|
|
|
public Mp3InputStream(InputStream input) throws IOException {
|
2015-02-09 05:48:55 +01:00
|
|
|
decoder = new Decoder();
|
2015-02-12 09:52:19 +01:00
|
|
|
bitstream = new Bitstream(input);
|
2015-02-09 05:48:55 +01:00
|
|
|
try {
|
|
|
|
header = bitstream.readFrame();
|
|
|
|
} catch (BitstreamException e) {
|
2015-02-12 09:52:19 +01:00
|
|
|
Log.error(e);
|
2015-02-09 05:48:55 +01:00
|
|
|
}
|
2015-08-26 16:29:28 +02:00
|
|
|
if (header == null) {
|
|
|
|
close();
|
|
|
|
throw new IOException("Failed to read header from MP3 input stream.");
|
|
|
|
}
|
2015-02-12 09:52:19 +01:00
|
|
|
|
|
|
|
channels = (header.mode() == Header.SINGLE_CHANNEL) ? 1 : 2;
|
2015-02-09 05:48:55 +01:00
|
|
|
sampleRate = header.frequency();
|
2015-02-12 09:52:19 +01:00
|
|
|
|
2015-02-09 05:48:55 +01:00
|
|
|
buf = new SampleBuffer(sampleRate, channels);
|
|
|
|
decoder.setOutputBuffer(buf);
|
2015-02-12 09:52:19 +01:00
|
|
|
|
2015-02-09 05:48:55 +01:00
|
|
|
try {
|
|
|
|
decoder.decodeFrame(header, bitstream);
|
|
|
|
} catch (DecoderException e) {
|
2015-02-12 09:52:19 +01:00
|
|
|
Log.error(e);
|
2015-02-09 05:48:55 +01:00
|
|
|
}
|
2015-02-12 09:52:19 +01:00
|
|
|
|
2015-02-09 05:48:55 +01:00
|
|
|
bufLen = buf.getBufferLength();
|
|
|
|
bitstream.closeFrame();
|
|
|
|
}
|
2015-02-12 09:52:19 +01:00
|
|
|
|
2015-02-09 05:48:55 +01:00
|
|
|
@Override
|
|
|
|
public int read() throws IOException {
|
2015-02-12 09:52:19 +01:00
|
|
|
if (atEnd())
|
2015-02-09 05:48:55 +01:00
|
|
|
return -1;
|
2015-02-12 09:52:19 +01:00
|
|
|
while (bpos / 2 >= bufLen) {
|
2015-02-09 05:48:55 +01:00
|
|
|
try {
|
|
|
|
header = bitstream.readFrame();
|
2015-02-12 09:52:19 +01:00
|
|
|
if (header == null) {
|
2015-02-09 05:48:55 +01:00
|
|
|
buf.clear_buffer();
|
2015-02-12 09:52:19 +01:00
|
|
|
|
|
|
|
endOfStream = true;
|
2015-02-09 05:48:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
buf.clear_buffer();
|
|
|
|
decoder.decodeFrame(header, bitstream);
|
|
|
|
bufLen = buf.getBufferLength();
|
|
|
|
bitstream.closeFrame();
|
2015-02-12 09:52:19 +01:00
|
|
|
} catch (DecoderException | BitstreamException e) {
|
|
|
|
Log.error(e);
|
2015-02-09 05:48:55 +01:00
|
|
|
}
|
2015-02-12 09:52:19 +01:00
|
|
|
bpos = 0;
|
2015-02-09 05:48:55 +01:00
|
|
|
}
|
2015-02-12 09:52:19 +01:00
|
|
|
int npos = bpos / 2;
|
2015-02-09 05:48:55 +01:00
|
|
|
bpos++;
|
2015-02-12 09:52:19 +01:00
|
|
|
|
|
|
|
if (bpos % 2 == 0)
|
|
|
|
return (buf.getBuffer()[npos] >> 8) & 0xff;
|
2015-02-09 05:48:55 +01:00
|
|
|
else
|
2015-02-12 09:52:19 +01:00
|
|
|
return (buf.getBuffer()[npos]) & 0xff;
|
2015-02-09 05:48:55 +01:00
|
|
|
}
|
2015-02-12 09:52:19 +01:00
|
|
|
|
2015-02-09 05:48:55 +01:00
|
|
|
@Override
|
2015-02-12 09:52:19 +01:00
|
|
|
public boolean atEnd() { return endOfStream; }
|
|
|
|
|
2015-02-09 05:48:55 +01:00
|
|
|
@Override
|
2015-02-12 09:52:19 +01:00
|
|
|
public int getChannels() { return channels; }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getRate() { return sampleRate; }
|
|
|
|
|
2015-02-09 05:48:55 +01:00
|
|
|
@Override
|
|
|
|
public int read(byte[] b, int off, int len) throws IOException {
|
2015-02-12 09:52:19 +01:00
|
|
|
for (int i = 0; i < len; i++) {
|
2015-02-09 05:48:55 +01:00
|
|
|
try {
|
|
|
|
int value = read();
|
2015-02-12 09:52:19 +01:00
|
|
|
if (value >= 0)
|
2015-02-09 05:48:55 +01:00
|
|
|
b[i] = (byte) value;
|
2015-02-12 09:52:19 +01:00
|
|
|
else
|
|
|
|
return (i == 0) ? -1 : i;
|
2015-02-09 05:48:55 +01:00
|
|
|
} catch (IOException e) {
|
2015-02-12 09:52:19 +01:00
|
|
|
Log.error(e);
|
2015-02-09 05:48:55 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2015-02-12 09:52:19 +01:00
|
|
|
|
2015-02-09 05:48:55 +01:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2015-02-12 09:52:19 +01:00
|
|
|
@Override
|
|
|
|
public int read(byte[] b) throws IOException { return read(b, 0, b.length); }
|
|
|
|
|
2015-02-09 05:48:55 +01:00
|
|
|
@Override
|
|
|
|
public long skip(long length) {
|
2015-02-12 09:52:19 +01:00
|
|
|
if (bufLen <= 0)
|
|
|
|
Log.warn("Mp3InputStream: skip: bufLen not yet determined.");
|
|
|
|
|
|
|
|
int skipped = 0;
|
|
|
|
while (skipped + bufLen * 2 < length) {
|
2015-02-09 05:48:55 +01:00
|
|
|
try {
|
|
|
|
header = bitstream.readFrame();
|
2015-02-12 09:52:19 +01:00
|
|
|
if (header == null) {
|
|
|
|
// Log.warn("Mp3InputStream: skip: header is null.");
|
|
|
|
endOfStream = true;
|
2015-02-09 05:48:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-02-12 09:52:19 +01:00
|
|
|
|
|
|
|
// last frame that won't be skipped so better read it
|
|
|
|
if (skipped + bufLen * 2 * 4 >= length || bufLen <= 0) {
|
2015-02-09 05:48:55 +01:00
|
|
|
buf.clear_buffer();
|
|
|
|
decoder.decodeFrame(header, bitstream);
|
|
|
|
bufLen = buf.getBufferLength();
|
|
|
|
}
|
2015-02-12 09:52:19 +01:00
|
|
|
skipped += bufLen * 2 - bpos;
|
|
|
|
|
2015-02-09 05:48:55 +01:00
|
|
|
bitstream.closeFrame();
|
2015-02-12 09:52:19 +01:00
|
|
|
bpos = 0;
|
|
|
|
} catch (BitstreamException | DecoderException e) {
|
|
|
|
Log.error(e);
|
2015-02-09 05:48:55 +01:00
|
|
|
}
|
|
|
|
}
|
2015-02-12 09:52:19 +01:00
|
|
|
if (bufLen * 2 - bpos > length - skipped) {
|
|
|
|
bpos += length - skipped;
|
|
|
|
skipped += length - skipped;
|
2015-02-09 05:48:55 +01:00
|
|
|
}
|
2015-02-12 09:52:19 +01:00
|
|
|
|
|
|
|
return skipped;
|
2015-02-09 05:48:55 +01:00
|
|
|
}
|
2015-02-14 05:17:33 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() throws IOException {
|
|
|
|
try {
|
|
|
|
bitstream.close();
|
|
|
|
} catch (BitstreamException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2015-02-09 05:48:55 +01:00
|
|
|
}
|