analysis now functions; music starts playing instantly after pressing
the begin button
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user