igorski / MWEngine

Audio engine and DSP library for Android, written in C++ providing low latency performance within a musical context, while providing a Java/Kotlin API. Supports both OpenSL and AAudio.
MIT License
257 stars 45 forks source link

How to access and get data from SWIG correctly #134

Closed Riobener closed 3 years ago

Riobener commented 3 years ago

Good afternoon, the question may represent my complete lack of understanding of technology Swig. I tried to make some simple level meter. I noticed that for this job, your library has a class LevelUtility. Then i tried just to get some simple data to understand what I have to process later, and then got this in log: D/MWENGINE: LEVEL METER nl.igorski.mwengine.core.SWIGTYPE_p_SAMPLE_TYPE@3e95730

I'm 100% sure that i'm really wrong about how it should work, but i have some bad knowledges of c++, so i hoped, SWIG will help me to get access to variables and methods in c++ part of library, without writing any c++ line or using my custom JNI methods.

Here is what i expected that should work: Log.d(LOG_TAG, "LEVEL METER " + LevelUtility.linear(manager.getInstrument(0).getAudioChannel(),0));

I would be very happy if you could give an example of how to work correctly with LevelUtility and other classes like this from java code.

igorski commented 3 years ago

Ah great catch. That was completely broken!

The way SWIG works is that it provides an interface where you can use a C/C++ library from a higher level language. The idea is that nl.igorski.mwengine.core provides you with a Java interface which you can use without being concerned about the underlying native implementation.

What the problem was here is that the resolution of the samples (float or double) is defined in the native code upon build (using macro _SAMPLETYPE). This macro was not translated to the Java friendly float or double primitives. I have just pushed a fix for this.

The idea of the level utility is that it will provide you values representing the output volume in a range of options (e.g. 0 - 1 level, root mean square or dB). You can interpolate between the current and previous value in your UI where you can request a new value using an interval of your liking.

Riobener commented 3 years ago

It works fine now. Well done!