basic mp3 support added
This commit is contained in:
parent
107b0e2553
commit
29225f167d
@ -28,11 +28,14 @@ public class Mp3AudioData implements AudioData {
|
|||||||
private int sampleRate;
|
private int sampleRate;
|
||||||
private long sampleCount;
|
private long sampleCount;
|
||||||
private float durationInSeconds;
|
private float durationInSeconds;
|
||||||
|
private byte channels;
|
||||||
Bitstream bitstream;
|
Bitstream bitstream;
|
||||||
MP3Decoder decoder;
|
MP3Decoder decoder;
|
||||||
OutputBuffer sampleBuffer;
|
OutputBuffer sampleBuffer;
|
||||||
Mp3.Music test;
|
private byte[] currentByteSet;
|
||||||
|
private byte[] workset;
|
||||||
|
private int indexHead = -1;
|
||||||
|
|
||||||
public Mp3AudioData(FileHandle audioFile) {
|
public Mp3AudioData(FileHandle audioFile) {
|
||||||
try {
|
try {
|
||||||
MP3File mp3File = new MP3File(audioFile.file());
|
MP3File mp3File = new MP3File(audioFile.file());
|
||||||
@ -49,9 +52,10 @@ public class Mp3AudioData implements AudioData {
|
|||||||
try {
|
try {
|
||||||
Header header = bitstream.readFrame();
|
Header header = bitstream.readFrame();
|
||||||
if (header == null) throw new GdxRuntimeException("Empty MP3");
|
if (header == null) throw new GdxRuntimeException("Empty MP3");
|
||||||
int channels = header.mode() == Header.SINGLE_CHANNEL ? 1 : 2;
|
channels = (byte) (header.mode() == Header.SINGLE_CHANNEL ? 1 : 2);
|
||||||
sampleBuffer = new OutputBuffer(channels, false);
|
sampleBuffer = new OutputBuffer(channels, false);
|
||||||
decoder.setOutputBuffer(sampleBuffer);
|
decoder.setOutputBuffer(sampleBuffer);
|
||||||
|
workset = new byte[channels*2];
|
||||||
} catch (BitstreamException e) {
|
} catch (BitstreamException e) {
|
||||||
throw new GdxRuntimeException("error while preloading mp3", e);
|
throw new GdxRuntimeException("error while preloading mp3", e);
|
||||||
}
|
}
|
||||||
@ -109,44 +113,65 @@ public class Mp3AudioData implements AudioData {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int readSamples(float[] samples) {
|
public int readSamples(float[] samples) {
|
||||||
int totalRead = 0;
|
int framesRead = 0;
|
||||||
|
|
||||||
for (int sid = 0; sid < samples.length; sid++) {
|
for (int sid = 0; sid < samples.length; sid++) {
|
||||||
try {
|
for (int wsid = 0; wsid < workset.length; wsid++) {
|
||||||
Header header = bitstream.readFrame();
|
workset[wsid] = nextByte();
|
||||||
|
}
|
||||||
|
if (currentByteSet != null) {
|
||||||
|
samples[sid] += (workset[1] << 8) + (workset[0] & 0x00ff);
|
||||||
|
if (channels > 1) {
|
||||||
|
samples[sid] += (workset[3] << 8) + (workset[2] & 0x00ff);
|
||||||
|
samples[sid] /= 2;
|
||||||
|
}
|
||||||
|
framesRead ++;
|
||||||
|
|
||||||
if (header == null) break;
|
samples[sid] /= Short.MAX_VALUE+1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return framesRead;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte nextByte() {
|
||||||
|
indexHead++;
|
||||||
|
if (currentByteSet == null || indexHead >= currentByteSet.length) {
|
||||||
|
loadNextBuffer();
|
||||||
|
if (currentByteSet == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
indexHead = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentByteSet[indexHead];
|
||||||
|
}
|
||||||
|
|
||||||
|
public int loadNextBuffer() {
|
||||||
|
int bytesRead = 0;
|
||||||
|
try {
|
||||||
|
Header header = bitstream.readFrame();
|
||||||
|
if (header != null) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
decoder.decodeFrame(header, bitstream);
|
decoder.decodeFrame(header, bitstream);
|
||||||
} catch (ArrayIndexOutOfBoundsException | DecoderException e) {
|
} catch (ArrayIndexOutOfBoundsException | DecoderException e) {
|
||||||
System.out.println(e);
|
System.out.println(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
bitstream.closeFrame();
|
bitstream.closeFrame();
|
||||||
sampleBuffer.reset();
|
bytesRead = sampleBuffer.reset();
|
||||||
byte[] buffer = sampleBuffer.getBuffer();
|
|
||||||
|
|
||||||
samples[sid] += (buffer[1] << 8) + (buffer[0] & 0x00ff);
|
currentByteSet = sampleBuffer.getBuffer();
|
||||||
if (sampleBuffer.isStereo()) {
|
} else {
|
||||||
samples[sid] += (buffer[3] << 8) + (buffer[2] & 0x00ff);
|
currentByteSet = null;
|
||||||
samples[sid] /= 2;
|
|
||||||
}
|
|
||||||
totalRead++;
|
|
||||||
|
|
||||||
samples[sid] /= Short.MAX_VALUE + 1;
|
|
||||||
} catch (BitstreamException e1) {
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
} catch (BitstreamException e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
|
return bytesRead;
|
||||||
return totalRead;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public float getSampleRate() {
|
public float getSampleRate() {
|
||||||
return sampleRate;
|
return sampleRate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -68,10 +68,10 @@ public class WavDecoder {
|
|||||||
samples[sampleID] /= 2;
|
samples[sampleID] /= 2;
|
||||||
}
|
}
|
||||||
framesRead ++;
|
framesRead ++;
|
||||||
|
|
||||||
|
samples[sampleID] /= Short.MAX_VALUE+1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
samples[sampleID] /= Short.MAX_VALUE+1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return framesRead;
|
return framesRead;
|
||||||
|
Loading…
Reference in New Issue
Block a user