Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm dsp testing ground #899

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ paper
pnpm-lock.yaml
pnpm-workspace.yaml
**/dev-dist
superdough-wasm
website/.astro
1 change: 1 addition & 0 deletions packages/superdough-wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rustsaw/target
57 changes: 57 additions & 0 deletions packages/superdough-wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# superdough-wasm

This is just a very early experiment to find out how to run wasm in an AudioWorklet.
WASM can be compiled from several languages, which are tested here..

## zig

<https://dev.to/sleibrock/webassembly-with-zig-part-1-4onm>

```sh
# (re)compile dsp.zig
brew install zig # prequisite
cd zigsaw
zig build-lib zigsaw.zig -target wasm32-freestanding -dynamic -rdynamic -O ReleaseSmall # build
npx http-server .. -o # run
```

wasm file size: 690B

## rust

<https://developer.mozilla.org/en-US/docs/WebAssembly/Rust_to_Wasm>

```sh
# https://www.rust-lang.org/tools/install
cargo install wasm-pack # prequisite
cd rustsaw
wasm-pack build --target bundler # build
npx http-server .. -o # run
```

wasm file size: 653B

## c

<https://emscripten.org/docs/getting_started/Tutorial.html>

```sh
# brew install emscripten
cd csaw
emcc -O2 csaw.c -o csaw # build
npx http-server .. -o
```

wasm file size: 680B

## assemblyscript

<https://www.assemblyscript.org/getting-started.html#setting-up-a-new-project>

```sh
cd ascsaw
# npm i
npm run asbuild # build
```

wasm file size: 122B !
22 changes: 22 additions & 0 deletions packages/superdough-wasm/ascsaw/asconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"targets": {
"debug": {
"outFile": "build/debug.wasm",
"textFile": "build/debug.wat",
"sourceMap": true,
"debug": true
},
"release": {
"outFile": "build/release.wasm",
"textFile": "build/release.wat",
"sourceMap": true,
"optimizeLevel": 3,
"shrinkLevel": 2,
"converge": false,
"noAssert": false
}
},
"options": {
"bindings": "esm"
}
}
3 changes: 3 additions & 0 deletions packages/superdough-wasm/ascsaw/assembly/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function saw(t: f64, f: f64): f64 {
return (((f * t * 1.0) % 1.0) - 0.5) * 2.0;
}
6 changes: 6 additions & 0 deletions packages/superdough-wasm/ascsaw/assembly/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "assemblyscript/std/assembly.json",
"include": [
"./**/*.ts"
]
}
2 changes: 2 additions & 0 deletions packages/superdough-wasm/ascsaw/build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
54 changes: 54 additions & 0 deletions packages/superdough-wasm/ascsaw/package-lock.json

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

25 changes: 25 additions & 0 deletions packages/superdough-wasm/ascsaw/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "ascsaw",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "node tests",
"asbuild:debug": "asc assembly/index.ts --target debug",
"asbuild:release": "asc assembly/index.ts --target release",
"asbuild": "npm run asbuild:debug && npm run asbuild:release",
"start": "npx serve ."
},
"author": "",
"license": "ISC",
"devDependencies": {
"assemblyscript": "^0.27.22"
},
"type": "module",
"exports": {
".": {
"import": "./build/release.js",
"types": "./build/release.d.ts"
}
}
}
1 change: 1 addition & 0 deletions packages/superdough-wasm/csaw/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
csaw
7 changes: 7 additions & 0 deletions packages/superdough-wasm/csaw/csaw.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <math.h>
#include <emscripten.h>

EMSCRIPTEN_KEEPALIVE
double saw(double t, double f) {
return fmod((f * t * 1.0), 1.0) - 0.5 * 2.0;
}
Binary file added packages/superdough-wasm/csaw/csaw.wasm
Binary file not shown.
11 changes: 11 additions & 0 deletions packages/superdough-wasm/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html>
<head>
<title>WASM AudioWorklet Demo</title>
</head>
<body>
<button id="play">play</button>
<input type="range" min="55" max="880" id="freq" />
</body>
<script src="./main.js"></script>
</html>
24 changes: 24 additions & 0 deletions packages/superdough-wasm/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
let ac;
document.getElementById('play').addEventListener('click', async () => {
ac = ac || new AudioContext();
await ac.resume();
await ac.audioWorklet.addModule('./worklet.js');
const node = new AudioWorkletNode(ac, 'saw-processor');

//let res = await fetch('./zigsaw/zigsaw.wasm');
// let res = await fetch('./csaw/csaw.wasm');
let res = await fetch('./ascsaw/build/release.wasm');
//let res = await fetch('./rustsaw/pkg/rustsaw_bg.wasm');
const buffer = await res.arrayBuffer();
node.port.onmessage = (e) => {
if (e.data === 'OK') {
console.log('worklet ready');
}
};
node.port.postMessage({ webassembly: buffer });
node.connect(ac.destination);

document.getElementById('freq').addEventListener('input', async (e) => {
node.port.postMessage({ frequency: e.target.value });
});
});
123 changes: 123 additions & 0 deletions packages/superdough-wasm/rustsaw/Cargo.lock

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

13 changes: 13 additions & 0 deletions packages/superdough-wasm/rustsaw/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "rustsaw"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
description = "A sample project with wasm-pack"
license = "MIT/Apache-2.0"
edition = "2018"

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.2"
6 changes: 6 additions & 0 deletions packages/superdough-wasm/rustsaw/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn saw(t: f64, f: f64) -> f64 {
return (((f * t * 1.0) % 1.0) - 0.5) * 2.0;
}
Loading
Loading