switched to using sprites for testing (in progress)

This commit is contained in:
Harrison Deng 2017-09-11 14:58:04 -05:00
parent ca84a4522e
commit 3e05e66a38

View File

@ -6,6 +6,7 @@ import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format; import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch; 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.MathUtils;
import zero1hd.rhythmbullet.audio.MusicDataPack; import zero1hd.rhythmbullet.audio.MusicDataPack;
@ -16,9 +17,11 @@ public class HorizontalVisualizer extends VisualizerCore {
private int barWidth; private int barWidth;
private int binsPerBar; private int binsPerBar;
private int spaceBetweenBars; private int spaceBetweenBars;
private int[] barHeights; private Sprite[] bars;
private int smoothRange; private int smoothRange;
private float barHeightMultiplier; private float barHeightMultiplier;
private float rotation;
public HorizontalVisualizer() { public HorizontalVisualizer() {
super(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/2, 0, 0); super(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/2, 0, 0);
barHeightMultiplier = Gdx.graphics.getHeight()*0.01f; barHeightMultiplier = Gdx.graphics.getHeight()*0.01f;
@ -32,34 +35,41 @@ public class HorizontalVisualizer extends VisualizerCore {
spaceBetweenBars = (barWidth-2); spaceBetweenBars = (barWidth-2);
barWidth -= spaceBetweenBars; barWidth -= spaceBetweenBars;
smoothRange = 4; smoothRange = 4;
barHeights = new int[barCount]; bars = new Sprite[barCount];
for (int i = 0; i < bars.length; i++) {
bars[i] = new Sprite(bar);
}
} }
@Override @Override
public void render(Batch batch, float parentAlpha) { public void render(Batch batch, float parentAlpha) {
if (cmi != null) { if (cmi != null) {
//Averaging bins together
for (int i = 0; i < barCount; i++) { for (int i = 0; i < barCount; i++) {
barHeights[i] = 0; bars[i].setSize(barWidth, 0);
float barHeight = 0;
for (int j = 0; j < binsPerBar; j++) { for (int j = 0; j < binsPerBar; j++) {
barHeights[i] += Math.abs(audioPCM[j+i*binsPerBar]); barHeight += Math.abs(audioPCM[j+i*binsPerBar]);
} }
barHeights[i] /= binsPerBar; barHeight /= binsPerBar;
barHeights[i] *= barHeightMultiplier; barHeight *= barHeightMultiplier;
bars[i].setSize(barWidth, barHeight);
} }
//Drawing...
for (int i = 0; i < barCount; i++) { for (int i = 0; i < barCount; i++) {
int avg = 0; int avg = 0;
//Averaging the bars
for (int range = 0; range < smoothRange; range++) { for (int range = 0; range < smoothRange; range++) {
if (i+range < barCount) { if (i+range < barCount) {
avg += barHeights[i+range]; avg += bars[i+range].getHeight();
} }
if (i-range >= 0) { if (i-range >= 0) {
avg += barHeights[i-range]; avg += bars[i-range].getHeight();
} }
} }
barHeights[i] = avg/(smoothRange*2); bars[i].setSize(barWidth, avg/(smoothRange*2));
batch.draw(bar, xPos + i*(barWidth+spaceBetweenBars), yPos, barWidth, barHeights[i]); bars[i].draw(batch);
} }
} }
super.render(batch, parentAlpha); super.render(batch, parentAlpha);