re-organized visualizer setup for future ease of use

This commit is contained in:
2018-02-04 22:36:16 -06:00
parent 7f51ca2c55
commit b9f70e0c9d
6 changed files with 324 additions and 299 deletions

View File

@@ -1,224 +0,0 @@
package zero1hd.rhythmbullet.audio.visualizer;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import zero1hd.rhythmbullet.audio.MusicManager;
public class BasicVisualizer extends VisualizerCore {
private Pixmap pixmap;
private Texture barTexture;
private int barWidth;
private int binsPerBar;
private int spaceBetweenBars;
private Sprite[] bars;
private float[] barHeights;
private int smoothRange;
private float barHeightMultiplier;
private float rotation;
private Vector2 angleRot;
private boolean flip;
private Array<MirrorVisualizer> mirrors;
private boolean reverse;
private float maxAvgHeight;
private float currentAvg;
public BasicVisualizer() {
super(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/2, 0, 0);
mirrors = new Array<>();
pixmap = new Pixmap(2, 2, Format.RGBA8888);
pixmap.setColor(Color.WHITE);
pixmap.fill();
barCount = 70;
smoothRange = 3;
angleRot = new Vector2(MathUtils.cosDeg(rotation), MathUtils.sinDeg(rotation));
bars = new Sprite[barCount];
barHeights = new float[barCount];
barTexture = new Texture(pixmap);
for (int i = 0; i < bars.length; i++) {
bars[i] = new Sprite(barTexture);
}
updatePositionInfo();
pixmap.dispose();
}
@Override
public void render(Batch batch, float parentAlpha) {
if (mm != null) {
for (int i = 0; i < bars.length; i++) {
bars[i].draw(batch);
for (int j = 0; j < mirrors.size; j++) {
mirrors.get(j).render(i, batch, parentAlpha, bars);
}
}
}
super.render(batch, parentAlpha);
}
public void update(float delta) {
//Averaging bins together
for (int i = 0; i < barCount; i++) {
float barHeight = 0;
for (int j = 0; j < binsPerBar; j++) {
barHeight += Math.abs(audioPCM[j+i*binsPerBar +1]);
}
barHeight /= binsPerBar;
barHeight *= barHeightMultiplier;
barHeights[i] = barHeight;
}
currentAvg = 0;
for (int i = 0; i < barCount; i++) {
int avg = 0;
//Averaging the bars
for (int range = 0; range < smoothRange; range++) {
if (i+range < barCount) {
avg += barHeights[i+range];
}
if (i-range >= 0) {
avg += barHeights[i-range];
}
}
avg /= smoothRange*2;
barHeights[i] = avg;
currentAvg += barHeights[i];
if (bars[i].getHeight() > barHeights[i]) {
bars[i].setSize(barWidth, bars[i].getHeight() - (15f*delta*(bars[i].getHeight()-barHeights[i])));
} else {
bars[i].setSize(barWidth, bars[i].getHeight() + (20f*delta*(barHeights[i] - bars[i].getHeight())));
}
}
currentAvg /= barHeights.length;
if (currentAvg > maxAvgHeight) {
maxAvgHeight = currentAvg;
}
}
@Override
public void setMM(MusicManager mm) {
maxAvgHeight = 0;
currentAvg = 0;
float validBins = (1700/((mm.getSampleRate()/2)/((mm.getReadWindowSize()/2)+1)));
Gdx.app.debug("Visualizer", "valid frequency bins " + validBins);
binsPerBar = MathUtils.round((validBins/barCount));
barHeights = new float[barCount];
super.setMM(mm);
}
@Override
public void dispose() {
barTexture.dispose();
super.dispose();
}
public void updatePositionInfo() {
barHeightMultiplier = height*0.03f;
int barSpace = 0;
angleRot.set(MathUtils.cosDeg(rotation), MathUtils.sinDeg(rotation));
barWidth = MathUtils.round((float) width/(float) barCount);
barWidth -= spaceBetweenBars;
for (int i = 0; i < bars.length; i++) {
barSpace = i*(barWidth+spaceBetweenBars);
if (flip) {
bars[i].setRotation(rotation+180);
} else {
bars[i].setRotation(rotation);
}
if (reverse) {
bars[bars.length-i-1].setPosition(xPos + barSpace*angleRot.x, yPos + barSpace*angleRot.y);
} else {
bars[i].setPosition(xPos + barSpace*angleRot.x, yPos + barSpace*angleRot.y);
}
for (int mirrorIndex = 0; mirrorIndex < mirrors.size; mirrorIndex++) {
mirrors.get(mirrorIndex).position(i, barWidth, spaceBetweenBars);
}
}
}
public float getActualWidth() {
return (barWidth+spaceBetweenBars)*(barCount - 1);
}
public void setColor(Color color) {
for (int i = 0; i < bars.length; i++) {
bars[i].setColor(color);
}
for (int i = 0; i < mirrors.size; i++) {
mirrors.get(i).setColor(color);
}
}
public void setColor(float r, float g, float b, float a) {
for (int i = 0; i < bars.length; i++) {
bars[i].setColor(r, g, b, a);
}
for (int i = 0; i < mirrors.size; i++) {
mirrors.get(i).setColor(r, g, b, a);
}
}
public void setRotation(float rotation) {
this.rotation = rotation;
}
public void flip() {
flip = !flip;
}
public boolean isFlipped() {
return flip;
}
public void addMirrorVisualizer(MirrorVisualizer mirror) {
updatePositionInfo();
mirror.setup(bars, xPos, yPos, rotation);
mirrors.add(mirror);
setxPos(((getWidth() - getActualWidth())/2f));
}
public void removeMirrorVisualizer(MirrorVisualizer mirror) {
mirrors.removeValue(mirror, true);
}
public void setSpaceBetweenBars(int spaceBetweenBars) {
this.spaceBetweenBars = spaceBetweenBars;
}
public int getBarWidth() {
return barWidth;
}
public void reverse() {
reverse = reverse ? false : true;
}
public boolean isReversed() {
return reverse;
}
public Sprite[] getBars() {
return bars;
}
public float getCurrentAvg() {
return currentAvg;
}
public float getMaxAvgHeight() {
return maxAvgHeight;
}
public int getSpaceBetweenBars() {
return spaceBetweenBars;
}
}

View File

@@ -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;

View File

@@ -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;
}