Skip to content

Commit

Permalink
refactor: rename buffer_size to frame_size in decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Apehum committed Oct 18, 2023
1 parent ce8b3e2 commit 991b38e
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Sample code from [OpusTest.java](https://github.com/plasmoapp/opus-jni-rust/blob
short[] rawSamples = new short[960];

// Creates a new encoder in mono with 1024 mtu size and application mode VOIP
OpusEncoder encoder = OpusEncoder.create(48_000, false, 960, OpusMode.VOIP);
OpusEncoder encoder = OpusEncoder.create(48_000, false, 1024, OpusMode.VOIP);

// Sets encoder bitrate to 50k
encoder.setBitrate(50_000);
Expand All @@ -41,7 +41,7 @@ encoder.close();
* Decoding
*/

// Creates a new decoder in mono with 960 buffer size
// Creates a new decoder in mono with 960 frame size
OpusDecoder decoder = OpusDecoder.create(48_000, false, 960);

// Decodes the encoded audio data into an audio samples
Expand Down
10 changes: 5 additions & 5 deletions rust/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ pub extern "system" fn Java_com_plasmoverse_opus_OpusDecoder_createNative(
_class: JClass,
sample_rate: jint,
stereo: jboolean,
buffer_size: jint
frame_size: jint
) -> jlong {
match create_decoder(sample_rate, stereo, buffer_size) {
match create_decoder(sample_rate, stereo, frame_size) {
Ok(pointer) => pointer,
Err(exception) => {
env.throw_new_exception(exception);
Expand Down Expand Up @@ -69,7 +69,7 @@ pub unsafe extern "system" fn Java_com_plasmoverse_opus_OpusDecoder_decodeNative
fn create_decoder(
sample_rate: jint,
stereo: jboolean,
buffer_size: jint
frame_size: jint
) -> Result<jlong, JavaException> {
let channels = match stereo {
1u8 => Channels::Stereo,
Expand All @@ -82,7 +82,7 @@ fn create_decoder(
let decoder_container = DecoderContainer {
decoder,
channels,
buffer_size,
frame_size,
};

Ok(decoder_container.into_jlong_pointer())
Expand Down Expand Up @@ -137,7 +137,7 @@ unsafe fn decoder_decode<'local>(
.err_into_opus_exception("Failed to convert byte array to rust vec".into())?
};

let mut decoded = vec![0i16; container.buffer_size as usize * container.channels as usize];
let mut decoded = vec![0i16; container.frame_size as usize * container.channels as usize];

let result = container.decoder.decode(&encoded, &mut decoded, false)
.err_into_opus_exception("Failed to decode audio".into())?;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/decoder_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::util::pointer::JavaPointers;
pub struct DecoderContainer {
pub decoder: Decoder,
pub channels: Channels,
pub buffer_size: i32
pub frame_size: i32
}

impl JavaPointers<DecoderContainer> for DecoderContainer {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/plasmoverse/opus/OpusDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ public final class OpusDecoder {
*
* @param sampleRate The sample rate of the audio data.
* @param stereo {@code true} if the audio is in stereo format, {@code false} for mono.
* @param bufferSize Size of the buffer for processing audio.
* @param frameSize Size of the frame.
* @throws IOException If an error occurs while extracting the native library.
* @throws UnsatisfiedLinkError If the native libraries fail to load.
* @throws OpusException If the opus decoder fail to initialize.
* @return An instance of the opus decoder.
*/
public static OpusDecoder create(int sampleRate, boolean stereo, int bufferSize) throws IOException, OpusException {
public static OpusDecoder create(int sampleRate, boolean stereo, int frameSize) throws IOException, OpusException {
OpusLibrary.load();

long pointer = createNative(sampleRate, stereo, bufferSize);
long pointer = createNative(sampleRate, stereo, frameSize);

return new OpusDecoder(pointer);
}

private static native long createNative(int sampleRate, boolean stereo, int mtuSize);
private static native long createNative(int sampleRate, boolean stereo, int frameSize);


private final long pointer;
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/com/plasmoverse/opus/OpusTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public void decode() throws Exception {
* Decoding
*/

// Creates a new decoder in mono with 960 buffer size
// buffer_size should be frame_size * channels
// Creates a new decoder in mono with 960 frame size
OpusDecoder decoder = OpusDecoder.create(48_000, false, 960);

// Decodes the encoded audio data into an audio samples
Expand Down

0 comments on commit 991b38e

Please sign in to comment.