visualizer now directly grabs data from openal.. slightly less synched for better efficiency?
This commit is contained in:
@@ -265,4 +265,14 @@ public class Mp3Manager implements MusicManager {
|
||||
public int framesRead() {
|
||||
return readIndex;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getChannelCount() {
|
||||
return channels;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Music getMusic() {
|
||||
return playbackMusic;
|
||||
}
|
||||
}
|
||||
|
@@ -141,4 +141,14 @@ public class WAVManager implements MusicManager {
|
||||
public int framesRead() {
|
||||
return readIndex;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getChannelCount() {
|
||||
return d.getChannels();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Music getMusic() {
|
||||
return music;
|
||||
}
|
||||
}
|
||||
|
@@ -1,10 +1,21 @@
|
||||
package zero1hd.rhythmbullet.desktop.graphics.ui.components;
|
||||
|
||||
import static org.lwjgl.openal.AL10.*;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ShortBuffer;
|
||||
|
||||
import org.lwjgl.openal.AL11;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.backends.lwjgl.audio.OpenALMusic;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Widget;
|
||||
import com.badlogic.gdx.utils.Disposable;
|
||||
import com.badlogic.gdx.utils.reflect.ClassReflection;
|
||||
import com.badlogic.gdx.utils.reflect.Field;
|
||||
import com.badlogic.gdx.utils.reflect.ReflectionException;
|
||||
|
||||
import zero1hd.rhythmbullet.audio.visualizer.BasicVisualizer;
|
||||
import zero1hd.rhythmbullet.util.MusicManager;
|
||||
@@ -14,8 +25,20 @@ public class Visualizer extends Widget implements Disposable {
|
||||
private boolean updatePositioning = true;
|
||||
private boolean mmSet;
|
||||
private MusicManager mm;
|
||||
private ShortBuffer buffer;
|
||||
private int sourceID;
|
||||
public Visualizer() {
|
||||
vis = new BasicVisualizer();
|
||||
|
||||
try {
|
||||
Field tempBufferField = ClassReflection.getDeclaredField(OpenALMusic.class, "tempBuffer");
|
||||
tempBufferField.setAccessible(true);
|
||||
buffer = ((ByteBuffer) tempBufferField.get(null)).asShortBuffer();
|
||||
} catch (IllegalArgumentException | SecurityException | ReflectionException e) {
|
||||
e.printStackTrace();
|
||||
Gdx.app.debug("Visualizer reflection", "Failed attempt at retrieving tempBuffer field.");
|
||||
Gdx.app.exit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -34,18 +57,47 @@ public class Visualizer extends Widget implements Disposable {
|
||||
mmSet = true;
|
||||
}
|
||||
|
||||
vis.modify(delta);
|
||||
vis.update(delta);
|
||||
updateVisualizerProperties();
|
||||
if (updatePositioning) {
|
||||
vis.updatePositionInfo();
|
||||
vis.setxPos((getWidth() - vis.getActualWidth())/2f);
|
||||
}
|
||||
if (mmSet) {
|
||||
setupPCMData();
|
||||
}
|
||||
vis.calculate(delta);
|
||||
super.act(delta);
|
||||
}
|
||||
|
||||
public void setupPCMData() {
|
||||
short chanVal;
|
||||
|
||||
int pos = (int) ((alGetSourcef(sourceID, AL11.AL_SAMPLE_OFFSET)-4));
|
||||
try {
|
||||
buffer.position((int) Math.max(0, pos));
|
||||
} catch (IllegalArgumentException outOfBounds) {
|
||||
System.out.println(outOfBounds);
|
||||
}
|
||||
for (int sid = 0; sid < vis.getAudioPCM().length && sid < buffer.remaining(); sid++) {
|
||||
for (int channel = 0; channel < mm.getChannelCount(); channel ++) {
|
||||
if (vis.getAudioPCM()[sid] < (chanVal = buffer.get())) {
|
||||
vis.getAudioPCM()[sid] = chanVal;
|
||||
}
|
||||
}
|
||||
vis.getAudioPCM()[sid] /= Short.MAX_VALUE+1;
|
||||
}
|
||||
}
|
||||
|
||||
public void setMM(MusicManager mm) {
|
||||
try {
|
||||
Field sourceIDField = ClassReflection.getDeclaredField(OpenALMusic.class, "sourceID");
|
||||
sourceIDField.setAccessible(true);
|
||||
sourceID = (int) sourceIDField.get(mm.getMusic());
|
||||
} catch (ReflectionException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.mm = mm;
|
||||
mmSet = false;
|
||||
}
|
||||
|
Reference in New Issue
Block a user