Skip to content

Commit

Permalink
Make denoiser thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
henkelmax committed Aug 10, 2023
1 parent 922f321 commit 1e21a8b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 46 deletions.
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>de.maxhenkel.rnnoise4j</groupId>
<artifactId>rnnoise4j</artifactId>
<version>2.0.1</version>
<version>2.0.2</version>

<name>RNNoise wrapper for Java</name>
<url>https://maxhenkel.de</url>
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A Java wrapper for [RNNoise](https://jmvalin.ca/demo/rnnoise/) written in Rust u
<dependency>
<groupId>de.maxhenkel.rnnoise4j</groupId>
<artifactId>rnnoise4j</artifactId>
<version>2.0.0</version>
<version>2.0.2</version>
</dependency>

<repositories>
Expand All @@ -25,7 +25,7 @@ A Java wrapper for [RNNoise](https://jmvalin.ca/demo/rnnoise/) written in Rust u

``` groovy
dependencies {
implementation 'de.maxhenkel.rnnoise4j:rnnoise4j:2.0.0'
implementation 'de.maxhenkel.rnnoise4j:rnnoise4j:2.0.2'
}
repositories {
Expand Down
61 changes: 29 additions & 32 deletions rust/Cargo.lock

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

2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rnnoise4j"
version = "2.0.0"
version = "2.0.2"
edition = "2021"

[dependencies]
Expand Down
6 changes: 3 additions & 3 deletions rust/src/rnnoise/rnnoise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use crate::rnnoise::exceptions::{throw_illegal_argument_exception, throw_illegal
const FLOAT_SHORT_SCALE: f32 = (i16::MAX - 1i16) as f32;

#[no_mangle]
pub extern "C" fn Java_de_maxhenkel_rnnoise4j_Denoiser_createDenoiser(_env: JNIEnv, _class: JClass) -> jlong {
pub extern "C" fn Java_de_maxhenkel_rnnoise4j_Denoiser_createDenoiser0(_env: JNIEnv, _class: JClass) -> jlong {
let denoiser = DenoiseState::new();
return create_pointer(denoiser);
}

#[no_mangle]
pub extern "C" fn Java_de_maxhenkel_rnnoise4j_Denoiser_denoise<'a>(mut env: JNIEnv<'a>, obj: JObject<'a>, input: JShortArray<'a>) -> JShortArray<'a> {
pub extern "C" fn Java_de_maxhenkel_rnnoise4j_Denoiser_denoise0<'a>(mut env: JNIEnv<'a>, obj: JObject<'a>, input: JShortArray<'a>) -> JShortArray<'a> {
let denoiser = match get_denoiser(&mut env, &obj) {
Some(denoiser) => denoiser,
None => {
Expand Down Expand Up @@ -93,7 +93,7 @@ pub extern "C" fn Java_de_maxhenkel_rnnoise4j_Denoiser_denoise<'a>(mut env: JNIE
}

#[no_mangle]
pub extern "C" fn Java_de_maxhenkel_rnnoise4j_Denoiser_destroyDenoiser(mut env: JNIEnv, obj: JObject) {
pub extern "C" fn Java_de_maxhenkel_rnnoise4j_Denoiser_destroyDenoiser0(mut env: JNIEnv, obj: JObject) {
let pointer = get_pointer(&mut env, &obj);

if pointer == 0 {
Expand Down
24 changes: 17 additions & 7 deletions src/main/java/de/maxhenkel/rnnoise4j/Denoiser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,33 @@ public class Denoiser implements AutoCloseable {

public Denoiser() throws IOException, UnknownPlatformException {
RNNoise.load();
denoiser = createDenoiser();
denoiser = createDenoiser0();
}

private static native long createDenoiser();
private static native long createDenoiser0();

public native short[] denoise(short[] input);
private native short[] denoise0(short[] input);

private native long destroyDenoiser();
public short[] denoise(short[] input) {
synchronized (this) {
return denoise0(input);
}
}

private native long destroyDenoiser0();

@Override
public void close() {
destroyDenoiser();
denoiser = 0L;
synchronized (this) {
destroyDenoiser0();
denoiser = 0L;
}
}

public boolean isClosed() {
return denoiser == 0L;
synchronized (this) {
return denoiser == 0L;
}
}

}

0 comments on commit 1e21a8b

Please sign in to comment.