Skip to content

Commit

Permalink
v3.0 java (#679)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksyeo1010 authored Oct 24, 2023
1 parent 8ffcc4e commit c837399
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 20 deletions.
13 changes: 11 additions & 2 deletions demo/java-swing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ plugins {

repositories {
mavenCentral()
maven {
url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1265/'
}
maven {
url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1268/'
}
maven {
url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1270/'
}
}

sourceSets {
Expand All @@ -18,13 +27,13 @@ sourceSets {
jar {
manifest {
attributes 'Main-Class': 'Main',
"Class-Path": "picovoice-2.2.1.jar;commons-cli-1.4.jar"
"Class-Path": "picovoice-3.0.0.jar;commons-cli-1.4.jar"
}
from {configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}


dependencies {
implementation 'ai.picovoice:picovoice-java:2.2.1'
implementation 'ai.picovoice:picovoice-java:3.0.0'
implementation 'commons-cli:commons-cli:1.4'
}
15 changes: 12 additions & 3 deletions demo/java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ plugins {

repositories {
mavenCentral()
maven {
url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1265/'
}
maven {
url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1268/'
}
maven {
url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1270/'
}
}

sourceSets {
Expand All @@ -15,14 +24,14 @@ sourceSets {
}

dependencies {
implementation 'ai.picovoice:picovoice-java:2.2.1'
implementation 'ai.picovoice:picovoice-java:3.0.0'
implementation 'commons-cli:commons-cli:1.4'
}

jar {
manifest {
attributes "Main-Class": "ai.picovoice.picovoicedemo.MicDemo",
"Class-Path" : "picovoice-2.2.1.jar;commons-cli-1.4.jar"
"Class-Path" : "picovoice-3.0.0.jar;commons-cli-1.4.jar"
}
from sourceSets.main.output
exclude "**/FileDemo.class"
Expand All @@ -34,7 +43,7 @@ jar {
task fileDemoJar(type: Jar) {
manifest {
attributes "Main-Class": "ai.picovoice.picovoicedemo.FileDemo",
"Class-Path" : "picovoice-2.2.1.jar;commons-cli-1.4.jar"
"Class-Path" : "picovoice-3.0.0.jar;commons-cli-1.4.jar"
}
from sourceSets.main.output
exclude "**/MicDemo.class"
Expand Down
12 changes: 9 additions & 3 deletions sdk/java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {

ext {
PUBLISH_GROUP_ID = 'ai.picovoice'
PUBLISH_VERSION = '2.2.1'
PUBLISH_VERSION = '3.0.0'
PUBLISH_ARTIFACT_ID = 'picovoice-java'
}

Expand All @@ -18,6 +18,12 @@ java {

repositories {
mavenCentral()
maven {
url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1265/'
}
maven {
url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1268/'
}
}

sourceSets {
Expand All @@ -40,8 +46,8 @@ if (file("${rootDir}/publish-mavencentral.gradle").exists()) {
}

dependencies {
implementation 'ai.picovoice:porcupine-java:2.2.1'
implementation 'ai.picovoice:rhino-java:2.2.1'
implementation 'ai.picovoice:porcupine-java:3.0.0'
implementation 'ai.picovoice:rhino-java:3.0.0'
testImplementation 'com.google.code.gson:gson:2.10.1'
testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.2'
Expand Down
25 changes: 20 additions & 5 deletions sdk/java/src/ai/picovoice/picovoice/Picovoice.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ public Picovoice(
.setKeywordPath(keywordPath)
.build();

if (!porcupine.getVersion().startsWith("2.2.")) {
if (!porcupine.getVersion().startsWith("3.0.")) {
final String message = String.format(
"Expected Porcupine library with version '2.2.x' but received %s",
"Expected Porcupine library with version '3.0.x' but received %s",
porcupine.getVersion());
throw new PicovoiceException(message);
}
Expand All @@ -123,9 +123,9 @@ public Picovoice(
.setRequireEndpoint(requireEndpoint)
.build();

if (!rhino.getVersion().startsWith("2.2.")) {
if (!rhino.getVersion().startsWith("3.0.")) {
final String message = String.format(
"Expected Rhino library with version '2.2.x' but received %s",
"Expected Rhino library with version '3.0.x' but received %s",
rhino.getVersion());
throw new PicovoiceException(message);
}
Expand Down Expand Up @@ -208,13 +208,28 @@ public void process(short[] pcm) throws PicovoiceException {
}
}

/**
* Resets the internal state of Picovoice. It should be called before processing a new stream of audio
* or when Picovoice was stopped while processing a stream of audio.
*
* @throws PicovoiceException if reset fails.
*/
public void reset() throws PicovoiceException {
try {
this.isWakeWordDetected = false;
this.rhino.reset();
} catch (RhinoException e) {
throw mapToPicovoiceException(e);
}
}

/**
* Getter for version.
*
* @return Version.
*/
public String getVersion() {
return "2.2.0";
return "3.0.0";
}

/**
Expand Down
44 changes: 37 additions & 7 deletions sdk/java/test/ai/picovoice/picovoice/PicovoiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import javax.sound.sampled.UnsupportedAudioFileException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class PicovoiceTest {
Expand Down Expand Up @@ -155,14 +156,33 @@ void getSampleRate() throws PicovoiceException {
assertTrue(picovoice.getSampleRate() > 0);
}

void runTestCase(
String audioFileName,
String expectedIntent,
Map<String, String> expectedSlots)
throws PicovoiceException, IOException, UnsupportedAudioFileException {
isWakeWordDetected = false;
@Test
void testReset() throws PicovoiceException, IOException, UnsupportedAudioFileException {
PicovoiceWakeWordCallback callback = new PicovoiceWakeWordCallback() {
@Override
public void invoke() {
try {
picovoice.reset();
} catch (PicovoiceException e) {
assertNull(e);
}
}
};

picovoice = new Picovoice.Builder()
.setAccessKey(accessKey)
.setKeywordPath(getTestKeywordPath("en", "picovoice"))
.setWakeWordCallback(callback)
.setContextPath(getTestContextPath("en", "coffee_maker"))
.setInferenceCallback(inferenceCallback)
.build();

inferenceResult = null;
processFileHelper("picovoice-coffee.wav");
assertTrue(inferenceResult == null);
}

void processFileHelper(String audioFileName) throws PicovoiceException, IOException, UnsupportedAudioFileException {
int frameLen = picovoice.getFrameLength();
File testAudioPath = new File(getTestAudioFilePath(audioFileName));

Expand All @@ -174,12 +194,22 @@ void runTestCase(
short[] picovoiceFrame = new short[frameLen];
int numBytesRead;
while ((numBytesRead = audioInputStream.read(pcm)) != -1) {

if (numBytesRead / byteDepth == frameLen) {
ByteBuffer.wrap(pcm).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(picovoiceFrame);
picovoice.process(picovoiceFrame);
}
}
}

void runTestCase(
String audioFileName,
String expectedIntent,
Map<String, String> expectedSlots)
throws PicovoiceException, IOException, UnsupportedAudioFileException {
isWakeWordDetected = false;
inferenceResult = null;

processFileHelper(audioFileName);

assertTrue(isWakeWordDetected);
assertEquals(inferenceResult.getIntent(), expectedIntent);
Expand Down

0 comments on commit c837399

Please sign in to comment.