Implemented new audio engine and deck engine

This commit is contained in:
patben8 committed 2026-11-02 22:26:19 +01:00
1 parent 4fdb2b7ce1
commit f5e7202f3f
19 files changed
+1794 -577

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
# Rubber Band (keylock)
`src/lib/audio/rubberband.wasm` is the [Rubber Band Library](https://breakfastquay.com/rubberband/)
3.3.0 compiled to WebAssembly with Konduktor's own glue (`glue.c`). The deck's
audio worklet uses it for keylock: tempo changes without a pitch change. It
runs the R2 ("faster") engine in real-time mode.
**Licence.** Rubber Band is GPL-2.0-or-later. Every upstream source file says
"either version 2 of the License, or (at your option) any later version", and
its bundled `src/ext` code (KissFFT, Speex resampler, getopt, pommier) is
permissive. That is what allows it to combine with Konduktor's AGPL-3.0, via
GPLv3 §13. Do NOT replace it with a prebuilt npm package:
`rubberband-wasm`'s glue is labelled "GPLv2" without "or later", and
`@echogarden/rubberband-wasm` is GPL-2.0-only. Neither can be combined with
AGPL-3.0.
**Corresponding source** for the `.wasm`: `build.sh` plus `glue.c` plus the
upstream release it downloads, which is pinned by version and SHA-256.
**Rebuilding** is needed only when bumping Rubber Band or changing the glue:
run `./build.sh`, which needs `emcc` on PATH (`brew install emscripten`). The
`.wasm` is checked in, so neither `npm run dev` nor a release build needs
emscripten.
**Measured** (2026-11-02, Apple M3 Pro, stereo 44.1 kHz, 128-frame quanta):
about 40-55x realtime in both JavaScriptCore (WKWebView) and V8 (WebView2).
p99 is about 0.17 ms per quantum against a 2.9 ms budget, so 8 stretchers
(2 stem decks) fit on the one audio render thread. The R3 ("finer") engine is
about 11x realtime and does not fit at 8. `-msimd128` measured no faster than
the `-Oz` npm build.
+48
View File
@@ -0,0 +1,48 @@
#!/usr/bin/env bash
# Builds src/lib/audio/rubberband.wasm — the Rubber Band Library compiled to
# WebAssembly with Konduktor's own glue (glue.c) — for the deck's keylock.
#
# The .wasm is checked in, so dev and release builds need no emscripten; rerun
# this only when bumping Rubber Band or changing the glue. Needs emcc on PATH
# (macOS: `brew install emscripten`).
#
# Upstream is pinned by version AND SHA-256. Rubber Band is GPL-2.0-or-later
# ("either version 2 of the License, or (at your option) any later version"),
# which is what lets it combine with Konduktor's AGPL-3.0 — do NOT swap in a
# prebuilt npm package: their glue is labelled GPL-2.0(-only).
set -euo pipefail
cd "$(dirname "$0")"
VERSION=3.3.0
SHA256=d9ef89e2b8ef9f85b13ac3c2faec30e20acf2c9f3a9c8c45ce637f2bc95e576c
TARBALL="upstream/rubberband-${VERSION}.tar.bz2"
SRC="upstream/rubberband-${VERSION}"
OUT=../src/lib/audio/rubberband.wasm
mkdir -p upstream
if [ ! -f "$TARBALL" ]; then
curl -fsSL -o "$TARBALL" "https://breakfastquay.com/files/releases/rubberband-${VERSION}.tar.bz2"
fi
echo "${SHA256} ${TARBALL}" | shasum -a 256 -c -
rm -rf "$SRC"
tar xjf "$TARBALL" -C upstream
# -O3 + SIMD for speed on the audio thread (every deck's stretchers share one
# render thread). Single-threaded: RubberBandSingle.cpp sets NO_THREADING.
FLAGS=(-O3 -flto -msimd128 -fno-rtti -I"$SRC/rubberband")
# Rubber Band is C++, the glue C: compile each with its own driver, link as C++.
OBJ=upstream/obj
mkdir -p "$OBJ"
em++ "${FLAGS[@]}" -c "$SRC/single/RubberBandSingle.cpp" -o "$OBJ/rubberband.o"
emcc "${FLAGS[@]}" -c glue.c -o "$OBJ/glue.o"
em++ "${FLAGS[@]}" "$OBJ/rubberband.o" "$OBJ/glue.o" \
-sSTANDALONE_WASM=1 \
-sALLOW_MEMORY_GROWTH=1 \
-sFILESYSTEM=0 \
-sASSERTIONS=0 \
-sERROR_ON_UNDEFINED_SYMBOLS=1 \
--no-entry \
-o "$OUT"
echo "wrote $OUT ($(wc -c < "$OUT" | tr -d ' ') bytes)"
+48
View File
@@ -0,0 +1,48 @@
/*
* Konduktor's WebAssembly interface to the Rubber Band Library, used by the
* deck's keylock (time-stretch without a pitch change). Part of Konduktor,
* licensed under the GNU AGPL-3.0 (see LICENSE at the repository root).
*
* Plain exports of the C API the deck worklet calls, plus malloc/free so it
* can place its sample buffers in the module's memory. Rubber Band itself is
* GPL-2.0-or-later (see README.md here).
*/
#include <stdlib.h>
#include <emscripten.h>
#include <rubberband-c.h>
#define EXPORT EMSCRIPTEN_KEEPALIVE
EXPORT void *kd_malloc(size_t size) { return malloc(size); }
EXPORT void kd_free(void *ptr) { free(ptr); }
EXPORT RubberBandState kd_new(unsigned int sample_rate, unsigned int channels,
RubberBandOptions options, double time_ratio,
double pitch_scale) {
return rubberband_new(sample_rate, channels, options, time_ratio, pitch_scale);
}
EXPORT void kd_delete(RubberBandState s) { rubberband_delete(s); }
EXPORT void kd_reset(RubberBandState s) { rubberband_reset(s); }
EXPORT void kd_set_time_ratio(RubberBandState s, double r) { rubberband_set_time_ratio(s, r); }
EXPORT void kd_set_pitch_scale(RubberBandState s, double p) { rubberband_set_pitch_scale(s, p); }
EXPORT void kd_set_max_process_size(RubberBandState s, unsigned int n) {
rubberband_set_max_process_size(s, n);
}
EXPORT unsigned int kd_get_preferred_start_pad(RubberBandState s) {
return rubberband_get_preferred_start_pad(s);
}
EXPORT unsigned int kd_get_start_delay(RubberBandState s) { return rubberband_get_start_delay(s); }
EXPORT unsigned int kd_get_samples_required(RubberBandState s) {
return rubberband_get_samples_required(s);
}
EXPORT void kd_process(RubberBandState s, const float *const *input, unsigned int samples,
int final) {
rubberband_process(s, input, samples, final);
}
EXPORT int kd_available(RubberBandState s) { return rubberband_available(s); }
EXPORT unsigned int kd_retrieve(RubberBandState s, float *const *output, unsigned int samples) {
return rubberband_retrieve(s, output, samples);
}