visualizer synching improved

This commit is contained in:
Harrison Deng 2018-01-14 14:07:55 -06:00
parent b07c9ffd44
commit c91a08bc3b

View File

@ -25,7 +25,7 @@ public class Visualizer extends Widget implements Disposable {
private boolean updatePositioning = true;
private boolean mmSet;
private MusicManager mm;
private ShortBuffer prevBuffer;
private ShortBuffer playingBuffer;
private ShortBuffer compareBuffer;
private ShortBuffer buffer;
private int sourceID;
@ -87,15 +87,15 @@ public class Visualizer extends Widget implements Disposable {
int bufferPosOffset = 0;
if (mm.playbackIndexUpdate() != readWindowIndex) {
bufferPosOffset = updateBufferPosition();
if (bufferPosOffset < 0) {
prevBuffer.position(prevBuffer.limit()+bufferPosOffset);
if (bufferPosOffset < 0 && playingBuffer.limit()+bufferPosOffset > 0) {
playingBuffer.position(playingBuffer.limit()+bufferPosOffset);
}
}
for (int sid = 0; sid < vis.getAudioPCM().length && sid < buffer.remaining(); sid++) {
for (int channel = 0; channel < mm.getChannelCount(); channel ++) {
if (prevBuffer.position() < prevBuffer.limit()) {
if (vis.getAudioPCM()[sid] < (chanVal = prevBuffer.get())) {
if (playingBuffer.position() < playingBuffer.limit()) {
if (vis.getAudioPCM()[sid] < (chanVal = playingBuffer.get())) {
vis.getAudioPCM()[sid] = chanVal;
}
} else {
@ -128,12 +128,12 @@ public class Visualizer extends Widget implements Disposable {
}
private void bufferChanged() {
prevBuffer.clear();
prevBuffer.put(compareBuffer);
playingBuffer.clear();
playingBuffer.put(compareBuffer);
}
private int updateBufferPosition() {
int pos = (int) ((alGetSourcef(sourceID, AL11.AL_SAMPLE_OFFSET))-mm.getReadWindowSize()*mm.getChannelCount());
int pos = (int) ((alGetSourcef(sourceID, AL11.AL_SAMPLE_OFFSET))-buffer.limit()-mm.getChannelCount()*mm.getReadWindowSize());
try {
readWindowIndex = mm.getPlaybackIndexPosition()-1;
buffer.position(pos);
@ -154,10 +154,10 @@ public class Visualizer extends Widget implements Disposable {
int originalPos = buffer.position();
System.out.println(visRefreshRate);
prevBuffer = ShortBuffer.allocate(buffer.capacity());
playingBuffer = ShortBuffer.allocate(buffer.capacity());
buffer.rewind();
prevBuffer.put(buffer);
prevBuffer.flip();
playingBuffer.put(buffer);
playingBuffer.flip();
compareBuffer = ShortBuffer.allocate(buffer.capacity());
buffer.rewind();