Skip to content

Commit

Permalink
Add opening SOFA from memory
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiltd committed Oct 15, 2024
1 parent 48e7839 commit b9f71fd
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
2 changes: 1 addition & 1 deletion libmysofa-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ cc = "1.0.101"
system-deps = "7.0"

[package.metadata.system-deps]
libmysofa = "1"
libmysofa = "1.3"

[package.metadata.cargo-machete]
ignored = ["libz-sys"]
29 changes: 28 additions & 1 deletion libmysofa-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

use std::os::raw::{c_char, c_int, c_short, c_uint, c_ulong, c_void};
use std::os::raw::{c_char, c_int, c_long, c_short, c_uint, c_ulong, c_void};

pub const MYSOFA_DEFAULT_NEIGH_STEP_ANGLE: f64 = 0.5;
pub const MYSOFA_DEFAULT_NEIGH_STEP_RADIUS: f64 = 0.01;
Expand Down Expand Up @@ -173,6 +173,33 @@ extern "C" {
neighbor_radius_step: f32,
) -> *mut MYSOFA_EASY;

pub fn mysofa_open_data(
data: *const c_char,
size: c_long,
samplerate: f32,
filterlength: *mut c_int,
err: *mut c_int,
) -> *mut MYSOFA_EASY;

pub fn mysofa_open_data_no_norm(
data: *const c_char,
size: c_long,
samplerate: f32,
filterlength: *mut c_int,
err: *mut c_int,
) -> *mut MYSOFA_EASY;

pub fn mysofa_open_data_advanced(
data: *const c_char,
size: c_long,
samplerate: f32,
filterlength: *mut c_int,
err: *mut c_int,
norm: bool,
neighbor_angle_step: f32,
neighbor_radius_step: f32,
) -> *mut MYSOFA_EASY;

pub fn mysofa_open_cached(
filename: *const c_char,
samplerate: f32,
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,17 @@ mod tests {
let mut filter = Filter::new(filt_len);
sofa.filter(0.0, 1.0, 0.0, &mut filter);
}

#[test]
fn open_data_test() {
let cwd = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
std::env::set_current_dir(cwd).unwrap();

let data = std::fs::read("libmysofa-sys/libmysofa/tests/tester.sofa").unwrap();
let sofa = Sofar::open_data(&data).unwrap();
let filt_len = sofa.filter_len();

let mut filter = Filter::new(filt_len);
sofa.filter(0.0, 1.0, 0.0, &mut filter);
}
}
52 changes: 52 additions & 0 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,46 @@ impl OpenOptions {
cached: self.cached,
})
}

/// Open a SOFA file using provided bytes and open options specified in `self`
///
/// ```no_run
/// use sofar::reader::OpenOptions;
/// let data: Vec<u8> = std::fs::read("my/sofa/file.sofa").unwrap();
///
/// let sofa = OpenOptions::new()
/// .normalized(false)
/// .sample_rate(44100.0)
/// .open_data(&data)
/// .unwrap();
/// ```
pub fn open_data<B: AsRef<[u8]>>(&self, bytes: B) -> Result<Sofar, Error> {
let mut filter_len = 0;
let mut err = 0;

let raw = unsafe {
ffi::mysofa_open_data_advanced(
bytes.as_ref().as_ptr() as _,
bytes.as_ref().len() as _,
self.sample_rate,
&mut filter_len,
&mut err,
self.normalized,
self.neighbor_angle_step,
self.neighbor_radius_step,
)
};

if raw.is_null() || err != ffi::MYSOFA_OK {
return Err(Error::from_raw(err));
}

Ok(Sofar {
raw,
filter_len: filter_len as usize,
cached: self.cached,
})
}
}

impl Default for OpenOptions {
Expand Down Expand Up @@ -227,6 +267,18 @@ impl Sofar {
OpenOptions::new().open(path)
}

/// Open a SOFA using provided bytes and the default open options
///
/// ```no_run
/// use sofar::reader::Sofar;
/// let data: Vec<u8> = std::fs::read("my/sofa/file.sofa").unwrap();
///
/// let sofa = Sofar::open_data(&data).unwrap();
/// ```
pub fn open_data<B: AsRef<[u8]>>(bytes: B) -> Result<Sofar, Error> {
OpenOptions::new().open_data(bytes)
}

pub fn filter_len(&self) -> usize {
self.filter_len
}
Expand Down

0 comments on commit b9f71fd

Please sign in to comment.