made progress on analysis screen and fixing average beats per second
This commit is contained in:
		| @@ -43,13 +43,14 @@ public class AudioAnalyzer { | ||||
| 	int UMThresholdCalcRange; | ||||
| 	int bassThresholdCalcRange; | ||||
| 	 | ||||
| 	public MiniSender sender; | ||||
| 	public volatile MiniSender sender; | ||||
| 	 | ||||
| 	private float avgBPS; | ||||
| 	 | ||||
| 	int PUID; | ||||
| 	boolean work; | ||||
| 	private volatile int progress; | ||||
| 	private float secondsPerFrame; | ||||
| 	public AudioAnalyzer() { | ||||
| 		sender = new MiniSender(); | ||||
| 		 | ||||
| @@ -122,6 +123,7 @@ public class AudioAnalyzer { | ||||
| 					UMSpectralFlux.add(fluxVal); | ||||
| 					tasksDone++; | ||||
| 					progress = (int) (100f*tasksDone/totalTasks); | ||||
| 					sender.send(MiniEvents.ANALYZER_ITERATED); | ||||
| 				} | ||||
| 				 | ||||
| 				if (work) { | ||||
| @@ -136,7 +138,6 @@ public class AudioAnalyzer { | ||||
| 		}; | ||||
| 		 | ||||
| 		thresholdCalculator = new Runnable() { | ||||
| 			 | ||||
| 			@Override | ||||
| 			public void run() { | ||||
| 				 | ||||
| @@ -184,13 +185,15 @@ public class AudioAnalyzer { | ||||
| 					} else { | ||||
| 						UMPrunned.add(0); | ||||
| 					} | ||||
| 					 | ||||
| 					 | ||||
| 				} | ||||
| 				Gdx.app.debug("Audio Analyzer", "Data prunned."); | ||||
|  | ||||
| 				float secondsUnit = audioData.getFormat().getSampleRate()/audioData.getReadWindowSize(); | ||||
| 				secondsPerFrame = audioData.getReadWindowSize()/audioData.getFormat().getSampleRate(); | ||||
| 				//peak detection | ||||
| 				avgBPS = -1f; | ||||
| 				int beats = 0; | ||||
| 				 | ||||
| 				int lastID = 0; | ||||
| 				for (int i = 0; i < UMPrunned.size-1 && work; i++) { | ||||
| 					bassPeaks.add((bassPrunned.get(i) > bassPrunned.get(i+1) ? bassPrunned.get(i) : 0)); | ||||
| 					if (bassPeaks.get(i) > bassMaxValue) { | ||||
| @@ -209,15 +212,21 @@ public class AudioAnalyzer { | ||||
| 						overlappedPeaks.add(0); | ||||
| 					} | ||||
| 					 | ||||
| 					avgBPS = -1f; | ||||
| 					int beats = 0; | ||||
| 					if (avgBPS == -1 && bassPeaks.get(i) != 0) { | ||||
| 						//this should actually equal to 1; | ||||
| 						avgBPS = 0; | ||||
| 					} else if (bassPeaks.get(i) == 0) { | ||||
| 					} else if (avgBPS == 0 && bassPeaks.get(i) == 0) { | ||||
| 						avgBPS ++; | ||||
| 					} else { | ||||
| 						beats ++; | ||||
| 						lastID = i; | ||||
| 					} | ||||
| 					//then we minus one from the beats so it actually works out | ||||
| 					avgBPS -= UMPrunned.size-lastID; | ||||
| 					avgBPS /= beats; | ||||
| 					avgBPS /= secondsUnit; | ||||
| 					avgBPS *= secondsPerFrame; | ||||
| 				} | ||||
| 				 | ||||
| 				 | ||||
| @@ -329,4 +338,8 @@ public class AudioAnalyzer { | ||||
| 	public float getAvgBPS() { | ||||
| 		return avgBPS; | ||||
| 	} | ||||
| 	 | ||||
| 	public float getsecondsPerFrame() { | ||||
| 		return secondsPerFrame; | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -93,7 +93,7 @@ public class AudioInfo implements Disposable { | ||||
| 	} | ||||
| 	 | ||||
| 	public String getSongName() { | ||||
| 		return songName; | ||||
| 		return songName.replace('_', ' '); | ||||
| 	} | ||||
| 	 | ||||
| 	public Texture getAlbumCover() { | ||||
|   | ||||
| @@ -16,7 +16,7 @@ public class RhythmMapAlgorithm implements Runnable { | ||||
| 	private MersenneTwister rand; | ||||
| 	private GamePlayMap map; | ||||
| 	private float avgBPS; | ||||
| 	 | ||||
| 	private float second; | ||||
| 	public RhythmMapAlgorithm(AudioAnalyzer analyzer) { | ||||
| 		bassPeaks = analyzer.getBassPeaks(); | ||||
| 		UMPeaks = analyzer.getUMPeaks(); | ||||
| @@ -24,6 +24,7 @@ public class RhythmMapAlgorithm implements Runnable { | ||||
| 		map = new GamePlayMap(analyzer.getAudioData()); | ||||
| 		rand = new MersenneTwister(analyzer.getPUID()); | ||||
| 		avgBPS = analyzer.getAvgBPS(); | ||||
| 		second = analyzer.getsecondsPerFrame(); | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| @@ -31,13 +32,19 @@ public class RhythmMapAlgorithm implements Runnable { | ||||
| 		map.beginBuild(); | ||||
| 		for (int index = 0; index < bassPeaks.size; index++) { | ||||
| 			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 | ||||
| 					float endRadius = rand.nextFloat()*Polyjet.GAME_AREA_WIDTH; | ||||
| 					 | ||||
| 					map.addToMap(Entities.VOID_CIRCLE,  | ||||
| 							overlappedPeaks.get(index)*Polyjet.GAME_AREA_WIDTH,  | ||||
| 							endRadius,  | ||||
| 							rand.nextFloat()*Polyjet.GAME_AREA_WIDTH,  | ||||
| 							rand.nextFloat()*Polyjet.GAME_AREA_HEIGHT); | ||||
| 				} else if (bassPeaks.get(index) != 0) { | ||||
| 							rand.nextFloat()*Polyjet.GAME_AREA_HEIGHT, | ||||
| 							avgBPS, | ||||
| 							3f*second | ||||
| 							); | ||||
| 				} | ||||
| 				if (bassPeaks.get(index) != 0) { | ||||
| 					map.addToMap(Entities.BAR,  | ||||
| 							MathUtils.round(rand.nextFloat()*Polyjet.GAME_AREA_WIDTH),  | ||||
| 							bassPeaks.get(index)*Polyjet.GAME_AREA_HEIGHT/avgBPS); | ||||
|   | ||||
| @@ -31,6 +31,7 @@ public class PreGameScreen extends ScreenAdapter implements TransitionAdapter, M | ||||
| 		stage = new Stage(new ScreenViewport()); | ||||
| 		cameraPos = new Vector3(stage.getCamera().position); | ||||
| 		this.core = core; | ||||
| 		postTransition(); | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
|   | ||||
| @@ -1,7 +1,9 @@ | ||||
| package zero1hd.polyjet.ui.pages; | ||||
|  | ||||
| import com.badlogic.gdx.assets.AssetManager; | ||||
| import com.badlogic.gdx.graphics.Color; | ||||
| 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.actions.Actions; | ||||
| 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.TextButton; | ||||
| import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; | ||||
| import com.badlogic.gdx.utils.Align; | ||||
|  | ||||
| import zero1hd.polyjet.audio.AudioAnalyzer; | ||||
| import zero1hd.polyjet.audio.AudioData; | ||||
| @@ -18,7 +21,7 @@ import zero1hd.polyjet.audio.AudioInfo; | ||||
| import zero1hd.polyjet.util.MiniEvents; | ||||
| import zero1hd.polyjet.util.MiniListener; | ||||
|  | ||||
| public class AnalyzePage extends Page { | ||||
| public class AnalyzePage extends Page implements MiniListener { | ||||
| 	AudioAnalyzer audioAnalyzer; | ||||
| 	AudioData music; | ||||
| 	 | ||||
| @@ -29,8 +32,8 @@ public class AnalyzePage extends Page { | ||||
| 	 | ||||
| 	private TextButton back; | ||||
| 	private Image loadingCircle; | ||||
| 	 | ||||
| 	private Label status; | ||||
| 	private long startTime, endTime; | ||||
| 	private Label[] info; | ||||
| 	public AnalyzePage(Skin skin, AssetManager assets) { | ||||
| 		super("Results", skin); | ||||
| 		this.skin = skin; | ||||
| @@ -53,32 +56,41 @@ public class AnalyzePage extends Page { | ||||
| 			} | ||||
| 		}); | ||||
| 		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); | ||||
| 	} | ||||
| 	 | ||||
| 	public void setSong(AudioData music, AudioInfo audioInfo, MiniListener listener) { | ||||
| 		songInfo.clear(); | ||||
| 		 | ||||
| 		songInfo.defaults().align(Align.left); | ||||
| 		audioAnalyzer = new AudioAnalyzer(); | ||||
| 		audioAnalyzer.sender.addListener(this); | ||||
| 		 | ||||
| 		this.music = music; | ||||
| 		audioAnalyzer.startAnalyticalThread(music); | ||||
| 		songInfo.add(new Label(audioInfo.getSongName(), skin, "large-font", skin.getColor("default"))); | ||||
| 		songInfo.row(); | ||||
| 		songInfo.add(status); | ||||
| 		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.add(info[i]); | ||||
| 		} | ||||
| 		startTime = System.currentTimeMillis(); | ||||
| 		songInfo.pack(); | ||||
| 		songInfo.setPosition((getWidth()-songInfo.getWidth())/2f, (getHeightBelowTitle()-songInfo.getHeight())/2f); | ||||
| 		addActor(songInfo); | ||||
| 		info[0].addAction(Actions.color(Color.BLACK, 2.5f)); | ||||
| 		 | ||||
| 	} | ||||
| 	 | ||||
| 	 | ||||
| 	@Override | ||||
| 	public void act(float delta) { | ||||
| 		if (music != null) { | ||||
| 			status.setText("Initial analysis: " + audioAnalyzer.getProgress()); | ||||
| 		} | ||||
| 		super.act(delta); | ||||
| 	} | ||||
| 	 | ||||
| @@ -93,4 +105,25 @@ public class AnalyzePage extends Page { | ||||
| 	public void clearAudioData() { | ||||
| 		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; | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -98,7 +98,7 @@ public class MainPage extends Page { | ||||
| 									Actions.run(new Runnable() { | ||||
| 										@Override | ||||
| 										public void run() { | ||||
| 											core.setScreen(new PreGameScreen(core).postTransition()); | ||||
| 											core.setScreen(new PreGameScreen(core)); | ||||
| 										} | ||||
| 									}), Actions.parallel(Actions.scaleTo(1, 1), Actions.alpha(0.6f)))); | ||||
| 				} | ||||
|   | ||||
| @@ -75,7 +75,7 @@ public class MusicSelectionPage extends Page { | ||||
| 	 | ||||
| 	@Override | ||||
| 	public void act(float delta) { | ||||
| 		 | ||||
| 		back.toFront(); | ||||
| 		super.act(delta); | ||||
| 	} | ||||
| 	 | ||||
| @@ -137,7 +137,6 @@ public class MusicSelectionPage extends Page { | ||||
| 					notice.setMovable(false); | ||||
| 					loadingWindow.remove(); | ||||
| 					addActor(notice); | ||||
| 					back.toFront(); | ||||
| 				} | ||||
| 			} | ||||
| 		}).start(); | ||||
|   | ||||
| @@ -1,5 +1,5 @@ | ||||
| package zero1hd.polyjet.util; | ||||
|  | ||||
| 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; | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user