diff --git a/core/src/zero1hd/rhythmbullet/audio/Mp3AudioData.java b/core/src/zero1hd/rhythmbullet/audio/Mp3AudioData.java index cba4662..5e75862 100755 --- a/core/src/zero1hd/rhythmbullet/audio/Mp3AudioData.java +++ b/core/src/zero1hd/rhythmbullet/audio/Mp3AudioData.java @@ -28,11 +28,14 @@ public class Mp3AudioData implements AudioData { private int sampleRate; private long sampleCount; private float durationInSeconds; - + private byte channels; Bitstream bitstream; MP3Decoder decoder; OutputBuffer sampleBuffer; - Mp3.Music test; + private byte[] currentByteSet; + private byte[] workset; + private int indexHead = -1; + public Mp3AudioData(FileHandle audioFile) { try { MP3File mp3File = new MP3File(audioFile.file()); @@ -49,9 +52,10 @@ public class Mp3AudioData implements AudioData { try { Header header = bitstream.readFrame(); 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); decoder.setOutputBuffer(sampleBuffer); + workset = new byte[channels*2]; } catch (BitstreamException e) { throw new GdxRuntimeException("error while preloading mp3", e); } @@ -109,44 +113,65 @@ public class Mp3AudioData implements AudioData { @Override public int readSamples(float[] samples) { - int totalRead = 0; - + int framesRead = 0; for (int sid = 0; sid < samples.length; sid++) { - try { - Header header = bitstream.readFrame(); + for (int wsid = 0; wsid < workset.length; wsid++) { + 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 { decoder.decodeFrame(header, bitstream); } catch (ArrayIndexOutOfBoundsException | DecoderException e) { System.out.println(e); } + bitstream.closeFrame(); - sampleBuffer.reset(); - byte[] buffer = sampleBuffer.getBuffer(); + bytesRead = sampleBuffer.reset(); - samples[sid] += (buffer[1] << 8) + (buffer[0] & 0x00ff); - if (sampleBuffer.isStereo()) { - samples[sid] += (buffer[3] << 8) + (buffer[2] & 0x00ff); - samples[sid] /= 2; - } - totalRead++; - - samples[sid] /= Short.MAX_VALUE + 1; - } catch (BitstreamException e1) { - e1.printStackTrace(); + currentByteSet = sampleBuffer.getBuffer(); + } else { + currentByteSet = null; } + } catch (BitstreamException e1) { + e1.printStackTrace(); } - - return totalRead; + return bytesRead; } - + @Override public float getSampleRate() { return sampleRate; } - - - } diff --git a/core/src/zero1hd/wavedecoder/WavDecoder.java b/core/src/zero1hd/wavedecoder/WavDecoder.java index 21c5d88..3025d51 100755 --- a/core/src/zero1hd/wavedecoder/WavDecoder.java +++ b/core/src/zero1hd/wavedecoder/WavDecoder.java @@ -68,10 +68,10 @@ public class WavDecoder { samples[sampleID] /= 2; } framesRead ++; + + samples[sampleID] /= Short.MAX_VALUE+1; } - - samples[sampleID] /= Short.MAX_VALUE+1; } return framesRead;