Skip to content
Merged
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
4 changes: 2 additions & 2 deletions .github/release/cli.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- The CLI loads Homebrew's libjack, which we do not sign. -->
<key>com.apple.security.cs.disable-library-validation</key>
<!-- Under the hardened runtime, reading a microphone needs this. The CLI loads no library we do not sign. -->
<key>com.apple.security.device.audio-input</key>
<true/>
</dict>
</plist>
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,27 @@ jobs:
make test CXX=clang++
- name: The published tuner.wasm reads the test tones
run: node hosts/wasm/test/core.test.mjs docs/tuner.wasm

# The CoreAudio host only builds on a Mac, so build it on every push and pull request: a break there would
# otherwise show up first in a release. Only system libraries may be linked. No audio input is used.
macos:
runs-on: macos-15
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Build the live tuner and tone on CoreAudio
run: make live CXX=clang++
- name: Only system libraries are linked
run: |
set -euo pipefail
for b in build/squatch-tuner-live build/squatch-tone; do
otool -L "$b"
bad=$(otool -L "$b" | tail -n +2 | awk '{print $1}' | grep -Ev '^(/System/|/usr/lib/)' || true)
if [ -n "$bad" ]; then echo "::error::$b links a library that is not part of macOS: $bad"; exit 1; fi
done
- name: Lists the audio devices
run: |
set -euo pipefail
build/squatch-tuner-live --list-devices
build/squatch-tone --list-devices
17 changes: 10 additions & 7 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,16 @@ jobs:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: JACK (Homebrew; the CLI needs it installed to run)
run: brew install jack
- name: Build for Apple silicon, build paths mapped out (ad-hoc signed by the linker)
- name: Build for Apple silicon on CoreAudio, build paths mapped out (ad-hoc signed by the linker)
run: |
set -euo pipefail
jp=$(brew --prefix jack)
make live CXX=clang++ CXXFLAGS="-std=c++17 -O3 -Wall -Wextra -Wpedantic -Wshadow -Iinclude -I$jp/include -L$jp/lib -ffile-prefix-map=$PWD=."
make live CXX=clang++ CXXFLAGS="-std=c++17 -O3 -Wall -Wextra -Wpedantic -Wshadow -Iinclude -ffile-prefix-map=$PWD=."
# The macOS CLI needs nothing installed: only system libraries may be linked.
for b in build/squatch-tuner-live build/squatch-tone; do
otool -L "$b"
bad=$(otool -L "$b" | tail -n +2 | awk '{print $1}' | grep -Ev '^(/System/|/usr/lib/)' || true)
if [ -n "$bad" ]; then echo "::error::$b links a library that is not part of macOS: $bad"; exit 1; fi
done
python3 tools/release-scan build/squatch-tuner-live build/squatch-tone
lipo -archs build/squatch-tuner-live
codesign --verify --strict build/squatch-tuner-live
Expand Down Expand Up @@ -171,7 +174,7 @@ jobs:
run: |
set -euo pipefail
mkdir stage && ditto -x -k cli-macos-unsigned.zip stage
# Homebrew's libjack is not signed by us, so the hardened runtime must allow loading it.
# The hardened runtime needs the audio-input entitlement for the tuner to hear a microphone.
tools/release-signing sign --entitlements .github/release/cli.entitlements stage/squatch-tuner-live stage/squatch-tone
mkdir out
ditto -c -k stage "out/squatch-tuner-live-$TAG-macos-arm64.zip"
Expand Down Expand Up @@ -231,7 +234,7 @@ jobs:

