|
|
|
@@ -32,7 +32,7 @@ public class PCMObtainer implements Observer, PCMSystem {
|
|
|
|
|
private float[] frequencyBins = new float[windowSize / 2];
|
|
|
|
|
private FloatFFT_1D fft = new FloatFFT_1D(windowSize);
|
|
|
|
|
private ShortBuffer playingBuffer;
|
|
|
|
|
private ShortBuffer compareBuffer;
|
|
|
|
|
private ShortBuffer intermediateBuffer;
|
|
|
|
|
private ShortBuffer buffer;
|
|
|
|
|
private int sourceID;
|
|
|
|
|
private int channelCount;
|
|
|
|
@@ -50,7 +50,7 @@ public class PCMObtainer implements Observer, PCMSystem {
|
|
|
|
|
bufferField.setAccessible(true);
|
|
|
|
|
buffer = ((ByteBuffer) bufferField.get(null)).asShortBuffer().asReadOnlyBuffer();
|
|
|
|
|
playingBuffer = ShortBuffer.allocate(buffer.capacity());
|
|
|
|
|
compareBuffer = ShortBuffer.allocate(buffer.capacity());
|
|
|
|
|
intermediateBuffer = ShortBuffer.allocate(buffer.capacity());
|
|
|
|
|
} catch (IllegalArgumentException | SecurityException | ReflectionException e) {
|
|
|
|
|
Gdx.app.debug("Visualizer reflection", "Failed attempt at retrieving tempBuffer field.", e);
|
|
|
|
|
Gdx.app.exit();
|
|
|
|
@@ -79,13 +79,13 @@ public class PCMObtainer implements Observer, PCMSystem {
|
|
|
|
|
|
|
|
|
|
// Begin comparison
|
|
|
|
|
buffer.rewind();
|
|
|
|
|
if (compareBuffer.compareTo(buffer) != 0) {
|
|
|
|
|
if (intermediateBuffer.compareTo(buffer) != 0) {
|
|
|
|
|
bufferChanged();
|
|
|
|
|
|
|
|
|
|
// Begin copying current buffer to the comparison buffer
|
|
|
|
|
compareBuffer.clear();
|
|
|
|
|
compareBuffer.put(buffer);
|
|
|
|
|
compareBuffer.flip();
|
|
|
|
|
intermediateBuffer.clear();
|
|
|
|
|
intermediateBuffer.put(buffer);
|
|
|
|
|
intermediateBuffer.flip();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reset buffer to proper position.
|
|
|
|
@@ -94,7 +94,7 @@ public class PCMObtainer implements Observer, PCMSystem {
|
|
|
|
|
|
|
|
|
|
private void bufferChanged() {
|
|
|
|
|
playingBuffer.position(0);
|
|
|
|
|
playingBuffer.put(compareBuffer);
|
|
|
|
|
playingBuffer.put(intermediateBuffer);
|
|
|
|
|
|
|
|
|
|
synchronizeBufferWithPlayback();
|
|
|
|
|
}
|
|
|
|
@@ -158,7 +158,6 @@ public class PCMObtainer implements Observer, PCMSystem {
|
|
|
|
|
private volatile boolean run = true;
|
|
|
|
|
private long timeOfLastRead;
|
|
|
|
|
private long waitTime;
|
|
|
|
|
private boolean read = true;
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
while (run) {
|
|
|
|
@@ -167,12 +166,11 @@ public class PCMObtainer implements Observer, PCMSystem {
|
|
|
|
|
timeOfLastRead = TimeUtils.millis();
|
|
|
|
|
|
|
|
|
|
//calculate current pcm data and notify that there is new data
|
|
|
|
|
if (read) {
|
|
|
|
|
calcPCMData();
|
|
|
|
|
fft.realForward(PCM);
|
|
|
|
|
updated = true;
|
|
|
|
|
windowsRead++;
|
|
|
|
|
}
|
|
|
|
|
calcPCMData();
|
|
|
|
|
fft.realForward(PCM);
|
|
|
|
|
updated = true;
|
|
|
|
|
windowsRead++;
|
|
|
|
|
|
|
|
|
|
//contemplate synchronization
|
|
|
|
|
try {
|
|
|
|
|
currentPlaybackWindow = MathUtils.round((mc.getCurrentPosition() * sampleRate) / windowSize);
|
|
|
|
@@ -182,9 +180,8 @@ public class PCMObtainer implements Observer, PCMSystem {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (windowsRead != currentPlaybackWindow) {
|
|
|
|
|
read = synchronizeBufferWithPlayback();
|
|
|
|
|
synchronizeBufferWithPlayback();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//wait for a bit before reading again depending on the speed at which the system does playback.
|
|
|
|
|
waitTime = Math.max(0, millisPerWindow - TimeUtils.timeSinceMillis(timeOfLastRead));
|
|
|
|
|
try {
|
|
|
|
@@ -203,6 +200,7 @@ public class PCMObtainer implements Observer, PCMSystem {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Gdx.app.debug(thread.getName(), "stopped");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void start() {
|
|
|
|
@@ -218,7 +216,6 @@ public class PCMObtainer implements Observer, PCMSystem {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void stop() {
|
|
|
|
|
Gdx.app.debug("PCMObtainer", "stopping " + thread.getName());
|
|
|
|
|
run = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|