Skip to content

updated for 0.7.1 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
128 changes: 75 additions & 53 deletions DeepSpeech-jni/org_mozilla_deepspeech_DeepSpeech.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
#include <iostream>
#include "org_mozilla_deepspeech_DeepSpeech.h"

jint
Java_org_mozilla_deepspeech_DeepSpeech_nCreateModel(JNIEnv *env, jclass, jstring modelPath,
jlong nCep,
jlong nContext, jstring alphabetConfigPath,
jlong beamWidth,
jobject modelStatePtr) {
jboolean isModelPathCopy, isAlphaBetCopy;
jboolean isModelPathCopy;
ModelState *ptr = nullptr;
auto modelPathCStr = (char *) env->GetStringUTFChars(modelPath, &isModelPathCopy);
auto alphaBetCStr = (char *) env->GetStringUTFChars(alphabetConfigPath, &isAlphaBetCopy);

jint state = DS_CreateModel(modelPathCStr, static_cast<unsigned int>(nCep),
static_cast<unsigned int>(nContext), alphaBetCStr,
static_cast<unsigned int>(beamWidth),
&ptr);
// https://github.com/mozilla/DeepSpeech/commit/8c820817794d445746aefb1b5347b35bf5e0c621#diff-0317a0e76ece10e0dba742af310a2362
jint state = DS_CreateModel(modelPathCStr, &ptr);

auto *bufferPtr = (jlong *) (env->GetDirectBufferAddress(modelStatePtr));

bufferPtr[0] = reinterpret_cast<jlong>(ptr);
Expand All @@ -26,46 +22,62 @@ Java_org_mozilla_deepspeech_DeepSpeech_nCreateModel(JNIEnv *env, jclass, jstring
}

void Java_org_mozilla_deepspeech_DeepSpeech_destroyModel(JNIEnv *, jclass, jlong modelPtr) {
DS_DestroyModel(reinterpret_cast<ModelState *>(modelPtr));
DS_FreeModel(reinterpret_cast<ModelState *>(modelPtr));
}

jint
Java_org_mozilla_deepspeech_DeepSpeech_getModelBeamWidth(JNIEnv *env, jclass, jlong modelStatePtr) {
return DS_GetModelBeamWidth((ModelState *) modelStatePtr);
}

jint
Java_org_mozilla_deepspeech_DeepSpeech_setModelBeamWidth(JNIEnv *env, jclass, jlong modelStatePtr, jlong beamWidth) {
return DS_SetModelBeamWidth((ModelState *) modelStatePtr, static_cast<unsigned int>(beamWidth));
}

jint
Java_org_mozilla_deepspeech_DeepSpeech_getModelSampleRate(JNIEnv *env, jclass, jlong modelStatePtr) {
return DS_GetModelSampleRate((ModelState *) modelStatePtr);
}

jint
Java_org_mozilla_deepspeech_DeepSpeech_enableDecoderWithLM(JNIEnv *env, jclass, jlong modelStatePtr,
jstring alphaBetConfigPath,
jstring lmPath,
jstring triePath, jfloat alpha,
jfloat beta) {
jboolean isAlphabetStrCopy, isLmPathCopy, isTriePathCopy;
auto alphaBetConfigPathCStr = const_cast<char *>(env->GetStringUTFChars(alphaBetConfigPath,
&isAlphabetStrCopy));
auto lmPathCStr = const_cast<char *>(env->GetStringUTFChars(lmPath, &isLmPathCopy));
auto triePathCStr = const_cast<char *>(env->GetStringUTFChars(triePath, &isTriePathCopy));

jint status = DS_EnableDecoderWithLM((ModelState *) modelStatePtr, alphaBetConfigPathCStr,
lmPathCStr, triePathCStr,
alpha, beta);

if (isAlphabetStrCopy == JNI_TRUE) {
env->ReleaseStringUTFChars(alphaBetConfigPath, alphaBetConfigPathCStr);
}
jstring scorerPath,
jfloat alpha, jfloat beta) {
jboolean isLmPathCopy;
auto scorerPathCStr = const_cast<char *>(env->GetStringUTFChars(scorerPath, &isLmPathCopy));

// https://github.com/mozilla/DeepSpeech/pull/2681/files
// lm: models/lm.binary, trie: models/trie
// became scorer: models/kenlm.scorer
DS_EnableExternalScorer((ModelState *) modelStatePtr, scorerPathCStr);
jint status = DS_SetScorerAlphaBeta((ModelState *) modelStatePtr, alpha, beta);

if (isLmPathCopy == JNI_TRUE) {
env->ReleaseStringUTFChars(lmPath, lmPathCStr);
}
if (isTriePathCopy == JNI_TRUE) {
env->ReleaseStringUTFChars(triePath, triePathCStr);
env->ReleaseStringUTFChars(scorerPath, scorerPathCStr);
}

return status;
}

jint
Java_org_mozilla_deepspeech_DeepSpeech_setScorerAlphaBeta(JNIEnv *env, jclass, jlong modelStatePtr,
jfloat alpha, jfloat beta) {
return DS_SetScorerAlphaBeta((ModelState *) modelStatePtr, alpha, beta);
}

jint
Java_org_mozilla_deepspeech_DeepSpeech_disableExternalScorer(JNIEnv *env, jclass, jlong modelStatePtr) {
jint status = DS_DisableExternalScorer((ModelState *) modelStatePtr);
return status;
}

jstring
Java_org_mozilla_deepspeech_DeepSpeech_nSpeechToText(JNIEnv *env, jclass, jlong modelStatePtr,
jobject audioBuffer, jlong numSamples,
jlong sampleRate) {
jobject audioBuffer, jlong numSamples) {
auto *array = (short *) (env->GetDirectBufferAddress(audioBuffer));
char *cStr = DS_SpeechToText((ModelState *) modelStatePtr, array,
static_cast<unsigned int>(numSamples),
(unsigned int) sampleRate);
static_cast<unsigned int>(numSamples));
if (cStr == nullptr) {
return nullptr;
}
Expand All @@ -76,12 +88,10 @@ Java_org_mozilla_deepspeech_DeepSpeech_nSpeechToText(JNIEnv *env, jclass, jlong

jstring
Java_org_mozilla_deepspeech_DeepSpeech_speechToTextUnsafe(JNIEnv *env, jclass, jlong modelStatePtr,
jlong audioBuffer, jlong numSamples,
jlong sampleRate) {
jlong audioBuffer, jlong numSamples) {
auto *array = (short *) (audioBuffer);
char *cStr = DS_SpeechToText((ModelState *) modelStatePtr, array,
static_cast<unsigned int>(numSamples),
(unsigned int) sampleRate);
static_cast<unsigned int>(numSamples));
if (cStr == nullptr) {
return nullptr;
}
Expand All @@ -95,36 +105,34 @@ Java_org_mozilla_deepspeech_DeepSpeech_nSpeechToTextWithMetadata(JNIEnv *env, jc
jlong modelStatePtr,
jobject audioBuffer,
jlong bufferSize,
jlong sampleRate) {
jlong numResults) {
auto *array = static_cast<short *>(env->GetDirectBufferAddress(audioBuffer));
auto metaPtr = reinterpret_cast<jlong>(DS_SpeechToTextWithMetadata((ModelState *) modelStatePtr,
array,
static_cast<unsigned int>(bufferSize),
static_cast<unsigned int>(sampleRate)));
static_cast<unsigned int>(numResults)));
return metaPtr;
}
jlong
Java_org_mozilla_deepspeech_DeepSpeech_speechToTextWithMetadataUnsafe(JNIEnv *, jclass,
jlong modelStatePtr,
jlong audioBuffer,
jlong bufferSize,
jlong sampleRate) {
jlong numResults) {
auto *array = (short *)audioBuffer;
auto metaPtr = reinterpret_cast<jlong>(DS_SpeechToTextWithMetadata((ModelState *) modelStatePtr,
array,
static_cast<unsigned int>(bufferSize),
static_cast<unsigned int>(sampleRate)));
static_cast<unsigned int>(numResults)));
return metaPtr;
}

