analysis now functions; music starts playing instantly after pressing
the begin button
This commit is contained in:
parent
49a441132d
commit
eb384e82ae
@ -2,7 +2,9 @@ package zero1hd.rhythmbullet.audio.analyzer;
|
||||
|
||||
import java.util.Observable;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.utils.FloatArray;
|
||||
import com.badlogic.gdx.utils.TimeUtils;
|
||||
|
||||
import edu.emory.mathcs.jtransforms.fft.FloatFFT_1D;
|
||||
import zero1hd.rhythmbullet.audio.processor.AudioProcessor;
|
||||
@ -17,6 +19,7 @@ public class DynamicAudioAnalyzer extends Observable implements Runnable {
|
||||
private FloatArray[] flux;
|
||||
private FloatArray[] threshold;
|
||||
private volatile int pUID = 0;
|
||||
private long timer;
|
||||
|
||||
public DynamicAudioAnalyzer(AudioProcessor processor, AudioAnalyzerSection... sections) {
|
||||
this.sections = sections;
|
||||
@ -41,6 +44,7 @@ public class DynamicAudioAnalyzer extends Observable implements Runnable {
|
||||
if (thread == null) {
|
||||
thread = new Thread(this, threadName);
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
} else {
|
||||
throw new IllegalStateException("Cannot have two analyzer threads.");
|
||||
}
|
||||
@ -51,6 +55,8 @@ public class DynamicAudioAnalyzer extends Observable implements Runnable {
|
||||
}
|
||||
|
||||
private void calculateSpectralFlux() {
|
||||
Gdx.app.debug("Spectral Flux Calculation", "Beginning...");
|
||||
timer = TimeUtils.millis();
|
||||
float[] audioPCM = new float[WINDOWLENGTH];
|
||||
float[] spectrum = new float[(WINDOWLENGTH/2)+1];
|
||||
float[] lastSpectrum = new float[spectrum.length];
|
||||
@ -81,7 +87,7 @@ public class DynamicAudioAnalyzer extends Observable implements Runnable {
|
||||
|
||||
for (int section = 0; section < sections.length; section++) {
|
||||
float currentFlux = 0;
|
||||
for (int bin = sections[section].getLower(); bin < sections[section].getUpper(); section++) {
|
||||
for (int bin = sections[section].getLower(); bin < sections[section].getUpper(); bin++) {
|
||||
currentFlux += Math.max(0f, spectrum[bin] - lastSpectrum[bin]);
|
||||
}
|
||||
flux[section].add(currentFlux);
|
||||
@ -93,12 +99,16 @@ public class DynamicAudioAnalyzer extends Observable implements Runnable {
|
||||
for (int section = 0; section < sections.length; section++) {
|
||||
sections[section].setPUID(pUID);
|
||||
}
|
||||
|
||||
Gdx.app.debug("Spectral Flux Calculation", "Finished. Took " + (TimeUtils.timeSinceMillis(timer)) + "ms");
|
||||
}
|
||||
|
||||
private void calculateThreshold() {
|
||||
long subTimer = TimeUtils.millis();
|
||||
Gdx.app.debug("Threshold Calculation", "Beginning...");
|
||||
for (int section = 0; section < sections.length && work; section++) {
|
||||
FloatArray fluxArray = flux[section];
|
||||
for (int bin = 0; bin < fluxArray.size; fluxArray.size++) {
|
||||
for (int bin = 0; bin < fluxArray.size; bin++) {
|
||||
int range = sections[section].getThresholdRange();
|
||||
int start = Math.max(0, bin - range);
|
||||
int end = Math.min(fluxArray.size, bin + range);
|
||||
@ -111,9 +121,13 @@ public class DynamicAudioAnalyzer extends Observable implements Runnable {
|
||||
threshold[section].add(average);
|
||||
}
|
||||
}
|
||||
|
||||
Gdx.app.debug("Spectral Flux Calculation", "Finished. Took " + (TimeUtils.timeSinceMillis(subTimer)) + "ms");
|
||||
}
|
||||
|
||||
private void calculatePeaks() {
|
||||
long subTimer = TimeUtils.millis();
|
||||
Gdx.app.debug("Peak Calculation", "Beginning...");
|
||||
for (int section = 0; section < sections.length && work; section++) {
|
||||
FloatArray peaks = new FloatArray();
|
||||
|
||||
@ -126,7 +140,8 @@ public class DynamicAudioAnalyzer extends Observable implements Runnable {
|
||||
|
||||
sections[section].setPeaks(peaks);
|
||||
}
|
||||
|
||||
Gdx.app.debug("Spectral Flux Calculation", "Finished. Took " + (TimeUtils.timeSinceMillis(subTimer)) + "ms. Total time was " + (TimeUtils.timeSinceMillis(timer)));
|
||||
|
||||
setChanged();
|
||||
notifyObservers();
|
||||
}
|
||||
|
@ -133,6 +133,7 @@ public class AnalysisPage extends Page {
|
||||
AudioAnalyzerSection midSection = new AudioAnalyzerSection(7, 25, 1.5f, 3);
|
||||
|
||||
audioAnalyzer = new DynamicAudioAnalyzer(mc.getMusicList().newAudioProcessor(mc.getCurrentMusicFileHandle()), bass, midSection);
|
||||
audioAnalyzer.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -15,6 +15,7 @@ import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.badlogic.gdx.scenes.scene2d.Actor;
|
||||
import com.badlogic.gdx.scenes.scene2d.InputEvent;
|
||||
import com.badlogic.gdx.scenes.scene2d.InputListener;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Button;
|
||||
@ -28,6 +29,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.VerticalGroup;
|
||||
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
|
||||
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
|
||||
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent;
|
||||
import com.badlogic.gdx.utils.Align;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
|
||||
@ -118,8 +120,16 @@ public class MusicSelectionPage extends Page implements Observer {
|
||||
addActor(musicInfoTable);
|
||||
|
||||
beginButton = new TextButton("Begin", skin);
|
||||
beginButton.addListener(new ChangeListener() {
|
||||
@Override
|
||||
public void changed(ChangeEvent event, Actor actor) {
|
||||
if (musicSelectDelay != 0) {
|
||||
mc.setMusicByFileHandle(getSelectedMusic());
|
||||
musicSelectDelay = 0f;
|
||||
}
|
||||
}
|
||||
});
|
||||
beginButton.addListener(beginButtonListener);
|
||||
|
||||
mmc.addObserver(this);
|
||||
mc.addObserver(this);
|
||||
mc.getMusicList().addObserver(this);
|
||||
|
Loading…
Reference in New Issue
Block a user