Skip to content

Commit

Permalink
Feat/wasm (#13)
Browse files Browse the repository at this point in the history
feat: add wasm
  • Loading branch information
thewh1teagle authored Dec 8, 2024
1 parent e7940fa commit de32ec2
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 10 deletions.
24 changes: 17 additions & 7 deletions .github/workflows/publish-libaec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,37 @@ jobs:
fail-fast: false
matrix:
include:
- platform: "macos-latest" # for Arm-based Macs (M1 and above)
- platform: "macos-latest" # Wasm32-emscripten
args: "--target wasm32-unknown-emscripten"
build-dir: "libaec-wasm32-unknown-emscripten"
archive: "tar"
target: "wasm32-unknown-emscripten"

- platform: "macos-latest" # macOS arm64
args: "--target aarch64-apple-darwin"
build-dir: "libaec-osx-aarch64"
archive: "tar"
target: "aarch64-apple-darwin"

- platform: "macos-latest" # for Apple IOS
- platform: "macos-latest" # IOS
args: "--target aarch64-apple-ios"
build-dir: "libaec-ios-aarch64"
archive: "tar"
target: "aarch64-apple-ios"

- platform: "macos-latest" # for Intel-based Macs
- platform: "macos-latest" # macOS x86-64
args: "--target x86_64-apple-darwin"
build-dir: "libaec-osx-x86-64"
archive: "tar"
target: "x86_64-apple-darwin"

- platform: "ubuntu-22.04" # Linux 22.04 x86_64
- platform: "ubuntu-22.04" # Linux x86_64
args: ""
build-dir: "libaec-linux-x86-64"
archive: "tar"
target: "x86_64-unknown-linux-gnu"

- platform: "macos-latest" # Linux Android arm64
- platform: "macos-latest" # Linux arm64
args: ""
build-dir: "libaec-android-aarch64"
archive: "tar"
Expand All @@ -47,13 +53,13 @@ jobs:
archive: "zip"
target: "x86_64-pc-windows-msvc"

- platform: "windows-latest" # Windows ARM (aarch64)
- platform: "windows-latest" # Windows arm64
args: "--target aarch64-pc-windows-msvc"
build-dir: "libaec-win-aarch64"
archive: "zip"
target: "aarch64-pc-windows-msvc"

- platform: "ubuntu-22.04" # Raspberry Pi 4 (64-bit ARM)
- platform: "ubuntu-22.04" # Linux arm64
args: "--target aarch64-unknown-linux-gnu"
build-dir: "libaec-linux-aarch64"
archive: "tar"
Expand Down Expand Up @@ -83,6 +89,10 @@ jobs:
local-cache: true
if: matrix.target == 'aarch64-linux-android'

- name: Setup emsdk
uses: mymindstorm/setup-emsdk@v14
if: contains(matrix.target, 'wasm')

- name: Install Rust stable
uses: dtolnay/rust-toolchain@stable
with:
Expand Down
11 changes: 11 additions & 0 deletions BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ Build
cargo build --release --target aarch64-apple-ios
```

## Build for wasm

Use [wasm-pack](https://rustwasm.github.io/docs/wasm-pack) with [emscripten.org](https://emscripten.org)

```console
brew install emscripten
rustup target add wasm32-unknown-emscripten
cargo build --release --target wasm32-unknown-emscripten
CC=emcc AR=emar wasm-pack build
```

## Build pyaec (Python)

Use [uv](https://astral.sh/blog/uv)
Expand Down
61 changes: 61 additions & 0 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,12 @@ homepage = "https://github.com/thewh1teagle/aec-rs"
[dependencies]
aec-rs-sys = { path = "crates/aec-rs-sys", version = "1.0.0" }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = { version = "0.2.99" }

[lib]
# For wasm
crate-type = ["cdylib", "rlib"]

[dev-dependencies]
hound = { version = "3.5.1" }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Acoustic echo cancellation in Rust based on [speexdsp](https://github.com/xiph/s
- 🦀 Rust and 🐍 Python support
- 🔗 Easy integration with C/C++ (or any other language) via C API
- 📦 Precompiled library and C header files available in the [releases](https://github.com/thewh1teagle/aec-rs/releases/latest)
- 🖥️ Support for Windows (x86/arm64), Linux (x86/arm64), macOS (x86/arm64), Android (arm64), IOS (arm64)
- 🖥️ Support for Windows (x86/arm64), Linux (x86/arm64), macOS (x86/arm64), Android (arm64), IOS (arm64), and WASM!
- 🖥️ Run on Raspberry PI as well

# Install
Expand Down
11 changes: 9 additions & 2 deletions crates/aec-rs-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@ fn main() {
if target.contains("android") {
clang_target = "armv8-linux-androideabi".to_string();
}
let bindings = bindgen::Builder::default()
.header("wrapper.h")

let mut bindings = bindgen::Builder::default().header("wrapper.h");

if target.contains("wasm") {
// See https://github.com/rust-lang/rust-bindgen/issues/2624#issuecomment-1708117271
bindings = bindings.clang_arg("-fvisibility=default");
}

let bindings = bindings
.clang_arg(format!("-I{}", lib_dst.display()))
// Explicitly set target in case we are cross-compiling.
// See https://github.com/rust-lang/rust-bindgen/issues/1780 for context.
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

use std::os::raw::c_void;
/// See https://www.speex.org/docs/api/speex-api-reference/speex__echo_8h.html
#[derive(Debug, Clone)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
pub struct AecConfig {
pub frame_size: usize,
pub filter_length: i32,
Expand All @@ -21,12 +25,15 @@ impl Default for AecConfig {
}
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
pub struct Aec {
echo_state: *mut aec_rs_sys::SpeexEchoState,
preprocess_state: Option<*mut aec_rs_sys::SpeexPreprocessState>,
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
impl Aec {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
pub fn new(config: &AecConfig) -> Self {
let echo_state = unsafe {
aec_rs_sys::speex_echo_state_init(config.frame_size as i32, config.filter_length)
Expand Down Expand Up @@ -54,6 +61,7 @@ impl Aec {
}
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
pub fn cancel_echo(&self, rec_buffer: &[i16], echo_buffer: &[i16], out_buffer: &mut [i16]) {
unsafe {
aec_rs_sys::speex_echo_cancellation(
Expand Down

0 comments on commit de32ec2

Please sign in to comment.