diff --git a/core/src/zero1hd/rhythmbullet/audio/Mp3Manager.java b/core/src/zero1hd/rhythmbullet/audio/Mp3Manager.java index 4078ba7..29e8491 100755 --- a/core/src/zero1hd/rhythmbullet/audio/Mp3Manager.java +++ b/core/src/zero1hd/rhythmbullet/audio/Mp3Manager.java @@ -3,6 +3,8 @@ package zero1hd.rhythmbullet.audio; import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantLock; import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException; import org.jaudiotagger.audio.exceptions.ReadOnlyFileException; @@ -32,7 +34,7 @@ public class Mp3Manager implements MusicManager { private int sampleRate; private long sampleCount; - private float durationInSeconds; + private double durationInSeconds; private byte channels; Bitstream bitstream; MP3Decoder decoder; @@ -41,22 +43,29 @@ public class Mp3Manager implements MusicManager { private byte[] workset; private int indexHead = -1; + ReentrantLock lock = new ReentrantLock(); + private ExecutorService exec; - private boolean loadComplete; public Mp3Manager(FileHandle audioFile) { + lock = new ReentrantLock(); + exec = Executors.newSingleThreadExecutor(); exec.submit(() -> { + lock.lock(); try { MP3File mp3File = new MP3File(audioFile.file()); - sampleCount = MathUtils.round((float) (mp3File.getAudioHeader().getSampleRateAsNumber()*mp3File.getMP3AudioHeader().getPreciseTrackLength())); - durationInSeconds = mp3File.getMP3AudioHeader().getNumberOfFrames()/readWindowSize; + sampleCount = MathUtils.round(Float.valueOf(String.valueOf((mp3File.getAudioHeader().getSampleRateAsNumber()*mp3File.getMP3AudioHeader().getPreciseTrackLength())))); sampleRate = mp3File.getMP3AudioHeader().getSampleRateAsNumber(); + durationInSeconds = mp3File.getMP3AudioHeader().getPreciseTrackLength(); + } catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) { e.printStackTrace(); + } finally { + lock.unlock(); } - loadComplete = true; + }); bitstream = new Bitstream(audioFile.read()); @@ -111,7 +120,7 @@ public class Mp3Manager implements MusicManager { @Override public float getDuration() { - return durationInSeconds; + return Float.valueOf(String.valueOf(durationInSeconds)); } @Override @@ -222,6 +231,11 @@ public class Mp3Manager implements MusicManager { @Override public boolean isFinishedLoading() { - return loadComplete; + try { + return lock.tryLock(0, TimeUnit.SECONDS); + } catch (InterruptedException e) { + e.printStackTrace(); + return false; + } } } diff --git a/core/src/zero1hd/rhythmbullet/graphics/ui/components/Visualizer.java b/core/src/zero1hd/rhythmbullet/graphics/ui/components/Visualizer.java index 1697e09..0ac6f60 100755 --- a/core/src/zero1hd/rhythmbullet/graphics/ui/components/Visualizer.java +++ b/core/src/zero1hd/rhythmbullet/graphics/ui/components/Visualizer.java @@ -1,5 +1,6 @@ package zero1hd.rhythmbullet.graphics.ui.components; +import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.scenes.scene2d.ui.Widget; @@ -25,9 +26,13 @@ public class Visualizer extends Widget { @Override public void act(float delta) { if (mm != null && mm.isFinishedLoading() && !mmSet) { + Gdx.app.debug("Visualizer", "\nsample count: " + mm.getSampleCount() + + "\nDuration in seconds: " + mm.getDuration() + + "\nSample rate: " + mm.getSampleRate()); vis.setMM(mm); mmSet = true; } + vis.modify(delta); vis.setHeight(getHeight()); vis.setWidth(getWidth());