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;
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;
}
}

View File

@ -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();
}

View File

@ -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;
}
}

View File

@ -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));
}

View File

@ -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);
}
}
}

View File

@ -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

View File

@ -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