functional (i think?)

This commit is contained in:
Harrison Deng 2017-10-06 23:52:56 -05:00
parent 2801adbd6e
commit 087b709a02
2 changed files with 26 additions and 7 deletions

View File

@ -3,6 +3,8 @@ package zero1hd.rhythmbullet.audio;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; 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.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException; import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
@ -32,7 +34,7 @@ public class Mp3Manager implements MusicManager {
private int sampleRate; private int sampleRate;
private long sampleCount; private long sampleCount;
private float durationInSeconds; private double durationInSeconds;
private byte channels; private byte channels;
Bitstream bitstream; Bitstream bitstream;
MP3Decoder decoder; MP3Decoder decoder;
@ -41,22 +43,29 @@ public class Mp3Manager implements MusicManager {
private byte[] workset; private byte[] workset;
private int indexHead = -1; private int indexHead = -1;
ReentrantLock lock = new ReentrantLock();
private ExecutorService exec; private ExecutorService exec;
private boolean loadComplete;
public Mp3Manager(FileHandle audioFile) { public Mp3Manager(FileHandle audioFile) {
lock = new ReentrantLock();
exec = Executors.newSingleThreadExecutor(); exec = Executors.newSingleThreadExecutor();
exec.submit(() -> { exec.submit(() -> {
lock.lock();
try { try {
MP3File mp3File = new MP3File(audioFile.file()); MP3File mp3File = new MP3File(audioFile.file());
sampleCount = MathUtils.round((float) (mp3File.getAudioHeader().getSampleRateAsNumber()*mp3File.getMP3AudioHeader().getPreciseTrackLength())); sampleCount = MathUtils.round(Float.valueOf(String.valueOf((mp3File.getAudioHeader().getSampleRateAsNumber()*mp3File.getMP3AudioHeader().getPreciseTrackLength()))));
durationInSeconds = mp3File.getMP3AudioHeader().getNumberOfFrames()/readWindowSize;
sampleRate = mp3File.getMP3AudioHeader().getSampleRateAsNumber(); sampleRate = mp3File.getMP3AudioHeader().getSampleRateAsNumber();
durationInSeconds = mp3File.getMP3AudioHeader().getPreciseTrackLength();
} catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) { } catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
lock.unlock();
} }
loadComplete = true;
}); });
bitstream = new Bitstream(audioFile.read()); bitstream = new Bitstream(audioFile.read());
@ -111,7 +120,7 @@ public class Mp3Manager implements MusicManager {
@Override @Override
public float getDuration() { public float getDuration() {
return durationInSeconds; return Float.valueOf(String.valueOf(durationInSeconds));
} }
@Override @Override
@ -222,6 +231,11 @@ public class Mp3Manager implements MusicManager {
@Override @Override
public boolean isFinishedLoading() { public boolean isFinishedLoading() {
return loadComplete; try {
return lock.tryLock(0, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}
} }
} }

View File

@ -1,5 +1,6 @@
package zero1hd.rhythmbullet.graphics.ui.components; package zero1hd.rhythmbullet.graphics.ui.components;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.ui.Widget; import com.badlogic.gdx.scenes.scene2d.ui.Widget;
@ -25,9 +26,13 @@ public class Visualizer extends Widget {
@Override @Override
public void act(float delta) { public void act(float delta) {
if (mm != null && mm.isFinishedLoading() && !mmSet) { 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); vis.setMM(mm);
mmSet = true; mmSet = true;
} }
vis.modify(delta); vis.modify(delta);
vis.setHeight(getHeight()); vis.setHeight(getHeight());
vis.setWidth(getWidth()); vis.setWidth(getWidth());