Skip to content

Commit

Permalink
use v3-beta.7 and add usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
GiviMAD committed Sep 2, 2023
1 parent 212c7e9 commit 65d3b02
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 104 deletions.
65 changes: 3 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
<img src="./logo.png?raw=true" width="400px"</img>
</div>

## Description
## Overview

Java wrapper for [Rustpotter](https://github.com/GiviMAD/rustpotter) (an open source wakeword spotter forged in rust).

For an usage example, please refer to the "getDetectionsInFile" test implementation.

## Installation

This package is available on the [Maven Central Repository](https://search.maven.org/artifact/io.github.givimad/rustpotter-java).
Expand All @@ -17,64 +19,3 @@ The distributed package support the following platforms:
* debian (arm, arm64, x86_64)
* macOS (arm64, x86_64)
* windows (x86_64)

## Examples

### Initialize the Detector:

```java
var rustpotterBuilder = new RustpotterBuilder();
rustpotterBuilder.setThreshold(config.threshold);
rustpotterBuilder.setAveragedThreshold(config.averagedThreshold);
rustpotterBuilder.setComparatorRef(config.comparatorRef);
rustpotterBuilder.setComparatorBandSize(config.comparatorBandSize);
rustpotterBuilder.setBitsPerSample(bitDepth);
rustpotterBuilder.setSampleRate(frequency);
rustpotterBuilder.setChannels(channels);
rustpotterBuilder.setEndianness(Endianness.Little);
var rustpotter = rustpotterBuilder.build();
rustpotterBuilder.delete();
// Add wakeword model
rustpotter.addWakewordModelFile(modelPath.toString());
```

### Detect Wakewords:

```java
private void processAudioStream(Rustpotter rustpotter, Consumer<String> listener, InputStream audioStream, AtomicBoolean aborted) {
int numBytesRead;
var frameSize = (int) rustpotter.getSamplesPerFrame();
var bufferSize = frameSize * 2;
ByteBuffer captureBuffer = ByteBuffer.allocate(bufferSize);
captureBuffer.order(ByteOrder.LITTLE_ENDIAN);
short[] audioBuffer = new short[frameSize];
while (!aborted.get()) {
try {
// read a buffer of audio
numBytesRead = audioStream.read(captureBuffer.array(), 0, captureBuffer.capacity());
if (aborted.get()) {
break;
}
if (numBytesRead != bufferSize) {
// do not pass incomplete buffers
Thread.sleep(100);
continue;
}
captureBuffer.asShortBuffer().get(audioBuffer);
var result = rustpotter.processSort(audioBuffer);
if (result.isPresent()) {
var detection = result.get();
var word = detection.getName();
logger.debug("keyword '{}' detected with score {}!", detection.getName(), detection.getScore());
detection.delete();
listener.accept(word);
}
} catch (IOException | InterruptedException e) {
String errorMessage = e.getMessage();
throw e; // another consumer for the errors makes more sense as this function is intended to run on a separate thread.
}
}
rustpotter.delete();
logger.debug("rustpotter stopped");
}
```
58 changes: 32 additions & 26 deletions binding/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions binding/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustpotter-java"
version = "3.0.0-beta.5"
version = "3.0.0-beta.7"
edition = "2021"
description = "Java binding for Rustpotter, an open source wakeword spotter forged in rust."

Expand All @@ -10,7 +10,7 @@ crate-type = ["cdylib"]

[dependencies]
log = "^0.4.19"
rustpotter = { version = "3.0.0-beta.5", features = [ "debug", "record" ] }
rustpotter = { version = "3.0.0-beta.7", features = [ "debug", "record" ] }

[build-dependencies]
flapigen = "0.6.0"
Expand Down
5 changes: 5 additions & 0 deletions binding/src/java_glue.rs.in
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ impl Rustpotter {
pub fn getBytesPerFrame(&self) -> usize {
self.rustpotter.get_bytes_per_frame()
}
pub fn reset(&mut self) {
self.rustpotter.reset()
}
}

foreign_typemap!(
Expand Down Expand Up @@ -384,6 +387,8 @@ foreign_class!(
fn Rustpotter::getSamplesPerFrame(&self) -> usize;
/// Returns size in bytes for the desired chunk.
fn Rustpotter::getBytesPerFrame(&self) -> usize;
/// Reset internal state.
fn Rustpotter::reset(&mut self);
/// Removes a wakeword by name.
fn Rustpotter::removeWakeword(&mut self, name: &str);
foreign_code r#"
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.givimad</groupId>
<artifactId>rustpotter-java</artifactId>
<version>3.0.0-beta.5</version>
<version>3.0.0-beta.7</version>

<name>rustpotter-java</name>
<url>https://github.com/GiviMAD/rustpotter-java</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ public final long getBytesPerFrame() {
return ret;
}
private static native long do_getBytesPerFrame(long self);
/**
* Reset internal state.
*/
public final void reset() {
do_reset(mNativeObj);
}
private static native void do_reset(long self);
/**
* Removes a wakeword by name.
*/
Expand Down

This file was deleted.

Loading

0 comments on commit 65d3b02

Please sign in to comment.