progress on asynchronous song property loading

This commit is contained in:
Harrison Deng 2017-10-06 15:59:02 -05:00
parent 99dbbb41f7
commit 8962ab97eb
7 changed files with 55 additions and 24 deletions

View File

@ -1,6 +1,8 @@
package zero1hd.rhythmbullet.audio; package zero1hd.rhythmbullet.audio;
import java.io.IOException; 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.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException; import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
@ -39,15 +41,23 @@ public class Mp3Manager implements MusicManager {
private byte[] workset; private byte[] workset;
private int indexHead = -1; private int indexHead = -1;
private ExecutorService exec;
private boolean loadComplete;
public Mp3Manager(FileHandle audioFile) { public Mp3Manager(FileHandle audioFile) {
try { exec = Executors.newSingleThreadExecutor();
MP3File mp3File = new MP3File(audioFile.file()); exec.submit(() -> {
sampleCount = MathUtils.round((float) (mp3File.getAudioHeader().getSampleRateAsNumber()*mp3File.getMP3AudioHeader().getPreciseTrackLength())); try {
durationInSeconds = mp3File.getMP3AudioHeader().getNumberOfFrames()/readWindowSize; MP3File mp3File = new MP3File(audioFile.file());
sampleRate = mp3File.getMP3AudioHeader().getSampleRateAsNumber(); sampleCount = MathUtils.round((float) (mp3File.getAudioHeader().getSampleRateAsNumber()*mp3File.getMP3AudioHeader().getPreciseTrackLength()));
} catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) { durationInSeconds = mp3File.getMP3AudioHeader().getNumberOfFrames()/readWindowSize;
e.printStackTrace(); sampleRate = mp3File.getMP3AudioHeader().getSampleRateAsNumber();
} } catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) {
e.printStackTrace();
}
loadComplete = true;
});
bitstream = new Bitstream(audioFile.read()); bitstream = new Bitstream(audioFile.read());
decoder = new MP3Decoder(); decoder = new MP3Decoder();
@ -91,6 +101,7 @@ public class Mp3Manager implements MusicManager {
public void dispose() { public void dispose() {
playbackMusic.stop(); playbackMusic.stop();
playbackMusic.dispose(); playbackMusic.dispose();
exec.shutdown();
try { try {
bitstream.close(); bitstream.close();
} catch (BitstreamException e) { } catch (BitstreamException e) {
@ -121,9 +132,8 @@ public class Mp3Manager implements MusicManager {
samples[sid] /= Short.MAX_VALUE+1; samples[sid] /= Short.MAX_VALUE+1;
} }
} }
if (framesRead != 0) { currentReadWindowIndex++;
currentReadWindowIndex++;
}
return framesRead; return framesRead;
} }
@ -209,4 +219,9 @@ public class Mp3Manager implements MusicManager {
public void setVolume(float percent) { public void setVolume(float percent) {
playbackMusic.setVolume(percent); playbackMusic.setVolume(percent);
} }
@Override
public boolean isFinishedLoading() {
return loadComplete;
}
} }

View File

@ -68,4 +68,10 @@ public interface MusicManager extends Disposable {
public void setOnCompletionListener(OnCompletionListener listener); public void setOnCompletionListener(OnCompletionListener listener);
public void setVolume(float percent); public void setVolume(float percent);
/**
* If the the properties of the song are done loading
* @return whether its done loading
*/
public boolean isFinishedLoading();
} }

View File

@ -19,7 +19,6 @@ public class WAVManager implements MusicManager {
private int currentReadWindowIndex; private int currentReadWindowIndex;
private Music playbackMusic; private Music playbackMusic;
WavDecoder decoder; WavDecoder decoder;
public WAVManager(FileHandle file) { public WAVManager(FileHandle file) {
try { try {
decoder = new WavDecoder(file); decoder = new WavDecoder(file);
@ -122,4 +121,9 @@ public class WAVManager implements MusicManager {
public void setVolume(float percent) { public void setVolume(float percent) {
playbackMusic.setVolume(percent); playbackMusic.setVolume(percent);
} }
@Override
public boolean isFinishedLoading() {
return true;
}
} }

View File

@ -95,9 +95,9 @@ public class BasicVisualizer extends VisualizerCore {
} }
@Override @Override
public void setMM(MusicManager mdp) { public void setMM(MusicManager mm) {
super.setMM(mdp); super.setMM(mm);
float validBins = (5000/((mdp.getSampleRate()/2)/((audioPCM.length/2)+1))); float validBins = (5000/((mm.getSampleRate()/2)/((audioPCM.length/2)+1)));
Gdx.app.debug("Visualizer", "valid frequency bins " + validBins); Gdx.app.debug("Visualizer", "valid frequency bins " + validBins);
binsPerBar = MathUtils.round((validBins/barCount)); binsPerBar = MathUtils.round((validBins/barCount));
} }

View File

@ -24,11 +24,9 @@ public class VisualizerCore implements Disposable {
public void calculate() { public void calculate() {
if (mm != null) { if (mm != null) {
mm.playbackIndexUpdate(); mm.playbackIndexUpdate();
if (mm.getPlaybackIndexPosition() > mm.getCurrentReadWindowIndex()) { while (mm.isPlaying() && mm.getPlaybackIndexPosition() > mm.getCurrentReadWindowIndex()) {
while (mm.getPlaybackIndexPosition() > mm.getCurrentReadWindowIndex()) { mm.readSamples(audioPCM);
mm.readSamples(audioPCM); fft.realForward(audioPCM);
fft.realForward(audioPCM);
}
} }
} }
} }

View File

@ -10,6 +10,8 @@ import zero1hd.rhythmbullet.audio.visualizer.BasicVisualizer;
public class Visualizer extends Widget { public class Visualizer extends Widget {
private BasicVisualizer vis; private BasicVisualizer vis;
private boolean updatePositioning = true; private boolean updatePositioning = true;
private boolean mmSet;
private MusicManager mm;
public Visualizer() { public Visualizer() {
vis = new BasicVisualizer(); vis = new BasicVisualizer();
} }
@ -32,12 +34,18 @@ public class Visualizer extends Widget {
vis.updatePositionInfo(); vis.updatePositionInfo();
} }
vis.calculate(); vis.calculate();
if (mm != null && mm.isFinishedLoading() && !mmSet) {
vis.setMM(mm);
mmSet = true;
}
super.act(delta); super.act(delta);
} }
public void setMDP(MusicManager mdp) { public void setMM(MusicManager mm) {
vis.setMM(mdp); this.mm = mm;
mmSet = false;
} }
@Override @Override

View File

@ -22,7 +22,7 @@ public class MainPage extends Page implements OnDifferentSongListener {
titleBar = new TitleBarVisualizer(core.getAssetManager(), camera); titleBar = new TitleBarVisualizer(core.getAssetManager(), camera);
addActor(titleBar); addActor(titleBar);
titleBar.getHvisual().setMDP(sc.getCurrentSong()); titleBar.getHvisual().setMM(sc.getCurrentSong());
sc.addOnDifferentSongListener(this); sc.addOnDifferentSongListener(this);
@ -44,7 +44,7 @@ public class MainPage extends Page implements OnDifferentSongListener {
@Override @Override
public void onDifferentSong(MusicManager mdp) { public void onDifferentSong(MusicManager mdp) {
titleBar.getHvisual().setMDP(mdp); titleBar.getHvisual().setMM(mdp);
} }
@Override @Override