Picovoice / porcupine

On-device wake word detection powered by deep learning
https://picovoice.ai/
Apache License 2.0
3.79k stars 504 forks source link

Loading Porcupine via JNI interface in Unity #100

Closed rtumelty closed 6 years ago

rtumelty commented 6 years ago

Hi, I have been trying to integrate Porcupine into a proof of concept demo built for Android in Unity. I am attempting to access the ARMv7 .so library supplied in the GitHub repo using the DllImport function. The library appears to load correctly (as I do not receive a DllNotFoundException), however I am experiencing EntryPointNotFoundExceptions when trying to call the native functions. I think perhaps I am declaring the function calls incorrectly - if you can help me see where I'm going wrong I'd greatly appreciate it! See relevant code below.

` public class PorcupineManager : ScriptableObject {

...

[DllImport("pv_porcupine")]
private static extern long init(string modelFilePath, string[] keywordFilePaths, float[] sensitivities);
[DllImport("pv_porcupine")]
private static extern int process(long porcupineObjectId, short[] pcm);
[DllImport("pv_porcupine")]
private static extern void delete(long porcupineObjectId);

public void Init() {
    Debug.Log("Attempting porcupine init: \n"
    + Path.Combine(Application.dataPath, modelPath) + "\n"
    + Path.Combine(Application.dataPath, keywordPath));
    porcupineObject = init(Path.Combine(Application.dataPath, modelPath), new string[] { Path.Combine(Application.dataPath, keywordPath)}, new float[] { sensitivity });
    porcupineLoaded = true;
}

...

} `

Excerpt from log:

2018-10-16 17:51:06.654 19176-19208/com.meowtek.commandandcontrol E/Unity: EntryPointNotFoundException: init at (wrapper managed-to-native) PorcupineManager:init (string,string[],single[]) at PorcupineManager.Init () [0x0006e] in /Users/Ronan/Unity Projects/CommandAndControl/Assets/Scripts/Porcupine/PorcupineManager.cs:40 at PorcupineTest+c__Iterator0.MoveNext () [0x0005d] in /Users/Ronan/Unity Projects/CommandAndControl/Assets/Scripts/Porcupine/PorcupineTest.cs:17 at UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) [0x00028] in /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17

kenarsa commented 6 years ago

isn't this more of a question for unity folks?

rtumelty commented 6 years ago

Not really - my issue isn't so much with how to do external calls to Unity (I know how to do that). It's to do with how exactly the functions are declared in the Porcupine library, as my attempt to mirror the calls in the Android demo aren't finding the entry points in the library. Ideally I'm looking for an API reference.

kenarsa commented 6 years ago

You mean the C JNI interface?

rtumelty commented 6 years ago

Documentation on it, yes. I have found the relevant calls in Porcupine.java:

` private native long init(String modelFilePath, String[] keywordFilePaths, float[] sensitivities);

private native int process(long object, short[] pcm);

private native void delete(long object);`

And created corresponding external calls in my Unity C#: [DllImport("pv_porcupine")] private static extern long init(string modelFilePath, string[] keywordFilePaths, float[] sensitivities); [DllImport("pv_porcupine")] private static extern int process(long porcupineObjectId, short[] pcm); [DllImport("pv_porcupine")] private static extern void delete(long porcupineObjectId);

Clearly though, something doesn't match up, and is causing the entry point for the library to not be found - I'm guessing it's something to do with the parameters passed.

kenarsa commented 6 years ago

I think its the way methods are named. JNI has a specific way of doing it. Why don't you use the build for armlinux or RPi (2/3)? You can build your own JNI on top of it. At this point, we do not have plans to open source the JNI code as there is proprietary information in it.