made progress on analysis screen and fixing average beats per second

This commit is contained in:
Harrison Deng 2017-07-10 00:29:50 -05:00
parent d815a3c053
commit cf0114bac3
8 changed files with 82 additions and 29 deletions

View File

@ -43,13 +43,14 @@ public class AudioAnalyzer {
int UMThresholdCalcRange; int UMThresholdCalcRange;
int bassThresholdCalcRange; int bassThresholdCalcRange;
public MiniSender sender; public volatile MiniSender sender;
private float avgBPS; private float avgBPS;
int PUID; int PUID;
boolean work; boolean work;
private volatile int progress; private volatile int progress;
private float secondsPerFrame;
public AudioAnalyzer() { public AudioAnalyzer() {
sender = new MiniSender(); sender = new MiniSender();
@ -122,6 +123,7 @@ public class AudioAnalyzer {
UMSpectralFlux.add(fluxVal); UMSpectralFlux.add(fluxVal);
tasksDone++; tasksDone++;
progress = (int) (100f*tasksDone/totalTasks); progress = (int) (100f*tasksDone/totalTasks);
sender.send(MiniEvents.ANALYZER_ITERATED);
} }
if (work) { if (work) {
@ -136,7 +138,6 @@ public class AudioAnalyzer {
}; };
thresholdCalculator = new Runnable() { thresholdCalculator = new Runnable() {
@Override @Override
public void run() { public void run() {
@ -184,13 +185,15 @@ public class AudioAnalyzer {
} else { } else {
UMPrunned.add(0); UMPrunned.add(0);
} }
} }
Gdx.app.debug("Audio Analyzer", "Data prunned."); Gdx.app.debug("Audio Analyzer", "Data prunned.");
float secondsUnit = audioData.getFormat().getSampleRate()/audioData.getReadWindowSize(); secondsPerFrame = audioData.getReadWindowSize()/audioData.getFormat().getSampleRate();
//peak detection //peak detection
avgBPS = -1f;
int beats = 0; int lastID = 0;
for (int i = 0; i < UMPrunned.size-1 && work; i++) { for (int i = 0; i < UMPrunned.size-1 && work; i++) {
bassPeaks.add((bassPrunned.get(i) > bassPrunned.get(i+1) ? bassPrunned.get(i) : 0)); bassPeaks.add((bassPrunned.get(i) > bassPrunned.get(i+1) ? bassPrunned.get(i) : 0));
if (bassPeaks.get(i) > bassMaxValue) { if (bassPeaks.get(i) > bassMaxValue) {
@ -209,15 +212,21 @@ public class AudioAnalyzer {
overlappedPeaks.add(0); overlappedPeaks.add(0);
} }
avgBPS = -1f;
int beats = 0;
if (avgBPS == -1 && bassPeaks.get(i) != 0) { if (avgBPS == -1 && bassPeaks.get(i) != 0) {
//this should actually equal to 1;
avgBPS = 0; avgBPS = 0;
} else if (bassPeaks.get(i) == 0) { } else if (avgBPS == 0 && bassPeaks.get(i) == 0) {
avgBPS ++; avgBPS ++;
} else { } else {
beats ++; beats ++;
lastID = i;
} }
//then we minus one from the beats so it actually works out
avgBPS -= UMPrunned.size-lastID;
avgBPS /= beats; avgBPS /= beats;
avgBPS /= secondsUnit; avgBPS *= secondsPerFrame;
} }
@ -329,4 +338,8 @@ public class AudioAnalyzer {
public float getAvgBPS() { public float getAvgBPS() {
return avgBPS; return avgBPS;
} }
public float getsecondsPerFrame() {
return secondsPerFrame;
}
} }

View File

@ -93,7 +93,7 @@ public class AudioInfo implements Disposable {
} }
public String getSongName() { public String getSongName() {
return songName; return songName.replace('_', ' ');
} }
public Texture getAlbumCover() { public Texture getAlbumCover() {

View File

@ -16,7 +16,7 @@ public class RhythmMapAlgorithm implements Runnable {
private MersenneTwister rand; private MersenneTwister rand;
private GamePlayMap map; private GamePlayMap map;
private float avgBPS; private float avgBPS;
private float second;
public RhythmMapAlgorithm(AudioAnalyzer analyzer) { public RhythmMapAlgorithm(AudioAnalyzer analyzer) {
bassPeaks = analyzer.getBassPeaks(); bassPeaks = analyzer.getBassPeaks();
UMPeaks = analyzer.getUMPeaks(); UMPeaks = analyzer.getUMPeaks();
@ -24,6 +24,7 @@ public class RhythmMapAlgorithm implements Runnable {
map = new GamePlayMap(analyzer.getAudioData()); map = new GamePlayMap(analyzer.getAudioData());
rand = new MersenneTwister(analyzer.getPUID()); rand = new MersenneTwister(analyzer.getPUID());
avgBPS = analyzer.getAvgBPS(); avgBPS = analyzer.getAvgBPS();
second = analyzer.getsecondsPerFrame();
} }
@Override @Override
@ -31,13 +32,19 @@ public class RhythmMapAlgorithm implements Runnable {
map.beginBuild(); map.beginBuild();
for (int index = 0; index < bassPeaks.size; index++) { for (int index = 0; index < bassPeaks.size; index++) {
if (bassPeaks.get(index) != 0 || UMPeaks.get(index) != 0) { if (bassPeaks.get(index) != 0 || UMPeaks.get(index) != 0) {
if (overlappedPeaks.get(index) != 0) { if (overlappedPeaks.get(index+3) != 0) {
//TODO basic void circle spawning //TODO basic void circle spawning
float endRadius = rand.nextFloat()*Polyjet.GAME_AREA_WIDTH;
map.addToMap(Entities.VOID_CIRCLE, map.addToMap(Entities.VOID_CIRCLE,
overlappedPeaks.get(index)*Polyjet.GAME_AREA_WIDTH, endRadius,
rand.nextFloat()*Polyjet.GAME_AREA_WIDTH, rand.nextFloat()*Polyjet.GAME_AREA_WIDTH,
rand.nextFloat()*Polyjet.GAME_AREA_HEIGHT); rand.nextFloat()*Polyjet.GAME_AREA_HEIGHT,
} else if (bassPeaks.get(index) != 0) { avgBPS,
3f*second
);
}
if (bassPeaks.get(index) != 0) {
map.addToMap(Entities.BAR, map.addToMap(Entities.BAR,
MathUtils.round(rand.nextFloat()*Polyjet.GAME_AREA_WIDTH), MathUtils.round(rand.nextFloat()*Polyjet.GAME_AREA_WIDTH),
bassPeaks.get(index)*Polyjet.GAME_AREA_HEIGHT/avgBPS); bassPeaks.get(index)*Polyjet.GAME_AREA_HEIGHT/avgBPS);

View File

@ -31,6 +31,7 @@ public class PreGameScreen extends ScreenAdapter implements TransitionAdapter, M
stage = new Stage(new ScreenViewport()); stage = new Stage(new ScreenViewport());
cameraPos = new Vector3(stage.getCamera().position); cameraPos = new Vector3(stage.getCamera().position);
this.core = core; this.core = core;
postTransition();
} }
@Override @Override

View File

@ -1,7 +1,9 @@
package zero1hd.polyjet.ui.pages; package zero1hd.polyjet.ui.pages;
import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.actions.Actions; import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image; import com.badlogic.gdx.scenes.scene2d.ui.Image;
@ -11,6 +13,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Slider;
import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.Align;
import zero1hd.polyjet.audio.AudioAnalyzer; import zero1hd.polyjet.audio.AudioAnalyzer;
import zero1hd.polyjet.audio.AudioData; import zero1hd.polyjet.audio.AudioData;
@ -18,7 +21,7 @@ import zero1hd.polyjet.audio.AudioInfo;
import zero1hd.polyjet.util.MiniEvents; import zero1hd.polyjet.util.MiniEvents;
import zero1hd.polyjet.util.MiniListener; import zero1hd.polyjet.util.MiniListener;
public class AnalyzePage extends Page { public class AnalyzePage extends Page implements MiniListener {
AudioAnalyzer audioAnalyzer; AudioAnalyzer audioAnalyzer;
AudioData music; AudioData music;
@ -29,8 +32,8 @@ public class AnalyzePage extends Page {
private TextButton back; private TextButton back;
private Image loadingCircle; private Image loadingCircle;
private long startTime, endTime;
private Label status; private Label[] info;
public AnalyzePage(Skin skin, AssetManager assets) { public AnalyzePage(Skin skin, AssetManager assets) {
super("Results", skin); super("Results", skin);
this.skin = skin; this.skin = skin;
@ -54,31 +57,40 @@ public class AnalyzePage extends Page {
}); });
songInfo.setSize(getWidth(), getHeightBelowTitle()); songInfo.setSize(getWidth(), getHeightBelowTitle());
status = new Label(null, skin, "sub-font", skin.getColor("default"));
info = new Label[6];
for (int i = 0; i < info.length; i++) {
info[i] = new Label(null, skin, "sub-font", skin.getColor("default"));
}
addActor(back); addActor(back);
} }
public void setSong(AudioData music, AudioInfo audioInfo, MiniListener listener) { public void setSong(AudioData music, AudioInfo audioInfo, MiniListener listener) {
songInfo.clear(); songInfo.clear();
songInfo.defaults().align(Align.left);
audioAnalyzer = new AudioAnalyzer(); audioAnalyzer = new AudioAnalyzer();
audioAnalyzer.sender.addListener(this);
this.music = music; this.music = music;
audioAnalyzer.startAnalyticalThread(music); audioAnalyzer.startAnalyticalThread(music);
songInfo.add(new Label(audioInfo.getSongName(), skin, "large-font", skin.getColor("default"))); songInfo.add(new Label(audioInfo.getSongName(), skin, "default-font", skin.getColor("default")));
for (int i = 0; i < info.length; i++) {
info[i].setColor(1f, 1f, 1f, 0f);
info[i].setText(null);
songInfo.row(); songInfo.row();
songInfo.add(status); songInfo.add(info[i]);
}
startTime = System.currentTimeMillis();
songInfo.pack();
songInfo.setPosition((getWidth()-songInfo.getWidth())/2f, (getHeightBelowTitle()-songInfo.getHeight())/2f);
addActor(songInfo); addActor(songInfo);
info[0].addAction(Actions.color(Color.BLACK, 2.5f));
} }
@Override @Override
public void act(float delta) { public void act(float delta) {
if (music != null) {
status.setText("Initial analysis: " + audioAnalyzer.getProgress());
}
super.act(delta); super.act(delta);
} }
@ -93,4 +105,25 @@ public class AnalyzePage extends Page {
public void clearAudioData() { public void clearAudioData() {
music = null; music = null;
} }
@Override
public void handle(MiniEvents ID) {
switch (ID) {
case ANALYZER_ITERATED:
info[0].setText("Initial analysis: " + audioAnalyzer.getProgress() + "%");
break;
case SPECTRAL_FLUX_DONE:
songInfo.row();
endTime = System.currentTimeMillis();
info[1].setText("Done. Analyze time: " + ((endTime - startTime)/1000f) + "s");
info[1].addAction(Actions.color(Color.BLACK, 0.75f));
info[2].setText("Average enemies per second: " + audioAnalyzer.getAvgBPS());
info[2].addAction(Actions.color(Color.BLACK, 1.5f));
songInfo.addAction(Actions.moveTo(40f, getHeightBelowTitle()-songInfo.getHeight()-25f, 1f, Interpolation.smooth));
break;
default:
break;
}
}
} }

View File

@ -98,7 +98,7 @@ public class MainPage extends Page {
Actions.run(new Runnable() { Actions.run(new Runnable() {
@Override @Override
public void run() { public void run() {
core.setScreen(new PreGameScreen(core).postTransition()); core.setScreen(new PreGameScreen(core));
} }
}), Actions.parallel(Actions.scaleTo(1, 1), Actions.alpha(0.6f)))); }), Actions.parallel(Actions.scaleTo(1, 1), Actions.alpha(0.6f))));
} }

View File

@ -75,7 +75,7 @@ public class MusicSelectionPage extends Page {
@Override @Override
public void act(float delta) { public void act(float delta) {
back.toFront();
super.act(delta); super.act(delta);
} }
@ -137,7 +137,6 @@ public class MusicSelectionPage extends Page {
notice.setMovable(false); notice.setMovable(false);
loadingWindow.remove(); loadingWindow.remove();
addActor(notice); addActor(notice);
back.toFront();
} }
} }
}).start(); }).start();

View File

@ -1,5 +1,5 @@
package zero1hd.polyjet.util; package zero1hd.polyjet.util;
public enum MiniEvents { public enum MiniEvents {
SPECTRAL_FLUX_DONE, MUSIC_DATA_CLEANED, MUSIC_SELECTED, BACK, MAP_GENERATED; SPECTRAL_FLUX_DONE, MUSIC_DATA_CLEANED, MUSIC_SELECTED, BACK, MAP_GENERATED, ANALYZER_ITERATED;
} }