improvements on visualizer in terms of synchronization
This commit is contained in:
@@ -14,8 +14,6 @@ public class DesktopLauncher {
|
||||
config.resizable = false;
|
||||
config.allowSoftwareMode = true;
|
||||
config.useHDPI = true;
|
||||
config.vSyncEnabled = false;
|
||||
config.foregroundFPS = 0;
|
||||
core = new RhythmBullet();
|
||||
core.setInitialScreen(new LoadingScreen(core));
|
||||
new LwjglApplication(core, config);
|
||||
|
@@ -92,8 +92,9 @@ public class Mp3Manager implements MusicManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playbackIndexUpdate() {
|
||||
public int playbackIndexUpdate() {
|
||||
playbackIndex = (int) (playbackMusic.getPosition() * sampleRate / readWindowSize);
|
||||
return playbackIndex;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -47,8 +47,9 @@ public class WAVManager implements MusicManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playbackIndexUpdate() {
|
||||
public int playbackIndexUpdate() {
|
||||
playbackIndex = (int) ((getSampleRate() * getDuration())/getReadWindowSize());
|
||||
return playbackIndex;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -29,18 +29,20 @@ public class Visualizer extends Widget implements Disposable {
|
||||
private int sourceID;
|
||||
private float visRefreshRate = 1f/60f;
|
||||
private float timer;
|
||||
private int readWindowIndex;
|
||||
public Visualizer() {
|
||||
vis = new BasicVisualizer();
|
||||
|
||||
try {
|
||||
Field bufferField = ClassReflection.getDeclaredField(OpenALMusic.class, "tempBuffer");
|
||||
bufferField.setAccessible(true);
|
||||
buffer = ((ByteBuffer) bufferField.get(null)).asShortBuffer();
|
||||
buffer = ((ByteBuffer) bufferField.get(null)).asShortBuffer().asReadOnlyBuffer();
|
||||
} catch (IllegalArgumentException | SecurityException | ReflectionException e) {
|
||||
e.printStackTrace();
|
||||
Gdx.app.debug("Visualizer reflection", "Failed attempt at retrieving tempBuffer field.");
|
||||
Gdx.app.exit();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -79,21 +81,31 @@ public class Visualizer extends Widget implements Disposable {
|
||||
|
||||
public void calcPCMData() {
|
||||
short chanVal;
|
||||
|
||||
int pos = (int) ((alGetSourcef(sourceID, AL11.AL_SAMPLE_OFFSET)));
|
||||
try {
|
||||
buffer.position(pos);
|
||||
} catch (IllegalArgumentException outOfBounds) {
|
||||
System.out.println(outOfBounds);
|
||||
if (mm.playbackIndexUpdate() != readWindowIndex) {
|
||||
updateBufferPosition();
|
||||
}
|
||||
|
||||
for (int sid = 0; sid < vis.getAudioPCM().length && sid < buffer.remaining(); sid++) {
|
||||
for (int channel = 0; channel < mm.getChannelCount(); channel ++) {
|
||||
if (vis.getAudioPCM()[sid] < (chanVal = buffer.get())) {
|
||||
vis.getAudioPCM()[sid] = chanVal;
|
||||
}
|
||||
}
|
||||
vis.getAudioPCM()[sid] /= Short.MAX_VALUE+1;
|
||||
vis.getAudioPCM()[sid] /= Short.MAX_VALUE+1f;
|
||||
}
|
||||
readWindowIndex++;
|
||||
}
|
||||
|
||||
private int updateBufferPosition() {
|
||||
int pos = (int) ((alGetSourcef(sourceID, AL11.AL_SAMPLE_OFFSET))-mm.getReadWindowSize()*mm.getChannelCount());
|
||||
try {
|
||||
readWindowIndex = mm.getPlaybackIndexPosition();
|
||||
buffer.position(pos);
|
||||
} catch (IllegalArgumentException iae) {
|
||||
buffer.position(0);
|
||||
Gdx.app.error("BufferPosition", iae + " position was " + pos);
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
public void setMM(MusicManager mm) {
|
||||
@@ -102,7 +114,6 @@ public class Visualizer extends Widget implements Disposable {
|
||||
sourceIDField.setAccessible(true);
|
||||
sourceID = (int) sourceIDField.get(mm.getMusic());
|
||||
} catch (ReflectionException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.mm = mm;
|
||||
|
Reference in New Issue
Block a user