- \`squatch-tuner-$TAG-source.tar.gz\`: the source.
- \`squatch-tuner-web-$TAG.zip\`: the browser demo, the same files as the published demo page.
- \`squatch-tuner-live-$TAG-linux-x86_64.tar.gz\`, \`-linux-arm64.tar.gz\`, \`squatch-tuner-live-$TAG-macos-arm64*.zip\`: the headless tuner behind a JACK input, with \`squatch-tone\`, an exact test tone. They need a running JACK server.
- \`squatch-tuner-live-$TAG-linux-x86_64.tar.gz\`, \`-linux-arm64.tar.gz\`, \`squatch-tuner-live-$TAG-macos-arm64*.zip\`: the headless tuner, with \`squatch-tone\`, an exact test tone. On Linux they need a running JACK server; the macOS build uses CoreAudio and needs nothing installed.

$status

Expand Down
21 changes: 16 additions & 5 deletions EXPORTED
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Written by tools/export-public from squatch-dsp at 5c5c9d9c27e037652f018974e14c57b11768053e (hosts/jack/jack_session.hpp only).
# Written by tools/export-public from squatch-dsp at 4ce2afb79bed94dcfadd5e18afb4fa51cf8bd853.
# Do not edit these files here: change them upstream and export again (see the README).
de9ec875a3df8d6488afd6893577bd995198373ca2bd2152887dcc5008504dc2 RESULTS.md
94b7020753515c516163468fd2b55406d1701c7ba032a8683073f6216598a8cc bench/algos.hpp
Expand Down Expand Up @@ -36,12 +36,23 @@ c44ba12f19a44ba3c0b782f6a1564699f0ccd00275450e47c4c0f7221de8f1d2 docs/tuner.was
6b22cbbde838e4c52af1308374a0a99cb35ee80e0c3a140495ecba564383dadd docs/worklet.js
1418fa07dbbc8cacd97726623b64417bff884004726efd04226b9f264c28298c hosts/common/display.hpp
2a945c68d96879b468bcfe0e1828ef115e69ad6c0c9cc2d398c65732e25c8dcf hosts/common/hold.hpp
67d2581b74bb60130369dfd7c36c261c9265d1fcc6489b3c8d75375e1f84f839 hosts/common/host_clock.hpp
b3665ead3181c6617fb86481539f3bcfc5d7620844488957daddfb9fbf507512 hosts/common/ir_file.hpp
3d9b651e6176dbb3897a8cbc4febcd4e461602f5cba279e9e95a095337980a17 hosts/common/latest.hpp
134a449bd05bd627bc9962a044468e9d1416d96e8268f14ee0374ae8142f94ba hosts/common/tone_gen.hpp
a02486cb10744347717cd51882c7ad6959139443c276532ffc69b454d949d5d6 hosts/common/tuner_frames.hpp
bfd7189893f32863cfdcdae2ca0a44638fb0893b7211aa6f400644f0336e5f40 hosts/jack/jack_session.hpp
1b1ac06435467c26a930cb8a9f281586efa9996c76675ad114715b86e9855cef hosts/jack/tone.cpp
52aa713ec159261e2e2b0dd329a8aa55585cb2a7e098a3c80ade9f8f6350f5e9 hosts/jack/tuner_live.cpp
1070f089a30dcbc128684a5650d04d82c4c496033f20ac7712b491dac7e32cc3 hosts/common/tuner_host.hpp
01aba5f2559e193676a3e4811d5f5d5a9f30d47f5ec81a7db972b9d60c561104 hosts/common/tuner_publisher.hpp
894ad767f4daabe66df14c6104915dacfbbbb7fe0d8e71a35719f8eb2a1419c7 hosts/coreaudio/Info.plist
0d40b74aab99e5694b923e498c7ccda1e0cafbdfa169eff5186fe78205374089 hosts/coreaudio/audio_chunks.hpp
52bb018f635163ae0d81da359efc8fc94cff1c054adf8d56339bcd8988b3aad8 hosts/coreaudio/coreaudio_session.hpp
3b24d9b4cdac4d6d3e280a68b0c424bad2d1245df1efea29c99d5f20501ccfe6 hosts/coreaudio/tone.cpp
8160e00f87175633b1b217db65c4a48f89d7c5badb1bffd7fbfed66b46f6d3f2 hosts/coreaudio/tuner_live.cpp
1f1b5d2cc9fc6b042afcdd27a0ec6d1a39d52836beb7c17f48b4e20cdd644cfe hosts/coreaudio/tuner_render.hpp
1b4b7b68affbd4395b7d1eb611b01b1220d4d090ab5fbc3ed3a866534a4beba7 hosts/jack/jack_session.hpp
2489d436b9dee2e69eaa0816e8f1b9679eb7150ae8bb9877dc4f678ae4cf1a00 hosts/jack/tone.cpp
601b67e21fab60418bfef5849dbfcefca32568861c90781eac54f9a1049ce70c hosts/jack/tuner_live.cpp
b6cc7da0e324458d7dbfa49f05b29d5d56b3ef9c9eaf0e6f4877fc6227dbe480 hosts/tests/test_host_audio.cpp
8559c84df869cdaa5f4200d8503018fa9ba54208a164f9313bf2e3523676e17b hosts/tests/test_hosts.cpp
0d8c6ad65e5ae06f9c8b1fcabf8270e783812f6d89076d0fab04a88247685a13 hosts/wasm/build.py
22863bba7ba0ee81f6095f6e463926ddf3f545836a2bdd25565f8a63ab91c7c1 hosts/wasm/demo.css
Expand Down Expand Up @@ -86,7 +97,7 @@ cbe66e0adc53dcf976819861a1fc3aeb862ef1c839aa1f5916d1169bdc8630db include/squatc
33d19f272b80e6d4e165d4d8c2705ff42aa2fa2f3f3092e853eed87a56958bd6 results/real.md
818c8ee6462a64bff412e3cf2113d0cf92f3a5daefb8088655381e2111e15b54 results/synth.md
2c724991273af88988ec5da7d76717814d829162e6d3680773dfa3eb48025d1b tests/test_core.cpp
9cba71b30d7e537cd98c8f2880da323e16c3e3963018bf621870543e6e9a1331 tools/export-public
ef551ef238d36f01bee7a4006ac35d8fff62f3a6211e25b4db043c80bcd40bed tools/export-public
27be201ebb49c36ae14442a8dd76cef12efeee46ea64e6e222769bc101a30b4a tools/release-scan
414c14c757ea09536eb85c185dcb3962b25aaba4d25b10530b24d2e1749b1c63 tools/release-signing
9942263d376c55df83073aa9cfa635f3a736e0fc9874f043483f68bf3d544b07 tools/test-release-scan
Expand Down
39 changes: 30 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ BUILD := build

BENCH_COMMON := bench/pv.cpp bench/poly_real.cpp
HEADERS := $(wildcard include/squatch/tuner/*.hpp) $(wildcard bench/*.hpp)
HOST_HEADERS := $(wildcard hosts/common/*.hpp) $(wildcard hosts/jack/*.hpp) include/squatch/dsp/latest.hpp
HOST_HEADERS := $(wildcard hosts/common/*.hpp) $(wildcard hosts/jack/*.hpp) $(wildcard hosts/coreaudio/*.hpp) include/squatch/dsp/latest.hpp

# The Guitar-TECHS single-note DI recordings (CC BY 4.0), downloaded separately: see RESULTS.md.
GUITAR_TECHS ?= data/guitar-techs
Expand All @@ -36,19 +36,40 @@ $(BUILD)/test_hosts: hosts/tests/test_hosts.cpp $(HEADERS) $(HOST_HEADERS) | $(B
$(BUILD):
mkdir -p $(BUILD)

test: $(BUILD)/tests $(BUILD)/test_hosts
# The CoreAudio host's audio path, offline: no audio device and no CoreAudio types, so it runs on any machine.
$(BUILD)/test_host_audio: hosts/tests/test_host_audio.cpp $(HEADERS) $(HOST_HEADERS) | $(BUILD)
$(CXX) $(CXXFLAGS) -O1 -g -fsanitize=address,undefined -o $@ $< -lpthread

test: $(BUILD)/tests $(BUILD)/test_hosts $(BUILD)/test_host_audio
./$(BUILD)/tests
./$(BUILD)/test_hosts

# The live JACK host and its exact test tone (hosts/jack). Needs the JACK headers
# (libjack-jackd2-dev on Debian and Ubuntu); not part of `all`.
./$(BUILD)/test_host_audio

# The live tuner and its exact test tone. On macOS they use CoreAudio directly (hosts/coreaudio): only
# system frameworks, nothing to install. Elsewhere they use JACK (hosts/jack), which needs its headers
# (libjack-jackd2-dev on Debian and Ubuntu). `make live LIVE_BACKEND=jack` forces JACK on a Mac that has it.
# Not part of `all`.
ifeq ($(shell uname -s),Darwin)
LIVE_BACKEND ?= coreaudio
else
LIVE_BACKEND ?= jack
endif
ifeq ($(LIVE_BACKEND),coreaudio)
LIVE_LIBS := -framework CoreAudio -framework AudioToolbox -framework CoreFoundation -lpthread
# The tuner reads a microphone: its Info.plist (embedded in the binary) carries the text macOS shows when
# it asks, so a launch from anywhere but Terminal is asked instead of being killed.
TUNER_LINK := -Wl,-sectcreate,__TEXT,__info_plist,hosts/coreaudio/Info.plist
else
LIVE_LIBS := -ljack -lpthread
TUNER_LINK :=
endif
live: $(BUILD)/squatch-tuner-live $(BUILD)/squatch-tone

$(BUILD)/squatch-tuner-live: hosts/jack/tuner_live.cpp $(HEADERS) $(HOST_HEADERS) | $(BUILD)
$(CXX) $(CXXFLAGS) -o $@ $< -ljack -lpthread
$(BUILD)/squatch-tuner-live: hosts/$(LIVE_BACKEND)/tuner_live.cpp $(HEADERS) $(HOST_HEADERS) | $(BUILD)
$(CXX) $(CXXFLAGS) -o $@ $< $(LIVE_LIBS) $(TUNER_LINK)

$(BUILD)/squatch-tone: hosts/jack/tone.cpp $(HOST_HEADERS) | $(BUILD)
$(CXX) $(CXXFLAGS) -o $@ $< -ljack -lpthread
$(BUILD)/squatch-tone: hosts/$(LIVE_BACKEND)/tone.cpp $(HOST_HEADERS) | $(BUILD)
$(CXX) $(CXXFLAGS) -o $@ $< $(LIVE_LIBS)

# The browser demo, built into build/web-demo (docs/ holds the published copy). Needs
# Emscripten on PATH; demo-test also needs Node 22 and Chrome.
Expand Down
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ void audioCallback(const float* in, int n) {
|---|---|
| `hosts/wasm/` | the browser demo: the core compiled to WebAssembly (31 kB, no imports) in an AudioWorklet, echo cancellation, noise suppression and automatic gain off, readings to the page 30 times a second |
| `hosts/web/` | the tuner page (strobe, needle, seals, A4 reference, light and dark themes, phone layout), and `serve.py`, a standard-library bridge that streams a live tuner to it |
| `hosts/jack/` | `squatch-tuner-live`, the tuner behind a JACK input (the Pi 5 host in RESULTS.md), and `squatch-tone`, an exact test tone |
| `hosts/common/` | what every host shares: note naming and the reading handoff |
| `hosts/jack/` | `squatch-tuner-live`, the tuner behind a JACK input (Linux; the Pi 5 host in RESULTS.md), and `squatch-tone`, an exact test tone |
| `hosts/coreaudio/` | the same two programs for macOS, on CoreAudio directly: no JACK, only system frameworks |
| `hosts/common/` | what every host shares: note naming, the reading handoff, and the live tuner's real-time step and JSON line |

## Build and test

Expand All @@ -134,7 +135,7 @@ make test # core tests (ASan + UBSan, including zero heap allocations i
make all # the tests and the bench programs
make results # every number in RESULTS.md, about 20 minutes; needs the Guitar-TECHS
# recordings: make results GUITAR_TECHS=/path/to/guitar-techs
make live # the JACK host (Linux, libjack-jackd2-dev)
make live # CoreAudio on macOS (nothing to install); JACK elsewhere (libjack-jackd2-dev)
make demo # the browser demo into build/web-demo (needs Emscripten)
make demo-test # the module in Node, then the page in headless Chrome against test tones
make lint # complexity limits (lizard)
Expand All @@ -144,6 +145,24 @@ To run the demo locally, serve `docs/` over HTTPS or from `localhost`, which bro
trust with a microphone: `python3 -m http.server -d docs 8000`, then open
`http://localhost:8000/`.

