improvements on visualizer in terms of synchronization

This commit is contained in:
Harrison Deng 2018-01-10 23:07:55 -06:00
parent df56545408
commit 03966d124a
6 changed files with 28 additions and 16 deletions

View File

@ -38,7 +38,7 @@ public class BasicVisualizer extends VisualizerCore {
pixmap.setColor(Color.WHITE); pixmap.setColor(Color.WHITE);
pixmap.fill(); pixmap.fill();
barCount = 70; barCount = 70;
smoothRange = 2; smoothRange = 3;
angleRot = new Vector2(MathUtils.cosDeg(rotation), MathUtils.sinDeg(rotation)); angleRot = new Vector2(MathUtils.cosDeg(rotation), MathUtils.sinDeg(rotation));
bars = new Sprite[barCount]; bars = new Sprite[barCount];
barHeights = new float[barCount]; barHeights = new float[barCount];
@ -106,7 +106,7 @@ public class BasicVisualizer extends VisualizerCore {
public void setMM(MusicManager mm) { public void setMM(MusicManager mm) {
maxAvgHeight = 0; maxAvgHeight = 0;
currentAvg = 0; currentAvg = 0;
float validBins = (5500/((mm.getSampleRate()/2)/((mm.getReadWindowSize()/2)+1))); float validBins = (6400/((mm.getSampleRate()/2)/((mm.getReadWindowSize()/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));
barHeights = new float[barCount]; barHeights = new float[barCount];

View File

@ -10,8 +10,9 @@ public interface MusicManager extends Disposable {
/** /**
* sets a integer variable to the current window of audio data the playback is at. * sets a integer variable to the current window of audio data the playback is at.
* Useful for efficiency because we compute once for that frame then get the values everytime it is required instead of calculating every time we get the index. * Useful for efficiency because we compute once for that frame then get the values everytime it is required instead of calculating every time we get the index.
* @return the playback window index.
*/ */
public void playbackIndexUpdate(); public int playbackIndexUpdate();
/** /**
* Gets the current position in seconds * Gets the current position in seconds

View File

@ -14,8 +14,6 @@ public class DesktopLauncher {
config.resizable = false; config.resizable = false;
config.allowSoftwareMode = true; config.allowSoftwareMode = true;
config.useHDPI = true; config.useHDPI = true;
config.vSyncEnabled = false;
config.foregroundFPS = 0;
core = new RhythmBullet(); core = new RhythmBullet();
core.setInitialScreen(new LoadingScreen(core)); core.setInitialScreen(new LoadingScreen(core));
new LwjglApplication(core, config); new LwjglApplication(core, config);

View File

@ -92,8 +92,9 @@ public class Mp3Manager implements MusicManager {
} }
@Override @Override
public void playbackIndexUpdate() { public int playbackIndexUpdate() {
playbackIndex = (int) (playbackMusic.getPosition() * sampleRate / readWindowSize); playbackIndex = (int) (playbackMusic.getPosition() * sampleRate / readWindowSize);
return playbackIndex;
} }
@Override @Override

View File

@ -47,8 +47,9 @@ public class WAVManager implements MusicManager {
} }
@Override @Override
public void playbackIndexUpdate() { public int playbackIndexUpdate() {
playbackIndex = (int) ((getSampleRate() * getDuration())/getReadWindowSize()); playbackIndex = (int) ((getSampleRate() * getDuration())/getReadWindowSize());
return playbackIndex;
} }
@Override @Override

View File

@ -29,18 +29,20 @@ public class Visualizer extends Widget implements Disposable {
private int sourceID; private int sourceID;
private float visRefreshRate = 1f/60f; private float visRefreshRate = 1f/60f;
private float timer; private float timer;
private int readWindowIndex;
public Visualizer() { public Visualizer() {
vis = new BasicVisualizer(); vis = new BasicVisualizer();
try { try {
Field bufferField = ClassReflection.getDeclaredField(OpenALMusic.class, "tempBuffer"); Field bufferField = ClassReflection.getDeclaredField(OpenALMusic.class, "tempBuffer");
bufferField.setAccessible(true); bufferField.setAccessible(true);
buffer = ((ByteBuffer) bufferField.get(null)).asShortBuffer(); buffer = ((ByteBuffer) bufferField.get(null)).asShortBuffer().asReadOnlyBuffer();
} catch (IllegalArgumentException | SecurityException | ReflectionException e) { } catch (IllegalArgumentException | SecurityException | ReflectionException e) {
e.printStackTrace(); e.printStackTrace();
Gdx.app.debug("Visualizer reflection", "Failed attempt at retrieving tempBuffer field."); Gdx.app.debug("Visualizer reflection", "Failed attempt at retrieving tempBuffer field.");
Gdx.app.exit(); Gdx.app.exit();
} }
} }
@Override @Override
@ -79,21 +81,31 @@ public class Visualizer extends Widget implements Disposable {
public void calcPCMData() { public void calcPCMData() {
short chanVal; short chanVal;
if (mm.playbackIndexUpdate() != readWindowIndex) {
int pos = (int) ((alGetSourcef(sourceID, AL11.AL_SAMPLE_OFFSET))); updateBufferPosition();
try {
buffer.position(pos);
} catch (IllegalArgumentException outOfBounds) {
System.out.println(outOfBounds);
} }
for (int sid = 0; sid < vis.getAudioPCM().length && sid < buffer.remaining(); sid++) { for (int sid = 0; sid < vis.getAudioPCM().length && sid < buffer.remaining(); sid++) {
for (int channel = 0; channel < mm.getChannelCount(); channel ++) { for (int channel = 0; channel < mm.getChannelCount(); channel ++) {
if (vis.getAudioPCM()[sid] < (chanVal = buffer.get())) { if (vis.getAudioPCM()[sid] < (chanVal = buffer.get())) {
vis.getAudioPCM()[sid] = chanVal; 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) { public void setMM(MusicManager mm) {
@ -102,7 +114,6 @@ public class Visualizer extends Widget implements Disposable {
sourceIDField.setAccessible(true); sourceIDField.setAccessible(true);
sourceID = (int) sourceIDField.get(mm.getMusic()); sourceID = (int) sourceIDField.get(mm.getMusic());
} catch (ReflectionException e) { } catch (ReflectionException e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
this.mm = mm; this.mm = mm;