fixing errors

This commit is contained in:
Harrison Deng 2017-05-22 23:00:12 -05:00
parent dd4d6b502a
commit 0fff556332
8 changed files with 57 additions and 47 deletions

View File

@ -1,12 +1,11 @@
package zero1hd.polyjet.audio; package zero1hd.polyjet.audio;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.FloatArray; import com.badlogic.gdx.utils.FloatArray;
import edu.emory.mathcs.jtransforms.fft.FloatFFT_1D; import edu.emory.mathcs.jtransforms.fft.FloatFFT_1D;
import zero1hd.polyjet.util.MiniEvents; import zero1hd.polyjet.util.MiniEvents;
import zero1hd.polyjet.util.MiniListener; import zero1hd.polyjet.util.MiniSender;
public class AudioAnalyzer { public class AudioAnalyzer {
private boolean containsData; private boolean containsData;
@ -45,11 +44,12 @@ public class AudioAnalyzer {
int UMThresholdCalcRange; int UMThresholdCalcRange;
int bassThresholdCalcRange; int bassThresholdCalcRange;
private Array<MiniListener> listeners; public MiniSender sender;
private volatile int progress; private volatile int progress;
public AudioAnalyzer() { public AudioAnalyzer() {
listeners = new Array<>(); sender = new MiniSender();
analysisAlgorithm = new Runnable() { analysisAlgorithm = new Runnable() {
@ -113,7 +113,7 @@ public class AudioAnalyzer {
shrinkData(); shrinkData();
containsData = true; containsData = true;
send(MiniEvents.SPECTRAL_FLUX_DONE); sender.send(MiniEvents.SPECTRAL_FLUX_DONE);
} }
}; };
@ -196,6 +196,8 @@ public class AudioAnalyzer {
Gdx.app.debug("Audio Analyzer", "overlapped beats checked."); Gdx.app.debug("Audio Analyzer", "overlapped beats checked.");
finalized = true; finalized = true;
sender.send(MiniEvents.MUSIC_DATA_CLEANED);
} }
}; };
@ -304,14 +306,4 @@ public class AudioAnalyzer {
public boolean isFinalized() { public boolean isFinalized() {
return finalized; return finalized;
} }
public void addListener(MiniListener listener) {
listeners.add(listener);
}
public void send(MiniEvents ID) {
while (listeners.iterator().hasNext()) {
listeners.iterator().next().handle(ID);
}
}
} }

View File

@ -8,10 +8,11 @@ import javax.sound.sampled.AudioFormat;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music; import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Disposable;
import zero1hd.wavedecoder.WavDecoder; import zero1hd.wavedecoder.WavDecoder;
public class WavAudioData implements AudioData { public class WavAudioData implements AudioData, Disposable {
private int readWindowSize = 1024; private int readWindowSize = 1024;
private AudioFormat format; private AudioFormat format;
int readIndex; int readIndex;
@ -46,8 +47,6 @@ public class WavAudioData implements AudioData {
public void reset() { public void reset() {
if (playbackMusic != null) { if (playbackMusic != null) {
playbackMusic.stop(); playbackMusic.stop();
playbackMusic.dispose();
playbackMusic = null;
} }
} }
@ -75,4 +74,9 @@ public class WavAudioData implements AudioData {
public int getSampleCount() { public int getSampleCount() {
return decoder.getDataSize()/(2*decoder.getChannels()); return decoder.getDataSize()/(2*decoder.getChannels());
} }
@Override
public void dispose() {
playbackMusic.dispose();
}
} }

View File

@ -53,8 +53,7 @@ public class MusicSelectionPage extends Page {
}); });
addActor(back); addActor(back);
loadingWindow = new LoadingWindow(core.getDefaultSkin(), "tinted", true, core.getAssetManager(), core.prefs.getFloat("fx vol")); loadingWindow.setPosition((getWidth()-loadingWindow.getWidth())/2f, (getHeight()-loadingWindow.getHeight())/2f);
loadingWindow.setPosition((getWidth()-loadingWindow.getWidth())/2f, (getHeight()-loadingWindow.getHeight())/2f);
addActor(loadingWindow); addActor(loadingWindow);
loadingWindow.playOpenSound(); loadingWindow.playOpenSound();
loadingWindow.setMovable(false); loadingWindow.setMovable(false);

