improved audio data structure, started work on analysis page

This commit is contained in:
Harrison Deng 2017-05-09 22:23:12 -05:00
parent 819fb35680
commit 6bc04d85c9
11 changed files with 127 additions and 19 deletions

View File

@ -0,0 +1,15 @@
package zero1hd.polyjet.audio;
import com.badlogic.gdx.files.FileHandle;
public class Audio {
public static AudioData getAudioData(FileHandle file) {
if (file.extension().equalsIgnoreCase("wav")) {
return new WavAudioData(file);
} else if (file.extension().equalsIgnoreCase("mp3")) {
return new Mp3AudioData(file);
}
return null;
}
}

View File

@ -21,6 +21,12 @@ public class Mp3AudioData implements AudioData {
private Music playbackMusic; private Music playbackMusic;
private int readIndex; private int readIndex;
public Mp3AudioData(FileHandle setAudio) {
setAudioFile(setAudio);
}
@Override @Override
public void readIndexUpdate() { public void readIndexUpdate() {
readIndex = (int) (playbackMusic.getPosition() * audioFormat.getSampleRate() / readWindowSize); readIndex = (int) (playbackMusic.getPosition() * audioFormat.getSampleRate() / readWindowSize);

View File

@ -20,6 +20,10 @@ public class WavAudioData implements AudioData {
WavDecoder decoder; WavDecoder decoder;
WavInfo wavinfo; WavInfo wavinfo;
public WavAudioData(FileHandle audioFileHandle) {
setAudioFile(audioFileHandle);
}
@Override @Override
public void readIndexUpdate() { public void readIndexUpdate() {
readIndex = (int) (playbackMusic.getPosition() * wavinfo.getSampleRate() / readWindowSize); readIndex = (int) (playbackMusic.getPosition() * wavinfo.getSampleRate() / readWindowSize);
@ -35,7 +39,7 @@ public class WavAudioData implements AudioData {
} }
@Override @Override
public void setAudioFile(FileHandle audioFileHandler) { public void setAudioFile(FileHandle audioFileHandle) {
reset(); reset();
try { try {
decoder.setAudioFile(wavinfo); decoder.setAudioFile(wavinfo);
@ -43,7 +47,7 @@ public class WavAudioData implements AudioData {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
playbackMusic = Gdx.audio.newMusic(audioFileHandler); playbackMusic = Gdx.audio.newMusic(audioFileHandle);
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, (float) wavinfo.getSampleRate(), 16, wavinfo.getChannels(), wavinfo.getChannels()*2, (float) wavinfo.getSampleRate(), false); format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, (float) wavinfo.getSampleRate(), 16, wavinfo.getChannels(), wavinfo.getChannels()*2, (float) wavinfo.getSampleRate(), false);
} }

View File

@ -0,0 +1,12 @@
package zero1hd.polyjet.screens;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import zero1hd.polyjet.ui.pages.Page;
public class AnalyzePage extends Page {
public AnalyzePage(Skin skin) {
super("Results", skin);
}
}

View File

@ -31,6 +31,7 @@ public class PreGameScreen extends ScreenAdapter implements TransitionAdapter {
Label lastStatement; Label lastStatement;
private MainMenu mainMenu; private MainMenu mainMenu;
private MusicSelectionPage musicSelection;
public PreGameScreen(final Polyjet core, MainMenu mainMenu) { public PreGameScreen(final Polyjet core, MainMenu mainMenu) {
this.core = core; this.core = core;
@ -58,7 +59,7 @@ public class PreGameScreen extends ScreenAdapter implements TransitionAdapter {
stage.addActor(cyberCircle1); stage.addActor(cyberCircle1);
//draw music selector //draw music selector
MusicSelectionPage musicSelection = new MusicSelectionPage(core, mainMenu); musicSelection = new MusicSelectionPage(core, mainMenu);
stage.addActor(musicSelection); stage.addActor(musicSelection);
musicSelection.beginMusicSearch(); musicSelection.beginMusicSearch();
@ -77,6 +78,13 @@ public class PreGameScreen extends ScreenAdapter implements TransitionAdapter {
stage.act(); stage.act();
stage.draw(); stage.draw();
if (stage.getCamera().position.x != cameraTarget.x) {
stage.getCamera().position.lerp(cameraTarget, 0.25f);
}
if (cameraTarget.x != 1.5f*Gdx.graphics.getWidth() && musicSelection.getAudio() != null) {
cameraTarget.x = 1.5f*Gdx.graphics.getWidth();
}
super.render(delta); super.render(delta);
} }

View File

@ -26,6 +26,9 @@ import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.utils.Align; import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.Disposable; import com.badlogic.gdx.utils.Disposable;
import zero1hd.polyjet.audio.AudioData;
import zero1hd.polyjet.audio.Mp3AudioData;
import zero1hd.polyjet.audio.WavAudioData;
import zero1hd.wavedecoder.WavInfo; import zero1hd.wavedecoder.WavInfo;
public class MusicSelectable extends Button implements Disposable { public class MusicSelectable extends Button implements Disposable {
@ -46,10 +49,14 @@ public class MusicSelectable extends Button implements Disposable {
private Label previousTopLabel; private Label previousTopLabel;
private Label ratedDifficultyLabel; private Label ratedDifficultyLabel;
private Skin skin; private Skin skin;
private FileHandle musicFile;
public MusicSelectable(FileHandle musicFile, Preferences musicData, final Skin skin, Texture defaultAlbumC) { public MusicSelectable(FileHandle musicFile, Preferences musicData, final Skin skin, Texture defaultAlbumC) {
super(skin, "info-button"); super(skin, "info-button");
this.skin = skin; this.skin = skin;
this.musicFile = musicFile;
setName(musicFile.name()); setName(musicFile.name());
this.albumCover = defaultAlbumC; this.albumCover = defaultAlbumC;
@ -179,4 +186,8 @@ public class MusicSelectable extends Button implements Disposable {
albumCover.dispose(); albumCover.dispose();
} }
public FileHandle getMusicFile() {
return musicFile;
}
} }

View File

@ -14,8 +14,11 @@ import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
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.Polyjet; import zero1hd.polyjet.Polyjet;
import zero1hd.polyjet.audio.Audio;
import zero1hd.polyjet.audio.AudioData;
import zero1hd.polyjet.screens.MainMenu; import zero1hd.polyjet.screens.MainMenu;
import zero1hd.polyjet.ui.builders.MusicSelectable; import zero1hd.polyjet.ui.builders.MusicSelectable;
import zero1hd.polyjet.ui.windows.LoadingWindow; import zero1hd.polyjet.ui.windows.LoadingWindow;
@ -31,6 +34,7 @@ public class MusicSelectionPage extends Page {
protected volatile boolean cancel; protected volatile boolean cancel;
private TextButton back; private TextButton back;
private AudioData audio;
public MusicSelectionPage(final Polyjet core, final MainMenu mainMenu) { public MusicSelectionPage(final Polyjet core, final MainMenu mainMenu) {
super("Select music", core.defaultSkin); super("Select music", core.defaultSkin);
this.core = core; this.core = core;
@ -101,6 +105,35 @@ public class MusicSelectionPage extends Page {
Gdx.app.debug("Music Search Thread", "Finished loading: " + selectable.getName()); Gdx.app.debug("Music Search Thread", "Finished loading: " + selectable.getName());
musicChoices.add(selectable).prefSize(panelWidthCalc(getWidth()), musicChoiceScroller.getHeight()); musicChoices.add(selectable).prefSize(panelWidthCalc(getWidth()), musicChoiceScroller.getHeight());
selectable.addInfoToPanel(musicChoiceScroller, panelWidthCalc(getWidth()) - 20f); selectable.addInfoToPanel(musicChoiceScroller, panelWidthCalc(getWidth()) - 20f);
musicChoices.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
final NoticeWindow notice = new NoticeWindow(core.defaultSkin, "tinted", "are you sure?", "Analyze this song?");
notice.setSize(0.3f*getWidth(), 0.3f*getHeight());
notice.setPosition((getWidth()-notice.getWidth())/2f, (getHeight()-notice.getHeight())/2f);
notice.setupButton("cancel", new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
notice.remove();
}
}, Align.left);
notice.setupButton("confirm", new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
audio = Audio.getAudioData(selectable.getMusicFile());
}
}, Align.right);
notice.setModal(true);
notice.setMovable(false);
addActor(notice);
back.toFront();
}
});
} }
}); });
@ -128,4 +161,12 @@ public class MusicSelectionPage extends Page {
public float panelWidthCalc(float origWidth) { public float panelWidthCalc(float origWidth) {
return (float) (Math.sqrt(getWidth()*35f)+80f); return (float) (Math.sqrt(getWidth()*35f)+80f);
} }
public AudioData getAudio() {
return audio;
}
public void removeAudio() {
audio = null;
}
} }

View File

@ -14,7 +14,10 @@ import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import zero1hd.polyjet.Polyjet; import zero1hd.polyjet.Polyjet;
import zero1hd.polyjet.audio.Audio;
import zero1hd.polyjet.audio.AudioAnalyzer; import zero1hd.polyjet.audio.AudioAnalyzer;
import zero1hd.polyjet.audio.AudioData;
import zero1hd.polyjet.audio.Mp3AudioData;
import zero1hd.polyjet.audio.WavAudioData; import zero1hd.polyjet.audio.WavAudioData;
import zero1hd.polyjet.screens.MainMenu; import zero1hd.polyjet.screens.MainMenu;
import zero1hd.polyjet.ui.windows.BeatViewer; import zero1hd.polyjet.ui.windows.BeatViewer;
@ -25,7 +28,6 @@ import zero1hd.polyjet.ui.windows.MusicSelector;
import zero1hd.polyjet.ui.windows.VolumeWindow; import zero1hd.polyjet.ui.windows.VolumeWindow;
public class CreativeStage extends Stage { public class CreativeStage extends Stage {
WavAudioData audioWrapper;
MusicController musicPlayBackControls; MusicController musicPlayBackControls;
MusicSelector musicSelector; MusicSelector musicSelector;
FPSWindow fpsViewer; FPSWindow fpsViewer;
@ -37,12 +39,11 @@ public class CreativeStage extends Stage {
Window toolbox; Window toolbox;
Polyjet core; Polyjet core;
boolean postAnalysisComplete; boolean postAnalysisComplete;
private AudioData audioWrapper;
public CreativeStage(final Polyjet core, final MainMenu mainMenu) { public CreativeStage(final Polyjet core, final MainMenu mainMenu) {
this.core = core; this.core = core;
analyzer = new AudioAnalyzer(); analyzer = new AudioAnalyzer();
audioWrapper = new WavAudioData();
musicPlayBackControls = new MusicController(core.defaultSkin, audioWrapper);
musicSelector = new MusicSelector("Select Audio File", core.defaultSkin, core.prefs.getString("music dir"), "default"); musicSelector = new MusicSelector("Select Audio File", core.defaultSkin, core.prefs.getString("music dir"), "default");
musicSelector.postInit(); musicSelector.postInit();
musicSelector.refresh(); musicSelector.refresh();
@ -180,7 +181,7 @@ public class CreativeStage extends Stage {
@Override @Override
public void act(float delta) { public void act(float delta) {
if (musicSelector.isConfirmed()) { if (musicSelector.isConfirmed()) {
audioWrapper.setAudioFile(musicSelector.getSelectedMusic()); audioWrapper = Audio.getAudioData(musicSelector.getSelectedMusic());
audioWrapper.getPlaybackMusic().setVolume(core.prefs.getFloat("music vol")/100f); audioWrapper.getPlaybackMusic().setVolume(core.prefs.getFloat("music vol")/100f);
analyzer.resetVars(); analyzer.resetVars();
analyzer.startAnalyticalThread(audioWrapper); analyzer.startAnalyticalThread(audioWrapper);

View File

@ -13,6 +13,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import zero1hd.polyjet.audio.AudioData;
import zero1hd.polyjet.audio.WavAudioData; import zero1hd.polyjet.audio.WavAudioData;
public class MusicController extends Window { public class MusicController extends Window {
@ -20,9 +21,9 @@ public class MusicController extends Window {
Skin skin; Skin skin;
private Image togglePlay; private Image togglePlay;
private TextField info; private TextField info;
private WavAudioData audiofile; private AudioData audiofile;
public MusicController(final Skin skin, final WavAudioData audiofile) { public MusicController(final Skin skin, final AudioData audiofile) {
super("Playback Controller", skin); super("Playback Controller", skin);
this.audiofile = audiofile; this.audiofile = audiofile;

View File

@ -9,23 +9,31 @@ import com.badlogic.gdx.utils.Align;
public class NoticeWindow extends Window { public class NoticeWindow extends Window {
private Label noticeText; private Label noticeText;
private TextButton finalButton;
private Skin skin; private Skin skin;
public NoticeWindow(Skin skin, String styleName, String text) { public NoticeWindow(Skin skin, String styleName, String text) {
super("Notice", skin); super("Notice", skin);
this.skin = skin; this.skin = skin;
debug();
noticeText = new Label(text, skin); noticeText = new Label(text, skin, "sub-font", skin.getColor("default"));
noticeText.setWrap(true); noticeText.setWrap(true);
noticeText.setAlignment(Align.center); noticeText.setAlignment(Align.center);
add(noticeText).expandX().fill().center().padLeft(20f).padRight(20f); add(noticeText).expandX().fill().center().padLeft(20f).padRight(20f);
} }
public void setupButton(String text, ChangeListener changeListener) { public NoticeWindow(Skin skin, String styleName, String title, String text) {
finalButton = new TextButton(text, skin, "sub-font"); super(title, skin, styleName);
finalButton.addListener(changeListener); this.skin = skin;
add(finalButton);
noticeText = new Label(text, skin, "sub-font", skin.getColor("default"));
noticeText.setWrap(true);
noticeText.setAlignment(Align.center);
add(noticeText).expandX().fill().center().padLeft(20f).padRight(20f);
} }
public void setupButton(String text, ChangeListener changeListener, int alignment) {
TextButton button = new TextButton(text, skin, "sub-font");
button.addListener(changeListener);
add(button).align(alignment);
}
} }

5
core/src/zero1hd/polyjet/util/Base64Preferences.java Normal file → Executable file
View File

@ -11,9 +11,9 @@ public class Base64Preferences {
FileHandle base64File; FileHandle base64File;
HashMap<String, String> prefKeys; HashMap<String, String> prefKeys;
Json json; Json json;
@SuppressWarnings("unchecked")
public Base64Preferences(String prefName) { public Base64Preferences(String prefName) {
base64File = Gdx.files.external(".prefs/" + prefName); base64File = Gdx.files.external(".prefs/.priv/" + prefName);
json = new Json(); json = new Json();
flush(); flush();
@ -35,6 +35,7 @@ public class Base64Preferences {
base64File.writeString(Base64Coder.encodeString(json.toJson(obj)), true); base64File.writeString(Base64Coder.encodeString(json.toJson(obj)), true);
} }
@SuppressWarnings("unchecked")
public void flush() { public void flush() {
prefKeys = json.fromJson(HashMap.class, new String(Base64Coder.decode(base64File.readString()))); prefKeys = json.fromJson(HashMap.class, new String(Base64Coder.decode(base64File.readString())));
if (prefKeys == null) { if (prefKeys == null) {