Skip to content

Commit

Permalink
new jni
Browse files Browse the repository at this point in the history
  • Loading branch information
albho committed Mar 22, 2024
1 parent 4104008 commit b2e43f4
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/android-perf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
run: ./gradlew assembleDebug

- name: Build androidTest
run: ./gradlew assembleDebugAndroidTest
run: ./gradlew assembleDebugAndroidTest -DtestBuildType=perf

- name: Run tests on AppCenter
run: appcenter test run espresso
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ dependencies {
}
```

Create an instance of the engine and generate speech:
Create an instance of the engine and perform speaker diarization on an audio file:

```java
import ai.picovoice.falcon.*;
Expand All @@ -247,7 +247,7 @@ try {
.build(appContext);

File audioFile = new File("${AUDIO_FILE_PATH}");
FalconSegments segments = falcon.processFile(audioFile.getAbsolutePath());
FalconSegment[] segments = falcon.processFile(audioFile.getAbsolutePath());

} catch (FalconException ex) { }
```
Expand Down
2 changes: 1 addition & 1 deletion binding/android/Falcon/falcon/consumer-rules.pro
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
-keep class ai.picovoice.falcon.*Exception { <init>(...); }
-keep class ai.picovoice.falcon.FalconSegments { <init>(...); }
-keep class ai.picovoice.falcon.FalconSegment { <init>(...); }
2 changes: 1 addition & 1 deletion binding/android/Falcon/falcon/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
-keep class ai.picovoice.falcon.*Exception { <init>(...); }
-keep class ai.picovoice.falcon.FalconSegments { <init>(...); }
-keep class ai.picovoice.falcon.FalconSegment { <init>(...); }
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ public void delete() {
* equal to {@link #getSampleRate()} and be 16-bit linearly-encoded. Furthermore,
* Falcon operates on single channel audio. If you wish to process data in a different
* sample rate or format, consider using {@link #processFile(String)}.
* @return FalconSegments object which contains the diarization results of the engine.
* @return FalconSegment[] object which contains the diarization results of the engine.
* @throws FalconException if there is an error while processing the audio frame.
*/
public FalconSegments process(short[] pcm) throws FalconException {
public FalconSegment[] process(short[] pcm) throws FalconException {
if (handle == 0) {
throw new FalconInvalidStateException("Attempted to call Falcon process after delete.");
}
Expand All @@ -109,10 +109,10 @@ public FalconSegments process(short[] pcm) throws FalconException {
*
* @param path Absolute path to the audio file. The supported formats are:
* `3gp (AMR)`, `FLAC`, `MP3`, `MP4/m4a (AAC)`, `Ogg`, `WAV` and `WebM`.
* @return FalconSegments object which contains the diarization results of the engine.
* @return FalconSegment[] object which contains the diarization results of the engine.
* @throws FalconException if there is an error while processing the audio frame.
*/
public FalconSegments processFile(String path) throws FalconException {
public FalconSegment[] processFile(String path) throws FalconException {
if (handle == 0) {
throw new FalconInvalidStateException("Attempted to call Falcon processFile after delete.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ static native long init(

static native void delete(long object);

static native FalconSegments process(
static native FalconSegment[] process(
long object,
short[] pcm,
int numSamples) throws FalconException;

static native FalconSegments processFile(
static native FalconSegment[] processFile(
long object,
String path) throws FalconException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2024 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.
Unless required by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language governing permissions and
limitations under the License.
*/

package ai.picovoice.falcon;

/**
* FalconSegment class.
*/
public class FalconSegment {
private final float startSec;
private final float endSec;
private final int speakerTag;

/**
* Constructor.
*
* @param startSec Start of segment in seconds.
* @param endSec End of segment in seconds.
* @param speakerTag A non-negative integer that identifies unique speakers.
*/
public FalconSegment(
float startSec,
float endSec,
int speakerTag
) {
this.startSec = startSec;
this.endSec = endSec;
this.speakerTag = speakerTag;
}

/**
* Getter for the start of segment in seconds.
*
* @return Start of segment in seconds.
*/
public float getStartSec() {
return startSec;
}

/**
* Getter for the end of segment in seconds.
*
* @return End of segment in seconds.
*/
public float getEndSec() {
return endSec;
}

/**
* Getter for the speaker tag.
*
* @return Speaker tag.
*/
public int getSpeakerTag() {
return speakerTag;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import java.nio.ByteOrder;
import java.util.Arrays;

import ai.picovoice.falcon.FalconSegments;
import ai.picovoice.falcon.FalconSegment;

public class BaseTest {

Expand Down Expand Up @@ -105,8 +105,8 @@ protected static short[] readAudioFile(String audioFile) throws Exception {
}

protected void validateMetadata(
FalconSegments.Segment[] segments,
FalconSegments.Segment[] expectedSegments
FalconSegment[] segments,
FalconSegment[] expectedSegments
) {
assertEquals(segments.length, expectedSegments.length);
for (int i = 0; i < segments.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import ai.picovoice.falcon.Falcon;
import ai.picovoice.falcon.FalconException;
import ai.picovoice.falcon.FalconSegments;
import ai.picovoice.falcon.FalconSegment;


@RunWith(Enclosed.class)
Expand Down Expand Up @@ -136,7 +136,7 @@ public static class DiarizationTests extends BaseTest {
public String testAudioFile;

@Parameterized.Parameter(value = 1)
public FalconSegments.Segment[] expectedSegments;
public FalconSegment[] expectedSegments;

@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> initParameters() throws IOException {
Expand All @@ -157,15 +157,15 @@ public static Collection<Object[]> initParameters() throws IOException {

String testAudioFile = String.format("audio_samples/%s", audioFile);

FalconSegments.Segment[] paramSegments = new FalconSegments.Segment[segments.size()];
FalconSegment[] paramSegments = new FalconSegment[segments.size()];
for (int j = 0; j < segments.size(); j++) {
JsonObject segmentObject = segments.get(j).getAsJsonObject();

float startSec = segmentObject.get("start_sec").getAsFloat();
float endSec = segmentObject.get("end_sec").getAsFloat();
int speakerTag = segmentObject.get("speaker_tag").getAsInt();

paramSegments[j] = new FalconSegments.Segment(
paramSegments[j] = new FalconSegment(
startSec,
endSec,
speakerTag
Expand All @@ -190,11 +190,11 @@ public void testDiarization() throws Exception {
File audioFile = new File(testResourcesPath, testAudioFile);
short[] pcm = readAudioFile(audioFile.getAbsolutePath());

FalconSegments result = falcon.process(pcm);
FalconSegment[] result = falcon.process(pcm);

assertEquals(result.getSegmentArray().length, expectedSegments.length);
for (int i = 0; i < result.getSegmentArray().length; i++) {
validateMetadata(result.getSegmentArray(), expectedSegments);
assertEquals(result.length, expectedSegments.length);
for (int i = 0; i < result.length; i++) {
validateMetadata(result, expectedSegments);
}
falcon.delete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

import ai.picovoice.falcon.Falcon;
import ai.picovoice.falcon.FalconException;
import ai.picovoice.falcon.FalconSegments;
import ai.picovoice.falcon.FalconSegment;

public class MainActivity extends AppCompatActivity {

Expand Down Expand Up @@ -93,7 +93,7 @@ public void runTest() {
result = new TestResult();
result.testName = "Test Process";
try {
FalconSegments processResult = processTestAudio(falcon, "audio_samples/test.wav");
FalconSegment[] processResult = processTestAudio(falcon, "audio_samples/test.wav");
if (processResult != null) {
result.success = true;
} else {
Expand Down Expand Up @@ -176,7 +176,7 @@ private String getModelFile() {
return "models/falcon_params.pv";
}

private FalconSegments processTestAudio(@NonNull Falcon l, String audioPath) throws Exception {
private FalconSegment[] processTestAudio(@NonNull Falcon l, String audioPath) throws Exception {
File testAudio = new File(getApplicationContext().getFilesDir(), audioPath);

if (!testAudio.exists()) {
Expand Down
4 changes: 2 additions & 2 deletions binding/android/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Perform diarization on an audio file by providing the absolute path to the file:

```java
File audioFile = new File("${AUDIO_FILE_PATH}");
FalconSegments segments = falcon.processFile(audioFile.getAbsolutePath());
FalconSegment[] segments = falcon.processFile(audioFile.getAbsolutePath());
```

Perform diarization on raw audio data (sample rate of 16 kHz, 16-bit linearly encoded and 1 channel):
Expand All @@ -71,7 +71,7 @@ Perform diarization on raw audio data (sample rate of 16 kHz, 16-bit linearly en
short[] getAudioData() {
// ...
}
FalconSegments segments = falcon.process(getAudioData());
FalconSegment[] segments = falcon.process(getAudioData());
```

The return value `segments` represents an array of segments, each with the following metadata items:
Expand Down
2 changes: 0 additions & 2 deletions demo/android/FalconDemo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,3 @@ Ensure you have an Android device connected or simulator running. Then run the f
cd demo/android/FalconDemo
./gradlew connectedAndroidTest -PpvTestingAccessKey="YOUR_ACCESS_KEY_HERE"
```

The test results are stored in `falcon-demo-app/build/reports`.
4 changes: 2 additions & 2 deletions demo/android/FalconDemo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
google()
mavenCentral()
maven {
url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1322/'
url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1323/'
}
}
dependencies {
Expand All @@ -20,7 +20,7 @@ allprojects {
google()
mavenCentral()
maven {
url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1322/'
url 'https://s01.oss.sonatype.org/content/repositories/aipicovoice-1323/'
}
}
}
Expand Down
Loading

0 comments on commit b2e43f4

Please sign in to comment.