visualizer should now be stable (with proper new buffer system)

This commit is contained in:
Harrison Deng 2018-05-18 01:25:02 -05:00
parent 874ef52bb9
commit 0fdc65796d
2 changed files with 11 additions and 14 deletions

View File

@ -9,7 +9,7 @@ import com.badlogic.gdx.utils.Disposable;
public interface MusicManager extends Disposable { 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 every time it is required instead of calculating every time we get the index.
* @return the playback window index. * @return the playback window index.
*/ */
public int playbackIndexUpdate(); public int playbackIndexUpdate();

View File

@ -10,6 +10,7 @@ import org.lwjgl.openal.AL11;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.audio.OpenALMusic; import com.badlogic.gdx.backends.lwjgl.audio.OpenALMusic;
import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.utils.reflect.ClassReflection; import com.badlogic.gdx.utils.reflect.ClassReflection;
import com.badlogic.gdx.utils.reflect.Field; import com.badlogic.gdx.utils.reflect.Field;
import com.badlogic.gdx.utils.reflect.ReflectionException; import com.badlogic.gdx.utils.reflect.ReflectionException;
@ -47,22 +48,18 @@ public class DesktopVisualizer extends MusicManagerFFT implements Visualizer {
int bufferPosOffset = 0; int bufferPosOffset = 0;
if (mm.playbackIndexUpdate() != readWindowIndex) { if (mm.playbackIndexUpdate() != readWindowIndex) {
bufferPosOffset = updateBufferPosition(); bufferPosOffset = updateBufferPosition();
if (bufferPosOffset < 0 && playingBuffer.limit()+bufferPosOffset > 0) { if (bufferPosOffset < 0) {
playingBuffer.position(playingBuffer.limit()+bufferPosOffset); playingBuffer.position(Math.max(playingBuffer.limit()+bufferPosOffset, 0));
} else {
buffer.position(bufferPosOffset);
} }
} }
for (int sid = 0; sid < getAudioPCM().length && sid < playingBuffer.remaining(); sid++) {
for (int sid = 0; sid < getAudioPCM().length && sid < buffer.remaining(); sid++) { getAudioPCM()[sid] = 0;
for (int channel = 0; channel < mm.getChannelCount(); channel ++) { for (int channel = 0; channel < mm.getChannelCount(); channel ++) {
if (playingBuffer.position() < playingBuffer.limit()) {
if (getAudioPCM()[sid] < (chanVal = playingBuffer.get())) { if (getAudioPCM()[sid] < (chanVal = playingBuffer.get())) {
getAudioPCM()[sid] = chanVal; getAudioPCM()[sid] = chanVal;
} }
} else {
if (getAudioPCM()[sid] < (chanVal = buffer.get())) {
getAudioPCM()[sid] = chanVal;
}
}
} }
getAudioPCM()[sid] /= Short.MAX_VALUE+1f; getAudioPCM()[sid] /= Short.MAX_VALUE+1f;
} }
@ -96,7 +93,7 @@ public class DesktopVisualizer extends MusicManagerFFT implements Visualizer {
//put the second portion into the first... //put the second portion into the first...
playingBuffer.put(playingBuffer.array(), buffer.capacity(), buffer.capacity()); playingBuffer.put(playingBuffer.array(), buffer.capacity(), buffer.capacity());
} }
//put the new buffer in the second portion. //put the new buffer into the remaining portion.
playingBuffer.put(compareBuffer); playingBuffer.put(compareBuffer);
} }