diff --git a/core/src/zero1hd/rhythmbullet/audio/visualizer/BasicVisualizer.java b/core/src/zero1hd/rhythmbullet/audio/visualizer/BasicVisualizer.java index a5a12fc..e55caf2 100755 --- a/core/src/zero1hd/rhythmbullet/audio/visualizer/BasicVisualizer.java +++ b/core/src/zero1hd/rhythmbullet/audio/visualizer/BasicVisualizer.java @@ -38,7 +38,7 @@ public class BasicVisualizer extends VisualizerCore { pixmap.setColor(Color.WHITE); pixmap.fill(); barCount = 70; - smoothRange = 2; + smoothRange = 3; angleRot = new Vector2(MathUtils.cosDeg(rotation), MathUtils.sinDeg(rotation)); bars = new Sprite[barCount]; barHeights = new float[barCount]; @@ -106,7 +106,7 @@ public class BasicVisualizer extends VisualizerCore { public void setMM(MusicManager mm) { maxAvgHeight = 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); binsPerBar = MathUtils.round((validBins/barCount)); barHeights = new float[barCount]; diff --git a/core/src/zero1hd/rhythmbullet/util/MusicManager.java b/core/src/zero1hd/rhythmbullet/util/MusicManager.java index e3bb40e..4f16a35 100755 --- a/core/src/zero1hd/rhythmbullet/util/MusicManager.java +++ b/core/src/zero1hd/rhythmbullet/util/MusicManager.java @@ -10,8 +10,9 @@ public interface MusicManager extends Disposable { /** * 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. + * @return the playback window index. */ - public void playbackIndexUpdate(); + public int playbackIndexUpdate(); /** * Gets the current position in seconds diff --git a/desktop/src/zero1hd/rhythmbullet/desktop/DesktopLauncher.java b/desktop/src/zero1hd/rhythmbullet/desktop/DesktopLauncher.java index 0f176ff..4f9712d 100755 --- a/desktop/src/zero1hd/rhythmbullet/desktop/DesktopLauncher.java +++ b/desktop/src/zero1hd/rhythmbullet/desktop/DesktopLauncher.java @@ -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); diff --git a/desktop/src/zero1hd/rhythmbullet/desktop/audio/Mp3Manager.java b/desktop/src/zero1hd/rhythmbullet/desktop/audio/Mp3Manager.java index b372def..f672af8 100755 --- a/desktop/src/zero1hd/rhythmbullet/desktop/audio/Mp3Manager.java +++ b/desktop/src/zero1hd/rhythmbullet/desktop/audio/Mp3Manager.java @@ -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 diff --git a/desktop/src/zero1hd/rhythmbullet/desktop/audio/WAVManager.java b/desktop/src/zero1hd/rhythmbullet/desktop/audio/WAVManager.java index 14beb23..9a3cb1f 100755 --- a/desktop/src/zero1hd/rhythmbullet/desktop/audio/WAVManager.java +++ b/desktop/src/zero1hd/rhythmbullet/desktop/audio/WAVManager.java @@ -47,8 +47,9 @@ public class WAVManager implements MusicManager { } @Override - public void playbackIndexUpdate() { + public int playbackIndexUpdate() { playbackIndex = (int) ((getSampleRate() * getDuration())/getReadWindowSize()); + return playbackIndex; } @Override diff --git a/desktop/src/zero1hd/rhythmbullet/desktop/graphics/ui/components/Visualizer.java b/desktop/src/zero1hd/rhythmbullet/desktop/graphics/ui/components/Visualizer.java index fb04aee..1e7287c 100755 --- a/desktop/src/zero1hd/rhythmbullet/desktop/graphics/ui/components/Visualizer.java +++ b/desktop/src/zero1hd/rhythmbullet/desktop/graphics/ui/components/Visualizer.java @@ -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;