diff --git a/native_client/java/jni/deepspeech.i b/native_client/java/jni/deepspeech.i index 4bbdc776..c028714c 100644 --- a/native_client/java/jni/deepspeech.i +++ b/native_client/java/jni/deepspeech.i @@ -6,6 +6,8 @@ %} %include "typemaps.i" +%include "enums.swg" +%javaconst(1); %include "arrays_java.i" // apply to DS_FeedAudioContent and DS_SpeechToText @@ -15,12 +17,6 @@ %pointer_functions(ModelState*, modelstatep); %pointer_functions(StreamingState*, streamingstatep); -%typemap(newfree) char* "DS_FreeString($1);"; - -%include "carrays.i" -%array_functions(struct TokenMetadata, TokenMetadata_array); -%array_functions(struct CandidateTranscript, CandidateTranscript_array); - %extend struct CandidateTranscript { /** * Retrieve one TokenMetadata element @@ -29,8 +25,8 @@ * * @return The TokenMetadata requested or null */ - TokenMetadata getToken(int i) { - return TokenMetadata_array_getitem(self->tokens, i); + const TokenMetadata& getToken(int i) { + return self->tokens[i]; } } @@ -42,8 +38,8 @@ * * @return The CandidateTranscript requested or null */ - CandidateTranscript getTranscript(int i) { - return CandidateTranscript_array_getitem(self->transcripts, i); + const CandidateTranscript& getTranscript(int i) { + return self->transcripts[i]; } ~Metadata() { @@ -58,9 +54,11 @@ %nodefaultctor TokenMetadata; %nodefaultdtor TokenMetadata; +%typemap(newfree) char* "DS_FreeString($1);"; %newobject DS_SpeechToText; %newobject DS_IntermediateDecode; %newobject DS_FinishStream; +%newobject DS_ErrorCodeToErrorMessage; %rename ("%(strip:[DS_])s") ""; diff --git a/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech/DeepSpeechModel.java b/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech/DeepSpeechModel.java index a5b339b3..eafa11e2 100644 --- a/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech/DeepSpeechModel.java +++ b/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech/DeepSpeechModel.java @@ -11,8 +11,15 @@ public class DeepSpeechModel { } // FIXME: We should have something better than those SWIGTYPE_* - SWIGTYPE_p_p_ModelState _mspp; - SWIGTYPE_p_ModelState _msp; + private SWIGTYPE_p_p_ModelState _mspp; + private SWIGTYPE_p_ModelState _msp; + + private void evaluateErrorCode(int errorCode) { + DeepSpeech_Error_Codes code = DeepSpeech_Error_Codes.swigToEnum(errorCode); + if (code != DeepSpeech_Error_Codes.ERR_OK) { + throw new RuntimeException("Error: " + impl.ErrorCodeToErrorMessage(errorCode) + " (0x" + Integer.toHexString(errorCode) + ")."); + } + } /** * @brief An object providing an interface to a trained DeepSpeech model. @@ -20,10 +27,12 @@ public class DeepSpeechModel { * @constructor * * @param modelPath The path to the frozen model graph. + * + * @throws RuntimeException on failure. */ public DeepSpeechModel(String modelPath) { this._mspp = impl.new_modelstatep(); - impl.CreateModel(modelPath, this._mspp); + evaluateErrorCode(impl.CreateModel(modelPath, this._mspp)); this._msp = impl.modelstatep_value(this._mspp); } @@ -43,10 +52,10 @@ public class DeepSpeechModel { * @param aBeamWidth The beam width used by the model. A larger beam width value * generates better results at the cost of decoding time. * - * @return Zero on success, non-zero on failure. + * @throws RuntimeException on failure. */ - public int setBeamWidth(long beamWidth) { - return impl.SetModelBeamWidth(this._msp, beamWidth); + public void setBeamWidth(long beamWidth) { + evaluateErrorCode(impl.SetModelBeamWidth(this._msp, beamWidth)); } /** @@ -70,19 +79,19 @@ public class DeepSpeechModel { * * @param scorer The path to the external scorer file. * - * @return Zero on success, non-zero on failure (invalid arguments). + * @throws RuntimeException on failure. */ public void enableExternalScorer(String scorer) { - impl.EnableExternalScorer(this._msp, scorer); + evaluateErrorCode(impl.EnableExternalScorer(this._msp, scorer)); } /** * @brief Disable decoding using an external scorer. * - * @return Zero on success, non-zero on failure (invalid arguments). + * @throws RuntimeException on failure. */ public void disableExternalScorer() { - impl.DisableExternalScorer(this._msp); + evaluateErrorCode(impl.DisableExternalScorer(this._msp)); } /** @@ -91,10 +100,10 @@ public class DeepSpeechModel { * @param alpha The alpha hyperparameter of the decoder. Language model weight. * @param beta The beta hyperparameter of the decoder. Word insertion weight. * - * @return Zero on success, non-zero on failure (invalid arguments). + * @throws RuntimeException on failure. */ public void setScorerAlphaBeta(float alpha, float beta) { - impl.SetScorerAlphaBeta(this._msp, alpha, beta); + evaluateErrorCode(impl.SetScorerAlphaBeta(this._msp, alpha, beta)); } /* @@ -132,10 +141,12 @@ public class DeepSpeechModel { * and finishStream(). * * @return An opaque object that represents the streaming state. + * + * @throws RuntimeException on failure. */ public DeepSpeechStreamingState createStream() { SWIGTYPE_p_p_StreamingState ssp = impl.new_streamingstatep(); - impl.CreateStream(this._msp, ssp); + evaluateErrorCode(impl.CreateStream(this._msp, ssp)); return new DeepSpeechStreamingState(impl.streamingstatep_value(ssp)); } diff --git a/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/CandidateTranscript.java b/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/CandidateTranscript.java index c02b39ad..fa13c474 100644 --- a/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/CandidateTranscript.java +++ b/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/CandidateTranscript.java @@ -9,8 +9,8 @@ package org.mozilla.deepspeech.libdeepspeech; /** - * A single transcript computed by the model, including a confidence value and - * the metadata for its constituent tokens. + * A single transcript computed by the model, including a confidence
+ * value and the metadata for its constituent tokens. */ public class CandidateTranscript { private transient long swigCPtr; @@ -36,14 +36,7 @@ public class CandidateTranscript { } /** - * Array of TokenMetadata objects - */ - public void setTokens(TokenMetadata value) { - implJNI.CandidateTranscript_tokens_set(swigCPtr, this, TokenMetadata.getCPtr(value), value); - } - - /** - * Array of TokenMetadata objects + * Array of TokenMetadata objects */ public TokenMetadata getTokens() { long cPtr = implJNI.CandidateTranscript_tokens_get(swigCPtr, this); @@ -51,31 +44,15 @@ public class CandidateTranscript { } /** - * Size of the tokens array + * Size of the tokens array */ - public void setNum_tokens(int value) { - implJNI.CandidateTranscript_num_tokens_set(swigCPtr, this, value); - } - - /** - * Size of the tokens array - */ - public int getNum_tokens() { + public long getNum_tokens() { return implJNI.CandidateTranscript_num_tokens_get(swigCPtr, this); } /** - * Approximated confidence value for this transcript. This is roughly the - * sum of the acoustic model logit values for each timestep/character that - * contributed to the creation of this transcript. - */ - public void setConfidence(double value) { - implJNI.CandidateTranscript_confidence_set(swigCPtr, this, value); - } - - /** - * Approximated confidence value for this transcript. This is roughly the - * sum of the acoustic model logit values for each timestep/character that + * Approximated confidence value for this transcript. This is roughly the
+ * sum of the acoustic model logit values for each timestep/character that
* contributed to the creation of this transcript. */ public double getConfidence() { @@ -83,14 +60,14 @@ public class CandidateTranscript { } /** - * Retrieve one TokenMetadata element - * - * @param i Array index of the TokenMetadata to get - * + * Retrieve one TokenMetadata element
+ *
+ * @param i Array index of the TokenMetadata to get
+ *
* @return The TokenMetadata requested or null */ public TokenMetadata getToken(int i) { - return new TokenMetadata(implJNI.CandidateTranscript_getToken(swigCPtr, this, i), true); + return new TokenMetadata(implJNI.CandidateTranscript_getToken(swigCPtr, this, i), false); } } diff --git a/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/DeepSpeech_Error_Codes.java b/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/DeepSpeech_Error_Codes.java new file mode 100644 index 00000000..ed47183e --- /dev/null +++ b/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/DeepSpeech_Error_Codes.java @@ -0,0 +1,65 @@ +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 4.0.1 + * + * Do not make changes to this file unless you know what you are doing--modify + * the SWIG interface file instead. + * ----------------------------------------------------------------------------- */ + +package org.mozilla.deepspeech.libdeepspeech; + +public enum DeepSpeech_Error_Codes { + ERR_OK(0x0000), + ERR_NO_MODEL(0x1000), + ERR_INVALID_ALPHABET(0x2000), + ERR_INVALID_SHAPE(0x2001), + ERR_INVALID_SCORER(0x2002), + ERR_MODEL_INCOMPATIBLE(0x2003), + ERR_SCORER_NOT_ENABLED(0x2004), + ERR_FAIL_INIT_MMAP(0x3000), + ERR_FAIL_INIT_SESS(0x3001), + ERR_FAIL_INTERPRETER(0x3002), + ERR_FAIL_RUN_SESS(0x3003), + ERR_FAIL_CREATE_STREAM(0x3004), + ERR_FAIL_READ_PROTOBUF(0x3005), + ERR_FAIL_CREATE_SESS(0x3006), + ERR_FAIL_CREATE_MODEL(0x3007); + + public final int swigValue() { + return swigValue; + } + + public static DeepSpeech_Error_Codes swigToEnum(int swigValue) { + DeepSpeech_Error_Codes[] swigValues = DeepSpeech_Error_Codes.class.getEnumConstants(); + if (swigValue < swigValues.length && swigValue >= 0 && swigValues[swigValue].swigValue == swigValue) + return swigValues[swigValue]; + for (DeepSpeech_Error_Codes swigEnum : swigValues) + if (swigEnum.swigValue == swigValue) + return swigEnum; + throw new IllegalArgumentException("No enum " + DeepSpeech_Error_Codes.class + " with value " + swigValue); + } + + @SuppressWarnings("unused") + private DeepSpeech_Error_Codes() { + this.swigValue = SwigNext.next++; + } + + @SuppressWarnings("unused") + private DeepSpeech_Error_Codes(int swigValue) { + this.swigValue = swigValue; + SwigNext.next = swigValue+1; + } + + @SuppressWarnings("unused") + private DeepSpeech_Error_Codes(DeepSpeech_Error_Codes swigEnum) { + this.swigValue = swigEnum.swigValue; + SwigNext.next = this.swigValue+1; + } + + private final int swigValue; + + private static class SwigNext { + private static int next = 0; + } +} + diff --git a/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/Metadata.java b/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/Metadata.java index bb9b0773..d2831bc4 100644 --- a/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/Metadata.java +++ b/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/Metadata.java @@ -40,14 +40,7 @@ public class Metadata { } /** - * Array of CandidateTranscript objects - */ - public void setTranscripts(CandidateTranscript value) { - implJNI.Metadata_transcripts_set(swigCPtr, this, CandidateTranscript.getCPtr(value), value); - } - - /** - * Array of CandidateTranscript objects + * Array of CandidateTranscript objects */ public CandidateTranscript getTranscripts() { long cPtr = implJNI.Metadata_transcripts_get(swigCPtr, this); @@ -55,28 +48,21 @@ public class Metadata { } /** - * Size of the transcripts array + * Size of the transcripts array */ - public void setNum_transcripts(int value) { - implJNI.Metadata_num_transcripts_set(swigCPtr, this, value); - } - - /** - * Size of the transcripts array - */ - public int getNum_transcripts() { + public long getNum_transcripts() { return implJNI.Metadata_num_transcripts_get(swigCPtr, this); } /** - * Retrieve one CandidateTranscript element - * - * @param i Array index of the CandidateTranscript to get - * + * Retrieve one CandidateTranscript element
+ *
+ * @param i Array index of the CandidateTranscript to get
+ *
* @return The CandidateTranscript requested or null */ public CandidateTranscript getTranscript(int i) { - return new CandidateTranscript(implJNI.Metadata_getTranscript(swigCPtr, this, i), true); + return new CandidateTranscript(implJNI.Metadata_getTranscript(swigCPtr, this, i), false); } } diff --git a/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/README.rst b/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/README.rst index 1279d717..bd89f9b8 100644 --- a/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/README.rst +++ b/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/README.rst @@ -4,7 +4,7 @@ Javadoc for Sphinx This code is only here for reference for documentation generation. -To update, please build SWIG (4.0 at least) and then run from native_client/java: +To update, please install SWIG (4.0 at least) and then run from native_client/java: .. code-block:: diff --git a/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/TokenMetadata.java b/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/TokenMetadata.java index 32246f1a..d14fc161 100644 --- a/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/TokenMetadata.java +++ b/native_client/java/libdeepspeech/src/main/java/org/mozilla/deepspeech/libdeepspeech_doc/TokenMetadata.java @@ -35,42 +35,21 @@ public class TokenMetadata { } /** - * The text corresponding to this token - */ - public void setText(String value) { - implJNI.TokenMetadata_text_set(swigCPtr, this, value); - } - - /** - * The text corresponding to this token + * The text corresponding to this token */ public String getText() { return implJNI.TokenMetadata_text_get(swigCPtr, this); } /** - * Position of the token in units of 20ms + * Position of the token in units of 20ms */ - public void setTimestep(int value) { - implJNI.TokenMetadata_timestep_set(swigCPtr, this, value); - } - - /** - * Position of the token in units of 20ms - */ - public int getTimestep() { + public long getTimestep() { return implJNI.TokenMetadata_timestep_get(swigCPtr, this); } /** - * Position of the token in seconds - */ - public void setStart_time(float value) { - implJNI.TokenMetadata_start_time_set(swigCPtr, this, value); - } - - /** - * Position of the token in seconds + * Position of the token in seconds */ public float getStart_time() { return implJNI.TokenMetadata_start_time_get(swigCPtr, this);