Coder Social home page Coder Social logo

Comments (10)

zsmaguc avatar zsmaguc commented on September 26, 2024 1

Win x64
libjni.zip

from porcupine.

kenarsa avatar kenarsa commented on September 26, 2024

this should work across different platforms ...
https://github.com/Picovoice/Porcupine/tree/master/demo/android

from porcupine.

tatanpoker09 avatar tatanpoker09 commented on September 26, 2024

But the binary files it uses are .so, I need .dll files in order to work with windows. Problem is, you can even see that those .so files in that android demo aren't regular, that .so is modified from the original in order to meet the required wrapping, you can see that in the next picture
image
As you can see, the methods were changed into Java_ai_picovoice_porcupine_Porcupine_method, along with JNICALL, JNIExport, JNIEnv*, etc etc.
In that same sense, I need those binaries but in dll form

from porcupine.

kenarsa avatar kenarsa commented on September 26, 2024

Got it. You don't want an Android build. Just want a JNI interface. I think you can use the ".a" files under lib/windows with your own JNI file and then build a ".dll" from them. Apologies for the confusion.

from porcupine.

tatanpoker09 avatar tatanpoker09 commented on September 26, 2024

I can't seem to modify them in any way, I'll try linking them to another c++ "wrapper program", with the wrapping methods and just a call inside.

from porcupine.

tatanpoker09 avatar tatanpoker09 commented on September 26, 2024

Alright, so after finally being able to build a succesful .dll with a JNI interface, I am encountering this error, which is directly linked to the dll.

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000067641f65, pid=1040, tid=0x0000000000000ee8
#
# JRE version: Java(TM) SE Runtime Environment (8.0_161-b12) (build 1.8.0_161-b12)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.161-b12 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [libpv_porcupine.dll+0x1f65]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\Programming\Game Development\PorcupineManager\hs_err_pid1040.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

This is how it is being called from Java:

public Porcupine(
            String modelFilePath,
            String keywordFilePath,
            float sensitivity) throws PorcupineException {
        numberKeywords = 1;
        System.out.println("Sensitivity: "+sensitivity);
        System.out.println("Modelfilepath: "+modelFilePath);
        System.out.println(keywordFilePath);
        System.out.println("==================");
        try {
            object = init(modelFilePath, new String[]{keywordFilePath}, new float[]{sensitivity});
        } catch (Exception e) {
            throw new PorcupineException(e);
        }
    }

And this is in C:

 JNIEXPORT jlong JNICALL Java_ai_picovoice_porcupine_Porcupine_init(JNIEnv *env, jobject thisObj, jstring str, jobjectArray objArray, jfloatArray floatArray){

    jfloat *body = (*env)->GetFloatArrayElements(env, floatArray, 0);
    const char *modelFilePath = (*env)->GetStringUTFChars(env, str, 0);

    int modelCount = (*env)->GetArrayLength(env, objArray);
    printf("Sensitivity: ");
    printf("%f \n", body[0]);
    printf("Modelfilepath: %s \n", modelFilePath);

    const char *keyfilepaths[modelCount];

    for (int i=0; i<modelCount; i++) {
      jstring string = (jstring) (*env)->GetObjectArrayElement(env, objArray, i);
      const char *keyfilepath = (*env)->GetStringUTFChars(env, string, 0);
      keyfilepaths[i] = keyfilepath;

        (*env)->ReleaseStringUTFChars(env, string, keyfilepath);
        printf("KeywordFile: %s", keyfilepaths[i]);
    }

    pv_porcupine_multiple_keywords_init(modelFilePath, modelCount, keyfilepaths, body, object);

      (*env)->ReleaseFloatArrayElements(env, floatArray, body, 0);
      (*env)->ReleaseStringUTFChars(env, str, modelFilePath);

    return object;
  }

Please help, I seriously don't know anymore.

Here's the error file: https://pastebin.com/kEZ5JA0k

from porcupine.

zsmaguc avatar zsmaguc commented on September 26, 2024

