Skip to content

Commit

Permalink
Enable building libmysofa without cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiltd committed Jun 22, 2024
1 parent 75a1da0 commit 3c34a6d
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 15 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@ categories = ["algorithms", "filesystem", "multimedia::audio"]
[features]
default = ["dsp"]
dsp = ["dep:realfft"]
cmake-build = ["ffi/cmake-build"]

[workspace]
members = ["libmysofa-sys"]

[dependencies]
ffi = { package = "libmysofa-sys", version = "0.1", path = "libmysofa-sys" }
realfft = {version = "3.2", optional = true}
realfft = {version = "3.3", optional = true}
thiserror = "1"

[dev-dependencies]
anyhow = "1.0"
assert_approx_eq = "1.1"
hound = "3.5"
cpal = "0.15"
criterion = "0.4"
criterion = "0.5"
rand = "0.8"
ringbuf = "0.3"
ringbuf = "0.4"

[[bench]]
name = "renderer"
Expand Down
14 changes: 7 additions & 7 deletions examples/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use hound::WavReader;
use sofar::reader::{Filter, OpenOptions, Sofar};
use sofar::render::Renderer;

use ringbuf::HeapRb;
use ringbuf::{traits::*, HeapRb};

use std::sync::{Arc, Condvar, Mutex};
use std::{env, io::Read};
Expand Down Expand Up @@ -92,7 +92,7 @@ where
let (mut producer, mut consumer) = ringbuf.split();

for _ in 0..BLOCK_LEN {
producer.push(0.0).unwrap();
producer.try_push(0.0).unwrap();
}

let stream = device.build_output_stream(
Expand All @@ -111,7 +111,7 @@ where
return;
}

while data_samples >= consumer.len() {
while data_samples >= consumer.occupied_len() {
let src = reader
.samples::<f32>()
.take(BLOCK_LEN)
Expand All @@ -125,14 +125,14 @@ where
.unwrap();

for (l, r) in Iterator::zip(left.iter(), right.iter()) {
producer.push(*l).unwrap();
producer.push(*r).unwrap();
producer.try_push(*l).unwrap();
producer.try_push(*r).unwrap();
}
}

for dst in data.chunks_exact_mut(2) {
dst[0] = consumer.pop().unwrap();
dst[1] = consumer.pop().unwrap();
dst[0] = consumer.try_pop().unwrap();
dst[1] = consumer.try_pop().unwrap();
}
},
|err| eprintln!("An error occurred on stream: {}", err),
Expand Down
11 changes: 9 additions & 2 deletions libmysofa-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ exclude = ["libmysofa/tests"]
libz-sys = { version = "1.1", default-features = false, features = ["static", "libc"] }

[build-dependencies]
cmake = "0.1"
system-deps = "6.1"
cc = "1.0.99"
system-deps = "7.0"
cmake = { version = "0.1", optional = true }

[features]
default = []

# Build libmysofa with its CMake. Requires that CMake is installed on the system.
cmake-build = ["cmake"]

[package.metadata.system-deps]
libmysofa = "1"
63 changes: 61 additions & 2 deletions libmysofa-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,69 @@
use cmake::Config;
use system_deps::{BuildInternalClosureError, Library};

use std::env;

#[cfg(not(feature = "cmake-build"))]
pub fn build_from_src(lib: &str, version: &str) -> Result<Library, BuildInternalClosureError> {
let mut config = Config::new("libmysofa");
let lib = lib.strip_prefix("lib").unwrap_or(lib);
let dst = std::path::PathBuf::from(env::var_os("OUT_DIR").unwrap());
let z_root = env::var_os("DEP_Z_ROOT").unwrap();

let mut cfg = cc::Build::new();
cfg.std("gnu99")
.flag_if_supported("-Wno-sign-compare")
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable")
.include("libmysofa/src/hrtf")
.include("src")
.file("libmysofa/src/hrtf/cache.c")
.file("libmysofa/src/hdf/gunzip.c")
.file("libmysofa/src/hdf/gcol.c")
.file("libmysofa/src/hrtf/tools.c")
.file("libmysofa/src/hdf/superblock.c")
.file("libmysofa/src/hrtf/loudness.c")
.file("libmysofa/src/hrtf/lookup.c")
.file("libmysofa/src/hdf/btree.c")
.file("libmysofa/src/hrtf/minphase.c")
.file("libmysofa/src/hrtf/neighbors.c")
.file("libmysofa/src/hrtf/easy.c")
.file("libmysofa/src/hrtf/check.c")
.file("libmysofa/src/hrtf/kdtree.c")
.file("libmysofa/src/hrtf/spherical.c")
.file("libmysofa/src/hrtf/reader.c")
.file("libmysofa/src/hdf/fractalhead.c")
.file("libmysofa/src/hrtf/interpolate.c")
.file("libmysofa/src/hrtf/resample.c")
.file("libmysofa/src/hdf/dataobject.c")
.file("libmysofa/src/resampler/speex_resampler.c")
.compile(lib);

Ok(Library {
name: lib.to_owned(),
version: version.to_owned(),
source: system_deps::Source::EnvVariables,
link_paths: vec![dst, z_root.into()],
libs: vec![
system_deps::InternalLib {
name: lib.to_owned(),
is_static_available: true,
},
system_deps::InternalLib {
name: "z".to_owned(),
is_static_available: true,
},
],
frameworks: Default::default(),
framework_paths: Default::default(),
include_paths: Default::default(),
defines: Default::default(),
ld_args: Default::default(),
statik: true,
})
}

#[cfg(feature = "cmake-build")]
pub fn build_from_src(lib: &str, version: &str) -> Result<Library, BuildInternalClosureError> {
let mut config = cmake::Config::new("libmysofa");
let z_root = env::var_os("DEP_Z_ROOT").unwrap();

let dst = config
Expand Down
9 changes: 9 additions & 0 deletions libmysofa-sys/src/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _CONFIG_H
#define _CONFIG_H

#define CMAKE_INSTALL_PREFIX "/usr/local"
#define CPACK_PACKAGE_VERSION_MAJOR 1
#define CPACK_PACKAGE_VERSION_MINOR 3
#define CPACK_PACKAGE_VERSION_PATCH 2

#endif // _CONFIG_H
7 changes: 7 additions & 0 deletions libmysofa-sys/src/mysofa_export.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef MYSOFA_EXPORT_H_
#define MYSOFA_EXPORT_H_

#define MYSOFA_EXPORT
#define MYSOFA_NO_EXPORT

#endif // MYSOFA_EXPORT_H_

0 comments on commit 3c34a6d

Please sign in to comment.