improved visualizer

This commit is contained in:
Harrison Deng 2017-09-10 20:30:45 -05:00
parent 639172a152
commit 1b61d8ba50
4 changed files with 51 additions and 25 deletions

View File

@ -4,6 +4,7 @@ import java.security.InvalidParameterException;
import java.util.Random;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Music.OnCompletionListener;
import com.badlogic.gdx.utils.Array;
@ -20,32 +21,37 @@ public class SongController implements OnCompletionListener {
private Array<OnDifferentSongListener> listeners;
public SongController(SongList songList) {
if (songList == null) throw new NullPointerException("song list is null...");
private Preferences prefs;
public SongController(SongList songList, Preferences prefs) {
if (prefs == null) throw new NullPointerException("preferences can't be null...");
if (songList == null) throw new NullPointerException("song list can't be null...");
if (!songList.isSearched()) throw new InvalidParameterException("Song list has to be searched already.");
this.prefs = prefs;
listeners = new Array<>();
this.songList = songList;
updateSong();
changeSong();
rand = new Random();
}
public void play() {
mdp.getPlaybackMusic().play();
mdp.getPlaybackMusic().setVolume(prefs.getFloat("music vol"));;
}
public void setSongByIndex(int index) {
this.currentPlaybackID = index;
updateSong();
changeSong();
}
public void skip() {
currentPlaybackID++;
updateSong();
changeSong();
}
public void previous() {
currentPlaybackID--;
updateSong();
changeSong();
}
@Override
@ -59,14 +65,14 @@ public class SongController implements OnCompletionListener {
currentPlaybackID = 0;
}
}
updateSong();
changeSong();
play();
}
}
public void shuffle() {
currentPlaybackID = rand.nextInt(songList.getAmountOfSongs());
updateSong();
changeSong();
}
public void setAutoPlay(boolean autoPlay) {
@ -86,7 +92,7 @@ public class SongController implements OnCompletionListener {
return autoPlay;
}
private void updateSong() {
private void changeSong() {
if (mdp != null) {
mdp.dispose();
}

View File

@ -16,6 +16,8 @@ public class HorizontalVisualizer extends VisualizerCore {
private int barWidth;
private int binsPerBar;
private int spaceBetweenBars;
private int[] barHeights;
private int smoothRange;
public HorizontalVisualizer() {
super(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/2, 0, 0);
@ -26,22 +28,39 @@ public class HorizontalVisualizer extends VisualizerCore {
bar = new Texture(pixmap);
pixmap.dispose();
barCount = 70;
barWidth = width/barCount;
barWidth = MathUtils.round(width/barCount);
spaceBetweenBars = 10;
barWidth -= spaceBetweenBars;
barWidth -= spaceBetweenBars/2;
smoothRange = 4;
barHeights = new int[barCount];
}
@Override
public void render(Batch batch, float parentAlpha) {
if (cmi != null) {
for (int i = 0; i < barCount; i++) {
float currentBinAvg = 0;
for (int j = 0; j < binsPerBar+4; j++) {
currentBinAvg += audioPCM[j+i*binsPerBar];
barHeights[i] = 0;
for (int j = 0; j < binsPerBar; j++) {
barHeights[i] += Math.abs(audioPCM[j+i*binsPerBar]);
}
currentBinAvg /= binsPerBar;
// currentBinAvg = (int) audioPCM[i];
batch.draw(bar, xPos + i*(barWidth+spaceBetweenBars), yPos, barWidth, currentBinAvg*2 +1);
barHeights[i] /= binsPerBar;
barHeights[i] ++;
barHeights[i] *= 5;
}
for (int i = 0; i < barCount; i++) {
int avg = 0;
for (int range = 0; range < smoothRange; range++) {
if (i+range < barCount) {
avg += barHeights[i+range];
}
if (i-range >= 0) {
avg += barHeights[i-range];
}
}
barHeights[i] = avg/(smoothRange*2);
batch.draw(bar, xPos + i*(barWidth+spaceBetweenBars), yPos, barWidth, barHeights[i]);
}
}
super.render(batch, parentAlpha);
@ -50,7 +69,7 @@ public class HorizontalVisualizer extends VisualizerCore {
@Override
public void setMDP(MusicDataPack mdp) {
super.setMDP(mdp);
float validBins = (5000/((mdp.getSampleRate()/2)/((audioPCM.length/2)+1)));
float validBins = (4000/((mdp.getSampleRate()/2)/((audioPCM.length/2)+1)));
Gdx.app.debug("Visualizer", "valid frequency bins " + validBins);
binsPerBar = MathUtils.round((validBins/barCount));
}

View File

@ -42,7 +42,7 @@ public class MainMenu extends ScreenAdapter implements TransitionAdapter {
songList.setSearchPath(core.getPrefs().getString("music dir"));
songList.refresh();
sc = new SongController(songList);
sc = new SongController(songList, core.getPrefs());
sc.setAutoPlay(true);
sc.setShuffle(true);
sc.play();
@ -68,7 +68,7 @@ public class MainMenu extends ScreenAdapter implements TransitionAdapter {
//End main menu
moreOptionsPage = new MoreOptionsPage(core, targetPosition);
optionsPage = new OptionsPage(core, targetPosition, moreOptionsPage, sc.getSongList());
optionsPage = new OptionsPage(core, targetPosition, moreOptionsPage, sc);
optionsPage.setPosition(Gdx.graphics.getWidth(), 0);
stage.addActor(optionsPage);

View File

@ -17,7 +17,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import zero1hd.rhythmbullet.RhythmBullet;
import zero1hd.rhythmbullet.audio.SongList;
import zero1hd.rhythmbullet.audio.SongController;
import zero1hd.rhythmbullet.screens.CreativeScreen;
import zero1hd.rhythmbullet.screens.MainMenu;
@ -27,7 +27,7 @@ public class OptionsPage extends Page {
private ProgressBar fxVolSlider;
private TextField directoryField;
public OptionsPage(final RhythmBullet core, final Vector3 targetPosition, final MoreOptionsPage moreOptionsPage, SongList sl) {
public OptionsPage(final RhythmBullet core, final Vector3 targetPosition, final MoreOptionsPage moreOptionsPage, SongController sc) {
optionsTable.defaults().spaceLeft(40f).padTop(5f).padBottom(5f).left();
Label optionGeneralTitle = new Label("General", core.getDefaultSkin(), "large-font", core.getDefaultSkin().getColor("default"));
@ -45,6 +45,7 @@ public class OptionsPage extends Page {
@Override
public void changed(ChangeEvent event, Actor actor) {
musicVolPercentage.setText(MathUtils.round(musicVolSlider.getValue()) + "%");
sc.getCurrentSong().getPlaybackMusic().setVolume(musicVolSlider.getPercent());
}
});
optionsTable.add(musicVolPercentage).expandX();
@ -129,7 +130,7 @@ public class OptionsPage extends Page {
Gdx.app.debug("Debug Field", debugCodeField.getText());
if (debugCodeField.getText().equals("creative")) {
Gdx.app.debug("Debug Field", "going to creative test room...");
core.setScreen(new CreativeScreen(core, (MainMenu) core.getScreen(), sl));
core.setScreen(new CreativeScreen(core, (MainMenu) core.getScreen(), sc.getSongList()));
}
}
return super.keyUp(event, keycode);
@ -139,8 +140,8 @@ public class OptionsPage extends Page {
public void saveOptions(Preferences prefs) {
Gdx.app.debug("Preferences", "Saved all basic options page values.");
prefs.putFloat("music vol", musicVolSlider.getValue());
prefs.putFloat("fx vol", fxVolSlider.getValue());
prefs.putFloat("music vol", musicVolSlider.getPercent());
prefs.putFloat("fx vol", fxVolSlider.getPercent());
prefs.putString("music dir", directoryField.getText());
}
}