jint Java_org_mozilla_deepspeech_DeepSpeech_nSetupStream(JNIEnv *env, jclass, jlong modelStatePtr,
jlong preAllocFrames, jlong sampleRate,
jobject streamPtr) {
StreamingState *pStreamingState;

jint status = DS_SetupStream((ModelState *) modelStatePtr,
static_cast<unsigned int>(preAllocFrames),
static_cast<unsigned int>(sampleRate), &pStreamingState);
jint status = DS_CreateStream((ModelState *) modelStatePtr, &pStreamingState);

auto p = (StreamingState **) env->GetDirectBufferAddress(streamPtr);
*p = pStreamingState;
return status;
Expand Down Expand Up @@ -155,20 +163,34 @@ jstring Java_org_mozilla_deepspeech_DeepSpeech_finishStream(JNIEnv *env, jclass,
}

jlong
Java_org_mozilla_deepspeech_DeepSpeech_finishStreamWithMetadata(JNIEnv *, jclass, jlong streamPtr) {
return reinterpret_cast<jlong>(DS_FinishStreamWithMetadata((StreamingState *) streamPtr));
Java_org_mozilla_deepspeech_DeepSpeech_finishStreamWithMetadata(JNIEnv *, jclass, jlong streamPtr, jlong numResults) {
return reinterpret_cast<jlong>(DS_FinishStreamWithMetadata((StreamingState *) streamPtr, numResults));
}

void Java_org_mozilla_deepspeech_DeepSpeech_discardStream(JNIEnv *, jclass, jlong streamPtr) {
DS_DiscardStream((StreamingState *) streamPtr);
DS_FreeStream((StreamingState *) streamPtr);
}

void Java_org_mozilla_deepspeech_DeepSpeech_freeMetadata(JNIEnv *, jclass, jlong metaPtr) {
DS_FreeMetadata((Metadata *) metaPtr);
}

void Java_org_mozilla_deepspeech_DeepSpeech_printVersions(JNIEnv *, jclass) {
DS_PrintVersions();
jstring Java_org_mozilla_deepspeech_DeepSpeech_getVersion(JNIEnv *env, jclass) {
char *cString = DS_Version();
size_t cStrLen = strlen(cString);
jstring str = env->NewString(reinterpret_cast<const jchar *>(cString),
static_cast<jsize>(cStrLen));
DS_FreeString(cString);
return str;
}

jstring Java_org_mozilla_deepspeech_DeepSpeech_errorCodeToErrorMessage(JNIEnv *env, jclass, jlong errorCode) {
char *cString = DS_ErrorCodeToErrorMessage(errorCode);
size_t cStrLen = strlen(cString);
jstring str = env->NewString(reinterpret_cast<const jchar *>(cString),
static_cast<jsize>(cStrLen));
DS_FreeString(cString);
return str;
}

jint Java_org_mozilla_deepspeech_DeepSpeech_nGetConfiguration(JNIEnv *, jclass) {
Expand All @@ -181,4 +203,4 @@ jint Java_org_mozilla_deepspeech_DeepSpeech_nGetConfiguration(JNIEnv *, jclass)
return BuildConfiguration::INVALID; // This should never be returned
#endif
#endif
}
}
31 changes: 25 additions & 6 deletions DeepSpeech-jni/org_mozilla_deepspeech_DeepSpeech.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,35 @@ enum BuildConfiguration {
};

JNIEXPORT jint JNICALL Java_org_mozilla_deepspeech_DeepSpeech_nCreateModel
(JNIEnv *, jclass, jstring, jlong, jlong, jstring, jlong, jobject);
(JNIEnv *, jclass, jstring, jobject);

JNIEXPORT void JNICALL Java_org_mozilla_deepspeech_DeepSpeech_destroyModel
(JNIEnv *, jclass, jlong);


JNIEXPORT jint JNICALL Java_org_mozilla_deepspeech_DeepSpeech_getModelBeamWidth
(JNIEnv *, jclass, jlong);

JNIEXPORT jint JNICALL Java_org_mozilla_deepspeech_DeepSpeech_setModelBeamWidth
(JNIEnv *, jclass, jlong, jlong);

JNIEXPORT jint JNICALL Java_org_mozilla_deepspeech_DeepSpeech_getModelSampleRate
(JNIEnv *, jclass, jlong);

JNIEXPORT jint JNICALL Java_org_mozilla_deepspeech_DeepSpeech_enableDecoderWithLM
(JNIEnv *, jclass, jlong, jstring, jstring, jstring, jfloat, jfloat);
(JNIEnv *, jclass, jlong, jstring, jfloat, jfloat);

JNIEXPORT jint JNICALL Java_org_mozilla_deepspeech_DeepSpeech_setScorerAlphaBeta
(JNIEnv *env, jclass, jlong, jfloat, jfloat);

JNIEXPORT jint JNICALL Java_org_mozilla_deepspeech_DeepSpeech_disableExternalScorer
(JNIEnv *env, jclass, jlong);

JNIEXPORT jstring JNICALL Java_org_mozilla_deepspeech_DeepSpeech_nSpeechToText
(JNIEnv *, jclass, jlong, jobject, jlong, jlong);
(JNIEnv *, jclass, jlong, jobject, jlong);

JNIEXPORT jstring JNICALL Java_org_mozilla_deepspeech_DeepSpeech_speechToTextUnsafe
(JNIEnv *, jclass, jlong, jlong, jlong, jlong);
(JNIEnv *, jclass, jlong, jlong, jlong);

JNIEXPORT jlong JNICALL Java_org_mozilla_deepspeech_DeepSpeech_nSpeechToTextWithMetadata
(JNIEnv *, jclass, jlong, jobject, jlong, jlong);
Expand All @@ -37,7 +53,7 @@ JNIEXPORT jlong JNICALL Java_org_mozilla_deepspeech_DeepSpeech_speechToTextWithM
(JNIEnv *, jclass, jlong, jlong, jlong, jlong);

JNIEXPORT jint JNICALL Java_org_mozilla_deepspeech_DeepSpeech_nSetupStream
(JNIEnv *, jclass, jlong, jlong, jlong, jobject);
(JNIEnv *, jclass, jlong, jobject);

JNIEXPORT void JNICALL Java_org_mozilla_deepspeech_DeepSpeech_nFeedAudioContent
(JNIEnv *, jclass, jlong, jobject, jlong);
Expand All @@ -57,9 +73,12 @@ JNIEXPORT void JNICALL Java_org_mozilla_deepspeech_DeepSpeech_discardStream
JNIEXPORT void JNICALL Java_org_mozilla_deepspeech_DeepSpeech_freeMetadata
(JNIEnv *, jclass, jlong);

JNIEXPORT void JNICALL Java_org_mozilla_deepspeech_DeepSpeech_printVersions
JNIEXPORT jstring JNICALL Java_org_mozilla_deepspeech_DeepSpeech_getVersion
(JNIEnv *, jclass);

JNIEXPORT jstring JNICALL Java_org_mozilla_deepspeech_DeepSpeech_errorCodeToErrorMessage
(JNIEnv *, jclass, jlong);

JNIEXPORT jint JNICALL Java_org_mozilla_deepspeech_DeepSpeech_nGetConfiguration
(JNIEnv *, jclass);

Expand Down
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM java:8

COPY . /idear
WORKDIR /idear
RUN ./gradlew buildPlugin

13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
default:
@docker run --rm -v $(PWD):/idear -w /idear openasr/idear ./gradlew buildPlugin

test:
@docker run --rm -v $(PWD):/idear -w /idear openasr/idear ./gradlew test

docker:
@rm -rf build out
@docker build -t openasr/idear .

push:
@docker push openasr/idear:latest

52 changes: 50 additions & 2 deletions libdeepspeech/build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
plugins {
id 'java'
id 'distribution'
id 'maven-publish'
}

group 'org.mozilla.deepspeech'
version '1.0'
version '1.0.4-SNAPSHOT'

sourceCompatibility = 1.6

Expand All @@ -14,4 +16,50 @@ repositories {
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile("org.jetbrains:annotations:16.0.1") // Jetbrains annotations library
}
}

//task linuxJniZip(type: Zip) {
// archiveName("deepspeech-jni.zip")
// destinationDir(file("$buildDir/dist"))
//// archiveFileName = "deepspeech-jni.zip"
//// destinationDirectory = file("$buildDir/target")
// from "../DeepSpeech-jni/libs/linux"
//}

//artifacts {
// linuxJniZip
//}

distributions {
linux {
contents {
from '../DeepSpeech-jni/libs/linux'
}
}
windows {
contents {
from '../DeepSpeech-jni/libs/windows'
}
}
}

publishing {
publications {
libdeepspeech(MavenPublication) {
from components.java
artifact linuxDistZip
artifact windowsDistZip
}
}
repositories {
maven {
name "GitHubPackages"
url "https://maven.pkg.github.com/" + System.getenv('GITHUB_REPOSITORY')
credentials {
username System.getenv('GITHUB_ACTOR')
password System.getenv('GITHUB_TOKEN')
}
}
}
}

Loading