From 6bc04d85c97cfa744e609c1288367448e6140741 Mon Sep 17 00:00:00 2001 From: Recrown Date: Tue, 9 May 2017 22:23:12 -0500 Subject: [PATCH] improved audio data structure, started work on analysis page --- core/src/zero1hd/polyjet/audio/Audio.java | 15 +++++++ .../zero1hd/polyjet/audio/Mp3AudioData.java | 6 +++ .../zero1hd/polyjet/audio/WavAudioData.java | 8 +++- .../zero1hd/polyjet/screens/AnalyzePage.java | 12 ++++++ .../polyjet/screens/PreGameScreen.java | 10 ++++- .../polyjet/ui/builders/MusicSelectable.java | 11 +++++ .../polyjet/ui/pages/MusicSelectionPage.java | 41 +++++++++++++++++++ .../polyjet/ui/stages/CreativeStage.java | 9 ++-- .../polyjet/ui/windows/MusicController.java | 5 ++- .../polyjet/ui/windows/NoticeWindow.java | 24 +++++++---- .../polyjet/util/Base64Preferences.java | 5 ++- 11 files changed, 127 insertions(+), 19 deletions(-) create mode 100755 core/src/zero1hd/polyjet/audio/Audio.java create mode 100755 core/src/zero1hd/polyjet/screens/AnalyzePage.java mode change 100644 => 100755 core/src/zero1hd/polyjet/util/Base64Preferences.java diff --git a/core/src/zero1hd/polyjet/audio/Audio.java b/core/src/zero1hd/polyjet/audio/Audio.java new file mode 100755 index 0000000..afe55bf --- /dev/null +++ b/core/src/zero1hd/polyjet/audio/Audio.java @@ -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; + } +} diff --git a/core/src/zero1hd/polyjet/audio/Mp3AudioData.java b/core/src/zero1hd/polyjet/audio/Mp3AudioData.java index 5c8cc7d..a26a086 100755 --- a/core/src/zero1hd/polyjet/audio/Mp3AudioData.java +++ b/core/src/zero1hd/polyjet/audio/Mp3AudioData.java @@ -21,6 +21,12 @@ public class Mp3AudioData implements AudioData { private Music playbackMusic; private int readIndex; + + public Mp3AudioData(FileHandle setAudio) { + setAudioFile(setAudio); + } + + @Override public void readIndexUpdate() { readIndex = (int) (playbackMusic.getPosition() * audioFormat.getSampleRate() / readWindowSize); diff --git a/core/src/zero1hd/polyjet/audio/WavAudioData.java b/core/src/zero1hd/polyjet/audio/WavAudioData.java index 1c8f102..da36729 100755 --- a/core/src/zero1hd/polyjet/audio/WavAudioData.java +++ b/core/src/zero1hd/polyjet/audio/WavAudioData.java @@ -20,6 +20,10 @@ public class WavAudioData implements AudioData { WavDecoder decoder; WavInfo wavinfo; + public WavAudioData(FileHandle audioFileHandle) { + setAudioFile(audioFileHandle); + } + @Override public void readIndexUpdate() { readIndex = (int) (playbackMusic.getPosition() * wavinfo.getSampleRate() / readWindowSize); @@ -35,7 +39,7 @@ public class WavAudioData implements AudioData { } @Override - public void setAudioFile(FileHandle audioFileHandler) { + public void setAudioFile(FileHandle audioFileHandle) { reset(); try { decoder.setAudioFile(wavinfo); @@ -43,7 +47,7 @@ public class WavAudioData implements AudioData { // TODO Auto-generated catch block 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); } diff --git a/core/src/zero1hd/polyjet/screens/AnalyzePage.java b/core/src/zero1hd/polyjet/screens/AnalyzePage.java new file mode 100755 index 0000000..d9dab97 --- /dev/null +++ b/core/src/zero1hd/polyjet/screens/AnalyzePage.java @@ -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); + } + +} diff --git a/core/src/zero1hd/polyjet/screens/PreGameScreen.java b/core/src/zero1hd/polyjet/screens/PreGameScreen.java index 663828f..18b3ca2 100755 --- a/core/src/zero1hd/polyjet/screens/PreGameScreen.java +++ b/core/src/zero1hd/polyjet/screens/PreGameScreen.java @@ -31,6 +31,7 @@ public class PreGameScreen extends ScreenAdapter implements TransitionAdapter { Label lastStatement; private MainMenu mainMenu; + private MusicSelectionPage musicSelection; public PreGameScreen(final Polyjet core, MainMenu mainMenu) { this.core = core; @@ -58,7 +59,7 @@ public class PreGameScreen extends ScreenAdapter implements TransitionAdapter { stage.addActor(cyberCircle1); //draw music selector - MusicSelectionPage musicSelection = new MusicSelectionPage(core, mainMenu); + musicSelection = new MusicSelectionPage(core, mainMenu); stage.addActor(musicSelection); musicSelection.beginMusicSearch(); @@ -77,6 +78,13 @@ public class PreGameScreen extends ScreenAdapter implements TransitionAdapter { stage.act(); 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); } diff --git a/core/src/zero1hd/polyjet/ui/builders/MusicSelectable.java b/core/src/zero1hd/polyjet/ui/builders/MusicSelectable.java index 27ea6df..5509459 100755 --- a/core/src/zero1hd/polyjet/ui/builders/MusicSelectable.java +++ b/core/src/zero1hd/polyjet/ui/builders/MusicSelectable.java @@ -26,6 +26,9 @@ import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.utils.Align; 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; public class MusicSelectable extends Button implements Disposable { @@ -46,10 +49,14 @@ public class MusicSelectable extends Button implements Disposable { private Label previousTopLabel; private Label ratedDifficultyLabel; private Skin skin; + + private FileHandle musicFile; public MusicSelectable(FileHandle musicFile, Preferences musicData, final Skin skin, Texture defaultAlbumC) { super(skin, "info-button"); this.skin = skin; + this.musicFile = musicFile; + setName(musicFile.name()); this.albumCover = defaultAlbumC; @@ -179,4 +186,8 @@ public class MusicSelectable extends Button implements Disposable { albumCover.dispose(); } + public FileHandle getMusicFile() { + return musicFile; + } + } diff --git a/core/src/zero1hd/polyjet/ui/pages/MusicSelectionPage.java b/core/src/zero1hd/polyjet/ui/pages/MusicSelectionPage.java index 1981143..3e237db 100755 --- a/core/src/zero1hd/polyjet/ui/pages/MusicSelectionPage.java +++ b/core/src/zero1hd/polyjet/ui/pages/MusicSelectionPage.java @@ -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.TextButton; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; +import com.badlogic.gdx.utils.Align; import zero1hd.polyjet.Polyjet; +import zero1hd.polyjet.audio.Audio; +import zero1hd.polyjet.audio.AudioData; import zero1hd.polyjet.screens.MainMenu; import zero1hd.polyjet.ui.builders.MusicSelectable; import zero1hd.polyjet.ui.windows.LoadingWindow; @@ -31,6 +34,7 @@ public class MusicSelectionPage extends Page { protected volatile boolean cancel; private TextButton back; + private AudioData audio; public MusicSelectionPage(final Polyjet core, final MainMenu mainMenu) { super("Select music", core.defaultSkin); this.core = core; @@ -101,6 +105,35 @@ public class MusicSelectionPage extends Page { Gdx.app.debug("Music Search Thread", "Finished loading: " + selectable.getName()); musicChoices.add(selectable).prefSize(panelWidthCalc(getWidth()), musicChoiceScroller.getHeight()); 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) { return (float) (Math.sqrt(getWidth()*35f)+80f); } + + public AudioData getAudio() { + return audio; + } + + public void removeAudio() { + audio = null; + } } diff --git a/core/src/zero1hd/polyjet/ui/stages/CreativeStage.java b/core/src/zero1hd/polyjet/ui/stages/CreativeStage.java index 192aed2..d13cb1c 100755 --- a/core/src/zero1hd/polyjet/ui/stages/CreativeStage.java +++ b/core/src/zero1hd/polyjet/ui/stages/CreativeStage.java @@ -14,7 +14,10 @@ import com.badlogic.gdx.scenes.scene2d.ui.Window; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import zero1hd.polyjet.Polyjet; +import zero1hd.polyjet.audio.Audio; import zero1hd.polyjet.audio.AudioAnalyzer; +import zero1hd.polyjet.audio.AudioData; +import zero1hd.polyjet.audio.Mp3AudioData; import zero1hd.polyjet.audio.WavAudioData; import zero1hd.polyjet.screens.MainMenu; import zero1hd.polyjet.ui.windows.BeatViewer; @@ -25,7 +28,6 @@ import zero1hd.polyjet.ui.windows.MusicSelector; import zero1hd.polyjet.ui.windows.VolumeWindow; public class CreativeStage extends Stage { - WavAudioData audioWrapper; MusicController musicPlayBackControls; MusicSelector musicSelector; FPSWindow fpsViewer; @@ -37,12 +39,11 @@ public class CreativeStage extends Stage { Window toolbox; Polyjet core; boolean postAnalysisComplete; + private AudioData audioWrapper; public CreativeStage(final Polyjet core, final MainMenu mainMenu) { this.core = core; 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.postInit(); musicSelector.refresh(); @@ -180,7 +181,7 @@ public class CreativeStage extends Stage { @Override public void act(float delta) { if (musicSelector.isConfirmed()) { - audioWrapper.setAudioFile(musicSelector.getSelectedMusic()); + audioWrapper = Audio.getAudioData(musicSelector.getSelectedMusic()); audioWrapper.getPlaybackMusic().setVolume(core.prefs.getFloat("music vol")/100f); analyzer.resetVars(); analyzer.startAnalyticalThread(audioWrapper); diff --git a/core/src/zero1hd/polyjet/ui/windows/MusicController.java b/core/src/zero1hd/polyjet/ui/windows/MusicController.java index dc52794..b76bf88 100755 --- a/core/src/zero1hd/polyjet/ui/windows/MusicController.java +++ b/core/src/zero1hd/polyjet/ui/windows/MusicController.java @@ -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.ClickListener; +import zero1hd.polyjet.audio.AudioData; import zero1hd.polyjet.audio.WavAudioData; public class MusicController extends Window { @@ -20,9 +21,9 @@ public class MusicController extends Window { Skin skin; private Image togglePlay; 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); this.audiofile = audiofile; diff --git a/core/src/zero1hd/polyjet/ui/windows/NoticeWindow.java b/core/src/zero1hd/polyjet/ui/windows/NoticeWindow.java index 9ca349e..69bb76c 100755 --- a/core/src/zero1hd/polyjet/ui/windows/NoticeWindow.java +++ b/core/src/zero1hd/polyjet/ui/windows/NoticeWindow.java @@ -9,23 +9,31 @@ import com.badlogic.gdx.utils.Align; public class NoticeWindow extends Window { private Label noticeText; - private TextButton finalButton; private Skin skin; - public NoticeWindow(Skin skin, String styleName, String text) { super("Notice", skin); this.skin = skin; - debug(); - noticeText = new Label(text, skin); + 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) { - finalButton = new TextButton(text, skin, "sub-font"); - finalButton.addListener(changeListener); - add(finalButton); + public NoticeWindow(Skin skin, String styleName, String title, String text) { + super(title, skin, styleName); + this.skin = skin; + + 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); + } + } diff --git a/core/src/zero1hd/polyjet/util/Base64Preferences.java b/core/src/zero1hd/polyjet/util/Base64Preferences.java old mode 100644 new mode 100755 index e21a1bf..2e9c6de --- a/core/src/zero1hd/polyjet/util/Base64Preferences.java +++ b/core/src/zero1hd/polyjet/util/Base64Preferences.java @@ -11,9 +11,9 @@ public class Base64Preferences { FileHandle base64File; HashMap prefKeys; Json json; - @SuppressWarnings("unchecked") + public Base64Preferences(String prefName) { - base64File = Gdx.files.external(".prefs/" + prefName); + base64File = Gdx.files.external(".prefs/.priv/" + prefName); json = new Json(); flush(); @@ -35,6 +35,7 @@ public class Base64Preferences { base64File.writeString(Base64Coder.encodeString(json.toJson(obj)), true); } + @SuppressWarnings("unchecked") public void flush() { prefKeys = json.fromJson(HashMap.class, new String(Base64Coder.decode(base64File.readString()))); if (prefKeys == null) {