From db5ec4e93683460d87851c4bf98cc97eca6a1842 Mon Sep 17 00:00:00 2001 From: Ian Lavery Date: Thu, 23 Nov 2023 15:00:20 -0800 Subject: [PATCH] v2.0 java (#260) --- .github/workflows/java-demos.yml | 5 ++- .github/workflows/java-perf.yml | 34 +++++++------- .github/workflows/java.yml | 5 ++- binding/java/build.gradle | 2 +- .../gradle/wrapper/gradle-wrapper.properties | 2 +- .../src/ai/picovoice/cheetah/Cheetah.java | 8 ++++ .../ai/picovoice/cheetah/CheetahNative.java | 2 + .../exception/CheetahActivationException.java | 6 ++- .../CheetahActivationLimitException.java | 6 ++- .../CheetahActivationRefusedException.java | 6 ++- .../CheetahActivationThrottledException.java | 6 ++- .../cheetah/exception/CheetahException.java | 33 +++++++++++++- .../cheetah/exception/CheetahIOException.java | 6 ++- .../CheetahInvalidArgumentException.java | 6 ++- .../CheetahInvalidStateException.java | 6 ++- .../exception/CheetahKeyException.java | 6 ++- .../exception/CheetahMemoryException.java | 6 ++- .../exception/CheetahRuntimeException.java | 6 ++- .../CheetahStopIterationException.java | 6 ++- .../cheetah/CheetahPerformanceTest.java | 1 - .../ai/picovoice/cheetah/CheetahTest.java | 45 ++++++++++++++++++- demo/java/build.gradle | 9 ++-- .../gradle/wrapper/gradle-wrapper.properties | 2 +- 23 files changed, 172 insertions(+), 42 deletions(-) diff --git a/.github/workflows/java-demos.yml b/.github/workflows/java-demos.yml index 28ea36d8..60f8c061 100644 --- a/.github/workflows/java-demos.yml +++ b/.github/workflows/java-demos.yml @@ -26,14 +26,15 @@ jobs: strategy: matrix: os: [ubuntu-latest, windows-latest, macos-latest] + java-version: ['11', '17', '19'] steps: - uses: actions/checkout@v3 - - name: Set up JDK 11 + - name: Set up JDK ${{ matrix.java-version }} uses: actions/setup-java@v3 with: - java-version: '11' + java-version: ${{ matrix.java-version }} distribution: 'temurin' - name: Build diff --git a/.github/workflows/java-perf.yml b/.github/workflows/java-perf.yml index 43c668ef..a22846ff 100644 --- a/.github/workflows/java-perf.yml +++ b/.github/workflows/java-perf.yml @@ -31,14 +31,14 @@ jobs: os: [ubuntu-latest, macos-latest, windows-latest] include: - os: ubuntu-latest - init_performance_threshold_sec: 2.0 - proc_performance_threshold_sec: 0.5 - - os: windows-latest - init_performance_threshold_sec: 2.1 - proc_performance_threshold_sec: 0.6 + init_performance_threshold_sec: 4.5 + proc_performance_threshold_sec: 1.0 - os: macos-latest - init_performance_threshold_sec: 2.5 - proc_performance_threshold_sec: 0.7 + init_performance_threshold_sec: 5.5 + proc_performance_threshold_sec: 3.0 + - os: windows-latest + init_performance_threshold_sec: 4.5 + proc_performance_threshold_sec: 1.0 steps: - uses: actions/checkout@v3 @@ -64,20 +64,20 @@ jobs: machine: [rpi3-32, rpi3-64, rpi4-32, rpi4-64, jetson] include: - machine: rpi3-32 - init_performance_threshold_sec: 7.5 - proc_performance_threshold_sec: 3.3 + init_performance_threshold_sec: 14.0 + proc_performance_threshold_sec: 9.3 - machine: rpi3-64 - init_performance_threshold_sec: 8.8 - proc_performance_threshold_sec: 3.3 + init_performance_threshold_sec: 13.5 + proc_performance_threshold_sec: 8.3 - machine: rpi4-32 - init_performance_threshold_sec: 5.6 - proc_performance_threshold_sec: 2.0 + init_performance_threshold_sec: 8.5 + proc_performance_threshold_sec: 4.6 - machine: rpi4-64 - init_performance_threshold_sec: 5.1 - proc_performance_threshold_sec: 1.9 + init_performance_threshold_sec: 7.8 + proc_performance_threshold_sec: 5.0 - machine: jetson - init_performance_threshold_sec: 5.1 - proc_performance_threshold_sec: 2.0 + init_performance_threshold_sec: 7.8 + proc_performance_threshold_sec: 5.5 steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/java.yml b/.github/workflows/java.yml index 52de5021..6c115811 100644 --- a/.github/workflows/java.yml +++ b/.github/workflows/java.yml @@ -30,14 +30,15 @@ jobs: strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] + java-version: ['11', '17', '19'] steps: - uses: actions/checkout@v3 - - name: Set up JDK 11 + - name: Set up JDK ${{ matrix.java-version }} uses: actions/setup-java@v3 with: - java-version: '11' + java-version: ${{ matrix.java-version }} distribution: 'temurin' - name: Build diff --git a/binding/java/build.gradle b/binding/java/build.gradle index c5b4e266..1cdb5671 100644 --- a/binding/java/build.gradle +++ b/binding/java/build.gradle @@ -7,7 +7,7 @@ plugins { ext { PUBLISH_GROUP_ID = 'ai.picovoice' - PUBLISH_VERSION = '1.1.1' + PUBLISH_VERSION = '2.0.0' PUBLISH_ARTIFACT_ID = 'cheetah-java' } diff --git a/binding/java/gradle/wrapper/gradle-wrapper.properties b/binding/java/gradle/wrapper/gradle-wrapper.properties index d2880ba8..774fae87 100644 --- a/binding/java/gradle/wrapper/gradle-wrapper.properties +++ b/binding/java/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/binding/java/src/ai/picovoice/cheetah/Cheetah.java b/binding/java/src/ai/picovoice/cheetah/Cheetah.java index e41c5ec9..e6d70452 100644 --- a/binding/java/src/ai/picovoice/cheetah/Cheetah.java +++ b/binding/java/src/ai/picovoice/cheetah/Cheetah.java @@ -19,6 +19,8 @@ */ public class Cheetah { + private static String sdk = "java"; + public static final String LIBRARY_PATH; public static final String MODEL_PATH; @@ -27,6 +29,10 @@ public class Cheetah { MODEL_PATH = Utils.getPackagedModelPath(); } + public static void setSdk(String sdk) { + Cheetah.sdk = sdk; + } + private long handle; /** @@ -53,6 +59,8 @@ private Cheetah( } catch (Exception exception) { throw new CheetahException(exception); } + CheetahNative.setSdk(Cheetah.sdk); + handle = CheetahNative.init( accessKey, modelPath, diff --git a/binding/java/src/ai/picovoice/cheetah/CheetahNative.java b/binding/java/src/ai/picovoice/cheetah/CheetahNative.java index b7d142fa..98a5561e 100644 --- a/binding/java/src/ai/picovoice/cheetah/CheetahNative.java +++ b/binding/java/src/ai/picovoice/cheetah/CheetahNative.java @@ -20,6 +20,8 @@ class CheetahNative { static native String getVersion(); + static native void setSdk(String sdk); + static native long init( String accessKey, String modelPath, diff --git a/binding/java/src/ai/picovoice/cheetah/exception/CheetahActivationException.java b/binding/java/src/ai/picovoice/cheetah/exception/CheetahActivationException.java index a0bda089..71f11c09 100644 --- a/binding/java/src/ai/picovoice/cheetah/exception/CheetahActivationException.java +++ b/binding/java/src/ai/picovoice/cheetah/exception/CheetahActivationException.java @@ -1,5 +1,5 @@ /* - Copyright 2022 Picovoice Inc. + Copyright 2022-2023 Picovoice Inc. You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE" file accompanying this source. @@ -20,4 +20,8 @@ public CheetahActivationException(Throwable cause) { public CheetahActivationException(String message) { super(message); } + + public CheetahActivationException(String message, String[] messageStack) { + super(message, messageStack); + } } diff --git a/binding/java/src/ai/picovoice/cheetah/exception/CheetahActivationLimitException.java b/binding/java/src/ai/picovoice/cheetah/exception/CheetahActivationLimitException.java index dd04e71b..63a06c9a 100644 --- a/binding/java/src/ai/picovoice/cheetah/exception/CheetahActivationLimitException.java +++ b/binding/java/src/ai/picovoice/cheetah/exception/CheetahActivationLimitException.java @@ -1,5 +1,5 @@ /* - Copyright 2022 Picovoice Inc. + Copyright 2022-2023 Picovoice Inc. You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE" file accompanying this source. @@ -20,4 +20,8 @@ public CheetahActivationLimitException(Throwable cause) { public CheetahActivationLimitException(String message) { super(message); } + + public CheetahActivationLimitException(String message, String[] messageStack) { + super(message, messageStack); + } } diff --git a/binding/java/src/ai/picovoice/cheetah/exception/CheetahActivationRefusedException.java b/binding/java/src/ai/picovoice/cheetah/exception/CheetahActivationRefusedException.java index 6b19c5ee..9a2b7673 100644 --- a/binding/java/src/ai/picovoice/cheetah/exception/CheetahActivationRefusedException.java +++ b/binding/java/src/ai/picovoice/cheetah/exception/CheetahActivationRefusedException.java @@ -1,5 +1,5 @@ /* - Copyright 2022 Picovoice Inc. + Copyright 2022-2023 Picovoice Inc. You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE" file accompanying this source. @@ -20,4 +20,8 @@ public CheetahActivationRefusedException(Throwable cause) { public CheetahActivationRefusedException(String message) { super(message); } + + public CheetahActivationRefusedException(String message, String[] messageStack) { + super(message, messageStack); + } } diff --git a/binding/java/src/ai/picovoice/cheetah/exception/CheetahActivationThrottledException.java b/binding/java/src/ai/picovoice/cheetah/exception/CheetahActivationThrottledException.java index 8977a56c..9d597d2c 100644 --- a/binding/java/src/ai/picovoice/cheetah/exception/CheetahActivationThrottledException.java +++ b/binding/java/src/ai/picovoice/cheetah/exception/CheetahActivationThrottledException.java @@ -1,5 +1,5 @@ /* - Copyright 2022 Picovoice Inc. + Copyright 2022-2023 Picovoice Inc. You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE" file accompanying this source. @@ -20,4 +20,8 @@ public CheetahActivationThrottledException(Throwable cause) { public CheetahActivationThrottledException(String message) { super(message); } + + public CheetahActivationThrottledException(String message, String[] messageStack) { + super(message, messageStack); + } } diff --git a/binding/java/src/ai/picovoice/cheetah/exception/CheetahException.java b/binding/java/src/ai/picovoice/cheetah/exception/CheetahException.java index 7870a047..f527eba8 100644 --- a/binding/java/src/ai/picovoice/cheetah/exception/CheetahException.java +++ b/binding/java/src/ai/picovoice/cheetah/exception/CheetahException.java @@ -1,5 +1,5 @@ /* - Copyright 2022 Picovoice Inc. + Copyright 2022-2023 Picovoice Inc. You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE" file accompanying this source. @@ -13,11 +13,42 @@ package ai.picovoice.cheetah; public class CheetahException extends Exception { + private final String message; + private final String[] messageStack; + public CheetahException(Throwable cause) { super(cause); + this.message = cause.getMessage(); + this.messageStack = null; } public CheetahException(String message) { super(message); + this.message = message; + this.messageStack = null; + } + + public CheetahException(String message, String[] messageStack) { + super(message); + this.message = message; + this.messageStack = messageStack; + } + + public String[] getMessageStack() { + return this.messageStack; + } + + @Override + public String getMessage() { + StringBuilder sb = new StringBuilder(message); + if (messageStack != null) { + if (messageStack.length > 0) { + sb.append(":"); + for (int i = 0; i < messageStack.length; i++) { + sb.append(String.format("\n [%d] %s", i, messageStack[i])); + } + } + } + return sb.toString(); } } diff --git a/binding/java/src/ai/picovoice/cheetah/exception/CheetahIOException.java b/binding/java/src/ai/picovoice/cheetah/exception/CheetahIOException.java index f05f502b..853569ed 100644 --- a/binding/java/src/ai/picovoice/cheetah/exception/CheetahIOException.java +++ b/binding/java/src/ai/picovoice/cheetah/exception/CheetahIOException.java @@ -1,5 +1,5 @@ /* - Copyright 2022 Picovoice Inc. + Copyright 2022-2023 Picovoice Inc. You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE" file accompanying this source. @@ -20,4 +20,8 @@ public CheetahIOException(Throwable cause) { public CheetahIOException(String message) { super(message); } + + public CheetahIOException(String message, String[] messageStack) { + super(message, messageStack); + } } diff --git a/binding/java/src/ai/picovoice/cheetah/exception/CheetahInvalidArgumentException.java b/binding/java/src/ai/picovoice/cheetah/exception/CheetahInvalidArgumentException.java index b20f70f0..49bf69cd 100644 --- a/binding/java/src/ai/picovoice/cheetah/exception/CheetahInvalidArgumentException.java +++ b/binding/java/src/ai/picovoice/cheetah/exception/CheetahInvalidArgumentException.java @@ -1,5 +1,5 @@ /* - Copyright 2022 Picovoice Inc. + Copyright 2022-2023 Picovoice Inc. You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE" file accompanying this source. @@ -20,4 +20,8 @@ public CheetahInvalidArgumentException(Throwable cause) { public CheetahInvalidArgumentException(String message) { super(message); } + + public CheetahInvalidArgumentException(String message, String[] messageStack) { + super(message, messageStack); + } } diff --git a/binding/java/src/ai/picovoice/cheetah/exception/CheetahInvalidStateException.java b/binding/java/src/ai/picovoice/cheetah/exception/CheetahInvalidStateException.java index 387d1ee6..b27ac39f 100644 --- a/binding/java/src/ai/picovoice/cheetah/exception/CheetahInvalidStateException.java +++ b/binding/java/src/ai/picovoice/cheetah/exception/CheetahInvalidStateException.java @@ -1,5 +1,5 @@ /* - Copyright 2022 Picovoice Inc. + Copyright 2022-2023 Picovoice Inc. You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE" file accompanying this source. @@ -20,4 +20,8 @@ public CheetahInvalidStateException(Throwable cause) { public CheetahInvalidStateException(String message) { super(message); } + + public CheetahInvalidStateException(String message, String[] messageStack) { + super(message, messageStack); + } } diff --git a/binding/java/src/ai/picovoice/cheetah/exception/CheetahKeyException.java b/binding/java/src/ai/picovoice/cheetah/exception/CheetahKeyException.java index 760cbf33..d4bebf11 100644 --- a/binding/java/src/ai/picovoice/cheetah/exception/CheetahKeyException.java +++ b/binding/java/src/ai/picovoice/cheetah/exception/CheetahKeyException.java @@ -1,5 +1,5 @@ /* - Copyright 2022 Picovoice Inc. + Copyright 2022-2023 Picovoice Inc. You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE" file accompanying this source. @@ -20,4 +20,8 @@ public CheetahKeyException(Throwable cause) { public CheetahKeyException(String message) { super(message); } + + public CheetahKeyException(String message, String[] messageStack) { + super(message, messageStack); + } } diff --git a/binding/java/src/ai/picovoice/cheetah/exception/CheetahMemoryException.java b/binding/java/src/ai/picovoice/cheetah/exception/CheetahMemoryException.java index b4cad213..d6b55af9 100644 --- a/binding/java/src/ai/picovoice/cheetah/exception/CheetahMemoryException.java +++ b/binding/java/src/ai/picovoice/cheetah/exception/CheetahMemoryException.java @@ -1,5 +1,5 @@ /* - Copyright 2022 Picovoice Inc. + Copyright 2022-2023 Picovoice Inc. You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE" file accompanying this source. @@ -20,4 +20,8 @@ public CheetahMemoryException(Throwable cause) { public CheetahMemoryException(String message) { super(message); } + + public CheetahMemoryException(String message, String[] messageStack) { + super(message, messageStack); + } } diff --git a/binding/java/src/ai/picovoice/cheetah/exception/CheetahRuntimeException.java b/binding/java/src/ai/picovoice/cheetah/exception/CheetahRuntimeException.java index ae9fb75b..b13c9d0c 100644 --- a/binding/java/src/ai/picovoice/cheetah/exception/CheetahRuntimeException.java +++ b/binding/java/src/ai/picovoice/cheetah/exception/CheetahRuntimeException.java @@ -1,5 +1,5 @@ /* - Copyright 2022 Picovoice Inc. + Copyright 2022-2023 Picovoice Inc. You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE" file accompanying this source. @@ -20,4 +20,8 @@ public CheetahRuntimeException(Throwable cause) { public CheetahRuntimeException(String message) { super(message); } + + public CheetahRuntimeException(String message, String[] messageStack) { + super(message, messageStack); + } } diff --git a/binding/java/src/ai/picovoice/cheetah/exception/CheetahStopIterationException.java b/binding/java/src/ai/picovoice/cheetah/exception/CheetahStopIterationException.java index 859af0e0..9f3326ff 100644 --- a/binding/java/src/ai/picovoice/cheetah/exception/CheetahStopIterationException.java +++ b/binding/java/src/ai/picovoice/cheetah/exception/CheetahStopIterationException.java @@ -1,5 +1,5 @@ /* - Copyright 2022 Picovoice Inc. + Copyright 2022-2023 Picovoice Inc. You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE" file accompanying this source. @@ -20,4 +20,8 @@ public CheetahStopIterationException(Throwable cause) { public CheetahStopIterationException(String message) { super(message); } + + public CheetahStopIterationException(String message, String[] messageStack) { + super(message, messageStack); + } } diff --git a/binding/java/test/ai/picovoice/cheetah/CheetahPerformanceTest.java b/binding/java/test/ai/picovoice/cheetah/CheetahPerformanceTest.java index ad909758..9d54b661 100644 --- a/binding/java/test/ai/picovoice/cheetah/CheetahPerformanceTest.java +++ b/binding/java/test/ai/picovoice/cheetah/CheetahPerformanceTest.java @@ -35,7 +35,6 @@ public class CheetahPerformanceTest { @Test void initPerformance() throws Exception { - long[] perfResults = new long[numTestIterations]; for (int i = 0; i < numTestIterations + 1; i++) { long before = System.nanoTime(); diff --git a/binding/java/test/ai/picovoice/cheetah/CheetahTest.java b/binding/java/test/ai/picovoice/cheetah/CheetahTest.java index 9b06d0ef..513b3cdf 100644 --- a/binding/java/test/ai/picovoice/cheetah/CheetahTest.java +++ b/binding/java/test/ai/picovoice/cheetah/CheetahTest.java @@ -33,16 +33,57 @@ public class CheetahTest { private final String accessKey = System.getProperty("pvTestingAccessKey"); @Test - void getVersion() throws Exception { + void getVersion() throws CheetahException { Cheetah cheetah = new Cheetah.Builder() .setAccessKey(accessKey) .build(); - assertTrue(cheetah.getVersion() != null && !cheetah.getVersion().equals("")); + cheetah.delete(); + } + @Test + void getFrameLength() throws CheetahException { + Cheetah cheetah = new Cheetah.Builder() + .setAccessKey(accessKey) + .build(); + assertTrue(cheetah.getFrameLength() > 0); + cheetah.delete(); + } + + @Test + void getSampleRate() throws CheetahException { + Cheetah cheetah = new Cheetah.Builder() + .setAccessKey(accessKey) + .build(); + assertTrue(cheetah.getSampleRate() > 0); cheetah.delete(); } + @Test + void getErrorStack() { + String[] error = {}; + try { + new Cheetah.Builder() + .setAccessKey("invalid") + .build(); + } catch (CheetahException e) { + error = e.getMessageStack(); + } + + assertTrue(0 < error.length); + assertTrue(error.length <= 8); + + try { + new Cheetah.Builder() + .setAccessKey("invalid") + .build(); + } catch (CheetahException e) { + for (int i = 0; i < error.length; i++) { + assertEquals(e.getMessageStack()[i], error[i]); + } + } + } + @ParameterizedTest(name = "test transcribe with automatic punctuation set to ''{0}''") @MethodSource("transcribeProvider") void transcribe(boolean enableAutomaticPunctuation, String referenceTranscript) throws Exception { diff --git a/demo/java/build.gradle b/demo/java/build.gradle index 3e19bfff..01ec8e72 100644 --- a/demo/java/build.gradle +++ b/demo/java/build.gradle @@ -4,6 +4,9 @@ plugins { repositories { mavenCentral() + maven { + url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1300' + } } sourceSets { @@ -15,14 +18,14 @@ sourceSets { } dependencies { - implementation 'ai.picovoice:cheetah-java:1.1.1' + implementation 'ai.picovoice:cheetah-java:2.0.0' implementation 'commons-cli:commons-cli:1.4' } jar { manifest { attributes "Main-Class": "ai.picovoice.cheetahdemo.MicDemo", - "Class-Path": "cheetah-1.1.1.jar;commons-cli-1.4.jar" + "Class-Path": "cheetah-2.0.0.jar;commons-cli-1.4.jar" } from sourceSets.main.output exclude "**/FileDemo.class" @@ -33,7 +36,7 @@ jar { task fileDemoJar(type: Jar) { manifest { attributes "Main-Class": "ai.picovoice.cheetahdemo.FileDemo", - "Class-Path": "cheetah-1.1.1.jar;commons-cli-1.4.jar" + "Class-Path": "cheetah-2.0.0.jar;commons-cli-1.4.jar" } from sourceSets.main.output exclude "**/MicDemo.class" diff --git a/demo/java/gradle/wrapper/gradle-wrapper.properties b/demo/java/gradle/wrapper/gradle-wrapper.properties index d2880ba8..774fae87 100644 --- a/demo/java/gradle/wrapper/gradle-wrapper.properties +++ b/demo/java/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists