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

View File

@ -16,6 +16,8 @@ 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 int smoothRange;
public HorizontalVisualizer() { public HorizontalVisualizer() {
super(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/2, 0, 0); super(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/2, 0, 0);
@ -26,22 +28,39 @@ public class HorizontalVisualizer extends VisualizerCore {
bar = new Texture(pixmap); bar = new Texture(pixmap);
pixmap.dispose(); pixmap.dispose();
barCount = 70; barCount = 70;
barWidth = width/barCount; barWidth = MathUtils.round(width/barCount);
spaceBetweenBars = 10; spaceBetweenBars = 10;
barWidth -= spaceBetweenBars; barWidth -= spaceBetweenBars/2;
smoothRange = 4;
barHeights = new int[barCount];
} }
@Override @Override
public void render(Batch batch, float parentAlpha) { public void render(Batch batch, float parentAlpha) {
if (cmi != null) { if (cmi != null) {
for (int i = 0; i < barCount; i++) { for (int i = 0; i < barCount; i++) {
float currentBinAvg = 0; barHeights[i] = 0;
for (int j = 0; j < binsPerBar+4; j++) { for (int j = 0; j < binsPerBar; j++) {
currentBinAvg += audioPCM[j+i*binsPerBar]; barHeights[i] += Math.abs(audioPCM[j+i*binsPerBar]);
} }
currentBinAvg /= binsPerBar; barHeights[i] /= binsPerBar;
// currentBinAvg = (int) audioPCM[i]; barHeights[i] ++;
batch.draw(bar, xPos + i*(barWidth+spaceBetweenBars), yPos, barWidth, currentBinAvg*2 +1); 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); super.render(batch, parentAlpha);
@ -50,7 +69,7 @@ public class HorizontalVisualizer extends VisualizerCore {
@Override @Override
public void setMDP(MusicDataPack mdp) { public void setMDP(MusicDataPack mdp) {
super.setMDP(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); Gdx.app.debug("Visualizer", "valid frequency bins " + validBins);
binsPerBar = MathUtils.round((validBins/barCount)); 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.setSearchPath(core.getPrefs().getString("music dir"));
songList.refresh(); songList.refresh();
sc = new SongController(songList); sc = new SongController(songList, core.getPrefs());
sc.setAutoPlay(true); sc.setAutoPlay(true);
sc.setShuffle(true); sc.setShuffle(true);
sc.play(); sc.play();
@ -68,7 +68,7 @@ public class MainMenu extends ScreenAdapter implements TransitionAdapter {
//End main menu //End main menu
moreOptionsPage = new MoreOptionsPage(core, targetPosition); 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); optionsPage.setPosition(Gdx.graphics.getWidth(), 0);
stage.addActor(optionsPage); 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 com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import zero1hd.rhythmbullet.RhythmBullet; import zero1hd.rhythmbullet.RhythmBullet;
import zero1hd.rhythmbullet.audio.SongList; import zero1hd.rhythmbullet.audio.SongController;
import zero1hd.rhythmbullet.screens.CreativeScreen; import zero1hd.rhythmbullet.screens.CreativeScreen;
import zero1hd.rhythmbullet.screens.MainMenu; import zero1hd.rhythmbullet.screens.MainMenu;
@ -27,7 +27,7 @@ public class OptionsPage extends Page {
private ProgressBar fxVolSlider; private ProgressBar fxVolSlider;
private TextField directoryField; 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(); optionsTable.defaults().spaceLeft(40f).padTop(5f).padBottom(5f).left();
Label optionGeneralTitle = new Label("General", core.getDefaultSkin(), "large-font", core.getDefaultSkin().getColor("default")); Label optionGeneralTitle = new Label("General", core.getDefaultSkin(), "large-font", core.getDefaultSkin().getColor("default"));
@ -45,6 +45,7 @@ public class OptionsPage extends Page {
@Override @Override
public void changed(ChangeEvent event, Actor actor) { public void changed(ChangeEvent event, Actor actor) {
musicVolPercentage.setText(MathUtils.round(musicVolSlider.getValue()) + "%"); musicVolPercentage.setText(MathUtils.round(musicVolSlider.getValue()) + "%");
sc.getCurrentSong().getPlaybackMusic().setVolume(musicVolSlider.getPercent());
} }
}); });
optionsTable.add(musicVolPercentage).expandX(); optionsTable.add(musicVolPercentage).expandX();
@ -129,7 +130,7 @@ public class OptionsPage extends Page {
Gdx.app.debug("Debug Field", debugCodeField.getText()); Gdx.app.debug("Debug Field", debugCodeField.getText());
if (debugCodeField.getText().equals("creative")) { if (debugCodeField.getText().equals("creative")) {
Gdx.app.debug("Debug Field", "going to creative test room..."); 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); return super.keyUp(event, keycode);
@ -139,8 +140,8 @@ public class OptionsPage extends Page {
public void saveOptions(Preferences prefs) { public void saveOptions(Preferences prefs) {
Gdx.app.debug("Preferences", "Saved all basic options page values."); Gdx.app.debug("Preferences", "Saved all basic options page values.");
prefs.putFloat("music vol", musicVolSlider.getValue()); prefs.putFloat("music vol", musicVolSlider.getPercent());
prefs.putFloat("fx vol", fxVolSlider.getValue()); prefs.putFloat("fx vol", fxVolSlider.getPercent());
prefs.putString("music dir", directoryField.getText()); prefs.putString("music dir", directoryField.getText());
} }
} }