Skip to content

Commit

Permalink
Improve logging with custom callback
Browse files Browse the repository at this point in the history
  • Loading branch information
fgnm committed Dec 18, 2023
1 parent 483a3e7 commit 0cd5a97
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/main/java/games/rednblack/miniaudio/MALogCallback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package games.rednblack.miniaudio;

/**
* Listener to get notified when native code generate some kind of logs.
*
* NOTE: this callback may be invoked on different threads.
*
* @author fgnm
*/
public interface MALogCallback {
void onLog(MALogLevel level, String message);
}
15 changes: 15 additions & 0 deletions src/main/java/games/rednblack/miniaudio/MALogLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,19 @@ public enum MALogLevel {
MALogLevel(int code) {
this.code = code;
}

public static MALogLevel decode (int code) {
switch (code) {
case 1:
return MALogLevel.ERROR;
case 2:
return MALogLevel.WARNING;
case 3:
return MALogLevel.INFO;
case 4:
return MALogLevel.DEBUG;
default:
return MALogLevel.NONE;
}
}
}
22 changes: 20 additions & 2 deletions src/main/java/games/rednblack/miniaudio/MiniAudio.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,25 @@ void ma_log_callback_jni(void* pUserData, ma_uint32 level, const char* pMessage)
private long engineAddress = 0;
private final MASound endCallbackSound;
private MASoundEndListener endListener;
private MALogCallback logCallback;

/**
* Create a new MiniAudio Engine Instance
*
*/
public MiniAudio() {
this(true);
this(null, true);
}

/**
* Create a new MiniAudio Engine Instance
*
* @param logCallback callback to forward native logs
* @param initEngine automatic init engine with default parameters
*/
public MiniAudio(boolean initEngine) {
public MiniAudio(MALogCallback logCallback, boolean initEngine) {
this.logCallback = logCallback;

int result = jniInitContext();
if (result != MAResult.MA_SUCCESS) {
throw new MiniAudioException("Unable to init MiniAudio Context", result);
Expand Down Expand Up @@ -1948,7 +1952,21 @@ public void on_native_sound_end(long soundAddress) {
}
}

/**
* Set a custom log callback function. If null, default libGDX logging will be used.
*
* @param logCallback custom log callback
*/
public void setLogCallback(MALogCallback logCallback) {
this.logCallback = logCallback;
}

public void on_native_log(int level, String message) {
if (logCallback != null) {
logCallback.onLog(MALogLevel.decode(level), message);
return;
}

if (Gdx.app == null) return;

switch (level) {
Expand Down
11 changes: 5 additions & 6 deletions testApp/src/main/java/games/rednblack/miniaudio/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ public static void main(String[] args) {
@Override
public void create() {
//miniAudio = new MiniAudio(1, 1, 0, 256, 44100);
miniAudio = new MiniAudio(false);
miniAudio.setLogLevel(MALogLevel.DEBUG);
miniAudio = new MiniAudio(null, false);
MADeviceInfo[] devices = miniAudio.enumerateDevices();
for (MADeviceInfo info : devices) {
System.out.println(info.isCapture + " " + info.idAddress + " . " + info.name);
}
miniAudio.initEngine(1, -1, -1, 2, 0, 512, 44100, MAFormatType.F32,false);
miniAudio.initEngine(1, -1, -1, 2, 0, 512, 44100, MAFormatType.F32,false, false, false);
miniAudio.setListenerDirection(0, 0, 1);
miniAudio.setListenerCone(MathUtils.PI / 4f, MathUtils.PI / 4f, 2f);

Expand All @@ -49,7 +48,7 @@ public void create() {
//int res = miniAudio.playSound("Median_test.ogg");
//int res = miniAudio.playSound("piano2.wav");
//System.out.println(res);
maSound = miniAudio.createSound("Legendary.mp3");
maSound = miniAudio.createSound("piano2.wav");
//maSound.setPositioning(MAPositioning.RELATIVE);
//maSound = miniAudio.createInputSound((short) 0, null);
maSound.setSpatialization(false);
Expand Down Expand Up @@ -103,9 +102,9 @@ public void create() {
assetManager = new AssetManager();
assetManager.getLogger().setLevel(Logger.DEBUG);
assetManager.setLoader(MASound.class, new MASoundLoader(miniAudio, assetManager.getFileHandleResolver()));
assetManager.load("game.ogg", MASound.class);
//assetManager.load("game.ogg", MASound.class);
assetManager.load("Median_test.ogg", MASound.class);
assetManager.load("Perfect_Mishap.ogg", MASound.class);
//assetManager.load("Perfect_Mishap.ogg", MASound.class);
assetManager.load("piano2.wav", MASound.class);
}

Expand Down
Binary file modified testApp/src/main/resources/libgdx-miniaudio64.so
Binary file not shown.

0 comments on commit 0cd5a97

Please sign in to comment.