re-organized visualizer setup for future ease of use
This commit is contained in:
parent
7f51ca2c55
commit
b9f70e0c9d
@ -17,7 +17,7 @@ public class MirrorVisualizer {
|
||||
rectCoordRot = new Vector2();
|
||||
}
|
||||
|
||||
protected void setup(Sprite[] bars, float xPos, float yPos, float rotation) {
|
||||
public void setup(Sprite[] bars, float xPos, float yPos, float rotation) {
|
||||
this.bars = new Sprite[bars.length];
|
||||
this.xPos = (int) xPos;
|
||||
this.yPos = (int) yPos;
|
||||
|
@ -8,21 +8,14 @@ import com.badlogic.gdx.utils.Disposable;
|
||||
import edu.emory.mathcs.jtransforms.fft.FloatFFT_1D;
|
||||
import zero1hd.rhythmbullet.audio.MusicManager;
|
||||
|
||||
public class VisualizerCore implements Disposable {
|
||||
public class MusicManagerFFT implements Disposable {
|
||||
protected MusicManager mm;
|
||||
private FloatFFT_1D fft;
|
||||
protected float width, height;
|
||||
protected float xPos, yPos;
|
||||
protected int barCount;
|
||||
private boolean calc;
|
||||
private ReentrantLock lock;
|
||||
protected float[] audioPCM;
|
||||
|
||||
public VisualizerCore(int width, int height, int x, int y) {
|
||||
this.height = height;
|
||||
this.width = width;
|
||||
this.xPos = x;
|
||||
this.yPos = y;
|
||||
public MusicManagerFFT() {
|
||||
lock = new ReentrantLock();
|
||||
}
|
||||
|
||||
@ -56,36 +49,6 @@ public class VisualizerCore implements Disposable {
|
||||
public void dispose() {
|
||||
}
|
||||
|
||||
public void setxPos(float xPos) {
|
||||
this.xPos = xPos;
|
||||
}
|
||||
|
||||
public void setyPos(float yPos) {
|
||||
this.yPos = yPos;
|
||||
}
|
||||
|
||||
public void setWidth(float width) {
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
public void setHeight(float height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public float getxPos() {
|
||||
return xPos;
|
||||
}
|
||||
public float getyPos() {
|
||||
return yPos;
|
||||
}
|
||||
|
||||
public float getWidth() {
|
||||
return width;
|
||||
}
|
||||
public float getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public MusicManager getMm() {
|
||||
return mm;
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package zero1hd.rhythmbullet.audio.visualizer;
|
||||
package zero1hd.rhythmbullet.desktop.audio.visualizer;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
@ -12,8 +12,9 @@ import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
|
||||
import zero1hd.rhythmbullet.audio.MusicManager;
|
||||
import zero1hd.rhythmbullet.audio.visualizer.MirrorVisualizer;
|
||||
|
||||
public class BasicVisualizer extends VisualizerCore {
|
||||
public class HorizontalVisualizer extends Visualizer {
|
||||
private Pixmap pixmap;
|
||||
private Texture barTexture;
|
||||
private int barWidth;
|
||||
@ -30,14 +31,23 @@ public class BasicVisualizer extends VisualizerCore {
|
||||
private boolean reverse;
|
||||
private float maxAvgHeight;
|
||||
private float currentAvg;
|
||||
private int barCount;
|
||||
private float width, height, x, y;
|
||||
|
||||
public BasicVisualizer() {
|
||||
super(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/2, 0, 0);
|
||||
public HorizontalVisualizer() {
|
||||
super();
|
||||
mirrors = new Array<>();
|
||||
pixmap = new Pixmap(2, 2, Format.RGBA8888);
|
||||
pixmap.setColor(Color.WHITE);
|
||||
pixmap.fill();
|
||||
barCount = 70;
|
||||
|
||||
width = Gdx.graphics.getWidth();
|
||||
height = Gdx.graphics.getHeight()/2f;
|
||||
x = 0;
|
||||
y = 0;
|
||||
|
||||
|
||||
smoothRange = 3;
|
||||
angleRot = new Vector2(MathUtils.cosDeg(rotation), MathUtils.sinDeg(rotation));
|
||||
bars = new Sprite[barCount];
|
||||
@ -65,6 +75,7 @@ public class BasicVisualizer extends VisualizerCore {
|
||||
}
|
||||
|
||||
public void update(float delta) {
|
||||
if (mm != null) {
|
||||
//Averaging bins together
|
||||
for (int i = 0; i < barCount; i++) {
|
||||
float barHeight = 0;
|
||||
@ -101,6 +112,7 @@ public class BasicVisualizer extends VisualizerCore {
|
||||
maxAvgHeight = currentAvg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMM(MusicManager mm) {
|
||||
@ -134,9 +146,9 @@ public class BasicVisualizer extends VisualizerCore {
|
||||
bars[i].setRotation(rotation);
|
||||
}
|
||||
if (reverse) {
|
||||
bars[bars.length-i-1].setPosition(xPos + barSpace*angleRot.x, yPos + barSpace*angleRot.y);
|
||||
bars[bars.length-i-1].setPosition(x + barSpace*angleRot.x, y + barSpace*angleRot.y);
|
||||
} else {
|
||||
bars[i].setPosition(xPos + barSpace*angleRot.x, yPos + barSpace*angleRot.y);
|
||||
bars[i].setPosition(x + barSpace*angleRot.x, y + barSpace*angleRot.y);
|
||||
}
|
||||
for (int mirrorIndex = 0; mirrorIndex < mirrors.size; mirrorIndex++) {
|
||||
mirrors.get(mirrorIndex).position(i, barWidth, spaceBetweenBars);
|
||||
@ -181,9 +193,9 @@ public class BasicVisualizer extends VisualizerCore {
|
||||
|
||||
public void addMirrorVisualizer(MirrorVisualizer mirror) {
|
||||
updatePositionInfo();
|
||||
mirror.setup(bars, xPos, yPos, rotation);
|
||||
mirror.setup(bars, x, y, rotation);
|
||||
mirrors.add(mirror);
|
||||
setxPos(((getWidth() - getActualWidth())/2f));
|
||||
x = (int) (((width - getActualWidth())/2f));
|
||||
}
|
||||
|
||||
public void removeMirrorVisualizer(MirrorVisualizer mirror) {
|
||||
@ -221,4 +233,36 @@ public class BasicVisualizer extends VisualizerCore {
|
||||
public int getSpaceBetweenBars() {
|
||||
return spaceBetweenBars;
|
||||
}
|
||||
|
||||
public void setX(float x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public float getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setY(float y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public float getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setWidth(float width) {
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
public float getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public void setHeight(float height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public float getHeight() {
|
||||
return height;
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package zero1hd.rhythmbullet.desktop.graphics.ui.components;
|
||||
package zero1hd.rhythmbullet.desktop.audio.visualizer;
|
||||
|
||||
import static org.lwjgl.openal.AL10.*;
|
||||
import static org.lwjgl.openal.AL10.alGetSourcef;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ShortBuffer;
|
||||
@ -9,32 +9,22 @@ import org.lwjgl.openal.AL11;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.backends.lwjgl.audio.OpenALMusic;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Widget;
|
||||
import com.badlogic.gdx.utils.Disposable;
|
||||
import com.badlogic.gdx.utils.reflect.ClassReflection;
|
||||
import com.badlogic.gdx.utils.reflect.Field;
|
||||
import com.badlogic.gdx.utils.reflect.ReflectionException;
|
||||
|
||||
import zero1hd.rhythmbullet.audio.MusicManager;
|
||||
import zero1hd.rhythmbullet.audio.visualizer.BasicVisualizer;
|
||||
import zero1hd.rhythmbullet.audio.visualizer.MusicManagerFFT;
|
||||
|
||||
public class Visualizer extends Widget implements Disposable {
|
||||
private BasicVisualizer vis;
|
||||
private boolean updatePositioning = true;
|
||||
private boolean mmSet;
|
||||
private MusicManager mm;
|
||||
public class Visualizer extends MusicManagerFFT {
|
||||
private ShortBuffer playingBuffer;
|
||||
private ShortBuffer compareBuffer;
|
||||
private ShortBuffer buffer;
|
||||
private int sourceID;
|
||||
private float visRefreshRate;
|
||||
private float timer;
|
||||
private int readWindowIndex;
|
||||
public Visualizer() {
|
||||
vis = new BasicVisualizer();
|
||||
|
||||
public Visualizer() {
|
||||
super();
|
||||
try {
|
||||
Field bufferField = ClassReflection.getDeclaredField(OpenALMusic.class, "tempBuffer");
|
||||
bufferField.setAccessible(true);
|
||||
@ -44,43 +34,6 @@ public class Visualizer extends Widget implements Disposable {
|
||||
Gdx.app.debug("Visualizer reflection", "Failed attempt at retrieving tempBuffer field.");
|
||||
Gdx.app.exit();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Batch batch, float parentAlpha) {
|
||||
vis.render(batch, parentAlpha);
|
||||
super.draw(batch, parentAlpha);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void act(float delta) {
|
||||
if (mm != null && mm.isFinishedLoading() && !mmSet) {
|
||||
vis.setMM(mm);
|
||||
visRefreshRate = mm.getReadWindowSize()/mm.getSampleRate();
|
||||
mmSet = true;
|
||||
Gdx.app.debug("Visualizer", "\nsample count: " + mm.getSampleCount()
|
||||
+ "\nDuration in seconds: " + mm.getDuration() +
|
||||
"\nSample rate: " + mm.getSampleRate() +
|
||||
"\nRefresh rate: " + visRefreshRate);
|
||||
}
|
||||
|
||||
vis.update(delta);
|
||||
updateVisualizerProperties();
|
||||
if (updatePositioning) {
|
||||
vis.updatePositionInfo();
|
||||
vis.setxPos((getWidth() - vis.getActualWidth())/2f);
|
||||
}
|
||||
if (mmSet) {
|
||||
if (timer >= visRefreshRate) {
|
||||
timer = 0;
|
||||
calcPCMData();
|
||||
vis.calculate(delta);
|
||||
} else {
|
||||
timer += delta;
|
||||
}
|
||||
}
|
||||
super.act(delta);
|
||||
}
|
||||
|
||||
public void calcPCMData() {
|
||||
@ -93,19 +46,19 @@ public class Visualizer extends Widget implements Disposable {
|
||||
}
|
||||
}
|
||||
|
||||
for (int sid = 0; sid < vis.getAudioPCM().length && sid < buffer.remaining(); sid++) {
|
||||
for (int sid = 0; sid < getAudioPCM().length && sid < buffer.remaining(); sid++) {
|
||||
for (int channel = 0; channel < mm.getChannelCount(); channel ++) {
|
||||
if (playingBuffer.position() < playingBuffer.limit()) {
|
||||
if (vis.getAudioPCM()[sid] < (chanVal = playingBuffer.get())) {
|
||||
vis.getAudioPCM()[sid] = chanVal;
|
||||
if (getAudioPCM()[sid] < (chanVal = playingBuffer.get())) {
|
||||
getAudioPCM()[sid] = chanVal;
|
||||
}
|
||||
} else {
|
||||
if (vis.getAudioPCM()[sid] < (chanVal = buffer.get())) {
|
||||
vis.getAudioPCM()[sid] = chanVal;
|
||||
if (getAudioPCM()[sid] < (chanVal = buffer.get())) {
|
||||
getAudioPCM()[sid] = chanVal;
|
||||
}
|
||||
}
|
||||
}
|
||||
vis.getAudioPCM()[sid] /= Short.MAX_VALUE+1f;
|
||||
getAudioPCM()[sid] /= Short.MAX_VALUE+1f;
|
||||
}
|
||||
readWindowIndex++;
|
||||
|
||||
@ -167,52 +120,7 @@ public class Visualizer extends Widget implements Disposable {
|
||||
|
||||
buffer.position(originalPos);
|
||||
this.mm = mm;
|
||||
mmSet = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColor(Color color) {
|
||||
vis.setColor(color);
|
||||
super.setColor(color);
|
||||
super.setMM(mm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColor(float r, float g, float b, float a) {
|
||||
vis.setColor(r, g, b, a);
|
||||
super.setColor(r, g, b, a);
|
||||
}
|
||||
|
||||
public void updateVisualizerProperties() {
|
||||
vis.setHeight(getHeight());
|
||||
vis.setWidth(getWidth());
|
||||
vis.setxPos(getX());
|
||||
vis.setyPos(getY());
|
||||
vis.setRotation(getRotation());
|
||||
}
|
||||
|
||||
public void setUpdatePositioning(boolean updatePositioning) {
|
||||
updateVisualPosition();
|
||||
this.updatePositioning = updatePositioning;
|
||||
}
|
||||
|
||||
public void updateVisualPosition() {
|
||||
updateVisualizerProperties();
|
||||
vis.updatePositionInfo();
|
||||
vis.setxPos(((vis.getWidth() - vis.getActualWidth())/2f));
|
||||
vis.updatePositionInfo();
|
||||
}
|
||||
|
||||
public boolean isUpdatePositioning() {
|
||||
return updatePositioning;
|
||||
}
|
||||
|
||||
public BasicVisualizer getVis() {
|
||||
return vis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
vis.dispose();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
package zero1hd.rhythmbullet.desktop.graphics.ui.components;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Widget;
|
||||
import com.badlogic.gdx.utils.Disposable;
|
||||
|
||||
import zero1hd.rhythmbullet.audio.MusicManager;
|
||||
import zero1hd.rhythmbullet.desktop.audio.visualizer.HorizontalVisualizer;
|
||||
|
||||
public class HorizontalVisualizerWidget extends Widget implements Disposable {
|
||||
private HorizontalVisualizer vis;
|
||||
private boolean updatePositioning = true;
|
||||
private MusicManager mm;
|
||||
private boolean initialLoad;
|
||||
private float visRefreshRate;
|
||||
private float timer;
|
||||
public HorizontalVisualizerWidget() {
|
||||
vis = new HorizontalVisualizer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Batch batch, float parentAlpha) {
|
||||
vis.render(batch, parentAlpha);
|
||||
super.draw(batch, parentAlpha);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void act(float delta) {
|
||||
if (mm != null && mm.isFinishedLoading() && !initialLoad) {
|
||||
visRefreshRate = mm.getReadWindowSize()/mm.getSampleRate();
|
||||
Gdx.app.debug("Visualizer", "\nsample count: " + mm.getSampleCount()
|
||||
+ "\nDuration in seconds: " + mm.getDuration() +
|
||||
"\nSample rate: " + mm.getSampleRate() +
|
||||
"\nRefresh rate: " + visRefreshRate);
|
||||
vis.setMM(mm);
|
||||
initialLoad = true;
|
||||
}
|
||||
|
||||
if (updatePositioning) {
|
||||
vis.updatePositionInfo();
|
||||
vis.setX((getWidth() - vis.getActualWidth())/2f);
|
||||
}
|
||||
|
||||
vis.update(delta);
|
||||
updateVisualizerProperties();
|
||||
if (mm != null && initialLoad) {
|
||||
if (timer >= visRefreshRate) {
|
||||
timer = 0;
|
||||
vis.calcPCMData();
|
||||
vis.calculate(delta);
|
||||
} else {
|
||||
timer += delta;
|
||||
}
|
||||
}
|
||||
super.act(delta);
|
||||
}
|
||||
|
||||
public void setMM(MusicManager mm) {
|
||||
initialLoad = false;
|
||||
this.mm = mm;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColor(Color color) {
|
||||
vis.setColor(color);
|
||||
super.setColor(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColor(float r, float g, float b, float a) {
|
||||
vis.setColor(r, g, b, a);
|
||||
super.setColor(r, g, b, a);
|
||||
}
|
||||
|
||||
public void updateVisualizerProperties() {
|
||||
vis.setHeight(getHeight());
|
||||
vis.setWidth(getWidth());
|
||||
vis.setX(getX());
|
||||
vis.setY(getY());
|
||||
vis.setRotation(getRotation());
|
||||
}
|
||||
|
||||
public void setUpdatePositioning(boolean updatePositioning) {
|
||||
updateVisualPosition();
|
||||
this.updatePositioning = updatePositioning;
|
||||
}
|
||||
|
||||
public void updateVisualPosition() {
|
||||
updateVisualizerProperties();
|
||||
vis.updatePositionInfo();
|
||||
vis.setX(((vis.getWidth() - vis.getActualWidth())/2f));
|
||||
vis.updatePositionInfo();
|
||||
}
|
||||
|
||||
public boolean isUpdatePositioning() {
|
||||
return updatePositioning;
|
||||
}
|
||||
|
||||
public HorizontalVisualizer getVis() {
|
||||
return vis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
vis.dispose();
|
||||
}
|
||||
|
||||
}
|
@ -19,7 +19,7 @@ import com.badlogic.gdx.utils.Disposable;
|
||||
import zero1hd.rhythmbullet.audio.visualizer.MirrorVisualizer;
|
||||
|
||||
public class TitleBarVisualizer extends Group implements Disposable {
|
||||
private Visualizer visual;
|
||||
private HorizontalVisualizerWidget visual;
|
||||
private MirrorVisualizer visual2;
|
||||
private Texture bgTexture;
|
||||
private Image bg;
|
||||
@ -32,7 +32,7 @@ public class TitleBarVisualizer extends Group implements Disposable {
|
||||
private float particleLimitTime;
|
||||
public TitleBarVisualizer(AssetManager assets) {
|
||||
if (assets == null) throw new NullPointerException("TitleBarVisualizer requires assets manager... ITS NULL YOU FOOL");
|
||||
visual = new Visualizer();
|
||||
visual = new HorizontalVisualizerWidget();
|
||||
visual.getVis().setSpaceBetweenBars(visual.getVis().getBarWidth()- 2);
|
||||
addActor(visual);
|
||||
|
||||
@ -110,7 +110,7 @@ public class TitleBarVisualizer extends Group implements Disposable {
|
||||
super.draw(batch, parentAlpha);
|
||||
}
|
||||
|
||||
public Visualizer getHvisual() {
|
||||
public HorizontalVisualizerWidget getHvisual() {
|
||||
return visual;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user