## Live on a Mac

On macOS, `make live` builds `squatch-tuner-live` and `squatch-tone` on CoreAudio. They link system
frameworks only, so a Mac needs nothing installed (no JACK); the options and the JSON lines are the
JACK host's. Linux keeps JACK.

```sh
build/squatch-tuner-live --list-devices # every device: channels, rate, which are the defaults
build/squatch-tuner-live # the default input device, channel 1
build/squatch-tuner-live --device umc --input 2 # a device by (part of) its name, channel 2
build/squatch-tone 110 --device umc --port 1 # an exact tone on that device's output 1
```

The first run asks, once, whether your terminal may use the microphone. If that was refused the input is
digital silence, and the tuner says so on stderr after two seconds. The programs use a device as it is: they
never change the Mac's default devices, a buffer size or a sample rate. `--out thru` needs one device with both
inputs and outputs (an interface).

## Where the code lives

The source of truth is [squatch-dsp](https://github.com/squatch-stack/squatch-dsp), where the
Expand Down
55 changes: 55 additions & 0 deletions hosts/common/host_clock.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: MIT
// Plumbing every live host shares, JACK or CoreAudio: the run flag a signal clears, the clocks, and a
// sleep to an absolute time. Nothing here runs in an audio callback except nowNs(), which is
// clock_gettime (no lock, no allocation; on Linux a vDSO call, on macOS a cheap system call).
#pragma once
#include <atomic>
#include <csignal>
#include <cstdint>
#include <ctime>

namespace squatch::host {

inline std::atomic<bool>& running() {
static std::atomic<bool> flag{true};
return flag;
}

inline uint64_t nowNs(clockid_t clock = CLOCK_MONOTONIC) {
timespec t;
clock_gettime(clock, &t);
return static_cast<uint64_t>(t.tv_sec) * 1000000000ull + static_cast<uint64_t>(t.tv_nsec);
}

inline void onStopSignal(int) { running().store(false); }

/// SIGINT and SIGTERM stop the program cleanly; so does the reader of stdout going away (SIGPIPE).
inline void installStopSignals() {
std::signal(SIGINT, onStopSignal);
std::signal(SIGTERM, onStopSignal);
std::signal(SIGPIPE, onStopSignal);
}

/// Sleep until an absolute CLOCK_MONOTONIC time, then advance it by `periodNs`.
/// macOS has no clock_nanosleep, so there it sleeps for the time left, read from the same clock.
inline void sleepUntil(uint64_t& deadlineNs, uint64_t periodNs) {
deadlineNs += periodNs;
#if defined(__APPLE__)
for (;;) {
const uint64_t now = nowNs();
if (now >= deadlineNs) return;
const uint64_t left = deadlineNs - now;
timespec t;
t.tv_sec = static_cast<time_t>(left / 1000000000ull);
t.tv_nsec = static_cast<long>(left % 1000000000ull);
if (nanosleep(&t, nullptr) == 0) return; // interrupted by a signal: go round with the time still left
}
#else
timespec t;
t.tv_sec = static_cast<time_t>(deadlineNs / 1000000000ull);
t.tv_nsec = static_cast<long>(deadlineNs % 1000000000ull);
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, nullptr);
#endif
}

} // namespace squatch::host
37 changes: 37 additions & 0 deletions hosts/common/tone_gen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: MIT
// The sine of squatch-tone, for any audio backend. The phase is a double accumulator, so the
// frequency is exact to the interface's own sample clock (which its input shares): the tuner's reading
// can be compared with HZ directly. It fades in and out over 10 ms. fill() is real-time safe.
#pragma once
#include <algorithm>
#include <atomic>
#include <cmath>
#include <cstdint>

namespace squatch::host {

struct ToneGen {
double phase = 0.0, step = 0.0; // turns, turns per sample
float amp = 0.1f, gain = 0.0f, fadeStep = 0.0f;
std::atomic<float> target{0.0f}; // 0 until connected, so the start time is the real one

void configure(double hz, double db, double rate) {
step = hz / rate;
amp = static_cast<float>(std::pow(10.0, db / 20.0));
fadeStep = static_cast<float>(1.0 / (0.010 * rate));
}

/// Writes `n` samples to o[0], o[stride], ... and returns the gain target it saw.
float fill(float* o, uint32_t n, uint32_t stride = 1) {
const float tgt = target.load(std::memory_order_relaxed);
for (uint32_t i = 0; i < n; ++i) {
gain = gain < tgt ? std::min(gain + fadeStep, tgt) : std::max(gain - fadeStep, tgt);
o[static_cast<size_t>(i) * stride] = amp * gain * static_cast<float>(std::sin(6.283185307179586 * phase));
phase += step;
phase -= std::floor(phase);
}
return tgt;
}
};

} // namespace squatch::host
Loading
Loading