diff --git a/.github/workflows/linux-aarch64.yml b/.github/workflows/linux-aarch64.yml new file mode 100644 index 0000000..562e2bd --- /dev/null +++ b/.github/workflows/linux-aarch64.yml @@ -0,0 +1,145 @@ +# aarch64 Linux build, gates and release tarball. +# +# WHY THIS EXISTS. x86_64 releases are built on the author's own machine. There +# is no ARM hardware on hand (no Pi, no arm64 laptop), so the aarch64 build can +# only be compiled and checked on a runner of its own architecture. +# +# WHY A DEBIAN CONTAINER ON AN UBUNTU RUNNER. `runs-on:` names the virtual +# machine, and GitHub hosts no Debian runner. Everything that reaches the +# tarball -- compiler, glibc, GTK3, JACK, lilv/suil/sratom, sndfile -- comes +# from debian:12 (bookworm). Ubuntu contributes only the kernel, Docker and the +# git that does the checkout. Debian 12 is glibc 2.36, so the binary runs on +# bookworm-era systems (Raspberry Pi OS included); a bare ubuntu-24.04-arm +# runner is glibc 2.39 and would raise that floor. +# +# WHAT THIS DOES NOT COVER: no live audio, no JACK server, no xrun count, no +# CPU-load figure. It proves the aarch64 build compiles clean, links, stays +# under the glibc ceiling and packages -- not that it sounds right on a board. +name: Linux aarch64 + +# Every push on every branch, except commits that cannot change the build. The +# list says what to IGNORE rather than what to match, so a forgotten entry costs +# a spare run instead of silently skipping a check. icons/ is NOT ignored: the +# tarball packages it. +on: + push: + branches: ['**'] + paths-ignore: &docs_only + - '**.md' + - 'LICENSE' + - '.gitignore' + pull_request: + paths-ignore: *docs_only + workflow_dispatch: + +concurrency: + group: linux-aarch64-${{ github.ref }} + cancel-in-progress: true + +jobs: + build: + name: aarch64 (ubuntu-24.04-arm, debian:12) + # GitHub-hosted arm64 runner, free for public repositories. Check the label + # against https://github.com/actions/runner-images before changing it. + runs-on: ubuntu-24.04-arm + + steps: + - uses: actions/checkout@v5 + + # Checkout and submodules run on the HOST, where git already is; only the + # build needs the Debian ABI. Never `--recursive`: the Makefile compiles + # only base/, pluginterfaces/ and public.sdk/ from the SDK, and its other + # submodules (vstgui4, doc, tutorials, cmake) are large and unused. + - name: Submodules + run: | + git submodule update --init ext/vst3sdk + git -C ext/vst3sdk submodule update --init base pluginterfaces public.sdk + + - name: Build, gate and package inside Debian 12 + run: | + docker run --rm -i \ + -v "$PWD:/src" -w /src \ + "debian:12" \ + bash -euo pipefail -s <<'CONTAINER' + export DEBIAN_FRONTEND=noninteractive + apt-get update -qq + + # One dev package per pkg-config probe in the Makefile: PKGS_REQ + # (gtk+-3.0, gtk+-x11-3.0, jack, sndfile), the optional alsa, + # samplerate, lilv-0, suil-0 and sratom-0, and the LV2 UI helpers' + # x11 and gtk+-2.0 (the gtk2 helper hosts GtkUI editors out of + # process, as in the x86_64 release). file and binutils are what the + # gates below run on; release-tarball.sh reads the arch with readelf. + apt-get install -y --no-install-recommends \ + build-essential pkg-config ca-certificates file binutils \ + libgtk-3-dev libjack-jackd2-dev libsndfile1-dev \ + libasound2-dev libsamplerate0-dev \ + liblilv-dev libsuil-dev libsratom-dev lv2-dev \ + libx11-dev libgtk2.0-dev + + # No `| head -1` on these: under pipefail, head closing the pipe early + # kills ldd with SIGPIPE (status 141) and errexit ends the job. + echo "== toolchain and ABI floor ==" + cc --version + ldd --version + uname -m + + echo "== build ==" + make VST3=1 -j"$(nproc)" 2>&1 | tee build.log + + BINS="src/jackdaw src/jackdaw-lv2ui-x11 src/jackdaw-lv2ui-gtk2" + + # The x86_64 build is warning-free under -Wall -Wextra, so any warning + # here is new and architecture-specific -- e.g. -Wtype-limits firing + # because char is unsigned on aarch64. SDK objects build with -w and + # never match ^src/. + echo "== gate: no compiler warnings in src/ ==" + if grep -E '^src/.*warning:' build.log; then + echo "FAIL: compiler warnings above" >&2; exit 1 + fi + + echo "== gate: every binary is aarch64 and resolves its libraries ==" + for b in $BINS; do + [ -f "$b" ] || { echo "FAIL: $b was not built" >&2; exit 1; } + info=$(file -b "$b") + echo "$b: $info" + case "$info" in + *'ARM aarch64'*) ;; + *) echo "FAIL: $b is not aarch64" >&2; exit 1 ;; + esac + if ldd "$b" | grep 'not found'; then + echo "FAIL: $b has unresolved libraries" >&2; exit 1 + fi + done + + # The container is what sets the floor; this catches a change of + # image, or a symbol pulled from a newer glibc some other way. + echo "== gate: glibc symbol ceiling 2.36 ==" + for b in $BINS; do + max=$(objdump -T "$b" | grep -o 'GLIBC_[0-9.]*' | sed 's/GLIBC_//' | sort -Vu | tail -n1) + echo "$b: GLIBC_$max" + if [ "$(printf '%s\n2.36\n' "$max" | sort -V | tail -n1)" != "2.36" ]; then + echo "FAIL: $b needs GLIBC_$max > 2.36" >&2; exit 1 + fi + done + + echo "== package ==" + ./release-tarball.sh + ls -l jackdaw-*-linux-aarch64.tar.gz + CONTAINER + + # The container ran as root, so everything it wrote is root-owned and the + # upload action, which does not run as root, cannot read it. + - name: Hand the workspace back to the runner + if: always() + run: sudo chown -R "$(id -u):$(id -g)" . + + # An artifact and not a release: the aarch64 build has never run on real + # hardware, so it is attached to a release by hand once someone has heard + # it on a board. + - name: Upload the release tarball + uses: actions/upload-artifact@v6 + with: + name: jackdaw-linux-aarch64 + path: jackdaw-*-linux-aarch64.tar.gz + if-no-files-found: error diff --git a/Makefile b/Makefile index 29245d2..50411a9 100644 --- a/Makefile +++ b/Makefile @@ -108,6 +108,7 @@ SRCS_C := \ $(SRCDIR)/undo.c \ $(SRCDIR)/project.c \ $(SRCDIR)/jackdaw-engine.c \ + $(SRCDIR)/rt_ringbuffer.c \ $(SRCDIR)/midicontrol.c \ $(SRCDIR)/pluginhost.c \ $(SRCDIR)/pluginhost_lv2.c \ diff --git a/README.md b/README.md index a65aefd..22539bf 100644 --- a/README.md +++ b/README.md @@ -164,8 +164,9 @@ a count of any recorded audio, so you can decide for yourself. ### Creating a release tarball -`release-tarball.sh` produces `jackdaw-.tar.gz` that unpacks into a -top-level `JackDAW/` directory containing the source, bundled headers, the VST3 +`release-tarball.sh` produces `jackdaw--linux-.tar.gz` (ARCH is +`x86_64` or `aarch64`, read from the binary; a `--no-binary` tarball is plain +`jackdaw-.tar.gz`) that unpacks into a top-level `JackDAW/` directory containing the source, bundled headers, the VST3 SDK, icons, packaging scripts and the prebuilt binary. Because the SDK is included the tarball is around 15 MB, and it builds VST3 support with no network access or submodule fetch. @@ -179,6 +180,16 @@ network access or submodule fetch. The default version comes from `#define VERSION` in `src/config.h`; edit the `VERSION=` variable at the top of the script, or pass it as the first argument. +### aarch64 (Raspberry Pi 4/5, Orange Pi 5, arm64 laptops) + +The aarch64 tarball is built by GitHub Actions (`.github/workflows/linux-aarch64.yml`) +on an arm64 runner inside a Debian 12 container, so it needs glibc 2.36 or newer. +Download it from the run's artifacts (`jackdaw-linux-aarch64`) and install it the +same way as x86_64; the installer checks that the binary's architecture matches. + +It has not yet been run on ARM hardware — it builds and packages, but no live +audio, xrun or CPU-load figure has been taken on a board. + --- ## Plugins @@ -207,11 +218,13 @@ On startup JackDAW scans and reports any **newly discovered** plugins. **Default search paths** (always present): - User: `~/.lv2`, `~/.vst`, `~/.vst3`, `~/.clap`, `~/.ladspa` -- LV2: `/usr/lib/lv2`, `/usr/local/lib/lv2`, `/usr/lib/x86_64-linux-gnu/lv2` +- LV2: `/usr/lib/lv2`, `/usr/local/lib/lv2`, `/usr/lib/-linux-gnu/lv2` - VST2: `/usr/lib/vst`, `/usr/local/lib/vst` -- VST3: `/usr/lib/vst3`, `/usr/local/lib/vst3`, `/usr/lib/x86_64-linux-gnu/vst3` +- VST3: `/usr/lib/vst3`, `/usr/local/lib/vst3`, `/usr/lib/-linux-gnu/vst3` - CLAP: `/usr/lib/clap`, `/usr/local/lib/clap` -- LADSPA: `/usr/lib/ladspa`, `/usr/local/lib/ladspa`, `/usr/lib/x86_64-linux-gnu/ladspa` +- LADSPA: `/usr/lib/ladspa`, `/usr/local/lib/ladspa`, `/usr/lib/-linux-gnu/ladspa` + +(`` is `x86_64` or `aarch64`, matching the build.) **Adding more folders / rescanning:** open the **Plugins…** paths dialog. It has **Add Folder…**, **Remove**, and **Scan** (rescan) buttons; extra paths are diff --git a/release-tarball.sh b/release-tarball.sh index de722cd..e2e132b 100755 --- a/release-tarball.sh +++ b/release-tarball.sh @@ -2,9 +2,11 @@ # # release-tarball.sh — build a JackDAW release tarball. # -# Produces jackdaw-.tar.gz which unpacks into a top-level directory -# named JackDAW/ containing the source, bundled headers, icons, packaging -# scripts and (by default) the prebuilt binary. +# Produces jackdaw--linux-.tar.gz (or jackdaw-.tar.gz +# for a source-only tarball) which unpacks into a top-level directory named +# JackDAW/ containing the source, bundled headers, icons, packaging scripts +# and (by default) the prebuilt binary. is read from the binary itself +# (x86_64, aarch64), so the name says what it runs on, not where it was packed. # # Usage: # ./release-tarball.sh [VERSION] [--no-binary] @@ -44,7 +46,21 @@ if [ -z "$VERSION" ]; then fi VERSION=${VERSION#v} # tolerate a leading 'v' -NAME="jackdaw-$VERSION" +# A binary tarball only runs on one architecture, so it says which in its +# name. Taken from the ELF header rather than uname -m, so a binary built +# elsewhere is still named for what it is. +ARCH="" +if [ "$INCLUDE_BINARY" -eq 1 ] && [ -f src/jackdaw ]; then + if command -v readelf >/dev/null 2>&1; then + case $(readelf -h src/jackdaw 2>/dev/null) in + *X86-64*) ARCH=x86_64 ;; + *AArch64*) ARCH=aarch64 ;; + esac + fi + [ -n "$ARCH" ] || ARCH=$(uname -m) +fi + +NAME="jackdaw-$VERSION${ARCH:+-linux-$ARCH}" TARBALL="$ROOT/$NAME.tar.gz" STAGE=$(mktemp -d) diff --git a/src/jackdaw-engine.c b/src/jackdaw-engine.c index f7b4056..861ef80 100644 --- a/src/jackdaw-engine.c +++ b/src/jackdaw-engine.c @@ -9,7 +9,7 @@ #include #include #include -#include +#include "rt_ringbuffer.h" #include /* jack_client_create_thread (RT-priority workers) */ #include #ifdef HAVE_SAMPLERATE @@ -103,10 +103,12 @@ typedef struct { /* Loop region (frames). Looping is active only while loop_enabled is set and * loop_end > loop_start; the playhead wraps loop_end -> loop_start once it has - * entered the region. Set/cleared from the main thread; read in the RT path. */ + * entered the region. Set/cleared from the main thread; read in the RT path. + * start/end are a pair: other threads read them through eng_loop_read(). */ volatile gint loop_enabled; volatile off_t loop_start; volatile off_t loop_end; + volatile gint loop_seq; /* odd while set_loop_range is mid-update */ /* Punch in/out recording (independent of looping). record_mode is set from * the UI menu; punch_armed is set when playback starts in punch mode and the @@ -157,20 +159,57 @@ typedef struct { volatile gint render_done; /* set by RT when render_end reached */ float *render_tap_L; float *render_tap_R; - jack_ringbuffer_t *render_rb_L; /* RT -> render writer thread */ - jack_ringbuffer_t *render_rb_R; + rt_ringbuffer_t *render_rb_L; /* RT -> render writer thread */ + rt_ringbuffer_t *render_rb_R; gboolean active; } JackDawEngine; static JackDawEngine engine; +/* Read a live port count from an RT thread. The count bounds reads of the + * port arrays, and the main thread publishes it with eng_publish_count() + * (a release store). The reader needs the matching acquire: volatile only + * stops the compiler caching the value, and on a weakly-ordered CPU + * (aarch64) a plain load followed by a load it merely guards (a control + * dependency) may be satisfied out of order. x86's TSO hid this. */ +static inline guint eng_count(volatile guint *slot) +{ + return (guint)g_atomic_int_get((gint *)slot); +} + +/* Loop range read off the main thread. start and end are two stores, so a + * reader could pair a new start with an old end and wrap to the wrong place + * (a seqlock around the pair closes that). The reader never waits for the + * writer: an RT thread spinning on a preempted main thread could spin for a + * whole scheduler slice. After a few tries it gives up and returns TRUE, and + * the caller keeps the pair it read last time, so a new range lands at most a + * cycle late but is never seen torn. */ +typedef struct { off_t start, end; } EngLoopRange; + +static gboolean eng_loop_read(EngLoopRange *out) +{ + for (int tries = 0; tries < 4; tries++) { + gint s0 = g_atomic_int_get(&engine.loop_seq); + if (s0 & 1) continue; + off_t a = __atomic_load_n(&engine.loop_start, __ATOMIC_RELAXED); + off_t b = __atomic_load_n(&engine.loop_end, __ATOMIC_RELAXED); + __atomic_thread_fence(__ATOMIC_ACQUIRE); /* pair loads before re-check */ + if (g_atomic_int_get(&engine.loop_seq) == s0) { + out->start = a; + out->end = b; + return FALSE; + } + } + return TRUE; +} + /* ----------------------------------------------------------------------- * Phase 2.5: Playback feeder thread * * A dedicated pthread fills play_buf_L/R ringbuffers from AudioClip data * so the RT callback always has audio to drain. No malloc/free/mutex - * in the feeder — only jack_ringbuffer_* (lock-free) and libsndfile reads. + * in the feeder — only rt_ringbuffer_* (lock-free) and libsndfile reads. * ----------------------------------------------------------------------- */ /* Output frames produced per slot per inner-loop pass (controls granularity). @@ -251,10 +290,10 @@ static void feeder_slot_release(guint i) static void feeder_write(JackDawTrack *t, size_t n) { if (n == 0) return; - jack_ringbuffer_write(t->play_buf_L, (const char *)feeder_L, - n * sizeof(float)); - jack_ringbuffer_write(t->play_buf_R, (const char *)feeder_R, - n * sizeof(float)); + rt_ringbuffer_write(t->play_buf_L, (const char *)feeder_L, + n * sizeof(float)); + rt_ringbuffer_write(t->play_buf_R, (const char *)feeder_R, + n * sizeof(float)); } static void feeder_emit_silence(JackDawTrack *t, size_t n) @@ -269,6 +308,7 @@ static void *feeder_thread_func(void *arg) (void)arg; guint i; struct timespec ts = { .tv_sec = 0, .tv_nsec = 2000000 }; /* 2 ms sleep */ + EngLoopRange feed_loop = { 0, 0 }; while (!feeder_stop_flag) { nanosleep(&ts, NULL); @@ -279,6 +319,7 @@ static void *feeder_thread_func(void *arg) continue; int jack_sr = (int)jack_get_sample_rate(engine.client); + eng_loop_read(&feed_loop); /* one pair for the whole pass */ for (i = 0; i < JACKDAW_MAX_TRACKS; i++) { JackDawTrack *t = engine.slots[i]; @@ -312,9 +353,9 @@ static void *feeder_thread_func(void *arg) /* --- Inner fill loop: top up the ringbuffer each wakeup --- */ while (TRUE) { - size_t space_L = jack_ringbuffer_write_space(t->play_buf_L) + size_t space_L = rt_ringbuffer_write_space(t->play_buf_L) / sizeof(float); - size_t space_R = jack_ringbuffer_write_space(t->play_buf_R) + size_t space_R = rt_ringbuffer_write_space(t->play_buf_R) / sizeof(float); size_t out_want = (space_L < space_R) ? space_L : space_R; if (out_want > FEEDER_CHUNK_FRAMES) @@ -332,8 +373,8 @@ static void *feeder_thread_func(void *arg) * past loop_end (playhead placed after the region) is left * alone, so playback there does not loop. --- */ if (g_atomic_int_get(&engine.loop_enabled)) { - off_t l_start = engine.loop_start; - off_t l_end = engine.loop_end; + off_t l_start = feed_loop.start; + off_t l_end = feed_loop.end; if (l_end > l_start) { if (pf == l_end) { pf = l_start; @@ -650,8 +691,8 @@ static void recorder_slot_finalize(guint i) JackDawTrack *t = engine.slots[i]; if (t && t->rec_buf_L && t->rec_buf_R) { while (TRUE) { - size_t avL = jack_ringbuffer_read_space(t->rec_buf_L) / sizeof(float); - size_t avR = jack_ringbuffer_read_space(t->rec_buf_R) / sizeof(float); + size_t avL = rt_ringbuffer_read_space(t->rec_buf_L) / sizeof(float); + size_t avR = rt_ringbuffer_read_space(t->rec_buf_R) / sizeof(float); size_t av = avL < avR ? avL : avR; if (av > REC_SCRATCH_FRAMES) av = REC_SCRATCH_FRAMES; /* Cap at expected_frames so the WAV ends exactly at the stop point */ @@ -662,8 +703,8 @@ static void recorder_slot_finalize(guint i) } if (av == 0) break; - jack_ringbuffer_read(t->rec_buf_L, (char *)rec_scratch_L, av * sizeof(float)); - jack_ringbuffer_read(t->rec_buf_R, (char *)rec_scratch_R, av * sizeof(float)); + rt_ringbuffer_read(t->rec_buf_L, (char *)rec_scratch_L, av * sizeof(float)); + rt_ringbuffer_read(t->rec_buf_R, (char *)rec_scratch_R, av * sizeof(float)); if (rs->channels == 1) { rs->written += sf_writef_float(rs->sf, rec_scratch_L, (sf_count_t)av); @@ -719,8 +760,8 @@ static void *recorder_thread_func(void *arg) if (!t || !t->rec_buf_L || !t->rec_buf_R) continue; while (TRUE) { - size_t avL = jack_ringbuffer_read_space(t->rec_buf_L) / sizeof(float); - size_t avR = jack_ringbuffer_read_space(t->rec_buf_R) / sizeof(float); + size_t avL = rt_ringbuffer_read_space(t->rec_buf_L) / sizeof(float); + size_t avR = rt_ringbuffer_read_space(t->rec_buf_R) / sizeof(float); size_t av = avL < avR ? avL : avR; if (av > REC_SCRATCH_FRAMES) av = REC_SCRATCH_FRAMES; /* Cap at expected_frames if stop has been signalled */ @@ -731,8 +772,8 @@ static void *recorder_thread_func(void *arg) } if (av == 0) break; - jack_ringbuffer_read(t->rec_buf_L, (char *)rec_scratch_L, av * sizeof(float)); - jack_ringbuffer_read(t->rec_buf_R, (char *)rec_scratch_R, av * sizeof(float)); + rt_ringbuffer_read(t->rec_buf_L, (char *)rec_scratch_L, av * sizeof(float)); + rt_ringbuffer_read(t->rec_buf_R, (char *)rec_scratch_R, av * sizeof(float)); if (rs->channels == 1) { rs->written += sf_writef_float(rs->sf, rec_scratch_L, (sf_count_t)av); @@ -812,11 +853,11 @@ static int eng_block_nev[JACKDAW_MAX_TRACKS]; /* ---- Preview-note injection (main thread -> RT) ---- * The main thread queues short MIDI messages tagged with a track slot; the RT * thread drains the ring once per cycle into per-slot scratch, and the gather - * function emits them at block offset 0. Lock-free SPSC via jack_ringbuffer. */ + * function emits them at block offset 0. Lock-free SPSC via rt_ringbuffer. */ #define ENG_PREVIEW_MAX 32 /* per-slot events drained per cycle */ typedef struct { gint32 slot; guint8 data[3]; } EngPrevMsg; -static jack_ringbuffer_t *eng_preview_rb; /* SPSC: main -> RT */ -static jack_ringbuffer_t *eng_control_rb; /* SPSC: RT -> main (control surface) */ +static rt_ringbuffer_t *eng_preview_rb; /* SPSC: main -> RT */ +static rt_ringbuffer_t *eng_control_rb; /* SPSC: RT -> main (control surface) */ static guint8 eng_preview_data[JACKDAW_MAX_TRACKS][ENG_PREVIEW_MAX][3]; static int eng_preview_n[JACKDAW_MAX_TRACKS]; @@ -840,7 +881,7 @@ static int eng_midi_cmp(const void *a, const void *b) * then live JACK MIDI input (while armed). Tracks sounding notes per slot. */ static int eng_gather_instrument_midi(int slot, JackDawTrack *t, off_t blk_start, jack_nframes_t nframes, gboolean playing, - gboolean armed, PhMidiEvent *mev, int cap) + void *mbuf, PhMidiEvent *mev, int cap) { int nev = 0; @@ -878,9 +919,10 @@ static int eng_gather_instrument_midi(int slot, JackDawTrack *t, off_t blk_start } } - if (armed && t->midi_in_idx >= 0 && - (guint)t->midi_in_idx < engine.midi_in_count && engine.midi_in[t->midi_in_idx]) { - void *mbuf = jack_port_get_buffer(engine.midi_in[t->midi_in_idx], nframes); + /* mbuf: this track's MIDI input for the cycle when it is armed, else NULL. + * Pre-fetched by the JACK thread; this runs on a worker, where + * jack_port_get_buffer() must not be called. */ + if (mbuf) { uint32_t mc = jack_midi_get_event_count(mbuf); for (uint32_t m = 0; m < mc && nev < cap; m++) { jack_midi_event_t ev; @@ -920,9 +962,9 @@ static int eng_gather_instrument_midi(int slot, JackDawTrack *t, off_t blk_start return nev; } -/* Enable flush-to-zero + denormals-are-zero on the calling thread's SSE unit. - * Cheap (two MXCSR register writes); safe to call every cycle. Re-armed before - * every plugin in pluginhost_process() too, in case a plugin clears MXCSR. */ +/* Enable flush-to-zero + denormals-are-zero on the calling thread (x86 MXCSR, + * aarch64 FPCR.FZ). Cheap; safe to call every cycle. Re-armed before every + * plugin in pluginhost_process() too, in case a plugin clears the bits. */ static inline void engine_rt_set_denormal_mode(void) { rt_set_denormal_mode(); @@ -1016,9 +1058,9 @@ static void engine_process_track(int i) } else { size_t got_L = 0, got_R = 0; if (t->play_buf_L && (flags & ENGINE_PLAYING)) - got_L = jack_ringbuffer_read(t->play_buf_L, (char *)bL, want); + got_L = rt_ringbuffer_read(t->play_buf_L, (char *)bL, want); if (t->play_buf_R && (flags & ENGINE_PLAYING)) - got_R = jack_ringbuffer_read(t->play_buf_R, (char *)bR, want); + got_R = rt_ringbuffer_read(t->play_buf_R, (char *)bR, want); if (got_L < want) memset((char *)bL + got_L, 0, want - got_L); if (got_R < want) memset((char *)bR + got_R, 0, want - got_R); } @@ -1047,7 +1089,10 @@ static void engine_process_track(int i) if (pk < REC_PEAK_MAX_BUCKETS) { t->rec_peak_buf[pk * 2] = wf_mn; t->rec_peak_buf[pk * 2 + 1] = wf_mx; - t->rec_peak_count = pk + 1; + /* Release store: the timeline reads the pair only once it + * sees the count, and a weakly-ordered CPU could otherwise + * make the count visible before the pair. */ + g_atomic_int_set(&t->rec_peak_count, pk + 1); } } } @@ -1062,7 +1107,8 @@ static void engine_process_track(int i) PhMidiEvent *mev = eng_block_ev[i]; int nev = eng_gather_instrument_midi(i, t, blk_start, nframes, (flags & ENGINE_PLAYING) != 0, - (tflags & TRACK_ARMED) != 0, + (tflags & TRACK_ARMED) ? + g_slot_midi_buf[i] : NULL, mev, ENG_MIDI_MAX_EV); eng_block_nev[i] = nev; if (chain && chain->n > 0) { @@ -1116,9 +1162,9 @@ static void engine_process_track(int i) /* Capture dry input to rec ringbuffers while recording. */ if (live_L && (flags & ENGINE_RECORDING)) { if (t->rec_buf_L) - jack_ringbuffer_write(t->rec_buf_L, (const char *)live_L, want); + rt_ringbuffer_write(t->rec_buf_L, (const char *)live_L, want); if (t->rec_buf_R) - jack_ringbuffer_write(t->rec_buf_R, (const char *)live_R, want); + rt_ringbuffer_write(t->rec_buf_R, (const char *)live_R, want); } /* Record MIDI with absolute timeline frames (instrument tracks). */ @@ -1136,8 +1182,8 @@ static void engine_process_track(int i) r.data[0] = ev.buffer[0]; r.data[1] = ev.size > 1 ? ev.buffer[1] : 0; r.data[2] = ev.size > 2 ? ev.buffer[2] : 0; - if (jack_ringbuffer_write_space(t->midi_rec_buf) >= sizeof r) - jack_ringbuffer_write(t->midi_rec_buf, (const char *)&r, sizeof r); + if (rt_ringbuffer_write_space(t->midi_rec_buf) >= sizeof r) + rt_ringbuffer_write(t->midi_rec_buf, (const char *)&r, sizeof r); } } } @@ -1200,12 +1246,12 @@ static int engine_process(jack_nframes_t nframes, void *arg) * so the live graph must touch none of them. Output silence, freeze the * transport, and return before any plugin or mix work. */ if (g_atomic_int_get(&engine.render_suspend)) { - for (i = 0; i < engine.audio_out_count; i++) { + for (i = 0; i < eng_count(&engine.audio_out_count); i++) { if (!engine.audio_out[i]) continue; port_buf = jack_port_get_buffer(engine.audio_out[i], nframes); memset(port_buf, 0, nframes * sizeof(float)); } - for (i = 0; i < engine.midi_out_count; i++) { + for (i = 0; i < eng_count(&engine.midi_out_count); i++) { if (!engine.midi_out[i]) continue; jack_midi_clear_buffer( jack_port_get_buffer(engine.midi_out[i], nframes)); @@ -1253,8 +1299,8 @@ static int engine_process(jack_nframes_t nframes, void *arg) for (i = 0; i < JACKDAW_MAX_TRACKS; i++) eng_preview_n[i] = 0; if (eng_preview_rb) { EngPrevMsg msg; - while (jack_ringbuffer_read_space(eng_preview_rb) >= sizeof msg) { - jack_ringbuffer_read(eng_preview_rb, (char *)&msg, sizeof msg); + while (rt_ringbuffer_read_space(eng_preview_rb) >= sizeof msg) { + rt_ringbuffer_read(eng_preview_rb, (char *)&msg, sizeof msg); if (msg.slot >= 0 && msg.slot < JACKDAW_MAX_TRACKS && eng_preview_n[msg.slot] < ENG_PREVIEW_MAX) { guint8 *d = eng_preview_data[msg.slot][eng_preview_n[msg.slot]++]; @@ -1277,8 +1323,8 @@ static int engine_process(jack_nframes_t nframes, void *arg) cm.data[0] = ev.buffer[0]; cm.data[1] = ev.size > 1 ? ev.buffer[1] : 0; cm.data[2] = ev.size > 2 ? ev.buffer[2] : 0; - if (jack_ringbuffer_write_space(eng_control_rb) >= sizeof cm) - jack_ringbuffer_write(eng_control_rb, (const char *)&cm, sizeof cm); + if (rt_ringbuffer_write_space(eng_control_rb) >= sizeof cm) + rt_ringbuffer_write(eng_control_rb, (const char *)&cm, sizeof cm); } } @@ -1289,9 +1335,11 @@ static int engine_process(jack_nframes_t nframes, void *arg) * remainder past loop_end is carried over so the clock's loop period equals * the region length, matching the feeder. Checked at block granularity, so * the loop point quantizes to the JACK period (acceptable for now). */ + static EngLoopRange rt_loop; /* JACK thread only; kept if a read fails */ + eng_loop_read(&rt_loop); if ((flags & ENGINE_PLAYING) && g_atomic_int_get(&engine.loop_enabled)) { - off_t l_start = engine.loop_start; - off_t l_end = engine.loop_end; + off_t l_start = rt_loop.start; + off_t l_end = rt_loop.end; off_t bstart = engine.play_pos - (off_t)nframes; if (l_end > l_start && bstart >= l_start && bstart < l_end && engine.play_pos >= l_end) @@ -1303,8 +1351,8 @@ static int engine_process(jack_nframes_t nframes, void *arg) * loop wrap; punch_armed gates it so normal recording is never affected. The * local `flags` is updated in step so the capture passes below act this block. */ if ((flags & ENGINE_PLAYING) && g_atomic_int_get(&engine.punch_armed)) { - off_t ls = engine.loop_start; - off_t le = engine.loop_end; + off_t ls = rt_loop.start; + off_t le = rt_loop.end; off_t bstart = engine.play_pos - (off_t)nframes; if (le > ls) { if (!(flags & ENGINE_RECORDING)) { @@ -1349,8 +1397,8 @@ static int engine_process(jack_nframes_t nframes, void *arg) * then fan the tracks out across the worker pool and sum the results. * * Pre-fetch: live audio input (for monitoring + dry capture) and the MIDI - * input buffer (for MIDI recording). Mirrors the gating that used to live in - * the per-track loop. */ + * input buffer (live instrument input + MIDI recording). Mirrors the gating + * that used to live in the per-track loop. */ for (i = 0; i < JACKDAW_MAX_TRACKS; i++) { g_slot_live_L[i] = NULL; g_slot_live_R[i] = NULL; @@ -1360,18 +1408,23 @@ static int engine_process(jack_nframes_t nframes, void *arg) gint tflags = g_atomic_int_get(&t->state_flags); gboolean instr = jackdaw_track_is_instrument(t); if (!instr && (tflags & TRACK_ARMED) && t->audio_in_idx >= 0 && - (guint)t->audio_in_idx < engine.audio_in_count && + (guint)t->audio_in_idx < eng_count(&engine.audio_in_count) && engine.audio_in[(guint)t->audio_in_idx]) { g_slot_live_L[i] = jack_port_get_buffer( engine.audio_in[(guint)t->audio_in_idx], nframes); - if (t->audio_src_port_r && engine.audio_in_r[(guint)t->audio_in_idx]) - g_slot_live_R[i] = jack_port_get_buffer( - engine.audio_in_r[(guint)t->audio_in_idx], nframes); + /* Loaded once: the main thread clears it before unregistering. */ + jack_port_t *pr = g_atomic_pointer_get( + &engine.audio_in_r[(guint)t->audio_in_idx]); + if (t->audio_src_port_r && pr) + g_slot_live_R[i] = jack_port_get_buffer(pr, nframes); } - if (instr && (tflags & TRACK_ARMED) && (flags & ENGINE_RECORDING) && + /* Every armed instrument track, not just while recording: the worker + * also feeds live input to the instrument (eng_gather_instrument_midi). + * The MIDI recorder keeps its own RECORDING gate. */ + if (instr && (tflags & TRACK_ARMED) && t->midi_in_idx >= 0 && - (guint)t->midi_in_idx < engine.midi_in_count && - engine.midi_in[t->midi_in_idx] && t->midi_rec_buf) + (guint)t->midi_in_idx < eng_count(&engine.midi_in_count) && + engine.midi_in[t->midi_in_idx]) g_slot_midi_buf[i] = jack_port_get_buffer( engine.midi_in[t->midi_in_idx], nframes); } @@ -1443,7 +1496,7 @@ static int engine_process(jack_nframes_t nframes, void *arg) } gfloat mpk_L = 0.0f, mpk_R = 0.0f; guint oi; - for (oi = 0; oi < engine.audio_out_count; oi++) { + for (oi = 0; oi < eng_count(&engine.audio_out_count); oi++) { if (!engine.audio_out[oi]) continue; port_buf = jack_port_get_buffer(engine.audio_out[oi], nframes); if (oi == 0) { @@ -1485,12 +1538,12 @@ static int engine_process(jack_nframes_t nframes, void *arg) engine.render_tap_R[k] = engine.master_R[k] * mvol; } size_t bytes = (size_t)in_range * sizeof(float); - if (jack_ringbuffer_write_space(engine.render_rb_L) >= bytes && - jack_ringbuffer_write_space(engine.render_rb_R) >= bytes) { - jack_ringbuffer_write(engine.render_rb_L, - (const char *)engine.render_tap_L, bytes); - jack_ringbuffer_write(engine.render_rb_R, - (const char *)engine.render_tap_R, bytes); + if (rt_ringbuffer_write_space(engine.render_rb_L) >= bytes && + rt_ringbuffer_write_space(engine.render_rb_R) >= bytes) { + rt_ringbuffer_write(engine.render_rb_L, + (const char *)engine.render_tap_L, bytes); + rt_ringbuffer_write(engine.render_rb_R, + (const char *)engine.render_tap_R, bytes); } } if (engine.play_pos >= engine.render_end) @@ -1533,7 +1586,7 @@ static int engine_process(jack_nframes_t nframes, void *arg) : engine.play_pos - (off_t)nframes; float *out_buf[2] = { NULL, NULL }; if (to_main) { - for (oi = 0; oi < engine.audio_out_count && oi < 2; oi++) + for (oi = 0; oi < eng_count(&engine.audio_out_count) && oi < 2; oi++) if (engine.audio_out[oi]) out_buf[oi] = jack_port_get_buffer( engine.audio_out[oi], nframes); @@ -1564,7 +1617,7 @@ static int engine_process(jack_nframes_t nframes, void *arg) } /* Clear all MIDI output buffers before any writes */ - for (oi = 0; oi < engine.midi_out_count; oi++) { + for (oi = 0; oi < eng_count(&engine.midi_out_count); oi++) { if (!engine.midi_out[oi]) continue; void *mbuf = jack_port_get_buffer(engine.midi_out[oi], nframes); jack_midi_clear_buffer(mbuf); @@ -1590,7 +1643,7 @@ static int engine_process(jack_nframes_t nframes, void *arg) JackDawTrack *t = engine.slots[i]; if (!t) continue; gint mi = t->midi_in_idx; - if (mi < 0 || (guint)mi >= engine.midi_out_count || !engine.midi_out[mi]) + if (mi < 0 || (guint)mi >= eng_count(&engine.midi_out_count) || !engine.midi_out[mi]) continue; if (out_done & (1u << mi)) continue; @@ -1609,7 +1662,7 @@ static int engine_process(jack_nframes_t nframes, void *arg) continue; } - if ((guint)mi >= engine.midi_in_count || !engine.midi_in[mi]) continue; + if ((guint)mi >= eng_count(&engine.midi_in_count) || !engine.midi_in[mi]) continue; void *ibuf = jack_port_get_buffer(engine.midi_in[mi], nframes); uint32_t mc = jack_midi_get_event_count(ibuf); uint32_t m; @@ -1672,18 +1725,18 @@ static int engine_buffer_size_cb(jack_nframes_t nframes, void *arg) for (i = 0; i < JACKDAW_MAX_TRACKS; i++) { JackDawTrack *t = engine.slots[i]; if (!t) continue; - if (t->play_buf_L) jack_ringbuffer_free(t->play_buf_L); - if (t->play_buf_R) jack_ringbuffer_free(t->play_buf_R); - if (t->rec_buf_L) jack_ringbuffer_free(t->rec_buf_L); - if (t->rec_buf_R) jack_ringbuffer_free(t->rec_buf_R); - t->play_buf_L = jack_ringbuffer_create(rb_bytes); - t->play_buf_R = jack_ringbuffer_create(rb_bytes); - t->rec_buf_L = jack_ringbuffer_create(rb_bytes); - t->rec_buf_R = jack_ringbuffer_create(rb_bytes); - if (t->play_buf_L) jack_ringbuffer_mlock(t->play_buf_L); - if (t->play_buf_R) jack_ringbuffer_mlock(t->play_buf_R); - if (t->rec_buf_L) jack_ringbuffer_mlock(t->rec_buf_L); - if (t->rec_buf_R) jack_ringbuffer_mlock(t->rec_buf_R); + if (t->play_buf_L) rt_ringbuffer_free(t->play_buf_L); + if (t->play_buf_R) rt_ringbuffer_free(t->play_buf_R); + if (t->rec_buf_L) rt_ringbuffer_free(t->rec_buf_L); + if (t->rec_buf_R) rt_ringbuffer_free(t->rec_buf_R); + t->play_buf_L = rt_ringbuffer_create(rb_bytes); + t->play_buf_R = rt_ringbuffer_create(rb_bytes); + t->rec_buf_L = rt_ringbuffer_create(rb_bytes); + t->rec_buf_R = rt_ringbuffer_create(rb_bytes); + if (t->play_buf_L) rt_ringbuffer_mlock(t->play_buf_L); + if (t->play_buf_R) rt_ringbuffer_mlock(t->play_buf_R); + if (t->rec_buf_L) rt_ringbuffer_mlock(t->rec_buf_L); + if (t->rec_buf_R) rt_ringbuffer_mlock(t->rec_buf_R); /* Ringbuffers just emptied — tell feeder to re-seek to play position */ feeder_slots[i].locate_frame = (off_t)engine.play_pos; g_atomic_int_set(&feeder_slots[i].locate_req, 1); @@ -2023,13 +2076,13 @@ gboolean jackdaw_engine_init(JackDawProject *project) if (!engine.metro_out) goto fail; /* Preview-note queue (main thread -> RT). Sized for many in-flight clicks. */ - eng_preview_rb = jack_ringbuffer_create(256 * sizeof(EngPrevMsg)); - if (eng_preview_rb) jack_ringbuffer_mlock(eng_preview_rb); + eng_preview_rb = rt_ringbuffer_create(256 * sizeof(EngPrevMsg)); + if (eng_preview_rb) rt_ringbuffer_mlock(eng_preview_rb); /* Control-surface input queue (RT -> main). Footswitch events are sparse; * 1024 slots is ample and the RT side drops on overflow, never blocks. */ - eng_control_rb = jack_ringbuffer_create(1024 * sizeof(JackDawCtlEvent)); - if (eng_control_rb) jack_ringbuffer_mlock(eng_control_rb); + eng_control_rb = rt_ringbuffer_create(1024 * sizeof(JackDawCtlEvent)); + if (eng_control_rb) rt_ringbuffer_mlock(eng_control_rb); /* Activate — after this the process callback can be called at any time */ if (jack_activate(engine.client) != 0) { @@ -2191,16 +2244,16 @@ void jackdaw_engine_quit(void) g_free(engine.slot_L[s]); engine.slot_L[s] = NULL; g_free(engine.slot_R[s]); engine.slot_R[s] = NULL; } - if (engine.render_rb_L) { jack_ringbuffer_free(engine.render_rb_L); engine.render_rb_L = NULL; } - if (engine.render_rb_R) { jack_ringbuffer_free(engine.render_rb_R); engine.render_rb_R = NULL; } + if (engine.render_rb_L) { rt_ringbuffer_free(engine.render_rb_L); engine.render_rb_L = NULL; } + if (engine.render_rb_R) { rt_ringbuffer_free(engine.render_rb_R); engine.render_rb_R = NULL; } g_free(engine.click_buf); engine.click_buf = NULL; engine.click_len = 0; g_free(engine.audio_in); engine.audio_in = NULL; g_free(engine.audio_out); engine.audio_out = NULL; g_free(engine.midi_in); engine.midi_in = NULL; g_free(engine.midi_out); engine.midi_out = NULL; - if (eng_preview_rb) { jack_ringbuffer_free(eng_preview_rb); eng_preview_rb = NULL; } - if (eng_control_rb) { jack_ringbuffer_free(eng_control_rb); eng_control_rb = NULL; } + if (eng_preview_rb) { rt_ringbuffer_free(eng_preview_rb); eng_preview_rb = NULL; } + if (eng_control_rb) { rt_ringbuffer_free(eng_control_rb); eng_control_rb = NULL; } engine.control_in = NULL; /* unregistered by jack_client_close above */ g_clear_pointer(&engine.control_src_port, g_free); } @@ -2210,9 +2263,9 @@ void jackdaw_engine_quit(void) gboolean jackdaw_engine_control_poll(JackDawCtlEvent *out) { if (!eng_control_rb || !out) return FALSE; - if (jack_ringbuffer_read_space(eng_control_rb) < sizeof(JackDawCtlEvent)) + if (rt_ringbuffer_read_space(eng_control_rb) < sizeof(JackDawCtlEvent)) return FALSE; - jack_ringbuffer_read(eng_control_rb, (char *)out, sizeof(JackDawCtlEvent)); + rt_ringbuffer_read(eng_control_rb, (char *)out, sizeof(JackDawCtlEvent)); return TRUE; } @@ -2251,8 +2304,8 @@ void jackdaw_engine_preview_note(JackDawTrack *t, guint8 pitch, msg.data[1] = (guint8)(pitch & 0x7F); msg.data[2] = on ? (velocity ? velocity : 1) : 0; - if (jack_ringbuffer_write_space(eng_preview_rb) >= sizeof msg) - jack_ringbuffer_write(eng_preview_rb, (const char *)&msg, sizeof msg); + if (rt_ringbuffer_write_space(eng_preview_rb) >= sizeof msg) + rt_ringbuffer_write(eng_preview_rb, (const char *)&msg, sizeof msg); } gboolean jackdaw_engine_is_running(void) @@ -2300,7 +2353,8 @@ gboolean jackdaw_engine_is_playing(void) * order that keeps the RT callback safe at every instant: * * grow — register the new ports FIRST, then raise the count. - * shrink — lower the count FIRST, then unregister the ports it dropped. + * shrink — lower the count FIRST, wait out any cycle that loaded the old + * count (eng_wait_rt_past), then unregister the ports it dropped. * * The old code did the opposite on both counts: it g_renew()'d the arrays the * callback was indexing (so a concurrent cycle could read through a freed @@ -2314,6 +2368,28 @@ static inline void eng_publish_count(volatile guint *slot, guint n) g_atomic_int_set((gint *)slot, (gint)n); } +/* Block until every RT cycle that could have seen the state from before this + * call has finished, so a port hidden from the callback (count lowered, or + * pointer cleared) can be unregistered. Lowering the count is not enough on + * its own: a cycle already in flight loaded the old count and may still be + * about to jack_port_get_buffer() the port. g_rt_cycle is bumped at the START + * of each callback, so two increments prove the in-flight cycle is over (the + * same rule as track_retire_is_safe). + * + * Bounded: if no cycle starts for ENG_RT_WAIT_US the callback is not being run + * (client zombified or server stalled), so nothing is in flight and returning + * is safe. Main thread only; costs at most about two periods. */ +#define ENG_RT_WAIT_US (2 * G_USEC_PER_SEC) +static void eng_wait_rt_past(void) +{ + if (!engine.active || !engine.client) return; + guint c0 = jackdaw_engine_get_cycle_count(); + gint64 deadline = g_get_monotonic_time() + ENG_RT_WAIT_US; + while ((guint)(jackdaw_engine_get_cycle_count() - c0) < 2u && + g_get_monotonic_time() < deadline) + g_usleep(500); +} + gboolean jackdaw_engine_set_audio_in_count(guint n) { guint i, old; @@ -2339,6 +2415,7 @@ gboolean jackdaw_engine_set_audio_in_count(guint n) eng_publish_count(&engine.audio_in_count, n); } else { eng_publish_count(&engine.audio_in_count, n); + eng_wait_rt_past(); for (i = n; i < old; i++) { if (engine.audio_in[i]) { jack_port_unregister(engine.client, engine.audio_in[i]); @@ -2379,6 +2456,7 @@ gboolean jackdaw_engine_set_audio_out_count(guint n) eng_publish_count(&engine.audio_out_count, n); } else { eng_publish_count(&engine.audio_out_count, n); + eng_wait_rt_past(); for (i = n; i < old; i++) { if (engine.audio_out[i]) { jack_port_unregister(engine.client, engine.audio_out[i]); @@ -2415,6 +2493,7 @@ gboolean jackdaw_engine_set_midi_in_count(guint n) eng_publish_count(&engine.midi_in_count, n); } else { eng_publish_count(&engine.midi_in_count, n); + eng_wait_rt_past(); for (i = n; i < old; i++) { if (engine.midi_in[i]) { jack_port_unregister(engine.client, engine.midi_in[i]); @@ -2451,6 +2530,7 @@ gboolean jackdaw_engine_set_midi_out_count(guint n) eng_publish_count(&engine.midi_out_count, n); } else { eng_publish_count(&engine.midi_out_count, n); + eng_wait_rt_past(); for (i = n; i < old; i++) { if (engine.midi_out[i]) { jack_port_unregister(engine.client, engine.midi_out[i]); @@ -2562,11 +2642,11 @@ gboolean jackdaw_engine_add_track(JackDawTrack *track) sr = engine.client ? jack_get_sample_rate(engine.client) : 48000; rb_bytes = (size_t)(2 * sr) * sizeof(float); - track->play_buf_L = jack_ringbuffer_create(rb_bytes); - track->play_buf_R = jack_ringbuffer_create(rb_bytes); - track->rec_buf_L = jack_ringbuffer_create(rb_bytes); - track->rec_buf_R = jack_ringbuffer_create(rb_bytes); - track->midi_rec_buf = jack_ringbuffer_create(TRACK_MIDI_RINGBUF_BYTES); + track->play_buf_L = rt_ringbuffer_create(rb_bytes); + track->play_buf_R = rt_ringbuffer_create(rb_bytes); + track->rec_buf_L = rt_ringbuffer_create(rb_bytes); + track->rec_buf_R = rt_ringbuffer_create(rb_bytes); + track->midi_rec_buf = rt_ringbuffer_create(TRACK_MIDI_RINGBUF_BYTES); if (!track->play_buf_L || !track->play_buf_R || !track->rec_buf_L || !track->rec_buf_R || !track->midi_rec_buf) { @@ -2574,10 +2654,10 @@ gboolean jackdaw_engine_add_track(JackDawTrack *track) return TRUE; } - jack_ringbuffer_mlock(track->play_buf_L); - jack_ringbuffer_mlock(track->play_buf_R); - jack_ringbuffer_mlock(track->rec_buf_L); - jack_ringbuffer_mlock(track->rec_buf_R); + rt_ringbuffer_mlock(track->play_buf_L); + rt_ringbuffer_mlock(track->play_buf_R); + rt_ringbuffer_mlock(track->rec_buf_L); + rt_ringbuffer_mlock(track->rec_buf_R); track->slot = i; engine.slots[i] = track; /* RT callback can see this now */ @@ -2642,9 +2722,11 @@ void jackdaw_engine_remove_track(JackDawTrack *track) if (track->audio_in_idx >= 0 && (guint)track->audio_in_idx < engine.audio_in_count && engine.audio_in_r[(guint)track->audio_in_idx]) { - jack_port_unregister(engine.client, - engine.audio_in_r[(guint)track->audio_in_idx]); - engine.audio_in_r[(guint)track->audio_in_idx] = NULL; + jack_port_t *pr = engine.audio_in_r[(guint)track->audio_in_idx]; + g_atomic_pointer_set(&engine.audio_in_r[(guint)track->audio_in_idx], + NULL); + eng_wait_rt_past(); + jack_port_unregister(engine.client, pr); } } g_clear_pointer(&track->audio_src_port, g_free); @@ -2822,7 +2904,7 @@ static void recorder_arm_all(void) * ringbuffer now (RECORDING isn't set yet, so the RT thread is not yet * writing to it). No WAV file. */ if (jackdaw_track_is_instrument(t)) { - if (t->midi_rec_buf) jack_ringbuffer_reset(t->midi_rec_buf); + if (t->midi_rec_buf) rt_ringbuffer_reset(t->midi_rec_buf); continue; } @@ -2882,7 +2964,7 @@ static gboolean midi_finalize_idle(gpointer data) for (guint i = 0; i < JACKDAW_MAX_TRACKS; i++) { JackDawTrack *t = engine.slots[i]; if (!t || !jackdaw_track_is_instrument(t) || !t->midi_rec_buf) continue; - if (jack_ringbuffer_read_space(t->midi_rec_buf) < sizeof(MidiRecEvent)) + if (rt_ringbuffer_read_space(t->midi_rec_buf) < sizeof(MidiRecEvent)) continue; off_t origin = t->rec_start_frame; @@ -2895,8 +2977,8 @@ static gboolean midi_finalize_idle(gpointer data) gint64 last_frame = origin; MidiRecEvent r; - while (jack_ringbuffer_read_space(t->midi_rec_buf) >= sizeof r) { - jack_ringbuffer_read(t->midi_rec_buf, (char *)&r, sizeof r); + while (rt_ringbuffer_read_space(t->midi_rec_buf) >= sizeof r) { + rt_ringbuffer_read(t->midi_rec_buf, (char *)&r, sizeof r); if (r.frame > last_frame) last_frame = r.frame; int st = r.data[0] & 0xF0, ch = r.data[0] & 0x0F, p = r.data[1] & 0x7F; if (st == 0x90 && r.data[2] > 0) { @@ -2956,12 +3038,12 @@ const JackDawRecNote *jackdaw_engine_rec_preview(JackDawTrack *t, guint *count) if (count) *count = 0; if (!t || !t->midi_rec_buf) return NULL; - size_t avail = jack_ringbuffer_read_space(t->midi_rec_buf); + size_t avail = rt_ringbuffer_read_space(t->midi_rec_buf); guint ne = (guint)(avail / sizeof(MidiRecEvent)); if (ne == 0) return NULL; if (ne > ENG_REC_PREVIEW_MAX) ne = ENG_REC_PREVIEW_MAX; - size_t got = jack_ringbuffer_peek(t->midi_rec_buf, (char *)ev, - (size_t)ne * sizeof(MidiRecEvent)); + size_t got = rt_ringbuffer_peek(t->midi_rec_buf, (char *)ev, + (size_t)ne * sizeof(MidiRecEvent)); ne = (guint)(got / sizeof(MidiRecEvent)); off_t now = (off_t)engine.play_pos; @@ -3044,8 +3126,8 @@ void jackdaw_engine_locate(off_t sample) JackDawTrack *t = engine.slots[i]; if (!t) continue; t->played_frames = sample; - if (t->play_buf_L) jack_ringbuffer_reset(t->play_buf_L); - if (t->play_buf_R) jack_ringbuffer_reset(t->play_buf_R); + if (t->play_buf_L) rt_ringbuffer_reset(t->play_buf_L); + if (t->play_buf_R) rt_ringbuffer_reset(t->play_buf_R); /* Tell feeder to re-seek each slot to the new position */ feeder_slots[i].locate_frame = sample; g_atomic_int_set(&feeder_slots[i].locate_req, 1); @@ -3067,8 +3149,8 @@ static void engine_loop_reseek(void) if (!t) continue; if (!playing) { t->played_frames = pos; - if (t->play_buf_L) jack_ringbuffer_reset(t->play_buf_L); - if (t->play_buf_R) jack_ringbuffer_reset(t->play_buf_R); + if (t->play_buf_L) rt_ringbuffer_reset(t->play_buf_L); + if (t->play_buf_R) rt_ringbuffer_reset(t->play_buf_R); } feeder_slots[i].locate_frame = pos; g_atomic_int_set(&feeder_slots[i].locate_req, 1); @@ -3080,15 +3162,26 @@ void jackdaw_engine_set_loop_range(off_t start, off_t end) if (start < 0) start = 0; if (end < 0) end = 0; if (end < start) { off_t tmp = start; start = end; end = tmp; } - engine.loop_start = start; - engine.loop_end = end; + /* Seqlock write (see eng_loop_read): odd, the pair, even. The release + * fence keeps the pair stores from overtaking the odd count; the final + * increment is a full barrier, so the pair is visible before the even. */ + g_atomic_int_inc(&engine.loop_seq); + __atomic_thread_fence(__ATOMIC_RELEASE); + __atomic_store_n(&engine.loop_start, start, __ATOMIC_RELAXED); + __atomic_store_n(&engine.loop_end, end, __ATOMIC_RELAXED); + g_atomic_int_inc(&engine.loop_seq); engine_loop_reseek(); } void jackdaw_engine_get_loop_range(off_t *start, off_t *end) { - if (start) *start = engine.loop_start; - if (end) *end = engine.loop_end; + /* Called off the main thread too (the offline render worker). Never from + * RT, so it may wait out a writer mid-update. */ + EngLoopRange r; + while (eng_loop_read(&r)) + g_thread_yield(); + if (start) *start = r.start; + if (end) *end = r.end; } void jackdaw_engine_set_loop_enabled(gboolean on) @@ -3385,10 +3478,10 @@ void jackdaw_engine_render_tap_start(off_t end_frame) { size_t bytes = (size_t)engine.buf_size * 64 * sizeof(float); if (bytes < 65536) bytes = 65536; - if (!engine.render_rb_L) engine.render_rb_L = jack_ringbuffer_create(bytes); - if (!engine.render_rb_R) engine.render_rb_R = jack_ringbuffer_create(bytes); - if (engine.render_rb_L) jack_ringbuffer_reset(engine.render_rb_L); - if (engine.render_rb_R) jack_ringbuffer_reset(engine.render_rb_R); + if (!engine.render_rb_L) engine.render_rb_L = rt_ringbuffer_create(bytes); + if (!engine.render_rb_R) engine.render_rb_R = rt_ringbuffer_create(bytes); + if (engine.render_rb_L) rt_ringbuffer_reset(engine.render_rb_L); + if (engine.render_rb_R) rt_ringbuffer_reset(engine.render_rb_R); engine.render_end = end_frame; g_atomic_int_set(&engine.render_done, 0); g_atomic_int_set(&engine.render_active, 1); @@ -3408,13 +3501,13 @@ gboolean jackdaw_engine_render_tap_done(void) size_t jackdaw_engine_render_tap_read(float *L, float *R, size_t max_frames) { if (!engine.render_rb_L || !engine.render_rb_R) return 0; - size_t avL = jack_ringbuffer_read_space(engine.render_rb_L) / sizeof(float); - size_t avR = jack_ringbuffer_read_space(engine.render_rb_R) / sizeof(float); + size_t avL = rt_ringbuffer_read_space(engine.render_rb_L) / sizeof(float); + size_t avR = rt_ringbuffer_read_space(engine.render_rb_R) / sizeof(float); size_t av = avL < avR ? avL : avR; if (av > max_frames) av = max_frames; if (av == 0) return 0; - jack_ringbuffer_read(engine.render_rb_L, (char *)L, av * sizeof(float)); - jack_ringbuffer_read(engine.render_rb_R, (char *)R, av * sizeof(float)); + rt_ringbuffer_read(engine.render_rb_L, (char *)L, av * sizeof(float)); + rt_ringbuffer_read(engine.render_rb_R, (char *)R, av * sizeof(float)); return av; } @@ -3480,9 +3573,10 @@ gboolean jackdaw_engine_set_track_stereo(JackDawTrack *t, gboolean stereo) if (!engine.audio_in_r[(guint)ai]) { char name[64]; g_snprintf(name, sizeof(name), "in_%uR", (guint)ai + 1); - engine.audio_in_r[(guint)ai] = jack_port_register(engine.client, + jack_port_t *pr = jack_port_register(engine.client, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0); - if (!engine.audio_in_r[(guint)ai]) return TRUE; + if (!pr) return TRUE; + g_atomic_pointer_set(&engine.audio_in_r[(guint)ai], pr); } } else { /* Clear the right source first so the RT callback stops reading it, @@ -3494,8 +3588,10 @@ gboolean jackdaw_engine_set_track_stereo(JackDawTrack *t, gboolean stereo) g_clear_pointer(&t->audio_src_port_r, g_free); } if (engine.audio_in_r[(guint)ai]) { - jack_port_unregister(engine.client, engine.audio_in_r[(guint)ai]); - engine.audio_in_r[(guint)ai] = NULL; + jack_port_t *pr = engine.audio_in_r[(guint)ai]; + g_atomic_pointer_set(&engine.audio_in_r[(guint)ai], NULL); + eng_wait_rt_past(); + jack_port_unregister(engine.client, pr); } } return FALSE; diff --git a/src/pluginhost.c b/src/pluginhost.c index 1ecd797..8643018 100644 --- a/src/pluginhost.c +++ b/src/pluginhost.c @@ -591,17 +591,23 @@ static void ph_seed_default_paths(void) } pluginhost_add_search_path(PH_LV2, "/usr/lib/lv2"); pluginhost_add_search_path(PH_LV2, "/usr/local/lib/lv2"); - pluginhost_add_search_path(PH_LV2, "/usr/lib/x86_64-linux-gnu/lv2"); +#ifdef PH_MULTIARCH_LIBDIR + pluginhost_add_search_path(PH_LV2, PH_MULTIARCH_LIBDIR "/lv2"); +#endif pluginhost_add_search_path(PH_VST2, "/usr/lib/vst"); pluginhost_add_search_path(PH_VST2, "/usr/local/lib/vst"); pluginhost_add_search_path(PH_VST3, "/usr/lib/vst3"); pluginhost_add_search_path(PH_VST3, "/usr/local/lib/vst3"); - pluginhost_add_search_path(PH_VST3, "/usr/lib/x86_64-linux-gnu/vst3"); +#ifdef PH_MULTIARCH_LIBDIR + pluginhost_add_search_path(PH_VST3, PH_MULTIARCH_LIBDIR "/vst3"); +#endif pluginhost_add_search_path(PH_CLAP, "/usr/lib/clap"); pluginhost_add_search_path(PH_CLAP, "/usr/local/lib/clap"); pluginhost_add_search_path(PH_LADSPA, "/usr/lib/ladspa"); pluginhost_add_search_path(PH_LADSPA, "/usr/local/lib/ladspa"); - pluginhost_add_search_path(PH_LADSPA, "/usr/lib/x86_64-linux-gnu/ladspa"); +#ifdef PH_MULTIARCH_LIBDIR + pluginhost_add_search_path(PH_LADSPA, PH_MULTIARCH_LIBDIR "/ladspa"); +#endif } void pluginhost_load_paths_from_settings(void) @@ -711,8 +717,9 @@ void pluginhost_process_midi(PluginInstance *inst, const PhMidiEvent *ev, if (!inst || !inst->ops) return; if (!g_atomic_int_get(&inst->active)) return; /* bypassed: leave L/R as-is */ - /* Re-arm FTZ/DAZ: the previous plugin (or this one) may have cleared MXCSR, - * which would let denormals stall this plugin's process(). */ + /* Re-arm FTZ/DAZ: the previous plugin (or this one) may have cleared the FP + * control register (MXCSR/FPCR), which would let denormals stall this + * plugin's process(). */ rt_set_denormal_mode(); gint64 _t0 = ph_diag_enabled() ? ph_now_us() : 0; if (inst->ops->process_midi) @@ -765,8 +772,9 @@ void pluginhost_process(PluginInstance *inst, float *L, float *R, int nframes) memcpy(inst->dry_R, R, (size_t)nframes * sizeof(float)); } - /* Re-arm FTZ/DAZ: the previous plugin (or this one) may have cleared MXCSR, - * which would let denormals stall this plugin's process(). */ + /* Re-arm FTZ/DAZ: the previous plugin (or this one) may have cleared the FP + * control register (MXCSR/FPCR), which would let denormals stall this + * plugin's process(). */ rt_set_denormal_mode(); gint64 _t0 = ph_diag_enabled() ? ph_now_us() : 0; inst->ops->process(inst, L, R, nframes); diff --git a/src/pluginhost_clap.c b/src/pluginhost_clap.c index eb0b178..34aa394 100644 --- a/src/pluginhost_clap.c +++ b/src/pluginhost_clap.c @@ -9,6 +9,7 @@ #include "clap/clap.h" #include "pluginhost_internal.h" +#include "rt_ringbuffer.h" #define CLAP_MAX_PENDING 256 @@ -34,9 +35,14 @@ typedef struct { clap_id *param_ids; guint n_params; - /* pending param edits flushed into in_events on the next process */ - clap_event_param_value_t pending[CLAP_MAX_PENDING]; - volatile gint n_pending; + /* Param edits: the main thread queues them on param_q; process() drains + * the queue into events[], which only the RT thread touches, and hands + * that to the plugin as in_events. A shared array with a shared count let + * the RT thread's reset to 0 race an append, replaying edits already + * delivered or dropping new ones. */ + rt_ringbuffer_t *param_q; /* main -> RT */ + clap_event_param_value_t events[CLAP_MAX_PENDING]; + guint n_events; /* RT only */ float *outL, *outR; int max_block; @@ -45,11 +51,11 @@ typedef struct { /* ---- Input/output event adapters ---- */ static uint32_t clap_in_size(const struct clap_input_events *l) -{ ClapBackend *b = (ClapBackend *)l->ctx; return (uint32_t)g_atomic_int_get(&b->n_pending); } +{ ClapBackend *b = (ClapBackend *)l->ctx; return b->n_events; } static const clap_event_header_t *clap_in_get(const struct clap_input_events *l, uint32_t i) { ClapBackend *b = (ClapBackend *)l->ctx; - return (i < (uint32_t)b->n_pending) ? &b->pending[i].header : NULL; } + return (i < b->n_events) ? &b->events[i].header : NULL; } static bool clap_out_push(const struct clap_output_events *l, const clap_event_header_t *e) @@ -137,6 +143,12 @@ static void clap_process_cb(PluginInstance *pi, float *L, float *R, int n) clap_audio_buffer_t in = { in_ch, NULL, 2, 0, 0 }; clap_audio_buffer_t out = { out_ch, NULL, 2, 0, 0 }; + b->n_events = 0; + while (b->param_q && b->n_events < CLAP_MAX_PENDING && + rt_ringbuffer_read(b->param_q, (char *)&b->events[b->n_events], + sizeof b->events[0]) == sizeof b->events[0]) + b->n_events++; + clap_input_events_t ine = { b, clap_in_size, clap_in_get }; clap_output_events_t oute = { b, clap_out_push }; @@ -154,7 +166,6 @@ static void clap_process_cb(PluginInstance *pi, float *L, float *R, int n) if (b->plugin && b->plugin->process) b->plugin->process(b->plugin, &pr); - g_atomic_int_set(&b->n_pending, 0); /* events consumed */ memcpy(L, b->outL, (size_t)n * sizeof(float)); memcpy(R, b->outR, (size_t)n * sizeof(float)); @@ -180,6 +191,7 @@ static void clap_destroy(PluginInstance *pi) if (b->dl) dlclose(b->dl); g_free(b->param_ids); g_free(b->outL); g_free(b->outR); + rt_ringbuffer_free(b->param_q); g_free(b); } @@ -228,22 +240,21 @@ static gboolean clap_param_display(PluginInstance *pi, guint i, char *buf, gsize static void clap_param_set(PluginInstance *pi, guint i, float v) { ClapBackend *b = (ClapBackend *)pi->backend; - if (i >= b->n_params) return; - gint slot = g_atomic_int_get(&b->n_pending); - if (slot >= CLAP_MAX_PENDING) return; - clap_event_param_value_t *ev = &b->pending[slot]; - memset(ev, 0, sizeof(*ev)); - ev->header.size = sizeof(*ev); - ev->header.time = 0; - ev->header.space_id = CLAP_CORE_EVENT_SPACE_ID; - ev->header.type = CLAP_EVENT_PARAM_VALUE; - ev->param_id = b->param_ids[i]; - ev->note_id = -1; - ev->port_index= -1; - ev->channel = -1; - ev->key = -1; - ev->value = v; - g_atomic_int_set(&b->n_pending, slot + 1); + if (i >= b->n_params || !b->param_q) return; + clap_event_param_value_t ev; + if (rt_ringbuffer_write_space(b->param_q) < sizeof ev) return; + memset(&ev, 0, sizeof(ev)); + ev.header.size = sizeof(ev); + ev.header.time = 0; + ev.header.space_id = CLAP_CORE_EVENT_SPACE_ID; + ev.header.type = CLAP_EVENT_PARAM_VALUE; + ev.param_id = b->param_ids[i]; + ev.note_id = -1; + ev.port_index= -1; + ev.channel = -1; + ev.key = -1; + ev.value = v; + rt_ringbuffer_write(b->param_q, (const char *)&ev, sizeof ev); } static void clap_param_range(PluginInstance *pi, guint i, float *mn, float *mx) @@ -361,6 +372,9 @@ PluginInstance *ph_clap_instantiate(const PluginInfo *info, double sr, int max_b b->max_block = max_block; b->outL = g_new0(float, max_block); b->outR = g_new0(float, max_block); + b->param_q = rt_ringbuffer_create(CLAP_MAX_PENDING * + sizeof(clap_event_param_value_t)); + if (b->param_q) rt_ringbuffer_mlock(b->param_q); b->state = (const clap_plugin_state_t *) plugin->get_extension(plugin, CLAP_EXT_STATE); diff --git a/src/pluginhost_internal.h b/src/pluginhost_internal.h index c20334e..484e01d 100644 --- a/src/pluginhost_internal.h +++ b/src/pluginhost_internal.h @@ -5,6 +5,16 @@ G_BEGIN_DECLS +/* Debian-style multiarch library directory for the architecture this binary + * was built for. Distro plug-ins live under it (/usr/lib//lv2 etc.), + * and it used to be hard-coded to x86_64, so an aarch64 build never found + * them. Left undefined on anything else; callers skip the path. */ +#if defined(__x86_64__) +# define PH_MULTIARCH_LIBDIR "/usr/lib/x86_64-linux-gnu" +#elif defined(__aarch64__) +# define PH_MULTIARCH_LIBDIR "/usr/lib/aarch64-linux-gnu" +#endif + /* Backend vtable. Every backend fills one of these on a PluginInstance. */ typedef struct { void (*process) (PluginInstance *, float *L, float *R, int n); diff --git a/src/pluginhost_lv2.c b/src/pluginhost_lv2.c index 2487029..a09ffe1 100644 --- a/src/pluginhost_lv2.c +++ b/src/pluginhost_lv2.c @@ -19,7 +19,7 @@ #include #include #include -#include +#include "rt_ringbuffer.h" #include #include #include @@ -82,8 +82,12 @@ static void lv2_apply_search_path(const GList *extra) if (p->len) g_string_append_c(p, ':'); g_string_append(p, env); } - if (p->len == 0) - g_string_append(p, "/usr/lib/lv2:/usr/lib/x86_64-linux-gnu/lv2"); + if (p->len == 0) { + g_string_append(p, "/usr/lib/lv2"); +#ifdef PH_MULTIARCH_LIBDIR + g_string_append(p, ":" PH_MULTIARCH_LIBDIR "/lv2"); +#endif + } LilvNode *node = lilv_new_string(world, p->str); lilv_world_set_option(world, LILV_OPTION_LV2_PATH, node); lilv_node_free(node); @@ -166,8 +170,8 @@ static LV2_Feature feat_log = { LV2_LOG__log, &lv2_log }; typedef struct { const LV2_Worker_Interface *iface; LV2_Handle plugin; /* lilv instance handle */ - jack_ringbuffer_t *requests; /* RT -> worker */ - jack_ringbuffer_t *responses; /* worker -> RT */ + rt_ringbuffer_t *requests; /* RT -> worker */ + rt_ringbuffer_t *responses; /* worker -> RT */ sem_t sem; GThread *thread; volatile gint quit; @@ -180,13 +184,29 @@ static LV2_Worker_Status worker_respond_cb(LV2_Worker_Respond_Handle h, uint32_t size, const void *data) { Worker *w = h; - if (jack_ringbuffer_write_space(w->responses) < sizeof(size) + size) + if (rt_ringbuffer_write_space(w->responses) < sizeof(size) + size) return LV2_WORKER_ERR_NO_SPACE; - jack_ringbuffer_write(w->responses, (const char *)&size, sizeof(size)); - jack_ringbuffer_write(w->responses, (const char *)data, size); + rt_ringbuffer_write(w->responses, (const char *)&size, sizeof(size)); + rt_ringbuffer_write(w->responses, (const char *)data, size); return LV2_WORKER_SUCCESS; } +/* Take the next record's size header off a worker ring, but only once its + * whole payload is in: the producer writes header and payload as two separate + * writes, so the header alone does not mean the record is complete. Consuming + * it early would hand work()/work_response() a short payload and desync every + * record after it. An incomplete record is left for the next wake-up (worker) + * or the next cycle (RT). RT-safe. */ +static gboolean worker_ring_take(rt_ringbuffer_t *rb, uint32_t *size) +{ + size_t avail = rt_ringbuffer_read_space(rb); + if (avail < sizeof *size) return FALSE; + rt_ringbuffer_peek(rb, (char *)size, sizeof *size); + if (avail - sizeof *size < *size) return FALSE; + rt_ringbuffer_read_advance(rb, sizeof *size); + return TRUE; +} + static gpointer worker_thread_fn(gpointer arg) { Worker *w = arg; @@ -195,13 +215,12 @@ static gpointer worker_thread_fn(gpointer arg) sem_wait(&w->sem); if (g_atomic_int_get(&w->quit)) break; uint32_t size; - while (jack_ringbuffer_read_space(w->requests) >= sizeof(size)) { - jack_ringbuffer_read(w->requests, (char *)&size, sizeof(size)); + while (worker_ring_take(w->requests, &size)) { if (size > sizeof(buf)) { /* oversized: drain & drop */ - jack_ringbuffer_read_advance(w->requests, size); + rt_ringbuffer_read_advance(w->requests, size); continue; } - jack_ringbuffer_read(w->requests, buf, size); + rt_ringbuffer_read(w->requests, buf, size); if (w->iface && w->iface->work) w->iface->work(w->plugin, worker_respond_cb, w, size, buf); } @@ -215,10 +234,10 @@ static LV2_Worker_Status worker_schedule_cb(LV2_Worker_Schedule_Handle h, { Worker *w = h; if (!w->requests) return LV2_WORKER_ERR_UNKNOWN; /* not yet wired */ - if (jack_ringbuffer_write_space(w->requests) < sizeof(size) + size) + if (rt_ringbuffer_write_space(w->requests) < sizeof(size) + size) return LV2_WORKER_ERR_NO_SPACE; - jack_ringbuffer_write(w->requests, (const char *)&size, sizeof(size)); - jack_ringbuffer_write(w->requests, (const char *)data, size); + rt_ringbuffer_write(w->requests, (const char *)&size, sizeof(size)); + rt_ringbuffer_write(w->requests, (const char *)data, size); sem_post(&w->sem); return LV2_WORKER_SUCCESS; } @@ -229,10 +248,9 @@ static void worker_apply_responses(Worker *w) if (!w->active || !w->iface) return; char buf[WORKER_BUF_BYTES]; uint32_t size; - while (jack_ringbuffer_read_space(w->responses) >= sizeof(size)) { - jack_ringbuffer_read(w->responses, (char *)&size, sizeof(size)); - if (size > sizeof(buf)) { jack_ringbuffer_read_advance(w->responses, size); continue; } - jack_ringbuffer_read(w->responses, buf, size); + while (worker_ring_take(w->responses, &size)) { + if (size > sizeof(buf)) { rt_ringbuffer_read_advance(w->responses, size); continue; } + rt_ringbuffer_read(w->responses, buf, size); if (w->iface->work_response) w->iface->work_response(w->plugin, size, buf); } @@ -246,8 +264,8 @@ static void worker_init(Worker *w, LilvInstance *inst) if (!iface) { w->active = FALSE; return; } w->iface = iface; w->plugin = lilv_instance_get_handle(inst); - w->requests = jack_ringbuffer_create(WORKER_BUF_BYTES * 4); - w->responses = jack_ringbuffer_create(WORKER_BUF_BYTES * 4); + w->requests = rt_ringbuffer_create(WORKER_BUF_BYTES * 4); + w->responses = rt_ringbuffer_create(WORKER_BUF_BYTES * 4); sem_init(&w->sem, 0, 0); w->quit = 0; w->thread = g_thread_new("lv2-worker", worker_thread_fn, w); @@ -261,8 +279,8 @@ static void worker_destroy(Worker *w) sem_post(&w->sem); if (w->thread) g_thread_join(w->thread); sem_destroy(&w->sem); - if (w->requests) jack_ringbuffer_free(w->requests); - if (w->responses) jack_ringbuffer_free(w->responses); + if (w->requests) rt_ringbuffer_free(w->requests); + if (w->responses) rt_ringbuffer_free(w->responses); w->active = FALSE; } @@ -337,8 +355,8 @@ typedef struct { * completely mute. Both rings are lock-free: the RT thread drains * atoms_to_dsp into the input sequence before run() and copies whatever the * plugin wrote on its atom OUTPUT ports into atoms_to_ui after it. */ - jack_ringbuffer_t *atoms_to_dsp; /* UI thread -> RT */ - jack_ringbuffer_t *atoms_to_ui; /* RT -> UI thread */ + rt_ringbuffer_t *atoms_to_dsp; /* UI thread -> RT */ + rt_ringbuffer_t *atoms_to_ui; /* RT -> UI thread */ void *atom_scratch; /* RT read buffer, sized to the ring */ guint32 atom_scratch_sz; LV2_URID urid_ev_transfer; @@ -429,14 +447,14 @@ static guint32 lv2_stage_ui_atoms(Lv2Backend *b) guint32 used = 0; for (;;) { Lv2AtomRec rec; - if (jack_ringbuffer_read_space(b->atoms_to_dsp) < sizeof rec) break; - jack_ringbuffer_peek(b->atoms_to_dsp, (char *)&rec, sizeof rec); - if (jack_ringbuffer_read_space(b->atoms_to_dsp) < sizeof rec + rec.size) break; + if (rt_ringbuffer_read_space(b->atoms_to_dsp) < sizeof rec) break; + rt_ringbuffer_peek(b->atoms_to_dsp, (char *)&rec, sizeof rec); + if (rt_ringbuffer_read_space(b->atoms_to_dsp) < sizeof rec + rec.size) break; if (used + sizeof rec + rec.size > b->atom_scratch_sz) break; /* next cycle */ - jack_ringbuffer_read_advance(b->atoms_to_dsp, sizeof rec); + rt_ringbuffer_read_advance(b->atoms_to_dsp, sizeof rec); memcpy((char *)b->atom_scratch + used, &rec, sizeof rec); - jack_ringbuffer_read(b->atoms_to_dsp, - (char *)b->atom_scratch + used + sizeof rec, rec.size); + rt_ringbuffer_read(b->atoms_to_dsp, + (char *)b->atom_scratch + used + sizeof rec, rec.size); used += sizeof rec + rec.size; } return used; @@ -496,10 +514,10 @@ static void lv2_collect_atom_out(Lv2Backend *b) LV2_ATOM_SEQUENCE_FOREACH(seq, evt) { const LV2_Atom *atom = &evt->body; Lv2AtomRec rec = { b->atoms[a].index, (guint32)lv2_atom_total_size(atom) }; - if (jack_ringbuffer_write_space(b->atoms_to_ui) < sizeof rec + rec.size) + if (rt_ringbuffer_write_space(b->atoms_to_ui) < sizeof rec + rec.size) return; /* editor is behind; drop the rest of this block */ - jack_ringbuffer_write(b->atoms_to_ui, (const char *)&rec, sizeof rec); - jack_ringbuffer_write(b->atoms_to_ui, (const char *)atom, rec.size); + rt_ringbuffer_write(b->atoms_to_ui, (const char *)&rec, sizeof rec); + rt_ringbuffer_write(b->atoms_to_ui, (const char *)atom, rec.size); } } } @@ -615,8 +633,8 @@ static void lv2_destroy(PluginInstance *pi) g_free(b->misc); for (guint a = 0; a < b->n_atoms; a++) { g_free(b->atoms[a].buf[0]); g_free(b->atoms[a].buf[1]); } g_free(b->atoms); - if (b->atoms_to_dsp) jack_ringbuffer_free(b->atoms_to_dsp); - if (b->atoms_to_ui) jack_ringbuffer_free(b->atoms_to_ui); + if (b->atoms_to_dsp) rt_ringbuffer_free(b->atoms_to_dsp); + if (b->atoms_to_ui) rt_ringbuffer_free(b->atoms_to_ui); g_free(b->atom_scratch); g_free(b->uri); g_free(b->ui_uri); g_free(b->ui_type); g_free(b); @@ -700,10 +718,10 @@ void ph_lv2_atom_ui_open(PluginInstance *pi, gboolean open) if (open && b->atoms_to_ui) { /* Drop anything queued while no editor was listening — it is stale by * the time one opens. Draining from the READ side is this thread's own - * half of the ring; jack_ringbuffer_reset touches both pointers and + * half of the ring; rt_ringbuffer_reset touches both pointers and * would race the RT thread if it were mid-write. */ - jack_ringbuffer_read_advance(b->atoms_to_ui, - jack_ringbuffer_read_space(b->atoms_to_ui)); + rt_ringbuffer_read_advance(b->atoms_to_ui, + rt_ringbuffer_read_space(b->atoms_to_ui)); } g_atomic_int_set(&b->ui_open, open ? 1 : 0); } @@ -718,10 +736,10 @@ gboolean ph_lv2_atom_to_dsp(PluginInstance *pi, guint32 port, if (b->midi_in_atom < 0 || b->atoms[b->midi_in_atom].index != port) return FALSE; Lv2AtomRec rec = { port, size }; - if (jack_ringbuffer_write_space(b->atoms_to_dsp) < sizeof rec + size) + if (rt_ringbuffer_write_space(b->atoms_to_dsp) < sizeof rec + size) return FALSE; - jack_ringbuffer_write(b->atoms_to_dsp, (const char *)&rec, sizeof rec); - jack_ringbuffer_write(b->atoms_to_dsp, (const char *)atom, size); + rt_ringbuffer_write(b->atoms_to_dsp, (const char *)&rec, sizeof rec); + rt_ringbuffer_write(b->atoms_to_dsp, (const char *)atom, size); return TRUE; } @@ -732,16 +750,16 @@ gboolean ph_lv2_atom_from_dsp(PluginInstance *pi, guint32 *port, if (!b || !b->atoms_to_ui) return FALSE; Lv2AtomRec rec; - if (jack_ringbuffer_read_space(b->atoms_to_ui) < sizeof rec) return FALSE; - jack_ringbuffer_peek(b->atoms_to_ui, (char *)&rec, sizeof rec); - if (jack_ringbuffer_read_space(b->atoms_to_ui) < sizeof rec + rec.size) + if (rt_ringbuffer_read_space(b->atoms_to_ui) < sizeof rec) return FALSE; + rt_ringbuffer_peek(b->atoms_to_ui, (char *)&rec, sizeof rec); + if (rt_ringbuffer_read_space(b->atoms_to_ui) < sizeof rec + rec.size) return FALSE; /* still being written; try again */ - jack_ringbuffer_read_advance(b->atoms_to_ui, sizeof rec); + rt_ringbuffer_read_advance(b->atoms_to_ui, sizeof rec); if (rec.size > bufsz) { /* cannot deliver it: drop it */ - jack_ringbuffer_read_advance(b->atoms_to_ui, rec.size); + rt_ringbuffer_read_advance(b->atoms_to_ui, rec.size); return FALSE; } - jack_ringbuffer_read(b->atoms_to_ui, (char *)buf, rec.size); + rt_ringbuffer_read(b->atoms_to_ui, (char *)buf, rec.size); *port = rec.port; *size = rec.size; return TRUE; @@ -1270,12 +1288,12 @@ PluginInstance *ph_lv2_instantiate(const PluginInfo *info, double sr, int max_bl /* Editor <-> DSP atom rings. Sized for the largest thing that travels on * them (a state blob or a capture list, not a MIDI note), so a big message * is delivered rather than dropped. */ - b->atoms_to_dsp = jack_ringbuffer_create(LV2_ATOM_RING_BYTES); - b->atoms_to_ui = jack_ringbuffer_create(LV2_ATOM_RING_BYTES); + b->atoms_to_dsp = rt_ringbuffer_create(LV2_ATOM_RING_BYTES); + b->atoms_to_ui = rt_ringbuffer_create(LV2_ATOM_RING_BYTES); b->atom_scratch_sz = LV2_ATOM_RING_BYTES; b->atom_scratch = g_malloc0(b->atom_scratch_sz); - if (b->atoms_to_dsp) jack_ringbuffer_mlock(b->atoms_to_dsp); - if (b->atoms_to_ui) jack_ringbuffer_mlock(b->atoms_to_ui); + if (b->atoms_to_dsp) rt_ringbuffer_mlock(b->atoms_to_dsp); + if (b->atoms_to_ui) rt_ringbuffer_mlock(b->atoms_to_ui); /* The instrument's MIDI sink = the first atom INPUT port. */ b->midi_in_atom = -1; for (guint a = 0; a < b->n_atoms; a++) diff --git a/src/rt_denormal.h b/src/rt_denormal.h index bad7deb..c143735 100644 --- a/src/rt_denormal.h +++ b/src/rt_denormal.h @@ -2,11 +2,22 @@ * * JACK does not set FTZ/DAZ on a client's process thread, so plugins whose * filter/reverb/feedback paths produce subnormal floats stall the CPU 10-100x - * and blow the RT deadline. Worse, a plugin may clear the MXCSR control word + * and blow the RT deadline. Worse, a plugin may clear the FP control register * inside its own process() call, leaving the REST of the chain exposed — so we * re-arm before every plugin, not just once per cycle (matching Reaper/Ardour). * - * Cheap: two MXCSR register writes. Safe to include from C and C++. */ + * x86-64 needs two MXCSR bits: FTZ flushes subnormal results, DAZ subnormal + * inputs. AArch64 needs one: FPCR bit 24 (FZ) does both, for scalar and + * Advanced SIMD alike, and there is no separate DAZ control. The bit position + * is the FPCR layout in glibc's aarch64 . The register is + * accessed with mrs/msr directly rather than through that header so the code + * does not depend on glibc. + * + * Before the aarch64 branch existed this compiled to an empty function on ARM, + * silently leaving every RT thread unprotected there. + * + * Cheap: two register writes on x86; on aarch64 one read, and a write only when + * FZ is found clear. Safe to include from C and C++. */ #ifndef JACKDAW_RT_DENORMAL_H #define JACKDAW_RT_DENORMAL_H @@ -14,13 +25,25 @@ # include # include # define JACKDAW_HAVE_SSE_DENORMAL 1 +#elif defined(__aarch64__) +# define JACKDAW_HAVE_AARCH64_DENORMAL 1 #endif static inline void rt_set_denormal_mode(void) { -#ifdef JACKDAW_HAVE_SSE_DENORMAL +#if defined(JACKDAW_HAVE_SSE_DENORMAL) _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON); +#elif defined(JACKDAW_HAVE_AARCH64_DENORMAL) + /* Read-modify-write: every other FPCR bit (the rounding mode above all) + * belongs to someone else, and a blind write would reset it. The write is + * skipped when FZ is already set — the common case, since this runs before + * every plugin and an FPCR write can serialise the pipeline on some cores. + * unsigned long is 64-bit on aarch64 Linux, as mrs/msr need an X register. */ + unsigned long fpcr; + __asm__ __volatile__("mrs %0, fpcr" : "=r"(fpcr)); + if (!(fpcr & (1UL << 24))) + __asm__ __volatile__("msr fpcr, %0" : : "r"(fpcr | (1UL << 24))); #endif } diff --git a/src/rt_ringbuffer.c b/src/rt_ringbuffer.c new file mode 100644 index 0000000..85020be --- /dev/null +++ b/src/rt_ringbuffer.c @@ -0,0 +1,155 @@ +/* rt_ringbuffer.c — see rt_ringbuffer.h for why this exists and its ordering. + * + * Indices are byte offsets in [0, size). read_ptr == write_ptr means empty; + * the writer stops one byte short of the reader, so full is never ambiguous + * with empty. That is the jack_ringbuffer rule, kept so buffers sized for it + * hold exactly what they held before. */ +#include +#include +#include + +#include "rt_ringbuffer.h" + +struct rt_ringbuffer { + char *buf; + size_t write_ptr; /* stored only by the producer */ + size_t read_ptr; /* stored only by the consumer */ + size_t size; /* power of two */ + size_t size_mask; + int mlocked; +}; + +/* The other side's index: acquire, so its copy is visible before we act on + * the space it reports. */ +static inline size_t rb_load_acq(const size_t *p) +{ + return __atomic_load_n(p, __ATOMIC_ACQUIRE); +} + +/* Our own index, after our copy: release, so the copy is visible before the + * other side can see the index move. */ +static inline void rb_store_rel(size_t *p, size_t v) +{ + __atomic_store_n(p, v, __ATOMIC_RELEASE); +} + +rt_ringbuffer_t *rt_ringbuffer_create(size_t sz) +{ + rt_ringbuffer_t *rb = malloc(sizeof *rb); + if (!rb) + return NULL; + + size_t size = 2; + while (size < sz) { + if (size > ((size_t)-1 >> 1)) { free(rb); return NULL; } + size <<= 1; + } + + rb->buf = malloc(size); + if (!rb->buf) { free(rb); return NULL; } + rb->size = size; + rb->size_mask = size - 1; + rb->write_ptr = 0; + rb->read_ptr = 0; + rb->mlocked = 0; + return rb; +} + +void rt_ringbuffer_free(rt_ringbuffer_t *rb) +{ + if (!rb) + return; + if (rb->mlocked) + munlock(rb->buf, rb->size); + free(rb->buf); + free(rb); +} + +/* 0 on success, -1 if mlock failed (typically RLIMIT_MEMLOCK); the ring works + * either way, it just may page. */ +int rt_ringbuffer_mlock(rt_ringbuffer_t *rb) +{ + if (mlock(rb->buf, rb->size)) + return -1; + rb->mlocked = 1; + return 0; +} + +/* Not thread safe: only while neither side can be touching the ring. */ +void rt_ringbuffer_reset(rt_ringbuffer_t *rb) +{ + rb_store_rel(&rb->read_ptr, 0); + rb_store_rel(&rb->write_ptr, 0); +} + +size_t rt_ringbuffer_read_space(const rt_ringbuffer_t *rb) +{ + size_t w = rb_load_acq(&rb->write_ptr); + size_t r = rb_load_acq(&rb->read_ptr); + return (w - r) & rb->size_mask; +} + +size_t rt_ringbuffer_write_space(const rt_ringbuffer_t *rb) +{ + size_t w = rb_load_acq(&rb->write_ptr); + size_t r = rb_load_acq(&rb->read_ptr); + return (r - w - 1) & rb->size_mask; +} + +/* Copy up to cnt bytes out from the read index without moving it. */ +static size_t rb_copy_out(const rt_ringbuffer_t *rb, char *dest, size_t cnt) +{ + size_t avail = rt_ringbuffer_read_space(rb); + size_t n = cnt < avail ? cnt : avail; + if (n == 0) + return 0; + + size_t r = rb->read_ptr; + size_t n1 = rb->size - r; + if (n1 > n) + n1 = n; + memcpy(dest, rb->buf + r, n1); + if (n > n1) + memcpy(dest + n1, rb->buf, n - n1); + return n; +} + +size_t rt_ringbuffer_peek(rt_ringbuffer_t *rb, char *dest, size_t cnt) +{ + return rb_copy_out(rb, dest, cnt); +} + +size_t rt_ringbuffer_read(rt_ringbuffer_t *rb, char *dest, size_t cnt) +{ + size_t n = rb_copy_out(rb, dest, cnt); + if (n) + rb_store_rel(&rb->read_ptr, (rb->read_ptr + n) & rb->size_mask); + return n; +} + +/* Discard up to cnt readable bytes. Clamped to what is readable, so a caller + * that skips a malformed record cannot move the reader past the writer. */ +void rt_ringbuffer_read_advance(rt_ringbuffer_t *rb, size_t cnt) +{ + size_t avail = rt_ringbuffer_read_space(rb); + size_t n = cnt < avail ? cnt : avail; + rb_store_rel(&rb->read_ptr, (rb->read_ptr + n) & rb->size_mask); +} + +size_t rt_ringbuffer_write(rt_ringbuffer_t *rb, const char *src, size_t cnt) +{ + size_t space = rt_ringbuffer_write_space(rb); + size_t n = cnt < space ? cnt : space; + if (n == 0) + return 0; + + size_t w = rb->write_ptr; + size_t n1 = rb->size - w; + if (n1 > n) + n1 = n; + memcpy(rb->buf + w, src, n1); + if (n > n1) + memcpy(rb->buf, src + n1, n - n1); + rb_store_rel(&rb->write_ptr, (w + n) & rb->size_mask); + return n; +} diff --git a/src/rt_ringbuffer.h b/src/rt_ringbuffer.h new file mode 100644 index 0000000..8ccf15d --- /dev/null +++ b/src/rt_ringbuffer.h @@ -0,0 +1,54 @@ +/* rt_ringbuffer.h — lock-free single-producer / single-consumer byte ring. + * + * A drop-in for the subset of JackDAW uses: same calls, + * same arguments, same capacity rule (the size is rounded up to a power of + * two and one byte of it is never filled), same "reset is not thread safe". + * One deliberate difference: read_advance is clamped to what is readable, so + * skipping a record can never move the reader past the writer. + * + * WHY NOT jack_ringbuffer_*. Those live in whatever libjack the user runs, and + * two of the libjacks in the field do not order the data copy against the + * index update: jack2 before 1.9.22 (Debian 12 / Raspberry Pi OS bookworm ship + * 1.9.21) and PipeWire's pipewire-jack (plain volatile indices, no barrier). + * On x86 that is harmless, because stores become visible in program order. On + * a weakly ordered CPU (aarch64) the consumer can see the advanced write index + * before the bytes behind it land, and read stale audio, MIDI or atom data. + * Owning the ring makes the ordering a property of JackDAW, not of the host's + * libjack. + * + * Ordering: each index is written only by its own side, with a release store + * after the copy; the other side reads it with an acquire load before touching + * the bytes. That covers both hazards — reading bytes not yet written, and + * overwriting bytes not yet read. + * + * RT-safe: read, peek, read_advance, read_space, write and write_space do no + * allocation, locking or system calls. create, free and mlock are not RT. */ +#ifndef JACKDAW_RT_RINGBUFFER_H +#define JACKDAW_RT_RINGBUFFER_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct rt_ringbuffer rt_ringbuffer_t; + +rt_ringbuffer_t *rt_ringbuffer_create(size_t sz); +void rt_ringbuffer_free(rt_ringbuffer_t *rb); +int rt_ringbuffer_mlock(rt_ringbuffer_t *rb); +void rt_ringbuffer_reset(rt_ringbuffer_t *rb); + +size_t rt_ringbuffer_read_space(const rt_ringbuffer_t *rb); +size_t rt_ringbuffer_write_space(const rt_ringbuffer_t *rb); + +size_t rt_ringbuffer_read(rt_ringbuffer_t *rb, char *dest, size_t cnt); +size_t rt_ringbuffer_peek(rt_ringbuffer_t *rb, char *dest, size_t cnt); +void rt_ringbuffer_read_advance(rt_ringbuffer_t *rb, size_t cnt); +size_t rt_ringbuffer_write(rt_ringbuffer_t *rb, const char *src, size_t cnt); + +#ifdef __cplusplus +} +#endif + +#endif /* JACKDAW_RT_RINGBUFFER_H */ diff --git a/src/timeline.c b/src/timeline.c index 948fd38..f9ee91c 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -607,7 +607,7 @@ static gboolean wave_view_draw(GtkWidget *widget, cairo_t *cr) } } else { /* Live waveform — read peak buffer written by the RT callback */ - gint pk_count = rec_t->rec_peak_count; /* read once: RT may still write */ + gint pk_count = g_atomic_int_get(&rec_t->rec_peak_count); /* acquire: pairs below it are written */ gfloat *pk_buf = rec_t->rec_peak_buf; guint blk = rec_t->rec_peak_block; diff --git a/src/track.c b/src/track.c index 6a69e4e..0fd94f5 100644 --- a/src/track.c +++ b/src/track.c @@ -63,11 +63,11 @@ static void jackdaw_track_finalize(GObject *obj) g_ptr_array_free(t->fx_list, TRUE); } - if (t->play_buf_L) jack_ringbuffer_free(t->play_buf_L); - if (t->play_buf_R) jack_ringbuffer_free(t->play_buf_R); - if (t->rec_buf_L) jack_ringbuffer_free(t->rec_buf_L); - if (t->rec_buf_R) jack_ringbuffer_free(t->rec_buf_R); - if (t->midi_rec_buf) jack_ringbuffer_free(t->midi_rec_buf); + if (t->play_buf_L) rt_ringbuffer_free(t->play_buf_L); + if (t->play_buf_R) rt_ringbuffer_free(t->play_buf_R); + if (t->rec_buf_L) rt_ringbuffer_free(t->rec_buf_L); + if (t->rec_buf_R) rt_ringbuffer_free(t->rec_buf_R); + if (t->midi_rec_buf) rt_ringbuffer_free(t->midi_rec_buf); G_OBJECT_CLASS(jackdaw_track_parent_class)->finalize(obj); } diff --git a/src/track.h b/src/track.h index d1955ac..db7f7bc 100644 --- a/src/track.h +++ b/src/track.h @@ -2,7 +2,7 @@ #define TRACK_H_INCLUDED #include -#include +#include "rt_ringbuffer.h" #include "audio_clip.h" #include "clipregion.h" #include "midiclip.h" @@ -78,13 +78,13 @@ struct _JackDawTrack { volatile gfloat peak_R; /* Playback ringbuffers: fed by main thread, drained by RT callback */ - jack_ringbuffer_t *play_buf_L; - jack_ringbuffer_t *play_buf_R; + rt_ringbuffer_t *play_buf_L; + rt_ringbuffer_t *play_buf_R; /* Record ringbuffers: filled by RT callback, drained by main thread */ - jack_ringbuffer_t *rec_buf_L; - jack_ringbuffer_t *rec_buf_R; - jack_ringbuffer_t *midi_rec_buf; + rt_ringbuffer_t *rec_buf_L; + rt_ringbuffer_t *rec_buf_R; + rt_ringbuffer_t *midi_rec_buf; /* Playback position in frames within clip */ volatile off_t played_frames;