From 8962ab97eb2c7cee89888aaec0b9849506fe9bc7 Mon Sep 17 00:00:00 2001 From: Recrown Date: Fri, 6 Oct 2017 15:59:02 -0500 Subject: [PATCH] progress on asynchronous song property loading --- .../rhythmbullet/audio/Mp3Manager.java | 37 +++++++++++++------ .../rhythmbullet/audio/MusicManager.java | 6 +++ .../rhythmbullet/audio/WAVManager.java | 6 ++- .../audio/visualizer/BasicVisualizer.java | 6 +-- .../audio/visualizer/VisualizerCore.java | 8 ++-- .../graphics/ui/components/Visualizer.java | 12 +++++- .../graphics/ui/pages/MainPage.java | 4 +- 7 files changed, 55 insertions(+), 24 deletions(-) diff --git a/core/src/zero1hd/rhythmbullet/audio/Mp3Manager.java b/core/src/zero1hd/rhythmbullet/audio/Mp3Manager.java index 4ab8127..4078ba7 100755 --- a/core/src/zero1hd/rhythmbullet/audio/Mp3Manager.java +++ b/core/src/zero1hd/rhythmbullet/audio/Mp3Manager.java @@ -1,6 +1,8 @@ package zero1hd.rhythmbullet.audio; import java.io.IOException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException; import org.jaudiotagger.audio.exceptions.ReadOnlyFileException; @@ -39,15 +41,23 @@ public class Mp3Manager implements MusicManager { private byte[] workset; private int indexHead = -1; + private ExecutorService exec; + private boolean loadComplete; + public Mp3Manager(FileHandle audioFile) { - try { - MP3File mp3File = new MP3File(audioFile.file()); - sampleCount = MathUtils.round((float) (mp3File.getAudioHeader().getSampleRateAsNumber()*mp3File.getMP3AudioHeader().getPreciseTrackLength())); - durationInSeconds = mp3File.getMP3AudioHeader().getNumberOfFrames()/readWindowSize; - sampleRate = mp3File.getMP3AudioHeader().getSampleRateAsNumber(); - } catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) { - e.printStackTrace(); - } + exec = Executors.newSingleThreadExecutor(); + exec.submit(() -> { + try { + MP3File mp3File = new MP3File(audioFile.file()); + sampleCount = MathUtils.round((float) (mp3File.getAudioHeader().getSampleRateAsNumber()*mp3File.getMP3AudioHeader().getPreciseTrackLength())); + durationInSeconds = mp3File.getMP3AudioHeader().getNumberOfFrames()/readWindowSize; + sampleRate = mp3File.getMP3AudioHeader().getSampleRateAsNumber(); + } catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) { + e.printStackTrace(); + } + + loadComplete = true; + }); bitstream = new Bitstream(audioFile.read()); decoder = new MP3Decoder(); @@ -91,6 +101,7 @@ public class Mp3Manager implements MusicManager { public void dispose() { playbackMusic.stop(); playbackMusic.dispose(); + exec.shutdown(); try { bitstream.close(); } catch (BitstreamException e) { @@ -121,9 +132,8 @@ public class Mp3Manager implements MusicManager { samples[sid] /= Short.MAX_VALUE+1; } } - if (framesRead != 0) { - currentReadWindowIndex++; - } + currentReadWindowIndex++; + return framesRead; } @@ -209,4 +219,9 @@ public class Mp3Manager implements MusicManager { public void setVolume(float percent) { playbackMusic.setVolume(percent); } + + @Override + public boolean isFinishedLoading() { + return loadComplete; + } } diff --git a/core/src/zero1hd/rhythmbullet/audio/MusicManager.java b/core/src/zero1hd/rhythmbullet/audio/MusicManager.java index 594fdf5..21cbc1e 100755 --- a/core/src/zero1hd/rhythmbullet/audio/MusicManager.java +++ b/core/src/zero1hd/rhythmbullet/audio/MusicManager.java @@ -68,4 +68,10 @@ public interface MusicManager extends Disposable { public void setOnCompletionListener(OnCompletionListener listener); public void setVolume(float percent); + + /** + * If the the properties of the song are done loading + * @return whether its done loading + */ + public boolean isFinishedLoading(); } diff --git a/core/src/zero1hd/rhythmbullet/audio/WAVManager.java b/core/src/zero1hd/rhythmbullet/audio/WAVManager.java index 008089c..dcbcf18 100755 --- a/core/src/zero1hd/rhythmbullet/audio/WAVManager.java +++ b/core/src/zero1hd/rhythmbullet/audio/WAVManager.java @@ -19,7 +19,6 @@ public class WAVManager implements MusicManager { private int currentReadWindowIndex; private Music playbackMusic; WavDecoder decoder; - public WAVManager(FileHandle file) { try { decoder = new WavDecoder(file); @@ -122,4 +121,9 @@ public class WAVManager implements MusicManager { public void setVolume(float percent) { playbackMusic.setVolume(percent); } + + @Override + public boolean isFinishedLoading() { + return true; + } } diff --git a/core/src/zero1hd/rhythmbullet/audio/visualizer/BasicVisualizer.java b/core/src/zero1hd/rhythmbullet/audio/visualizer/BasicVisualizer.java index b887a0d..9236ef7 100755 --- a/core/src/zero1hd/rhythmbullet/audio/visualizer/BasicVisualizer.java +++ b/core/src/zero1hd/rhythmbullet/audio/visualizer/BasicVisualizer.java @@ -95,9 +95,9 @@ public class BasicVisualizer extends VisualizerCore { } @Override - public void setMM(MusicManager mdp) { - super.setMM(mdp); - float validBins = (5000/((mdp.getSampleRate()/2)/((audioPCM.length/2)+1))); + public void setMM(MusicManager mm) { + super.setMM(mm); + float validBins = (5000/((mm.getSampleRate()/2)/((audioPCM.length/2)+1))); Gdx.app.debug("Visualizer", "valid frequency bins " + validBins); binsPerBar = MathUtils.round((validBins/barCount)); } diff --git a/core/src/zero1hd/rhythmbullet/audio/visualizer/VisualizerCore.java b/core/src/zero1hd/rhythmbullet/audio/visualizer/VisualizerCore.java index b110988..6b1bbd8 100755 --- a/core/src/zero1hd/rhythmbullet/audio/visualizer/VisualizerCore.java +++ b/core/src/zero1hd/rhythmbullet/audio/visualizer/VisualizerCore.java @@ -24,11 +24,9 @@ public class VisualizerCore implements Disposable { public void calculate() { if (mm != null) { mm.playbackIndexUpdate(); - if (mm.getPlaybackIndexPosition() > mm.getCurrentReadWindowIndex()) { - while (mm.getPlaybackIndexPosition() > mm.getCurrentReadWindowIndex()) { - mm.readSamples(audioPCM); - fft.realForward(audioPCM); - } + while (mm.isPlaying() && mm.getPlaybackIndexPosition() > mm.getCurrentReadWindowIndex()) { + mm.readSamples(audioPCM); + fft.realForward(audioPCM); } } } diff --git a/core/src/zero1hd/rhythmbullet/graphics/ui/components/Visualizer.java b/core/src/zero1hd/rhythmbullet/graphics/ui/components/Visualizer.java index 23ede89..225e6f1 100755 --- a/core/src/zero1hd/rhythmbullet/graphics/ui/components/Visualizer.java +++ b/core/src/zero1hd/rhythmbullet/graphics/ui/components/Visualizer.java @@ -10,6 +10,8 @@ import zero1hd.rhythmbullet.audio.visualizer.BasicVisualizer; public class Visualizer extends Widget { private BasicVisualizer vis; private boolean updatePositioning = true; + private boolean mmSet; + private MusicManager mm; public Visualizer() { vis = new BasicVisualizer(); } @@ -32,12 +34,18 @@ public class Visualizer extends Widget { vis.updatePositionInfo(); } vis.calculate(); + + if (mm != null && mm.isFinishedLoading() && !mmSet) { + vis.setMM(mm); + mmSet = true; + } super.act(delta); } - public void setMDP(MusicManager mdp) { - vis.setMM(mdp); + public void setMM(MusicManager mm) { + this.mm = mm; + mmSet = false; } @Override diff --git a/core/src/zero1hd/rhythmbullet/graphics/ui/pages/MainPage.java b/core/src/zero1hd/rhythmbullet/graphics/ui/pages/MainPage.java index e225311..90f3cc0 100755 --- a/core/src/zero1hd/rhythmbullet/graphics/ui/pages/MainPage.java +++ b/core/src/zero1hd/rhythmbullet/graphics/ui/pages/MainPage.java @@ -22,7 +22,7 @@ public class MainPage extends Page implements OnDifferentSongListener { titleBar = new TitleBarVisualizer(core.getAssetManager(), camera); addActor(titleBar); - titleBar.getHvisual().setMDP(sc.getCurrentSong()); + titleBar.getHvisual().setMM(sc.getCurrentSong()); sc.addOnDifferentSongListener(this); @@ -44,7 +44,7 @@ public class MainPage extends Page implements OnDifferentSongListener { @Override public void onDifferentSong(MusicManager mdp) { - titleBar.getHvisual().setMDP(mdp); + titleBar.getHvisual().setMM(mdp); } @Override