I put this together, without any knowledge of c/c++. It works, but there is no error handling and there are probably some other mistakes.

It would be nice if Porcupine team shared their implementation of native JNI functions from the Android demo project.

#include "ai_picovoice_porcupine_Porcupine.h"
#include "../include/picovoice.h"
#include "../include/pv_porcupine.h"
#include <jni.h>
#include <string.h>
#include <iostream>

using namespace std;

//TODO: error handling

JNIEXPORT jint JNICALL Java_ai_picovoice_porcupine_Porcupine_getFrameLength(JNIEnv *env, jobject obj) {
    return pv_porcupine_frame_length();
}

JNIEXPORT jint JNICALL Java_ai_picovoice_porcupine_Porcupine_getSampleRate(JNIEnv *env, jobject obj) {
    return pv_sample_rate();
}

JNIEXPORT jlong JNICALL Java_ai_picovoice_porcupine_Porcupine_init(JNIEnv *env, jobject obj, jstring modelFile, jobjectArray keywordFiles, jfloatArray sensitivities) {
    long object = 0;
    pv_porcupine_object_t *result = (pv_porcupine_object_t *) &object;

    jclass thisClass = env->GetObjectClass(obj);
    jfieldID fidNumber = env->GetFieldID(thisClass, "numberKeywords", "I");
    jint numberKeywords = env->GetIntField(obj, fidNumber);

    const char *ckwFiles[numberKeywords];
    for (int i=0; i<numberKeywords; i++) {
        jstring jkwFile = (jstring) env->GetObjectArrayElement(keywordFiles, i);
        const char *ckwFile = env->GetStringUTFChars(jkwFile, NULL);
        ckwFiles[i] = strdup(ckwFile);
        env->ReleaseStringUTFChars(jkwFile, ckwFile);
        env->DeleteLocalRef(jkwFile);
    }

    const char *modelFileCStr = env->GetStringUTFChars(modelFile, NULL);
    jfloat *senBody = env->GetFloatArrayElements(sensitivities, 0);

    pv_porcupine_multiple_keywords_init(modelFileCStr, numberKeywords, ckwFiles, senBody, &result);

    env->ReleaseFloatArrayElements(sensitivities, senBody, 0);
    env->ReleaseStringUTFChars(modelFile, modelFileCStr);

    return (jlong) result;
}

JNIEXPORT jint JNICALL Java_ai_picovoice_porcupine_Porcupine_process(JNIEnv *env, jobject obj, jlong porcupineObject, jshortArray pcm) {
    int result;

    jshort *body = env->GetShortArrayElements(pcm, 0);

    pv_porcupine_multiple_keywords_process((pv_porcupine_object_t *) porcupineObject, body, &result);

    env->ReleaseShortArrayElements(pcm, body, 0);

    return result;
}

JNIEXPORT void JNICALL Java_ai_picovoice_porcupine_Porcupine_delete(JNIEnv *env, jobject obj, jlong porcupineObject) {
    pv_porcupine_delete((pv_porcupine_object_t *) porcupineObject);
}

from porcupine.

tatanpoker09 avatar tatanpoker09 commented on September 26, 2024

@zsmaguc Same error is happening, do you think you can provide me the dll you got from that code? AMD64, Windows

from porcupine.

tatanpoker09 avatar tatanpoker09 commented on September 26, 2024

You just saved my life, I love you <3

from porcupine.

cogmeta avatar cogmeta commented on September 26, 2024

@tatanpoker09 and @zsmaguc Please help. I am getting following error

exception in thread "main" java.lang.UnsatisfiedLinkError: C:\xxx\porcupine_java\ai\picovoice\porcupine\jni\libjni.dll: Can't find dependent libraries
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(Unknown Source)
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at ai.picovoice.porcupine.Porcupine.(Porcupine.java:29)

I have following libs in ./jni folder

20-02-2019 10:25

..
19-02-2019 23:10 51,330 libjni.dll
19-02-2019 22:49 141,942 libpv_porcupine.dll
19-03-2017 02:27 632,800 msvcrt.dll

from porcupine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.