View File

@ -17,6 +17,7 @@ public class Page extends Group {
public Page() { public Page() {
setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
listeners = new Array<>();
setTouchable(Touchable.childrenOnly); setTouchable(Touchable.childrenOnly);
} }
@ -39,6 +40,9 @@ public class Page extends Group {
} }
public void addListener(MiniListener handler) { public void addListener(MiniListener handler) {
if (listeners == null) {
Gdx.app.log("Test", "URS STEWPID");
}
listeners.add(handler); listeners.add(handler);
} }
} }

View File

@ -14,7 +14,6 @@ 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.AudioData;
import zero1hd.polyjet.screens.MainMenu; import zero1hd.polyjet.screens.MainMenu;
@ -39,15 +38,14 @@ public class CreativeStage extends Stage implements MiniListener {
AudioAnalyzer analyzer; AudioAnalyzer analyzer;
Window toolbox; Window toolbox;
Polyjet core; Polyjet core;
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();
analyzer.addListener(this); analyzer.sender.addListener(this);
musicSelector = new MusicSelector("Select Audio File", core.getDefaultSkin(), core.prefs.getString("music dir"), "default"); musicSelector = new MusicSelector("Select Audio File", core.getDefaultSkin(), core.prefs.getString("music dir"), "default");
musicSelector.addListener(this); musicSelector.miniSender.addListener(this);
musicSelector.postInit(); musicSelector.postInit();
musicSelector.refresh(); musicSelector.refresh();
@ -196,7 +194,6 @@ public class CreativeStage extends Stage implements MiniListener {
public void dispose() { public void dispose() {
analyzer.resetVars(); analyzer.resetVars();
analyzer.shrinkData(); analyzer.shrinkData();
audioWrapper.reset();
super.dispose(); super.dispose();
} }
@ -208,13 +205,13 @@ public class CreativeStage extends Stage implements MiniListener {
volumeWindow.setMusic(null); volumeWindow.setMusic(null);
graphViewer.setData(null, null, null); graphViewer.setData(null, null, null);
musicPlayBackControls.setAudiofile(null); musicPlayBackControls.setAudiofile(null);
analyzer.startAnalyticalThread(Audio.getAudioData(musicSelector.getSelectedMusic())); analyzer.startAnalyticalThread(musicSelector.getSelectedMusic());
break; break;
case SPECTRAL_FLUX_DONE: case SPECTRAL_FLUX_DONE:
analyzer.runThresholdCleaning(); analyzer.runThresholdCleaning();
break; break;
case MUSIC_DATA_CLEANED: case MUSIC_DATA_CLEANED:
musicPlayBackControls.setAudiofile(Audio.getAudioData(musicSelector.getSelectedMusic())); musicPlayBackControls.setAudiofile(musicSelector.getSelectedMusic());
volumeWindow.setMusic(musicPlayBackControls.getAudiofile()); volumeWindow.setMusic(musicPlayBackControls.getAudiofile());
beatViewer.setMusic(musicPlayBackControls.getAudiofile()); beatViewer.setMusic(musicPlayBackControls.getAudiofile());
graphViewer.setData(analyzer.getBassPeaks(), analyzer.getUMPeaks(), musicPlayBackControls.getAudiofile()); graphViewer.setData(analyzer.getBassPeaks(), analyzer.getUMPeaks(), musicPlayBackControls.getAudiofile());

View File

@ -115,19 +115,17 @@ public class MusicController extends Window {
public void setAudiofile(AudioData audiofile) { public void setAudiofile(AudioData audiofile) {
this.audiofile.reset();
this.audiofile = audiofile; this.audiofile = audiofile;
if (audiofile == null) { if (audiofile == null) {
togglePlay.setDrawable(skin.getDrawable("loading")); togglePlay.setDrawable(skin.getDrawable("loading"));
info.setText("Analyzing..."); info.setText("Analyzing...");
} else { } else {
audiofile.reset();
togglePlay.setDrawable(skin.getDrawable("arrow")); togglePlay.setDrawable(skin.getDrawable("arrow"));
info.setText("Ready."); info.setText("Ready.");
audiofile.getPlaybackMusic().play(); audiofile.getPlaybackMusic().play();
audiofile.getPlaybackMusic().pause(); audiofile.getPlaybackMusic().pause();
} }
} }
public AudioData getAudiofile() { public AudioData getAudiofile() {

View File

@ -11,8 +11,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 com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Array;
import zero1hd.polyjet.audio.Audio;
import zero1hd.polyjet.audio.AudioData;
import zero1hd.polyjet.util.MiniEvents; import zero1hd.polyjet.util.MiniEvents;
import zero1hd.polyjet.util.MiniListener; import zero1hd.polyjet.util.MiniSender;
public class MusicSelector extends Window { public class MusicSelector extends Window {
boolean confirmed; boolean confirmed;
@ -22,8 +24,7 @@ public class MusicSelector extends Window {
private List<String> musicList; private List<String> musicList;
private String path; private String path;
private ScrollPane listScroller; private ScrollPane listScroller;
public MiniSender miniSender;
private Array<MiniListener> listeners;
public MusicSelector(String title, Skin skin, final String path, String listStyle) { public MusicSelector(String title, Skin skin, final String path, String listStyle) {
super(title, skin); super(title, skin);
@ -32,7 +33,7 @@ public class MusicSelector extends Window {
padLeft(5f); padLeft(5f);
padRight(5f); padRight(5f);
listeners = new Array<>(); miniSender = new MiniSender();
setSize(Gdx.graphics.getWidth()*0.5f, Gdx.graphics.getHeight()*0.5f); setSize(Gdx.graphics.getWidth()*0.5f, Gdx.graphics.getHeight()*0.5f);
@ -46,7 +47,7 @@ public class MusicSelector extends Window {
public void changed(ChangeEvent event, Actor actor) { public void changed(ChangeEvent event, Actor actor) {
confirmed = true; confirmed = true;
selectedMusic = Gdx.files.absolute(path+System.getProperty("file.separator")+musicList.getSelected()); selectedMusic = Gdx.files.absolute(path+System.getProperty("file.separator")+musicList.getSelected());
send(MiniEvents.MUSIC_SELECTED); miniSender.send(MiniEvents.MUSIC_SELECTED);
} }
}); });
add(confirmButton).right(); add(confirmButton).right();
@ -86,17 +87,7 @@ public class MusicSelector extends Window {
return isBack; return isBack;
} }
public FileHandle getSelectedMusic() { public AudioData getSelectedMusic() {
return selectedMusic; return Audio.getAudioData(selectedMusic);
}
private void send(MiniEvents ID) {
while (listeners.iterator().hasNext()) {
listeners.iterator().next().handle(ID);
}
}
public void addListener(MiniListener handler) {
listeners.add(handler);
} }
} }

View File

@ -0,0 +1,25 @@
package zero1hd.polyjet.util;
import com.badlogic.gdx.utils.Array;
public class MiniSender {
private Array<MiniListener> listeners;
public MiniSender() {
listeners = new Array<MiniListener>();
}
public void send(MiniEvents ID) {
for (MiniListener listener : listeners) {
listener.handle(ID);
}
}
public void addListener(MiniListener handler) {
listeners.add(handler);
}
public void removeListener(MiniListener listener) {
listeners.removeValue(listener, true);
}
}