From 473adee28174d3fe6fd88e71b083093d7589e96b Mon Sep 17 00:00:00 2001 From: Recrown Date: Sun, 28 May 2017 20:58:04 -0500 Subject: [PATCH] Revert "mp3 reading progress" This reverts commit 39baa6a0e2422c8c9620962e96c69ea142ab178f. --- build.gradle | 1 + .../zero1hd/polyjet/audio/Mp3AudioData.java | 88 +++++++++++-------- 2 files changed, 52 insertions(+), 37 deletions(-) diff --git a/build.gradle b/build.gradle index 59a1e5c..8900c00 100755 --- a/build.gradle +++ b/build.gradle @@ -84,6 +84,7 @@ project(":core") { compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion" compile "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion" + compile group: 'com.googlecode.soundlibs', name: 'mp3spi', version: '1.9.5-1' compile group: 'com.googlecode.soundlibs', name: 'jlayer', version: '1.0.1-1' compile "org.apache.commons:commons-math3:3.2" diff --git a/core/src/zero1hd/polyjet/audio/Mp3AudioData.java b/core/src/zero1hd/polyjet/audio/Mp3AudioData.java index 3953fd4..72bcc70 100755 --- a/core/src/zero1hd/polyjet/audio/Mp3AudioData.java +++ b/core/src/zero1hd/polyjet/audio/Mp3AudioData.java @@ -1,5 +1,6 @@ package zero1hd.polyjet.audio; +import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -18,44 +19,47 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.audio.Music; import com.badlogic.gdx.files.FileHandle; -import javazoom.jl.decoder.Bitstream; -import javazoom.jl.decoder.BitstreamException; import javazoom.jl.decoder.Decoder; -import javazoom.jl.decoder.DecoderException; -import javazoom.jl.decoder.SampleBuffer; +import javazoom.spi.mpeg.sampled.convert.DecodedMpegAudioInputStream; public class Mp3AudioData implements AudioData { private int readWindowSize = 1024; + private AudioInputStream din; + private AudioFormat decodedFormat; private Music playbackMusic; private int readIndex; private int sampleCount; + private AudioInputStream in; - private Bitstream bitStream; - Decoder decoder; + DecodedMpegAudioInputStream dStream; - private FileHandle audioFile; + Decoder decoder = new Decoder(); public Mp3AudioData(FileHandle audioFile) { - decoder = new Decoder(); - this.audioFile = audioFile; - bitStream = new Bitstream(audioFile.read()); try { sampleCount = (int) new MP3File(audioFile.file()).getMP3AudioHeader().getNumberOfFrames(); - - AudioInputStream in = AudioSystem.getAudioInputStream(audioFile.file()); - AudioFormat baseFormat = in.getFormat(); - decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, - baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false); - - in.close(); - in = null; - } catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException | UnsupportedAudioFileException e1) { + } catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e1) { e1.printStackTrace(); } + reset(); + + try { + File file = audioFile.file(); + in = AudioSystem.getAudioInputStream(file); + din = null; + AudioFormat baseFormat = in.getFormat(); + decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, + baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false); + din = AudioSystem.getAudioInputStream(decodedFormat, in); + } catch (UnsupportedAudioFileException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + playbackMusic = Gdx.audio.newMusic(audioFile); } @@ -75,13 +79,16 @@ public class Mp3AudioData implements AudioData { playbackMusic.stop(); playbackMusic.dispose(); playbackMusic = null; + } + + if (din != null) { try { - bitStream.close(); - } catch (BitstreamException e) { + din.close(); + in.close(); + } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } - bitStream = new Bitstream(audioFile.read()); } } @@ -104,23 +111,30 @@ public class Mp3AudioData implements AudioData { for (int currentSample = 0; currentSample < samples.length; currentSample++) { - try { - SampleBuffer sb = (SampleBuffer) decoder.decodeFrame(bitStream.readFrame(), bitStream); - bitStream.closeFrame(); - short[] sampleBuffer = sb.getBuffer(); - - for (int i = 0; i < sampleBuffer.length; i++) { - sampleAverage += sampleBuffer[i]; + for (int channel = 0; channel < decodedFormat.getChannels(); channel++) { + try { + int readCount = din.read(toBeConverted, 0, toBeConverted.length); + + ByteBuffer bBuffer = ByteBuffer.allocate(2); + bBuffer.order(ByteOrder.LITTLE_ENDIAN); + bBuffer.put(toBeConverted[0]); + bBuffer.put(toBeConverted[1]); + + sampleAverage += bBuffer.getShort(0); + + if (readCount == -1) { + break; + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } - - sampleAverage /= sampleBuffer.length * Short.MAX_VALUE + 1; - samples[currentSample] = sampleAverage; - sampleAverage = 0; - samplesRead++; - } catch (DecoderException | BitstreamException e) { - // TODO Auto-generated catch block - e.printStackTrace(); } + + sampleAverage /= decodedFormat.getChannels() * Short.MAX_VALUE + 1; + samples[currentSample] = sampleAverage; + sampleAverage = 0; + samplesRead++; } return samplesRead; }