diff --git a/.gitignore b/.gitignore index 92db1ab..0a9a6b8 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ NuGet.Local.Config _tmp_*.txt *.user *.suo +tools/aqua-feel/out/ diff --git a/Directory.Build.props b/Directory.Build.props index c8a3c67..ac7209e 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -2,8 +2,8 @@ $(CULTLIB_ROOT) $(MSBuildThisFileDirectory)..\CultLib - - 42dce3611a81adc5c5fac54cadbaa5637d6022d3 + + abd1a92eb24e1af3fc7cdd1bf98a88dbdebc2938 C:\Program Files\Git\cmd\git.exe git diff --git a/README.md b/README.md index ee44b52..879b5ea 100644 --- a/README.md +++ b/README.md @@ -156,11 +156,18 @@ handoff state lives in [`state/spine.yaml`](state/spine.yaml). ## Building against CultLib -Builds are pinned to CultLib 2.0.0 (tag `cultlib-unity-v2.0.0`, revision `42dce3611a81adc5c5fac54cadbaa5637d6022d3`); the build fails if the CultLib checkout differs or is dirty. Create a pinned worktree and point the build at it: +Builds are pinned to CultLib revision `abd1a92eb24e1af3fc7cdd1bf98a88dbdebc2938` (#91); the build fails if the CultLib checkout differs or is dirty. Create a pinned worktree and point the build at it: ```powershell -git -C worktree add --detach cultlib-unity-v2.0.0 +git -C worktree add --detach abd1a92eb24e1af3fc7cdd1bf98a88dbdebc2938 dotnet build -p:CultLibRoot= # or set CULTLIB_ROOT= ``` -Without an override the sibling `..\CultLib` checkout is used and must itself be at that tag. +Without an override the sibling `..\CultLib` checkout is used and must itself be at that revision. + +**Stores are one-way, and rolling back can lose data.** Since the pin move (#35) list elements carry ids, and a store holding any element id is stamped with a newer header. What an older build does with it depends on the store kind: + +- **Single-file stores** (header v3 with ids): the patch catalog (`patches/aquasynth-patch-cultcache.cc`), the speech training store and the daemon store. Pins before `cultlib-unity-v2.0.0` / `caching-unity-v2.0.0`, including NuGet `cultlib-v1.0.32`, read these without complaint, silently drop the ids, and can rewrite elements without them. Only v2.0.0 and later refuse by name. +- **Directory stores** (header v5 with ids): the IPA trial results store (`ipa-trial-results.cc`, `UseDirectoryStore`). These are refused by name from `cultlib-unity-v1.0.57` / `caching-unity-v1.2.0`. + +So: take a copy of any store before the upgrade and restore from it if you roll back, never write with the new pin and then read with an old one, and upgrade every reader before any writer. Stores written before the move still open; ids are minted on load. diff --git a/docs/dings-patch-identity-authority-map.md b/docs/dings-patch-identity-authority-map.md new file mode 100644 index 0000000..78b7b40 --- /dev/null +++ b/docs/dings-patch-identity-authority-map.md @@ -0,0 +1,357 @@ +# Dings patch identity: authority map + +**Status (2026-11-12).** Done: step 1 (identity from the catalog, `2e28f56`), step 4 (one validation gate, `e188424`), step 5a (neutral fallback id tied to the catalog mirror, this change). Stopped, with the unblock for each in the Migration plan: step 2 (needs the MCP caller-identity fact and a listening), step 3 (needs a ruling that breaks the byte-identity rule), step 5b (needs a generated file, an owner's decision on drifted values and an ear check). + +Serves follow-up `aqua-body:follow_up:aquasynth-dings-authority-map`. Claims are from +source at `c0fe4ee` (origin/main). Nothing was built or run; the one place behaviour is +inferred rather than read is marked. Paths are repo-relative; `Host` = +`tools/AquaSynthDingsHost`, `Mcp` = `tools/AquaSynthDingsMcp`. + +Companion to `docs/research/dings-playback-and-ogg-export.md`, which describes the +playback path and an export sketch. This note asks a narrower question: in the Dings +path, what decides which sound "this patch" is, and how many places decide it. + +## What I counted + +The follow-up says four owners. The source supports four places that *decide* a +voice's identity, plus one place that names the same thing a second time without +deciding, and one generated mirror: + +| # | Owner | Decides | Section | +|---|---|---|---| +| 1 | `DingCatalog` | the instrument id and the script that defines its sound | A | +| 2 | Host and daemon derivation | the daemon `patchId`, `faustName`, `revision`, session id and store file names | B | +| 3 | MCP `AgentInstrumentRegistry` | which instrument an agent session gets | C | +| 4 | `tools/dings-voices` | which voice a sender (agent localpart) gets, in a separate synth | D | +| - | Earcon Workshop `IDENTITIES` | reuses catalog instrument ids with its own parameters | E | +| - | `tools/aqua-feel/lowered-dings` | checked-in copy of #1, guarded by a test | E | + +The follow-up does not list its four, so this grouping is my reading. Someone who +counted differently (for example splitting the daemon's session id from the store file +name, or counting Earcon Workshop as a fifth) would get a different number from the same +code. The grouping above follows who writes the key. + +## A. `DingCatalog` instrument id and script + +- Where: `src/AquaSynth.Dings/DingCatalog.cs:39-45` (four instruments), built by the + private `Instrument(...)` at `:56-57`, which writes the `Script` string (an AquaSynth patch script, `param path=...; voice wave=...`, that `FaustEmitter.EmitScript` lowers to Faust; not Faust source) and + fixes `RootFrequency = 261.6256f` for all four. +- Keys on: the instrument `Id` string (`warm-bell`, `glass-chime`, `soft-triangle`, + `muted-pluck`), case-insensitive dictionary (`:35`). Events are a separate keyed + dictionary (`:16-27`, nine ids) and are not voice identity. +- Lifetime: compiled in. Changing an entry means a new build of every consumer. +- Writer: whoever edits the file. `ValidateCuration` (`:47-64`) is the only gate. +- Readers: `Host/DingsPlaybackHost.cs:66-68` (via `DingCatalog.PlayRejection`, `DingCatalog.cs:17-24`), `Mcp/DingService.cs:12-13`, + `Mcp/AgentInstrumentRegistry.cs:11`, `Mcp/DingTools.cs:28,31,49`, + `tools/AquaLower/Program.cs:17`, and the tests `DingCatalogTests`. +- Content hash: none. The id is a label; the script is the content. Nothing here says + "this script, at this version". + +## B. Host and daemon derivation of the patch + +The Host does not pass the catalog id through. It sends the daemon identity fields +derived from it by `DingCatalog.Identity` (`DingCatalog.cs:12-14`, added in step 1; before +it, at `965dfe8` (origin/main before this change), `AquaDaemonClient.cs:20` composed the strings inline), and the daemon +builds more keys from them. + +- `Host/AquaDaemonClient.cs:20-21` per note: `var identity = DingCatalog.Identity(instrument)` + gives `patchId = "dings." + id`, `faustName = "dings_" + id with - as _`, `revision = 1` + (a literal inside `Identity`); the same envelope sets `durationSeconds = 1.5f` and + `controls["/ding/frequency"]`. +- `src/AquaSynth.Faust/NativeFaustRuntime.cs:596-599`: the compile key is SHA-256 of + `"{revision}\n{faustName}\n{script}"`. `patchId` is not in it. So two ids with the same + name and script share a compile key; one id with an edited script gets a new key even + though `revision` stays 1. +- `src/AquaSynth.Faust/AquaSynthDaemonService.cs:1136`: session id = + `StoreFileName.From(patchId) + "-" + compileKey[..12]`. A recompile with the same + session id disposes the old session (`:366-376`). +- `src/AquaSynth.Core/StoreFileName.cs:26-54`: the mapping from any id to a file name + (ASCII letters, digits, `-`, `_`, `.` pass unchanged unless the id has an uppercase letter, a device stem, a trailing dot, is over 100 characters or already ends in a hash shape; anything else gets a 16-hex SHA-256 suffix, `StoreFileName.cs:37,59-67`). + `dings.warm-bell` is a fixed point (pinned by `StoreFileNameTests`). +- Keyed by patch: the session document. `WriteReceiptAsync("sessions", sessionId, session)` + (`:407`) runs on every compile, and `SampleAsync` compiles on every note + (`:622-635`), so the store holds `sessions/.cc` for each + distinct `(patchId, compile key)`, rewritten on each note. This is the one store + artifact addressable by instrument. `NativeFaustRuntime.cs:516,628-636` would also write + `.dsp`, but only when `options.DspSourceDirectory` is set; the + daemon builds its native options from sample rate and render limits only + (`AquaSynthDaemonService.cs:330-334`) and nothing else in `src` or `tools` sets it, so + for the Dings host it is off. (Read from source, not observed.) +- Not keyed by patch: render output and the render and compile receipts. Samples are + `/samples/.wav` (`:664-667`), where + `commandId` is a fresh `ding-` per note (`AquaDaemonClient.cs:19`). Receipts go + through `WriteReceiptAsync` (`:922-937`) under command-derived ids, with the patch id + only in the body. The store therefore keeps one WAV per note played, none addressable + by instrument. Whether the store ever gets cleaned I did not find. +- Faust name, three forms: the Host sends `dings_`; the daemon applies + `SafeFaustName` (`NativeFaustRuntime.cs:602-607`, called at + `AquaSynthDaemonService.cs:911` and `NativeFaustRuntime.cs:513`) so the manifest and + emitted source carry the rewritten name, while the compile key (`:596-599`) hashes the + raw `identity.FaustName`. Two raw names that sanitise alike compile alike but key + differently. +- The `dings_` rule now lives once, in `DingCatalog.Identity`. `AquaDaemonClient.cs:20` + and `tools/AquaLower/Program.cs:19` call it; `LoweredDingsFedToTheFeelMeterMatchTheCatalog` + compares against a literal per instrument, and `CallersDeriveIdentityOnlyThroughTheCatalog` + fails if either caller stops calling it. (History: at `965dfe8` the rule was written three + times, in those two files and in the test at `DingCatalogTests.cs:45`.) +- Writer: the Host (fields) and the daemon (session id, file names). Readers: the + daemon's compile/render path and receipts; no Dings code reads the patch id back. +- `Host/AquaDaemonClient.cs:24-29` returns the WAV path from the receipt. The Host + never asks the daemon which patch rendered it. + +## C. MCP `AgentInstrumentRegistry` + +- Where: `Mcp/AgentInstrumentRegistry.cs:10` (dictionary), `:13-17` `GetOrAssign`, + `:19-29` `Claim`, `:31-36` `AssignDeterministically`. +- Keys on: `agentSessionId`, which is `server.SessionId ?? "stdio:" + ProcessId` + (`Mcp/DingTools.cs:45`; that `SessionId` is null under stdio is inferred from the `??` fallback, not read from the SDK). That is the MCP transport session, not the agent. In HTTP + mode (`Mcp/Program.cs:34-54`, `Stateless = false`) it lives as long as that MCP + session; in stdio mode it is the bridge process id. +- Value: an instrument id string. Unclaimed sessions get + `SHA256(sessionId)[0..4] % sortedInstrumentIds.Length`. `Claim` overwrites silently, + and there is no uniqueness check, so two sessions can hold the same instrument. +- Lifetime: in-process, a `ConcurrentDictionary`. Lost on bridge restart. A restarted + or reconnected session gets a different session id, hence generally a different + hashed instrument unless it claims again. (Inferred from the keying; I did not + observe a reconnect.) +- Writer: the agent, through `claim_instrument`, or the hash. Reader: `play_ding` + (`DingTools.cs:16`) and `get_agent_instrument` (`:19`). +- Also decides: `DingService.PlayAsync` (`Mcp/DingService.cs:12-14`) validates event, + instrument and gain, and the Host validates the same three (`DingsPlaybackHost.cs:66-68`). + Post-step-4 state: the Host owns the gate through `DingCatalog.PlayRejection`; the + bridge copy is the DEPRECATED fail-fast path (see Migration plan, Step 4). They agree + because both read `DingCatalog`. + +## D. `tools/dings-voices` sender voices + +- Where: `tools/dings-voices/src/voices.mjs:8` (`SENDERS`, eight localparts), `:51-73` + (`VOICES`, one `hit` function each), `:13-18` (`RHYTHM`, four motifs). +- Keys on: sender localpart x motif (`mention`, `dm`, `message`, `invite`), emitted as + `senders//.ogg` and `manifest.json` + (`finish.py:62`, shape `{neutral, senders:{localpart:{motif:path}}}`). +- It does not use `DingCatalog`, `AquaSynthDaemon` or Faust. It is a JS synthesizer that + imports only the sample rate from `earcon-workshop/web/earcon.js` (`voices.mjs:5`). + Its vocabulary (4 motifs, 8 senders) shares no ids with Aqua's (9 events, 4 + instruments). +- One link to the catalog family: the neutral fallback is rendered through the Earcon + Workshop `warm-bell` identity (`build.mjs:10-14`), with an event mapping of + `mention=warning, dm=waiting, message=message, invite=done` (`:12`) that the file + itself calls provisional. +- Lifetime: a build artifact. The files are not committed (README, "Not here"); the + consumer, Ratatoskr's `dings.ts`, is outside this repo and I did not read it. +- Writer: a person editing `voices.mjs`; `NIBU_PROPOSALS` (`:81-92`) shows the selection + of the default voice is a one-line assignment (`VOICES.nibu = ...`, `:92`). +- This is the only place a *sender* is identity. Everything else in the Dings path + keys on an MCP session. + +## E. Places that echo the identity without deciding it + +- Earcon Workshop `IDENTITIES`, `tools/earcon-workshop/web/earcon.js:22-27`. Same four + ids; its comment (`:21`) says values "start from the Dings catalog instruments and + were not tuned by ear". The values have drifted from the catalog: + + | id | catalog root / decay (`DingCatalog.cs:41-44`) | workshop `rootHz` / `decay` | + |---|---|---| + | warm-bell | 261.6256 / 1.15 | 261.6256 / 0.55 | + | glass-chime | 261.6256 / 0.85 | 329.6276 / 0.45 | + | muted-pluck | 261.6256 / 0.34 | 293.6648 / 0.25 | + + Decay is read with different units in the two voice models, so only the root column + is a clean disagreement. A "warm-bell" in the Ratatoskr neutral fallback is therefore + not the "warm-bell" an MCP agent hears. +- `tools/aqua-feel/lowered-dings/` (checked in): `.dsp` per instrument and `dings.json`, + produced by `AquaLower --dings`. `DingCatalogTests` fails when it falls out of date + (the test comment at the top of `LoweredDingsFedToTheFeelMeterMatchTheCatalog` names + the regeneration command). A mirror guarded by a test; not an independent owner. + `tools/aqua-feel/shelf.mjs:64` and `catalog.js:22-24` read it, and + `shelf.mjs:64` defines `warm-bell` as the shelf default (`DEF_INS`, used at + `:71,:75,:76`), a fifth voice default beyond the registry's hash. + +## Where owners can disagree + +1. **Name versus content.** A is a name plus a script. B keys sessions on the content + (compile key) and labels receipts with the name. Editing a script in place keeps + `patchId = dings.warm-bell` and `revision = 1`, so receipts from before and after the + edit carry the same patch id and different session ids. Nothing in a receipt says + which catalog version produced it. +2. **Session versus agent.** C assigns by MCP session; the stated intent in + `Mcp/README.md:3-4` is "which agent spoke". After a reconnect the same agent can + change timbre. D, meanwhile, keys on the agent address localpart. No code maps a + session id to a localpart, so the two cannot be joined today. +3. **Four timbres versus eight senders.** C picks among 4 catalog instruments by hash; + D has 8 hand-made voices. An agent can have one voice in `play_ding` and a different + one in its Ratatoskr notification, and nothing records either choice. +4. **Same id, different sound.** `warm-bell` is a catalog instrument (A), a workshop + identity (E) and the neutral fallback voice (D). Three definitions, parameter sets + that have drifted (table above). +5. **Derivation (resolved by step 1 for the code callers).** At `965dfe8` the `dings_` + rule was written three times (`AquaDaemonClient.cs:20`, `AquaLower/Program.cs:19`, the + test). Now it is in `DingCatalog.Identity`. What remains: the `dings.` patch id appears + in `StoreFileNameTests` as a literal, and the test pins literals, so a rename of the rule + fails those tests on purpose rather than silently moving store file names. +6. **Double validation (step 4 done).** The Host owns the gate (`DingsPlaybackHost.cs:66` + calls `DingCatalog.PlayRejection`); `DingService.cs:12-14` is the deprecated bridge + fail-fast copy. Still two checks until the bridge copy is removed; safe while both read one dictionary. + +## The Host pipe protocol, as read from source + +This is the "private pipe": the only channel into the audio host. MCP is a client of it; +it is not itself the pipe. + +**Listener.** `AquaSynthDingsHost.exe`. `Host/Program.cs:3-4` takes the named mutex +`Local\GameCult.AquaSynthDings.Playback.v1` and exits if another instance holds it, then +runs `DingsPlaybackHost.RunAsync` (`:7`). The accept loop is +`DingsPlaybackHost.cs:12-20`: one `NamedPipeServerStream` per accepted client, name +`GameCult.AquaSynthDings.Playback.v1` (`src/AquaSynth.Dings/DingsProtocol.cs:8`), +`PipeDirection.InOut`, byte mode, `MaxAllowedServerInstances`, `Asynchronous | +CurrentUserOnly` (`:16`). Each connection is handled fire-and-forget (`:18`). The host +uses NAudio WASAPI (`Host/DingAudioPlayer.cs`) and is Windows-only in practice. + +**Clients.** `Mcp/DingsHostClient.cs`, used by the MCP bridge (both stdio and +`--http`) and by the bridge's direct flags `--play`, `--get-volume`, `--set-volume`, +`--mute` (`Mcp/Program.cs:9-32`). Nothing else in the repo opens the pipe. + +**Framing** (identical in both directions): +`[int32 length][MessagePack body]`. The length is `BitConverter.GetBytes(int)`, so +native byte order (little-endian on every platform this builds for, but the code does not +fix it). Valid length is 1 to 1,048,576 bytes; outside that the reader throws +(`DingsPlaybackHost.cs:86-87`, `DingsHostClient.cs:42`). Body is +`MessagePackSerializer` of the keyed records below. One request, one response, then the +server disposes the pipe (`DingsPlaybackHost.cs:24-36`). No streaming, no pipelining, no +request id. + +**Request** `DingsCommand` (`DingsProtocol.cs:13-19`), keyed: + +| key | field | note | +|---|---|---| +| 0 | `Version` | must equal `DingsProtocol.Version` = 1, else status `incompatible` (`DingsPlaybackHost.cs:41`) | +| 1 | `Kind` | `Ping, Play, StopAll, GetVolume, SetVolume, SetMuted` (`:11`) | +| 2 | `EventId` | catalog event id, `Play` only | +| 3 | `InstrumentId` | catalog instrument id, `Play` only | +| 4 | `Value` | `Play`: gain 0..1; `SetVolume`: volume 0..1; `SetMuted`: `>= .5` means muted | + +**Response** `DingsResponse` (`DingsProtocol.cs:21-31`): `Version`, `Succeeded`, +`Status`, `Message`, `PlaybackId`, `Volume`, `Muted`, `HostProcessId`, `SessionName` +(default `"AquaSynth Dings"`). Every response carries current volume and mute, built at +`DingsPlaybackHost.cs:79-80`. + +**Per kind** (`DingsPlaybackHost.cs:39-62`): `Ping` and `GetVolume` are the same branch +(status `ready`). `SetVolume` rejects non-finite or out-of-range values +(`rejected`) and otherwise sets `audio.Volume` (`volume-set`). `SetMuted` -> `muted` / +`unmuted`. `StopAll` -> `stopped`. `Play` (`:64-77`) validates event, instrument and +gain, renders each note through the daemon, queues each WAV with its delay, and answers +`playing` with a fresh `ding-` `PlaybackId`. The answer comes after rendering and +queueing, not after sound. `PlaybackId` is not stored and nothing takes it as input. +Any exception becomes `Succeeded = false, Status = "failed", Message = ex.Message` +(`:32-35`), provided the pipe is still connected. + +**Client behaviour** (`Mcp/DingsHostClient.cs:15-48`): the client validates the +response `Version` (`:46`). Connect timeout 350 ms; on `TimeoutException` or +`IOException` it launches the host (`StartHost`, `:50-56`: newest +`AquaSynthDingsHost.exe` under `tools/AquaSynthDingsHost/bin`, hidden window, working +directory the repo root found by walking up to `AquaSynth.sln`) and retries up to 20 +times at 500 ms connect with 100 ms gaps, all inside a 15 s overall timeout. A +`Succeeded = false` response is turned into `InvalidOperationException(response.Message)` +by `DingService.EnsureSuccess` (`DingService.cs:25-29`). + +**MCP side, for completeness.** Tools (`Mcp/DingTools.cs`): `play_ding`, +`get_agent_instrument`, `claim_instrument`, `list_ding_events`, `list_instruments`, +`stop_dings`, `get_volume`, `set_volume`, `mute_dings`. Transports: stdio by default +(`Program.cs:56-65`); `--http [url]` serves Streamable HTTP at `/mcp`, default +`http://127.0.0.1:17878`, singleton by mutex `Local\GameCult.AquaSynthDings.McpHttp.v1` +(`Program.cs:34-54`). Only `play_ding`, `get_agent_instrument`, `claim_instrument` touch +agent identity; the volume and stop tools address the whole host and are +machine-global. + +**Not authenticated beyond the OS.** `CurrentUserOnly` limits it to the same Windows +user. Any process of that user can send `Play`. There is no per-caller identity on the +pipe: `DingsCommand` carries an instrument, not a caller, so the Host cannot know who +asked. + +**Status (2026-11-12): the owner is RULED.** The operator tier, relayed through Persona, ruled that `DingCatalog` is the single owner of Dings patch identity. The sections below were written as a proposal; they are kept as the analysis behind the ruling. Their per-step claims are still unverified plans, except step 1, which landed on branch `aqua/dingcatalog-owner-step1`. + +## One owner: DingCatalog (ruled; analysis below was written as a PROPOSAL) + +**Proposal.** `DingCatalog` (`src/AquaSynth.Dings`) becomes the only place that decides +what a voice is, and also the only place that derives the daemon keys from it. Its +instrument gains a content revision (a hash of `Script` plus the parameters that matter), +and one function in that assembly produces `PatchId`, `FaustName` and the revision for +the daemon (`AquaLower` needs only the faust-name part of it, not +`patchId` or revision). Everything else keys on the catalog id and takes the rest from the catalog. + +End state versus this change: the content revision named above is deferred (step 3 of the Migration plan below). Until it lands, `revision` is the literal 1 and the catalog function returns it; the revision hash is not part of the first consolidation. + +**What the others become.** +- B (Host/daemon): the Host stops composing `patchId`/`faustName`/`revision`; it calls + the catalog function. `AquaLower/Program.cs:19` calls only the faust-name slice of it (done in step 1); the test pins literals instead of calling it. The daemon + keeps its session and file naming; those are daemon-internal and derive from what it + is given. +- C (registry): stays in the bridge as a cache of choices, but its assignment function + moves beside the catalog (so a future export can call it) and its key becomes an + agent identity rather than an MCP session id. This depends on the open question + below. +- D (`dings-voices`): stays a separate synthesizer for sender notifications, since its + voices are JS functions with no Faust form. Its link to the owner is by reference: + the neutral fallback names a catalog instrument id instead of a second `warm-bell`. + Whether sender voices should ever become catalog instruments is a product fork, not + part of this change. +- E: Earcon Workshop `IDENTITIES` either read their shared values from a generated + file (as `lowered-dings` already is) or drop the shared ids; the drifted values in + the table above are resolved by whoever owns the workshop's tuning. + +**Why this owner.** A already has the most readers (the list in section A), is the only +definition of the script, and is the only one the host cannot run without. C, D and E +are all consumers or approximations of it by name. + +**Migration order.** One list only: the "Migration plan" section at the end of this note (the original five-item order was merged into it). + +**Risks.** +- Step 2 changes what agents hear on first use after deploy; nobody has listened to a + before/after. +- The deferred content revision changes session ids and session file names, visible in + receipts but not read by anything I found. +- Oracle loss in step 1 (handled): the guard test uses a literal per instrument, so it does + not compare the shared function against itself. +- Collapsing 8 senders onto 4 instruments would lose distinctness; the proposal does not + do that, and I am recommending against it without having listened to either set. +- Moving derivation to the catalog assembly makes `AquaSynth.Dings` the home of + daemon-protocol knowledge; if the daemon is expected to run without that assembly, + this is the wrong direction. I did not check whether anything else wants that. + +**Confidence.** Moderate on the owner choice; it follows reader counts and the write +path, not any prior art on identity registries. Lower on step 2, because it depends on +what the MCP bridge can learn about its caller. + +## Could not verify + +- That "four owners" in the follow-up means these four. The count here is mine. +- Whether an MCP client can tell the bridge which agent it is. The only identity read in + source is `server.SessionId` (`DingTools.cs:45`); I did not inspect the MCP SDK for + client-info fields, or any harness config. +- What Ratatoskr's `dings.ts` actually reads and whether it shares any naming with + `DingCatalog`; it is not in this repo. +- Behaviour at runtime: reconnect session ids, store growth under `.aquasynth/dings-host`, + and whether the Host's `Ping`/`GetVolume` have different callers in practice. The + pipe protocol above is from reading, not from a captured exchange. Nothing was built, + run, or listened to for this note. + +## Migration plan (ruled 2026-11-12: DingCatalog is the owner) + +Rules for every step: deprecate before removing (mark the old path with a pointer, keep it working, delete in a later pass after the window); `patchId`, `faustName`, `revision = 1`, session ids, compile keys and `sessions/<...>.cc` names stay byte-identical until step 3; no change to the Host or pipe protocol. Deprecation window: one merged release of the owner path with the old path still working and marked; removal only in a separate PR after the operator hears or reviews nothing regressed. + +**Step 1 (done in this change).** `DingCatalog.Identity(instrument)` returns `PatchId`, `FaustName`, `Revision` (literal 1). `AquaDaemonClient` and `AquaLower --dings` call it. The guard test no longer carries a private copy of the rule: it compares against literals per instrument, and `IdentityPinsTheOldDerivedValuesForAllFourInstruments` pins the old derived values for all four. `CallersDeriveIdentityOnlyThroughTheCatalog` reads `AquaDaemonClient.cs` and `AquaLower/Program.cs` and fails if either lacks the `DingCatalog.Identity(` call or reintroduces a `"dings_"` / `$"dings` literal (a source check, not a behaviour check). +- Verify: the pin test; `DingCatalogTests` and `StoreFileNameTests`; `AquaLower --dings tools/aqua-feel/lowered-dings` leaves `git status` clean (the checked-in mirror is byte-identical). +- Not verified: the Host sending the same JSON to the daemon at runtime (compile-checked and read, not run against a daemon). +- Rollback: revert the commit; no stored state or wire format moved. +- Echo sites in this step: the `lowered-dings` mirror is tied to the catalog by `AquaLower` calling `Identity` and stays test-guarded. Earcon Workshop `IDENTITIES` is NOT tied: it is browser JS with its own voice model (`fmRatio`, `harmonics`, seconds-based decay) and shares only ids, so tying needs a generated file and a build step, which is step 5. It is marked deprecated with a pointer, and still works. + +**Step 2. Agent identity for the registry** (`AgentInstrumentRegistry` key). STOPPED, not implemented. Needs a fact nobody has: what the MCP bridge can learn about its caller. The only identity read in source is `server.SessionId` (`tools/AquaSynthDingsMcp/DingTools.cs:45`); the MCP SDK's client-info fields and the harness configuration were not inspected. It also needs a before/after listening that nobody has done, because a new key changes which instrument an agent hears on first use after deploy. Unblock with: (1) the fact, i.e. whether the MCP SDK / harness exposes a stable agent or client identity to a server tool call (supplied by whoever reads the SDK source or runs the bridge under the harness: Aqua can read the SDK, the harness owner can say what a client sends); (2) a ruling on which field becomes the key; (3) an operator listening of old vs new assignment for the agents in use. Verify: a test that the same agent id maps to the same instrument across sessions, and the listening above. Deprecate: keep the session-id keyed path behind the new key for one release. Rollback: revert; assignments are an in-memory cache. + +**Step 3. Content revision** (replace the literal 1 with a catalog hash). STOPPED, not implemented. It deliberately breaks byte-identity: compile keys, session ids and `sessions/<...>.cc` names all change, which is contrary to the standing rule above that they stay byte-identical until this step. So it needs its own ruling before anyone cuts: what feeds the hash (script text only, or events, root frequency and control path too) and whether old session files are left or removed. Unblock with that ruling, from the operator (the migration owner who set the rule); Aqua can draft the options but not choose. Verify: receipts before and after, with the old keys listed as expected to change. Rollback: revert to literal 1; new session files stay as orphans, harmless. + +**Step 4. One validation gate (done in this change).** Finding: both gates already read the same `DingCatalog.Events` / `DingCatalog.Instruments` dictionaries, so the change is the single rule text, not the data. The Host was already the only enforcing gate (every pipe client passes it); the bridge check is a pre-filter that cannot be forwarded away without a visible change (bridge throws `ArgumentException` / `ArgumentOutOfRangeException`, the Host path surfaces as `InvalidOperationException`; the bridge also lets a NaN gain through to the Host's rejection). So: `DingCatalog.PlayRejection(eventId, instrumentId, gain)` is now the one rule and returns the Host's exact rejection text or null; `DingsPlaybackHost.PlayAsync` calls it (same texts, same `rejected` status, same non-finite gain rejection). `DingService.PlayAsync` keeps its own check unchanged, marked DEPRECATED with a pointer comment; remove it only in a separate PR after the window. No protocol change, no identity change, no audible change. +- Verify (ran): `DingCatalogTests` + `DingsBridgeTests` + all `Ding*`/`StoreFileName*` focused tests (79 passed, includes the step-1 identity pins untouched); `dotnet build tools/AquaSynthDingsHost` clean. New tests pin the gate texts (incl. NaN/Infinity), the exact guard statement in the Host source (`PlayRejection(...) is { } x` followed by `return Response(false, "rejected", x)`, comment-stripped; a source-shape check, not a behaviour check, so it cannot see a bypass built some other way), and that the deprecated bridge path keeps its exception types, sends nothing to the Host when it rejects, and agrees with the gate's texts. Each was mutation-checked (text change, gate call removed from Host, bridge exception type/text changed, NaN check dropped): each mutation failed at least one test. +- Not verified: the Host rejecting over a live pipe (compile-checked and read; no Host test project exists and none was added); nothing heard. +- Rollback: revert; both gates read one dictionary, so behaviour is the same either way. + +**Step 5. Echo sites by reference.** (a) DONE in this change. `tools/dings-voices/src/voices.mjs:11` now holds `NEUTRAL_INSTRUMENT_ID = "warm-bell"` (was a literal in `build.mjs`), `build.mjs:13` renders `renderFamily(NEUTRAL_INSTRUMENT_ID)`, and `tools/dings-voices/src/neutral_instrument.test.mjs` asserts the id is one of the `tools/aqua-feel/lowered-dings/*.dsp` names, the checked-in mirror that `AquaLower` generates from `DingCatalog` and `DingCatalogTests.LoweredDingsFedToTheFeelMeterMatchTheCatalog` (`tests/AquaSynth.Dsl.Tests/DingCatalogTests.cs:92`) guards. The constant lives in `voices.mjs` because `build.mjs` writes files on import and cannot be loaded by a test. The test is wired into the `Run` line of `tools/dings-voices/README.md` (dings-voices has no package.json or test runner; `polyphony.test.mjs` is run the same way). Verify (ran): `node src/neutral_instrument.test.mjs` passes (1 assertion file, 2 asserts); changing the id to `bogus-bell` makes it fail with `neutral instrument "bogus-bell" is not a catalog id`; `node src/polyphony.test.mjs` still passes; `node src/build.mjs` writes `neutral-{dm,invite,mention,message}.wav` with identical sha256 before and after (`a3bf9076...`, `4a4652d6...`, `25938d0e...`, `26f1d74a...`). No audible change; nothing heard. Rollback: revert; nothing else reads the constant. (b) STOPPED, not implemented. Intent: export the shared ids and root frequencies from the catalog into a checked-in file (as `lowered-dings` is), guarded by a test, and have `earcon.js` read it. It needs a generated file plus a build step, and the Earcon Workshop is browser JS with its own voice model (`tools/earcon-workshop/web/earcon.js:24`, `IDENTITIES`), so the generator has no existing home. It also needs an owner's decision on the drifted values (table in section E: root 329.6276 vs 261.6256 for glass-chime, 293.6648 vs 261.6256 for muted-pluck; decay is in different units) and an ear check, since the workshop values were never tuned to the catalog. Unblock with: (1) the workshop owner's ruling per drifted value: catalog wins, workshop wins, or the id is dropped from `IDENTITIES`; (2) a ruling on where the generator runs (AquaLower flag or a node script); (3) an operator ear check of the four identities after the change. Deprecation: `IDENTITIES` keeps its inline values, marked, until the generated file is read in production. Verify: `tools/earcon-workshop/test.mjs`, the guard test, and the ear check. Rollback: revert; the inline values still exist. diff --git a/docs/faust-toolchain-boundary.md b/docs/faust-toolchain-boundary.md index 2a717a1..ee46cae 100644 --- a/docs/faust-toolchain-boundary.md +++ b/docs/faust-toolchain-boundary.md @@ -91,6 +91,27 @@ search; `FaustHome` then supplies `share/faust` if the prefix is not the file's grandparent. The loader prepends `lib` and `bin` to `PATH` using the platform path separator. +## Linux Native Toolchain + +Built and run on Debian 13 (2026-11-03). Faust `aa30fd7` (2.90.20), LLVM 19 +(`LLVM_CONFIG=/usr/lib/llvm-19/bin/llvm-config`, `INCLUDE_DYNAMIC=ON`, +`backends/most.cmake`), installed to a prefix; `make -j4`. The first link +failed with `cannot find -lPolly -lPollyISL -lzstd`; the install prefix that +resulted has `lib/libfaust.so` but `share/faust` holds only the architecture +files, not `stdfaust.lib` and the other `.lib` files (they live in the separate +faustlibraries repo). Compiling any patch that imports `stdfaust.lib` then +fails with `unable to open file stdfaust.lib`. Point `AQUASYNTH_FAUST_HOME` at a +home whose `lib/` has `libfaust.so` and whose `share/faust` has both. + +`tools/FaustRender` renders one `.dsp` with an `rpm` control to WAVs plus +`manifest.json` (hashes, peak, rms, non-finite flag). It measures; it does not +listen. `docs/renders/thruster-v5-manifest.json` is its output for +`patches/thruster/thruster.dsp` at PR #3 head `c6561da`, 4 s per case. The render is +deterministic (two reruns byte-identical); the 2026-10-31 sweep file is a 6 s +render, which is why its hash differs from a 4 s sweep. That manifest describes the mix at `c6561da`; +`patches/thruster/thruster.dsp` changed on 2026-11-05 (lower idle noise floors, rumble low-pass opens at low load; see +`docs/renders/aqua-feel-2026-11-05.md`), so its `dspSha256` and WAV hashes no longer match the repo's patch. + ## Invariants - AquaSynth remains the authority for what a patch means. diff --git a/docs/faust-unity-player-scoping.md b/docs/faust-unity-player-scoping.md new file mode 100644 index 0000000..9313cb3 --- /dev/null +++ b/docs/faust-unity-player-scoping.md @@ -0,0 +1,316 @@ +# Faust DSP in a built Unity player (Windows + Linux): scoping + +Scoping only. No code changed. Follow-up `aqua-body:follow_up:chat-s3500-3500`, 2026-11-06. + +Question: how does Faust DSP (the thruster, `patches/thruster/thruster.dsp`, v5) reach a +built Unity player on Windows and Linux? (a) ahead-of-time (AOT) Faust to C++ per patch +inside a native plugin, or (b) runtime `libfaust`/LLVM inside the player. + +**Limitation:** AOT Faust-to-C++ fixes the patch set at build time. Player-authored or modded patches cannot run in a built player; live-edited patches run only in the editor (the developer-time lane of path (b)). + +Marks: **[V]** verified here by reading source or running a tool on 2026-11-06. +**[U]** unverified: from memory, or needs a Unity editor/player or a Windows host this run +did not have. Nothing here has been run inside Unity. + +## Recommendation + +Take (a) for shipped patches such as the thruster. Keep (b) as a developer-time lane only, if +at all. Confidence: about 80% on the choice, lower on the details marked [U]. + +The cost of (a): the patch set is fixed at build time. A built player cannot run +player-authored or modded patches; live-edited patches run only in the editor, through (b). + +- (a) fits the thruster: a fixed graph with ordinary controls, which + `docs/faust-toolchain-boundary.md` ("Parameter changes are not recompiles"; "Shipping + builds should prefer Faust-generated native targets") already says is the shipping shape. +- (b) buys live recompilation of `.aqua` text in the player. Nothing asks for that yet. It + costs a ~130 MB LLVM dependency [V: `libLLVM.so.19.1` is 129,673,080 bytes on this host], + the Faust library tree at runtime, and a JIT in the player. +- Faust already ships a Unity target that is path (a) with the bridge written + (`architecture/unity/`, `faust2unity`, section "What Faust ships"). Start from it and do + not hand-roll a plugin. + +## What Faust ships for Unity [V] + +Checked in the 2.90.20 source tree this host built (`faust-llvm-build-20261031/src/architecture/unity/`). + +| File | Role | +|---|---| +| `unity.cpp` (253 lines) | Unity **native audio SDK** effect plugin: `UnityGetAudioEffectDefinitions`, `ProcessCallback`, `SetFloatParameterCallback`. | +| `unityplugin.cpp` (307 lines) | Plain C exports (`Faust_setParameterValue` etc.) for P/Invoke. | +| `FaustPlugin_template.cs` | `MonoBehaviour` on an `AudioSource`; `OnAudioFilterRead` calls `ctx.process(...)`. | +| `FaustUtilities_template.cs`, `FaustPolyUtilities_template.cs` | `DllImport` glue, inspector UI, polyphonic variant. | + +So Faust provides both bridge shapes the question names. The `FaustPlugin_template.cs` +path is the **OnAudioFilterRead bridge**: Unity calls `OnAudioFilterRead(float[] buffer, int +numchannels)` on its audio thread and C# P/Invokes the native `process`. `unity.cpp` is the +**native audio SDK** shape: Unity's mixer calls `ProcessCallback` directly with no managed +frame in the audio path. + +`faust2unity` builds the native libraries and C# scripts and packs a `.unitypackage`; +platform scripts are `faust2winunity` (MinGW cross-compile: `-w32`/`-w64`) and +`faust2linuxunity` (`g++`, `-fPIC -shared`, `.so`) [V: scripts and README read; not run]. + +## Path (a): AOT Faust to C++ in a native plugin + +### Per-platform outputs + +| Platform | Output | Toolchain | Status | +|---|---|---|---| +| Windows x64 | `FaustPlugin_.dll` | `faust2winunity -64` uses MinGW (`x86_64-w64-mingw32-gcc`) [V: script]. An MSVC build of the generated C++ is plausible but not in Faust's scripts. | [U] runs in a Unity Windows player | +| Linux x86_64 | `libFaustPlugin_.so` | `g++ -fPIC -shared` (`faust2linuxunity`) [V: script] | [U] runs in a Unity Linux player | +| Inputs | `thruster.dsp` + `stdfaust.lib` tree | `faust` CLI with `-I` at faustlibraries | [V] | + +Generating the thruster as C++ works [V]: `faust -I faustlibraries-20261031 -a minimal.cpp +-single -ftz 2 -vec -lv 1 thruster.dsp` gave 1,105 lines (40,045 bytes) with Faust 2.90.20. +Those are the same flags `AquaSynthNativeCompiler.CompileSource` passes to the JIT +(`src/AquaSynth.Faust/NativeFaustRuntime.cs:530`). The AOT lane should pick its flags +deliberately, not inherit them: `-vec` is a choice that the measurement plan below should +test against scalar output. + +Windows and Linux builds need two different hosts or a cross-compiler. Starfire (Windows) +has Faust and a compiler only if someone installs them there [U]. + +### What is reusable from AquaSynth + +| Part | Reusable for (a)? | Evidence | +|---|---|---| +| `patches/thruster/thruster.dsp` | Yes, as is. It is hand-written Faust, "not a `.aqua` script" (file header). | [V] | +| `FaustExport` / `FaustEmitter` (`src/AquaSynth.Core/FaustExport.cs`, 2,558 lines) | Only for `.aqua` patches. The thruster does not go through it. For an `.aqua` patch it yields the `.dsp` source to feed `faust -lang cpp`; `DspSourceDirectory` already writes it (`NativeFaustRuntime.cs:628`). | [V] | +| `FaustCompiler.CompileAsync` (`src/AquaSynth.Faust/FaustCompiler.cs:56`) | Yes. Runs the `faust` CLI with `-lang Cpp`; `FaustTargetLanguage.Cpp` exists. This is the AOT generator step. | [V] | +| `NativeFaustRuntime.cs` (1,132 lines: `AquaSynthPatchCompiler`, `AquaSynthStreamingPatch`, `FaustNativeToolchain`) | **Not in the shipped player.** It is the libfaust loader and the managed host. Under (a) it stays on the build/authoring side: it renders reference audio and the manifest that the plugin's output is checked against. | [V] | +| `tools/FaustRender` + `docs/renders/thruster-v5-manifest.json` | Yes, as the golden reference: hashes, peak, RMS, `nonFinite` per rpm case, 48 kHz / 512-frame blocks. The native plugin should be diffed against this. The manifest hashes predate the 2026-11-05 edit to `thruster.dsp` (`docs/faust-toolchain-boundary.md`, "Linux Native Toolchain"), so re-render first. | [V] | +| `AquaSynthNativeManifest` (id, compile key, sample rate, I/O counts, Faust version) | Yes as the artifact provenance record. It has **no parameter table** (no min, max, default). That is a gap for crossing params (below). | [V] | + +Missing for (a), not in AquaSynth today: a build step that runs `faust2unity`-style +generation per patch and records the result as artifacts. That should be a script in +`tools/`, not a new library. Per the boundary doc, AquaSynth owns "generated +source/native artifact layout" and "parameter and bus manifests", and Aquarium/Unity owns +"artifact loading policy for its platform". + +### Hot-path hazards in the managed bridge (if OnAudioFilterRead + managed host) + +If the player used `AquaSynthStreamingPatch` directly (managed host, native `libfaust` +instance), these are in the audio callback [V: `NativeFaustRuntime.cs`]: + +- `ProcessBlock(float[][] ...)` (line 251) allocates `IntPtr[]` arrays and pins six + `GCHandle`s per call. Use the pointer overload (line 235) with preallocated pinned buffers + (what `tools/FaustRender/Program.cs:37-40` already does). +- `SetControls` does a string dictionary lookup per control per block and a `Marshal.WriteInt32` + (lines 841-869). It also throws `ArgumentException` for an unknown path (line 865): an + exception in an audio callback. Resolve zone pointers once, off-thread. +- The `FaustNativeToolchain` delegates are `Marshal.GetDelegateForFunctionPointer` thunks + (line 886), not function pointers; fine for control rate, avoidable per block. + +Under the AOT plugin, none of this exists: the cost moves to the plugin's `setParamValue` +and `compute`. + +### Native audio SDK vs OnAudioFilterRead + +| | Native audio SDK (`unity.cpp`) | `OnAudioFilterRead` bridge (`FaustPlugin_template.cs`) | +|---|---|---| +| Where it runs | Mixer effect, native audio thread, no managed code | Managed callback on the audio thread, then P/Invoke | +| Source/generator | Effect slot on a mixer group; `ProcessCallback` returns `ERR_UNSUPPORTED` if `inchannels != outchannels` (`unity.cpp:179`) [V] | Component on a GameObject with `AudioSource` | +| Params | `SetFloatParameterCallback`, set from mixer exposed params or `AudioMixer.SetFloat` [U] | `setParameter(int, float)` per patch component | +| Risk | Mixer setup per patch; 15-char parameter names (below) | GC pauses or managed allocation can overrun the callback if the managed side allocates [U] | +| Fit for thruster | Better on determinism; whether a generator-only (0-input) effect behaves well in a mixer slot is [U] | Matches Faust's template; many instances are easy (one component each) | + +The thruster has 0 inputs and 1 output. In both shapes a 1-channel Faust output goes to +the first interleaved channel only and zero-fills the rest (`interleavedWrite`, +`architecture/faust/dsp/dsp-tools.h:216-223`) [V]. A stereo player would hear left only +unless the patch is made to output two channels or the plugin is edited. + +## Path (b): runtime `libfaust`/LLVM in the player + +`AquaSynthPatchCompiler` already is this, host-side: `createCDSPFactoryFromString` via +`NativeLibrary.Load` (`NativeFaustRuntime.cs:793`), the C API delegates (727-737), instances +created and computed through `computeCDSPInstance`. + +Per-platform outputs: + +| Platform | Ship | Evidence | +|---|---|---| +| Linux x64 | `libfaust.so.2.90.20` (8.4 MB) + system `libLLVM.so.19.1` (129.7 MB) + `libz3.so.4`, `libffi.so.8`, `libedit.so.2`, `libzstd`, `libz` (all dynamic deps) + `share/faust/*.lib` | [V: `ldd`, `ls`, doc] | +| Windows x64 | `libfaust.dll` plus `share/faust` | [U] no Windows libfaust built or run by this run. `AquaSynthFaustToolchain.LibraryFileNames` looks for `libfaust.dll`/`faust.dll` (`NativeFaustRuntime.cs:654-657`); the doc says `C:\Program Files\Faust` is the default home. | + +Costs specific to AquaSynth's own history, from `docs/faust-toolchain-boundary.md`: +the Linux link first failed (`cannot find -lPolly -lPollyISL -lzstd`), and the prefix +`share/faust` lacked `stdfaust.lib` until the faustlibraries tree was added. The library +tree has to travel with the player (`AquaSynthFaustToolchain.Find` probes +`/Tools/Faust`, line 666). + +Beyond size: + +- A JIT needs executable memory. Desktop Windows/Linux allow it; iOS and consoles do not + [U]. A player that needs those later cannot have (b) as its only path. +- The compile step is multi-hundred-millisecond to seconds and must run on a worker thread + with a finished-DSP handoff (boundary doc, "Runtime Rule"). `AquaSynthNativeManifest.CompileMilliseconds` + records it [V: field exists; no number measured here]. +- Output from a JIT with `-vec` at optLevel `-1` is whatever LLVM emits on that machine. + It is not bit-identical across hosts [U], so the reproducibility invariant in the + boundary doc ("artifacts are reproducible from script, version, target, options") weakens. + +## Licensing if shipped + +Read from files in the tree this run built [V] unless marked. + +| Component | Licence | Ship implication | +|---|---|---| +| Faust compiler / `libfaust` 2.90.20 | LGPL-2.1-or-later (header of `COPYING.txt`: "GNU Lesser General Public License ... version 2.1") | Dynamic loading of an unmodified `libfaust.so/.dll` satisfies LGPL if the user can replace it. Ship licence text and source offer. [U] legal review. | +| Faust architecture files (`unity.cpp`) | LGPL-3.0-or-later **with exception**: you may create a larger work with the architecture section and distribute it under terms of your choice, **so long as the architecture section is not modified** | Use `unity.cpp` / `unityplugin.cpp` unmodified and the rest of the plugin has no copyleft. Editing them (e.g. to add param clamping) drops the exception for that file. Prefer clamping elsewhere (below). | +| Generated C++ from `faust` (the DSP section of the output) | Not governed by the compiler licence per Faust's licence note [U: from Faust FAQ, not checked here] | Verify before shipping. | +| `faustlibraries` (`stdfaust.lib`, used by the thruster via `os`, `fi`, `no`, `si`, `ba`, `ma`) | Per-function: `oscillators.lib`, `noises.lib`, `signals.lib`, `basics.lib`, `maths.lib` headers say LGPL (GRAME); `filters.lib` says "Each function in this library has its own license" (e.g. Julius O. Smith III) | Compiled into the generated code. Need a per-function licence inventory of the functions the thruster calls before shipping. Not done. | +| LLVM (path (b) only) | Apache-2.0 with LLVM exceptions [U: from memory, not in tree] | Permissive; ship notice. Also notices for z3 (MIT), libffi (MIT), libedit (BSD) [U]. | + +Net: (a) puts only unmodified-architecture-file and faustlibraries notices in the player. (b) +adds LGPL relinking obligations for `libfaust` and a third-party LLVM stack. + +## Crossing bounded parameters (rpm now; load/velocity/quality later) + +Today's thruster has one control: +`rpm = hslider("rpm", 1200, 300, 6000, 1) : si.smooth(ba.tau2pole(0.2))` +(`thruster.dsp` line 6). `load` is derived from it (`load = (rpm - 300) / 5700`). The +generated C++ declares it with `addHorizontalSlider("rpm", &fHslider0, 1.2e+03f, 3e+02f, 6e+03f, 1.0f)` +[V]. Later controls (load, velocity, quality) must each be declared the same way, with a +default, min and max. + +### What crosses and how + +- **AOT / `OnAudioFilterRead`**: C# calls `Faust_setParameterValue(ctx, index, float)` + (`FaustUtilities_template.cs:307`), which does `*fZone = v` with no clamp + (`APIUI.h` `setParamValue(int, FAUSTFLOAT)` = `*fItems[p].fZone = v`) [V]. Parameters are + addressed by integer index in declaration order. The `.cs` file prints min/max/init + (`getParameterMin/Max`). +- **AOT / native audio SDK**: `ComputeUnityParameters` fills Unity's + `UnityAudioParameterDefinition` with `min`, `max`, `defaultval` from the Faust UI + (`unity.cpp:116-118`) [V]. Unity then knows the range and the default; the inspector and + mixer use it. **Names are cut to 15 characters** (`strncpy(pdef[i].name, ..., 15)`, + `unity.cpp:112`) [V]. `rpm` fits; Aqua-style paths such as `/curves//depth` do not. + The AquaSynth path lengths need a unique-prefix rule or the control must be + addressed by index. +- **Runtime `libfaust` (b)**: `AquaSynthStreamingPatch.SetControls` writes the float's + bits to the zone pointer with `Marshal.WriteInt32` (`NativeFaustRuntime.cs:868`). No + range or default crosses; `FaustUiGlue.AddControl` discards `init, min, max, step` + (line 1028) [V]. + +Float writes are the only thing crossing. A single aligned 32-bit store is atomic on x86-64, +so no lock is needed for one scalar control [U: assumes the compiler does not tear; the +generated code reads each zone once per block, `float fSlow0 = static_cast(fHslider0)`, +`compute` line 430 in the generated file] [V for the read]. A control that moves within a +block takes effect at the next block, smoothed by `si.smooth` (rpm uses a 0.2 s time +constant). + +### Needed for several bounded params + +1. A parameter table per patch alongside the existing `AquaSynthNativeManifest`: + `{path, index, default, min, max}`. (b) can get it from `FaustUiGlue` if + `AddControl` stops discarding `init/min/max`; (a) can get it from the same + `faust -json` output or from the C++ UI callbacks at build time. +2. One clamp site (below), driven from that table, owned by AquaSynth, not by + Unity code. +3. A normalized-to-range mapping owned by AquaSynth for load/velocity/quality when the game + supplies 0..1, if that is the game's contract. `ControlSurfaceCatalog` already holds + normalized surfaces for `.aqua` (`hslider(path, DefaultNormalized, 0, 1, 0.001)`, + `FaustExport.cs:157`) [V]; the thruster has none. + +## Are param inputs clamped today? + +No, for the thruster path. Layer by layer [V]: + +| Layer | Clamp? | +|---|---| +| `thruster.dsp` | No. Slider min/max are UI metadata. The generated `compute` reads the zone raw (`fSlow0 = fHslider0`). An rpm of 20000 or -5 reaches `si.smooth` and every formula (`shaft = 28 + rpm*0.025`, `load = (rpm-300)/5700`, band centres, drive). | +| `FaustNativeToolchain.WriteControl` (`NativeFaustRuntime.cs:860-869`) | No. Only unknown-path rejection, and `NaN`/infinity written as-is. | +| `AquaSynthStreamingPatch.SetControls` / `ProcessBlock` | No. | +| `tools/FaustRender/Program.cs:51` | No; it only sweeps within its CLI arguments. | +| `NativeFaustRuntimeTests.cs` | No test for out-of-range, NaN, or infinity control values found (grep for clamp/throws/range: no hit). | +| `ControlSplineTimeline` (`src/AquaSynth.Core/ControlSplineTimeline.cs:167-231`) | Yes, but only normalized `[0,1]` for `.aqua` control splines. It is not in the thruster path. | +| Faust `APIUI::setParamValue` (Unity bridge) | No (`*fZone = v`). | +| Unity side | The mixer/inspector clamp to the declared `min/max` in the native SDK path [U]. A script calling `setParameter` or `SetFloat` directly is not clamped by Faust's glue [V]. | + +The final `tanh` and the 0.425 trim (`thruster.dsp` last line) bound the output. Peak at +rpm 4800 is 0.36 (`docs/renders/thruster-v5-manifest.json`) [V]. Whether out-of-range rpm stays +finite and bounded is **not measured**: the manifest covers 300..6000 only. Hence +`nonFinite` coverage in the plan below includes out-of-range and non-finite inputs. + +Recommended clamp site: in the AquaSynth-owned manifest/host layer (for (a), a small +wrapper in the managed script that Faust's unmodified architecture files sit under; for (b), +`WriteControl`), clamping to the declared min/max and rejecting NaN. Do not edit +`unity.cpp`/`APIUI.h` (loses the architecture exception). The DSP itself can also carry the +bound: `hslider(...) : max(300) : min(6000)` in the `.dsp` is the one place both +lanes would share. That is a patch edit and a design choice for the thruster's owner, not +decided here. + +## Callback-deadline measurement plan (rerunnable) + +Goal: show, per path and platform, that `compute` fits the audio callback with margin, +including at the worst rpm sweep, under CPU load. Nothing here has been run. + +**Deadline.** `D = frames / sampleRate`. Unity's block comes from `AudioSettings.GetDSPBufferSize` +(`FaustPlugin_template.cs` `getBufferSize`). Test 256, 512, 1024 frames at 44.1 k and +48 k (the render manifest uses 48 k / 512, i.e. D = 10.67 ms). Proposed pass line: +p99.9 under 25% of D, max under 50% of D (Aqua's ruling, 2026-11-07). The spike's +shared-host outliers (max 10.8-17.1 ms, 4-5 blocks over deadline) are **unexplained**: the spike +(`spikes/faust-aot-thruster/`) shows they are not per-block DSP cost (an empty timed busy-wait stalls +the same way), but not what causes them. Until a quiet or realtime host explains or removes them, the +max line has no verdict from this host. + +**What the spike measures, and what it does not.** It times a bare C loop calling the compiled +`process` back to back on Linux. That shows the patch is cheap (median 0.05 ms against 10.67 ms). It +does not show that a Unity player meets its deadline: the player's audio thread, the managed-to-native +transition, IL2CPP, mixer and other DSP sharing the callback, and Windows scheduling are all absent. + +**What to time.** + +1. `compute(count, ...)` alone, pure C++ (AOT): build `thruster.cpp` with Faust's + `architecture/bench.cpp` or a 30-line driver; `std::chrono::steady_clock` around each + `compute`; 200,000 blocks; discard the first 1,000. [V: `bench.cpp` exists in + `architecture/`; not run.] +2. The whole plugin callback: `ProcessCallback` or `OnAudioFilterRead` including + `interleavedWrite` and the parameter write; timing taken inside the plugin with a + preallocated ring of timestamps, no logging in the callback; dumped on unload. +3. Managed host (b): `FaustRender`-style loop over `AquaSynthStreamingPatch.ProcessBlock` + with the pointer overload, `Stopwatch.GetTimestamp` around each call, GC allocation count + (`GC.GetAllocatedBytesForCurrentThread`) asserted zero across the loop. + +**Inputs.** The rpm cases from the manifest (600, 4800, sweeps) plus 300 and 6000 steady, a +full 300->6000->300 ramp changing rpm every block, and the out-of-range set +(`-1e9`, `0`, `1e6`, `NaN`, `+Inf`) with finite-and-bounded output asserted. Add the +rpm-and-load product of any new controls. Include denormal check (`-ftz 2` build vs without). + +**Conditions.** Idle host and with the Starfire load other Minds create (record +`nvidia-smi` and `query user` before and after; she wants it saturated, but a GPU hang resets +her display, so the run is CPU-only); one thread pinned; power plan noted; Debug and Release +for managed pieces. Linux on Yggdrasil or this host, Windows on Starfire over `ssh starfire` +(not run, and it is Meta's shared machine, so one job at a time). + +**Variants to compare**, same inputs: AOT `-vec` vs scalar; AOT `-O2` vs `-O3 -march=x86-64-v2` +(avoid `-march=native`, which the player's CPU may not match); JIT optLevel `-1` vs 3 [U: flag +meaning]. First-callback and instantiation time recorded separately from steady state. + +**Output.** One JSON per run: host, OS, CPU model, Faust version, compile flags, block size, +sample rate, n, min/p50/p99/p99.9/max in microseconds, % of D, overrun count (`t > D`), peak, +rms, `nonFinite`, `dspSha256`. Same shape and location as `docs/renders/thruster-v5-manifest.json` +so the existing hashes can be compared. A script for it belongs in `tools/` next to +`FaustRender`. + +**In the real player.** Unity does not expose callback timing directly. Time inside +the plugin as above, and read `AudioSettings` underrun counters / Profiler `Audio` markers +[U: exact counter names] over a 10-minute run in a development build on each OS. + +## Unverified list, in one place + +- That a built player can run anything but the patches compiled into it: player-authored or + modded patches are out of reach by construction under (a); live edit needs the editor and (b). + +- That the generated Windows `.dll` and Linux `.so` load and play in a Unity Windows/Linux + player (no Unity available in this run). +- `faust2winunity` end-to-end on any host (MinGW not checked). +- Behaviour of a 0-input generator in a mixer effect slot; mixer parameter exposure. +- Generated-code licence position, LLVM/z3/libffi/libedit licences, and a per-function + inventory of `faustlibraries` licences for the thruster's calls. +- A Windows `libfaust.dll` build and its dependencies. +- Compile latency in milliseconds for the thruster (the field exists, no number taken). +- Out-of-range / non-finite rpm behaviour of the thruster output. +- Atomicity of the float zone write across a block boundary in a real plugin. +- Unity's DSP buffer size defaults and underrun counter names. diff --git a/docs/listening/tract-weak-spots-1-analysis-20261113.md b/docs/listening/tract-weak-spots-1-analysis-20261113.md new file mode 100644 index 0000000..cf583c2 --- /dev/null +++ b/docs/listening/tract-weak-spots-1-analysis-20261113.md @@ -0,0 +1,76 @@ +# tract-weak-spots-1: why the predictions failed, and what was changed + +The verdict is `tract-weak-spots-1-meta-20261113.md`. Every falsifiable prediction in the kit failed although the formant numbers +behind them were right. The kit measured the steady part of each vowel (35-85 % of the clip) and the peak of each burst; the faults +Meta heard sit in the first 100 ms of every clip, at the phone boundaries, and in how long the burst lasts. Nothing in the old +measurement set looked there. Everything below was measured on the clips Meta heard (`weak-kit-out/clips`, WordRender at 140 Hz) before +any change, and again after. + +## Causes found (measured, not guessed) + +| # | what she heard | cause | evidence (before) | +|---|---|---|---| +| 1 | nibu: "both consonants are just clicks"; leak-before: "reversed percussive hit" | The merged control curve of a surface holds the previous phone's value to the boundary and steps to the next phone's first value there (two points at one time). A step in a ladder cell's area is a one-sample transient. | Pitch-period window peaks: 0.399 at 95 ms and 0.487 at 275 ms, against vowel pulses of ~0.1-0.2; both instants are phone boundaries of the script (`start=0.095`, `0.275`). The whole-render peak the kit reported (0.487) was this click. Click ratio 3.54 (`ear_metrics.py`). The old report called 0.487 vs 0.399 a "leak" difference. | +| 2 | every clip starts as a fade-in; the "kazoo" with "no audible closure" (ba, no burst); the /ɪ/ not like "bit" | `si.smoo` starts every control slider at 0. A word began with the tract shut, the vowel weights at 0 and the source off, and climbed for ~100 ms. The 140 ms "closure" of ba was largely this climb. | Isolated /ɪ/: level -66 dB at 0 ms, -45 at 40 ms, -30 at 100 ms, -28 from 140 ms; LPC bandwidth of F1 367 Hz at 0 ms, 232 at 60 ms, 103 at 100 ms, 90 from 140 ms. 100 of the clip's 309 ms were the climb; the steady-state window (35-85 %) started after it. | +| 3 | ba with burst: "fah then bah" | The stop opened over 0.65 to 1.0 of its gesture: 33 ms of a 95 ms /b/. A burst of that length is a fricative. | Burst-notes: ba burst 0.0707-0.1039 s (33 ms). A recogniser reads the clip as `v ɑ` (with the burst), `b ɑ` without. | +| 4 | nasals: "basketball buzzer, no audible articulation", /n/ and /m/ alike | The murmur had no entry and exit transitions (the step of cause 1) and began after the climb of cause 2; the machine ear read /n/ as /m/ (`v ɑ m ɑ`). | recogniser, before: ana -> `v ɑ m ɑ`. | +| 5 | bun with burst: "a truck releasing its pneumatics" | cause 3 (long burst), same mechanism. | burst 0.0721-0.0998 s (28 ms). | + +## What was changed (PR "phone transitions", this branch) + +1. `PatchScript.WithPhoneTransition`: every authored gesture spline starts, when an earlier gesture left the surface at another value, + with a 30 ms ramp from the previous value (at most 0.4 of the gesture, and before its second point). The first phone of a word and + surfaces no earlier phone touched are unchanged. Test: `PhoneTransitionTests.NoSurfaceStepsAtAPhoneBoundary` (worst step in 1 ms + around each boundary of nibu: below 0.03; before, 0.77: the vowel's 1 against the nasal's released 0.23). +2. A stop holds all its articulators shut until 12 ms before its end and opens in those 12 ms (`StopReleaseSeconds`, a design value); + it used to open over 0.65 to 1 of its gesture. Test: `AStopOpensInTwelveMillisecondsNotAThirdOfItsGesture`. +3. `smoo_init` replaces `si.smoo` on every control-surface and spline-point slider of the emitted DSP: the smoother starts at its input + and keeps si.smoo's pole (a 22.7 ms time constant), so live control moves as fast as before. Patch parameters keep `si.smoo`. The + source's own 10 ms pitch-ratio smoother starts at its input too. Tests: `ControlSmoothersStartAtTheirInputNotAtZero` (text) and + `ASmootherStartsAtItsInputAndMovesAtSiSmoosPace` (the emitted smoother, rendered). +4. The word scaffold's vocal envelope gets a 12 ms attack (it was 0, so the now-instant start was itself a click: first-sample peak 0.22). +5. The ramp into a stop (item 1) sweeps the contact through every aperture between open and shut, and the exporter's burst noise is + `4 * opening * (1 - opening)` times a gain that was held through the whole gesture: each closing ramp was a 15-20 ms hiss before the + stop (>4 kHz share of the 6 ms frames at 188 ms of /ata/: -1 dB, against -57 dB with the closure stepped). The burst gain now exists + only from the stop's release start (`PatchScript.StopReleaseStartSeconds`, the one owner of the release time), one ramp either side. + A stop's lips are shut (aperture 0), not at 0.04: the word-initial /b/ voice bar read -19.1 dB re the vowel after it and /p/ -30.1 + against the design -34 to -26 and -60 to -38; shut they read -27.7 and -38.8. Spline handle times follow their segment (a third of + it), so the C# timeline and the DSP agree on the 12 ms opening (the lowering ignores handle times). Tests: + `ClosingIntoAStopMakesNoHighFrequencyNoise`, `TheDspRendersTheStopOpeningAsTheTimelineEvaluatesIt`, `PlosiveClosureCarriesTheVoiceBarOnlyWhenVoiced`. +6. `tools/formant-check/ear_metrics.py` (click ratio, largest sample step, LPC bandwidths, HNR, energy above 4 kHz) and + `tools/formant-check/phone_probe.py` (a speech recogniser as a machine ear) so these faults are measured, not heard first by Meta. + +### After (same words, same 140 Hz, `fix-out-c`) + +| word | before | after | +|---|---|---| +| nibu_flat | click ratio 3.54, peaks 0.399 / 0.487; recogniser `i b u` | peaks at the boundaries 0.077 / 0.089; click ratio 1.79 (the vowel's own pulses); median sample step in the /b/ closure 0.0007 (0.0005 before; 0.0161 with the ramp hiss); recogniser `n i l t uː` as read before the closure-noise and shut-lips fixes (not re-run: no torch on this host; the /n/ is back, the /b/ reads as `l t`: see below) | +| ba (burst) | recogniser `v ɑ`; burst 33 ms | `b ɑ`; opening 12 ms | +| ba (no burst) | `b ɑ` | `m ɑ` | +| ana / ama | `v ɑ m ɑ` / `v ɑ m ɑ` | `ɑ n ɑ` / `ɑ m ɑ` | +| isolated /i/ | `h iː`; level climb 100 ms | `i`; level within 3 dB of the steady value by 20 ms | +| isolated /ɪ/ | `h i` | `ʔ i` | + +## Not fixed, and why (open until she listens) + +- **Voice quality: "kazoo with a two-pack-a-day smoking habit" (the two clips she kept), "reedy, like Pink Trombone" (/ʊ/), "basketball + buzzer" (nasals).** Her comments on the two keeps say the vowel is right and the voice is wrong. Candidates, none tested by ear: + (a) the glottal source is a band-limited saw through a one-pole lowpass with no closed phase (`FaustExport.PrimitiveSourceExpression`), + where a Rosenberg/LF pulse has the open and closed phases real fold flow has; (b) harmonics-to-noise ratio of the steady part is 13-15 dB + (healthy voices ~20+), from the source's `0.08 * noise` term and the saw's aliasing; (c) LPC bandwidths of the steady vowel are 90 Hz (F1), + 130 (F2), 100 (F3) for /ɪ/: plausible, so the wall-loss hypothesis is weak (the F2 of /ɑ/, 29 Hz, and the F1 of /i/, 14-24 Hz, are LPC + artefacts of a pole sitting on a harmonic). Settling this needs a listening kit that varies one of them at a time; the next kit does. +- **/ɪ/ as the "eu" of cordon bleu.** The recogniser cannot judge this tract's vowel quality (every isolated vowel came back /i/). The + confirmed fault in that clip is cause 2: a third of it was a muffled climb through bandwidths of 300 Hz. Whether the steady /ɪ/ then + reads as "bit" is for her ear; lax vowels in isolation and without vowel-inherent spectral change are known to be misheard + (Hillenbrand et al. 1995, Strange et al. 1983), so the next kit also carries /ɪ/ in a word. +- **The medial /b/ of nibu reads as `l t` to the recogniser after the fix, where it read `b` with the click and the long burst.** The closure + voice bar sat 10 dB under the vowel (a murmur-like level; a stop's runs 20+ dB under; 16-19 dB with the lips shut), and a labial burst is injected at the glottal + end in a word with no other plosive (`FaustExport` burst injection), so it is filtered by the whole tract like aspiration. Both are + next, with the listening to decide. + +## What the old measurements missed, as a rule + +A steady-state window measures a steady state. Faults of onset, boundary and duration need metrics that look at onsets, boundaries +and durations: click ratio and step size at each phone boundary, time-to-full-level, burst length, bandwidths, HNR. These now exist +(`ear_metrics.py`) and the click and onset ones are pinned by tests. diff --git a/docs/listening/tract-weak-spots-1-meta-20261113.json b/docs/listening/tract-weak-spots-1-meta-20261113.json new file mode 100644 index 0000000..9504d9f --- /dev/null +++ b/docs/listening/tract-weak-spots-1-meta-20261113.json @@ -0,0 +1,231 @@ +{ + "schema": "aqua.listen.verdict.v1", + "kit": { + "id": "tract-weak-spots-1", + "title": "Articulatory vocal tract: stress, release, nasals, lax vowels" + }, + "exportedAt": "2026-11-13T11:27:36.539Z", + "seed": 1326320895, + "consent": true, + "listener": "Meta", + "device": null, + "notes": "", + "summary": { + "total": 14, + "keep": 2, + "fix": 12, + "drop": 0, + "unanswered": 0 + }, + "clips": [ + { + "position": 1, + "id": "lax-ih", + "title": "isolated /ɪ/ 180 ms, 140 Hz. Measured: Burg F1/F2 375/2024 Hz; tube chain 346/1987 (+8.5/+1.8 %); Hillenbrand 1995 male 429/2034 (-12.5/-0.5 %); Peterson & Barney 392/1993 (-4.3/+1.5 %). Present in the DSL table and renderable. PREDICTION: heard as the vowel of 'bit' (or a slightly high, 'ee'-leaning version of it). FALSIFIED if heard as 'ee' of 'beet', as 'eh' of 'bet', or as a different phone.", + "group": "lax", + "feel": "the short vowel in 'bit'", + "file": "v_ih.wav", + "pair": null, + "slot": null, + "verdict": "fix", + "reason": "It's more like the eu in cordon bleu than the i in bit ", + "landed": 2, + "device": null + }, + { + "position": 2, + "id": "burst-ba-on", + "title": "ba with the plosive burst (gain 0.06, a design value). Measured: burst alone peaks 0.0142 at 85 ms = -24.0 dB re vowel peak, falls to a tenth in 17 ms, burst rms in the 30 ms after its peak -31.0 dB re vowel rms. PREDICTION: a slightly crisper, more 'b'-like onset than B/off; the burst is quiet and short so the difference is small, 'a little pop' at most. FALSIFIED if it sounds like a hiss, a click, or a separate noise after the vowel started, or if no difference is heard.", + "group": "burst", + "feel": "the syllable 'bah' with a clean, crisp 'b' at the start", + "file": "ba.wav", + "pair": "burst-ba-off", + "slot": "A", + "verdict": "fix", + "reason": "More like fah then bah but you can hear something there", + "landed": 2, + "device": null + }, + { + "position": 3, + "id": "burst-ba-off", + "title": "ba with the release_flow splines dropped (WordRender --no-burst). Measured: closure peak 0.0078 (voice bar), release (voicing breaking through)peak 0.1087 = -6.3 dB re vowel peak 0.2247, no burst noise. PREDICTION: a soft, voiced 'b' with the vowel starting smoothly; may sound slightly 'w'-like or lack the pop. FALSIFIED if listeners cannot hear any difference from the burst twin (then burst gain 0.06 is inaudible).", + "group": "burst", + "feel": "the syllable 'bah' with a soft start", + "file": "ba_noburst.wav", + "pair": "burst-ba-on", + "slot": "B", + "verdict": "fix", + "reason": "There is no audible constriction. While the vowel is recognizable, it still sounds like a kazoo", + "landed": 2, + "device": null + }, + { + "position": 4, + "id": "stress-nibu-flat", + "title": "nibu, no stress marks. Measured: /i/ 150-250 ms rms vs the stressed render +0.21 dB; unstressed vowel 180 ms, f0 139.9 Hz. PREDICTION: reads asan even two-syllable word with no strong first syllable. FALSIFIED if listeners hear it as stressed on a syllable or cannot tell it from the stressed twin.", + "group": "stress", + "feel": "a made-up two-syllable word, 'nee-boo', both syllables even", + "file": "nibu_flat.wav", + "pair": "stress-nibu", + "slot": "A", + "verdict": "fix", + "reason": "These both sound like 'ee'oo, with both consonants you're going for being just clicks", + "landed": 2, + "device": null + }, + { + "position": 5, + "id": "stress-nibu", + "title": "nibu with primary stress on /i/ (NI-bu). Measured: /i/ 247 ms vs 180 ms (+75 ms; design +68), f0 148.1 vs 139.9 Hz (x1.059), amplitude factor 1.30; level of /i/ in the 150-250 ms window only +0.21 dB (five-vowel mean +1.93 dB, per vowel +1.2 to +3.0). PREDICTION: first syllable heard as the prominent one, mostly through length and pitch, little through loudness; B should be picked as 'stressed on the first syllable'. FALSIFIED if it sounds no more prominent than the flat twin, if it sounds like a question or a pitch accent rather than stress, or if the level is what is heard (then the f0-scatter question is wrong).", + "group": "stress", + "feel": "the same made-up word, with the first syllable stronger: 'NEE-boo'", + "file": "nibu.wav", + "pair": "stress-nibu-flat", + "slot": "B", + "verdict": "fix", + "reason": "", + "landed": 2, + "device": null + }, + { + "position": 6, + "id": "stress-i-flat", + "title": "isolated /i/ 180 ms at 140 Hz, no stress. Measured: level 100-170 ms is the reference. PREDICTION: plain short vowel. FALSIFIED if it already sounds emphasised.", + "group": "stress", + "feel": "a single short 'ee' vowel, plain", + "file": "v_i.wav", + "pair": "stress-i", + "slot": "A", + "verdict": "keep", + "reason": "Still spoken by a kazoo with a two pack a day smoking habit, but it's an ee", + "landed": 4, + "device": null + }, + { + "position": 7, + "id": "stress-i", + "title": "isolated /i/ with primary stress: 247 ms, f0 x1.06, amplitude 1.30. Measured against the flat /i/: +1.22 dB total (f0 rise alone -0.80 dB, the amplitude and the rest +2.02 dB). PREDICTION: heard as longer and a little higher; the 1.2 dB louder is under what a listener can judge, so 'longer and higher' is the answer, 'louder' is not. FALSIFIED if it sounds the same length and pitch, or clearly louder.", + "group": "stress", + "feel": "the same 'ee' vowel, held longer and said with emphasis", + "file": "v_i_s.wav", + "pair": "stress-i-flat", + "slot": "B", + "verdict": "keep", + "reason": "", + "landed": 4, + "device": null + }, + { + "position": 8, + "id": "lax-oo-short", + "title": "isolated /ʊ/ 180 ms, 140 Hz. Measured: Burg F1/F2 486/894 Hz; tube chain 478/850 (+1.7/+5.2 %); Hillenbrand 469/1123 (+3.7/-20.4 %); Peterson &Barney 437/1023 (+11.3/-12.6 %). Present in the DSL table and renderable. PREDICTION: heard as the vowel of 'book', probably darker or rounder than a natural one (F2 20 % under the population mean, the back-vowel gap). FALSIFIED if heard as 'oo' of 'boot', 'o' of 'boat' or 'uh' of 'but'.", + "group": "lax", + "feel": "the short vowel in 'book'", + "file": "v_oo_short.wav", + "pair": null, + "slot": null, + "verdict": "fix", + "reason": "It's distinguishable, but it strongly has that reedy quality which I associate with pink trombone and that new one I shared", + "landed": 3, + "device": null + }, + { + "position": 9, + "id": "leak-nibu-flat-after", + "title": "nibu (flat) after the fix: release_flow is zero outside its own plosive. Same audio as stress-nibu-flat, kept as its own clip so the before/after pair is one-to-one. Measured: leak 0.0 from 275 ms on, burst of the b itself unchanged (bin 0.0226 at 81 ms before and after). PREDICTION: clean hum into thevowel, no noisy onset; preferred over 'before' if the leak is audible. FALSIFIED if 'before' is preferred or no difference is heard.", + "group": "release-leak", + "feel": "a made-up word that should start with a clean, smooth hum into the vowel: 'nee-boo'", + "file": "nibu_flat_after.wav", + "pair": "leak-nibu-flat-before", + "slot": "A", + "verdict": "fix", + "reason": "Just slightly less reverse attack on this one", + "landed": 2, + "device": null + }, + { + "position": 10, + "id": "leak-nibu-flat-before", + "title": "nibu (flat) rendered at main c0e6b98, before the release_flow fix. Measured: burst-gain leak vs the fixed render, peak 0.0688 and rms 0.0067 inthe leading /n/ (0-95 ms; the /n/ itself is rms 0.0058, so the hiss is louder than the murmur), peak 0.0504 over /i/ onset 95-150 ms, 0.0019 after; whole-render peak 0.487 vs 0.399. PREDICTION: an audible hiss or click as the /n/ opens into the vowel, the first sound of the word being a noisy 'sn'-like or 'tsh' onset instead of a clean nasal hum. FALSIFIED if the two are indistinguishable (then the leak is inaudible and the fix is cosmetic).", + "group": "release-leak", + "feel": "a made-up word that should start with a clean, smooth hum into the vowel: 'nee-boo'", + "file": "nibu_flat_before.wav", + "pair": "leak-nibu-flat-after", + "slot": "B", + "verdict": "fix", + "reason": "'Ee'oo with some odd clicks that sound more like a reversed percussive hit than a consonant", + "landed": 2, + "device": null + }, + { + "position": 11, + "id": "nasal-n", + "title": "/ɑnɑ/. Measured: murmur -9.64 dB re the flanking vowels (2.17 dB above /m/; the gap is 2.01/2.17/2.05/1.61 dB at f0 110/140/170/200 Hz, so not harmonic sampling), Burg N1 332 Hz, bands 38.4/28.2/16.8/0.9 dB. PREDICTION: heard as 'ah-nah', a tongue-tip hum, a shade stronger than the /m/ one; the 2 dB isnot expected to be noticed. FALSIFIED if heard as 'ama' or 'anga', if it sounds much louder than the /m/ clip, or if there is hiss.", + "group": "nasal", + "feel": "'ah-nah': the tongue touches behind the teeth for a soft hum between two open vowels", + "file": "ana.wav", + "pair": null, + "slot": null, + "verdict": "fix", + "reason": "It just sounds like a basket ball buzzer, no audible articulation", + "landed": 2, + "device": null + }, + { + "position": 12, + "id": "burst-bun-off", + "title": "bun, /bʊn/, no burst. Measured: closure peak 0.0043, release peak 0.1074 = -5.9 dB re vowel peak 0.2126. PREDICTION: soft 'b' then a short backvowel then a nasal; the final /n/ is a clean hum (no leak since the fix). FALSIFIED as for burst-ba-off.", + "group": "burst", + "feel": "a short word like 'bunn' or 'boon' said quickly, soft start", + "file": "bun_noburst.wav", + "pair": "burst-bun-on", + "slot": "A", + "verdict": "fix", + "reason": "Oou'mou is what this sounds closest to, with the apostrophe representing a clicking sound that's in there", + "landed": 2, + "device": null + }, + { + "position": 13, + "id": "burst-bun-on", + "title": "bun, /bʊn/, with burst. Measured: burst peak 0.0130 at 81 ms = -24.3 dB re vowel peak, tenth in 15 ms; tail leak after the fix 0.0 (was 0.0126 at the end before). PREDICTION: slightly crisper onset than off; final /n/ clean. FALSIFIED as for burst-ba-on.", + "group": "burst", + "feel": "a short word like 'bunn' or 'boon' said quickly, crisp 'b' start", + "file": "bun.wav", + "pair": "burst-bun-off", + "slot": "B", + "verdict": "fix", + "reason": "The main difference seems to be a sort of air release added to the beginning which resembles more a truck releasing its pneumatics than a human", + "landed": 2, + "device": null + }, + { + "position": 14, + "id": "nasal-m", + "title": "/ɑmɑ/. Measured: murmur -11.81 dB re the flanking vowels (design band -16..-8), Burg N1 295 Hz, band levels 0-400/400-800/800-1600/1600-3200 Hzof the murmur 36.3/23.6/21.3/2.7 dB. PREDICTION: heard as 'ah-mah', a lip-closed hum between the vowels, clearly softer than the vowels. FALSIFIED if heard as 'abah' or 'awah', if the hum is as loud as the vowels, or if there is a gap or click.", + "group": "nasal", + "feel": "'ah-mah': lips close for a soft hum between two open vowels", + "file": "ama.wav", + "pair": null, + "slot": null, + "verdict": "fix", + "reason": "That m is almost coming through, still more of a basketball buzzer", + "landed": 2, + "device": null + } + ], + "pairs": [ + { + "a": "burst-ba-on", + "b": "burst-ba-off", + "preferred": "burst-ba-on" + }, + { + "a": "stress-nibu-flat", + "b": "stress-nibu", + "preferred": "no-difference" + } + ], + "_truncated": "the mail reader cut the body at 12000 characters inside the pairs list; pairs after the third are not recorded" +} \ No newline at end of file diff --git a/docs/listening/tract-weak-spots-1-meta-20261113.md b/docs/listening/tract-weak-spots-1-meta-20261113.md new file mode 100644 index 0000000..5f063cc --- /dev/null +++ b/docs/listening/tract-weak-spots-1-meta-20261113.md @@ -0,0 +1,32 @@ +# Listening verdict: tract-weak-spots-1 (page tract-listen-1b6d180) + +Listener: Meta (operator), 2026-11-13. Exported 11:27Z by the page (`aqua.listen.verdict.v1`, seed 1326320895), pasted in +#aquasynth (truncated after clip 5) and emailed to aqua@minds.gamecult.org at 11:30Z; the data here is the email, +parsed whole by `mind-mail read` except the tail: the mail reader cut the body at 12000 characters inside `pairs`, so +only the first two pair preferences survive. Raw data: `tract-weak-spots-1-meta-20261113.json`. Totals: 2 keep, 12 fix, 0 drop. +`position` is the page's random order for this listener; ids are the kit's. + +| # | clip | verdict | what she heard | +|---|---|---|---| +| 1 | `lax-ih` (`v_ih.wav`) | fix | It's more like the eu in cordon bleu than the i in bit | +| 2 | `burst-ba-on` (`ba.wav`) | fix | More like fah then bah but you can hear something there | +| 3 | `burst-ba-off` (`ba_noburst.wav`) | fix | There is no audible constriction. While the vowel is recognizable, it still sounds like a kazoo | +| 4 | `stress-nibu-flat` (`nibu_flat.wav`) | fix | These both sound like 'ee'oo, with both consonants you're going for being just clicks | +| 5 | `stress-nibu` (`nibu.wav`) | fix | (no comment) | +| 6 | `stress-i-flat` (`v_i.wav`) | keep | Still spoken by a kazoo with a two pack a day smoking habit, but it's an ee | +| 7 | `stress-i` (`v_i_s.wav`) | keep | (no comment) | +| 8 | `lax-oo-short` (`v_oo_short.wav`) | fix | It's distinguishable, but it strongly has that reedy quality which I associate with pink trombone and that new one I shared | +| 9 | `leak-nibu-flat-after` (`nibu_flat_after.wav`) | fix | Just slightly less reverse attack on this one | +| 10 | `leak-nibu-flat-before` (`nibu_flat_before.wav`) | fix | 'Ee'oo with some odd clicks that sound more like a reversed percussive hit than a consonant | +| 11 | `nasal-n` (`ana.wav`) | fix | It just sounds like a basket ball buzzer, no audible articulation | +| 12 | `burst-bun-off` (`bun_noburst.wav`) | fix | Oou'mou is what this sounds closest to, with the apostrophe representing a clicking sound that's in there | +| 13 | `burst-bun-on` (`bun.wav`) | fix | The main difference seems to be a sort of air release added to the beginning which resembles more a truck releasing its pneumatics than a human | +| 14 | `nasal-m` (`ama.wav`) | fix | That m is almost coming through, still more of a basketball buzzer | + +Pairs: `burst-ba-on` preferred over `burst-ba-off`; `stress-nibu-flat` vs `stress-nibu`: no difference. +(`leak-nibu-flat-before`/`after` and the bun pair carry no recorded preference; she heard "just slightly less reverse +attack" on the fixed one, the pneumatic-truck "air release" on the bun burst.) + +Every prediction that was falsifiable failed, though the formant numbers behind them were measured correctly. The +two keeps are the isolated /i/ pair, and even those were "a kazoo with a two pack a day smoking habit". Analysis and +the fixes it led to: `tract-weak-spots-1-analysis-20261113.md`. diff --git a/docs/renders/aqua-feel-2026-11-05.md b/docs/renders/aqua-feel-2026-11-05.md new file mode 100644 index 0000000..020ba01 --- /dev/null +++ b/docs/renders/aqua-feel-2026-11-05.md @@ -0,0 +1,46 @@ +# aqua-feel first run, 2026-11-05 + +Nothing here was heard. These are metrics from offline renders (faustwasm, mono, 48 kHz, 3 s per patch); they are evidence, +not verdicts. `tools/aqua-feel/README.md` says what each number is and where it breaks. + +Before: `patches/` at `origin/main` d8c625c under the same rules: the gate fails on 3 items (PRC SYNTH1: overs, true peak, +loudness; sfxr golf: true peak, loudness, low motion; sfxr explosion: low motion). After: PRC SYNTH1 gain trimmed 1.4 to +0.45 (a uniform gain: crest and centroid are unchanged), and `thruster.dsp` mix edited (below). Gate after: pass, 4 waivers +(golf x3, explosion x1), 1 warning (golf, +0.9 dBTP). + +## prc-synth1-calibrated + + samplePeak 2.10 -> 0.67 -1.42 + truePeakDb 6.44 -> -3.41 -9.86 + overSamples 18633 -> 0 -18633.00 + loudnessLufs -0.67 -> -10.53 -9.86 + crestDb 6.67 -> 6.67 +0.00 + centroidMedianHz 794.44 -> 794.44 +0.00 + noiseToToneDb -17.18 -> -17.18 +0.00 + noiseToToneADb -19.14 -> -19.14 +0.00 + onsetMs 0.13 -> 0.13 +0.00 + attackMs 0.33 -> 4.50 (the metric changed to a 150 ms window with a held envelope during review; the patch did not) + lowMotion false -> false + +The shipped file's `patch gain=1.4` is the constant the parity probe in `Dx7ReferenceParityTests` writes; the probe asserts +RMS ratio against Dexed, so the test is untouched and still builds its own script at 1.4. Only the shipped library patch +carries the trim. Whether 0.45 is the right level for a listener is unchecked. + +## thruster + +Edits to `patches/thruster/thruster.dsp` (mirrored in `tools/aqua-trial/thruster-trial.dsp`): rasp gain `0.3 + 1.5*load` to +`0.05 + 1.75*load`; rattle floor `0.1 + 0.9*load^2` to `0.015 + 0.985*load^2`; rumble low-pass `60 + 2.5*shaft` to +`60 + 2.5*shaft + 140*(1-load)`. At load 1 (rpm 6000) every term equals its old value (rasp 1.8, rattle 1, low-pass 60 + 2.5*shaft). +Decomposition at rpm 300 before the edit (A-weighted noise-to-tone, dB): full mix +21, rasp removed +20.5, rattle removed ++13.0, both removed +12.5. The rattle modes, not the rasp, were most of the audible-band noise at low rpm; with both gone a +floor of +12.5 dB remains (the pitch-wandering saw smears its harmonics, which the line detector counts as noise). + + rpm A-weighted noise-to-tone dB (before -> after) loudness LUFS centroid Hz + 300 20.7 -> 13.1 -29.5 -> -30.2 2388 -> 752 + 600 23.0 -> 5.0 -28.0 -> -28.7 2531 -> 1228 + 1200 20.1 -> 12.7 -25.4 -> -26.1 2913 -> 2114 + 4800 10.3 -> 9.7 -17.3 -> -17.4 4987 -> 4914 + +The centroid falls a lot at low rpm: the sound is darker as well as less noisy. Whether that is "more tone" to a listener or +"muffled" is exactly what this meter cannot say. The listening trial (`tools/aqua-trial`) now sweeps a narrower noise range at +rpm 600 (see its README). diff --git a/docs/renders/aqua-feel-native-diff-2026-11-10.json b/docs/renders/aqua-feel-native-diff-2026-11-10.json new file mode 100644 index 0000000..35b0987 --- /dev/null +++ b/docs/renders/aqua-feel-native-diff-2026-11-10.json @@ -0,0 +1,278 @@ +{ + "libfaustWasm": "2.90.19", + "sampleRate": 48000, + "items": "core", + "smoother": "library", + "rows": [ + { + "id": "thruster@rpm300", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.026072267442941666, + "maxErrAt": 142707, + "rmsErr": 0.002385837227503057, + "relRmsErrDb": -28.770505442344437, + "wasm": { + "samplePeak": 0.14264661073684692, + "truePeakDb": -16.90120405295226, + "loudnessLufs": -30.226833288468896, + "centroidMedianHz": 751.9185607592701, + "overSamples": 0, + "rmsDb": -23.676437109698263, + "crestDb": 6.761666256511239, + "noiseToToneDb": -6.937988717513285, + "noiseToToneADb": 13.092512355240387, + "lowMotion": false + }, + "native": { + "samplePeak": 0.14242461323738098, + "truePeakDb": -16.915261179402926, + "loudnessLufs": -30.216031113774466, + "centroidMedianHz": 751.528437557621, + "overSamples": 0, + "rmsDb": -23.670746205832152, + "crestDb": 6.74244718138185, + "noiseToToneDb": -6.888378522251635, + "noiseToToneADb": 13.056342933565631, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "thruster@rpm600", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.049423644319176674, + "maxErrAt": 142945, + "rmsErr": 0.004812282166272209, + "relRmsErrDb": -23.51217894411906, + "wasm": { + "samplePeak": 0.15958738327026367, + "truePeakDb": -15.926835268189564, + "loudnessLufs": -28.69474712970973, + "centroidMedianHz": 1227.9568144573402, + "overSamples": 0, + "rmsDb": -22.840558133782523, + "crestDb": 6.900529208325618, + "noiseToToneDb": -0.7035305515542589, + "noiseToToneADb": 5.044756311984671, + "lowMotion": false + }, + "native": { + "samplePeak": 0.1601162701845169, + "truePeakDb": -15.90057398970821, + "loudnessLufs": -28.709339901369557, + "centroidMedianHz": 1224.4350816353904, + "overSamples": 0, + "rmsDb": -22.846021449137904, + "crestDb": 6.934730747403392, + "noiseToToneDb": -0.6166079485198495, + "noiseToToneADb": 6.053873560512358, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "thruster@rpm1200", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.08972565084695816, + "maxErrAt": 143228, + "rmsErr": 0.012448641233006897, + "relRmsErrDb": -16.610480431926955, + "wasm": { + "samplePeak": 0.19161102175712585, + "truePeakDb": -14.343339234769212, + "loudnessLufs": -26.051033400616078, + "centroidMedianHz": 2114.222071076734, + "overSamples": 0, + "rmsDb": -21.48680915202533, + "crestDb": 7.1352188868162605, + "noiseToToneDb": -4.949059402813134, + "noiseToToneADb": 12.652391835948997, + "lowMotion": false + }, + "native": { + "samplePeak": 0.1893547624349594, + "truePeakDb": -14.423961460001248, + "loudnessLufs": -26.05070520432609, + "centroidMedianHz": 2115.6363534197994, + "overSamples": 0, + "rmsDb": -21.484457233344667, + "crestDb": 7.029981882815694, + "noiseToToneDb": -4.965083035181173, + "noiseToToneADb": 12.716143920958068, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "thruster@rpm4800", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.2870774045586586, + "maxErrAt": 142979, + "rmsErr": 0.054912511748541046, + "relRmsErrDb": -8.246006212467824, + "wasm": { + "samplePeak": 0.36383503675460815, + "truePeakDb": -8.715719927350808, + "loudnessLufs": -17.418922989049733, + "centroidMedianHz": 4914.23686401765, + "overSamples": 0, + "rmsDb": -16.959964506391177, + "crestDb": 8.17805487903365, + "noiseToToneDb": -2.834032055202698, + "noiseToToneADb": 9.67560044209106, + "lowMotion": false + }, + "native": { + "samplePeak": 0.3622153401374817, + "truePeakDb": -8.742560302886037, + "loudnessLufs": -17.424886445264, + "centroidMedianHz": 4908.148280531137, + "overSamples": 0, + "rmsDb": -16.961323502311725, + "crestDb": 8.140660284709858, + "noiseToToneDb": -2.9540607279645457, + "noiseToToneADb": 9.40924191267517, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "sfxr-classic-classic-sfxr-abstract-golf", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.000002171844244003296, + "maxErrAt": 117685, + "rmsErr": 9.912912629276112e-8, + "relRmsErrDb": -134.45251378934543, + "wasm": { + "samplePeak": 0.9895820021629333, + "truePeakDb": 0.9076630707842905, + "loudnessLufs": -5.327743141800993, + "centroidMedianHz": 8147.614236982103, + "overSamples": 0, + "rmsDb": -5.623460642702886, + "crestDb": 5.532496403571967, + "noiseToToneDb": -0.9960950126656059, + "noiseToToneADb": 7.586387572827178, + "lowMotion": true + }, + "native": { + "samplePeak": 0.9895820021629333, + "truePeakDb": 0.9076628030277624, + "loudnessLufs": -5.327743184793801, + "centroidMedianHz": 8147.614175634302, + "overSamples": 0, + "rmsDb": -5.623460696287697, + "crestDb": 5.5324964571567765, + "noiseToToneDb": -0.9960947920917818, + "noiseToToneADb": 7.5863877077205615, + "lowMotion": true + }, + "flagsWasm": [ + "waived truePeakDb", + "waived lowMotion", + "waived loudnessLufs", + "warn truePeakDb" + ], + "flagsNative": [ + "waived truePeakDb", + "waived lowMotion", + "waived loudnessLufs", + "warn truePeakDb" + ], + "violations": [] + }, + { + "id": "sfxr-explosion", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.000002158805727958679, + "maxErrAt": 129292, + "rmsErr": 1.1522412234189382e-7, + "relRmsErrDb": -127.13848106038596, + "wasm": { + "samplePeak": 0.6732206344604492, + "truePeakDb": -3.0561513953602306, + "loudnessLufs": -8.634121016036506, + "centroidMedianHz": 9795.322296183533, + "overSamples": 0, + "rmsDb": -11.630620604104497, + "crestDb": 8.193768979977403, + "noiseToToneDb": 60, + "noiseToToneADb": 60, + "lowMotion": true + }, + "native": { + "samplePeak": 0.6732206344604492, + "truePeakDb": -3.0561512376276347, + "loudnessLufs": -8.634121064908708, + "centroidMedianHz": 9795.322310734966, + "overSamples": 0, + "rmsDb": -11.630620652864625, + "crestDb": 8.193769028737531, + "noiseToToneDb": 60, + "noiseToToneADb": 60, + "lowMotion": true + }, + "flagsWasm": [ + "waived lowMotion" + ], + "flagsNative": [ + "waived lowMotion" + ], + "violations": [] + }, + { + "id": "tract/vowel-a", + "lenWasm": 34560, + "lenNative": 34560, + "maxErr": 0.0001921914517879486, + "maxErrAt": 28187, + "rmsErr": 0.000046111049670587934, + "relRmsErrDb": -52.60441410619507, + "wasm": { + "samplePeak": 0.08378415554761887, + "truePeakDb": -21.518327105526673, + "loudnessLufs": -34.251568232392565, + "centroidMedianHz": 1937.1220888731689, + "overSamples": 0, + "rmsDb": -33.80275018649363, + "crestDb": 12.265988122620389, + "noiseToToneDb": -10.436050344670264, + "noiseToToneADb": -12.885220061403881, + "lowMotion": false + }, + "native": { + "samplePeak": 0.08390921354293823, + "truePeakDb": -21.50539262673788, + "loudnessLufs": -34.23471535099468, + "centroidMedianHz": 1937.2296872141594, + "overSamples": 0, + "rmsDb": -33.78684837329708, + "crestDb": 12.263041385142849, + "noiseToToneDb": -10.447393994101596, + "noiseToToneADb": -12.89873810612136, + "lowMotion": false + }, + "flagsWasm": [ + "FAIL onsetMs" + ], + "flagsNative": [ + "FAIL onsetMs" + ], + "violations": [] + } + ] +} \ No newline at end of file diff --git a/docs/renders/aqua-feel-native-diff-2026-11-10.md b/docs/renders/aqua-feel-native-diff-2026-11-10.md new file mode 100644 index 0000000..84c56d7 --- /dev/null +++ b/docs/renders/aqua-feel-native-diff-2026-11-10.md @@ -0,0 +1,118 @@ +# aqua-feel: faustwasm vs the daemon's native libfaust path + +**Nothing here was heard.** Numbers from offline buffers, same Faust source, 48 kHz, mono, 512-frame blocks, same params. + +- wasm side: `tools/aqua-feel/render.js` (faustwasm 0.20.0, libfaust 2.90.19 and the stdlib bundled with it, `-ftz 2`). +- native side: `tools/FaustNativeRender` over `AquaSynthPatchCompiler` (`NativeFaustRuntime.cs:530`: `-single -ftz 2 -vec -lv 1`), libfaust 2.90.20 + LLVM built in the Linux sandbox 2026-10-31, with the stdlib `faustlibraries-20261031` (master 3d8c6ef) from `AQUASYNTH_FAUST_HOME`. **The two sides compile against different standard-library text**, and that decides the thruster result below; "native" here means this sandbox's library, which the repo does not pin (`docs/faust-toolchain-boundary.md`: the host's `share/faust`). +- driver: `node tools/aqua-feel/native-diff.mjs --tract `; raw numbers in `aqua-feel-native-diff-2026-11-10.json`. +- tract voice: `vowel-a` Faust source from the 2026-11-08 render kit (0.72 s, default controls). Not in the repo (`--tract` takes a path), not an aqua-feel catalog item, so its `FAIL onsetMs` is the patch-group rule applied by this script, not a repo gate result. +- the script exits 1 on a render error, a length mismatch, or a violation of its `LIMITS` (see "Tolerance in `native-diff.mjs`" below); the verdicts here are the reader's. +- Nothing here was heard. + +Columns are wasm / native. + +| item | max abs err (at sample) | err vs signal RMS | sample peak | centroid Hz | LUFS | true peak dBTP | gate flags | +|---|---|---|---|---|---|---|---| +| thruster rpm300 | 2.6e-2 (142707) | -28.8 dB | .1426/.1424 | 751.9/751.5 | -30.227/-30.216 | -16.90/-16.92 | none / none | +| thruster rpm600 | 4.9e-2 | -23.5 dB | .1596/.1601 | 1228.0/1224.4 | -28.695/-28.709 | -15.93/-15.90 | none / none | +| thruster rpm1200 | 9.0e-2 | -16.6 dB | .1916/.1894 | 2114.2/2115.6 | -26.051/-26.051 | -14.34/-14.42 | none / none | +| thruster rpm4800 | 2.9e-1 | -8.2 dB | .3638/.3622 | 4914.2/4908.1 | -17.419/-17.425 | -8.72/-8.74 | none / none | +| sfxr classic abstract-golf | 2.2e-6 | -134.5 dB | .9896/.9896 | 8147.6/8147.6 | -5.328/-5.328 | +0.908/+0.908 | waived truePeakDb, lowMotion, loudnessLufs; warn truePeakDb / identical | +| sfxr explosion | 2.2e-6 | -127.1 dB | .6732/.6732 | 9795.3/9795.3 | -8.634/-8.634 | -3.06/-3.06 | waived lowMotion / identical | +| tract vowel-a | 1.9e-4 | -52.6 dB | .08378/.08391 | 1937.1/1937.2 | -34.252/-34.235 | -21.52/-21.51 | FAIL onsetMs / identical | + +Hiss caps (noiseToToneADb, wasm / native; cap): rpm300 13.09/13.06 (16), rpm600 5.04/6.05 (8), rpm1200 12.65/12.72 (15). rpm4800 9.68/9.41 (no cap). + +## Verdict + +**sfxr (two items): the paths agree** to float rounding (2e-6). These two patches use no library smoother (`smooth01` and a local `seg_smooth`), so the agreement says nothing about the library difference below. The gate flags are the script's `gate()` applied to each side's metrics for these two items only; they match. + +**Tract voice: agrees to -52.6 dB** (max 1.9e-4 on a 0.084 peak); metrics within 0.02 dB and 0.01 % centroid. The residual is the same library difference: the source calls `si.smoo` 101 times; with the old smoother form pinned on both sides the error is -61.2 dB, with the new form pinned on both it is 3.1e-7 (-109.3 dB). + +**Thruster: waveforms diverge, level and spectrum metrics stay close.** Relative RMS error -28.8 dB at rpm300 to -8.2 dB at rpm4800. At rpm4800 it grows over time: windowed -53, -33, -22, -15, -10 and -4 dB for 0-0.25, 0.25-0.5, 0.5-1, 1-1.5, 1.5-2 and 2-3 s. Differences in the metrics: LUFS 0.015 dB, true peak 0.081 dB, sample peak 1.2 %, centroid 0.3 %, crest 0.11 dB, hiss within 0.27 dB except rpm600 A-weighted, 1.0 dB higher native. + +### Cause (isolated by a separate Soul pass, 2026-11-10) + +The thruster smooths `rpm` with `si.smooth(ba.tau2pole(0.2))`, and every oscillator integrates that into phase. The two library texts define `si.smooth` differently: + +- the wasm bundle (libfaust 2.90.19, `/usr/share/faust`): `smooth(s) = *(1.0 - s) : + ~ *(s)`. In float32 it stalls: the rpm freezes at **4796.632** from sample ~72063 (1.5 s) for a target of 4800. A float32 simulation without fused multiply-add reproduces 4796.6318359375. +- the native overlay (faustlibraries master 3d8c6ef, `signals.lib:391`): `smooth = imp(3)`, an error-form recursion whose comment says it cannot stall. It reaches 4800.0001. + +Evidence that the library text, not `-vec`, `-lv`, `-double` or contraction, decides it: the `faust` CLI emits the same update expression for the same `si.smooth` source under scalar, `-vec -lv 0/1` and `-double`. With the old explicit form pinned in the DSP on both sides, native ends at 4797.6573 (LLVM does contract to FMA, which moves the stall point from 4796.632 to 4797.657, matching the FMA simulation) and wasm at 4796.6319. With the explicit `imp(3)` form pinned on both sides, thruster error falls from 2.61e-2 to 5.7e-7 (rpm300, -120.3 dB) and from 2.87e-1 to 1.13e-4 (rpm4800, -80.5 dB). The stall point is also hypersensitive: one ulp on the pole constant gives 4799.38 or 4798.57. + +The earlier version of this report said the cause was the float32 rounding of the smoother on a native path that "does not stall for an unisolated reason" and suggested `-vec`; that was wrong. The `wander` term is not the cause (removing it leaves rpm300 2.62e-2, rpm4800 2.89e-1). + +The isolation probes are scratch scripts in `/var/lib/mind-aqua-self/soul-nd-1` (not committed); the numbers above are not in the `.json`, which holds the seven main rows plus the tract. + +### What this does and does not invalidate + +- Level, true-peak, centroid, crest and hiss claims for the thruster hold on both libraries within the numbers above. The rpm600 hiss cap (8 dB) has 1.95 dB of headroom with the native library, not the 2.96 dB the threshold note assumes; still passes. +- Sample- or phase-level comparisons of the thruster (null tests, exact waveform diffs, sub-ms timing of the rotor tone) are specific to the library that rendered them. +- Which library a given daemon uses depends on its `share/faust`. A daemon shipping a release stdlib with the old `si.smooth` would stall like wasm; this sandbox's would not. The only Windows Faust install found (Starfire, 2.85.5) has the old form; see the section on the production stdlib below. +- **Not tested:** the other aqua-feel items that do not call `si.smooth`/`si.smoo` (the 81 items are 45 patch renders and 36 Ding event mixes; this report covers 12 sources plus the tract), the Ding event mixes (only one note per instrument is rendered below), block sizes other than 512, and any live (non-offline) host. + +## Toolchain + +dotnet 10.0.403 builds `FaustNativeRender` with `/var/lib/mind-aqua-self/env-review.sh`. Re-running the script reproduces the JSON byte for byte. + +## Which stdlib the production daemon compiles against (2026-11-10) + +The repo neither vendors nor pins faustlibraries (`.gitmodules` holds only zynaddsubfx and pink-trombone-mod). `NativeFaustRuntime.cs:530` passes `-I /share/faust`, and `AquaSynthFaustToolchain.Find` (`NativeFaustRuntime.cs:651`) takes `home` from `AQUASYNTH_FAUST_HOME`, then `AQUARIUM_FAUST_HOME`, then `Tools/Faust` beside the app, then `C:\Program Files\Faust` (Windows) or `/usr/local`, `/usr`. So the daemon compiles against whatever Faust install the host has. + +| host | Faust | `si.smooth` in its `signals.lib` | stalls (float32)? | +|---|---|---|---| +| Starfire `C:\Program Files\Faust` (read over ssh; faust.exe 2.85.5, `signals.lib` 2026-03-20, `smooth = smooth_imp(0)`, line 382) | 2.85.5 | `(1.0 - s) * x + s * y`, the same recursion as wasm's | yes, by the same mechanism | +| aqua-feel's wasm bundle (faustwasm 0.20.0, libfaust 2.90.19; embedded `signals.lib`) | 2.90.19 | `smooth(s) = *(1.0 - s) : + ~ *(s)` | yes: 4796.632 | +| this sandbox (`faustlibraries-20261031`, master 3d8c6ef, a one-commit shallow clone) | 2.90.20 | `smooth = imp(3)` with a comment on the stall | no | + +I read only that Starfire file's `smooth` definition; I did not render on Starfire. On the evidence, the Windows daemon uses the old form, so **aqua-feel's wasm numbers are the closer model of that host than this sandbox's native render is**; the sandbox's imp(3) library is not what any shipped host has. Whether the daemon runs on Starfire in production is not established here; it is the only Windows Faust install found. + +## The twelve sources that call `si.smooth` / `si.smoo` + +The earlier "eleven" was seven patches, three Ding instruments and the larynx graph; the catalog has seven patches besides the thruster **including** the larynx graph, and **four** Ding instruments (`muted-pluck` was missed). Rendered: the thruster at rpm 1200, those seven patches, and one note per instrument (261.6256 Hz, 1.5 s, not the mixed event). Three conditions, each the same Faust text on both sides: as shipped (each side's own stdlib), the old smoother pinned (`nd_smooth(s) = *(1.0 - s) : + ~ *(s)`), and imp(3) pinned. Pinning rewrites `si.smooth` and `si.smoo` in the source text; a smoother called inside another library function is not rewritten, the `dx7-algorithm-32` residual below is not that, see `dx7-algorithm-32-native-diff-cause.md`. The tract voice rides along as a thirteenth row. Columns are native minus wasm. Raw rows: `aqua-feel-native-diff-smoother-{library,old,imp3}-2026-11-10.json`. + +| source | stdlib as shipped (wasm bundle vs sandbox): max err / rel RMS dB | old smoother pinned | imp3 pinned | ΔLUFS lib / old / imp3 | Δ true peak dB lib / old / imp3 | Δ centroid % lib / old / imp3 | +|---|---|---|---|---|---|---| +| thruster@rpm1200 | 9.0e-2 / -16.6 | 4.5e-2 / -21.2 | 7.1e-6 / -99.9 | 0.000 / 0.001 / -0.000 | -0.081 / -0.152 / 0.000 | 0.07 / -0.08 / 0.00 | +| advanced-alien-multivalve-graph | 2.9e-5 / -68.4 | 1.0e-5 / -72.5 | 7.9e-7 / -97.9 | -0.000 / -0.000 / -0.000 | -0.001 / -0.000 / -0.000 | -0.00 / 0.00 / 0.00 | +| advanced-bird-syrinx | 2.8e-3 / -27.3 | 1.6e-3 / -31.7 | 3.2e-6 / -88.3 | -0.001 / -0.001 / -0.000 | -0.000 / -0.000 / -0.000 | -0.00 / -0.00 / 0.00 | +| advanced-bird-syrinx-graph | 9.4e-4 / -38.4 | 2.7e-4 / -47.1 | 1.5e-5 / -75.4 | 0.000 / -0.000 / 0.000 | 0.000 / 0.000 / -0.000 | -0.01 / 0.00 / -0.00 | +| advanced-syrinx-voice | 1.3e-3 / -23.9 | 3.6e-4 / -34.6 | 2.3e-6 / -86.3 | 0.001 / 0.000 / 0.000 | 0.003 / 0.003 / -0.000 | -0.00 / -0.00 / 0.00 | +| advanced-human-larynx-graph | 9.6e-5 / -38.1 | 3.3e-5 / -46.6 | 7.6e-7 / -94.2 | 0.002 / 0.001 / -0.000 | 0.002 / 0.001 / -0.000 | 0.05 / 0.05 / 0.04 | +| dx7-algorithm-32-additive-organ | 1.7e-4 / -79.3 | 1.6e-4 / -79.5 | 1.6e-4 / -79.5 | 0.000 / -0.000 / 0.000 | -0.000 / -0.000 / -0.000 | -0.00 / -0.00 / -0.00 | +| dx7-algorithm-8-bright-pair | 4.1e-6 / -94.5 | 4.2e-6 / -94.2 | 3.9e-6 / -94.7 | 0.000 / 0.000 / 0.000 | -0.000 / -0.000 / -0.000 | 0.00 / 0.00 / -0.00 | +| ding-instrument/glass-chime | 3.8e-2 / -18.4 | 2.5e-2 / -21.9 | 3.3e-7 / -130.0 | 0.000 / 0.000 / 0.000 | -0.000 / 0.000 / -0.000 | 0.02 / 0.01 / -0.00 | +| ding-instrument/muted-pluck | 6.6e-4 / -49.8 | 6.1e-4 / -50.2 | 3.0e-8 / -141.7 | -0.000 / -0.000 / -0.000 | -0.000 / -0.000 / -0.000 | 0.00 / -0.01 / -0.00 | +| ding-instrument/soft-triangle | 4.3e-3 / -30.3 | 2.4e-3 / -35.2 | 4.5e-8 / -142.0 | 0.000 / -0.000 / -0.000 | -0.000 / -0.000 / 0.000 | 0.16 / 0.12 / -0.00 | +| ding-instrument/warm-bell | 5.4e-2 / -18.3 | 3.2e-2 / -22.9 | 2.3e-7 / -134.2 | 0.000 / 0.000 / -0.000 | -0.000 / 0.000 / -0.000 | 0.01 / 0.00 / -0.00 | +| tract/vowel-a | 1.9e-4 / -52.6 | 6.7e-5 / -61.2 | 3.1e-7 / -109.3 | 0.017 / 0.006 / -0.000 | 0.013 / 0.004 / -0.000 | 0.01 / 0.01 / -0.00 | + +Largest metric deltas over all sources and the three conditions: |LUFS| 0.017 dB (tract), |true peak| 0.152 dB (thruster, old pinned), |centroid| 0.16 % (soft-triangle; the earlier core run has 0.3 % at thruster rpm600). **No gate flag differs on any source in any condition**; the tract's patch-group `FAIL onsetMs` is on both sides (see above). + +What the data says: + +- With imp(3) pinned on both sides all 13 rows agree to -75 dB or better (Ding instruments -130 to -142 dB, thruster -99.9 dB, tract -109 dB; worst: `advanced-bird-syrinx-graph` -75.4 dB). `dx7-algorithm-32-additive-organ` sits at -79.3 (library), -79.5 (old) and -79.5 dB (imp3), so the pinned smoother does not move it. Cause found 2026-11-12 (`dx7-algorithm-32-native-diff-cause.md`): the native phasor increment rounds one 2^-30 step differently once `ba.time / ma.SR` shares the reciprocal of the sample rate (about -93 dB without it), and `si.smoo` text on the macro params is the rest (-141 dB with both removed). +- With the old smoother pinned the thruster still differs (-21.2 dB): native's LLVM contracts the recursion to FMA and stalls at 4797.657 instead of wasm's 4796.632. The old form's stall point depends on the arithmetic; the imp(3) form does not. +- As shipped, nine of the 13 rows differ at waveform level by -16 to -50 dB (thruster, the three syrinx patches, the larynx graph, all four Ding instruments); `advanced-alien-multivalve-graph` (-68 dB), the tract (-52.6 dB) and the two dx7 patches are closer. + +## Tolerance in `native-diff.mjs` + +The limits (`LIMITS`) and the comparison now live in `tools/aqua-feel/native-compare.js`; `native-diff.test.mjs` trips each one. Added, because the data shows a gap. Across all 13 sources and the 3 conditions the metric deltas stay under |LUFS| 0.017 dB, |true peak| 0.152 dB, centroid 0.3 %. A deliberate +1 dB gain on the native buffer moves LUFS and true peak by 1.0 dB. The limits sit between: 0.1 dB, 0.5 dB, 1 %. Sample-level error has no limit with the library smoother (the waveforms legitimately diverge), but with `--smoother imp3` it has one at -60 dB (the worst row is -75.4 dB; a one-sample shift reads about -4 dB). + +Mutation check on the tool, `--items smoother --smoother imp3`: unmutated, 12 of 12 clean, exit 0; +1 dB native gain, 12 of 12 flagged (`loudnessLufs`, `truePeakDb`, `relRmsErrDb`), exit 1; one-sample native shift, 12 of 12 flagged (`relRmsErrDb`), exit 1. A one-sample shift is not detectable with the library smoother; `loudnessLufs`, `truePeakDb` and centroid also cannot see it. The limits are tripwires fit to these 13 sources, not taste. + +## What changes in aqua-feel's conclusions + +- Nothing in the gate changes: no flag differs on any tested source, and every metric delta is far below any threshold in `thresholds.json` that I looked at except the rpm600 thruster hiss cap noted above (1.0 dB, still under the cap). +- aqua-feel's WAVs are a fair rendering of what a host with the old `si.smooth` produces (the only Windows install found). They are **not** sample-identical to this sandbox's native render, and no sample-level claim about the thruster, the syrinx patches or the Ding instruments should be carried across them. +- The listening trial heard wasm renders; for a Windows daemon that is the closer path. For a daemon built on the sandbox's library, the same sources have the same level and spectrum and a different phase trajectory. +- Nothing was heard. + +## Proposed faustlibraries pin (not made) + +The cause is that `si.smooth` is whatever the host's `share/faust` says. Options, in order of my preference (confidence moderate; none of this is Soul-checked): +1. Vendor a pinned faustlibraries tree under the daemon's `Tools/Faust/share/faust` at build, record its commit in the compile manifest next to `faustLibrary`, and have `AquaSynthNativeOptions` prefer it over the system install. This makes the wasm bundle, Starfire and the sandbox agree only if aqua-feel's wasm path pins the same text too (faustwasm takes a library via its virtual FS), so pin both. +2. Decide the smoother once, in AquaSynth's emitted source: lowering emits a local smoother definition instead of calling `si.smooth`, so no host's stdlib decides it. Smaller, but changes every lowered patch and does not cover library functions that smooth internally. +3. Leave unpinned and record the host library identity in each compile manifest, accepting skew. + +Which smoother is the right one is a sound decision, not a measurement: imp(3) cannot stall (the library's own comment), the old form lands 0.07 % short of an rpm target at 4800. That is the owner's call. diff --git a/docs/renders/aqua-feel-native-diff-smoother-imp3-2026-11-10.json b/docs/renders/aqua-feel-native-diff-smoother-imp3-2026-11-10.json new file mode 100644 index 0000000..70ade89 --- /dev/null +++ b/docs/renders/aqua-feel-native-diff-smoother-imp3-2026-11-10.json @@ -0,0 +1,480 @@ +{ + "libfaustWasm": "2.90.19", + "sampleRate": 48000, + "items": "smoother", + "smoother": "imp3", + "rows": [ + { + "id": "thruster@rpm1200", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.000007145106792449951, + "maxErrAt": 129748, + "rmsErr": 8.55413991926508e-7, + "relRmsErrDb": -99.87174472945775, + "wasm": { + "samplePeak": 0.18935100734233856, + "truePeakDb": -14.4241663695795, + "loudnessLufs": -26.05070420011194, + "centroidMedianHz": 2115.6325442720204, + "overSamples": 0, + "rmsDb": -21.48445687766135, + "crestDb": 7.029809275623743, + "noiseToToneDb": -4.965082233659885, + "noiseToToneADb": 12.71614612278843, + "lowMotion": false + }, + "native": { + "samplePeak": 0.1893547624349594, + "truePeakDb": -14.423961460001248, + "loudnessLufs": -26.05070520432609, + "centroidMedianHz": 2115.6363534197994, + "overSamples": 0, + "rmsDb": -21.484457233344667, + "crestDb": 7.029981882815694, + "noiseToToneDb": -4.965083035181173, + "noiseToToneADb": 12.716143920958068, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "advanced-alien-multivalve-graph", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 7.934868335723877e-7, + "maxErrAt": 35537, + "rmsErr": 8.335841454269617e-8, + "relRmsErrDb": -97.93819913427402, + "wasm": { + "samplePeak": 0.03596395254135132, + "truePeakDb": -28.873901668042834, + "loudnessLufs": -44.01583944248863, + "centroidMedianHz": 1719.746341068706, + "overSamples": 0, + "rmsDb": -38.86915824557575, + "crestDb": 9.986506563399741, + "noiseToToneDb": 2.2949371593837427, + "noiseToToneADb": -35.26674665881937, + "lowMotion": false + }, + "native": { + "samplePeak": 0.03596379607915878, + "truePeakDb": -28.87393371231402, + "loudnessLufs": -44.01584006251751, + "centroidMedianHz": 1719.7470242831325, + "overSamples": 0, + "rmsDb": -38.86915865101764, + "crestDb": 9.986469180550948, + "noiseToToneDb": 2.2949373787323424, + "noiseToToneADb": -35.266660047022036, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "advanced-bird-syrinx", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.0000031944364309310913, + "maxErrAt": 45510, + "rmsErr": 3.8508989002632593e-7, + "relRmsErrDb": -88.25157230135844, + "wasm": { + "samplePeak": 0.07248446345329285, + "truePeakDb": -22.789810284125757, + "loudnessLufs": -39.38587031070514, + "centroidMedianHz": 3246.9416863147353, + "overSamples": 0, + "rmsDb": -35.86910042371045, + "crestDb": 13.073998993865658, + "noiseToToneDb": 3.398154713653433, + "noiseToToneADb": -0.2652457381911617, + "lowMotion": false + }, + "native": { + "samplePeak": 0.07248446345329285, + "truePeakDb": -22.789811642249525, + "loudnessLufs": -39.38587068917686, + "centroidMedianHz": 3246.965668802187, + "overSamples": 0, + "rmsDb": -35.86909904000429, + "crestDb": 13.0739976101595, + "noiseToToneDb": 3.3981546091622192, + "noiseToToneADb": -0.2652490593489034, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "advanced-bird-syrinx-graph", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.000015079975128173828, + "maxErrAt": 32899, + "rmsErr": 0.000001467630377556357, + "relRmsErrDb": -75.44121356490483, + "wasm": { + "samplePeak": 0.06643311679363251, + "truePeakDb": -23.549215787508704, + "loudnessLufs": -41.27251453792896, + "centroidMedianHz": 3608.1313492575864, + "overSamples": 0, + "rmsDb": -35.83554258887882, + "crestDb": 12.283235156759115, + "noiseToToneDb": 3.2057938097979033, + "noiseToToneADb": -14.047787523198789, + "lowMotion": false + }, + "native": { + "samplePeak": 0.06643310934305191, + "truePeakDb": -23.549216695315987, + "loudnessLufs": -41.27251147778152, + "centroidMedianHz": 3608.0824292245743, + "overSamples": 0, + "rmsDb": -35.83554221039036, + "crestDb": 12.28323380413414, + "noiseToToneDb": 3.205791878004028, + "noiseToToneADb": -14.047777587591895, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "advanced-syrinx-voice", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.000002266373485326767, + "maxErrAt": 34649, + "rmsErr": 1.2956788367741612e-7, + "relRmsErrDb": -86.30175634080427, + "wasm": { + "samplePeak": 0.01358692068606615, + "truePeakDb": -37.32686366831106, + "loudnessLufs": -43.745499382417364, + "centroidMedianHz": 3723.182853016622, + "overSamples": 0, + "rmsDb": -46.7956174011546, + "crestDb": 9.458038205292446, + "noiseToToneDb": 1.791072654520292, + "noiseToToneADb": 1.7998750278369968, + "lowMotion": false + }, + "native": { + "samplePeak": 0.013587156310677528, + "truePeakDb": -37.32703848354456, + "loudnessLufs": -43.74549926644062, + "centroidMedianHz": 3723.2127010397694, + "overSamples": 0, + "rmsDb": -46.7956172502661, + "crestDb": 9.45818868394479, + "noiseToToneDb": 1.7910715795642531, + "noiseToToneADb": 1.7998739270327846, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "advanced-human-larynx-graph", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 7.587950676679611e-7, + "maxErrAt": 23972, + "rmsErr": 3.7846162388541006e-8, + "relRmsErrDb": -94.24621859478574, + "wasm": { + "samplePeak": 0.007788178510963917, + "truePeakDb": -42.17101862717542, + "loudnessLufs": -50.40721974974582, + "centroidMedianHz": 310.7891589769418, + "overSamples": 0, + "rmsDb": -50.08572494872536, + "crestDb": 7.914442895151346, + "noiseToToneDb": -30.816131815537496, + "noiseToToneADb": -27.766650651768487, + "lowMotion": false + }, + "native": { + "samplePeak": 0.0077881766483187675, + "truePeakDb": -42.17102078454992, + "loudnessLufs": -50.40722107125285, + "centroidMedianHz": 310.8999754545593, + "overSamples": 0, + "rmsDb": -50.08572618353941, + "crestDb": 7.914442052620592, + "noiseToToneDb": -30.816127938801678, + "noiseToToneADb": -27.766649805181505, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "dx7-algorithm-32-additive-organ", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.000164765864610672, + "maxErrAt": 23021, + "rmsErr": 0.000020540035138682098, + "relRmsErrDb": -79.45828076486667, + "wasm": { + "samplePeak": 0.6862082481384277, + "truePeakDb": -3.2699164206425015, + "loudnessLufs": -9.759124233116152, + "centroidMedianHz": 656.4226191119797, + "overSamples": 0, + "rmsDb": -8.539759068566523, + "crestDb": 5.268877746918222, + "noiseToToneDb": -37.01983113652173, + "noiseToToneADb": -37.788337502316935, + "lowMotion": false + }, + "native": { + "samplePeak": 0.6861962676048279, + "truePeakDb": -3.2700776073676936, + "loudnessLufs": -9.75912403739766, + "centroidMedianHz": 656.4212037746422, + "overSamples": 0, + "rmsDb": -8.53975888452333, + "crestDb": 5.268725914302816, + "noiseToToneDb": -37.0197944928564, + "noiseToToneADb": -37.78830410408929, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "dx7-algorithm-8-bright-pair", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.000003874767571687698, + "maxErrAt": 4727, + "rmsErr": 3.855039254064681e-7, + "relRmsErrDb": -94.6889441823849, + "wasm": { + "samplePeak": 0.150354266166687, + "truePeakDb": -16.445140264060527, + "loudnessLufs": -27.707281021900723, + "centroidMedianHz": 570.7301006928219, + "overSamples": 0, + "rmsDb": -25.228220791943087, + "crestDb": 8.770535898454384, + "noiseToToneDb": -33.00376708572004, + "noiseToToneADb": -31.168271339188635, + "lowMotion": false + }, + "native": { + "samplePeak": 0.15035313367843628, + "truePeakDb": -16.44521182640461, + "loudnessLufs": -27.707279746651498, + "centroidMedianHz": 570.7300173443439, + "overSamples": 0, + "rmsDb": -25.228219801277547, + "crestDb": 8.770469484271047, + "noiseToToneDb": -33.00374306131664, + "noiseToToneADb": -31.168260425434468, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "ding-instrument/glass-chime", + "lenWasm": 72000, + "lenNative": 72000, + "maxErr": 3.3155083656311035e-7, + "maxErrAt": 1305, + "rmsErr": 1.7352308119215504e-8, + "relRmsErrDb": -130.0126824087709, + "wasm": { + "samplePeak": 0.17621399462223053, + "truePeakDb": -15.062043767848326, + "loudnessLufs": -21.567272532108692, + "centroidMedianHz": 1602.876747515682, + "overSamples": 0, + "rmsDb": -22.84411377344017, + "crestDb": 7.764921701255961, + "noiseToToneDb": -3.1568108379227766, + "noiseToToneADb": -2.864104015545095, + "lowMotion": false + }, + "native": { + "samplePeak": 0.17621399462223053, + "truePeakDb": -15.062043917300066, + "loudnessLufs": -21.567272049422396, + "centroidMedianHz": 1602.87659074774, + "overSamples": 0, + "rmsDb": -22.844113441684826, + "crestDb": 7.764921369500614, + "noiseToToneDb": -3.156811070401325, + "noiseToToneADb": -2.8641043762574725, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "ding-instrument/muted-pluck", + "lenWasm": 72000, + "lenNative": 72000, + "maxErr": 2.9802322387695312e-8, + "maxErrAt": 39, + "rmsErr": 2.9894375811414372e-9, + "relRmsErrDb": -141.73373986178612, + "wasm": { + "samplePeak": 0.184849813580513, + "truePeakDb": -14.6610699229291, + "loudnessLufs": -27.093755336399102, + "centroidMedianHz": 337.57025931776576, + "overSamples": 0, + "rmsDb": -22.505770402082735, + "crestDb": 7.842150740622795, + "noiseToToneDb": -1.8131372817002074, + "noiseToToneADb": -3.773645767164254, + "lowMotion": false + }, + "native": { + "samplePeak": 0.184849813580513, + "truePeakDb": -14.66107041311351, + "loudnessLufs": -27.093755376725216, + "centroidMedianHz": 337.5700092248225, + "overSamples": 0, + "rmsDb": -22.505770501962672, + "crestDb": 7.842150840502738, + "noiseToToneDb": -1.813137412998607, + "noiseToToneADb": -3.773645881399787, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "ding-instrument/soft-triangle", + "lenWasm": 72000, + "lenNative": 72000, + "maxErr": 4.470348358154297e-8, + "maxErrAt": 8043, + "rmsErr": 4.533743604935639e-9, + "relRmsErrDb": -141.9875633660136, + "wasm": { + "samplePeak": 0.19771063327789307, + "truePeakDb": -14.073779284051348, + "loudnessLufs": -24.40759733605614, + "centroidMedianHz": 432.727373830872, + "overSamples": 0, + "rmsDb": -22.83707655432693, + "crestDb": 8.757677097882045, + "noiseToToneDb": -7.168489439744874, + "noiseToToneADb": -9.027978911866263, + "lowMotion": false + }, + "native": { + "samplePeak": 0.19771063327789307, + "truePeakDb": -14.073779075736933, + "loudnessLufs": -24.407597412236093, + "centroidMedianHz": 432.72721346965216, + "overSamples": 0, + "rmsDb": -22.83707661841455, + "crestDb": 8.757677161969662, + "noiseToToneDb": -7.168489371555516, + "noiseToToneADb": -9.027978838572636, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "ding-instrument/warm-bell", + "lenWasm": 72000, + "lenNative": 72000, + "maxErr": 2.3096799850463867e-7, + "maxErrAt": 4434, + "rmsErr": 1.76394996238211e-8, + "relRmsErrDb": -134.18055622815194, + "wasm": { + "samplePeak": 0.2345581352710724, + "truePeakDb": -12.586384128665326, + "loudnessLufs": -19.507703963974553, + "centroidMedianHz": 930.5955363634018, + "overSamples": 0, + "rmsDb": -19.84892961990138, + "crestDb": 7.25393962687934, + "noiseToToneDb": -4.493021207525722, + "noiseToToneADb": -3.568407049551638, + "lowMotion": false + }, + "native": { + "samplePeak": 0.2345581352710724, + "truePeakDb": -12.586384726471685, + "loudnessLufs": -19.50770471111663, + "centroidMedianHz": 930.5954188145085, + "overSamples": 0, + "rmsDb": -19.84893033283087, + "crestDb": 7.253940339808832, + "noiseToToneDb": -4.49302081177872, + "noiseToToneADb": -3.568406831675894, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "tract/vowel-a", + "lenWasm": 34560, + "lenNative": 34560, + "maxErr": 3.129243850708008e-7, + "maxErrAt": 32270, + "rmsErr": 6.777997120190261e-8, + "relRmsErrDb": -109.27441638992673, + "wasm": { + "samplePeak": 0.08390945941209793, + "truePeakDb": -21.50535980428806, + "loudnessLufs": -34.23468728787301, + "centroidMedianHz": 1937.2312975664222, + "overSamples": 0, + "rmsDb": -33.786820379265095, + "crestDb": 12.26303884229992, + "noiseToToneDb": -10.447396268131833, + "noiseToToneADb": -12.898741047906523, + "lowMotion": false + }, + "native": { + "samplePeak": 0.08390921354293823, + "truePeakDb": -21.50539262673788, + "loudnessLufs": -34.23471535099468, + "centroidMedianHz": 1937.2296872141594, + "overSamples": 0, + "rmsDb": -33.78684837329708, + "crestDb": 12.263041385142849, + "noiseToToneDb": -10.447393994101596, + "noiseToToneADb": -12.89873810612136, + "lowMotion": false + }, + "flagsWasm": [ + "FAIL onsetMs" + ], + "flagsNative": [ + "FAIL onsetMs" + ], + "violations": [] + } + ] +} \ No newline at end of file diff --git a/docs/renders/aqua-feel-native-diff-smoother-library-2026-11-10.json b/docs/renders/aqua-feel-native-diff-smoother-library-2026-11-10.json new file mode 100644 index 0000000..4ec5042 --- /dev/null +++ b/docs/renders/aqua-feel-native-diff-smoother-library-2026-11-10.json @@ -0,0 +1,480 @@ +{ + "libfaustWasm": "2.90.19", + "sampleRate": 48000, + "items": "smoother", + "smoother": "library", + "rows": [ + { + "id": "thruster@rpm1200", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.08972565084695816, + "maxErrAt": 143228, + "rmsErr": 0.012448641233006897, + "relRmsErrDb": -16.610480431926955, + "wasm": { + "samplePeak": 0.19161102175712585, + "truePeakDb": -14.343339234769212, + "loudnessLufs": -26.051033400616078, + "centroidMedianHz": 2114.222071076734, + "overSamples": 0, + "rmsDb": -21.48680915202533, + "crestDb": 7.1352188868162605, + "noiseToToneDb": -4.949059402813134, + "noiseToToneADb": 12.652391835948997, + "lowMotion": false + }, + "native": { + "samplePeak": 0.1893547624349594, + "truePeakDb": -14.423961460001248, + "loudnessLufs": -26.05070520432609, + "centroidMedianHz": 2115.6363534197994, + "overSamples": 0, + "rmsDb": -21.484457233344667, + "crestDb": 7.029981882815694, + "noiseToToneDb": -4.965083035181173, + "noiseToToneADb": 12.716143920958068, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "advanced-alien-multivalve-graph", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.000028988346457481384, + "maxErrAt": 35885, + "rmsErr": 0.0000024852740234353792, + "relRmsErrDb": -68.44968710695318, + "wasm": { + "samplePeak": 0.03596847131848335, + "truePeakDb": -28.872530673301476, + "loudnessLufs": -44.01558191383624, + "centroidMedianHz": 1719.821403397199, + "overSamples": 0, + "rmsDb": -38.86917357972277, + "crestDb": 9.98761318843934, + "noiseToToneDb": 2.2946722668875523, + "noiseToToneADb": -35.26574829949449, + "lowMotion": false + }, + "native": { + "samplePeak": 0.03596379607915878, + "truePeakDb": -28.87393371231402, + "loudnessLufs": -44.01584006251751, + "centroidMedianHz": 1719.7470242831325, + "overSamples": 0, + "rmsDb": -38.86915865101764, + "crestDb": 9.986469180550948, + "noiseToToneDb": 2.2949373787323424, + "noiseToToneADb": -35.266660047022036, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "advanced-bird-syrinx", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.0027769599109888077, + "maxErrAt": 45282, + "rmsErr": 0.0004283705044505341, + "relRmsErrDb": -27.32626534167103, + "wasm": { + "samplePeak": 0.0724848210811615, + "truePeakDb": -22.789532845508607, + "loudnessLufs": -39.384826220237244, + "centroidMedianHz": 3246.9736644772215, + "overSamples": 0, + "rmsDb": -35.86925853361916, + "crestDb": 13.074199958593006, + "noiseToToneDb": 3.395163753535492, + "noiseToToneADb": -0.26769809662280863, + "lowMotion": false + }, + "native": { + "samplePeak": 0.07248446345329285, + "truePeakDb": -22.789811642249525, + "loudnessLufs": -39.38587068917686, + "centroidMedianHz": 3246.965668802187, + "overSamples": 0, + "rmsDb": -35.86909904000429, + "crestDb": 13.0739976101595, + "noiseToToneDb": 3.3981546091622192, + "noiseToToneADb": -0.2652490593489034, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "advanced-bird-syrinx-graph", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.0009388141334056854, + "maxErrAt": 33098, + "rmsErr": 0.00010481991412365385, + "relRmsErrDb": -38.36457659477791, + "wasm": { + "samplePeak": 0.06643299758434296, + "truePeakDb": -23.549220126103343, + "loudnessLufs": -41.272964930830874, + "centroidMedianHz": 3608.3773722521782, + "overSamples": 0, + "rmsDb": -35.835637421519465, + "crestDb": 12.28331440320251, + "noiseToToneDb": 3.205981107524842, + "noiseToToneADb": -14.050756471755903, + "lowMotion": false + }, + "native": { + "samplePeak": 0.06643310934305191, + "truePeakDb": -23.549216695315987, + "loudnessLufs": -41.27251147778152, + "centroidMedianHz": 3608.0824292245743, + "overSamples": 0, + "rmsDb": -35.83554221039036, + "crestDb": 12.28323380413414, + "noiseToToneDb": 3.205791878004028, + "noiseToToneADb": -14.047777587591895, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "advanced-syrinx-voice", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.0012954180128872395, + "maxErrAt": 33655, + "rmsErr": 0.00017071330356165833, + "relRmsErrDb": -23.90529822473789, + "wasm": { + "samplePeak": 0.013509727083146572, + "truePeakDb": -37.32985445982076, + "loudnessLufs": -43.74666608622773, + "centroidMedianHz": 3723.272760511277, + "overSamples": 0, + "rmsDb": -46.796675488586175, + "crestDb": 9.409607002734177, + "noiseToToneDb": 1.790967089244596, + "noiseToToneADb": 1.7997448212983471, + "lowMotion": false + }, + "native": { + "samplePeak": 0.013587156310677528, + "truePeakDb": -37.32703848354456, + "loudnessLufs": -43.74549926644062, + "centroidMedianHz": 3723.2127010397694, + "overSamples": 0, + "rmsDb": -46.7956172502661, + "crestDb": 9.45818868394479, + "noiseToToneDb": 1.7910715795642531, + "noiseToToneADb": 1.7998739270327846, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "advanced-human-larynx-graph", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.00009577462333254516, + "maxErrAt": 34584, + "rmsErr": 0.00002426493085728514, + "relRmsErrDb": -38.10546679970197, + "wasm": { + "samplePeak": 0.007785667199641466, + "truePeakDb": -42.173192592326515, + "loudnessLufs": -50.40895750738575, + "centroidMedianHz": 310.7359227777714, + "overSamples": 0, + "rmsDb": -50.08733256154268, + "crestDb": 7.913249276521016, + "noiseToToneDb": -30.80971405508631, + "noiseToToneADb": -27.762876726971946, + "lowMotion": false + }, + "native": { + "samplePeak": 0.0077881766483187675, + "truePeakDb": -42.17102078454992, + "loudnessLufs": -50.40722107125285, + "centroidMedianHz": 310.8999754545593, + "overSamples": 0, + "rmsDb": -50.08572618353941, + "crestDb": 7.914442052620592, + "noiseToToneDb": -30.816127938801678, + "noiseToToneADb": -27.766649805181505, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "dx7-algorithm-32-additive-organ", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.00016526132822036743, + "maxErrAt": 23020, + "rmsErr": 0.000020994103574012447, + "relRmsErrDb": -79.26822857998425, + "wasm": { + "samplePeak": 0.6861975789070129, + "truePeakDb": -3.270050577733717, + "loudnessLufs": -9.759294906714691, + "centroidMedianHz": 656.4219631461098, + "overSamples": 0, + "rmsDb": -8.539888188883904, + "crestDb": 5.268871817143614, + "noiseToToneDb": -37.019826930432856, + "noiseToToneADb": -37.7883341551492, + "lowMotion": false + }, + "native": { + "samplePeak": 0.6861962676048279, + "truePeakDb": -3.2700776073676936, + "loudnessLufs": -9.75912403739766, + "centroidMedianHz": 656.4212037746422, + "overSamples": 0, + "rmsDb": -8.53975888452333, + "crestDb": 5.268725914302816, + "noiseToToneDb": -37.0197944928564, + "noiseToToneADb": -37.78830410408929, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "dx7-algorithm-8-bright-pair", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.000004090368747711182, + "maxErrAt": 4395, + "rmsErr": 3.9566542791342453e-7, + "relRmsErrDb": -94.46295503093202, + "wasm": { + "samplePeak": 0.1503547728061676, + "truePeakDb": -16.445110330444138, + "loudnessLufs": -27.707288476356375, + "centroidMedianHz": 570.7272310041899, + "overSamples": 0, + "rmsDb": -25.22822394507269, + "crestDb": 8.7705683198403, + "noiseToToneDb": -33.00382123099267, + "noiseToToneADb": -31.168321209089953, + "lowMotion": false + }, + "native": { + "samplePeak": 0.15035313367843628, + "truePeakDb": -16.44521182640461, + "loudnessLufs": -27.707279746651498, + "centroidMedianHz": 570.7300173443439, + "overSamples": 0, + "rmsDb": -25.228219801277547, + "crestDb": 8.770469484271047, + "noiseToToneDb": -33.00374306131664, + "noiseToToneADb": -31.168260425434468, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "ding-instrument/glass-chime", + "lenWasm": 72000, + "lenNative": 72000, + "maxErr": 0.03821572195738554, + "maxErrAt": 19431, + "rmsErr": 0.006593494603898945, + "relRmsErrDb": -18.417512948342896, + "wasm": { + "samplePeak": 0.17621681094169617, + "truePeakDb": -15.062032445423984, + "loudnessLufs": -21.56737037885148, + "centroidMedianHz": 1602.6138671100102, + "overSamples": 0, + "rmsDb": -22.844945161193554, + "crestDb": 7.765891909110638, + "noiseToToneDb": -3.1846833757850774, + "noiseToToneADb": -2.8894771746074213, + "lowMotion": false + }, + "native": { + "samplePeak": 0.17621399462223053, + "truePeakDb": -15.062043917300066, + "loudnessLufs": -21.567272049422396, + "centroidMedianHz": 1602.87659074774, + "overSamples": 0, + "rmsDb": -22.844113441684826, + "crestDb": 7.764921369500614, + "noiseToToneDb": -3.156811070401325, + "noiseToToneADb": -2.8641043762574725, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "ding-instrument/muted-pluck", + "lenWasm": 72000, + "lenNative": 72000, + "maxErr": 0.000662742939312011, + "maxErrAt": 13239, + "rmsErr": 0.00011858435565109058, + "relRmsErrDb": -49.764983408100534, + "wasm": { + "samplePeak": 0.18485014140605927, + "truePeakDb": -14.661061484678669, + "loudnessLufs": -27.09375077529527, + "centroidMedianHz": 337.55371147245654, + "overSamples": 0, + "rmsDb": -22.505768680170274, + "crestDb": 7.8421644228588985, + "noiseToToneDb": -1.8131427242661857, + "noiseToToneADb": -3.7732237451762884, + "lowMotion": false + }, + "native": { + "samplePeak": 0.184849813580513, + "truePeakDb": -14.66107041311351, + "loudnessLufs": -27.093755376725216, + "centroidMedianHz": 337.5700092248225, + "overSamples": 0, + "rmsDb": -22.505770501962672, + "crestDb": 7.842150840502738, + "noiseToToneDb": -1.813137412998607, + "noiseToToneADb": -3.773645881399787, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "ding-instrument/soft-triangle", + "lenWasm": 72000, + "lenNative": 72000, + "maxErr": 0.004339124541729689, + "maxErrAt": 27366, + "rmsErr": 0.0017425528838580662, + "relRmsErrDb": -30.292986249468473, + "wasm": { + "samplePeak": 0.19771148264408112, + "truePeakDb": -14.073758822230804, + "loudnessLufs": -24.407607566689737, + "centroidMedianHz": 432.02737524513924, + "overSamples": 0, + "rmsDb": -22.837073455193796, + "crestDb": 8.757711313308086, + "noiseToToneDb": -7.16850817229934, + "noiseToToneADb": -9.027202849078392, + "lowMotion": false + }, + "native": { + "samplePeak": 0.19771063327789307, + "truePeakDb": -14.073779075736933, + "loudnessLufs": -24.407597412236093, + "centroidMedianHz": 432.72721346965216, + "overSamples": 0, + "rmsDb": -22.83707661841455, + "crestDb": 8.757677161969662, + "noiseToToneDb": -7.168489371555516, + "noiseToToneADb": -9.027978838572636, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "ding-instrument/warm-bell", + "lenWasm": 72000, + "lenNative": 72000, + "maxErr": 0.05384007841348648, + "maxErrAt": 22907, + "rmsErr": 0.010932589918993823, + "relRmsErrDb": -18.335820982841014, + "wasm": { + "samplePeak": 0.23455719649791718, + "truePeakDb": -12.586378660039248, + "loudnessLufs": -19.50785299525781, + "centroidMedianHz": 930.5251827853554, + "overSamples": 0, + "rmsDb": -19.848928933876216, + "crestDb": 7.2539041772066515, + "noiseToToneDb": -4.487748875412059, + "noiseToToneADb": -3.561906168663185, + "lowMotion": false + }, + "native": { + "samplePeak": 0.2345581352710724, + "truePeakDb": -12.586384726471685, + "loudnessLufs": -19.50770471111663, + "centroidMedianHz": 930.5954188145085, + "overSamples": 0, + "rmsDb": -19.84893033283087, + "crestDb": 7.253940339808832, + "noiseToToneDb": -4.49302081177872, + "noiseToToneADb": -3.568406831675894, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "tract/vowel-a", + "lenWasm": 34560, + "lenNative": 34560, + "maxErr": 0.0001921914517879486, + "maxErrAt": 28187, + "rmsErr": 0.000046111049670587934, + "relRmsErrDb": -52.60441410619507, + "wasm": { + "samplePeak": 0.08378415554761887, + "truePeakDb": -21.518327105526673, + "loudnessLufs": -34.251568232392565, + "centroidMedianHz": 1937.1220888731689, + "overSamples": 0, + "rmsDb": -33.80275018649363, + "crestDb": 12.265988122620389, + "noiseToToneDb": -10.436050344670264, + "noiseToToneADb": -12.885220061403881, + "lowMotion": false + }, + "native": { + "samplePeak": 0.08390921354293823, + "truePeakDb": -21.50539262673788, + "loudnessLufs": -34.23471535099468, + "centroidMedianHz": 1937.2296872141594, + "overSamples": 0, + "rmsDb": -33.78684837329708, + "crestDb": 12.263041385142849, + "noiseToToneDb": -10.447393994101596, + "noiseToToneADb": -12.89873810612136, + "lowMotion": false + }, + "flagsWasm": [ + "FAIL onsetMs" + ], + "flagsNative": [ + "FAIL onsetMs" + ], + "violations": [] + } + ] +} \ No newline at end of file diff --git a/docs/renders/aqua-feel-native-diff-smoother-old-2026-11-10.json b/docs/renders/aqua-feel-native-diff-smoother-old-2026-11-10.json new file mode 100644 index 0000000..486b785 --- /dev/null +++ b/docs/renders/aqua-feel-native-diff-smoother-old-2026-11-10.json @@ -0,0 +1,480 @@ +{ + "libfaustWasm": "2.90.19", + "sampleRate": 48000, + "items": "smoother", + "smoother": "old", + "rows": [ + { + "id": "thruster@rpm1200", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.04497168958187103, + "maxErrAt": 141577, + "rmsErr": 0.007318071121139892, + "relRmsErrDb": -21.224986931184873, + "wasm": { + "samplePeak": 0.19161102175712585, + "truePeakDb": -14.343339234769212, + "loudnessLufs": -26.051033400616078, + "centroidMedianHz": 2114.222071076734, + "overSamples": 0, + "rmsDb": -21.48680915202533, + "crestDb": 7.1352188868162605, + "noiseToToneDb": -4.949059402813134, + "noiseToToneADb": 12.652391835948997, + "lowMotion": false + }, + "native": { + "samplePeak": 0.1881352812051773, + "truePeakDb": -14.4953016770333, + "loudnessLufs": -26.05000842555788, + "centroidMedianHz": 2112.509030190081, + "overSamples": 0, + "rmsDb": -21.48554731845163, + "crestDb": 6.974952256138389, + "noiseToToneDb": -4.986880144493831, + "noiseToToneADb": 12.399428225827492, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "advanced-alien-multivalve-graph", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.000010244548320770264, + "maxErrAt": 35688, + "rmsErr": 0.0000015676294788398326, + "relRmsErrDb": -72.45230428385759, + "wasm": { + "samplePeak": 0.03596847131848335, + "truePeakDb": -28.872530673301476, + "loudnessLufs": -44.01558191383624, + "centroidMedianHz": 1719.821403397199, + "overSamples": 0, + "rmsDb": -38.86917357972277, + "crestDb": 9.98761318843934, + "noiseToToneDb": 2.2946722668875523, + "noiseToToneADb": -35.26574829949449, + "lowMotion": false + }, + "native": { + "samplePeak": 0.03596773371100426, + "truePeakDb": -28.872900426797354, + "loudnessLufs": -44.015640274857304, + "centroidMedianHz": 1719.8495576360149, + "overSamples": 0, + "rmsDb": -38.86920427795782, + "crestDb": 9.987465762817079, + "noiseToToneDb": 2.294718621072647, + "noiseToToneADb": -35.265694920035834, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "advanced-bird-syrinx", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.0016491450369358063, + "maxErrAt": 45282, + "rmsErr": 0.00025934850824015885, + "relRmsErrDb": -31.68498145060413, + "wasm": { + "samplePeak": 0.0724848210811615, + "truePeakDb": -22.789532845508607, + "loudnessLufs": -39.384826220237244, + "centroidMedianHz": 3246.9736644772215, + "overSamples": 0, + "rmsDb": -35.86925853361916, + "crestDb": 13.074199958593006, + "noiseToToneDb": 3.395163753535492, + "noiseToToneADb": -0.26769809662280863, + "lowMotion": false + }, + "native": { + "samplePeak": 0.07248431444168091, + "truePeakDb": -22.78984322120366, + "loudnessLufs": -39.38565490955099, + "centroidMedianHz": 3246.899582775747, + "overSamples": 0, + "rmsDb": -35.86920348626081, + "crestDb": 13.074084200179179, + "noiseToToneDb": 3.396666156772234, + "noiseToToneADb": -0.2667876831196381, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "advanced-bird-syrinx-graph", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.0002693869173526764, + "maxErrAt": 32700, + "rmsErr": 0.00003826655739568763, + "relRmsErrDb": -47.117064721842894, + "wasm": { + "samplePeak": 0.06643299758434296, + "truePeakDb": -23.549220126103343, + "loudnessLufs": -41.272964930830874, + "centroidMedianHz": 3608.3773722521782, + "overSamples": 0, + "rmsDb": -35.835637421519465, + "crestDb": 12.28331440320251, + "noiseToToneDb": 3.205981107524842, + "noiseToToneADb": -14.050756471755903, + "lowMotion": false + }, + "native": { + "samplePeak": 0.06643303483724594, + "truePeakDb": -23.549216558140902, + "loudnessLufs": -41.27307677467835, + "centroidMedianHz": 3608.449964447863, + "overSamples": 0, + "rmsDb": -35.835623258716865, + "crestDb": 12.283305111089557, + "noiseToToneDb": 3.20619543415809, + "noiseToToneADb": -14.048989011382716, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "advanced-syrinx-voice", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.00035776360891759396, + "maxErrAt": 33071, + "rmsErr": 0.00004987960856320729, + "relRmsErrDb": -34.59218482563713, + "wasm": { + "samplePeak": 0.013509727083146572, + "truePeakDb": -37.32985445982076, + "loudnessLufs": -43.74666608622773, + "centroidMedianHz": 3723.272760511277, + "overSamples": 0, + "rmsDb": -46.796675488586175, + "crestDb": 9.409607002734177, + "noiseToToneDb": 1.790967089244596, + "noiseToToneADb": 1.7997448212983471, + "lowMotion": false + }, + "native": { + "samplePeak": 0.013522684574127197, + "truePeakDb": -37.32721733719029, + "loudnessLufs": -43.74636063400179, + "centroidMedianHz": 3723.1202654076387, + "overSamples": 0, + "rmsDb": -46.79638101226304, + "crestDb": 9.41763937099232, + "noiseToToneDb": 1.7908757840446228, + "noiseToToneADb": 1.7996603730072778, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "advanced-human-larynx-graph", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.00003339786780998111, + "maxErrAt": 29445, + "rmsErr": 0.0000091717665877173, + "relRmsErrDb": -46.55598808562419, + "wasm": { + "samplePeak": 0.007785667199641466, + "truePeakDb": -42.173192592326515, + "loudnessLufs": -50.40895750738575, + "centroidMedianHz": 310.7359227777714, + "overSamples": 0, + "rmsDb": -50.08733256154268, + "crestDb": 7.913249276521016, + "noiseToToneDb": -30.80971405508631, + "noiseToToneADb": -27.762876726971946, + "lowMotion": false + }, + "native": { + "samplePeak": 0.00778665067628026, + "truePeakDb": -42.172393196766, + "loudnessLufs": -50.40839338870849, + "centroidMedianHz": 310.88482517283643, + "overSamples": 0, + "rmsDb": -50.086764610534054, + "crestDb": 7.913778447935089, + "noiseToToneDb": -30.8115791514923, + "noiseToToneADb": -27.76354669965494, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "dx7-algorithm-32-additive-organ", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.00016482919454574585, + "maxErrAt": 23021, + "rmsErr": 0.000020542966407864508, + "relRmsErrDb": -79.45691216935256, + "wasm": { + "samplePeak": 0.6861975789070129, + "truePeakDb": -3.270050577733717, + "loudnessLufs": -9.759294906714691, + "centroidMedianHz": 656.4219631461098, + "overSamples": 0, + "rmsDb": -8.539888188883904, + "crestDb": 5.268871817143614, + "noiseToToneDb": -37.019826930432856, + "noiseToToneADb": -37.7883341551492, + "lowMotion": false + }, + "native": { + "samplePeak": 0.6861848831176758, + "truePeakDb": -3.2702216053436386, + "loudnessLufs": -9.759310761718336, + "centroidMedianHz": 656.4199149750565, + "overSamples": 0, + "rmsDb": -8.539903940430642, + "crestDb": 5.268726863889608, + "noiseToToneDb": -37.01979015752506, + "noiseToToneADb": -37.78830074856767, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "dx7-algorithm-8-bright-pair", + "lenWasm": 144000, + "lenNative": 144000, + "maxErr": 0.000004217028617858887, + "maxErrAt": 4395, + "rmsErr": 4.0587175382789543e-7, + "relRmsErrDb": -94.24174055880029, + "wasm": { + "samplePeak": 0.1503547728061676, + "truePeakDb": -16.445110330444138, + "loudnessLufs": -27.707288476356375, + "centroidMedianHz": 570.7272310041899, + "overSamples": 0, + "rmsDb": -25.22822394507269, + "crestDb": 8.7705683198403, + "noiseToToneDb": -33.00382123099267, + "noiseToToneADb": -31.168321209089953, + "lowMotion": false + }, + "native": { + "samplePeak": 0.1503528207540512, + "truePeakDb": -16.44522744967477, + "loudnessLufs": -27.707277125976997, + "centroidMedianHz": 570.7333021952353, + "overSamples": 0, + "rmsDb": -25.228219761996577, + "crestDb": 8.770451367352216, + "noiseToToneDb": -33.00373339984028, + "noiseToToneADb": -31.168249293529612, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "ding-instrument/glass-chime", + "lenWasm": 72000, + "lenNative": 72000, + "maxErr": 0.0249517853371799, + "maxErrAt": 16135, + "rmsErr": 0.004421638829626474, + "relRmsErrDb": -21.888160741392436, + "wasm": { + "samplePeak": 0.17621681094169617, + "truePeakDb": -15.062032445423984, + "loudnessLufs": -21.56737037885148, + "centroidMedianHz": 1602.6138671100102, + "overSamples": 0, + "rmsDb": -22.844945161193554, + "crestDb": 7.765891909110638, + "noiseToToneDb": -3.1846833757850774, + "noiseToToneADb": -2.8894771746074213, + "lowMotion": false + }, + "native": { + "samplePeak": 0.1762169897556305, + "truePeakDb": -15.06203177829347, + "loudnessLufs": -21.567324293584342, + "centroidMedianHz": 1602.7309843808985, + "overSamples": 0, + "rmsDb": -22.8449443529706, + "crestDb": 7.765899914787472, + "noiseToToneDb": -3.15615732806026, + "noiseToToneADb": -2.8631443461744297, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "ding-instrument/muted-pluck", + "lenWasm": 72000, + "lenNative": 72000, + "maxErr": 0.0006083704647608101, + "maxErrAt": 11771, + "rmsErr": 0.00011333511339155152, + "relRmsErrDb": -50.15824170095642, + "wasm": { + "samplePeak": 0.18485014140605927, + "truePeakDb": -14.661061484678669, + "loudnessLufs": -27.09375077529527, + "centroidMedianHz": 337.55371147245654, + "overSamples": 0, + "rmsDb": -22.505768680170274, + "crestDb": 7.8421644228588985, + "noiseToToneDb": -1.8131427242661857, + "noiseToToneADb": -3.7732237451762884, + "lowMotion": false + }, + "native": { + "samplePeak": 0.18485012650489807, + "truePeakDb": -14.661062836676077, + "loudnessLufs": -27.093759010723893, + "centroidMedianHz": 337.5238499031714, + "overSamples": 0, + "rmsDb": -22.505770398210146, + "crestDb": 7.842165440710797, + "noiseToToneDb": -1.8131374325665277, + "noiseToToneADb": -3.773589944716136, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "ding-instrument/soft-triangle", + "lenWasm": 72000, + "lenNative": 72000, + "maxErr": 0.0023703736951574683, + "maxErrAt": 25715, + "rmsErr": 0.0009930534248776234, + "relRmsErrDb": -35.17725332142618, + "wasm": { + "samplePeak": 0.19771148264408112, + "truePeakDb": -14.073758822230804, + "loudnessLufs": -24.407607566689737, + "centroidMedianHz": 432.02737524513924, + "overSamples": 0, + "rmsDb": -22.837073455193796, + "crestDb": 8.757711313308086, + "noiseToToneDb": -7.16850817229934, + "noiseToToneADb": -9.027202849078392, + "lowMotion": false + }, + "native": { + "samplePeak": 0.19771143794059753, + "truePeakDb": -14.07376035946359, + "loudnessLufs": -24.407608271539363, + "centroidMedianHz": 432.56670373115156, + "overSamples": 0, + "rmsDb": -22.837076588859357, + "crestDb": 8.757712483053472, + "noiseToToneDb": -7.168496214436587, + "noiseToToneADb": -9.027680538222803, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "ding-instrument/warm-bell", + "lenWasm": 72000, + "lenNative": 72000, + "maxErr": 0.031899021938443184, + "maxErrAt": 20346, + "rmsErr": 0.0064883595529472505, + "relRmsErrDb": -22.867583977030858, + "wasm": { + "samplePeak": 0.23455719649791718, + "truePeakDb": -12.586378660039248, + "loudnessLufs": -19.50785299525781, + "centroidMedianHz": 930.5251827853554, + "overSamples": 0, + "rmsDb": -19.848928933876216, + "crestDb": 7.2539041772066515, + "noiseToToneDb": -4.487748875412059, + "noiseToToneADb": -3.561906168663185, + "lowMotion": false + }, + "native": { + "samplePeak": 0.23455692827701569, + "truePeakDb": -12.58637491873251, + "loudnessLufs": -19.50777054747485, + "centroidMedianHz": 930.5575374762606, + "overSamples": 0, + "rmsDb": -19.84893016359035, + "crestDb": 7.253895474424504, + "noiseToToneDb": -4.485889851528931, + "noiseToToneADb": -3.56069167084123, + "lowMotion": false + }, + "flagsWasm": [], + "flagsNative": [], + "violations": [] + }, + { + "id": "tract/vowel-a", + "lenWasm": 34560, + "lenNative": 34560, + "maxErr": 0.00006673485040664673, + "maxErrAt": 28187, + "rmsErr": 0.000017135153866313287, + "relRmsErrDb": -61.202754106950046, + "wasm": { + "samplePeak": 0.08378415554761887, + "truePeakDb": -21.518327105526673, + "loudnessLufs": -34.251568232392565, + "centroidMedianHz": 1937.1220888731689, + "overSamples": 0, + "rmsDb": -33.80275018649363, + "crestDb": 12.265988122620389, + "noiseToToneDb": -10.436050344670264, + "noiseToToneADb": -12.885220061403881, + "lowMotion": false + }, + "native": { + "samplePeak": 0.08382102102041245, + "truePeakDb": -21.514509846938733, + "loudnessLufs": -34.245380439304775, + "centroidMedianHz": 1937.234382427543, + "overSamples": 0, + "rmsDb": -33.796727413065646, + "crestDb": 12.263786346131285, + "noiseToToneDb": -10.440493522060173, + "noiseToToneADb": -12.890432956436477, + "lowMotion": false + }, + "flagsWasm": [ + "FAIL onsetMs" + ], + "flagsNative": [ + "FAIL onsetMs" + ], + "violations": [] + } + ] +} \ No newline at end of file diff --git a/docs/renders/dx7-algorithm-32-native-diff-cause.md b/docs/renders/dx7-algorithm-32-native-diff-cause.md new file mode 100644 index 0000000..2a1c02c --- /dev/null +++ b/docs/renders/dx7-algorithm-32-native-diff-cause.md @@ -0,0 +1,57 @@ +# dx7-algorithm-32-additive-organ: why native vs wasm reads -79.5 dB + +**Nothing here was heard.** Offline buffers, 48 kHz, mono, 3 s, no params, same Faust text on both sides. Same toolchain as `aqua-feel-native-diff-2026-11-10.md` (wasm: faustwasm 0.20.0 / libfaust 2.90.19; native: `FaustNativeRender`, libfaust 2.90.20, `-single -ftz 2 -vec -lv 1`, faustlibraries master 3d8c6ef via `AQUASYNTH_FAUST_HOME`). + +## Verdict: cause found, two causes, neither a float32 floor and neither a bug in AquaSynth + +The -79.5 dB is a real, deterministic phase drift of the sine voices, not noise, not a quiet signal and not a sample shift. + +1. **Increment rounding in `os.phasor`, selected by whether `1/ma.SR` is shared** (-79.3 to -93.0 dB when removed). Lowered patches define `time = ba.time / ma.SR;`. With that division in the graph, the native compiler computes the phasor increment as `freq * fround(1/SR)`; the wasm compiler keeps `freq / SR`. For 392 Hz at 48 kHz: `fround(392/48000) = 0.008166667074`, `fround(392 * fround(1/48000)) = 0.008166666143`, one 2^-30 step (9.31e-10) apart. Measured effective increments: wasm 0.008166667074 on both, native 0.008166667074 without the division in the graph and **0.008166666143** with it. The phase then drifts linearly (about 1.3e-4 cycles over 3 s between the sides), so the waveform error grows linearly with time (0 to 1.65e-4 over 0.45 s, then follows the envelope down). +2. **`si.smoo` library text** on `/macro/drawbar` and `/macro/shimmer` (-93.0 to -141.2 dB when also pinned). This is the known old-form vs `imp(3)` difference. The 2026-11-10 imp3 pin did not show it because cause 1 sat 14 dB above it. + +With both removed the patch reads **-141.2 dB (max 1.8e-7)**, the same float floor as the Ding instruments (-130 to -142 dB). + +## Evidence (isolating experiments; each changes one thing) + +Baseline reproduced through `native-diff.mjs --items smoother`: 1.65e-4 max, -79.27 dB. The signal is not quiet: RMS 0.193, peak 0.686. The last 2 s are digital silence and contribute zero error (73 % exactly-equal samples is that silence, not agreement). The error is zero at nothing but silence and rises linearly while the voices sound; lag test: lag 0 summed squared error 6.3e-5 vs 18.4 at lag +-1 (sums over 144000 samples), so no shift. + +| experiment | rel RMS err dB | max err | +|---|---|---| +| whole patch, as lowered | -79.3 | 1.65e-4 | +| filters stripped (`fi.lowpass`/`fi.highpass`) | -79.3 | 1.66e-4 | +| per voice, no filters: v0 / v1 / v2 / v3 / v4 / v5 | -85.8 / -79.3 / -143.3 / -73.8 / -143.1 / -109.0 | | +| `sin(2pi*os.phasor(1,392))*0.5` alone | -163.7 | 3e-8 | +| same sine times `age+1`, a ramp, or any slow signal | -64 to -74 | 1e-4 to 3e-3 | +| `os.phasor(1,392)` alone | exact (0) | 0 | +| `os.phasor(1,392) + (ba.time/ma.SR > 100)` (never true) | -33.2 | 1.0 (one wrap sample) | +| same with `ba.time > 1e9` or `ma.SR > 1e9` instead | exact (0) | 0 | +| same with `ba.time / 48000.0 > 100` | exact (0) | 0 | +| effective phasor increment, native, with / without the division | 0.008166666143 / 0.008166667074 | | +| whole patch, `time = ba.time / 48000.0;` only | -93.0 | 1.78e-5 | +| whole patch, every `ma.SR` a literal | -93.0 | 1.78e-5 | +| whole patch, phasor replaced by a float-accumulating one on both sides (library phasor text is fixed point in master, see below) | -84.0 | 9.4e-5 | +| age literal + `si.smoo` pinned to old form / imp3 | -114.0 / **-141.2** | 1.3e-6 / 1.8e-7 | +| `si.smoo` pinned to imp3 only (SR still shared) | -79.5 | 1.65e-4 | + +Voices 2 and 4 (588 and 980 Hz) are clean because their increments round the same either way; 196, 392 and 784 are not (1176 Hz rounds the same either way). After cause 1 is removed only voice 1 (the one scaled by `patch_param_0`, the smoothed drawbar) stays at -91.5 dB, and voice 5 (shimmer) at -109 dB: cause 2. + +Arithmetic check: `Math.fround(392 * Math.fround(1/48000))` = 0.008166666142642498, matching the measured native increment to 10 digits. + +## Scope + +- Other patches with the same `time = ba.time / ma.SR;` line are not affected the same way: with imp3 pinned and `time` made literal, `advanced-bird-syrinx-graph` stays -75.4, `advanced-human-larynx-graph` -94.2, `dx7-algorithm-8-bright-pair` -94.7, `advanced-alien-multivalve-graph` moves -97.9 to -110.9. Whether a patch is affected depends on whether its phasor increments round differently under division vs reciprocal multiply. +- `native-diff.mjs --smoother imp3` therefore cannot reach the float floor for patches whose phasor increment is affected; its `-60 dB` limit still holds with the new reading (-79.5 is explained, not a worse mismatch). + +## Not verified + +- I did not read the generated C++/wasm. "Native uses `freq * fround(1/SR)`, wasm `freq/SR`" is inferred from the measured increments and the arithmetic above (exact match), not from emitted code (the wasm build has no C++ backend; no `faust` CLI here). It is also not separated whether the difference is libfaust 2.90.19 vs 2.90.20, or the `-vec -lv 1` / LLVM path, or the library's phasor text (fixed point in master, which also looks different from what the wasm bundle holds). Settling it: emit the same DSP with one libfaust version's C++ backend and compare the `instanceConstants` lines, or render the wasm side with a 2.90.20 wasm build. +- Which side is nearer the ideal is not important: both are within one 2^-30 increment step of 392/48000 and in opposite directions (+4.1e-10, -5.2e-10). + +## Reproduce + +``` +. /var/lib/mind-aqua-self/env-review.sh +export AQUASYNTH_FAUST_HOME=/var/lib/mind-aqua-self/faust-home-overlay-20261031 +cd tools/aqua-feel && node native-diff.mjs /var/lib/mind-aqua-self/native-diff-bin --items smoother --out "$TMPDIR/out" +``` +then edit the lowered DSP text on both sides (as the table says) and render with `renderOnce` and `FaustNativeRender`. The experiment scripts are scratch in `$TMPDIR` and not committed. diff --git a/docs/renders/thruster-v5-manifest.json b/docs/renders/thruster-v5-manifest.json new file mode 100644 index 0000000..1907dea --- /dev/null +++ b/docs/renders/thruster-v5-manifest.json @@ -0,0 +1,46 @@ +{ + "patch": "thruster", + "dspSha256": "8afc045eab94f396e21d96a262c5d71910103acd51da5913d3eb3036b48a5809", + "sampleRate": 48000, + "blockFrames": 512, + "seconds": 4, + "rpmControl": "rpm", + "os": "Debian GNU/Linux 13 (trixie)", + "faustHome": "/var/lib/mind-aqua-self/faust-home-overlay-20261031", + "faustLibrary": "/var/lib/mind-aqua-self/faust-home-overlay-20261031/lib/libfaust.so", + "cases": [ + { + "label": "low-rpm600", + "file": "thruster-low-rpm600.wav", + "rpmStart": 600, + "rpmEnd": 600, + "frames": 192000, + "peak": 0.18832043, + "rms": 0.07348131804614891, + "nonFinite": false, + "sha256": "4aeaa1f4d9aa5d63025dd8b3d45b25fc6b030a366ba05237c0021b508308e0ba" + }, + { + "label": "high-rpm4800", + "file": "thruster-high-rpm4800.wav", + "rpmStart": 4800, + "rpmEnd": 4800, + "frames": 192000, + "peak": 0.3635283, + "rms": 0.14323418081979195, + "nonFinite": false, + "sha256": "390874aa1d5c4d50f251fa0735b82f859bba43ffee4236fd24d5b06a4733ef7d" + }, + { + "label": "sweep-600-4800", + "file": "thruster-sweep-600-4800.wav", + "rpmStart": 600, + "rpmEnd": 4800, + "frames": 192000, + "peak": 0.34473976, + "rms": 0.11363349353245127, + "nonFinite": false, + "sha256": "ad8d1508aa2b37b5038a6acfcfb21fc46ae04f0180adcd6ea60edc7e918e4068" + } + ] +} \ No newline at end of file diff --git a/docs/research/articulatory-vocal-tract-sources.md b/docs/research/articulatory-vocal-tract-sources.md new file mode 100644 index 0000000..d1f350e --- /dev/null +++ b/docs/research/articulatory-vocal-tract-sources.md @@ -0,0 +1,167 @@ +# Articulatory vocal tract: sources and what AquaSynth takes from them + +Read 2026-11-08 for follow-up `aqua-body:follow_up:chat-s4456-4456`. The full fact sheet with quoted values and the list of +what could not be retrieved is the Eyes pass `tract-literature-20261108` (research notes in the aqua mind; +working copy `work/tract-literature-facts.md` in the aqua workspace). This page keeps only what the tract design rests on. +Nothing built from it has been heard by anyone; every number below is a measurement or a published value, not a listening result. + +## Sources and what each decides + +| Source | What we take | What we do not take | +|---|---|---| +| Kelly & Lochbaum 1962; Smith, *Physical Audio Signal Processing* (KL junctions); Liljencrants 1985 / Story 1995 (section length `c / 2Fs`, i.e. one sample per section); Mullen, Howard & Murphy 2006 (boundary reflections, wall loss 0.90-0.98 sets bandwidth) | The ladder: one cell per sample, junction `r = (A[i-1]-A[i])/(A[i-1]+A[i])`, glottal reflection about +0.75, lip reflection about -0.85, a per-pass loss factor for bandwidth | Fractional-delay cells (Välimäki & Karjalainen, Mathur & Story): needed for continuous length change, not built; the tract length is fixed per patch | +| Pink Trombone (Neil Thapen 2017, MIT; read in chdh/pink-trombone-mod `Tract.ts`) | Constants (glottal 0.75, lip -0.85, 0.999 per pass), the 3-port velum junction `rL=(2A_k-S)/S`, `rR`, `rN`, with `S = A_k + A_(k+1) + A_velum` | Its 44-cell tract at twice the sample rate and its Gaussian tongue model as the vowel source: the vowel shapes now come from measured area functions | +| Story, Titze & Hoffman 1996/1998 (MRI area functions; numeric tables paywalled) and **Story 2008, JASA 123(1):327-335, PMC2377017, Table I** (44-section area functions of 11 vowels incl. /ɪ/ /ʊ/, one adult male, 2002 scan) | The 11 vowel area functions, as data: `docs/research/data/story2008-area-functions.csv` | Story's 1994 set (plot only). The sizes are one speaker's, not a population mean (see the measured gap below) | +| Story 2005, JASA 117(5):3231-3254 (area function model: vowel substrate `(pi/4)[Omega + q1 phi1 + q2 phi2]^2`, consonant constriction `C(i) = 1 - exp(-ln16((i-lc)/rc)^2)`, nasal port at about section 22) | The principle: a vowel is a point in a low-dimensional space of whole area functions, blended in diameter space, with a consonant constriction laid on top of it. We blend the 11 measured shapes by weights instead of PCA modes (Omega, phi1, phi2 are in the paper's Table III; not transcribed) | Its 14-parameter control set | +| VocalTractLab (Birkholz; GPL; 2.4 manual, 2005 dissertation) | Nasal cavity area function (Dang & Honda subject, 19 sections x 0.6 cm = 11.4 cm, 0.76-4.24 cm^2); the observation that nasal antiresonances come from the oral cavity as a side branch of the open velum; c = 350 m/s; the wall-loss and radiation (R||L) constants as the reference for the independent check | Its 3D articulators and glottis models; the four paranasal sinus Helmholtz resonators (zeros at 552-1682 Hz) and piriform fossae are not modelled | +| Maeda 1979/1990 (seven jaw/tongue/lip/larynx parameters from cineradiographs; `A = alpha d^beta`) | Structure only: the argument for articulator-level parameters over formant knobs | Per-vowel parameter values (not retrievable); no Maeda parameterisation is built | +| Nasal coupling: Fujimura 1962 (nasal murmur: very low N1 near 250-400 Hz, zeros), Dang & Honda 1994/1995, Story 2005 tier IV | A velum that opens the 3-port junction; the murmur's low first formant and the oral side-branch zero as the measured behaviour to look for | Per-consonant zero ranges for /m n ŋ/: no readable primary source | +| LF glottal flow (Fant, Liljencrants & Lin 1985; Fant 1995 `Rd`; Gobl 2021; Wang & Gobl 2023 regressions `Rkp = 0.118 Rd + 0.224`, `Rap = 0.048 Rd - 0.01`) | Reference for the source spectrum: Rd is a tension axis (low = tense), tilt cutoff falls as Rd rises. The current source is a band-limited saw through a one-pole lowpass whose cutoff is the `tilt` control: it matches the LF pulse's spectral tilt only qualitatively | An LF pulse generator: not built. If the source is moved to LF, `Rd` replaces `tilt` | +| Hillenbrand et al. 1995, JASA 97(5):3099-3111 (male means computed from the public data of github.com/santiagobarreda/hillenbrand_et_al_1995, MIT) and Peterson & Barney 1952 (means from phonTools `pb52`) | The reference formants F1/F2/F3 per vowel for the verification, table below | Table V itself was not read; P&B vowel labels are inferred from the data codes | +| Stress: Sluijter & van Heuven 1996 (ICSLP table + JASA abstract); Fry 1955/58, Lieberman 1960 (secondary reports only) | Stressed syllables are longer (stressed 220 ms against 160 ms unstressed in the initial-stress pairs; a stressed/unstressed ratio of 1.68 in a native-speaker study), 1-3 dB louder overall, f0 higher by about 6 percent when unaccented; duration is the most reliable cue, f0 the weakest in unaccented words | Fry's and Lieberman's own magnitudes (not retrieved) | +| Plosives: Lisker & Abramson 1964 (VOT, secondary table), Chodroff & Wilson 2014 (burst spectra, /b/ COG 2833 Hz, peak 1643 Hz), Stevens & Blumstein (labial onset spectrum diffuse-falling) | A /b/ burst is a short low-frequency-weighted transient with a short voice-onset lag (0-5 ms mean 1 ms for English /b/) | A /b/ voice-bar level in dB and burst duration norms: no source retrieved, so the AquaSynth levels for them are design choices, labelled as such | + +## Male reference formants used for verification (Hz) + +Hillenbrand 1995 male means (45 men, steady state; computed from the public data): /i/ 343/2323/3001, /ɪ/ 429/2034/2687, +/e/ 476/2090/2692, /ɛ/ 588/1803/2604, /æ/ 591/1930/2595, /ɑ/ 757/1309/2535, /ɔ/ 656/1023/2521, /o/ 498/910/2459, +/ʊ/ 469/1123/2435, /u/ 380/992/2355, /ʌ/ 621/1181/2548. Peterson & Barney 1952 male means (33 men): /i/ 267/2294/2937, +/ɪ/ 392/1993/2569, /ɛ/ 526/1854/2481, /æ/ 664/1727/2420, /ʌ/ 631/1192/2377, /ɑ/ 718/1091/2442, /ɔ/ 568/836/2403, +/ʊ/ 437/1023/2245, /u/ 307/876/2239. + +## The gap the area functions must be read against + +Story's shapes belong to one speaker. Computed independently (frequency-domain tube chain, `tools/formant-check/tube_formants.py`, +no AquaSynth code), the tabulated area functions at their own lengths give for example /i/ 261/2139, /ʌ/ 561/827, /ɑ/ 666/933, +/u/ 243/684 Hz, against the population means above (/i/ 343/2323, /ʌ/ 621/1181, /ɑ/ 757/1309, /u/ 380/992). So a render that +follows Story's area functions can be correct and still sit 10-30 percent from a population mean. The verification therefore +reports two distances: render against the area function's own predicted formants (does the ladder implement the tube), and +render against the population means (is the speaker typical). Only the first is a defect test. + +## Ladder against the tube chain (measured 2026-11-08, unheard) + +The ladder's lip termination is the open-end reflectance R(z) = -g (1-b)/(1-b z^-1) (Smith PASP; Valimaki and Karjalainen 1994; Levine and +Schwinger): g = 1 is the radiation load's `reflection` magnitude (sign stays negative), and b follows the lip cell's area, with a = sqrt(A/pi) cm, +q = (a fs / c)^2, c = 35000 cm/s, b/(1-b)^2 = q, b = ((2q+1) - sqrt(4q+1)) / (2q), clamped to [0, .9]. The low-frequency loss 1-|R|^2 is then +(ka)^2 and the DC group delay b/(1-b) samples is the end correction. A constant -.82 reflection, the earlier termination, had no F2 peak for +the rounded and back vowels and no end correction. + +`tools/formant-check/ladder_formants.py --filtered` runs the same ladder in numpy (24 cells, 17.52 cm, 350 m/s, loss .998, glottal reflection .75). +Against the independent tube chain (`tube_formants.py`, 17.52 cm; baffled-piston R||L radiation) the numpy ladder reads at g = 1 (F1/F2 error %): +i +1.4/+0.4, ih +1.7/+1.5, e +2.3/+4.2, eh +4.0/+3.1, ae +2.9/+3.5, uh +5.6/+4.3, ah +5.1/+6.6, aw +8.6/+3.7, o +8.2/+5.0, oo_short +5.5/+8.2, +u +6.0/+8.2. Lowering g does not help (g = .95: aw F1 +9.6; g = .9: the aw F2 peak is lost). The numpy ladder therefore reads systematically high +against the tube chain: F1 +4 to +9%, F2 +3 to +8% for the back vowels. That bias is diagnosed, not isolated: raising the glottal reflection to .9 moves only /aw/ F1 +(8.6 to 6.2%); a 1.4 times larger end correction leaves a worst case of 6.9%; the remaining suspects (cell-length quantisation of a 24.03-cell +tract, and that the reference itself uses a different radiation model) are untested. It is the open item. + +Held tolerances (`VocalTractFormantTests`, harmonic-line reading of the 10 Hz render): the front vowels i ih e eh ae within 6% on F1 and F2; +the back and rounded vowels within 10%, which is the measured disagreement between two lip radiation models (the ladder's one-pole +Levine-Schwinger-form reflectance, the tube chain's baffled piston R||L) plus the ladder's cell quantisation. All eleven meet their bands. For +a while uh, ah, aw, o and oo_short read F1 +14.8, +10.8, +12.4, +25.8 and +13.0% against the tube chain and the tests were skipped with those +numbers; the numpy ladder read those F1 20-85 Hz lower than the render. The cause was the velopharyngeal junction in `FaustExport.VocalLadder.cs`: +it summed the areas of oral cells 11 and 12 and used their reflection coefficients for the junction that sits between cells 10 and 11, so the +pharynx (cells 10-12 of /o/: .52 .33 .52 cm^2) carried the next step's reflection with the wrong sign. Isolated by running the lip wave of the rendered +ladder under an impulse against the numpy copy (the first echo already differed: -0.30 against -0.23), with the velum shut. Fixed, the render agrees +with the numpy ladder to 10 Hz (o 520/830, uh 580/840, ah 690/980, aw 550/720, oo_short 500/920, u 290/820 at the harmonic lines) and the five tests +run. Read by Burg LPC on the renders (Soul pass of 2026-11-09, vowel window about 100-200 ms), F1 error against the tube chain fell from 5c32bf6 to the fix 05efdc9 as uh +5.3 to -0.5, ah +2.5 to -4.3, +aw +4.2 to +0.7, o +16.7 to +5.7, oo_short +12.7 to +4.9 percent (a reconcile rerun on the same renders, 2026-11-10, reproduces these to within 1 point in the 100-200 ms window, aw least: +3.6 to -0.1). +The figures depend on the window: in the 70-150 ms window of the table below the same renders read uh +4.4 to -2.1, ah -0.6 to -6.6, aw +1.3 to -3.4, o +16.3 to +2.3, oo_short +8.8 to +1.7, and the two vowels +the fix did not repair, u and i, read +10.7 to +13.8 and +15.6 to +17.1 (u +4.6 to +6.4 and i +10.8 to +12.2 at 100-200 ms). So the junction fix removed the large positive bias of o and oo_short in either window (ah turns from near zero or slightly high to 4-7 percent low), +and the earlier "-4.3..+5.9 after" range is the five repaired vowels plus u read at one window: it does not cover i and u, which stay 5-17 percent high by Burg and are the LPC's resolution limit at 140 Hz +(see the table note); ah undershoots by 4 to 7 percent after the fix. The earlier "12-26 percent high becomes 1.5-9 percent high" came from the harmonic-line picker, not from LPC. What remains against the tube chain +(F1 +1.5 to +9%, F2 +2 to +8% at the harmonic lines) is the two radiation models' disagreement above. + +Burg LPC (`burg_formants.py`) on the WordRender renders at 140 Hz (held vowels): /i/ in nibu 279/2105/2924, /u/ in nibu 283/836/2637, /ih/ in bin +359/2012/2536, /oo_short/ (bʊn) in bun 543/899/2781, against the tube chain i 251/2063/2877, u 271/761/2574, ih 346/1987/2491, oo_short 478/850/2760 and +Hillenbrand male means i 343/2323/3001, u 380/992/2355, ih 429/2034/2687, oo_short 469/1123/2435. + +## Not retrieved (from the fact sheet) + +Story-Titze-Hoffman 1996/1998 numeric tables; Fant 1960, Stevens-House, Wakita, Ladefoged tables; Maeda per-vowel values; +Dang & Honda 1994 text; per-consonant nasal zero ranges; Fry and Lieberman magnitudes; Klatt 1975; burst duration norms and the +/b/ voice-bar level; Liljencrants 1985; the JOS book tract chapters. The Story 2008 table came through a page summariser, not a PDF +text extraction; every column was checked by computing its formants (all eleven give vowel-typical F1/F2 orderings), which +catches a gross transcription error, not a small one. + +## Render verification, stress cue and plosive burst (2026-11-08, unheard) + +Renders: `WordRender` at f0 140 Hz, one 180 ms vowel per file (`v_`), plus /ɑmɑ/, /ɑnɑ/, /ˈnibu/ with and without primary stress, /bɪn/, /bʊn/. +Estimator: `tools/formant-check/verify_renders.py` over `burg_formants.py` (Burg LPC order 12 at 10 kHz, 30 ms frames, vowel window 70-150 ms), a separate +Python process that shares no code with the C# tests or the ladder; references are Hillenbrand 1995 and Peterson & Barney 1952 male means (above) and the +tube chain at 17.52 cm (`tube_formants.py`). Files: `/opt/commons/handoff/aqua-tract-articulatory-20261108` (wav/, scripts/, manifest.json, verify.txt, verify.json). + +| vowel | Burg F1/F2 | tube chain F1/F2 | render vs tube F1/F2 % | Hillenbrand F1/F2 | render vs Hillenbrand % | +|---|---|---|---|---|---| +| i | 294/2080 | 251/2063 | +17.0/+0.8 | 343/2323 | -14.3/-10.4 | +| ɪ | 375/2024 | 346/1987 | +8.5/+1.8 | 429/2034 | -12.5/-0.5 | +| e | 448/1925 | 432/1827 | +3.7/+5.4 | 476/2090 | -5.9/-7.9 | +| ɛ | 551/1603 | 527/1571 | +4.5/+2.0 | 588/1803 | -6.3/-11.1 | +| æ | 739/1550 | 729/1523 | +1.4/+1.8 | 591/1930 | +25.0/-19.7 | +| ʌ | 538/822 | 549/809 | -2.1/+1.6 | 621/1181 | -13.4/-30.4 | +| ɑ | 607/950 | 650/913 | -6.6/+4.0 | 757/1309 | -19.8/-27.4 | +| ɔ | 499/715 | 516/692 | -3.4/+3.4 | 656/1023 | -24.0/-30.1 | +| o | 488/806 | 477/787 | +2.3/+2.4 | 498/910 | -2.0/-11.4 | +| ʊ | 486/894 | 478/850 | +1.7/+5.2 | 469/1123 | +3.7/-20.4 | +| u | 308/811 | 271/761 | +13.8/+6.6 | 380/992 | -18.9/-18.2 | + +(Table re-rendered 2026-11-09 after the junction fix, the nasal and stress changes below; the first table, 2026-11-08, had o, ʊ and u 9-16% high.) + +Against its own area function the render sits within 6.6 percent on F1 for every vowel but /i/ (+17%) and /u/ (+13.8%), whose F1 near 270-300 Hz is under the LPC's resolution at a +140 Hz line spacing (the 10 Hz harmonic-line reading in `VocalTractFormantTests` puts them at 260 and 290 Hz, +3.6 and +7.0%); F2 of all eleven is within 6.6 percent. Against the population +means the render is 8-30 percent off for the back vowels' F2, which is the one-speaker gap described above, not a defect test. + +Nasal (steady parts: the consonant's middle 60 percent against the mean power of the flanking vowels, 70 ms before and 90 ms from 30 ms after): /ɑmɑ/ -11.8 dB, /ɑnɑ/ -9.6 dB, Burg N1 295 and 332 Hz, +inside Fujimura's low-N1 range. The target band is 8 to 16 dB under the vowels, a DESIGN value: no primary level was retrieved (secondary accounts of Fujimura 1962 and later put nasal murmurs +well under their vowels). Before: -13.2 and -3.8 dB by the same measure (the old whole-segment rms read -2.8 and +1.8 dB). Three causes, in order: the /n/ oral closure began to open at half the gesture, +a third before the velum and the contact (held to 0.8 now); then, with the junction on its own interface the nose took its share and both murmurs rose to -4.2 and -2.2 dB; the nasal path now carries +the damping of the nasal cavity (soft walls, turbinates, sinuses): loss .975 per cell, a bandwidth of about 380 Hz at this sample rate (`IpaGestureExperiment.NasalPathLoss`; the bandwidth is a design value, +none retrieved). /n/ stays 2 dB louder than /m/: the cause is not isolated (a shorter oral side branch is the physical candidate). + +Stress (cue values in `WeksaWordPlan.CueFor`): primary stress renders longer by 75 ms for the vowel (design 68 ms), f0 148.1 against 139.9 Hz (ratio 1.059; design 1.06). Level: +2.0 dB mean over the five vowels +i a u e o (`PrimaryStressRendersAboutTwoDecibelsLouderOnAverageOverVowels`, 2.05 dB in the last run) with the amplitude factor at 1.30 (was 1.26: +1.7 dB). The f0 rise alone moves the harmonics against F1 and +changes the level by -1.3 to +1.8 dB by vowel, so a single vowel is a noisy measure of the amplitude cue: /i/ in /nibu/ (steady window 150-250 ms) reads +0.21 dB in the last render (+0.99 before), isolated /i/ ++0.79 dB, /e/ +4.1 dB. The mean is what is held; the scatter between vowels is the harmonic/formant interaction of a 140 Hz voice, not the cue. + +Plosive: word-initial /b/ closure is the tract wall's voice bar, -29.2 dB re the following vowel, /p/ -40.1 dB (`PlosiveClosureCarriesTheVoiceBarOnlyWhenVoiced`); /bɪn/ closure peak 0.0036 (was 0.0000, below 16-bit +resolution), release 60-95 ms peak 0.0695, vowel 0.129. The wall radiation reads the first-cell pressure of the ladder (R0 + L0), band-limits it to 90-300 Hz and adds it at gain 0.7 (a DESIGN value for a voice-bar +level of -30 dB; the primary level was not retrieved, and the 2.4 manual's skin radiation, Birkholz & Zhang 2025, is the reference to read). In a vowel the term moves the f0 line by under 1 dB; the 200 and 300 Hz harmonics move up to +1.5 dB on one open vowel (wall-only measurement, Soul pass). The band is two second-order sections, not a brick wall: with the mouth and the velum shut the bar sits about 25 dB down above 300 Hz. The burst +itself (difference of renders with and without `release_flow`), before the #47 fix (c0e6b98; see the re-measure below for the post-fix figures), peaked at about 0.006-0.008, -24 dB re the vowel peak; the release peak in the table above is the voicing breaking through as the contact opens. Burst gain 0.06 is a DESIGN VALUE. + +Weak spots found in the renders, 2026-11-08 (not re-measured on 2026-11-12 unless named in the next paragraph): (1) the vowel level is not steady, it rises for about 100 ms after onset (ɑ: rms 0.028 at 80 ms to 0.079 at 160 ms) so any window early in a vowel +reads low; the cause is not isolated (surface smoothing and the pressure ramp are the suspects). (3) The voice bar's level is a design value and its band +(90-300 Hz) assumes f0 of 90 Hz and up. (4) Only f0 and level of the first vowel are checked per cue; the cues' perceptual strength is unknown. Everything here is a measurement, nothing was heard. + +Re-measure 2026-11-12. The stress, nasal and lax-vowel figures were taken on main c0e6b98, before the #47 burst fix (which does not touch them); the plosive-release bullet gives the c0e6b98 figures as "before" and the main af0e44f figures, after the #47 fix, as "after" (`tools/formant-check/weak_spots.py` over renders from `tools/aqua-listen/kits/tract-weak-spots/render.sh`; the renders reproduce the table above: /ɪ/ 375/2024 Hz, /ʊ/ 486/894 Hz, murmurs -11.81 and -9.64 dB, /i/ in /nibu/ +0.21 dB). Unheard. +- Stress level (100-170 ms of each vowel, stressed against unstressed, f0 rise split from the rest by a render of the unstressed vowel at 148.4 Hz): total i +1.22, ɑ +1.67, u +2.07, e +2.99, o +1.69 dB (mean 1.93, sd 0.60); + the f0 rise alone -0.80, +0.03, +0.17, +1.23, -0.07 dB; the rest (the amplitude factor 1.30 = 2.28 dB nominal, with the longer vowel) +2.02, +1.64, +1.90, +1.76, +1.76 dB (sd 0.14). The scatter between vowels is the f0 rise; the amplitude cue itself is even. + No level the f0 rise cannot move was found cheap, so the five-vowel-mean basis stands (`aqua-body:question:stress-amplitude-calibration-basis`); the rest sits 0.46 dB under the nominal 2.28 dB, cause not isolated. +- Plosive release: a defect was found and fixed. `release_flow` was held at the burst gain from the plosive's gesture on (a surface holds its first value before its first point and its last after its last), so every later contact state made noise: + the difference of the renders with and without `release_flow` peaked at 0.0302 in the final /n/ and tail of /bɪn/ (above its 0.0226 at the release, 81 ms), 0.0126 in /bʊn/; in /nibu/ (flat) the leading /n/ carried it backward: peak 0.0688, rms 0.0067 against the /n/'s own 0.0058. + After the fix (`BurstGainAroundGesture`: zero before and after the plosive, the gain through it; figures from here to the end of the bullet are post-fix, main af0e44f) the same difference is 0.0 from 120 ms after the release and the burst itself is unchanged (0.0226 at 81 ms in /bɪn/). Burst alone, after the fix: /bɪn/ 0.0226 (-15.1 dB re its vowel peak 0.129), /ba/ 0.0142 (-24.0 dB re 0.225), /bʊn/ 0.0130 (-24.3 dB re 0.213), + each falling to a tenth in 15-17 ms; the pre-fix "0.006-0.008, -24 dB" figure above does not hold for /bɪn/. The release peak of the full render (voicing breaking through) is -5.4, -6.3, -5.9 dB re the vowel peak. Test `PlosiveBurstExistsOnlyAroundItsOwnRelease`. +- Nasals: /m/ -11.81 dB, /n/ -9.64 dB re the flanking vowels, Burg N1 295 and 332 Hz. The 2 dB is not harmonic sampling: /n/ minus /m/ is 2.01, 2.17, 2.05, 1.61 dB at f0 110, 140, 170, 200 Hz. Band levels of the murmur (0-400, 400-800, 800-1600, 1600-3200 Hz): /m/ 36.3, 23.6, 21.3, 2.7; /n/ 38.4, 28.2, 16.8, 0.9 dB. + The cause (the shorter oral side branch, 7.1 cm against 9.6 cm from the velum junction) is a candidate, not shown. Open. +- Lax vowels: /ɪ/ and /ʊ/ are in the DSL table (`StoryVowelAreas`) and render in `WordRender` (/bɪn/, /bʊn/, isolated). Burg F1/F2: /ɪ/ 375/2024 against the tube chain 346/1987 (+8.5/+1.8 %), Hillenbrand 429/2034 (-12.5/-0.5 %), Peterson & Barney 392/1993 (-4.3/+1.5 %); + /ʊ/ 486/894 against 478/850 (+1.7/+5.2 %), 469/1123 (+3.7/-20.4 %), 437/1023 (+11.3/-12.6 %). No defect found. +- Burst place, 2026-11-12 (`burst_spectrum` in `tools/formant-check/weak_spots.py`: render minus `--no-burst` render, 20 ms Hann window from the release at 0.65 of the consonant, spectrum smoothed over 400 Hz, peak searched from 1 kHz up, a DESIGN floor; /aCa/, 140 Hz, 48 kHz, explicit plosive descriptors). Before: the burst joined the path input at the glottal end, so place reached it only through the constriction; + peak/centroid /t/ 4172/8729 Hz, /k/ 6984/10218, /p/ 7087/10423 (burst 0.019): /t/ below /k/, the order Chodroff and Wilson 2014 have reversed (t 5216, k 2868, p 2236 Hz). The decision gate said inject. Now a contact whose position has a control surface (made by the first non-labial plosive of a word) injects its burst there: `LadderInjection`, spread over the two cells whose centres bracket the place, + scaled by cell area over first-cell area (the ladder carries flow waves). /t/ 4008/6681 Hz, /d/ 4008/6693, /k/ 3747/9312 (peak 3747 without the floor too); /t/ is above /k/ by 261 Hz, short of the 2348 Hz of the corpus. Burst window peak /t/ 0.160, /k/ 0.148, /p/ 0.019; closure residue under 0.004. Unscaled, the injection made the closure 30 times louder (outside peak 0.058). + /p/ and /b/ keep the glottal end and their Faust source byte for byte (`PlaceTableKeepsLabialAndAlveolarPositions`); their burst peak above 1 kHz is 7087 Hz, above /t/, and below 1 kHz it is a 434 Hz low resonance, not the place cue: the labial site is not moved. Below 1 kHz /t/ and /d/ peak at 480 Hz, the same low resonance. Unheard. +- /t/ closure leak, 2026-11-12 (explicit descriptor, not in the table; /ata/ against /apa/ without burst, closure = 0.2 to 0.65 of the consonant, dB re the mean power of the flanking vowels): the alveolar closure was held at the nasal's 0.03 cm aperture and opened at 0.5, and read -14.3 dB against /p/ -22.1 (the lips seal /p/ whatever the tongue does); the same window with the aperture 0 for the whole gesture, -24.3. A non-labial plosive now holds aperture 0 to 0.65: -24.9 dB (test run) against /p/ -20.4. +- /k/, 2026-11-12 (the same /aka/ frame, closure 0.2 to 0.65 of the consonant, dB re the flanking vowels, burst removed): closure -14.9 dB against /p/ -20.4 and /t/ -24.9; the same closure with the glottal source's flow scale held at 0, -22.7 dB, so the tract seals and the leak is the voicing (150 Hz) the voiceless source still carries, reaching the output through a closure that leaves a short rear cavity. At the /t/ place (cell 36.7) the same /k/ gesture reads -24.8 dB, so it is the place and not the gesture. Silencing voicing in voiceless stops would change /p/ and /t/ (pinned); reported as an unmet criterion, not worked around. Velar place 9.81 cm behind the lips (0.44 of the tract; between /u/ narrowest dorsal section 26, 6.97 cm, and /a/ pharyngeal section 13, 12.14 cm). Burst: peak 0.0506 against the following vowel's 0.1129 (0.45), spectral peak 3100 Hz against Chodroff and Wilson 2014 /k/ 2868 Hz (/t/ 3950 Hz by the same measure); centroid 4626 Hz against /t/ 3629 Hz by the same measure (not judged: no literature centroid retrieved). + Its injected burst peaked at 0.76 of the following vowel's peak at the shared gain 0.06 (/b/ 0.19): `TongueStopBurstGain` 0.03 gives 0.39 (0.0438 against 0.1129). Burst spectrum unchanged by the gain: peak 4011 Hz, centroid 5796 Hz, inside 3.5-7 kHz. Not met: /t/ above /p/ in burst peak (/p/ 7087 Hz above 1 kHz, 434 Hz below): the labial burst is still injected at the glottal end, which this branch pins. /k/ closure at the old 0.44 fraction is not sealed (core -15.7 dB). Unheard. +- /d/, 2026-11-13 (table entry `voiced_alveolar_plosive`, no new mechanism: the `voiced` token selects the voice bar as it does for /b/): its consonant splines are identical to /t/'s (7 of 7: closure, contact, burst site and gain, source level aside), so closure position, duration and burst are /t/'s. Voice bar (word-initial, closure 0.03-0.058 s against the following vowel, deviation): /d/ -28.9 dB, /b/ -29.2, /t/ -38.7, /p/ -40.1; /d/ over /t/ 9.8 dB. Burst spectral peak 3950 Hz for both /d/ and /t/ (Chodroff and Wilson 2014: /d/ 4878, /t/ 5216, so the same place gives the same peak here, 0 Hz apart against their 338). Burst 0.0603 peak at its own release, 0.00002 before and 0.00000 after. The alveolar closure's voice bar does not differ from the labial one at this resolution (0.3 dB), so no number needed tuning. Unheard. +- /l/, 2026-11-13 (gate then build; /ɑlɑ/ at 140 Hz, scaffold durations; no source for English /l/ formants or level is in the mind (`huginn known`), so every target is a DESIGN value from the cut spec: F1 250-400 Hz, F2 1000-1300 Hz, a dip at 2-3 kHz, level -4 to -10 dB re the flanking vowels, no burst). Gate: the ladder holds a lateral's antiresonance but not its parallel channel. A branch port joins the oral path at any cell (here the ridge, 2.555 cm from the lips) and a closed stub path (4 cells, cross-section 1 cm^2, end reflection +1, aperture 0) beside a sustained /ɑ/ notches the harmonics: stub 2.5 cm -12.3 dB at 2940 Hz, 3.0 cm (the built length) -13.4 dB at 2800 Hz in /ɑlɑ/, 3.6 cm -6.9 dB at 2940 Hz, 4.5 cm -7.9 dB at 2380 Hz, all against the same render with the port shut, which is the render without the stub to 0.0 dB. What the ladder cannot hold is a second oral channel that rejoins the oral path past the contact (a branch has one junction and ends in its own radiation), so the midline is narrowed rather than closed with the flow going round it. Built: the `lateral` token opens the `side_stub` branch for the gesture and narrows the midline to 0.06 cm (held to 0.8), the stub declared in the scaffold only for a word with a lateral (/apa aba ana ata/ keep their script and Faust source byte for byte). + Measured (Burg order 12, 20 ms frames, 0.205-0.255 s; level by RMS of the middle 60 percent re the flanking vowels): level -5.0 dB (band met), F1 263 Hz (met), F2 725 Hz (NOT met: 1000-1300; the narrowed tongue-tip constriction on the /ɑ/ body has no dorsal control to raise it), dip 13.4 dB under the stub-shut render at 2800 Hz (met), burst none (the render with and without release_flow is the same render). The midline diameter is a steep and non-monotonic lever on level and on formants together, so level and F1/F2 could not be set independently: midline 0.028 cm reads -7.2 dB but the spectrum is the sealed-tract leak (energy under 1 kHz, no F2); 0.04 -2.6; 0.05 -5.8 (F1 202); 0.06 -5.0 (F1 263, F2 725); 0.065 -2.5; 0.07 +0.4; 0.08 -0.5 (F1 287, F2 713); 0.15 -0.9 (F1 463, F2 737). Unheard. +- Phones the tract cannot make: `WordRender` refuses a phone outside the table with exit 4 (`UNSUPPORTED PHONES (the tract has no gesture for them; nothing rendered): `); the table holds p b t d k m n l s z ʃ ʒ ʍ w and the Story vowels. A direct `phoneme_gesture` on an unknown IPA symbol still falls back to an alveolar approximant (`GestureDescriptor`); not changed here. + +Burst-window mutants (hand-applied in a scratch worktree on af0e44f plus the cut, never committed, 2026-11-12; `BurstGainAroundGesture`). The rendered test `PlosiveBurstExistsOnlyAroundItsOwnRelease` did not kill any of them, so `PlosiveBurstGainWindowIsOneRampEitherSide` reads the window off the spline (opens one ramp, 0.15 of the gesture, before the plosive; closed one ramp after it): + +| Mutant | Rendered test | Window test | +|---|---|---| +| ramp 0.15 x duration to 0 | survived | killed | +| ramp 0.15 x duration to 1.0 x duration | survived | killed | +| last point at start+duration+ramp moved to start+duration+3*ramp | survived | killed | + +Mutations (hand-applied, restored, each run against the named tests): pitch ratio ignored, burst removed, burst not shaped by opening, burst gain x20 (0.06 to 1.2), stress duration ignored, +stress pitch ignored, stress amplitude ignored, secondary equal to primary, probe timeline sample rate re-hardcoded to 48000: nine of nine killed. Script: `work/mut-b3.sh` in the aqua workspace. diff --git a/docs/research/data/story2008-area-functions.csv b/docs/research/data/story2008-area-functions.csv new file mode 100644 index 0000000..0a6e572 --- /dev/null +++ b/docs/research/data/story2008-area-functions.csv @@ -0,0 +1,49 @@ +# Story 2008, JASA 123(1):327-335, doi 10.1121/1.2805683, PMC2377017, Table I. Areas in cm^2, section 1 = glottis, section 44 = lips. +# Transcribed by an Eyes pass from the PMC page through a page summariser (not pdftotext). tools/formant-check/tube_formants.py checks every column for plausible formants. +section,i,ɪ,e,ɛ,æ,ʌ,ɑ,ɔ,o,ʊ,u +1,0.51,0.28,0.29,0.37,0.31,0.23,0.56,0.27,0.38,0.37,0.54 +2,0.59,0.21,0.26,0.30,0.21,0.34,0.62,0.43,0.45,0.38,0.61 +3,0.62,0.21,0.30,0.24,0.18,0.47,0.66,0.54,0.57,0.49,0.66 +4,0.72,0.30,0.40,0.23,0.23,0.60,0.78,0.67,0.77,0.62,0.75 +5,1.24,0.47,0.55,0.29,0.33,0.77,0.97,0.83,1.31,0.85,1.13 +6,2.30,0.71,0.74,0.41,0.50,1.06,1.16,0.92,1.92,1.28,1.99 +7,3.30,1.12,0.99,0.58,0.78,1.26,1.12,0.89,1.74,1.62,2.83 +8,3.59,1.48,1.09,0.82,0.96,1.09,0.82,0.73,1.11,1.47,2.90 +9,3.22,1.35,0.90,0.97,0.85,0.80,0.55,0.55,0.75,1.04,2.52 +10,2.86,1.05,0.69,0.82,0.63,0.65,0.45,0.44,0.59,0.81,2.40 +11,3.00,0.92,0.77,0.62,0.46,0.56,0.37,0.37,0.57,1.03,2.83 +12,3.61,0.92,1.31,0.54,0.36,0.47,0.29,0.28,0.68,1.44,3.56 +13,4.39,1.19,2.13,0.54,0.33,0.37,0.21,0.18,0.73,1.49,3.99 +14,4.95,1.94,2.74,0.68,0.46,0.24,0.15,0.14,0.67,1.28,3.89 +15,5.17,2.83,3.03,1.09,0.73,0.17,0.16,0.15,0.58,1.06,3.50 +16,5.16,3.31,3.23,1.62,1.00,0.18,0.25,0.14,0.49,0.85,3.04 +17,5.18,3.48,3.33,2.03,1.30,0.23,0.34,0.13,0.44,0.69,2.64 +18,5.26,3.60,3.27,2.35,1.66,0.27,0.43,0.14,0.42,0.56,2.44 +19,5.20,3.64,3.09,2.51,1.97,0.28,0.54,0.18,0.49,0.42,2.31 +20,5.02,3.49,2.84,2.39,2.06,0.29,0.61,0.20,0.53,0.27,2.07 +21,4.71,3.20,2.66,2.22,2.03,0.33,0.67,0.23,0.38,0.27,1.80 +22,4.13,2.90,2.46,2.13,2.01,0.52,0.98,0.49,0.30,0.41,1.52 +23,3.43,2.59,2.14,2.00,1.89,0.97,1.76,1.03,0.45,0.51,1.14 +24,2.83,2.21,1.79,1.78,1.66,1.50,2.75,1.58,0.61,0.49,0.74 +25,2.32,1.87,1.44,1.58,1.49,1.91,3.52,2.06,0.71,0.47,0.42 +26,1.83,1.54,1.17,1.43,1.42,2.23,4.08,2.61,0.79,0.50,0.22 +27,1.46,1.20,1.00,1.31,1.37,2.65,4.74,3.35,0.86,0.58,0.14 +28,1.23,0.92,0.88,1.23,1.34,3.29,5.61,4.34,1.01,0.80,0.20 +29,1.08,0.74,0.80,1.24,1.41,4.13,6.60,5.51,1.41,1.19,0.47 +30,0.94,0.59,0.81,1.38,1.58,5.00,7.61,6.70,2.09,1.62,0.89 +31,0.80,0.52,0.85,1.61,1.82,5.77,8.48,7.75,3.00,2.27,1.15 +32,0.67,0.54,0.84,1.82,2.19,6.33,9.06,8.63,4.10,3.24,1.42 +33,0.55,0.59,0.86,1.96,2.63,6.61,9.29,9.29,5.16,4.16,2.17 +34,0.46,0.65,0.96,2.01,2.97,6.63,9.26,9.59,6.22,5.00,3.04 +35,0.40,0.71,1.18,2.00,3.17,6.45,9.06,9.42,7.34,5.70,3.69 +36,0.36,0.67,1.35,1.95,3.40,6.04,8.64,8.78,8.15,6.11,4.70 +37,0.35,0.61,1.48,1.77,3.56,5.39,7.91,7.82,8.61,6.21,5.74 +38,0.35,0.57,1.62,1.48,3.57,4.42,6.98,6.50,8.37,6.29,5.41 +39,0.38,0.50,1.49,1.30,3.58,3.29,6.02,4.95,6.76,6.24,3.82 +40,0.51,0.48,1.29,1.21,3.44,2.37,5.13,3.47,4.37,4.91,2.34 +41,0.74,0.54,1.24,1.10,3.15,1.74,4.55,2.15,2.30,2.61,1.35 +42,0.92,0.73,1.17,0.99,3.38,1.36,4.52,1.38,1.06,1.09,0.65 +43,0.96,0.93,1.04,0.91,3.99,1.17,4.71,1.11,0.58,0.63,0.29 +44,0.91,0.82,0.95,0.78,4.17,0.99,4.72,0.90,0.47,0.59,0.16 +section_length_cm,0.384,0.376,0.386,0.393,0.366,0.390,0.388,0.395,0.417,0.440,0.445 +vtl_cm,16.90,16.55,16.98,17.30,16.11,17.14,17.09,17.40,18.33,19.34,19.59 diff --git a/docs/soundscape-grammar.md b/docs/soundscape-grammar.md new file mode 100644 index 0000000..b9e62f5 --- /dev/null +++ b/docs/soundscape-grammar.md @@ -0,0 +1,334 @@ +# Soundscape grammar for the Ratatoskr generative symphony (spec) + +Serves follow-up `aqua-body:follow_up:chat-s938-943`. **Grammar only.** No notes, +no motifs, no pitches, no instrument choices. Nothing here was rendered, run, +listened to, or measured; every number marked *proposed* is a design choice +awaiting a listening trial and a device measurement. Nothing in this document +may be cited as heard. + +## 0. Inputs not seen + +Two inputs named in the brief could not be read when this was written, and the +grammar is drafted without them. Each is a place to reconcile before the doc is +treated as in force. + +- `heimdall-body:follow_up:ratatoskr-creature-projection-v2`: the follow-up + document still answers `absent` from aqua (re-checked 2026-11-14). Its content + reached aqua as a spec relayed from #minds on 2026-11-14 (follow-up + `aqua-body:follow_up:chat-s8119-8119`): per participant `{id, estate, + sizeBucket, neutral, activity}`, spec only, Bifrost not cut. Section 2 is + reconciled against that relay, not against the Heimdall document; where the + two could differ, the Heimdall document wins and this doc follows. +- The Mimir harvest (`ratatoskr:follow_up:symphony-grammar-harvest-mimir`) is now + readable: [`docs/gamecult/symphony-grammar-harvest.md` in GameCult/Ratatoskr at + `39c99240`](https://eureka.gamecult.org/forge/GameCult/Ratatoskr/src/commit/39c99240420736a6d064a92dc2cc5b2624efa529/docs/gamecult/symphony-grammar-harvest.md) + (Void's read of GameCult/Mimir at `c6f4541`, as corrected by Ratatoskr #160; + that commit is on Ratatoskr `main`, and the + harvest was merged without a Soul pass). Section 4.1 + reconciles against both. The first draft of section 4 predates them. + +Context actually read: `docs/gamecult/ratatoskr-target.md` in `GameCult/Ratatoskr` +at `b33d8de` (the agreement that one activity vocabulary drives both sound and +motion, the member-safe projection, "kind, Mind, time and intensity, never +briefs or mind contents"), and `docs/agent-activity-grammar.md` (an earlier, +unmerged AquaSynth draft for the Dings event source; not part of this change). + +## 1. Purpose and owner + +One typed **activity frame** per Mind, produced once, read by two consumers: + +- the **orbit renderer** (Persona motion around Yggdrasil), and +- the **voice scheduler** (which instrument voice speaks, how densely). + +Neither consumer owns the vocabulary; neither adds a field. The vocabulary owner +is this grammar. The frame's producer is the Heimdall session (section 2). A +consumer that needs a distinction the vocabulary lacks raises it as a change to +this document, not as a private side channel. + +## 2. The activity frame + +Typed CultNet/CultCache document, projected member-safe. No private pipe: the +frame reaches both consumers by the same subscription, and a consumer that +cannot read it plays nothing and draws nothing. + +| field | type | values | note | +|---|---|---|---| +| `id` | id | participant identity | stable identity, not a run/session label; for a human, the member's stable id (prose below says "mind" for the participant an `id` names) | +| `estate` | enum, 3 | `human`, `mind`, `persona` | selects the vocabulary this participant may use; see below | +| `sizeBucket` | enum, 3 | `Small`, `Medium`, `Large` | coarse size bucket for the creature; scale per bucket is not fixed here; absent when `neutral` (O1) | +| `neutral` | bool | | separate flag that overrides any `sizeBucket`: no size drawn or carried; when set, `activity` and `sizeBucket` are omitted, see O1 | +| `activity` | struct | `kind` (enum, 7; humans: 3) and `energy` (enum, 4; humans: 3) | produced by the Heimdall session; absent when `neutral`; see below | + +`kind` is one of `Idle`, `Reading`, `Thinking`, `Working`, `Speaking`, `Waiting`, +`Alert`; `energy` is a coarse bucket, `Low`, `Mid`, `High`, `Peak`, never a float. + +These five fields are the whole participant projection. The previous draft also +carried `seq`, `at` and `override`; they are gone from the frame. Ordering and +observation age (rules S1, S2) are **needed from the feed envelope**, which +`ratatoskr:question:activity-feed-source` owns, and are an unconfirmed +assumption: if the feed cannot supply them, S1 and S2 cannot be implemented and +that is raised there, not patched by a sixth field. `Muted` is no longer frame +data (O2). + +Why this small: 7 x 4 = 28 states (the Mind/Persona set) fit in one byte on the +wire and in a lookup table on a phone; both consumers map state to behaviour by +table, no arithmetic on unbounded values. `kind` and `energy` are orthogonal on purpose, because +orbit radius/speed wants energy and voice choice wants kind. + +### Estate + +`estate` is set by the producer, never inferred by a consumer, and bounds what +the other fields may carry (per +`heimdall-body:follow_up:ratatoskr-creature-projection-v2`): + +- **`human`: coarse only.** `energy` is capped at 3 buckets (`Low`, `Mid`, + `High`; no `Peak`) and `kind` is the **human subset** `Idle`, `Working`, + `Speaking` (*proposed*). The subset is strictly smaller than the Mind/Persona + set and chosen to reveal less, never more: it omits `Reading`, `Thinking`, + `Waiting` and `Alert`, which describe what a participant is attending to or + how it is faring. A producer must not emit a `kind` or `energy` outside the + human set for a human. A consumer that receives one clamps it: an omitted + `kind` maps to `Idle`, `Peak` maps to `High`. Clamping only ever reveals + less. A human's `kind` that is present but not an enum value at all clamps + to `Idle` (reveal-less) for a non-neutral human; for `mind` and `persona` an + unknown `kind` is malformed and the frame is dropped (A4). + The relayed projection says humans get the coarse bucket by default, with a + typed opt-out setting `neutral`, and that Minds and Personas may show finer + buckets. It does not name the three-`kind` subset: that subset is this + grammar's proposal for "coarse", and stays *proposed* until Heimdall's + document names it or replaces it. +- **`mind` and `persona`: the finer 7 x 4 set** (all seven `kind`, all four + `energy` buckets). + +`sizeBucket` is three coarse buckets (`Small`, `Medium`, `Large`) and the +projection carries it for every estate except a `neutral` participant (O1). +Heimdall's projection lets a Mind or Persona show finer buckets than a human; +this grammar does not consume `sizeBucket` for sound at all, so the count is not +decided here and a finer set changes nothing in sections 3 to 5. The scale each +bucket draws at is **not fixed by this grammar**: the values 0.7 / 1 / 1.4 are +Ratatoskr placeholders, owned by the orbit renderer, and may change without +touching the frame. Exact vote weights, from which a bucket is derived, never +leave Bifrost and are never in the frame. + +Rules: + +- **A1. Source.** The frame comes from the Heimdall session and only from it. + Neither consumer infers activity from Matrix presence, typing, message + traffic, audio, or the room member list. Activity is a stub fed by the Heimdall + session; being in a room says nothing about being active. (The target already notes presence/typing are too coarse + for Minds.) *The kind list for Minds and Personas stays as relayed; the human subset is proposed (section 2).* +- **A2. Coarse by construction.** There is no continuous field in the frame. A + renderer that wants smooth motion interpolates between buckets locally; it + does not request finer data. +- **A3. Member-safe.** The frame carries `id`, `estate`, `sizeBucket`, `neutral` + and `activity`, and nothing else; when `neutral` is set, `activity` and + `sizeBucket` are stripped (O1). Exact vote weights never leave Bifrost. Never brief text, + document ids, claims, or mind contents. +- **A4. Missing is Idle.** No frame within the staleness window (section 5, S2) + means `Idle`/`Low`: no voice, and the orbit at rest. Silence is the default + state, not a failure state. Missing is not `neutral`: an absent frame is an + idle participant, a `neutral` participant is not drawn at all (O1). + +### Overrides + +- **O1. `neutral`** (the flag, set by the session or by a human's own typed + opt-out setting): a Neutral participant gets **no orbit and no voice**, and the + frame **strips `activity` and `sizeBucket` as well**: the producer omits both, and a consumer ignores them if present. An opted-out participant + reveals nothing, not even coarse activity or a coarse size bucket. It draws nothing and sounds nothing; there is no + "neutral pose" to render. Neutral overrides every `sizeBucket` bucket: none is drawn or carried, and no sound carries a size cue either. Neutral is + not an error and not Idle: the frame says only that this participant is not + shown. +- **O2. Muting one Mind** is a listener's client preference, not frame data: + no voice, orbit unchanged. It lets a listener silence one Mind without hiding + its motion, and it never appears in, or rewrites, a frame. +- **O3.** `neutral` is data in the frame, set upstream (by the session or by a + human's own typed setting). A consumer never applies a local override of it and + then reports that as the participant's state. Neutral means no size cue and + no activity cue, in sound as in the orbit: the voice scheduler gives a `neutral` + participant nothing to sound. + +## 3. Mapping tables (shape only) + +The grammar defines the mapping *axes*, not the values. Each consumer owns one +table keyed by the 28 Mind/Persona states (humans use the 3 x 3 subset); the tables are checked in beside this document +when values exist. + +| axis | owner | driven by | notes | +|---|---|---|---| +| orbit **existence** (drawn or not) | orbit renderer | `neutral` | `neutral` set: no orbit | +| orbit radius / speed / phase coupling | orbit renderer | `energy`, `kind` | Nibu's side; not specified here | +| creature scale | orbit renderer | `sizeBucket` | per-bucket scales are renderer placeholders, not fixed here | +| voice **presence** (sounding or silent) | voice scheduler | `neutral`, `kind` | `neutral` set: no voice, regardless of `kind`; `Idle`, `Waiting` default silent | +| voice **density** (events per window) | voice scheduler | `energy` | capped, section 5 | +| voice **role** (lead / support / texture) | voice scheduler | `kind` | role, not instrument | +| voice **identity** (which instrument) | voice scheduler | `id` | never `kind`; section 4 | + +The one rule that ties the two consumers: **`id` selects identity, `kind` +selects behaviour, `energy` selects amount.** A given Mind sounds like itself +in every state; what it does changes. + +## 4. Keeping voices distinct when mixed + +Principle, from the acoustic-niche and auditory-scene-analysis literature +(recalled; the Mimir harvest gives it no support or contradiction, see 4.1): voices that share a niche in +frequency, time and space mask each other; voices that differ on several +independent axes stay separable even at high density. Assign each Mind a +distinct **niche** on independent axes, and make the scheduler protect those +niches. Axes: + +1. **Register band.** Each Mind owns a band; bands do not overlap among + concurrently audible voices. Most robust single cue. +2. **Timbre family.** Distinct spectral envelope and onset shape. Identity is + carried by instrument (`ratatoskr-target.md`: "the instrument says who + spoke"); this grammar does not choose instruments. +3. **Rhythmic slot.** Each Mind owns a phase offset within a shared pulse grid, + so onsets interleave instead of colliding. Collisions are the main cause of + masking at low density. +4. **Stereo position.** Fixed pan per Mind, stable across sessions. Free on + phones with headphones, nearly useless on speakers; never the only cue. +5. **Role contrast.** At most one `lead` voice sounding at a time; the rest + support or texture (quieter, sparser, narrower band). + +Rules: + +- **V1. Identity is stable.** The niche (axes 1-4) is a function of `id` + only. It never moves with `kind`, `energy`, or which other Minds are present. + A listener learns a Mind once. +- **V2. Niche table is data**, held beside the Mind's other typed state + (the target already says the instrument per Mind belongs in a typed document + on the Mind side, not hardcoded). Not defined here. +- **V3. Voice cap.** At most **4** voices audible at once (*proposed*; the + human limit on following concurrent streams is small and phones are + small). Over the cap, the lowest-`energy` voices drop first, never the + `lead`. The cap is a requirement to measure, not a finding. +- **V4. Collision backoff.** If two voices would start within one pulse of each + other in the same register band, the lower-priority one slips to its next + slot. It does not stack. +- **V5. More Minds than niches.** When Minds exceed available niches, the + excess are *not* squeezed in by shrinking bands. They share a `texture` + voice or stay silent. Distinctness beats completeness. +- **V6. Identification is unproven.** Nominal separation (different bands, + different families) is not human discriminability. No Mind's niche is + accepted until a listener-identification trial passes (the existing + notification ledger requires the same). That trial has not happened. + +### 4.1 Reconciled against the Mimir harvest + +Read: the harvest at `39c99240` (linked in section 0; not copied here), in the +wording of Ratatoskr #160, Void's corrections to it. Rows are cited below by what +they say, not by number. + +What the corrections change here: + +- **The six-syllable packet song is Aqua's design choice, not Mimir's result.** + The harvest's "best contestant" claim (canary-packet-trill) is + confidence B, a hypothesis; Mimir's production word is the four-syllable, + 118 ms word. If this grammar adopts a six-syllable packet song, it does so by + its own choice at confidence B, and it is a thing to try in the listener trial, + not a measured recommendation. Nothing in section 4 depends on it. +- **Do not rest a niche on Mimir's identity figure.** The harvest's 0.908-0.997 + is a clean-input identity range. Per #160 the degraded numbers have a receipt, + but a narrow one: identity 0.532-0.908 and timing 0.187-0.728 for the + `compact-fast-index` hypothesis only, across four degraded cases + (`artifacts/bioacoustic-training/bioacoustic-20260524-150729/compact-fast-index/*/summary.json`, + synthetic damage). #160's description (not the harvest text) adds that other + hypotheses in that run range wider (identity to 0.998, timing to 0.807). The earlier relayed + "0.5-0.9" is replaced by this scoped range. These are one decoder's scores on synthetic damage, not a + property of any song, and no rule in section 4 or 5 depends on them. +- **The "event slot repeats about every 41 s" figure is derived, not measured.** + #160 says no receipt or doc states it: it is 128 words x 2 speakers = 256 + events at 0.16 s spacing = 40.96 s, read from Mimir's code. No rule here + depends on it. +- **The "too-short songs collapse" thresholds are not a design floor for this + grammar.** The harvest's own row reports them from one blur case under two + decoders (blur-light 4.358 with the razor decoder, 218.368 with the sprint + decoder), so they are decoder-dependent evidence on synthetic damage, not a + threshold for human listening. +- **A word identifies an event slot, not a source** (the harvest's event-slot row). Mimir's evidence + does not support "the instrument says who spoke" (V1, axis 2). That stays a + design rule of this grammar, to be tested by V6, with no Mimir backing. +- **No harmony evidence exists.** Mimir emits one low-gain song at a time and + tests no simultaneous voices, intervals, consonance or masking between + concurrent songs. V3 (voice cap), V4 (collision backoff) and V5 are Aqua's + design and rest on the recalled auditory-scene-analysis literature alone. +- **No pleasantness evidence exists.** Mimir's score is decodability by a machine + decoder against a known schedule; its gain was chosen for low audibility. Whether + any song, harmony or niche table is pleasant or distinguishable to a person is + unmeasured. **A listener trial is owed** (V6, section 7) and has not happened; + nothing here is heard. + +What the harvest does support, as design constraints already in this section's +spirit and still unmeasured for people: identity carried redundantly across +several channels at once, no sequence grammar for identity, irregular rather +than ladder-like rhythm, a deterministic generator authored once and rendered +(S4 already says the client ships rendered assets). Its open question "which +dimension bends with activity" is what section 3's rule answers by design: +`id` selects identity, `kind` behaviour, `energy` amount, so a Mind keeps one +timbre and root for life and the contour and density move; Mimir has no activity +channel to confirm that. + +The **human `kind` subset** (`Idle`, `Working`, `Speaking`, section 2) is not +touched by the harvest: Mimir carries no activity or privacy channel at all, so +the subset still rests only on the projection-v2 follow-up, which remains +unreadable from aqua (below). + +## 5. Timing, staleness and the callback deadline + +Hard rule: **the audio callback deadline wins over everything in this +document.** The callback never waits on the grammar, the network, a decode, an +allocation, or a lock. + +- **C1. Frame handoff is a snapshot.** The network/projection thread writes the + latest frame per `id` into a preallocated table; the scheduler thread reads + it and writes a preallocated voice-command queue; the audio callback only + reads that queue. No thread but the callback touches the output buffer; the + callback never blocks on, or is blocked by, the others (single-producer + single-consumer, fixed capacity). +- **C2. Drop, never delay.** If the queue is full or the scheduler is late, the + frame or command is dropped. A late sound is not played to catch up and its + internal timing is never stretched. +- **S1. Latest wins.** Per `id`, only the newest frame by the feed envelope's ordering matters (the frame itself carries none; see section 2). Intermediate + frames missed during a stall are not replayed. +- **S2. Staleness.** A frame whose envelope age exceeds **10 s** (*proposed*) is treated as + absent (rule A4). Sounding on stale activity is misleading. +- **S3. Rate.** Voice density per `energy` is bounded by a ceiling per Mind per + window (*proposed*, set with the device measurement); rule V3 bounds the + total. Bursts are coalesced, not queued. +- **S4. Pre-rendered material.** The client ships rendered audio assets, no + synth in the browser (`ratatoskr-target.md` agreements). The scheduler + selects and positions assets; it generates nothing at callback time. Browsers + block audio until a first user gesture: before it, the scheduler runs but the + output is silent, and no state is lost. +- **S5. Backgrounded or hidden page.** Voice scheduler pauses with the orbit + renderer's pause (the target already pauses the background when hidden). On + resume, S1/S2 apply; there is no catch-up burst. +- **S6. Failure is silent.** No frame source, a dead scheduler, a refused + autoplay, or a full queue all produce silence, with the orbit at rest. A + failure is not a `neutral` participant and does not remove the creature. Nothing + retries, alerts, or alters the frame source. + +## 6. What this grammar does not decide + +- Motifs, notes, pitches, instruments, envelopes. (Next layer; needs a + listening trial per the repo's audio doctrine.) +- Orbit geometry and shader work (Nibu's side). +- The feed transport. `ratatoskr:question:activity-feed-source` owns where the + frame comes from; this document fixes only its *shape*, and that it is a + typed CultNet document, not a private channel. +- Per-device budgets. All numbers marked *proposed* need a phone measurement. + +## 7. Verification owed + +None run. To be done before the grammar leaves draft: + +1. Section 2 is reconciled with the relayed projection-v2 spec (2026-11-14, five fields), not with the Heimdall document itself, which is still `absent` from aqua. Still owed: read that document when it is readable; confirm the feed envelope can supply per-participant ordering and age (S1, S2); get the human `kind` subset named or replaced by Heimdall (it is *proposed* here). +2. ~~Reconcile section 4 with the Mimir harvest.~~ Done in 4.1 (docs only; no listening). What the harvest could not give (harmony, pleasantness, discriminability by a person) goes to item 5. +3. Round-trip the frame as a CultCache document and check the size claim (one + byte per state, 28 states). +4. Measure voice cap, density ceilings and scheduler cost on a real phone. +5. Listener-identification trial for any niche table (rule V6). + +## 8. Status + +Draft, reconciled with the Mimir harvest (4.1) and with the relayed projection-v2 five-field spec (section 2) on 2026-11-14; the human `kind` subset is still proposed. Not implemented. No code changed. No audio was produced and none is +claimed. diff --git a/patches/dx7/public-domain/prc-synth1-calibrated.aqua b/patches/dx7/public-domain/prc-synth1-calibrated.aqua index 78bbe7d..05b5ce2 100644 --- a/patches/dx7/public-domain/prc-synth1-calibrated.aqua +++ b/patches/dx7/public-domain/prc-synth1-calibrated.aqua @@ -2,9 +2,11 @@ # Family: dx7/public-domain # Source: analog1.syx voice 17 parity probe, generated from calibrated DX7 lowering # Notes: Hard DX7 parity target that forced applied EG, summed route, feedback, detune, and COM calibration. +# Headroom: patch gain is 0.45, not the 1.4 the parity probe in Dx7ReferenceParityTests uses. At 1.4 this graph peaked at 2.10 +# (18,633 samples past full scale in a 3 s render, found by tools/aqua-feel); 0.45 keeps the same signal at 0.675, about -3.4 dBFS. patch - gain=1.4 + gain=0.45 soft_clip=false opgraph diff --git a/patches/sfxr/classic/classic-sfxr-abstract-golf.aqua b/patches/sfxr/classic/classic-sfxr-abstract-golf.aqua index c340ec7..39c7e15 100644 --- a/patches/sfxr/classic/classic-sfxr-abstract-golf.aqua +++ b/patches/sfxr/classic/classic-sfxr-abstract-golf.aqua @@ -31,7 +31,7 @@ voice phaser=0.0001458 phaser_ramp=-0.000504 -patch repeat=0.174744 +patch gain=0.5 voice use=Noise freq=20 @@ -48,7 +48,6 @@ voice tremolo=0.0264 tremolo_hz=13.04 -patch repeat=0.11315 voice wave=sine freq=57.5946 diff --git a/patches/sfxr/explosion.aqua b/patches/sfxr/explosion.aqua index 97dacf6..fcb60c3 100644 --- a/patches/sfxr/explosion.aqua +++ b/patches/sfxr/explosion.aqua @@ -3,7 +3,6 @@ # Source: BuiltInScripts.ReferenceScripts # Notes: Stock reference patch exported from the in-code catalog. -patch repeat=0.174744 voice wave=noise freq=20 diff --git a/patches/thruster/thruster.dsp b/patches/thruster/thruster.dsp new file mode 100644 index 0000000..c36a8ae --- /dev/null +++ b/patches/thruster/thruster.dsp @@ -0,0 +1,30 @@ +// thruster: sustained, heavy, brittle-hull thruster with an RPM control. +// Compile as Faust source (AquaSynthPatchCompiler.CompileSource); it is not a .aqua script. +// Control: rpm (300..6000). Rotor tone = 28 Hz + rpm*0.025 (about 36..180 Hz); three blades chop the rasp at 3x that. +declare name "thruster"; +import("stdfaust.lib"); + +rpm = hslider("rpm", 1200, 300, 6000, 1) : si.smooth(ba.tau2pole(0.2)); +load = (rpm - 300) / 5700; +shaft = 28 + rpm * 0.025; + +// effort: slow random wander of the shaft speed +wander = no.noise : fi.lowpass(2, 5) : *(0.35); +f0 = shaft * (1 + 0.04 * wander); + +// low rumble: saw at shaft speed plus a square a sub-octave down, low-passed +// the low-pass opens up as load falls, so at low rpm the rotor tone reaches the 150-300 Hz range a small speaker can show +rumble = (os.sawtooth(f0) + 0.7 * os.square(f0 * 0.5)) : fi.lowpass(3, 60 + 2.5 * shaft + 140 * (1 - load)); + +// blade pass: rasp noise chopped at 3x shaft, band tracks rpm +chop = 0.5 + 0.5 * os.osc(f0 * 3) ^ 2; +rasp = no.noise : fi.resonbp(700 + rpm * 0.6, 1.5, 1) : *(chop); + +// brittle hull: high-Q modes rattled by noise, louder with load (a 1.5% floor at idle, so low rpm is tone-led); tanh on the rattle alone keeps ring-up spikes from setting the peak +mode(f, q) = no.noise : fi.resonbp(f, q, 1); +rattle = (mode(173, 20) * (0.3 + 0.7 * load) + 0.8 * mode(311, 18) + 0.6 * mode(467, 16) + 0.4 * mode(1130, 14)) * (0.015 + 0.985 * load ^ 2) * 3 : *(4) : ma.tanh : /(4); + +mix = rumble * (0.5 + 0.5 * load) + rasp * (0.05 + 1.75 * load) + rattle; +// drive falls only gently with load so tanh stays out of saturation while high rpm still ends up louder than low rpm +// final 0.425 trim keeps peak under 0.4 at rpm 4800 +process = mix * (0.4 + 0.6 * load) * (0.9 - 0.5 * load) : ma.tanh : *(0.425); diff --git a/spikes/faust-aot-thruster/.gitignore b/spikes/faust-aot-thruster/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/spikes/faust-aot-thruster/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/spikes/faust-aot-thruster/README.md b/spikes/faust-aot-thruster/README.md new file mode 100644 index 0000000..6cad95b --- /dev/null +++ b/spikes/faust-aot-thruster/README.md @@ -0,0 +1,50 @@ +# Faust AOT spike: thruster v5 as a native lib (Linux) + +Spike for `docs/faust-unity-player-scoping.md` path (a). One patch, `patches/thruster/thruster.dsp` +(copied here as `thruster.dsp`; re-copy if the patch changes), compiled by the `faust` CLI to C++ and +wrapped behind a C ABI. Not wired into the solution or into Unity. + +Limitation: AOT fixes the patch set at build time, so player-authored patches cannot run this way. + +- `aqs_thruster.h`: the C ABI. `rpm` enters only via `aqs_thruster_set_rpm`: bounds are read from the + compiled patch's `hslider` (300..6000, default 1200); out-of-range and +-inf clamp to the nearest bound + (`AQS_PARAM_CLAMPED`), NaN applies the default (`AQS_PARAM_NAN`); both are counted and returned. + `aqs_thruster_process` rejects NULL, negative or >`AQS_MAX_BLOCK` frames instead of truncating. +- `test_thruster.c`: plain-C behaviour test (bounds, clamp, NaN, inf, hostile values give finite bounded + output, rpm actually changes the output, instances are deterministic). A flipped clamp bound fails it. +- `bench_thruster.c`: per-block `process` time at 48 kHz / 512 frames against the 10.667 ms deadline; prints one `OVERRUN` line + per block over the deadline with wall-clock timestamp, wall ms and thread CPU ms. +- `run.sh`: generate, build, test, bench. `FAUST`, `FAUST_PREFIX` (needs `include/faust`), `FAUST_LIBS` + (needs `stdfaust.lib`), `BLOCKS`, `RATE`, `FRAMES`, `CORE` (pin the bench to one cpu via `taskset`), `NOOP=1` (time a busy-wait + instead of the patch) override defaults. Exit status is test status; timing + is reported, not gated. + +**Scope: this measures a bare C loop.** `bench_thruster` calls `aqs_thruster_process` back to back on one +Linux thread. It shows the patch is cheap, not that a Unity player meets its deadline (no Unity audio +thread, P/Invoke, IL2CPP, mixer or Windows scheduling in it). + +Measured 2026-11-06/07, Faust 2.90.20, AMD EPYC-Genoa, shared Linux host (load average ~8 on 16 cores): +median 0.052 ms (0.49% of the 10.667 ms deadline), p99 0.08-0.16 ms (~1-1.5%). Cheap on this host: +median 0.052 ms, p99 under 0.16 ms. The host's tail is not judgeable (see below). + +**Max block time: UNEXPLAINED. It is not a pass.** Max was 10.8-17.1 ms, with 1-5 of 20000 blocks over +the deadline per run (400000-block `process`: 74 over in one run, core 3; see the control below). What the instrumented runs (`run.sh`, `CORE=` pins with +`taskset`; every overrun prints a `wall_unix` timestamp, wall ms and thread CPU ms) do show: + +- Not per-block DSP cost: `NOOP=1` times a 0.05 ms busy-wait instead of `process`, and stalls about the + same way (core 3, 400000 blocks: 74 overruns with `process`, 67 with the busy-wait, both ~11 ms; counts + differ by ~10%, no run-to-run spread measured for the control). +- Not a DSP-state pattern: overrun block indices differ every run and are not tied to the rpm sweep. +- Periodic in wall time, per core: pinned to core 3, overruns recur every ~0.3-0.4 s (mostly); pinned + to core 9, every ~1-2 s (17 in 400000 blocks). Cause of the periodicity untested (not shown to be a timer). Unpinned runs get 1-5 per 20000. +- The thread's CPU time over a stalled block is about equal to its wall time (e.g. 10.84 vs 10.85 ms), + so the stall is charged to the thread rather than showing as time off-CPU. An ordinary guest + run-queue preemption would be expected to show off-CPU time; this does not. The host (vCPU steal) is + left open. The earlier claim that these were scheduler preemption is withdrawn, not refuted. + +Cause not identified (candidates: hypervisor/container CPU accounting, per-core host timer or housekeeping +work, SMI; none tested). Whether the player would see it is unknown. A quiet, realtime-priority or +isolated-core host is needed before the max can be called anything. + +**Unverified:** Windows (MSVC build, `__declspec(dllexport)`), Unity P/Invoke, IL2CPP, the Unity audio +thread, any device. Nothing here ran inside Unity. diff --git a/spikes/faust-aot-thruster/aqs_thruster.cpp b/spikes/faust-aot-thruster/aqs_thruster.cpp new file mode 100644 index 0000000..8bfe829 --- /dev/null +++ b/spikes/faust-aot-thruster/aqs_thruster.cpp @@ -0,0 +1,71 @@ +#include "aqs_thruster.h" + +#include +#include + +#include +#include +#include +#include "thruster_dsp.h" + +struct aqs_thruster { + ThrusterDsp dsp; + APIUI ui; + float lo, hi, init, applied; + unsigned clamps = 0, nans = 0; +}; + +extern "C" { + +aqs_thruster* aqs_thruster_create(int sample_rate) { + if (sample_rate < 8000 || sample_rate > 384000) return nullptr; + aqs_thruster* t = new (std::nothrow) aqs_thruster(); + if (!t) return nullptr; + t->dsp.init(sample_rate); + t->dsp.buildUserInterface(&t->ui); + t->lo = t->ui.getParamMin(0); + t->hi = t->ui.getParamMax(0); + t->init = t->ui.getParamInit(0); + t->applied = t->init; + return t; +} + +void aqs_thruster_destroy(aqs_thruster* t) { delete t; } + +float aqs_thruster_rpm_min(const aqs_thruster* t) { return t->lo; } +float aqs_thruster_rpm_max(const aqs_thruster* t) { return t->hi; } +float aqs_thruster_rpm_default(const aqs_thruster* t) { return t->init; } + +int aqs_thruster_set_rpm(aqs_thruster* t, float rpm) { + if (!t) return -1; + int status = AQS_PARAM_OK; + if (std::isnan(rpm)) { + rpm = t->init; + status = AQS_PARAM_NAN; + ++t->nans; + } else if (rpm < t->lo) { + rpm = t->lo; + status = AQS_PARAM_CLAMPED; + ++t->clamps; + } else if (rpm > t->hi) { + rpm = t->hi; + status = AQS_PARAM_CLAMPED; + ++t->clamps; + } + t->applied = rpm; + t->ui.setParamValue(0, rpm); + return status; +} + +float aqs_thruster_rpm(const aqs_thruster* t) { return t->applied; } +unsigned aqs_thruster_clamp_count(const aqs_thruster* t) { return t->clamps; } +unsigned aqs_thruster_nan_count(const aqs_thruster* t) { return t->nans; } + +int aqs_thruster_process(aqs_thruster* t, float* out, int frames) { + if (!t || !out || frames < 0 || frames > AQS_MAX_BLOCK) return -1; + float* outs[1] = {out}; + t->dsp.compute(frames, nullptr, outs); + return frames; +} + +} // extern "C" diff --git a/spikes/faust-aot-thruster/aqs_thruster.h b/spikes/faust-aot-thruster/aqs_thruster.h new file mode 100644 index 0000000..0b93baf --- /dev/null +++ b/spikes/faust-aot-thruster/aqs_thruster.h @@ -0,0 +1,52 @@ +/* C ABI over the AOT-compiled thruster (patches/thruster/thruster.dsp). + * The host never touches Faust types. rpm enters only through aqs_thruster_set_rpm, + * which enforces the bounds declared in the .dsp (hslider min/max/init). */ +#ifndef AQS_THRUSTER_H +#define AQS_THRUSTER_H +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(_WIN32) +#define AQS_API __declspec(dllexport) +#else +#define AQS_API __attribute__((visibility("default"))) +#endif + +typedef struct aqs_thruster aqs_thruster; + +/* Result of a parameter write. The applied value is always inside [min,max]. */ +enum { + AQS_PARAM_OK = 0, /* applied as given */ + AQS_PARAM_CLAMPED = 1, /* out of range (including +-inf): applied the nearest bound */ + AQS_PARAM_NAN = 2 /* NaN: applied the declared default */ +}; + +/* Largest frame count one process call accepts; larger requests are rejected, not truncated. */ +#define AQS_MAX_BLOCK 16384 + +AQS_API aqs_thruster* aqs_thruster_create(int sample_rate); /* NULL on bad rate (<8000 or >384000) or OOM */ +AQS_API void aqs_thruster_destroy(aqs_thruster*); + +/* Declared bounds, read from the compiled patch. */ +AQS_API float aqs_thruster_rpm_min(const aqs_thruster*); +AQS_API float aqs_thruster_rpm_max(const aqs_thruster*); +AQS_API float aqs_thruster_rpm_default(const aqs_thruster*); + +/* Returns AQS_PARAM_*; negative on NULL handle. */ +AQS_API int aqs_thruster_set_rpm(aqs_thruster*, float rpm); +/* Value currently applied (post-clamp). */ +AQS_API float aqs_thruster_rpm(const aqs_thruster*); + +/* Counters since create, for the host to report: out-of-range and NaN writes. */ +AQS_API unsigned aqs_thruster_clamp_count(const aqs_thruster*); +AQS_API unsigned aqs_thruster_nan_count(const aqs_thruster*); + +/* Writes `frames` mono samples. Returns frames written, or -1 on NULL handle/out, + * frames < 0, or frames > AQS_MAX_BLOCK. No allocation, no locks. */ +AQS_API int aqs_thruster_process(aqs_thruster*, float* out, int frames); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/spikes/faust-aot-thruster/bench_thruster.c b/spikes/faust-aot-thruster/bench_thruster.c new file mode 100644 index 0000000..8972cb6 --- /dev/null +++ b/spikes/faust-aot-thruster/bench_thruster.c @@ -0,0 +1,59 @@ +/* Per-block callback time vs deadline. usage: bench_thruster [blocks] [rate] [frames] [noop] + noop: time a 0.05 ms busy-wait instead of process(), to tell host stalls from DSP cost */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include "aqs_thruster.h" + +static int cmp(const void* a, const void* b) { double x = *(const double*)a, y = *(const double*)b; return (x > y) - (x < y); } + +int main(int argc, char** argv) { + int blocks = argc > 1 ? atoi(argv[1]) : 20000; + int rate = argc > 2 ? atoi(argv[2]) : 48000; + int frames = argc > 3 ? atoi(argv[3]) : 512; + int noop = argc > 4; + aqs_thruster* t = aqs_thruster_create(rate); + if (!t || blocks < 100) return 2; + float* out = malloc(sizeof(float) * frames); + double* ms = malloc(sizeof(double) * blocks); + double* cpu = malloc(sizeof(double) * blocks); /* thread CPU time per block: a large wall-cpu gap means time off-CPU; no gap means the stall is charged to the thread */ + double* wall = malloc(sizeof(double) * blocks); /* CLOCK_REALTIME at block end, seconds, for lining up with host load */ + const int warm = 200; + for (int i = 0; i < warm + blocks; ++i) { + /* sweep rpm across its whole range so the smoother and filters keep moving */ + aqs_thruster_set_rpm(t, 300.f + 5700.f * (0.5f + 0.5f * (float)((i % 997) - 498) / 498.f)); + struct timespec a, b, ca, cb, w; + clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ca); + clock_gettime(CLOCK_MONOTONIC, &a); + if (noop) { struct timespec s; do clock_gettime(CLOCK_MONOTONIC, &s); while ((s.tv_sec - a.tv_sec) * 1e9 + (s.tv_nsec - a.tv_nsec) < 5e4); } + else aqs_thruster_process(t, out, frames); + clock_gettime(CLOCK_MONOTONIC, &b); + clock_gettime(CLOCK_THREAD_CPUTIME_ID, &cb); + clock_gettime(CLOCK_REALTIME, &w); + if (i >= warm) { + ms[i - warm] = (b.tv_sec - a.tv_sec) * 1e3 + (b.tv_nsec - a.tv_nsec) / 1e6; + cpu[i - warm] = (cb.tv_sec - ca.tv_sec) * 1e3 + (cb.tv_nsec - ca.tv_nsec) / 1e6; + wall[i - warm] = w.tv_sec + w.tv_nsec / 1e9; + } + } + double deadline = 1e3 * frames / rate, sum = 0; + int over = 0; + printf("start_cpu=%d\n", sched_getcpu()); + for (int i = 0; i < blocks; ++i) { + sum += ms[i]; + if (ms[i] > deadline) { + ++over; + printf("OVERRUN block=%d wall_unix=%.6f wall_ms=%.4f thread_cpu_ms=%.4f\n", i, wall[i], ms[i], cpu[i]); + } + } + qsort(ms, blocks, sizeof *ms, cmp); + double med = ms[blocks / 2], p99 = ms[(int)(blocks * 0.99)], mx = ms[blocks - 1]; + printf("rate=%d frames=%d blocks=%d deadline_ms=%.4f\n", rate, frames, blocks, deadline); + printf("median_ms=%.4f p99_ms=%.4f max_ms=%.4f mean_ms=%.4f\n", med, p99, mx, sum / blocks); + printf("median_pct_deadline=%.2f p99_pct_deadline=%.2f max_pct_deadline=%.2f over_deadline=%d\n", + 100 * med / deadline, 100 * p99 / deadline, 100 * mx / deadline, over); + free(out); free(ms); free(cpu); free(wall); aqs_thruster_destroy(t); + return 0; /* timing is reported, not gated: a shared host stalls */ +} diff --git a/spikes/faust-aot-thruster/run.sh b/spikes/faust-aot-thruster/run.sh new file mode 100755 index 0000000..40e9717 --- /dev/null +++ b/spikes/faust-aot-thruster/run.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# Build the AOT thruster spike, run the boundary test, then the per-block timing report. +# Linux only. usage: run.sh [--bench-only] ; env: FAUST (faust CLI), FAUST_PREFIX (has include/faust), +# FAUST_LIBS (dir with stdfaust.lib), CORE (cpu to pin the bench to via taskset; default: unpinned), OUT (build dir), BLOCKS, RATE, FRAMES. +set -euo pipefail +here=$(cd "$(dirname "$0")" && pwd) +FAUST=${FAUST:-faust} +FAUST_PREFIX=${FAUST_PREFIX:-$(dirname "$(dirname "$(command -v "$FAUST")")")} +FAUST_LIBS=${FAUST_LIBS:-$FAUST_PREFIX/share/faust} +OUT=${OUT:-$here/build} +mkdir -p "$OUT" +[ -f "$FAUST_LIBS/stdfaust.lib" ] || { echo "no stdfaust.lib in FAUST_LIBS=$FAUST_LIBS" >&2; exit 2; } +"$FAUST" -lang cpp -cn ThrusterDsp -I "$FAUST_LIBS" "$here/thruster.dsp" -o "$OUT/thruster_dsp.h" +"$FAUST" --version | head -1 +CXXF="-O2 -std=c++17 -fvisibility=hidden -I$OUT -I$here -I$FAUST_PREFIX/include" +g++ $CXXF -shared -fPIC -o "$OUT/libaqs_thruster.so" "$here/aqs_thruster.cpp" +gcc -O2 -I"$here" -o "$OUT/test_thruster" "$here/test_thruster.c" -L"$OUT" -laqs_thruster -lm -Wl,-rpath,"$OUT" +gcc -O2 -I"$here" -o "$OUT/bench_thruster" "$here/bench_thruster.c" -L"$OUT" -laqs_thruster -lm -Wl,-rpath,"$OUT" +"$OUT/test_thruster" +echo "--- timing (Linux $(uname -r), $(grep -m1 'model name' /proc/cpuinfo | cut -d: -f2 | xargs)); Windows/Unity: unverified" +PIN=() +if [ -n "${CORE:-}" ]; then + command -v taskset >/dev/null && PIN=(taskset -c "$CORE") || echo "CORE set but taskset missing: running unpinned" >&2 +fi +echo "bench start $(date -u +%FT%T.%NZ) pin=${PIN[*]:-none} loadavg_before=$(cut -d' ' -f1-3 /proc/loadavg)" +"${PIN[@]}" "$OUT/bench_thruster" "${BLOCKS:-20000}" "${RATE:-48000}" "${FRAMES:-512}" ${NOOP:+noop} +echo "bench end $(date -u +%FT%T.%NZ) loadavg_after=$(cut -d' ' -f1-3 /proc/loadavg)" diff --git a/spikes/faust-aot-thruster/test_thruster.c b/spikes/faust-aot-thruster/test_thruster.c new file mode 100644 index 0000000..2ecf293 --- /dev/null +++ b/spikes/faust-aot-thruster/test_thruster.c @@ -0,0 +1,94 @@ +/* Behaviour test for the bounded-parameter boundary. Plain C: proves the ABI is callable without C++. */ +#include +#include +#include +#include "aqs_thruster.h" + +static int fails; +#define CHECK(c) do { if (!(c)) { printf("FAIL line %d: %s\n", __LINE__, #c); ++fails; } } while (0) + +static float peak_after(aqs_thruster* t, float rpm, int* finite) { + static float buf[512]; + float pk = 0; + *finite = 1; + aqs_thruster_set_rpm(t, rpm); + for (int b = 0; b < 200; ++b) { /* ~2 s: lets the 0.2 s rpm smoother settle */ + CHECK(aqs_thruster_process(t, buf, 512) == 512); + for (int i = 0; i < 512; ++i) { + if (!isfinite(buf[i])) *finite = 0; + if (fabsf(buf[i]) > pk) pk = fabsf(buf[i]); + } + } + return pk; +} + +int main(void) { + CHECK(aqs_thruster_create(100) == NULL); + CHECK(aqs_thruster_create(1000000) == NULL); + aqs_thruster* t = aqs_thruster_create(48000); + CHECK(t != NULL); + if (!t) return 1; + + /* bounds come from the .dsp hslider("rpm", 1200, 300, 6000, 1) */ + CHECK(aqs_thruster_rpm_min(t) == 300.f); + CHECK(aqs_thruster_rpm_max(t) == 6000.f); + CHECK(aqs_thruster_rpm_default(t) == 1200.f); + CHECK(aqs_thruster_rpm(t) == 1200.f); + + CHECK(aqs_thruster_set_rpm(t, 4800.f) == AQS_PARAM_OK && aqs_thruster_rpm(t) == 4800.f); + CHECK(aqs_thruster_set_rpm(t, 300.f) == AQS_PARAM_OK && aqs_thruster_rpm(t) == 300.f); + CHECK(aqs_thruster_set_rpm(t, 6000.f) == AQS_PARAM_OK && aqs_thruster_rpm(t) == 6000.f); + CHECK(aqs_thruster_clamp_count(t) == 0 && aqs_thruster_nan_count(t) == 0); + + CHECK(aqs_thruster_set_rpm(t, 299.9f) == AQS_PARAM_CLAMPED && aqs_thruster_rpm(t) == 300.f); + CHECK(aqs_thruster_set_rpm(t, -50.f) == AQS_PARAM_CLAMPED && aqs_thruster_rpm(t) == 300.f); + CHECK(aqs_thruster_set_rpm(t, 6000.1f) == AQS_PARAM_CLAMPED && aqs_thruster_rpm(t) == 6000.f); + CHECK(aqs_thruster_set_rpm(t, INFINITY) == AQS_PARAM_CLAMPED && aqs_thruster_rpm(t) == 6000.f); + CHECK(aqs_thruster_set_rpm(t, -INFINITY) == AQS_PARAM_CLAMPED && aqs_thruster_rpm(t) == 300.f); + CHECK(aqs_thruster_clamp_count(t) == 5); + + aqs_thruster_set_rpm(t, 5000.f); + CHECK(aqs_thruster_set_rpm(t, NAN) == AQS_PARAM_NAN && aqs_thruster_rpm(t) == 1200.f); + CHECK(aqs_thruster_nan_count(t) == 1); + + CHECK(aqs_thruster_set_rpm(NULL, 1000.f) < 0); + float buf[16]; + CHECK(aqs_thruster_process(t, buf, -1) == -1); + CHECK(aqs_thruster_process(t, buf, AQS_MAX_BLOCK + 1) == -1); + CHECK(aqs_thruster_process(t, NULL, 16) == -1); + CHECK(aqs_thruster_process(NULL, buf, 16) == -1); + CHECK(aqs_thruster_process(t, buf, 0) == 0); + + /* hostile inputs never reach the DSP: output stays finite and under the patch's own peak bound */ + float hostile[] = {NAN, INFINITY, -INFINITY, 1e30f, -1e30f, 0.f}; + for (unsigned i = 0; i < sizeof hostile / sizeof *hostile; ++i) { + aqs_thruster* h = aqs_thruster_create(48000); + int fin; + float pk = peak_after(h, hostile[i], &fin); + CHECK(fin); + CHECK(pk > 0.01f && pk < 1.0f); + aqs_thruster_destroy(h); + } + + /* the patch is audibly rpm-dependent and loud at high rpm (guards against a stuck parameter) */ + int fin; + aqs_thruster* lo = aqs_thruster_create(48000); + aqs_thruster* hi = aqs_thruster_create(48000); + float plo = peak_after(lo, 300.f, &fin), phi = peak_after(hi, 4800.f, &fin); + CHECK(phi > plo); + CHECK(phi < 0.45f); /* patch comment: peak under 0.4 at rpm 4800, small margin */ + aqs_thruster_destroy(lo); aqs_thruster_destroy(hi); + + /* same rpm, same sample rate: two instances render identical bytes (noise is seeded) */ + aqs_thruster* a = aqs_thruster_create(48000); + aqs_thruster* b = aqs_thruster_create(48000); + static float xa[2048], xb[2048]; + aqs_thruster_set_rpm(a, 2000.f); aqs_thruster_set_rpm(b, 2000.f); + aqs_thruster_process(a, xa, 2048); aqs_thruster_process(b, xb, 2048); + CHECK(memcmp(xa, xb, sizeof xa) == 0); + aqs_thruster_destroy(a); aqs_thruster_destroy(b); + + aqs_thruster_destroy(t); + printf(fails ? "FAILED (%d)\n" : "ok\n", fails); + return fails != 0; +} diff --git a/spikes/faust-aot-thruster/thruster.dsp b/spikes/faust-aot-thruster/thruster.dsp new file mode 100644 index 0000000..c36a8ae --- /dev/null +++ b/spikes/faust-aot-thruster/thruster.dsp @@ -0,0 +1,30 @@ +// thruster: sustained, heavy, brittle-hull thruster with an RPM control. +// Compile as Faust source (AquaSynthPatchCompiler.CompileSource); it is not a .aqua script. +// Control: rpm (300..6000). Rotor tone = 28 Hz + rpm*0.025 (about 36..180 Hz); three blades chop the rasp at 3x that. +declare name "thruster"; +import("stdfaust.lib"); + +rpm = hslider("rpm", 1200, 300, 6000, 1) : si.smooth(ba.tau2pole(0.2)); +load = (rpm - 300) / 5700; +shaft = 28 + rpm * 0.025; + +// effort: slow random wander of the shaft speed +wander = no.noise : fi.lowpass(2, 5) : *(0.35); +f0 = shaft * (1 + 0.04 * wander); + +// low rumble: saw at shaft speed plus a square a sub-octave down, low-passed +// the low-pass opens up as load falls, so at low rpm the rotor tone reaches the 150-300 Hz range a small speaker can show +rumble = (os.sawtooth(f0) + 0.7 * os.square(f0 * 0.5)) : fi.lowpass(3, 60 + 2.5 * shaft + 140 * (1 - load)); + +// blade pass: rasp noise chopped at 3x shaft, band tracks rpm +chop = 0.5 + 0.5 * os.osc(f0 * 3) ^ 2; +rasp = no.noise : fi.resonbp(700 + rpm * 0.6, 1.5, 1) : *(chop); + +// brittle hull: high-Q modes rattled by noise, louder with load (a 1.5% floor at idle, so low rpm is tone-led); tanh on the rattle alone keeps ring-up spikes from setting the peak +mode(f, q) = no.noise : fi.resonbp(f, q, 1); +rattle = (mode(173, 20) * (0.3 + 0.7 * load) + 0.8 * mode(311, 18) + 0.6 * mode(467, 16) + 0.4 * mode(1130, 14)) * (0.015 + 0.985 * load ^ 2) * 3 : *(4) : ma.tanh : /(4); + +mix = rumble * (0.5 + 0.5 * load) + rasp * (0.05 + 1.75 * load) + rattle; +// drive falls only gently with load so tanh stays out of saturation while high rpm still ends up louder than low rpm +// final 0.425 trim keeps peak under 0.4 at rpm 4800 +process = mix * (0.4 + 0.6 * load) * (0.9 - 0.5 * load) : ma.tanh : *(0.425); diff --git a/src/AquaSynth.Core/BuiltInScripts.cs b/src/AquaSynth.Core/BuiltInScripts.cs index a8f4396..fb339f3 100644 --- a/src/AquaSynth.Core/BuiltInScripts.cs +++ b/src/AquaSynth.Core/BuiltInScripts.cs @@ -89,7 +89,6 @@ public static class BuiltInScripts drive=0.12 """), ("explosion", """ - patch repeat=0.174744 voice wave=noise freq=20 @@ -188,7 +187,7 @@ public static class BuiltInScripts phaser=0.0001458 phaser_ramp=-0.000504 - patch repeat=0.174744 + patch gain=0.5 voice use=Noise freq=20 @@ -205,7 +204,6 @@ public static class BuiltInScripts tremolo=0.0264 tremolo_hz=13.04 - patch repeat=0.11315 voice wave=sine freq=57.5946 diff --git a/src/AquaSynth.Core/ControlSplineTimeline.cs b/src/AquaSynth.Core/ControlSplineTimeline.cs index 232e9df..fd279b5 100644 --- a/src/AquaSynth.Core/ControlSplineTimeline.cs +++ b/src/AquaSynth.Core/ControlSplineTimeline.cs @@ -68,6 +68,21 @@ public sealed class ControlSplineTimeline .ToList(), StringComparer.OrdinalIgnoreCase); + /// + /// The splines that decide one surface. The timeline merges every enabled spline of a surface into one bezier curve over + /// the union of their points, so two or more splines become one merged spline; a single spline keeps its own shape. + /// The Faust exporter and the probe report take this instead of summing per-spline offsets. + /// + public static ControlSpline[] SurfaceSplines(IEnumerable all, string surfacePath) + { + var splines = all + .Where(spline => spline.Enabled && spline.SurfacePath.Equals(surfacePath, StringComparison.OrdinalIgnoreCase)) + .ToArray(); + if (splines.Length < 2) return splines; + var points = splines.SelectMany(spline => spline.Points).OrderBy(point => point.TimeSeconds).ToArray(); + return [new ControlSpline(string.Join("_", splines.Select(spline => spline.Name)), surfacePath, points)]; + } + public IReadOnlyList SurfacePaths => _pointsBySurface.Keys.ToArray(); public void SetFuturePoint(string surfacePath, float timeSeconds, float normalizedValue, float nowSeconds) => diff --git a/src/AquaSynth.Core/CultCachePatchDocument.cs b/src/AquaSynth.Core/CultCachePatchDocument.cs index 9e73596..12e9abe 100644 --- a/src/AquaSynth.Core/CultCachePatchDocument.cs +++ b/src/AquaSynth.Core/CultCachePatchDocument.cs @@ -27,7 +27,8 @@ public sealed record CultCachePatchDatabaseEntry( [property: Key(1)] string Family, [property: Key(2)] string Name, [property: Key(3)] string Source, - [property: Key(4)] string Notes); + [property: Key(4)] string Notes, + [property: Key(5), CultElementId("Path")] string ElementId = ""); [MessagePackObject] public sealed record CultCachePatchClaimCard( @@ -36,7 +37,8 @@ public sealed record CultCachePatchClaimCard( [property: Key(2)] CultCacheReferenceTruth Reference, [property: Key(3)] CultCacheIntentTruth Intent, [property: Key(4)] string Tier, - [property: Key(5)] CultCacheProofTruth Proof); + [property: Key(5)] CultCacheProofTruth Proof, + [property: Key(6), CultElementId("Id")] string ElementId = ""); [MessagePackObject] public sealed record CultCacheSpeechClaimCard( @@ -45,7 +47,8 @@ public sealed record CultCacheSpeechClaimCard( [property: Key(2)] CultCacheSpeechTargetTruth Target, [property: Key(3)] CultCacheIntentTruth Intent, [property: Key(4)] string Tier, - [property: Key(5)] CultCacheProofTruth Proof); + [property: Key(5)] CultCacheProofTruth Proof, + [property: Key(6), CultElementId("Id")] string ElementId = ""); [MessagePackObject] public sealed record CultCacheReferenceTruth( diff --git a/src/AquaSynth.Core/FaustExport.VocalLadder.cs b/src/AquaSynth.Core/FaustExport.VocalLadder.cs new file mode 100644 index 0000000..a7a4e66 --- /dev/null +++ b/src/AquaSynth.Core/FaustExport.VocalLadder.cs @@ -0,0 +1,184 @@ +using System.Text; + +namespace AquaSynth.Dsl; + +// The vocal_network resonator: a Kelly-Lochbaum ladder per waveguide path, one cell per sample (cell length c / sample +// rate), written as a Faust `letrec` so every wave variable is a one-sample state. Cell areas come from the morphology +// rest shape deformed by the path's area control, or from its measured vowel shapes (DeformedAreaExpression owns both). A branch port whose +// two paths are both in the network becomes a three-port parallel junction (Pink Trombone's velum: ports are the oral +// cells either side of the join and the branch path's first cell, whose area is the branch opening). +public static partial class FaustEmitter +{ + // Reflection at the closed glottal end of a path that carries a source. + private const float LadderGlottalReflection = 0.75f; + // Speed of sound in cm/s used only to size the lip end correction from the lip radius (the ladder's own cell length uses the path's speed). + private const float LipEndCorrectionSoundSpeedCentimeters = 35000f; + + private sealed record LadderPath( + WaveguidePath Path, + AreaFunction Area, + int AreaIndex, + int PathIndex, + string Prefix, + int Cells, + RadiationLoad? Radiation, + int RadiationIndex) + { + public string A(int i) => $"{Prefix}_A{i}"; + public string R(int i) => $"{Prefix}_R{i}"; + public string L(int i) => $"{Prefix}_L{i}"; + public string Refl(int i) => $"{Prefix}_r{i}"; + } + + // A flow that enters the path at a cell rather than at the glottal end: Position is a Faust expression for the normalized place (0 glottis, + // 1 lips) and Flow the signal as it would enter the first cell. It is spread over the two cells whose centres bracket the place (a hat of + // width one cell), so the place can move between gestures and a place at or behind the first cell centre is the glottal end. The ladder + // carries flow waves, so the same noise pressure in a cell of area A is a flow of Flow * A / A(first cell): a burst at the closure, + // where the area is nearly zero, is small until the constriction opens (measured: unscaled it was 30 times louder in the closure). + private sealed record LadderInjection(string Position, string Flow); + + private sealed record LadderJunction(LadderPath Oral, LadderPath Branch, int Interface, string Velum, string Left, string Right, string Nose); + + private static int LadderCells(WaveguidePath path, TractAreaFunction shape, FaustExportOptions options) => + Math.Max(3, (int)MathF.Round(shape.LengthMeters * options.SampleRate / Math.Max(1f, path.PropagationSpeedMetersPerSecond))); + + /// + /// Emits the area, reflection and junction definitions and the recursive ladder for , and returns, + /// per path, the identifier of its lip output (the right-going wave leaving its last cell) and, for each path that ends at a glottis, + /// the identifier of the pressure in its first cell (the tract wall radiation reads it). + /// + private static (IReadOnlyDictionary Lips, IReadOnlyDictionary GlottalPressures) EmitVocalLadder( + StringBuilder source, + SynthPatch patch, + VocalNetwork network, + string name, + IReadOnlyList members, + IReadOnlyDictionary inputs, + IReadOnlyDictionary> injections, + IReadOnlyDictionary admittances, + ParameterMap parameters, + List warnings) + { + var junctions = new List(); + foreach (var branchName in network.Branches) + { + var branch = patch.BranchPorts.FirstOrDefault(candidate => candidate.Name.Equals(branchName, StringComparison.OrdinalIgnoreCase)); + var oral = branch is null ? null : members.FirstOrDefault(member => member.Path.Name.Equals(branch.FromPath, StringComparison.OrdinalIgnoreCase)); + var nasal = branch is null ? null : members.FirstOrDefault(member => member.Path.Name.Equals(branch.ToPath, StringComparison.OrdinalIgnoreCase)); + if (branch is null || oral is null || nasal is null || oral == nasal) + { + continue; + } + + var cell = Math.Clamp((int)MathF.Round(Math.Clamp(branch.FromPosition, 0, 1) * oral.Cells), 1, oral.Cells - 2); + if (junctions.Any(existing => existing.Oral == oral && existing.Interface == cell) || junctions.Any(existing => existing.Branch == nasal)) + { + warnings.Add($"{name}: branch `{branch.Name}` shares a junction cell or target path with another branch; ignored by the ladder"); + continue; + } + + // The velopharyngeal port area, cm^2: opening * coupling * the design maximum (NasalTractAreas); it is the nasal ladder's first cell. + var velum = $"{name}_primitive_branch_{SafeIdentifier(branch.Name)}_velum_area"; + source.AppendLine($" {velum} = max(0.000001, {admittances[branch.Name]} * {F(NasalTractAreas.VelopharyngealPortMaximumAreaSquareCentimeters)});"); + // The junction sits on the interface between oral cells cell - 1 and cell: R(cell) is entered from R(cell - 1), L(cell - 1) from L(cell). + var sum = $"({oral.A(cell - 1)} + {oral.A(cell)} + {velum})"; + var key = $"{name}_primitive_branch_{SafeIdentifier(branch.Name)}"; + source.AppendLine($" {key}_refl_left = (2.0 * {oral.A(cell - 1)} - {sum}) / {sum};"); + source.AppendLine($" {key}_refl_right = (2.0 * {oral.A(cell)} - {sum}) / {sum};"); + source.AppendLine($" {key}_refl_nose = (2.0 * {velum} - {sum}) / {sum};"); + junctions.Add(new LadderJunction(oral, nasal, cell, velum, $"{key}_refl_left", $"{key}_refl_right", $"{key}_refl_nose")); + } + + var targets = junctions.ToDictionary(junction => junction.Branch); + var joinedAt = junctions.ToDictionary(junction => (junction.Oral, junction.Interface)); + var equations = new StringBuilder(); + + foreach (var member in members) + { + var targetJunction = targets.GetValueOrDefault(member); + var sites = new List<(string Site, string Flow)>(); + foreach (var injection in injections.GetValueOrDefault(member.Path.Name) ?? []) + { + var site = $"{member.Prefix}_injection{sites.Count}_site"; + source.AppendLine($" {site} = min({F(member.Cells - 0.5f)}, max(0.5, ({injection.Position}) * {F(member.Cells)}));"); + sites.Add((site, injection.Flow)); + } + + string Injected(int cell) => string.Concat(sites.Select(entry => $" + max(0.0, 1.0 - abs({F(cell + 0.5f)} - {entry.Site})) * ({entry.Flow}) * {member.A(cell)} / {member.A(0)}")); + for (var i = 0; i < member.Cells; i++) + { + if (i == 0 && targetJunction is not null) + { + source.AppendLine($" {member.A(0)} = {targetJunction.Velum};"); + continue; + } + + var area = DeformedAreaExpression(parameters, member.Area.Shape, member.Area.Deformation, $"/vocal/areas/{member.AreaIndex}/area", (i + 0.5f) / member.Cells, member.Area.Vowels); + source.AppendLine($" {member.A(i)} = max(0.000001, {area});"); + } + + for (var i = 1; i < member.Cells; i++) + { + source.AppendLine($" {member.Refl(i)} = ({member.A(i - 1)} - {member.A(i)}) / ({member.A(i - 1)} + {member.A(i)});"); + } + + var loss = $"clip01({parameters.Expression(OwnerField($"/vocal/paths/{member.PathIndex}", "loss"), member.Path.Loss)})"; + var lipReflection = member.Radiation is { } load + ? parameters.Expression(OwnerField($"/vocal/radiation/{member.RadiationIndex}", "reflection"), load.Reflection) + : F(new RadiationLoad("", "").Reflection); + var last = member.Cells - 1; + + equations.AppendLine(targetJunction is { } entering + ? $" '{member.R(0)} = ({entering.Oral.L(entering.Interface)} + {entering.Oral.R(entering.Interface - 1)}) * (1.0 + {entering.Nose}) * {loss} + {entering.Nose} * {member.L(0)} * {loss};" + : $" '{member.R(0)} = ({member.L(0)} * {F(LadderGlottalReflection)} + {(inputs.TryGetValue(member.Path.Name, out var input) ? input : "0.0")}{Injected(0)}) * {loss};"); + for (var i = 1; i < member.Cells; i++) + { + equations.AppendLine(joinedAt.TryGetValue((member, i), out var join) + ? $" '{member.R(i)} = ({join.Right} * {member.L(i)} + (1.0 + {join.Right}) * ({member.R(i - 1)} + {join.Branch.L(0)})) * {loss};" + : $" '{member.R(i)} = ({member.R(i - 1)} - {member.Refl(i)} * ({member.R(i - 1)} + {member.L(i)}){Injected(i)}) * {loss};"); + } + + for (var i = 0; i < last; i++) + { + equations.AppendLine(joinedAt.TryGetValue((member, i + 1), out var join) + ? $" '{member.L(i)} = ({join.Left} * {member.R(i)} + (1.0 + {join.Left}) * ({join.Branch.L(0)} + {member.L(i + 1)})) * {loss};" + : $" '{member.L(i)} = ({member.L(i + 1)} + {member.Refl(i + 1)} * ({member.R(i)} + {member.L(i + 1)})) * {loss};"); + } + + // Open-end reflectance R(z) = -g (1-b)/(1-b z^-1): g is the radiation load's reflection (DC gain, negative), b follows the + // lip cell's area so that the low-frequency loss 1-|R|^2 ~ (ka)^2 and the DC group delay b/(1-b) is the end correction + // (Levine & Schwinger; Smith PASP; Valimaki & Karjalainen 1994). With a = sqrt(A/pi) in cm, q = (a fs / c)^2, c = 35000 cm/s, + // b/(1-b)^2 = q gives b = ((2q+1) - sqrt(4q+1)) / (2q). The pole is one-sample state in the same slot the constant used. + var q = $"{member.Prefix}_lip_q"; + var b = $"{member.Prefix}_lip_b"; + source.AppendLine($" {q} = max(0.000001, pow(sqrt({member.A(last)} / ma.PI) * ma.SR / {F(LipEndCorrectionSoundSpeedCentimeters)}, 2.0));"); + source.AppendLine($" {b} = min(0.9, max(0.0, ((2.0 * {q} + 1.0) - sqrt(4.0 * {q} + 1.0)) / (2.0 * {q})));"); + equations.AppendLine($" '{member.L(last)} = ({b} * {member.L(last)} + (1.0 - {b}) * ({lipReflection}) * {member.R(last)}) * {loss};"); + } + + // Outputs: every path's lip wave, then the first-cell pressure R0 + L0 of each path that ends at a glottis (a branch path's first + // cell is the junction, not a glottis). + var glottal = members.Where(member => !targets.ContainsKey(member)).ToArray(); + var outputs = string.Join(", ", members.Select(member => member.R(member.Cells - 1)).Concat(glottal.Select(member => $"({member.R(0)} + {member.L(0)})"))); + var outputCount = members.Count + glottal.Length; + var ladder = $"{name}_primitive_ladder"; + source.AppendLine($" {ladder} = ({outputs}) letrec {{"); + source.Append(equations); + source.AppendLine(" };"); + + var lips = new Dictionary(StringComparer.OrdinalIgnoreCase); + var glottalPressures = new Dictionary(StringComparer.OrdinalIgnoreCase); + for (var m = 0; m < outputCount; m++) + { + var isLip = m < members.Count; + var owner = isLip ? members[m] : glottal[m - members.Count]; + var tap = $"{owner.Prefix}_{(isLip ? "lip" : "glottal_pressure")}"; + source.AppendLine(outputCount == 1 + ? $" {tap} = {ladder};" + : $" {tap} = {ladder} : ba.selector({m}, {outputCount});"); + (isLip ? lips : glottalPressures)[owner.Path.Name] = tap; + } + + return (lips, glottalPressures); + } +} diff --git a/src/AquaSynth.Core/FaustExport.cs b/src/AquaSynth.Core/FaustExport.cs index 2059892..c48c1cf 100644 --- a/src/AquaSynth.Core/FaustExport.cs +++ b/src/AquaSynth.Core/FaustExport.cs @@ -4,11 +4,11 @@ using System.Text; namespace AquaSynth.Dsl; -public sealed record FaustExportOptions(string Name = "aquasynth_patch", bool Stereo = false, bool DebugProbeUi = false); +public sealed record FaustExportOptions(string Name = "aquasynth_patch", bool Stereo = false, bool DebugProbeUi = false, int SampleRate = 48000); public sealed record FaustExport(string Source, IReadOnlyList Warnings); -public static class FaustEmitter +public static partial class FaustEmitter { public static FaustExport EmitScript(string script, FaustExportOptions? options = null) => Emit(PatchScript.Parse(script), options ?? new FaustExportOptions()); @@ -29,12 +29,10 @@ public static class FaustEmitter if (patch.Repeat is { } repeat) { var interval = parameters.Expression("/patch/repeat", repeat.IntervalSeconds); - source.AppendLine($"age = time - floor(time / {interval}) * {interval};"); - } - else - { - source.AppendLine("age = time;"); + // sfxr's repeat restarts the pitch slide and the arpeggio, not the envelope: age keeps running, the voices wrap their pitch age. + source.AppendLine($"repeat_interval = {interval};"); } + source.AppendLine("age = time;"); source.AppendLine("clip01(x) = min(1.0, max(0.0, x));"); source.AppendLine("wrap01(x) = x - floor(x);"); source.AppendLine("slew(rate,x) = x : *(1.0 - exp((0.0 - max(0.0001, rate)) / ma.SR)) : + ~ *(exp((0.0 - max(0.0001, rate)) / ma.SR));"); @@ -47,6 +45,15 @@ public static class FaustEmitter source.AppendLine("bez(t,a,b,c,d) = (1.0 - t) * (1.0 - t) * (1.0 - t) * a + 3.0 * (1.0 - t) * (1.0 - t) * t * b + 3.0 * (1.0 - t) * t * t * c + t * t * t * d;"); source.AppendLine("seg_bez(t,t0,d,a,b,c,e) = bez(clip01((t - t0) / max(0.0001, d)), a, b, c, e);"); source.AppendLine("smooth01(x) = clip01(x) * clip01(x) * (3.0 - 2.0 * clip01(x));"); + // si.smoo starts every control at 0 and climbs to it: a word began with the tract shut, the vowel weights at 0 and the source off, + // and the first ~100 ms of every render were that climb (the level rose 40 dB over the first 100 ms of an isolated vowel, and + // the bandwidths fell from ~300 Hz to 90). smoo_from starts at its input and smooths from there. smoo_init is si.smoo's own + // pole (1 - 44.1 / SR, a 22.7 ms time constant), so live control moves exactly as fast as before; only the start differs. + // Started at its input a constant control holds there to the bit; started at 0 (si.smoo, si.smooth) it stalls 1e-5 to 5e-5 short of it, + // in either way of writing the step. The source's pitch-ratio smoother starts at its input for that reason: from 0 the 2744 Hz alias + // source ran 0.12 Hz flat and leaked its harmonics into the alias measurement. + source.AppendLine("smoo_from(pole, x) = loop ~ _ with { loop(prev) = select2(1 - 1', prev * pole + x * (1.0 - pole), x); };"); + source.AppendLine("smoo_init(x) = smoo_from(1.0 - 44.1 / ma.SR, x);"); source.AppendLine("seg_smooth(t,t0,d,a,b) = a + (b - a) * smooth01((t - t0) / max(0.0001, d));"); source.AppendLine("seg_exp(t,t0,d,a,b) = exp(log(max(0.00001, a)) + (log(max(0.00001, b)) - log(max(0.00001, a))) * clip01((t - t0) / max(0.0001, d)));"); source.AppendLine("seg_curve(c,t,t0,d,a,b) = select2(c < 0.5, seg_exp(t,t0,d,a,b), seg(t,t0,d,a,b));"); @@ -154,15 +161,13 @@ public static class FaustEmitter var baseId = $"{id}_base"; var normalized = $"{id}_normalized"; var physical = $"{id}_physical"; - var publicControl = $"hslider(\"{Escape(surface.Path)}\", {F(surface.DefaultNormalized)}, 0, 1, 0.001) : si.smoo"; + var publicControl = $"hslider(\"{Escape(surface.Path)}\", {F(surface.DefaultNormalized)}, 0, 1, 0.001) : smoo_init"; var boundControl = parameters.TryBindingExpression(surface.FieldPath, out var bindingExpression) ? $"clip01(({bindingExpression} - {F(surface.MinValue)}) / max(0.000001, {F(surface.MaxValue - surface.MinValue)}))" : publicControl; source.AppendLine($"{baseId} = {boundControl};"); - var splines = patch.ControlSplines - .Where(spline => spline.Enabled && spline.SurfacePath.Equals(surface.Path, StringComparison.OrdinalIgnoreCase)) - .ToArray(); + var splines = ControlSplineTimeline.SurfaceSplines(patch.ControlSplines, surface.Path); if (splines.Length == 0) { source.AppendLine($"{normalized} = clip01({baseId});"); @@ -176,7 +181,7 @@ public static class FaustEmitter var splineId = $"{id}_spline_{splineIndex}"; EmitControlSpline(source, spline, splineId, surface.DefaultNormalized); var depth = $"{splineId}_depth"; - source.AppendLine($"{depth} = hslider(\"/splines/{Escape(spline.Name)}/depth\", 1, 0, 1, 0.001) : si.smoo;"); + source.AppendLine($"{depth} = hslider(\"/splines/{Escape(spline.Name)}/depth\", 1, 0, 1, 0.001) : smoo_init;"); terms.Add($"(({splineId}_value - {F(surface.DefaultNormalized)}) * {depth})"); } @@ -201,11 +206,11 @@ public static class FaustEmitter { var point = points[i]; source.AppendLine($"{splineId}_p{i}_time = hslider(\"/splines/{Escape(spline.Name)}/{i}/time\", {F(point.TimeSeconds)}, 0, 120, 0.001);"); - source.AppendLine($"{splineId}_p{i}_value = hslider(\"/splines/{Escape(spline.Name)}/{i}/value\", {F(Math.Clamp(point.Value, 0, 1))}, 0, 1, 0.001) : si.smoo;"); + source.AppendLine($"{splineId}_p{i}_value = hslider(\"/splines/{Escape(spline.Name)}/{i}/value\", {F(Math.Clamp(point.Value, 0, 1))}, 0, 1, 0.001) : smoo_init;"); source.AppendLine($"{splineId}_p{i}_out_time = hslider(\"/splines/{Escape(spline.Name)}/{i}/out_time\", {F(Math.Max(0, point.OutTimeOffsetSeconds))}, 0, 10, 0.001);"); - source.AppendLine($"{splineId}_p{i}_out_value = hslider(\"/splines/{Escape(spline.Name)}/{i}/out_value\", {F(point.OutValue == 0 ? point.Value : Math.Clamp(point.OutValue, 0, 1))}, 0, 1, 0.001) : si.smoo;"); + source.AppendLine($"{splineId}_p{i}_out_value = hslider(\"/splines/{Escape(spline.Name)}/{i}/out_value\", {F(point.OutValue == 0 ? point.Value : Math.Clamp(point.OutValue, 0, 1))}, 0, 1, 0.001) : smoo_init;"); source.AppendLine($"{splineId}_p{i}_in_time = hslider(\"/splines/{Escape(spline.Name)}/{i}/in_time\", {F(Math.Max(0, point.InTimeOffsetSeconds))}, 0, 10, 0.001);"); - source.AppendLine($"{splineId}_p{i}_in_value = hslider(\"/splines/{Escape(spline.Name)}/{i}/in_value\", {F(point.InValue == 0 ? point.Value : Math.Clamp(point.InValue, 0, 1))}, 0, 1, 0.001) : si.smoo;"); + source.AppendLine($"{splineId}_p{i}_in_value = hslider(\"/splines/{Escape(spline.Name)}/{i}/in_value\", {F(point.InValue == 0 ? point.Value : Math.Clamp(point.InValue, 0, 1))}, 0, 1, 0.001) : smoo_init;"); } if (points.Length == 1) @@ -327,6 +332,9 @@ public static class FaustEmitter ? $"{voiceAge} = wrap01(age / {F(voice.Note.TriggerIntervalSeconds)}) * {F(voice.Note.TriggerIntervalSeconds)};" : $"{voiceAge} = age;"); + var pitchAge = patch.Repeat is null ? voiceAge : $"{name}_pitch_age"; + if (patch.Repeat is not null) source.AppendLine($"{pitchAge} = {voiceAge} - floor({voiceAge} / repeat_interval) * repeat_interval;"); + var minFreq = parameters.Expression(OwnerField(ownerPath, "pitch/min_freq"), voice.Pitch.MinFrequencyHz); var noteFreq = NoteFrequencyExpression(source, patch.Playback, voice.Note, name, OwnerField(ownerPath, "note/frequency"), parameters); var noteGate = NoteGateExpression(source, patch.Playback, voice.Note, name, OwnerField(ownerPath, "note/gate"), parameters); @@ -335,7 +343,7 @@ public static class FaustEmitter var vibratoDepth = parameters.Expression(OwnerField(ownerPath, "pitch/vibrato"), voice.Pitch.VibratoDepth); var vibratoHz = parameters.Expression(OwnerField(ownerPath, "pitch/vibrato_hz"), voice.Pitch.VibratoHz); var vibratoDelay = parameters.Expression(OwnerField(ownerPath, "pitch/vibrato_delay"), voice.Pitch.VibratoDelaySeconds); - var baseFreq = $"max({minFreq}, {noteFreq} * pow(2.0, {pitchRamp} * {voiceAge} + 0.5 * {pitchDelta} * {voiceAge} * {voiceAge}))"; + var baseFreq = $"max({minFreq}, {noteFreq} * pow(2.0, {pitchRamp} * {pitchAge} + 0.5 * {pitchDelta} * {pitchAge} * {pitchAge}))"; var hasVibrato = voice.Pitch.VibratoDepth != 0 && voice.Pitch.VibratoHz > 0 || parameters.IsBound(OwnerField(ownerPath, "pitch/vibrato")) || parameters.IsBound(OwnerField(ownerPath, "pitch/vibrato_hz")); @@ -344,7 +352,7 @@ public static class FaustEmitter : ""; var arpeggio = voice.Arpeggio is null ? "1.0" - : $"select2({voiceAge} < {parameters.Expression(OwnerField(ownerPath, "arpeggio/delay"), voice.Arpeggio.DelaySeconds)}, {parameters.Expression(OwnerField(ownerPath, "arpeggio/multiplier"), voice.Arpeggio.Multiplier)}, 1.0)"; + : $"select2({pitchAge} < {parameters.Expression(OwnerField(ownerPath, "arpeggio/delay"), voice.Arpeggio.DelaySeconds)}, {parameters.Expression(OwnerField(ownerPath, "arpeggio/multiplier"), voice.Arpeggio.Multiplier)}, 1.0)"; var frequency = $"(({baseFreq}){vibrato}) * {arpeggio} * pow(2.0, patch_mod_pitch + {pitch})"; var dutyExpression = $"clip01({parameters.Expression(OwnerField(ownerPath, "osc/duty"), voice.Oscillator.Duty)} + {parameters.Expression(OwnerField(ownerPath, "duty/ramp"), voice.Duty.RampPerSecond)} * {voiceAge} + patch_mod_duty + {duty})"; var fmIndex = $"max(0.0, {parameters.Expression(OwnerField(ownerPath, "fm/index"), voice.Fm.Index)} + patch_mod_fm_index + {fmIndexMod}) * {FmDecay(parameters.Expression(OwnerField(ownerPath, "fm/decay"), voice.Fm.IndexDecaySeconds), voice.Fm.IndexDecaySeconds, parameters.IsBound(OwnerField(ownerPath, "fm/decay")), voiceAge)}"; @@ -577,6 +585,7 @@ public static class FaustEmitter var radiated = new List(); source.AppendLine($"{name}_primitive_drive = 1.0;"); + var members = new List(); foreach (var pathName in network.Paths) { if (!paths.TryGetValue(pathName, out var path)) @@ -590,24 +599,28 @@ public static class FaustEmitter continue; } - var safePath = SafeIdentifier(path.Name); - var areaIndex = AreaFunctionIndex(patch, area); - var pathIndex = WaveguidePathIndex(patch, path); - var pathField = $"/vocal/paths/{pathIndex}"; - var representativeArea = $"{name}_primitive_path_{safePath}_area"; - var delay = $"{name}_primitive_path_{safePath}_delay"; - var input = $"{name}_primitive_path_{safePath}_input"; - var incoming = $"{name}_primitive_path_{safePath}_incoming_wave"; - var wave = $"{name}_primitive_path_{safePath}_wave"; - var energyIn = $"{name}_primitive_path_{safePath}_energy_in"; - var energyOut = $"{name}_primitive_path_{safePath}_energy_out"; - var passivity = $"{name}_primitive_path_{safePath}_passivity_ratio"; - var loss = $"clip01({parameters.Expression(OwnerField(pathField, "loss"), path.Loss)})"; - source.AppendLine($" {representativeArea} = {ProbeSignal(options, $"/debug/{name}/path/{path.Name}/area", PrimitiveAreaExpression(patch, parameters, area, areaIndex), 0, 8)};"); - source.AppendLine($" {delay} = {ProbeSignal(options, $"/debug/{name}/path/{path.Name}/delay", $"({F(area.Shape.LengthMeters)} / max(1.0, {parameters.Expression(OwnerField(pathField, "speed"), path.PropagationSpeedMetersPerSecond)})) * ma.SR", 0, path.MaxDelaySamples)};"); - keepAlive.Add(representativeArea); - keepAlive.Add(delay); + var load = network.Radiation + .Select(loadName => radiation.TryGetValue(loadName, out var candidate) && candidate.Path.Equals(path.Name, StringComparison.OrdinalIgnoreCase) ? candidate : null) + .FirstOrDefault(candidate => candidate is not null); + members.Add(new LadderPath( + path, + area, + AreaFunctionIndex(patch, area), + WaveguidePathIndex(patch, path), + $"{name}_primitive_path_{SafeIdentifier(path.Name)}", + LadderCells(path, area.Shape, options), + load, + load is null ? 0 : RadiationLoadIndex(patch, load))); + } + // Per-path drive: sources, contact release and branch exchange sum into the path input. A branch whose two paths + // are both in this network is a ladder junction instead and exchanges no flow term. + var memberNames = members.Select(member => member.Path.Name).ToHashSet(StringComparer.OrdinalIgnoreCase); + var inputs = new Dictionary(StringComparer.OrdinalIgnoreCase); + var injections = new Dictionary>(StringComparer.OrdinalIgnoreCase); + foreach (var member in members) + { + var path = member.Path; var sourceTerms = network.Sources .Select(sourceName => sourcePorts.TryGetValue(sourceName, out var port) && port.Path.Equals(path.Name, StringComparison.OrdinalIgnoreCase) ? PrimitiveSourceExpression(source, patch, name, port, frequency, parameters, options, keepAlive) @@ -615,28 +628,71 @@ public static class FaustEmitter .Where(term => term is not null) .Cast() .ToList(); - var contactTerms = network.Contacts - .Select(contactName => contacts.TryGetValue(contactName, out var contact) && contact.Path.Equals(path.Name, StringComparison.OrdinalIgnoreCase) - ? PrimitiveContactExpression(source, patch, name, contact, parameters, options, keepAlive) - : null) - .Where(term => term is not null) - .Cast() - .ToList(); + var contactTerms = new List(); + var pathInjections = new List(); + foreach (var contactName in network.Contacts) + { + if (!contacts.TryGetValue(contactName, out var contact) || !contact.Path.Equals(path.Name, StringComparison.OrdinalIgnoreCase)) continue; + var (released, burst) = PrimitiveContactExpression(source, patch, name, contact, parameters, options, keepAlive); + // A contact that has a position control surface lets its burst go in at that place; one whose place is a fixed number + // keeps the burst at the glottal end with the rest of the path input. + var position = OwnerField($"/vocal/contacts/{ConstrictionContactIndex(patch, contact)}", "position"); + if (patch.ControlSurfaces.Any(surface => surface.FieldPath.Equals(position, StringComparison.OrdinalIgnoreCase))) + { + contactTerms.Add(released); + pathInjections.Add(new LadderInjection(parameters.Expression(position, contact.Position), burst)); + } + else contactTerms.Add($"({released} + {burst})"); + } + + injections[path.Name] = pathInjections; var branchTerms = network.Branches - .Select(branchName => branches.TryGetValue(branchName, out var branch) && branch.FromPath.Equals(path.Name, StringComparison.OrdinalIgnoreCase) + .Select(branchName => branches.TryGetValue(branchName, out var branch) && branch.FromPath.Equals(path.Name, StringComparison.OrdinalIgnoreCase) && !memberNames.Contains(branch.ToPath) ? PrimitiveBranchExpression(source, patch, name, branch, parameters, options, keepAlive) : null) .Where(term => term is not null) .Cast() .ToList(); var pathInputs = sourceTerms.Concat(contactTerms).Concat(branchTerms).ToList(); + var input = $"{member.Prefix}_input"; source.AppendLine($" {input} = {ProbeSignal(options, $"/debug/{name}/path/{path.Name}/input_flow", pathInputs.Count == 0 ? "0.0" : string.Join(" + ", pathInputs), -4, 4)};"); - source.AppendLine($" {incoming} = {ProbeSignal(options, $"/debug/{name}/path/{path.Name}/incoming_wave", input, -4, 4)};"); - source.AppendLine($" {wave} = {ProbeSignal(options, $"/debug/{name}/path/{path.Name}/outgoing_wave", $"({incoming}) : {PrimitiveDelayExpression(path, path.MaxDelaySamples, delay)} * {loss}", -4, 4)};"); + inputs[path.Name] = input; + keepAlive.Add(input); + } + + var admittances = new Dictionary(StringComparer.OrdinalIgnoreCase); + foreach (var branchName in network.Branches) + { + if (branches.TryGetValue(branchName, out var branch) && memberNames.Contains(branch.FromPath) && memberNames.Contains(branch.ToPath)) + { + admittances[branch.Name] = PrimitiveBranchAdmittance(source, patch, name, branch, parameters, options, keepAlive); + } + } + + var (lips, glottalPressures) = members.Count == 0 + ? (new Dictionary(), new Dictionary()) + : EmitVocalLadder(source, patch, network, name, members, inputs, injections, admittances, parameters, warnings); + + foreach (var member in members) + { + var path = member.Path; + var representativeArea = $"{member.Prefix}_area"; + var delay = $"{member.Prefix}_delay"; + var incoming = $"{member.Prefix}_incoming_wave"; + var wave = $"{member.Prefix}_wave"; + var energyIn = $"{member.Prefix}_energy_in"; + var energyOut = $"{member.Prefix}_energy_out"; + var passivity = $"{member.Prefix}_passivity_ratio"; + var meanArea = $"({string.Join(" + ", Enumerable.Range(0, member.Cells).Select(member.A))}) / {member.Cells}.0"; + source.AppendLine($" {representativeArea} = {ProbeSignal(options, $"/debug/{name}/path/{path.Name}/area", meanArea, 0, 8)};"); + source.AppendLine($" {delay} = {ProbeSignal(options, $"/debug/{name}/path/{path.Name}/delay", $"{member.Cells}.0", 0, member.Cells)};"); + source.AppendLine($" {incoming} = {ProbeSignal(options, $"/debug/{name}/path/{path.Name}/incoming_wave", inputs[path.Name], -4, 4)};"); + source.AppendLine($" {wave} = {ProbeSignal(options, $"/debug/{name}/path/{path.Name}/outgoing_wave", lips[path.Name], -4, 4)};"); source.AppendLine($" {energyIn} = {ProbeSignal(options, $"/debug/{name}/path/{path.Name}/energy_in", $"{representativeArea} * pow({incoming}, 2.0)", 0, 8)};"); source.AppendLine($" {energyOut} = {ProbeSignal(options, $"/debug/{name}/path/{path.Name}/energy_out", $"{representativeArea} * pow({wave}, 2.0)", 0, 8)};"); source.AppendLine($" {passivity} = {ProbeSignal(options, $"/debug/{name}/path/{path.Name}/passivity_ratio", $"{energyOut} / max(0.000001, {energyIn})", 0, 2)};"); - keepAlive.Add(input); + keepAlive.Add(representativeArea); + keepAlive.Add(delay); keepAlive.Add(incoming); keepAlive.Add(wave); keepAlive.Add(energyIn); @@ -650,6 +706,17 @@ public static class FaustEmitter radiated.Add(PrimitiveRadiationExpression(source, patch, name, load, wave, parameters, options, keepAlive)); } } + + if (glottalPressures.TryGetValue(path.Name, out var glottalPressure)) + { + // Tract wall radiation: the pressure inside the tract, low-passed, leaves through the cheeks and neck whether or not the mouth + // is open. It is the only sound a closed mouth makes (the voice bar of a voiced stop) and sits far under the mouth's output + // in a vowel. + var wall = $"{member.Prefix}_wall_radiation"; + source.AppendLine($" {wall} = {ProbeSignal(options, $"/debug/{name}/path/{path.Name}/wall_radiation", $"(({glottalPressure}) : fi.highpass(2, {WallRadiationHighpassHz}) : fi.lowpass(2, {WallRadiationLowpassHz})) * {WallRadiationGain}", -4, 4)};"); + keepAlive.Add(wall); + radiated.Add(wall); + } } var output = radiated.Count == 0 ? "0.0" : string.Join(" + ", radiated); @@ -662,32 +729,6 @@ public static class FaustEmitter return $"{name}_primitive_radiated"; } - private static string PrimitiveAreaExpression(SynthPatch patch, ParameterMap parameters, AreaFunction area, int areaIndex) - { - var baseDiameter = area.Shape.AverageDiameter(0, 1); - if (area.Deformation is null) - { - return F(baseDiameter * baseDiameter); - } - - var areaPath = $"/vocal/areas/{areaIndex}"; - var tongue = parameters.Expression(OwnerField(areaPath, "area/tongue_diameter"), area.Deformation.TongueDiameter); - var constriction = parameters.Expression(OwnerField(areaPath, "area/constriction_diameter"), area.Deformation.ConstrictionDiameter); - var lip = parameters.Expression(OwnerField(areaPath, "area/lip_opening"), area.Deformation.LipOpening); - return $"pow(max(0.0001, ({F(baseDiameter)} + ({tongue}) * 0.10 + ({lip}) * 0.08 + min(0.0, ({constriction}) - 1.0) * 0.35)), 2.0)"; - } - - private static string PrimitiveDelayExpression(WaveguidePath path, int maxDelay, string delay) => - path.DelayStrategy switch - { - WaveClockDelayStrategy.UnitGrid => $"de.delay({maxDelay}, int(max(1.0, {delay})))", - WaveClockDelayStrategy.HalfSampleGrid => $"de.fdelay({maxDelay}, round(max(0.5, {delay}) * 2.0) * 0.5)", - WaveClockDelayStrategy.FractionalLagrange => $"de.fdelayltv({Math.Clamp(path.DelayOrder, 1, 5)}, {maxDelay}, max({F((Math.Clamp(path.DelayOrder, 1, 5) - 1) * 0.5f)}, {delay}))", - WaveClockDelayStrategy.FractionalThiran => $"de.fdelay{Math.Clamp(path.DelayOrder, 1, 4)}a({maxDelay}, max({F(Math.Clamp(path.DelayOrder, 1, 4) - 0.5f)}, {delay}))", - WaveClockDelayStrategy.CrossfadedVariable => $"de.fdelayltv({Math.Clamp(path.DelayOrder, 1, 5)}, {maxDelay}, max({F((Math.Clamp(path.DelayOrder, 1, 5) - 1) * 0.5f)}, {delay}))", - _ => $"de.fdelay({maxDelay}, max(0.5, {delay}))" - }; - private static string PrimitiveSourceExpression( StringBuilder source, SynthPatch patch, @@ -706,19 +747,33 @@ public static class FaustEmitter var opening = parameters.Expression(OwnerField(owner, "opening"), port.Opening); var impedance = parameters.Expression(OwnerField(owner, "impedance"), port.Impedance); var flowScale = parameters.Expression(OwnerField(owner, "flow_scale"), port.FlowScale); + // f0 is the voice frequency times the source's ratio (the stress cue), smoothed over 10 ms so a step in the ratio is a glide. + // Its own 10 ms smoother also starts at its input: from 0 it glided the source's pitch up from nothing at the start of every word + // (the control feeding it used to hide that under its own 100 ms climb) and rounded to a value up to ~4e-5 off its input. + var pitchRatio = $"smoo_from(ba.tau2pole(0.01), {parameters.Expression(OwnerField(owner, "pitch_ratio"), port.PitchRatio)})"; var load = $"{voiceName}_primitive_source_{safe}_load_pressure"; var voiced = $"{voiceName}_primitive_source_{safe}_voiced"; var flow = $"{voiceName}_primitive_source_{safe}_flow"; source.AppendLine($" {load} = {ProbeSignal(options, $"/debug/{voiceName}/source/{port.Name}/load_pressure", $"({impedance}) * ({pressure})", 0, 2)};"); - source.AppendLine($" {voiced} = os.osc({frequency}) * clip01({opening}) * clip01(0.25 + {tension});"); - source.AppendLine($" {flow} = {ProbeSignal(options, $"/debug/{voiceName}/source/{port.Name}/flow", $"({flowScale}) * (({voiced}) * ma.tanh((({pressure}) * (0.5 + {tension})) / max(0.05, {impedance})) + 0.08 * no.noise * {parameters.Expression(OwnerField(owner, "noise"), port.Noise)})", -2, 2)};"); + // Harmonic glottal source: a band-limited saw through a one-pole lowpass. Tilt 0 is the brightest source and 1 the + // steepest rolloff; the cutoff sweeps five octaves, 6400 Hz down to 200 Hz, and sits below Nyquist. + var cutoff = $"min(0.45 * ma.SR, 200.0 * pow(2.0, (1.0 - clip01({parameters.Expression(OwnerField(owner, "tilt"), port.Tilt)})) * 5.0))"; + var wave = port.Pulse == GlottalPulse.Rosenberg ? RosenbergPulse($"(({frequency}) * {pitchRatio})") : $"os.sawtooth(({frequency}) * {pitchRatio})"; + source.AppendLine($" {voiced} = ({wave} : fi.lowpass(1, {cutoff})) * clip01({opening}) * clip01(0.25 + {tension});"); + source.AppendLine($" {flow} = {ProbeSignal(options, $"/debug/{voiceName}/source/{port.Name}/flow", $"({flowScale}) * (({voiced}) * ma.tanh((({pressure}) * (0.5 + {tension})) / max(0.05, {impedance})) + 0.08 * {(port.NoiseHighPassHz > 0 ? $"(no.noise : fi.highpass(2, {port.NoiseHighPassHz.ToString("0.###", CultureInfo.InvariantCulture)}))" : "no.noise")} * {parameters.Expression(OwnerField(owner, "noise"), port.Noise)})", -2, 2)};"); keepAlive.Add(load); keepAlive.Add(voiced); keepAlive.Add(flow); return flow; } - private static string PrimitiveContactExpression( + // Rosenberg C flow pulse of one period (phase p of 0..1): opening 0.5 (1 - cos(pi p / 0.4)) for p < 0.4, return cos(pi/2 (p - 0.4) / 0.16) + // for the next 0.16, closed after. Its mean is 0.4 / 2 + 0.16 * 2 / pi = 0.301859 and its rms about that mean is 0.372667; it is made zero-mean + // and scaled by 0.57735 / 0.372667 = 1.549 to the rms of os.sawtooth (1 / sqrt 3), so a swap of the pulse leaves the SOURCE level where it was (the rendered level moves: the pulse has no strong upper harmonics for the tract to pass). + public static string RosenbergPulse(string frequency) => + $"(((os.lf_sawpos({frequency}) : (\\(p).(select2(p < 0.4, select2(p < 0.56, 0.0, cos(ma.PI * 0.5 * (p - 0.4) / 0.16)), 0.5 * (1.0 - cos(ma.PI * p / 0.4)))))) - 0.301859) * 1.549)"; + + private static (string Released, string Burst) PrimitiveContactExpression( StringBuilder source, SynthPatch patch, string voiceName, @@ -733,13 +788,37 @@ public static class FaustEmitter var opening = $"clip01({parameters.Expression(OwnerField(owner, "opening"), contact.Opening)})"; var resistance = parameters.Expression(OwnerField(owner, "resistance"), contact.Resistance); var stored = parameters.Expression(OwnerField(owner, "stored_pressure"), contact.StoredPressure); + var releaseGain = parameters.Expression(OwnerField(owner, "release_flow"), contact.ReleaseFlow); var released = $"{voiceName}_primitive_contact_{safe}_released_flow"; var reservoir = $"{voiceName}_primitive_contact_{safe}_reservoir"; + var burst = $"{voiceName}_primitive_contact_{safe}_burst"; source.AppendLine($" {reservoir} = {ProbeSignal(options, $"/debug/{voiceName}/contact/{contact.Name}/reservoir", $"(({stored}) + (1.0 - {opening}) * {resistance})", 0, 2)};"); source.AppendLine($" {released} = {ProbeSignal(options, $"/debug/{voiceName}/contact/{contact.Name}/released_flow", $"({reservoir}) * {opening} * (1.0 - min(0.95, {resistance} * 0.5))", -2, 2)};"); + // Release burst: noise whose amplitude is the stored pressure through the opening area as it passes between shut and open + // (4 * o * (1 - o) peaks at half open and vanishes at both ends), so a shut or fully open contact is silent. + source.AppendLine($" {burst} = {ProbeSignal(options, $"/debug/{voiceName}/contact/{contact.Name}/burst", $"no.noise * ({releaseGain}) * ({stored}) * 4.0 * {opening} * (1.0 - {opening})", -2, 2)};"); keepAlive.Add(reservoir); + keepAlive.Add(burst); keepAlive.Add(released); - return released; + return (released, burst); + } + + // Open fraction of a branch port: the ladder reads it as the velum area when the branch joins two paths of the network, + // otherwise it scales the flow term PrimitiveBranchExpression exchanges. + private static string PrimitiveBranchAdmittance( + StringBuilder source, + SynthPatch patch, + string voiceName, + BranchPort branch, + ParameterMap parameters, + FaustExportOptions options, + List keepAlive) + { + var owner = $"/vocal/branches/{BranchPortIndex(patch, branch)}"; + var admittance = $"{voiceName}_primitive_branch_{SafeIdentifier(branch.Name)}_admittance"; + source.AppendLine($" {admittance} = {ProbeSignal(options, $"/debug/{voiceName}/branch/{branch.Name}/admittance", $"clip01({parameters.Expression(OwnerField(owner, "opening"), branch.Opening)}) * clip01({parameters.Expression(OwnerField(owner, "coupling"), branch.Coupling)})", 0, 1)};"); + keepAlive.Add(admittance); + return admittance; } private static string PrimitiveBranchExpression( @@ -751,18 +830,28 @@ public static class FaustEmitter FaustExportOptions options, List keepAlive) { - var index = BranchPortIndex(patch, branch); - var owner = $"/vocal/branches/{index}"; - var safe = SafeIdentifier(branch.Name); - var admittance = $"{voiceName}_primitive_branch_{safe}_admittance"; - var exchanged = $"{voiceName}_primitive_branch_{safe}_exchanged_flow"; - source.AppendLine($" {admittance} = {ProbeSignal(options, $"/debug/{voiceName}/branch/{branch.Name}/admittance", $"clip01({parameters.Expression(OwnerField(owner, "opening"), branch.Opening)}) * clip01({parameters.Expression(OwnerField(owner, "coupling"), branch.Coupling)})", 0, 1)};"); + var admittance = PrimitiveBranchAdmittance(source, patch, voiceName, branch, parameters, options, keepAlive); + var exchanged = $"{voiceName}_primitive_branch_{SafeIdentifier(branch.Name)}_exchanged_flow"; source.AppendLine($" {exchanged} = {ProbeSignal(options, $"/debug/{voiceName}/branch/{branch.Name}/exchanged_flow", $"0.0 - {admittance} * 0.05", -1, 1)};"); - keepAlive.Add(admittance); keepAlive.Add(exchanged); return exchanged; } + // DC and rumble block on the ladder's lip flow, below the lowest authored vowel F1 (about 270 Hz). It does not depend on the + // aperture: the ladder's lip cell already carries the lip area, and a corner that rose with aperture (40 + 500 Hz) sat above + // /u/'s F1 and put a 450 Hz hump in its envelope. The aperture stays the gain consonant closures use. + private const string RadiationHighpassHz = "200.0"; + + // Tract wall radiation (the voice bar). Corners: skin and soft tissue pass only the low end of the voice (below about 200-300 Hz), and + // a second-order highpass at 90 Hz keeps the slow pressure drift of a shutting tract out (a 40 Hz first-order corner left it in). The gain + // is a DESIGN VALUE set so a word-initial voiced closure reads 30 dB under the following vowel (/p/ 41 dB); the wall term moves a + // vowel's f0 line by under 1 dB and its 200 and 300 Hz harmonics by up to 1.5 dB on one open vowel (no primary level retrieved; + // docs/research/articulatory-vocal-tract-sources.md). Both corners are second-order sections: the closed-tract bar is about 25 dB down + // above 300 Hz, not cut off there. + private const string WallRadiationHighpassHz = "90.0"; + private const string WallRadiationLowpassHz = "300.0"; + private const string WallRadiationGain = "0.7"; + private static string PrimitiveRadiationExpression( StringBuilder source, SynthPatch patch, @@ -785,7 +874,7 @@ public static class FaustEmitter source.AppendLine($" {voiceName}_primitive_radiation_{safe}_reflection = {ProbeSignal(options, $"/debug/{voiceName}/radiation/{load.Name}/reflection", reflection, -1, 1)};"); source.AppendLine($" {boundary} = {ProbeSignal(options, $"/debug/{voiceName}/radiation/{load.Name}/boundary_flow", $"({wave}) * {aperture} / max(0.05, {impedance})", -4, 4)};"); source.AppendLine($" {flow} = {ProbeSignal(options, $"/debug/{voiceName}/radiation/{load.Name}/flow", boundary, -4, 4)};"); - source.AppendLine($" {output} = {ProbeSignal(options, $"/debug/{voiceName}/radiation/{load.Name}/output", $"{flow} : fi.highpass(1, 40.0 + 500.0 * {aperture})", -4, 4)};"); + source.AppendLine($" {output} = {ProbeSignal(options, $"/debug/{voiceName}/radiation/{load.Name}/output", $"{flow} : fi.highpass(1, {RadiationHighpassHz})", -4, 4)};"); keepAlive.Add($"{voiceName}_primitive_radiation_{safe}_reflection"); keepAlive.Add(boundary); keepAlive.Add(flow); @@ -1622,28 +1711,72 @@ public static class FaustEmitter return "1.0"; } - var position = Math.Clamp(terminal.Position, 0, 1); - var restDiameter = path.AreaFunction.DiameterAt(position); - if (path.AreaControl is not { } area) + return DeformedAreaExpression( + parameters, + path.AreaFunction, + path.AreaControl, + $"/acoustic/paths/{AcousticPathIndex(patch, path)}/area", + Math.Clamp(terminal.Position, 0, 1)); + } + + private static string SegmentAreaExpression( + SynthPatch patch, + ParameterMap parameters, + AcousticGraphSegment segment) => + DeformedAreaExpression( + parameters, + segment.Path.AreaFunction, + segment.Path.AreaControl, + $"/acoustic/paths/{AcousticPathIndex(patch, segment.Path)}/area", + (segment.StartPosition + segment.EndPosition) * 0.5f); + + /// + /// The one owner of the tract area model: cross-section area (cm^2) of a tract at a normalized position, as a Faust + /// expression over the control surfaces under . Acoustic graph terminals and segments and the + /// vocal ladder's cells all read their area here. The base diameter is either the rest shape deformed by the Gaussian + /// tongue and lip terms, or, when are given, the log-area blend of the measured vowel shapes + /// weighted by the vowel_* surfaces (no tongue term; lip_opening is then a cap on the lip cells). The constriction term + /// applies to either base. + /// + private static string DeformedAreaExpression( + ParameterMap parameters, + TractAreaFunction shape, + AcousticAreaControl? area, + string areaPath, + float position, + IReadOnlyList? vowels = null) + { + var restDiameter = shape.DiameterAt(position); + if (area is null) { return F(Math.Max(0.000001f, restDiameter * restDiameter)); } - var pathIndex = AcousticPathIndex(patch, path); - var areaPath = $"/acoustic/paths/{pathIndex}/area"; - var sections = Math.Max(2, path.AreaFunction.Sections); + var sections = Math.Max(2, shape.Sections); var index = F(position * (sections - 1)); - var tongueIndex = ScaledAreaIndex(parameters.Expression(OwnerField(areaPath, "tongue_index"), area.TongueIndex), area.IndexScale); - var tongueDiameter = parameters.Expression(OwnerField(areaPath, "tongue_diameter"), area.TongueDiameter); - var tongueWidth = $"max(0.001, {F(area.TongueWidth)} * {F(sections)})"; var constrictionIndex = ScaledAreaIndex(parameters.Expression(OwnerField(areaPath, "constriction_index"), area.ConstrictionIndex), area.IndexScale); var constrictionDiameter = parameters.Expression(OwnerField(areaPath, "constriction_diameter"), area.ConstrictionDiameter); var constrictionWidth = $"max(0.001, {F(area.ConstrictionWidth)} * {F(sections)})"; var lipOpening = parameters.Expression(OwnerField(areaPath, "lip_opening"), area.LipOpening); var lipWeight = MathF.Exp(0 - MathF.Pow((1 - position) / Math.Max(0.001f, area.LipWidth), 2)); - var tongueWeight = $"exp(0.0 - pow(({index} - ({tongueIndex})) / ({tongueWidth}), 2.0))"; var constrictionWeight = $"exp(0.0 - pow(({index} - ({constrictionIndex})) / ({constrictionWidth}), 2.0))"; - var baseDiameter = $"({F(restDiameter)} + (({tongueDiameter}) - {F(restDiameter)}) * ({tongueWeight}) + (({lipOpening}) - {F(restDiameter)}) * {F(lipWeight)})"; + string baseDiameter; + if (vowels is { Count: > 0 }) + { + // The cap applies to the cells inside the lip Gaussian's half-power region; elsewhere it is unbounded. + var blended = VowelBlendDiameter(parameters, vowels, areaPath, position); + baseDiameter = lipWeight >= 0.5f ? $"min({blended}, ({lipOpening}))" : blended; + baseDiameter = $"({baseDiameter})"; + } + else + { + var tongueIndex = ScaledAreaIndex(parameters.Expression(OwnerField(areaPath, "tongue_index"), area.TongueIndex), area.IndexScale); + var tongueDiameter = parameters.Expression(OwnerField(areaPath, "tongue_diameter"), area.TongueDiameter); + var tongueWidth = $"max(0.001, {F(area.TongueWidth)} * {F(sections)})"; + var tongueWeight = $"exp(0.0 - pow(({index} - ({tongueIndex})) / ({tongueWidth}), 2.0))"; + baseDiameter = $"({F(restDiameter)} + (({tongueDiameter}) - {F(restDiameter)}) * ({tongueWeight}) + (({lipOpening}) - {F(restDiameter)}) * {F(lipWeight)})"; + } + var diameter = $"max(0.0, min({baseDiameter}, ({constrictionDiameter}) + max(0.0, ({baseDiameter}) - ({constrictionDiameter})) * (1.0 - ({constrictionWeight}))))"; return $"(({diameter}) * ({diameter}))"; @@ -1651,38 +1784,17 @@ public static class FaustEmitter scale == 1 ? expression : $"(({expression}) * {F(scale)})"; } - private static string SegmentAreaExpression( - SynthPatch patch, - ParameterMap parameters, - AcousticGraphSegment segment) + // Diameter of the blend exp(sum_k w_k ln A_k / max(1e-3, sum_k w_k)) of the vowel shapes at one position, A_k read at that position + // by linear interpolation in area. Weights below zero count as zero; when they sum to under 1e-3 the blend is the geometric mean of + // the shapes (select2 picks it), so zero weights are a defined shape rather than NaN. + private static string VowelBlendDiameter(ParameterMap parameters, IReadOnlyList vowels, string areaPath, float position) { - var position = (segment.StartPosition + segment.EndPosition) * 0.5f; - var restDiameter = segment.Path.AreaFunction.DiameterAt(position); - if (segment.Path.AreaControl is not { } area) - { - return F(Math.Max(0.000001f, restDiameter * restDiameter)); - } - - var pathIndex = AcousticPathIndex(patch, segment.Path); - var areaPath = $"/acoustic/paths/{pathIndex}/area"; - var sections = Math.Max(2, segment.Path.AreaFunction.Sections); - var index = F(position * (sections - 1)); - var tongueIndex = ScaledAreaIndex(parameters.Expression(OwnerField(areaPath, "tongue_index"), area.TongueIndex), area.IndexScale); - var tongueDiameter = parameters.Expression(OwnerField(areaPath, "tongue_diameter"), area.TongueDiameter); - var tongueWidth = $"max(0.001, {F(area.TongueWidth)} * {F(sections)})"; - var constrictionIndex = ScaledAreaIndex(parameters.Expression(OwnerField(areaPath, "constriction_index"), area.ConstrictionIndex), area.IndexScale); - var constrictionDiameter = parameters.Expression(OwnerField(areaPath, "constriction_diameter"), area.ConstrictionDiameter); - var constrictionWidth = $"max(0.001, {F(area.ConstrictionWidth)} * {F(sections)})"; - var lipOpening = parameters.Expression(OwnerField(areaPath, "lip_opening"), area.LipOpening); - var lipWeight = MathF.Exp(0 - MathF.Pow((1 - position) / Math.Max(0.001f, area.LipWidth), 2)); - var tongueWeight = $"exp(0.0 - pow(({index} - ({tongueIndex})) / ({tongueWidth}), 2.0))"; - var constrictionWeight = $"exp(0.0 - pow(({index} - ({constrictionIndex})) / ({constrictionWidth}), 2.0))"; - var baseDiameter = $"({F(restDiameter)} + (({tongueDiameter}) - {F(restDiameter)}) * ({tongueWeight}) + (({lipOpening}) - {F(restDiameter)}) * {F(lipWeight)})"; - var diameter = $"max(0.0, min({baseDiameter}, ({constrictionDiameter}) + max(0.0, ({baseDiameter}) - ({constrictionDiameter})) * (1.0 - ({constrictionWeight}))))"; - return $"(({diameter}) * ({diameter}))"; - - static string ScaledAreaIndex(string expression, float scale) => - scale == 1 ? expression : $"(({expression}) * {F(scale)})"; + var logs = vowels.Select(vowel => MathF.Log(Math.Max(0.000001f, StoryVowelAreas.AreaAt(vowel.Areas, position)))).ToArray(); + var weights = vowels.Select(vowel => $"max(0.0, {parameters.Expression(OwnerField(areaPath, StoryVowelAreas.SurfaceField(vowel.Name)), 0)})").ToArray(); + var total = $"({string.Join(" + ", weights)})"; + var numerator = $"({string.Join(" + ", weights.Select((weight, k) => $"{weight} * ({F(logs[k])})"))})"; + var geometricMean = F(MathF.Exp(logs.Average())); + return $"sqrt(select2({total} < 0.001, exp({numerator} / max(0.001, {total})), {geometricMean}))"; } private static WaveClockPolicy ResolveWaveClock( diff --git a/src/AquaSynth.Core/IpaGestureExperiment.cs b/src/AquaSynth.Core/IpaGestureExperiment.cs index aead558..867e399 100644 --- a/src/AquaSynth.Core/IpaGestureExperiment.cs +++ b/src/AquaSynth.Core/IpaGestureExperiment.cs @@ -8,7 +8,8 @@ public sealed record IpaGestureExperimentTarget( string Ipa, string Descriptor, float DurationSeconds = 0.18f, - string Notes = ""); + string Notes = "", + float PitchRatio = 1f); public sealed record IpaGestureExperimentVariant( string Id, @@ -91,7 +92,8 @@ public static class IpaGestureExperiment string roundId, IEnumerable targets, IEnumerable variants, - int timelineBlocks = 12) + int timelineBlocks = 12, + float sampleRate = 48000) { ArgumentException.ThrowIfNullOrWhiteSpace(rootDirectory); ArgumentException.ThrowIfNullOrWhiteSpace(roundId); @@ -108,7 +110,7 @@ public static class IpaGestureExperiment throw new ArgumentException("At least one experiment variant is required.", nameof(variants)); } - var roundDirectory = Path.Combine(rootDirectory, SafeFileName(roundId)); + var roundDirectory = Path.Combine(rootDirectory, StoreFileName.From(roundId)); var candidateDirectory = Path.Combine(roundDirectory, "candidates"); var timelineDirectory = Path.Combine(roundDirectory, "timelines"); Directory.CreateDirectory(candidateDirectory); @@ -122,13 +124,13 @@ public static class IpaGestureExperiment { foreach (var variant in variantList) { - var candidateId = SafeFileName($"{target.Id}-{variant.Id}"); + var candidateId = StoreFileName.From($"{StoreFileName.From(target.Id)}/{StoreFileName.From(variant.Id)}"); var script = BuildCandidateScript(target, variant); var scriptPath = Path.Combine(candidateDirectory, $"{candidateId}.aqua"); File.WriteAllText(scriptPath, script, Encoding.UTF8); var patch = PatchScript.Parse(script); - var timeline = ProbeTimelineReport.Build(patch, NetworkName, Math.Max(1, timelineBlocks)); + var timeline = ProbeTimelineReport.Build(patch, NetworkName, Math.Max(1, timelineBlocks), sampleRate); var timelinePath = Path.Combine(timelineDirectory, $"{candidateId}.csv"); File.WriteAllText(timelinePath, ProbeTimelineReport.ToCsv(timeline), Encoding.UTF8); @@ -196,27 +198,89 @@ public static class IpaGestureExperiment var duration = Math.Clamp(target.DurationSeconds * variant.DurationScale, 0.03f, 1.5f); var intensity = Math.Clamp(variant.IntensityScale, 0.05f, 2f); - var sustain = Math.Clamp(duration + 0.12f, 0.15f, 2f); - var descriptor = target.Descriptor.Replace(' ', '_'); - var frequency = DescriptorTokens(target.Descriptor).Contains("vowel") ? 150 : 135; + return Scaffold( + [GestureLine(target, 0, duration, intensity)], + Math.Clamp(duration + 0.12f, 0.15f, 2f), + DescriptorTokens(target.Descriptor).Contains("vowel") ? 150 : 135); + } + + /// + /// One word on the same scaffold: the targets run back to back, each at its own intensity (a stress mark is a higher + /// intensity on the stressed vowel). An empty descriptor leaves it to the DSL's own IPA table. Pitch stays flat at ; no contour is authored. + /// + public static string BuildWordScript(IReadOnlyList<(IpaGestureExperimentTarget Target, float Intensity)> phones, float frequency = 140, float tailSeconds = 0.12f) + { + if (phones.Count == 0) throw new ArgumentException("a word needs at least one phone", nameof(phones)); + var lines = new List(); + var start = 0f; + foreach (var (target, intensity) in phones) + { + ArgumentException.ThrowIfNullOrWhiteSpace(target.Id); + ArgumentException.ThrowIfNullOrWhiteSpace(target.Ipa); + var duration = Math.Clamp(target.DurationSeconds, 0.03f, 1.5f); + lines.Add(GestureLine(target, start, duration, Math.Clamp(intensity, 0.05f, 2f))); + start += duration; + } + + return Scaffold(lines, Math.Clamp(start + tailSeconds, 0.15f, 4f), frequency, phones.Any(phone => NeedsSideStub(phone.Target))); + } + + // A lateral is the one gesture that opens the side stub (PatchScript.SideStubBranch); a word without one is scaffolded without it, byte for byte. + private static bool NeedsSideStub(IpaGestureExperimentTarget target) => + (target.Descriptor.Length > 0 ? target.Descriptor : PatchScript.TryIpaDescriptor(target.Ipa, out var known) ? known : "") + .Contains("lateral", StringComparison.OrdinalIgnoreCase); + + // The sublingual cavity beside a lateral's midline contact: a closed side branch of the oral tract at the alveolar ridge. Its quarter-wave + // antiresonance (c / 4L, lowered by the junction and the orifice) is the lateral's dip; the length is a DESIGN VALUE placing it between 2 and 3 kHz + // (no primary dimension retrieved). Cross-section 1 cm^2, four cells of the stub's own morphology; the first cell is the orifice, the branch + // opening times the port maximum (NasalTractAreas). The end is closed (reflection +1, aperture 0: it radiates nothing). + private const string SideStubLengthCentimeters = "3"; + private const string SideStubAreas = "1,1,1,1"; + + private static string SideStubDeclarations(bool sideStub) + { + if (!sideStub) return ""; + PatchScript.TryPlaceDistanceCentimeters("alveolar", out var ridge); + var at = 1f - ridge / StoryVowelAreas.MeanLengthCentimeters; + return $""" + morphology name=side_stub length_cm={SideStubLengthCentimeters} areas={SideStubAreas} + waveguide_path name=side_stub_path morphology=side_stub speed={TractSpeedMetersPerSecond} strategy=thiran order=1 max_delay=4096 loss=.98 + branch_port name={PatchScript.SideStubBranch} from=oral_path from_position={Format(at)} to=side_stub_path opening=0 coupling=1 + radiation_load name=side_stub_end path=side_stub_path kind=lip position=1 aperture=0 reflection=1 impedance=.28 - return $$""" - patch gain=.45 - morphology name=oral length_cm=17 diameters=.55,.75,1.05,1.45,1.25,.8 tongue_index=3 tongue_diameter=1.4 constriction_index=4 constriction_diameter=1 lip_opening=1.2 - morphology name=nasal length_cm=12 diameters=.04,.28,.52,.72 - waveguide_path name=oral_path morphology=oral strategy=thiran order=1 max_delay=4096 loss=.998 - waveguide_path name=nasal_path morphology=nasal strategy=thiran order=1 max_delay=4096 loss=.997 - source_port name=folds path=oral_path kind=glottal position=0 pressure=.66 tension=.54 opening=.42 noise=.04 impedance=.32 flow_scale=.12 - branch_port name=velopharynx from=oral_path from_position=.45 to=nasal_path opening=.01 coupling=1 - constriction_contact name=contact path=oral_path position=.92 opening=.5 resistance=.45 stored_pressure=.12 - radiation_load name=mouth path=oral_path kind=lip position=1 aperture=.8 reflection=-.82 impedance=.28 - probe_timeline name=flow network=voice blocks=12 block_size=64 - vocal_network name=voice paths=oral_path,nasal_path sources=folds contacts=contact branches=velopharynx radiation=mouth probe=flow - phoneme_gesture name={{SafeDslName(target.Id)}} ipa={{target.Ipa}} descriptor={{descriptor}} start=0 dur={{Format(duration)}} intensity={{Format(intensity)}} - vocal network=voice freq={{Format(frequency)}} gain=.7 sustain={{Format(sustain)}} """; } + private static string GestureLine(IpaGestureExperimentTarget target, float start, float duration, float intensity) => + $"phoneme_gesture name={SafeDslName(target.Id)} ipa={target.Ipa} {(target.Descriptor.Length == 0 ? "" : $"descriptor={target.Descriptor.Replace(' ', '_')} ")}start={Format(start)} dur={Format(duration)} intensity={Format(intensity)}{(target.PitchRatio == 1f ? "" : $" pitch_ratio={Format(target.PitchRatio)}")}"; + + // One cell per sample at 48 kHz: 17.52 cm * 48000 / 350 m/s = 24.03 cells, a ladder 17.5 cm long (the mean of Story's vocal tract lengths). + private const int TractSpeedMetersPerSecond = 350; + + // Per-cell pass factor of the nasal path. The nasal cavity is the most heavily damped part of the tract (soft walls, turbinates, the paranasal + // sinuses), so a nasal murmur has broad formants and sits well under its vowels. A factor of .975 per 0.73 cm cell is a bandwidth of about + // 380 Hz, a DESIGN VALUE set from the murmur level: with the junction on its own interface and .997 (the oral tract's .998 less a little) the + // murmur read -4.2 dB (/m/) and -2.2 dB (/n/) re the flanking vowels; the target is 8 to 16 dB under (no primary level or bandwidth retrieved). + private const string NasalPathLoss = ".975"; + + private static string Scaffold(IReadOnlyList gestureLines, float sustain, float frequency, bool sideStub = false) => $$""" + patch gain=.45 + morphology name=oral length_cm={{Format(StoryVowelAreas.MeanLengthCentimeters)}} diameters=.55,.75,1.05,1.45,1.25,.8 tongue_index=3 tongue_diameter=1.4 constriction_index=4 constriction_diameter=1 constriction_width=.03 lip_opening=4 + vowel_set area_function=oral source={{StoryVowelAreas.Source}} + morphology name=nasal length_cm={{Format(NasalTractAreas.LengthCentimeters)}} areas={{NasalTractAreas.AreasList()}} + waveguide_path name=oral_path morphology=oral speed={{TractSpeedMetersPerSecond}} strategy=thiran order=1 max_delay=4096 loss=.998 + waveguide_path name=nasal_path morphology=nasal speed={{TractSpeedMetersPerSecond}} strategy=thiran order=1 max_delay=4096 loss={{NasalPathLoss}} + source_port name=folds path=oral_path kind=glottal position=0 pressure=.66 tension=.54 opening=.42 noise=.04 impedance=.32 flow_scale=.12 + branch_port name=velopharynx from=oral_path from_position=.45 to=nasal_path opening=0 coupling=1 + constriction_contact name=contact path=oral_path position=.92 opening=1 resistance=.45 stored_pressure=.12 + radiation_load name=mouth path=oral_path kind=lip position=1 aperture=.8 reflection=-1 impedance=.28 + radiation_load name=nostril path=nasal_path kind=lip position=1 aperture=.8 reflection=-1 impedance=.28 + probe_timeline name=flow network=voice blocks=12 block_size=64 + {{SideStubDeclarations(sideStub)}}vocal_network name=voice paths=oral_path,nasal_path{{(sideStub ? ",side_stub_path" : "")}} sources=folds contacts=contact branches=velopharynx{{(sideStub ? "," + PatchScript.SideStubBranch : "")}} radiation=mouth,nostril{{(sideStub ? ",side_stub_end" : "")}} probe=flow + {{string.Join("\n", gestureLines)}} + vocal network=voice freq={{Format(frequency)}} gain=.7 attack=.012 sustain={{Format(sustain)}} + """; + private static IReadOnlyList SummarizeMetrics(IReadOnlyList metrics) => metrics .Where(metric => metric.Layer.Equals("gesture", StringComparison.OrdinalIgnoreCase)) @@ -260,7 +324,7 @@ public static class IpaGestureExperiment IReadOnlyList timeline) { var tokens = DescriptorTokens(target.Descriptor); - var expected = ExpectedSurfaces(tokens); + var expected = ExpectedSurfaces(tokens, target.Descriptor); var touched = patch.ControlSplines .Where(spline => spline.Enabled) .Select(spline => spline.SurfacePath) @@ -269,7 +333,7 @@ public static class IpaGestureExperiment var coverage = expected.Length == 0 ? 1 : expected.Count(surface => touched.Contains(surface)) / (float)expected.Length; - var direction = DirectionScore(tokens, patch.ControlSplines); + var direction = DirectionScore(tokens, target.Descriptor, patch.ControlSplines); var contour = ContourTimingScore(patch.ControlSplines); var primitive = PrimitiveTimelineScore(tokens, timeline); var score = 0.25f * coverage + 0.25f * direction + 0.20f * contour + 0.20f * primitive; @@ -287,7 +351,7 @@ public static class IpaGestureExperiment new(candidate.Id, target.Id, "gesture", name, Math.Clamp(value, 0, 1)); } - private static string[] ExpectedSurfaces(HashSet tokens) + private static string[] ExpectedSurfaces(HashSet tokens, string descriptor) { var surfaces = new List { @@ -300,11 +364,13 @@ public static class IpaGestureExperiment { surfaces.AddRange( [ - "/vocal/areas/0/area/tongue_index", - "/vocal/areas/0/area/tongue_diameter", "/vocal/areas/0/area/lip_opening", "/vocal/radiation/0/aperture" ]); + if (StoryVowelAreas.IdForDescriptor(descriptor) is { } vowelId) + { + surfaces.Add($"/vocal/areas/0/area/{StoryVowelAreas.SurfaceField(vowelId)}"); + } } else { @@ -341,7 +407,7 @@ public static class IpaGestureExperiment return surfaces.Distinct(StringComparer.OrdinalIgnoreCase).ToArray(); } - private static float DirectionScore(HashSet tokens, IReadOnlyList splines) + private static float DirectionScore(HashSet tokens, string descriptor, IReadOnlyList splines) { var checks = new List(); if (tokens.Contains("nasal")) @@ -364,8 +430,11 @@ public static class IpaGestureExperiment if (tokens.Contains("vowel")) { - checks.Add(Exists(splines, "/vocal/areas/0/area/tongue_index")); - checks.Add(Exists(splines, "/vocal/areas/0/area/tongue_diameter")); + if (StoryVowelAreas.IdForDescriptor(descriptor) is { } vowelId) + { + checks.Add(Exists(splines, $"/vocal/areas/0/area/{StoryVowelAreas.SurfaceField(vowelId)}")); + } + checks.Add(Exists(splines, "/vocal/radiation/0/aperture")); } @@ -705,13 +774,6 @@ public static class IpaGestureExperiment private static string SafeDslName(string value) => new(value.Select(character => char.IsLetterOrDigit(character) ? character : '_').ToArray()); - private static string SafeFileName(string value) - { - var invalid = Path.GetInvalidFileNameChars().ToHashSet(); - var safe = new string(value.Select(character => invalid.Contains(character) || char.IsWhiteSpace(character) ? '-' : character).ToArray()); - return safe.Length == 0 ? "candidate" : safe; - } - private static string Format(float value) => value.ToString("0.######", CultureInfo.InvariantCulture); diff --git a/src/AquaSynth.Core/IpaTrialResults.cs b/src/AquaSynth.Core/IpaTrialResults.cs index 7957cc9..589f2c4 100644 --- a/src/AquaSynth.Core/IpaTrialResults.cs +++ b/src/AquaSynth.Core/IpaTrialResults.cs @@ -40,7 +40,8 @@ public sealed record PrimitiveTimelineFact( [property: Key(4)] string Unit, [property: Key(5)] int BlockStart, [property: Key(6)] int BlockEnd, - [property: Key(7)] string Summary); + [property: Key(7)] string Summary, + [property: Key(8), CultElementId] string ElementId = ""); [CultDocument("aquasynth.song_challenge_evidence", "aquasynth.song_challenge_evidence.v1")] [MessagePackObject] diff --git a/src/AquaSynth.Core/Model.cs b/src/AquaSynth.Core/Model.cs index 82e4e79..ba38129 100644 --- a/src/AquaSynth.Core/Model.cs +++ b/src/AquaSynth.Core/Model.cs @@ -443,11 +443,15 @@ public sealed record AcousticPortNetwork( public IReadOnlyList Connections { get; init; } = Connections ?? Array.Empty(); } +/// A named measured tract shape: areas in cm^2 from the glottis to the lips, any number of sections. +public sealed record VowelAreaShape(string Name, IReadOnlyList Areas); + public sealed record AreaFunction( string Name, TractAreaFunction Shape, AcousticAreaControl? Deformation = null, - int EmitSections = 8); + int EmitSections = 8, + IReadOnlyList? Vowels = null); public sealed record WaveguidePath( string Name, @@ -468,7 +472,19 @@ public sealed record SourcePort( float Opening = 0.5f, float Noise = 0.08f, float Impedance = 0.35f, - float FlowScale = 0.02f); + float FlowScale = 0.02f, + float Tilt = 0.5f, + float PitchRatio = 1f, + GlottalPulse Pulse = GlottalPulse.Saw, + float NoiseHighPassHz = 0f); + +// The glottal flow waveform of a primitive source port. Saw is the band-limited saw the tilt lowpass shapes; Rosenberg is the +// Rosenberg C pulse (open phase 0.4 of the period, return phase 0.16), zero-mean and scaled to the saw's rms at the source. The rendered level still differs: the tract passes less of the pulse (-2.5 dB output rms on /ɑmi/, measured -2.54). +public enum GlottalPulse +{ + Saw, + Rosenberg +} public sealed record ConstrictionContact( string Name, diff --git a/src/AquaSynth.Core/NasalTractAreas.cs b/src/AquaSynth.Core/NasalTractAreas.cs new file mode 100644 index 0000000..572fb93 --- /dev/null +++ b/src/AquaSynth.Core/NasalTractAreas.cs @@ -0,0 +1,33 @@ +namespace AquaSynth.Dsl; + +/// +/// The owner of the nasal tract area function and of the velopharyngeal port's maximum area. Data: Birkholz P, 2005, "3D-Artikulatorische +/// Sprachsynthese", dissertation Universitaet Rostock, Table 2.3 (the nasal cavity of one subject, after Dang J, Honda K, 1994, "Morphological +/// and acoustical analysis of the nasal and the paranasal cavities", JASA 96(4):2088-2100): 19 sections of 0.6 cm (11.4 cm), areas in cm^2, +/// velopharyngeal end first, nostrils last. A test compares to the cited values. +/// Not modelled: the paranasal sinus Helmholtz resonators (zeros at 552-1682 Hz in the same source) and the piriform fossae. The table is a +/// one-subject measurement and is unheard here: nothing in this file says how any nasal sounds. +/// +public static class NasalTractAreas +{ + public const string Source = "birkholz2005-table2.3"; + public const float SectionLengthCentimeters = 0.6f; + public const float LengthCentimeters = 11.4f; + + public static IReadOnlyList Areas { get; } = + [ + 1.63f, 2.07f, 2.72f, 3.59f, 4.24f, 3.26f, 3.04f, 3.04f, 2.72f, 2.5f, + 2.39f, 2.39f, 1.85f, 0.76f, 1.41f, 1.74f, 1.30f, 1.74f, 0.76f, + ]; + + /// + /// Maximum cross-section area of the velopharyngeal port in cm^2: the ladder's port area is opening * coupling * this. A DESIGN VALUE: + /// no source for the range of the port area was retrieved (Birkholz's velum port is a controllable area; its range is not in the + /// material read). 0.8 cm^2 is a wide-open port, below the first nasal section (1.63 cm^2) so the port, not the table, is the narrowest + /// first cell. Pink Trombone's earlier constant, a 0.4 cm diameter (0.16 cm^2 area at full opening and coupling), was likewise unsourced. + /// + public const float VelopharyngealPortMaximumAreaSquareCentimeters = 0.8f; + + /// The scaffold's morphology line value for areas=, invariant-culture comma separated. + public static string AreasList() => string.Join(",", Areas.Select(area => area.ToString("0.##", System.Globalization.CultureInfo.InvariantCulture))); +} diff --git a/src/AquaSynth.Core/PatchScript.cs b/src/AquaSynth.Core/PatchScript.cs index 91fafa7..06b0b0b 100644 --- a/src/AquaSynth.Core/PatchScript.cs +++ b/src/AquaSynth.Core/PatchScript.cs @@ -8,6 +8,65 @@ public sealed class PatchScriptException(int line, string message) : Exception($ } public static class PatchScript { + /// The branch port a lateral gesture opens: a closed side stub beside the oral path at the alveolar ridge (the sublingual cavity). + /// A script that gestures a lateral without declaring it is refused, not approximated. + public const string SideStubBranch = "side_stub"; + + // Seconds an articulator takes to move from where the previous phone left it to this phone's first value. A tract does not step + // between phones: the merged surface curve held the previous value up to the boundary and jumped, and a jump in a ladder cell's + // area is a transient the ear hears as a click (nibu: peak 0.40 and 0.49 against a 0.2 vowel, at the 95 and 275 ms boundaries), + // and it leaves the consonant without the formant transitions that tell /b/ from a fade-in. A DESIGN VALUE inside the 20-50 ms + // of a CV formant transition; no measurement of this tract retrieved. + public const float PhoneTransitionSeconds = 0.03f; + + // Seconds a stop takes to open from full closure; its burst lasts about this long. A DESIGN VALUE inside the 5-15 ms of a + // stop burst; no measurement of this tract retrieved. The opening's noise is still shaped by the exporter. + private const float StopReleaseSeconds = 0.012f; + + /// Seconds after the start of a stop gesture of the given duration at which its articulators begin to open (the stop is + /// shut until then). The one owner of the release time: the contact, lip and constriction splines hold shut to here and the burst + /// gain window opens here; a test that wants the release reads it from here. + public static float StopReleaseStartSeconds(float durationSeconds) => durationSeconds - MathF.Min(StopReleaseSeconds, 0.3f * durationSeconds); + + /// + /// The DSL's own IPA table: the phones a phoneme_gesture knows without an explicit descriptor. A hand-written script + /// that names another phone falls back to an alveolar approximant; a caller that must not be approximated asks here first. + /// Vowels come from , the one table that also says which measured tract shape each selects. + /// + public static bool TryIpaDescriptor(string ipa, out string descriptor) + { + if (StoryVowelAreas.TryVowelForIpa(ipa, out var vowelDescriptor, out _)) + { + descriptor = vowelDescriptor; + return true; + } + + var known = ipa switch + { + "p" => "voiceless_bilabial_plosive", + "b" => "voiced_bilabial_plosive", + "t" => "voiceless_alveolar_plosive", + "d" => "voiced_alveolar_plosive", + "k" => "voiceless_velar_plosive", + "m" => "voiced_bilabial_nasal", + "n" => "voiced_alveolar_nasal", + "l" => "voiced_alveolar_lateral_approximant", + "s" => "voiceless_alveolar_fricative", + "z" => "voiced_alveolar_fricative", + "ʃ" => "voiceless_postalveolar_fricative", + "ʒ" => "voiced_postalveolar_fricative", + "ʍ" => "voiceless_labial_velar_fricative", + "w" => "voiced_labial_velar_approximant", + _ => null + }; + descriptor = known ?? ""; + return known is not null; + } + + /// The distance in cm from the lip plane at which a descriptor's place closes, or false when the place has no distance + /// (fraction only, or no place named): a caller must not invent one. + public static bool TryPlaceDistanceCentimeters(string descriptor, out float centimeters) => + Compiler.TryPlaceDistanceCentimeters(descriptor, out centimeters); public static SynthPatch Parse(string script) { @@ -217,6 +276,10 @@ public static class PatchScript FlushPendingOperatorGraph(); AddAreaFunction(fields, line); break; + case "vowel_set": + FlushPendingOperatorGraph(); + AddVowelSet(fields, line); + break; case "waveguide_path": FlushPendingOperatorGraph(); AddWaveguidePath(fields, line); @@ -713,6 +776,43 @@ public static class PatchScript RegisterAreaFunctionSurfaces(_areaFunctions.Count - 1, area); } + // vowel_set area_function= source=story2008: attaches the eleven measured vowel shapes to a morphology and + // registers a weight surface /vocal/areas//area/vowel_ in [0,1] for each (default 0; all zero is the geometric mean shape). + // The morphology's tongue surfaces stay registered but no longer shape the tract. Must precede the phoneme_gestures that drive it. + private void AddVowelSet(IReadOnlyDictionary fields, int line) + { + var name = RequiredAny(fields, ["area_function", "morphology", "area", "shape"], line); + var source = Required(fields, "source", line); + if (!source.Equals(StoryVowelAreas.Source, StringComparison.OrdinalIgnoreCase)) + { + throw new PatchScriptException(line, $"unknown vowel source `{source}` (only `{StoryVowelAreas.Source}`)"); + } + + if (!_areaFunctionsByName.TryGetValue(name, out var area)) + { + throw new PatchScriptException(line, $"unknown area function `{name}`"); + } + + if (area.Deformation is null) + { + throw new PatchScriptException(line, $"area function `{name}` has no area control (constriction and lip surfaces) for vowels to work with"); + } + + if (area.Vowels is not null) + { + throw new PatchScriptException(line, $"area function `{name}` already has vowel shapes"); + } + + var index = _areaFunctions.IndexOf(area); + var withVowels = area with { Vowels = StoryVowelAreas.All.Select(vowel => new VowelAreaShape(vowel.Id, vowel.Areas)).ToArray() }; + _areaFunctions[index] = withVowels; + _areaFunctionsByName[name] = withVowels; + foreach (var vowel in StoryVowelAreas.All) + { + RegisterNormalizedPrimitiveSurface(OwnerField($"/vocal/areas/{index}/area", StoryVowelAreas.SurfaceField(vowel.Id)), name, StoryVowelAreas.SurfaceField(vowel.Id), 0); + } + } + private void AddWaveguidePath(IReadOnlyDictionary fields, int line) { var name = Required(fields, "name", line); @@ -866,7 +966,11 @@ public static class PatchScript GetBoundFloat(fields, line, 0.5f, $"/vocal/sources/{_sourcePorts.Count}/opening", "opening", "open"), GetBoundFloat(fields, line, 0.08f, $"/vocal/sources/{_sourcePorts.Count}/noise", "noise", "aspiration"), GetBoundFloat(fields, line, 0.35f, $"/vocal/sources/{_sourcePorts.Count}/impedance", "impedance", "load", "source_impedance"), - GetBoundFloat(fields, line, 0.02f, $"/vocal/sources/{_sourcePorts.Count}/flow_scale", "flow_scale", "scale")); + GetBoundFloat(fields, line, 0.02f, $"/vocal/sources/{_sourcePorts.Count}/flow_scale", "flow_scale", "scale"), + GetBoundFloat(fields, line, 0.5f, $"/vocal/sources/{_sourcePorts.Count}/tilt", "tilt", "spectral_tilt"), + GetBoundFloat(fields, line, 1f, $"/vocal/sources/{_sourcePorts.Count}/pitch_ratio", "pitch_ratio", "f0_ratio"), + ParseGlottalPulse(GetAny(fields, ["pulse", "glottal_pulse"], "saw"), line), + Math.Clamp(GetFloat(fields, line, 0f, "noise_highpass", "noise_hp"), 0f, 8000f)); _sourcePorts.Add(port); _sourcePortsByName[name] = port; RegisterSourcePortSurfaces(_sourcePorts.Count - 1, port); @@ -1312,6 +1416,8 @@ public static class PatchScript RegisterNormalizedPrimitiveSurface(OwnerField(owner, "noise"), port.Name, "noise", port.Noise); RegisterNormalizedPrimitiveSurface(OwnerField(owner, "impedance"), port.Name, "impedance", port.Impedance, 0.05f, 2); RegisterNormalizedPrimitiveSurface(OwnerField(owner, "flow_scale"), port.Name, "flow_scale", port.FlowScale, 0, 0.2f); + RegisterNormalizedPrimitiveSurface(OwnerField(owner, "tilt"), port.Name, "tilt", port.Tilt); + RegisterNormalizedPrimitiveSurface(OwnerField(owner, "pitch_ratio"), port.Name, "pitch_ratio", port.PitchRatio, 0.5f, 2f); } private void RegisterAreaFunctionSurfaces(int index, AreaFunction area) @@ -1406,11 +1512,58 @@ public static class PatchScript duration * 0.25f, Math.Clamp(point.value, 0, 1))) .ToArray(); + splinePoints = FollowSegments(WithPhoneTransition(surfacePath, start, duration, splinePoints)); var spline = new ControlSpline(splineName, surfacePath, splinePoints, ControlSplineInterpolation.Bezier); _controlSplines.Add(spline); _controlSplinesByName[spline.Name] = spline; } + + + // Prepends the boundary ramp: a point at the gesture start holding the value the surface has there, then the gesture's first + // point PhoneTransitionSeconds later (at most 0.4 of the gesture, and before its second point). The first phone of a word, and + // a surface the earlier phones never touched, keep their value from the start. + private ControlSplinePoint[] WithPhoneTransition(string surfacePath, float start, float duration, ControlSplinePoint[] points) + { + ControlSplinePoint? previous = null; + foreach (var spline in _controlSplines) + { + if (!spline.SurfacePath.Equals(surfacePath, StringComparison.OrdinalIgnoreCase)) continue; + foreach (var point in spline.Points) + { + if (point.TimeSeconds <= start + 0.000001f && (previous is null || point.TimeSeconds >= previous.TimeSeconds)) previous = point; + } + } + + if (previous is null || points.Length < 2 || MathF.Abs(previous.Value - points[0].Value) <= 0.0005f) return points; + var ramp = MathF.Min(PhoneTransitionSeconds, 0.4f * duration); + ramp = MathF.Min(ramp, points[1].TimeSeconds - points[0].TimeSeconds); + if (ramp < 0.002f) return points; + var handle = ramp / 3; + var first = points[0]; + var lead = new ControlSplinePoint(first.TimeSeconds, previous.Value, handle, previous.Value, handle, previous.Value); + var arrival = first with { TimeSeconds = first.TimeSeconds + ramp, InTimeOffsetSeconds = handle, InValue = first.Value }; + return [lead, arrival, .. points.Skip(1)]; + } + + // Sets every handle's time to a third of the segment it belongs to. The Faust lowering evaluates a segment's Bezier on the + // linear fraction of its time and ignores the handle times; a handle of a third makes the C# timeline (which solves the curve's + // time for its parameter) agree with it exactly. A handle longer than the segment (0.25 of the gesture on a 12 ms opening) folds + // the C# curve back on itself. + private static ControlSplinePoint[] FollowSegments(ControlSplinePoint[] points) + { + if (points.Length < 2) return points; + var shaped = new ControlSplinePoint[points.Length]; + for (var i = 0; i < points.Length; i++) + { + var after = (i + 1 < points.Length ? points[i + 1].TimeSeconds - points[i].TimeSeconds : points[i].TimeSeconds - points[i - 1].TimeSeconds) / 3; + var before = (i > 0 ? points[i].TimeSeconds - points[i - 1].TimeSeconds : points[1].TimeSeconds - points[0].TimeSeconds) / 3; + shaped[i] = points[i] with { InTimeOffsetSeconds = before, OutTimeOffsetSeconds = after }; + } + + return shaped; + } + private string UniqueSplineName(string name) { var safe = SafeName(name); @@ -1429,7 +1582,7 @@ public static class PatchScript } } - private void EmitPhonemeGestureSplines(string name, string descriptor, float start, float duration, float intensity) + private void EmitPhonemeGestureSplines(string name, string descriptor, float start, float duration, float intensity, int line, float pitchRatio = 1f) { var tokens = DescriptorTokens(descriptor); var voiced = tokens.Contains("voiced") || tokens.Contains("vowel") || tokens.Contains("nasal") || tokens.Contains("approximant"); @@ -1438,28 +1591,60 @@ public static class PatchScript voiced = false; } - var place = PlaceTarget(tokens); var constriction = ConstrictionTarget(tokens); var pressure = Math.Clamp(0.45f + 0.30f * intensity, 0, 1); AddAuthoredSpline($"{name}_source_pressure", "/vocal/sources/0/pressure", start, duration, (0, 0.4f), (0.2f, pressure), (1, pressure * 0.85f)); AddAuthoredSpline($"{name}_source_tension", "/vocal/sources/0/tension", start, duration, (0, voiced ? 0.55f : 0.18f), (1, voiced ? 0.62f : 0.20f)); AddAuthoredSpline($"{name}_source_opening", "/vocal/sources/0/opening", start, duration, (0, voiced ? 0.45f : 0.25f), (1, voiced ? 0.48f : 0.28f)); + // Intensity is an amplitude factor on the source's flow scale (the level lever: pressure saturates in the tanh), and the f0 ratio + // is held per gesture; both default to the source's own value. + HoldFractionOfDefault($"{name}_source_level", "/vocal/sources/0/flow_scale", start, duration, intensity); + HoldOnSurface($"{name}_source_pitch", "/vocal/sources/0/pitch_ratio", start, duration, pitchRatio); AddAuthoredSpline($"{name}_source_noise", "/vocal/sources/0/noise", start, duration, (0, tokens.Contains("fricative") ? 0.45f : 0.05f), (1, tokens.Contains("fricative") ? 0.55f : 0.05f)); if (tokens.Contains("vowel")) { - EmitVowelGesture(name, tokens, start, duration, intensity); + EmitVowelGesture(name, descriptor, start, duration, line); return; } - AddAuthoredSpline($"{name}_constriction_index", "/vocal/areas/0/area/constriction_index", start, duration, (0, place), (1, place)); - AddAuthoredSpline($"{name}_constriction_diameter", "/vocal/areas/0/area/constriction_diameter", start, duration, (0, constriction), (0.5f, constriction), (1, Math.Min(0.65f, constriction + 0.2f))); + var length = _areaFunctions.Count > 0 ? _areaFunctions[0].Shape.LengthCentimeters : StoryVowelAreas.MeanLengthCentimeters; + var place = PlaceFraction(tokens, length); + if (_areaFunctions.Count > 0 && _areaFunctions[0].Vowels is not null && _areaFunctions[0].Deformation is { } control) + { + // Under vowel_set the constriction is placed by distance along the tract: the surface is a section index, so a normalized + // place fraction would land at fraction * sections / (sections - 1) of the tract, past the lips for a labial. + var sections = Math.Max(2, _areaFunctions[0].Shape.Sections); + HoldOnSurface($"{name}_constriction_index", "/vocal/areas/0/area/constriction_index", start, duration, place * (sections - 1) / control.IndexScale); + } + else + { + AddAuthoredSpline($"{name}_constriction_index", "/vocal/areas/0/area/constriction_index", start, duration, (0, place), (1, place)); + } + + // A nasal stop holds its oral closure for as long as the velum and the contact stay put (to 0.8 of the gesture): the murmur is the + // closed-mouth sound, and an alveolar closure that began to open at 0.5 let the oral tract radiate again over the last third (/n/ read + // -3.8 dB under its vowels against /m/ -13.2 dB, whose lips hold to 0.7). + var stopsBehindTheLips = (tokens.Contains("plosive") || tokens.Contains("stop")) && !(tokens.Contains("bilabial") || tokens.Contains("labiodental") || tokens.Contains("labial")); + // A stop whose seal is the tongue (not the lips) is closed completely and until the contact opens (StopReleaseStartSeconds): at the nasal's 0.03 cm + // aperture a /t/ closure measured -10.4 dB re its vowels against /p/ -19.7 (the lips seal /p/ whatever the tongue does), and + // rendered -21.5 dB with the aperture at 0 (area floor 1e-6 cm^2). + // Every stop holds all its articulators shut until StopReleaseSeconds before its end and opens in that time: the burst is the + // opening, and an opening spread over a third of the gesture (33 ms of the 95 ms /b/) made the burst a fricative's length + // (the phone recogniser heard /v/ before the vowel, Meta heard "fah then bah"). + var isStop = tokens.Contains("plosive") || tokens.Contains("stop"); + var stopHold = StopReleaseStartSeconds(duration) / duration; + var closureHold = tokens.Contains("nasal") || tokens.Contains("lateral") ? 0.8f : isStop ? stopHold : 0.5f; + var closed = stopsBehindTheLips ? 0f : constriction; + AddAuthoredSpline($"{name}_constriction_diameter", "/vocal/areas/0/area/constriction_diameter", start, duration, (0, closed), (closureHold, closed), (1, Math.Min(0.65f, constriction + 0.2f))); if (tokens.Contains("bilabial") || tokens.Contains("labiodental") || tokens.Contains("labial") || tokens.Contains("rounded")) { - var lipTarget = tokens.Contains("approximant") ? 0.38f : tokens.Contains("fricative") ? 0.18f : 0.04f; - AddAuthoredSpline($"{name}_lip_area", "/vocal/areas/0/area/lip_opening", start, duration, (0, lipTarget), (0.7f, lipTarget), (1, 0.45f)); - AddAuthoredSpline($"{name}_lip_radiation", "/vocal/radiation/0/aperture", start, duration, (0, lipTarget), (0.7f, lipTarget), (1, 0.75f)); + // A stop's lips are shut, not at a 0.04 leak: at 0.04 the closed-mouth voice bar of /b/ read -19.1 dB re the following vowel (the + // test's design value is -34 to -26) and /p/ -30.1 (-60 to -38); shut, -27.7 and -38.8. A nasal keeps its murmur's small opening. + var lipTarget = tokens.Contains("approximant") ? 0.38f : tokens.Contains("fricative") ? 0.18f : isStop ? 0f : 0.04f; + AddAuthoredSpline($"{name}_lip_area", "/vocal/areas/0/area/lip_opening", start, duration, (0, lipTarget), (isStop ? stopHold : 0.7f, lipTarget), (1, 0.45f)); + AddAuthoredSpline($"{name}_lip_radiation", "/vocal/radiation/0/aperture", start, duration, (0, lipTarget), (isStop ? stopHold : 0.7f, lipTarget), (1, 0.75f)); } if (tokens.Contains("nasal")) @@ -1469,32 +1654,143 @@ public static class PatchScript } else if (tokens.Contains("plosive") || tokens.Contains("stop")) { - AddAuthoredSpline($"{name}_contact", "/vocal/contacts/0/opening", start, duration, (0, 0.02f), (0.65f, 0.02f), (1, 0.9f)); - AddAuthoredSpline($"{name}_stored_pressure", "/vocal/contacts/0/stored_pressure", start, duration, (0, 0.4f), (0.65f, 0.75f), (1, 0.05f)); - AddAuthoredSpline($"{name}_velum", "/vocal/branches/0/opening", start, duration, (0, 0.01f), (1, 0.01f)); + AddAuthoredSpline($"{name}_contact", "/vocal/contacts/0/opening", start, duration, (0, 0.02f), (stopHold, 0.02f), (1, 0.9f)); + AddAuthoredSpline($"{name}_stored_pressure", "/vocal/contacts/0/stored_pressure", start, duration, (0, 0.4f), (stopHold, 0.75f), (1, 0.05f)); + // The release burst is turbulence at the opening constriction: its gain is held for the plosive and the exporter shapes + // it by the stored pressure and the opening (see PrimitiveContactExpression). The gain exists only around this plosive: + // a surface holds its last value after its last point (and its first before its first), so a gain left at its level + // made noise at every later contact state, the final nasal's closure and the tail included. + // The exporter's burst is noise times 4 * opening * (1 - opening): it sounds whenever the contact passes between shut and open + // in either direction, so a gain held through the closure made the 30 ms closing ramp a 15-20 ms hiss before every stop (the + // >4 kHz share of the frames before /t/ was -1 dB against -57 dB with the closure stepped). The gain exists only from the + // stop's release start to its end, one ramp (0.15 of that window) either side. + var releaseStart = StopReleaseStartSeconds(duration); + GainAroundGesture($"{name}_release_flow", "/vocal/contacts/0/release_flow", start + releaseStart, duration - releaseStart, stopsBehindTheLips ? TongueStopBurstGain : PlosiveBurstGain); + PlaceBurst(name, tokens, place, start, duration); + AddAuthoredSpline($"{name}_velum", "/vocal/branches/0/opening", start, duration, (0, VelumClosed), (1, VelumClosed)); } else if (tokens.Contains("fricative")) { AddAuthoredSpline($"{name}_contact", "/vocal/contacts/0/opening", start, duration, (0, 0.18f), (1, 0.22f)); AddAuthoredSpline($"{name}_contact_resistance", "/vocal/contacts/0/resistance", start, duration, (0, 0.75f), (1, 0.85f)); - AddAuthoredSpline($"{name}_velum", "/vocal/branches/0/opening", start, duration, (0, 0.01f), (1, 0.01f)); + AddAuthoredSpline($"{name}_velum", "/vocal/branches/0/opening", start, duration, (0, VelumClosed), (1, VelumClosed)); } else { AddAuthoredSpline($"{name}_contact", "/vocal/contacts/0/opening", start, duration, (0, 0.55f), (1, 0.65f)); } + + // A lateral opens the side stub for its gesture and only then (the stub is closed before and after, as the burst gain is). + if (tokens.Contains("lateral") && _branchPortsByName.ContainsKey(SideStubBranch)) + { + var branch = _branchPorts.FindIndex(candidate => candidate.Name.Equals(SideStubBranch, StringComparison.OrdinalIgnoreCase)); + GainAroundGesture($"{name}_side_stub", $"/vocal/branches/{branch}/opening", start, duration, 1f); + } } - private void EmitVowelGesture(string name, HashSet tokens, float start, float duration, float intensity) + // A vowel gesture raises the weight of the descriptor's Story vowel to 1 and holds every other vowel weight at 0 (the schwa and + // any vowel descriptor without a Story shape hold all of them at 0: the geometric-mean shape). The constriction and the lip cap + // stay open, the velum closed. A vowel gesture on a morphology without vowel_set is a parse error: the vowel surfaces do not exist. + private void EmitVowelGesture(string name, string descriptor, float start, float duration, int line) { - var frontness = tokens.Contains("front") ? 0.70f : tokens.Contains("back") ? 0.30f : 0.50f; - var openness = tokens.Contains("close") ? 0.30f : tokens.Contains("mid") ? 0.50f : tokens.Contains("open") ? 0.78f : 0.55f; - var rounding = tokens.Contains("rounded") ? 0.35f : tokens.Contains("unrounded") ? 0.72f : 0.60f; - AddAuthoredSpline($"{name}_tongue_index", "/vocal/areas/0/area/tongue_index", start, duration, (0, frontness), (1, frontness)); - AddAuthoredSpline($"{name}_tongue_diameter", "/vocal/areas/0/area/tongue_diameter", start, duration, (0, openness), (1, openness)); - AddAuthoredSpline($"{name}_lip_area", "/vocal/areas/0/area/lip_opening", start, duration, (0, rounding), (1, rounding)); - AddAuthoredSpline($"{name}_lip_radiation", "/vocal/radiation/0/aperture", start, duration, (0, Math.Clamp(rounding + 0.1f * intensity, 0, 1)), (1, Math.Clamp(rounding + 0.1f * intensity, 0, 1))); - AddAuthoredSpline($"{name}_velum", "/vocal/branches/0/opening", start, duration, (0, 0.01f), (1, 0.01f)); + if (_areaFunctions.Count == 0 || _areaFunctions[0].Vowels is null) + { + throw new PatchScriptException(line, $"vowel gesture `{name}` needs a vowel_set before it: without one the vowel surfaces do not exist and the gesture would shape nothing"); + } + + var target = StoryVowelAreas.IdForDescriptor(descriptor); + foreach (var vowel in StoryVowelAreas.All) + { + var weight = vowel.Id == target ? 1f : 0f; + HoldOnSurface($"{name}_{StoryVowelAreas.SurfaceField(vowel.Id)}", $"/vocal/areas/0/area/{StoryVowelAreas.SurfaceField(vowel.Id)}", start, duration, weight); + } + + // A vowel leaves the tract unconstricted: hold the constriction and the lip cap at the surface maximum. + AddAuthoredSpline($"{name}_constriction_diameter", "/vocal/areas/0/area/constriction_diameter", start, duration, (0, 1), (1, 1)); + AddAuthoredSpline($"{name}_lip_area", "/vocal/areas/0/area/lip_opening", start, duration, (0, 1), (1, 1)); + // The ladder's lip cell already carries the lip area into the resonance and the level. Aperture is the radiating + // gain the consonant closures use (the lip cell cannot shut), so a vowel keeps it fully open; scaling it by the lip + // area as well counted the rounding twice and left /u/ 30 dB under /i/. + AddAuthoredSpline($"{name}_lip_radiation", "/vocal/radiation/0/aperture", start, duration, (0, 1), (1, 1)); + AddAuthoredSpline($"{name}_velum", "/vocal/branches/0/opening", start, duration, (0, VelumClosed), (1, VelumClosed)); + // The contact is fully open for a vowel; a contact left half open by the previous stop would keep making release noise. + AddAuthoredSpline($"{name}_contact", "/vocal/contacts/0/opening", start, duration, (0, 1), (1, 1)); + } + + // Holds a surface at its default value times a factor (clamped to the surface range). + private void HoldFractionOfDefault(string name, string surfacePath, float start, float duration, float factor) + { + if (!_controlSurfacesByPath.TryGetValue(surfacePath, out var surface)) return; + var normalized = Math.Clamp(surface.DefaultNormalized * factor, 0, 1); + AddAuthoredSpline(name, surfacePath, start, duration, (0, normalized), (1, normalized)); + } + + // Holds a surface at a value in its own unit (cm, section index); splines carry the surface's normalized range. + private void HoldOnSurface(string name, string surfacePath, float start, float duration, float value) + { + if (!_controlSurfacesByPath.TryGetValue(surfacePath, out var surface) || surface.MaxValue <= surface.MinValue) + { + return; + } + + var normalized = Math.Clamp((value - surface.MinValue) / (surface.MaxValue - surface.MinValue), 0, 1); + AddAuthoredSpline(name, surfacePath, start, duration, (0, normalized), (1, normalized)); + } + + // A surface that is zero before and after the gesture and the given level through it (the plosive burst gain, the lateral's side stub opening); the ramps are 0.15 of the gesture long and run into the neighbouring + // phones (the leading one is cut where the word begins). + private void GainAroundGesture(string name, string surfacePath, float start, float duration, float gain) + { + if (!_controlSurfacesByPath.TryGetValue(surfacePath, out var surface) || surface.MaxValue <= surface.MinValue) + { + return; + } + + var level = Math.Clamp((gain - surface.MinValue) / (surface.MaxValue - surface.MinValue), 0, 1); + ControlSplinePoint At(float time, float value) => new(time, value, 0, value, 0, value); + var ramp = duration * 0.15f; + var points = new List(); + if (start > 0) points.Add(At(Math.Max(0, start - ramp), 0)); + points.Add(At(start, level)); + points.Add(At(start + duration, level)); + points.Add(At(start + duration + ramp, 0)); + var spline = new ControlSpline(UniqueSplineName(name), surfacePath, FollowSegments(points.ToArray()), ControlSplineInterpolation.Bezier); + _controlSplines.Add(spline); + _controlSplinesByName[spline.Name] = spline; + } + + // The place a plosive's burst enters the tract: the contact's position. A labial plosive keeps its burst at the glottal end (position 0) + // and writes nothing while no other plosive has driven the position, so a word of labials is unchanged (the position surface does not + // exist, and the exporter sends a burst in at the glottal end unless its contact has that surface); once any other plosive + // drives it, every labial plosive of the word writes position 0 too, or it would hold that plosive's place. + private bool _burstSiteDriven; + private readonly List<(string Name, float Start, float Duration)> _labialPlosives = []; + + private void PlaceBurst(string name, HashSet tokens, float place, float start, float duration) + { + var labial = tokens.Contains("bilabial") || tokens.Contains("labiodental") || tokens.Contains("labial"); + if (labial && !_burstSiteDriven) + { + _labialPlosives.Add((name, start, duration)); + return; + } + + if (!_burstSiteDriven) + { + _burstSiteDriven = true; + if (_constrictionContacts.Count > 0) + { + var contact = _constrictionContacts[0]; + RegisterNormalizedPrimitiveSurface(OwnerField("/vocal/contacts/0", "position"), contact.Name, "position", contact.Position); + } + + foreach (var (labialName, labialStart, labialDuration) in _labialPlosives) + { + HoldOnSurface($"{labialName}_burst_site", "/vocal/contacts/0/position", labialStart, labialDuration, 0); + } + } + + HoldOnSurface($"{name}_burst_site", "/vocal/contacts/0/position", start, duration, labial ? 0 : place); } private static string GestureDescriptor(string ipa, IReadOnlyDictionary fields) @@ -1513,24 +1809,7 @@ public static class PatchScript return $"{phonation}_{place}_{manner}"; } - return ipa switch - { - "p" => "voiceless_bilabial_plosive", - "b" => "voiced_bilabial_plosive", - "m" => "voiced_bilabial_nasal", - "s" => "voiceless_alveolar_fricative", - "z" => "voiced_alveolar_fricative", - "ʃ" => "voiceless_postalveolar_fricative", - "ʒ" => "voiced_postalveolar_fricative", - "ʍ" => "voiceless_labial_velar_fricative", - "w" => "voiced_labial_velar_approximant", - "i" => "voiced_close_front_unrounded_vowel", - "u" => "voiced_close_back_rounded_vowel", - "a" or "ɑ" => "voiced_open_back_unrounded_vowel", - "æ" => "voiced_open_front_unrounded_vowel", - "ə" => "voiced_mid_central_unrounded_vowel", - _ => "voiced_alveolar_approximant" - }; + return TryIpaDescriptor(ipa, out var known) ? known : "voiced_alveolar_approximant"; } private static HashSet DescriptorTokens(string descriptor) => @@ -1540,25 +1819,87 @@ public static class PatchScript .Split(['_', ',', '+', ' '], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) .ToHashSet(StringComparer.OrdinalIgnoreCase); - private static float PlaceTarget(HashSet tokens) + // A shut velum is opening 0: the port area is linear in the opening (opening * coupling * NasalTractAreas maximum), so 0.01 would + // leave a 0.008 cm^2 leak that shows in the oral formants. + private const float VelumClosed = 0f; + + // Gain of the plosive release burst (contact release_flow, surface range 0..2). A DESIGN VALUE set by measurement against the + // following vowel; no published burst level was retrieved. + private const float PlosiveBurstGain = 0.06f; + + // The same for a stop sealed by the tongue, whose burst goes in at the closure (LadderInjection) and reaches the lips almost + // unfiltered: at 0.06 the isolated /t/ burst peaked at 0.76 of the following vowel's peak (/b/ 0.19; the rule is under half), at + // 0.03 it is about half that. A DESIGN VALUE set by that measurement. + private const float TongueStopBurstGain = 0.03f; + + // Distance from the lip plane to the alveolar ridge in cm. A DESIGN VALUE; no source for the distance was retrieved. 2.555 cm puts the + // closure on the centre of ladder cell 20 of 24 of the scaffold tract (17.52 cm at 48 kHz and 350 m/s: (20.5 / 24) * 17.52 = 14.965 cm + // from the glottis), the cell grid being what a narrow constriction (scaffold constriction_width .03, e-fold 0.63 cm) can close; it is + // the ladder's counterpart of the tube check's closed end after section 37 of 44 (14.73 cm). + private const float AlveolarRidgeCentimetersFromLips = 2.555f; + + // The velar closure, cm behind the lip plane: between the narrowest dorsal section of /u/ (section 26, 6.97 cm) and the narrowest + // pharyngeal section of /a/ (section 13, 12.14 cm) of the Story 2008 area functions. 9.81 cm is the 0.44 fraction the table held before + // it had distances; it lies within 0.05 cm of the centre of oral ladder cell 10 of 24 (0.4375 of the tract), and the closure's Gaussian is narrower + // than a cell; the mean of the two sections, 9.555 cm, would fall 0.3 cm from the nearest cell centre (not tried). + private const float VelarCentimetersFromLips = 9.81f; + + // The lateral's midline: the tongue tip meets the ridge and the flow leaves by the sides, here the side stub's neighbour, the oral path + // through a midline held nearly shut for the gesture (to 0.8, as the nasal). A DESIGN VALUE set so the lateral reads between the vowels and the nasals. + private const float LateralMidlineDiameter = 0.06f; + + // Where a consonant closes. One entry per place, first token match wins. A place with a distance is placed by it, in cm behind the + // lip plane, and its normalized position (0 glottis, 1 lips) is 1 - distance / tract length, for the vowel_set section index and the + // hand-written-script fraction alike. A place with no distance is "fraction only": its fraction of the tract length from the glottis, + // a hand-set DESIGN VALUE, and no later cut may read a distance from it (TryPlaceDistanceCentimeters says absent). + private readonly record struct PlaceEntry(string[] Tokens, float? CentimetersFromLips, float Fraction); + + private static readonly PlaceEntry[] Places = + [ + new(["bilabial", "labiodental", "labial"], 0f, 0f), + new(["dental"], null, 0.88f), + new(["alveolar"], AlveolarRidgeCentimetersFromLips, 0f), + new(["postalveolar", "palatoalveolar"], null, 0.70f), + new(["retroflex"], null, 0.64f), + new(["palatal"], null, 0.56f), + new(["velar"], VelarCentimetersFromLips, 0f), + new(["uvular"], null, 0.34f), + new(["pharyngeal", "epiglottal"], null, 0.20f), + new(["glottal"], null, 0.05f), + ]; + + private const float PlaceFractionWhenUnnamed = 0.70f; + + private static PlaceEntry? FindPlace(HashSet tokens) { - if (tokens.Contains("bilabial") || tokens.Contains("labiodental") || tokens.Contains("labial")) return 0.96f; - if (tokens.Contains("dental")) return 0.88f; - if (tokens.Contains("alveolar")) return 0.78f; - if (tokens.Contains("postalveolar") || tokens.Contains("palatoalveolar")) return 0.70f; - if (tokens.Contains("retroflex")) return 0.64f; - if (tokens.Contains("palatal")) return 0.56f; - if (tokens.Contains("velar")) return 0.44f; - if (tokens.Contains("uvular")) return 0.34f; - if (tokens.Contains("pharyngeal") || tokens.Contains("epiglottal")) return 0.20f; - if (tokens.Contains("glottal")) return 0.05f; - return 0.70f; + foreach (var place in Places) + { + if (place.Tokens.Any(tokens.Contains)) return place; + } + + return null; } + internal static bool TryPlaceDistanceCentimeters(string descriptor, out float centimeters) + { + centimeters = FindPlace(DescriptorTokens(descriptor))?.CentimetersFromLips ?? float.NaN; + return !float.IsNaN(centimeters); + } + + // Normalized position of a place along a tract of the given length: 0 glottis, 1 lips. + private static float PlaceFraction(HashSet tokens, float lengthCentimeters) => + FindPlace(tokens) switch + { + { CentimetersFromLips: { } cm } => 1 - cm / lengthCentimeters, + { } place => place.Fraction, + _ => PlaceFractionWhenUnnamed + }; + private static float ConstrictionTarget(HashSet tokens) { if (tokens.Contains("plosive") || tokens.Contains("stop") || tokens.Contains("nasal")) return 0.03f; if (tokens.Contains("fricative")) return 0.16f; + if (tokens.Contains("lateral")) return LateralMidlineDiameter; if (tokens.Contains("approximant")) return 0.48f; if (tokens.Contains("trill") || tokens.Contains("tap") || tokens.Contains("flap")) return 0.24f; return 0.55f; @@ -2546,13 +2887,19 @@ public static class PatchScript var start = GetFloat(fields, line, 0, "start", "time", "at"); var duration = Math.Max(0.001f, GetFloat(fields, line, 0.12f, "dur", "duration", "length")); var intensity = Math.Clamp(GetFloat(fields, line, 1, "intensity", "gain", "strength"), 0, 2); + var pitchRatio = Math.Clamp(GetFloat(fields, line, 1, "pitch_ratio", "f0_ratio"), 0.5f, 2f); var descriptor = GestureDescriptor(ipa, fields); + if (DescriptorTokens(descriptor).Contains("lateral") && !_branchPortsByName.ContainsKey(SideStubBranch)) + { + throw new PatchScriptException(line, $"a lateral gesture needs the `{SideStubBranch}` branch port (the sublingual stub); it is not approximated by a plain approximant"); + } + var name = TryGetAny(fields, ["name", "n"], out var nameText) ? nameText : $"ipa_{SafeName(ipa)}_{_phonemeGestures.Count}"; _phonemeGestures.Add(new PhonemeGesture(name, ipa, descriptor, start, duration, intensity)); - EmitPhonemeGestureSplines(name, descriptor, start, duration, intensity); + EmitPhonemeGestureSplines(name, descriptor, start, duration, intensity, line, pitchRatio); } private void AddControlCurve(IReadOnlyDictionary fields, int line) @@ -3965,6 +4312,7 @@ public static class PatchScript "nasal_branch" or "nose_branch" or "nasal" => "nasal_branch", "tract_motion" or "shape_motion" or "slew" => "tract_motion", "path" or "acoustic_path" or "morphology_path" => "acoustic_path", + "vowel_set" or "vowelset" => "vowel_set", "waveguide_path" or "waveguidepath" or "wg_path" => "waveguide_path", "source_port" or "acoustic_source" or "port_source" => "source_port", "constriction_contact" or "contact_port" or "contact" => "constriction_contact", @@ -4097,6 +4445,13 @@ public static class PatchScript _ => throw new PatchScriptException(line, $"unknown tract propagation mode `{value}`") }; + private static GlottalPulse ParseGlottalPulse(string value, int line) => value.ToLowerInvariant() switch + { + "saw" or "sawtooth" => GlottalPulse.Saw, + "rosenberg" => GlottalPulse.Rosenberg, + _ => throw new PatchScriptException(line, $"unknown glottal pulse `{value}` (saw, rosenberg)") + }; + private static AcousticSourceKind ParseAcousticSourceKind(string value, int line) => value.ToLowerInvariant() switch { "glottal" or "glottis" or "larynx" => AcousticSourceKind.Glottal, diff --git a/src/AquaSynth.Core/ProbeTimelineReport.cs b/src/AquaSynth.Core/ProbeTimelineReport.cs index 6306308..42bf89f 100644 --- a/src/AquaSynth.Core/ProbeTimelineReport.cs +++ b/src/AquaSynth.Core/ProbeTimelineReport.cs @@ -7,7 +7,7 @@ public sealed record ProbeTimelineSample(int Block, string Primitive, string Sig public static class ProbeTimelineReport { - public static IReadOnlyList Build(SynthPatch patch, string networkName, int blocks = 4) + public static IReadOnlyList Build(SynthPatch patch, string networkName, int blocks = 4, float sampleRate = 48000) { var network = patch.VocalNetworks.First(network => network.Name.Equals(networkName, StringComparison.OrdinalIgnoreCase)); var areas = patch.AreaFunctions.ToDictionary(area => area.Name, StringComparer.OrdinalIgnoreCase); @@ -19,14 +19,15 @@ public static class ProbeTimelineReport var surfacesByField = patch.ControlSurfaces.ToDictionary(surface => surface.FieldPath, StringComparer.OrdinalIgnoreCase); var splinesBySurface = patch.ControlSplines .Where(spline => spline.Enabled) - .GroupBy(spline => spline.SurfacePath, StringComparer.OrdinalIgnoreCase) - .ToDictionary(group => group.Key, group => group.ToArray(), StringComparer.OrdinalIgnoreCase); + .Select(spline => spline.SurfacePath) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToDictionary(surfacePath => surfacePath, surfacePath => ControlSplineTimeline.SurfaceSplines(patch.ControlSplines, surfacePath), StringComparer.OrdinalIgnoreCase); var samples = new List(); var blockSize = patch.ProbeTimelines.FirstOrDefault(probe => probe.Name.Equals(network.ProbeTimeline, StringComparison.OrdinalIgnoreCase))?.BlockSize ?? 64; for (var block = 0; block < Math.Max(1, blocks); block++) { - var timeSeconds = block * blockSize / 48000f; + var timeSeconds = block * blockSize / sampleRate; var outgoingByPath = new Dictionary(StringComparer.OrdinalIgnoreCase); foreach (var pathName in network.Paths) { @@ -36,7 +37,7 @@ public static class ProbeTimelineReport var pathIndex = PathIndex(patch.WaveguidePaths, path); var speed = SurfaceValue(surfacesByField, splinesBySurface, $"/vocal/paths/{pathIndex}/speed", path.PropagationSpeedMetersPerSecond, timeSeconds); var loss = SurfaceValue(surfacesByField, splinesBySurface, $"/vocal/paths/{pathIndex}/loss", path.Loss, timeSeconds); - var delay = area.Shape.LengthMeters / Math.Max(1, speed) * 48000; + var delay = area.Shape.LengthMeters / Math.Max(1, speed) * sampleRate; var incoming = PathIncomingFlow(patch, network, sources, contacts, branches, path.Name, surfacesByField, splinesBySurface, timeSeconds); var outgoing = incoming * Math.Clamp(loss, 0, 1); outgoingByPath[path.Name] = outgoing; @@ -79,6 +80,9 @@ public static class ProbeTimelineReport Add(samples, block, $"contact:{contact.Name}", "resistance", resistance); Add(samples, block, $"contact:{contact.Name}", "reservoir", reservoir); Add(samples, block, $"contact:{contact.Name}", "released_flow", reservoir * opening * (1 - Math.Min(0.95f, resistance * 0.5f))); + var releaseGain = SurfaceValue(surfacesByField, splinesBySurface, $"{owner}/release_flow", contact.ReleaseFlow, timeSeconds); + // The exporter's burst is white noise of this amplitude (noise itself is not mirrored). + Add(samples, block, $"contact:{contact.Name}", "burst_amplitude", releaseGain * stored * 4 * opening * (1 - opening)); } foreach (var branchName in network.Branches) diff --git a/src/AquaSynth.Core/SfxrParams.cs b/src/AquaSynth.Core/SfxrParams.cs index 24069aa..1aa08aa 100644 --- a/src/AquaSynth.Core/SfxrParams.cs +++ b/src/AquaSynth.Core/SfxrParams.cs @@ -61,8 +61,7 @@ public sealed record SfxrParams PhaOffset = -0.22f, PhaRamp = -0.1f, VibStrength = 0.22f, - VibSpeed = 0.28f, - RepeatSpeed = 0.42f + VibSpeed = 0.28f }; public static SfxrParams Powerup() => new() diff --git a/src/AquaSynth.Core/SpeechDistributedTraining.cs b/src/AquaSynth.Core/SpeechDistributedTraining.cs index ec994aa..2c2f62d 100644 --- a/src/AquaSynth.Core/SpeechDistributedTraining.cs +++ b/src/AquaSynth.Core/SpeechDistributedTraining.cs @@ -32,7 +32,7 @@ public sealed record SpeechRenderResult( [property: CultName] string ResultId, [property: Key(1)] - [property: CultIndex("request")] + [property: CultRole("request")] string RequestId, [property: Key(2)] string WorkerId, [property: Key(3)] string RendererToolchain, @@ -67,7 +67,8 @@ public sealed record SpeechTimingReceipt( [property: Key(2)] double LatencyMilliseconds, [property: Key(3)] double BudgetMilliseconds, [property: Key(4)] float Confidence, - [property: Key(5)] string Notes); + [property: Key(5)] string Notes, + [property: Key(6), CultElementId] string ElementId = ""); [MessagePackObject] public sealed record SpeechUtteranceEmbeddingInputSnapshot( @@ -107,13 +108,15 @@ public sealed record SpeechPhoneticEventSnapshot( public sealed record SpeechScoreMetric( [property: Key(0)] string Name, [property: Key(1)] float Value, - [property: Key(2)] float Weight); + [property: Key(2)] float Weight, + [property: Key(3), CultElementId] string ElementId = ""); [MessagePackObject] public sealed record SpeechRenderArtifact( [property: Key(0)] string Kind, [property: Key(1)] string Uri, - [property: Key(2)] string ContentHash); + [property: Key(2)] string ContentHash, + [property: Key(3), CultElementId] string ElementId = ""); public enum SpeechRenderStatus { @@ -141,7 +144,8 @@ public sealed record SpeechWorkerPayloadArtifact( [property: Key(0)] string Kind, [property: Key(1)] string Uri, [property: Key(2)] string ContentHash, - [property: Key(3)] string MediaType); + [property: Key(3)] string MediaType, + [property: Key(4), CultElementId] string ElementId = ""); [CultDocument("aquasynth.speech_worker_admission_receipt", "aquasynth.speech_worker_admission_receipt.v1")] [MessagePackObject] @@ -150,7 +154,7 @@ public sealed record SpeechWorkerAdmissionReceipt( [property: CultName] string ReceiptId, [property: Key(1)] - [property: CultIndex("payload")] + [property: CultRole("payload")] string PayloadId, [property: Key(2)] string PeerId, [property: Key(3)] string LeaseId, diff --git a/src/AquaSynth.Core/StoreFileName.cs b/src/AquaSynth.Core/StoreFileName.cs new file mode 100644 index 0000000..9dcac70 --- /dev/null +++ b/src/AquaSynth.Core/StoreFileName.cs @@ -0,0 +1,95 @@ +using System.Security.Cryptography; +using System.Text; + +namespace AquaSynth.Dsl; + +/// +/// The one mapping from a caller-chosen id to a single file-name component in a store. +/// An id passes through unchanged only if it is already a fixed point: ASCII [A-Za-z0-9._-], +/// all lowercase (stores live on case-insensitive filesystems), at most long, +/// no trailing dot, not a Windows device stem (CON, NUL, COM1, ...), and not ending in a +/// "-" plus 16 hex digits. Every other id is sanitised and suffixed with "-" plus the first +/// 16 hex of SHA-256 of the original id. Passed-through names never end in the hash shape and +/// rewritten names always do, so the two sets cannot meet; distinct ids stay distinct up to a 64-bit +/// hash. The result is never a path: no separators, never "." or "..". +/// +/// +/// Not handled: symlinks and junctions already inside a store are followed, and Windows 8.3 short names +/// were not verified. An id with an uppercase letter is rewritten, so a batch directory named for such +/// an id gains a hash suffix. +/// +public static class StoreFileName +{ + private const int MaxStem = 100; + private const int HashLength = 16; + + public static string From(string id) + { + ArgumentNullException.ThrowIfNull(id); + if (id.Length == 0 || id.All(c => c == '.')) + { + throw new ArgumentException($"Id '{id}' cannot name a store file.", nameof(id)); + } + + var builder = new StringBuilder(Math.Min(id.Length, MaxStem)); + foreach (var c in id.Take(MaxStem)) + { + builder.Append(c is >= 'a' and <= 'z' or >= 'A' and <= 'Z' or >= '0' and <= '9' or '-' or '_' or '.' ? c : '_'); + } + + var safe = builder.ToString(); + if (IsFixedPoint(id, safe)) + { + return safe; + } + + // "nul.x-" still has the device stem "nul"; the underscore moves the whole name off it. + if (IsDeviceStem(safe)) + { + safe = "_" + safe; + } + + var hash = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(id)))[..HashLength].ToLowerInvariant(); + return $"{safe}-{hash}"; + } + + /// A child of named for an external id; the result never leaves the root. + public static string Combine(string root, string id) => Path.Combine(root, From(id)); + + private static bool IsFixedPoint(string id, string safe) + { + if (safe != id || id.EndsWith('.') || id.Any(char.IsAsciiLetterUpper) || IsDeviceStem(id) || EndsWithHashShape(id)) + { + return false; + } + + return true; + } + + // Windows resolves CON, PRN, AUX, NUL, COM0-9, LPT0-9 as devices whatever the extension. + private static bool IsDeviceStem(string id) + { + var dot = id.IndexOf('.'); + var stem = (dot < 0 ? id : id[..dot]).TrimEnd(' '); + return stem.Length switch + { + 3 => stem.Equals("con", StringComparison.OrdinalIgnoreCase) + || stem.Equals("prn", StringComparison.OrdinalIgnoreCase) + || stem.Equals("aux", StringComparison.OrdinalIgnoreCase) + || stem.Equals("nul", StringComparison.OrdinalIgnoreCase), + 4 => char.IsAsciiDigit(stem[3]) + && (stem.StartsWith("com", StringComparison.OrdinalIgnoreCase) || stem.StartsWith("lpt", StringComparison.OrdinalIgnoreCase)), + _ => false, + }; + } + + private static bool EndsWithHashShape(string id) + { + if (id.Length <= HashLength || id[^(HashLength + 1)] != '-') + { + return false; + } + + return id[^HashLength..].All(c => c is >= '0' and <= '9' or >= 'a' and <= 'f'); + } +} diff --git a/src/AquaSynth.Core/StoryVowelAreas.cs b/src/AquaSynth.Core/StoryVowelAreas.cs new file mode 100644 index 0000000..51ef70b --- /dev/null +++ b/src/AquaSynth.Core/StoryVowelAreas.cs @@ -0,0 +1,93 @@ +namespace AquaSynth.Dsl; + +/// +/// The owner of the measured vowel tract shapes and of the IPA vowel -> Story vowel mapping. Data: Story BH, 2008, "Comparison of +/// magnetic resonance imaging-based vocal tract area functions obtained from the same speaker in 1994 and 2002", JASA 123(1):327-335, +/// doi 10.1121/1.2805683, PMC2377017, Table I: eleven vowels, 44 sections glottis -> lips, areas in cm^2. The cited copy is +/// docs/research/data/story2008-area-functions.csv; a test compares every cell of this table to that file (transcription guard). +/// The table is a one-speaker measurement and is unheard here: nothing in this file says how any vowel sounds. +/// +/// Story's vocal tract lengths run 16.1-19.6 cm (the CSV carries each). The ladder has one length per patch, so a patch that uses +/// these shapes sets its oral morphology to and every vowel is read at that length; the sections are +/// resampled onto the ladder's cells by . +/// +/// Control surface ids (ascii, used in /vocal/areas/<i>/area/vowel_<id>): i ih e eh ae uh ah aw o oo_short u for +/// i ɪ e ɛ æ ʌ ɑ ɔ o ʊ u. The schwa has no Story shape: it is the all-weights-zero state, the geometric mean of the eleven shapes. +/// +public static class StoryVowelAreas +{ + public const string Source = "story2008"; + public const float MeanLengthCentimeters = 17.52f; + public const int Sections = 44; + + public sealed record Vowel(string Ipa, string Id, string Descriptor, IReadOnlyList Areas); + + public static string SurfaceField(string id) => $"vowel_{id}"; + + /// + /// Vowels other than the eleven shapes that the DSL's IPA table accepts, and the shape each selects. ɑ and a share a shape; the schwa + /// maps to null (no weight raised: the geometric-mean shape). + /// + public static bool TryVowelForIpa(string ipa, out string descriptor, out string? id) + { + if (ipa == "a") ipa = "ɑ"; + if (ipa == "ə") + { + descriptor = SchwaDescriptor; + id = null; + return true; + } + + var vowel = All.FirstOrDefault(candidate => candidate.Ipa == ipa); + descriptor = vowel?.Descriptor ?? ""; + id = vowel?.Id; + return vowel is not null; + } + + public const string SchwaDescriptor = "voiced_mid_central_unrounded_vowel"; + + /// + /// The explicit descriptor -> Story vowel id lookup (there is no feature-distance guessing). A vowel descriptor with no entry, + /// and the schwa's, return null: the geometric-mean shape. + /// + public static string? IdForDescriptor(string descriptor) => + All.FirstOrDefault(vowel => vowel.Descriptor.Equals(descriptor.Replace('-', '_').ToLowerInvariant(), StringComparison.Ordinal))?.Id; + + /// + /// Area in cm^2 of at a normalized glottis -> lips position: linear interpolation between section centres + /// (section j is centred at (j + 0.5) / n), held at the first and last section beyond them. + /// + public static float AreaAt(IReadOnlyList areas, float position) + { + var x = Math.Clamp(position * areas.Count - 0.5f, 0, areas.Count - 1); + var left = (int)MathF.Floor(x); + var right = Math.Min(areas.Count - 1, left + 1); + return areas[left] + (areas[right] - areas[left]) * (x - left); + } + + public static readonly IReadOnlyList All = + [ + new("i", "i", "voiced_close_front_unrounded_vowel", [ + 0.51f, 0.59f, 0.62f, 0.72f, 1.24f, 2.30f, 3.30f, 3.59f, 3.22f, 2.86f, 3.00f, 3.61f, 4.39f, 4.95f, 5.17f, 5.16f, 5.18f, 5.26f, 5.20f, 5.02f, 4.71f, 4.13f, 3.43f, 2.83f, 2.32f, 1.83f, 1.46f, 1.23f, 1.08f, 0.94f, 0.80f, 0.67f, 0.55f, 0.46f, 0.40f, 0.36f, 0.35f, 0.35f, 0.38f, 0.51f, 0.74f, 0.92f, 0.96f, 0.91f]), + new("ɪ", "ih", "voiced_near_close_near_front_unrounded_vowel", [ + 0.28f, 0.21f, 0.21f, 0.30f, 0.47f, 0.71f, 1.12f, 1.48f, 1.35f, 1.05f, 0.92f, 0.92f, 1.19f, 1.94f, 2.83f, 3.31f, 3.48f, 3.60f, 3.64f, 3.49f, 3.20f, 2.90f, 2.59f, 2.21f, 1.87f, 1.54f, 1.20f, 0.92f, 0.74f, 0.59f, 0.52f, 0.54f, 0.59f, 0.65f, 0.71f, 0.67f, 0.61f, 0.57f, 0.50f, 0.48f, 0.54f, 0.73f, 0.93f, 0.82f]), + new("e", "e", "voiced_close_mid_front_unrounded_vowel", [ + 0.29f, 0.26f, 0.30f, 0.40f, 0.55f, 0.74f, 0.99f, 1.09f, 0.90f, 0.69f, 0.77f, 1.31f, 2.13f, 2.74f, 3.03f, 3.23f, 3.33f, 3.27f, 3.09f, 2.84f, 2.66f, 2.46f, 2.14f, 1.79f, 1.44f, 1.17f, 1.00f, 0.88f, 0.80f, 0.81f, 0.85f, 0.84f, 0.86f, 0.96f, 1.18f, 1.35f, 1.48f, 1.62f, 1.49f, 1.29f, 1.24f, 1.17f, 1.04f, 0.95f]), + new("ɛ", "eh", "voiced_open_mid_front_unrounded_vowel", [ + 0.37f, 0.30f, 0.24f, 0.23f, 0.29f, 0.41f, 0.58f, 0.82f, 0.97f, 0.82f, 0.62f, 0.54f, 0.54f, 0.68f, 1.09f, 1.62f, 2.03f, 2.35f, 2.51f, 2.39f, 2.22f, 2.13f, 2.00f, 1.78f, 1.58f, 1.43f, 1.31f, 1.23f, 1.24f, 1.38f, 1.61f, 1.82f, 1.96f, 2.01f, 2.00f, 1.95f, 1.77f, 1.48f, 1.30f, 1.21f, 1.10f, 0.99f, 0.91f, 0.78f]), + new("æ", "ae", "voiced_near_open_front_unrounded_vowel", [ + 0.31f, 0.21f, 0.18f, 0.23f, 0.33f, 0.50f, 0.78f, 0.96f, 0.85f, 0.63f, 0.46f, 0.36f, 0.33f, 0.46f, 0.73f, 1.00f, 1.30f, 1.66f, 1.97f, 2.06f, 2.03f, 2.01f, 1.89f, 1.66f, 1.49f, 1.42f, 1.37f, 1.34f, 1.41f, 1.58f, 1.82f, 2.19f, 2.63f, 2.97f, 3.17f, 3.40f, 3.56f, 3.57f, 3.58f, 3.44f, 3.15f, 3.38f, 3.99f, 4.17f]), + new("ʌ", "uh", "voiced_open_mid_back_unrounded_vowel", [ + 0.23f, 0.34f, 0.47f, 0.60f, 0.77f, 1.06f, 1.26f, 1.09f, 0.80f, 0.65f, 0.56f, 0.47f, 0.37f, 0.24f, 0.17f, 0.18f, 0.23f, 0.27f, 0.28f, 0.29f, 0.33f, 0.52f, 0.97f, 1.50f, 1.91f, 2.23f, 2.65f, 3.29f, 4.13f, 5.00f, 5.77f, 6.33f, 6.61f, 6.63f, 6.45f, 6.04f, 5.39f, 4.42f, 3.29f, 2.37f, 1.74f, 1.36f, 1.17f, 0.99f]), + new("ɑ", "ah", "voiced_open_back_unrounded_vowel", [ + 0.56f, 0.62f, 0.66f, 0.78f, 0.97f, 1.16f, 1.12f, 0.82f, 0.55f, 0.45f, 0.37f, 0.29f, 0.21f, 0.15f, 0.16f, 0.25f, 0.34f, 0.43f, 0.54f, 0.61f, 0.67f, 0.98f, 1.76f, 2.75f, 3.52f, 4.08f, 4.74f, 5.61f, 6.60f, 7.61f, 8.48f, 9.06f, 9.29f, 9.26f, 9.06f, 8.64f, 7.91f, 6.98f, 6.02f, 5.13f, 4.55f, 4.52f, 4.71f, 4.72f]), + new("ɔ", "aw", "voiced_open_mid_back_rounded_vowel", [ + 0.27f, 0.43f, 0.54f, 0.67f, 0.83f, 0.92f, 0.89f, 0.73f, 0.55f, 0.44f, 0.37f, 0.28f, 0.18f, 0.14f, 0.15f, 0.14f, 0.13f, 0.14f, 0.18f, 0.20f, 0.23f, 0.49f, 1.03f, 1.58f, 2.06f, 2.61f, 3.35f, 4.34f, 5.51f, 6.70f, 7.75f, 8.63f, 9.29f, 9.59f, 9.42f, 8.78f, 7.82f, 6.50f, 4.95f, 3.47f, 2.15f, 1.38f, 1.11f, 0.90f]), + new("o", "o", "voiced_close_mid_back_rounded_vowel", [ + 0.38f, 0.45f, 0.57f, 0.77f, 1.31f, 1.92f, 1.74f, 1.11f, 0.75f, 0.59f, 0.57f, 0.68f, 0.73f, 0.67f, 0.58f, 0.49f, 0.44f, 0.42f, 0.49f, 0.53f, 0.38f, 0.30f, 0.45f, 0.61f, 0.71f, 0.79f, 0.86f, 1.01f, 1.41f, 2.09f, 3.00f, 4.10f, 5.16f, 6.22f, 7.34f, 8.15f, 8.61f, 8.37f, 6.76f, 4.37f, 2.30f, 1.06f, 0.58f, 0.47f]), + new("ʊ", "oo_short", "voiced_near_close_near_back_rounded_vowel", [ + 0.37f, 0.38f, 0.49f, 0.62f, 0.85f, 1.28f, 1.62f, 1.47f, 1.04f, 0.81f, 1.03f, 1.44f, 1.49f, 1.28f, 1.06f, 0.85f, 0.69f, 0.56f, 0.42f, 0.27f, 0.27f, 0.41f, 0.51f, 0.49f, 0.47f, 0.50f, 0.58f, 0.80f, 1.19f, 1.62f, 2.27f, 3.24f, 4.16f, 5.00f, 5.70f, 6.11f, 6.21f, 6.29f, 6.24f, 4.91f, 2.61f, 1.09f, 0.63f, 0.59f]), + new("u", "u", "voiced_close_back_rounded_vowel", [ + 0.54f, 0.61f, 0.66f, 0.75f, 1.13f, 1.99f, 2.83f, 2.90f, 2.52f, 2.40f, 2.83f, 3.56f, 3.99f, 3.89f, 3.50f, 3.04f, 2.64f, 2.44f, 2.31f, 2.07f, 1.80f, 1.52f, 1.14f, 0.74f, 0.42f, 0.22f, 0.14f, 0.20f, 0.47f, 0.89f, 1.15f, 1.42f, 2.17f, 3.04f, 3.69f, 4.70f, 5.74f, 5.41f, 3.82f, 2.34f, 1.35f, 0.65f, 0.29f, 0.16f]), + ]; +} diff --git a/src/AquaSynth.Core/WeksaPhones.cs b/src/AquaSynth.Core/WeksaPhones.cs new file mode 100644 index 0000000..4e21898 --- /dev/null +++ b/src/AquaSynth.Core/WeksaPhones.cs @@ -0,0 +1,113 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace AquaSynth.Dsl; + +public enum WeksaStress { None, Primary, Secondary } + +public sealed record WeksaPhone(string Ipa, WeksaStress Stress); + +public sealed record WeksaPhones(string Language, string Source, IReadOnlyList Phones); + +public sealed class WeksaPhonesException(string message) : Exception(message); + +/// +/// Reader for the Weksa phones line (`weksa lower --phones`): {"language","source","phones":[{"ipa","stress"}]}. It is +/// strict: a missing, extra, null or mistyped key, an unknown stress value, an empty phone list or malformed JSON is an +/// error, never a guess. Weksa owns the segmentation; durations, pitch and gesture stay with . +/// +public static class WeksaPhonesReader +{ + private static readonly JsonSerializerOptions Strict = new() + { + UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow, + AllowTrailingCommas = false, + ReadCommentHandling = JsonCommentHandling.Disallow, + PropertyNameCaseInsensitive = false + }; + + private sealed class PhoneDto + { + [JsonPropertyName("ipa")] public required string? Ipa { get; init; } + [JsonPropertyName("stress")] public required string? Stress { get; init; } + } + + private sealed class LineDto + { + [JsonPropertyName("language")] public required string? Language { get; init; } + [JsonPropertyName("source")] public required string? Source { get; init; } + [JsonPropertyName("phones")] public required List? Phones { get; init; } + } + + public static WeksaPhones Parse(string json) + { + LineDto? dto; + try { dto = JsonSerializer.Deserialize(json, Strict); } + catch (JsonException ex) { throw new WeksaPhonesException($"phones line is not the expected JSON: {ex.Message}"); } + if (dto is null) throw new WeksaPhonesException("phones line is null"); + if (string.IsNullOrWhiteSpace(dto.Language)) throw new WeksaPhonesException("`language` must be a non-empty string"); + if (string.IsNullOrWhiteSpace(dto.Source)) throw new WeksaPhonesException("`source` must be a non-empty string"); + if (dto.Phones is null || dto.Phones.Count == 0) throw new WeksaPhonesException("`phones` must be a non-empty list"); + var phones = new List(); + for (var i = 0; i < dto.Phones.Count; i++) + { + var p = dto.Phones[i] ?? throw new WeksaPhonesException($"phones[{i}] is null"); + if (string.IsNullOrWhiteSpace(p.Ipa)) throw new WeksaPhonesException($"phones[{i}].ipa must be a non-empty string"); + var stress = p.Stress switch + { + "none" => WeksaStress.None, + "primary" => WeksaStress.Primary, + "secondary" => WeksaStress.Secondary, + null => throw new WeksaPhonesException($"phones[{i}].stress must be a string"), + var other => throw new WeksaPhonesException($"phones[{i}].stress `{other}` is not none, primary or secondary") + }; + phones.Add(new WeksaPhone(p.Ipa, stress)); + } + + return new WeksaPhones(dto.Language, dto.Source, phones); + } +} + +/// +/// A Weksa phones line as a word the tract can render. Every phone must be in the DSL's IPA table +/// (); a phone outside it is listed in and the plan +/// carries no targets, so nothing is approximated. Base durations are the vowel/consonant lengths AquaSynth owns; stress +/// carries its three acoustic cues (): the marked phone is longer, louder and higher in f0. +/// +public sealed record WeksaWordPlan(IReadOnlyList<(IpaGestureExperimentTarget Target, float Intensity)> Phones, IReadOnlyList Unsupported) +{ + public const float VowelSeconds = 0.18f, ConsonantSeconds = 0.095f; + public bool Renderable => Unsupported.Count == 0; + + /// Duration factor, amplitude factor on the source flow and f0 ratio of a stressed phone against an unstressed one. + public readonly record struct StressCue(float Duration, float Amplitude, float PitchRatio); + + /// + /// Primary stress: duration 220/160 = 1.375 (stressed against unstressed syllable in initial-stress pairs, Sluijter and van + /// Heuven 1996), amplitude 1.30 = 2.3 dB on the source flow, f0 +6 percent (their unaccented f0 difference). The amplitude is set + /// by the rendered result, not the nominal 2 dB (their 1-3 dB overall-intensity difference): the f0 rise alone moves the harmonics against + /// F1 and changes the rendered level by -1.3 to +1.8 dB by vowel (i -0.7, a -0.3, u -1.3, e -0.8, o +1.8), so 1.26 rendered +1.7 dB on + /// average over those five and 1.30 renders +2.0. Secondary stress is placed halfway, in dB and in percent: a DESIGN VALUE, no + /// secondary-stress measurement was retrieved. Unheard. + /// + public static StressCue CueFor(WeksaStress stress) => stress switch + { + WeksaStress.Primary => new(220f / 160f, 1.30f, 1.06f), + WeksaStress.Secondary => new(1.19f, 1.14f, 1.03f), + _ => new(1f, 1f, 1f) + }; + + public static WeksaWordPlan From(WeksaPhones word, string label) + { + var unsupported = word.Phones.Where(p => !PatchScript.TryIpaDescriptor(p.Ipa, out _)).ToArray(); + if (unsupported.Length > 0) return new WeksaWordPlan([], unsupported); + var targets = word.Phones.Select((p, i) => + { + PatchScript.TryIpaDescriptor(p.Ipa, out var descriptor); + var cue = CueFor(p.Stress); + var seconds = (descriptor.EndsWith("_vowel", StringComparison.Ordinal) ? VowelSeconds : ConsonantSeconds) * cue.Duration; + return (new IpaGestureExperimentTarget($"{label}_{i}_{p.Ipa}", p.Ipa, "", seconds, PitchRatio: cue.PitchRatio), cue.Amplitude); + }).ToArray(); + return new WeksaWordPlan(targets, []); + } +} diff --git a/src/AquaSynth.Dings/DingCatalog.cs b/src/AquaSynth.Dings/DingCatalog.cs index 4da53ec..df4659e 100644 --- a/src/AquaSynth.Dings/DingCatalog.cs +++ b/src/AquaSynth.Dings/DingCatalog.cs @@ -4,8 +4,25 @@ public sealed record DingNote(double Semitones, int DelayMilliseconds, float Gai public sealed record DingEvent(string Id, string Meaning, IReadOnlyList Notes); public sealed record DingInstrument(string Id, string Description, string TimbreFamily, string Script, float RootFrequency, float Brightness, float DecaySeconds, bool UsesNoise); +/// The daemon-facing identity of a catalog instrument. Revision is the literal 1 until the deferred content-revision step. +public sealed record DingPatchIdentity(string PatchId, string FaustName, int Revision); + public static class DingCatalog { + /// The only derivation of PatchId, FaustName and revision from an instrument id. Host, AquaLower and the tests call this. + public static DingPatchIdentity Identity(DingInstrument instrument) => + new($"dings.{instrument.Id}", $"dings_{instrument.Id.Replace('-', '_')}", 1); + + /// The play validation gate. The Host is its enforcing owner (every pipe client passes it); the MCP bridge's + /// own check in DingService.PlayAsync is a deprecated fail-fast path. Returns the rejection text, or null when valid. + public static string? PlayRejection(string eventId, string instrumentId, float gain) + { + if (!Events.ContainsKey(eventId)) return $"Unknown event '{eventId}'."; + if (!Instruments.ContainsKey(instrumentId)) return $"Unknown instrument '{instrumentId}'."; + if (!float.IsFinite(gain) || gain is < 0 or > 1) return "Gain must be between 0 and 1."; + return null; + } + public static readonly IReadOnlyDictionary Events = new DingEvent[] { new("session.start", "Opening welcome", [new(0, 0), new(7, 220)]), diff --git a/src/AquaSynth.Faust/AquaSynthDaemonService.cs b/src/AquaSynth.Faust/AquaSynthDaemonService.cs index 28ae62d..907b518 100644 --- a/src/AquaSynth.Faust/AquaSynthDaemonService.cs +++ b/src/AquaSynth.Faust/AquaSynthDaemonService.cs @@ -77,7 +77,8 @@ public sealed record AquaSynthAutomationStreamCommand( [MessagePackObject] public sealed record AquaSynthAutomationControlFrame( [property: Key(0)] int Block, - [property: Key(1)] Dictionary Controls); + [property: Key(1)] Dictionary Controls, + [property: Key(2), CultElementId] string ElementId = ""); [CultDocument("aquasynth.command.instrument_open", AquaSynthDaemonSchemas.InstrumentOpenCommand)] [MessagePackObject] @@ -95,14 +96,14 @@ public sealed record AquaSynthInstrumentOpenCommand( [MessagePackObject] public sealed record AquaSynthInstrumentControlCommand( [property: Key(0), CultName] string CommandId, - [property: Key(1), CultIndex("session")] string SessionId, + [property: Key(1), CultRole("session")] string SessionId, [property: Key(2)] Dictionary Controls); [CultDocument("aquasynth.command.instrument_block", AquaSynthDaemonSchemas.InstrumentBlockCommand)] [MessagePackObject] public sealed record AquaSynthInstrumentBlockCommand( [property: Key(0), CultName] string CommandId, - [property: Key(1), CultIndex("session")] string SessionId, + [property: Key(1), CultRole("session")] string SessionId, [property: Key(2)] int FrameCount = 128, [property: Key(3)] float Gain = 1.0f, [property: Key(4)] Dictionary? Controls = null); @@ -111,13 +112,13 @@ public sealed record AquaSynthInstrumentBlockCommand( [MessagePackObject] public sealed record AquaSynthInstrumentCloseCommand( [property: Key(0), CultName] string CommandId, - [property: Key(1), CultIndex("session")] string SessionId); + [property: Key(1), CultRole("session")] string SessionId); [CultDocument("aquasynth.compiled_instrument_session", AquaSynthDaemonSchemas.CompiledInstrumentSession)] [MessagePackObject] public sealed record AquaSynthCompiledInstrumentSession( [property: Key(0), CultName] string SessionId, - [property: Key(1), CultIndex("patch")] string PatchId, + [property: Key(1), CultRole("patch")] string PatchId, [property: Key(2)] string CompileKey, [property: Key(3)] string FaustName, [property: Key(4)] int Revision, @@ -134,7 +135,7 @@ public sealed record AquaSynthCompiledInstrumentSession( [MessagePackObject] public sealed record AquaSynthLiveInstrumentSessionState( [property: Key(0), CultName] string SessionId, - [property: Key(1), CultIndex("patch")] string PatchId, + [property: Key(1), CultRole("patch")] string PatchId, [property: Key(2)] string Status, [property: Key(3)] string OpenedAtUtc, [property: Key(4)] string UpdatedAtUtc, @@ -154,7 +155,7 @@ public sealed record AquaSynthLiveInstrumentSessionState( public sealed record AquaSynthPatchCompileReceipt( [property: Key(0), CultName] string ReceiptId, [property: Key(1), CultIndex("command")] string CommandId, - [property: Key(2), CultIndex("patch")] string PatchId, + [property: Key(2), CultRole("patch")] string PatchId, [property: Key(3)] string SessionId, [property: Key(4)] string Status, [property: Key(5)] string DecidedAtUtc, @@ -170,7 +171,7 @@ public sealed record AquaSynthPatchCompileReceipt( public sealed record AquaSynthRenderSampleReceipt( [property: Key(0), CultName] string RenderId, [property: Key(1), CultIndex("command")] string CommandId, - [property: Key(2), CultIndex("patch")] string PatchId, + [property: Key(2), CultRole("patch")] string PatchId, [property: Key(3)] string SessionId, [property: Key(4)] string Status, [property: Key(5)] string CompletedAtUtc, @@ -190,7 +191,7 @@ public sealed record AquaSynthRenderSampleReceipt( public sealed record AquaSynthAutomationStreamReceipt( [property: Key(0), CultName] string StreamRunId, [property: Key(1), CultIndex("command")] string CommandId, - [property: Key(2), CultIndex("patch")] string PatchId, + [property: Key(2), CultRole("patch")] string PatchId, [property: Key(3)] string SessionId, [property: Key(4)] string Status, [property: Key(5)] string CompletedAtUtc, @@ -210,8 +211,8 @@ public sealed record AquaSynthAutomationStreamReceipt( public sealed record AquaSynthInstrumentOpenReceipt( [property: Key(0), CultName] string ReceiptId, [property: Key(1), CultIndex("command")] string CommandId, - [property: Key(2), CultIndex("patch")] string PatchId, - [property: Key(3), CultIndex("session")] string SessionId, + [property: Key(2), CultRole("patch")] string PatchId, + [property: Key(3), CultRole("session")] string SessionId, [property: Key(4)] string Status, [property: Key(5)] string OpenedAtUtc, [property: Key(6)] int SampleRate, @@ -228,7 +229,7 @@ public sealed record AquaSynthInstrumentOpenReceipt( public sealed record AquaSynthInstrumentControlReceipt( [property: Key(0), CultName] string ReceiptId, [property: Key(1), CultIndex("command")] string CommandId, - [property: Key(2), CultIndex("session")] string SessionId, + [property: Key(2), CultRole("session")] string SessionId, [property: Key(3)] string Status, [property: Key(4)] string AppliedAtUtc, [property: Key(5)] int ControlCount, @@ -240,7 +241,7 @@ public sealed record AquaSynthInstrumentControlReceipt( public sealed record AquaSynthInstrumentBlockReceipt( [property: Key(0), CultName] string BlockId, [property: Key(1), CultIndex("command")] string CommandId, - [property: Key(2), CultIndex("session")] string SessionId, + [property: Key(2), CultRole("session")] string SessionId, [property: Key(3)] string Status, [property: Key(4)] string CompletedAtUtc, [property: Key(5)] ulong Sequence, @@ -258,7 +259,7 @@ public sealed record AquaSynthInstrumentBlockReceipt( public sealed record AquaSynthInstrumentCloseReceipt( [property: Key(0), CultName] string ReceiptId, [property: Key(1), CultIndex("command")] string CommandId, - [property: Key(2), CultIndex("session")] string SessionId, + [property: Key(2), CultRole("session")] string SessionId, [property: Key(3)] string Status, [property: Key(4)] string ClosedAtUtc, [property: Key(5)] bool Released, @@ -276,7 +277,8 @@ public sealed record AquaSynthCultMeshStreamDescriptor( [property: Key(6)] int Channels, [property: Key(7)] int FramesPerPacket, [property: Key(8)] string[] PreferredTransports, - [property: Key(9)] string? MetadataSchemaId); + [property: Key(9)] string? MetadataSchemaId, + [property: Key(10), CultElementId("StreamId")] string ElementId = ""); [MessagePackObject] public sealed record AquaSynthAutomationPacketReceipt( @@ -289,7 +291,8 @@ public sealed record AquaSynthAutomationPacketReceipt( [property: Key(6)] string Float32Uri, [property: Key(7)] string ContentHash, [property: Key(8)] string PageRef, - [property: Key(9)] int ControlCount); + [property: Key(9)] int ControlCount, + [property: Key(10), CultElementId("Sequence")] string ElementId = ""); [CultDocument("aquasynth.operator_state", AquaSynthDaemonSchemas.OperatorState)] [CultGlobal] @@ -556,9 +559,9 @@ public sealed class AquaSynthDaemonService : IDisposable session.Stream.ProcessBlock([], outputs, command.FrameCount, session.Controls); var mono = MixMono(outputs, command.FrameCount, command.Gain * session.Gain); var blockId = BlockId(command.CommandId); - var blockDir = Path.Combine(options.StoreRoot, "live", SafeFileName(command.SessionId)); + var blockDir = Path.Combine(options.StoreRoot, "live", StoreFileName.From(command.SessionId)); Directory.CreateDirectory(blockDir); - var floatPath = Path.Combine(blockDir, $"{SafeFileName(blockId)}.f32"); + var floatPath = Path.Combine(blockDir, $"{StoreFileName.From(blockId)}.f32"); await WriteFloat32Async(floatPath, mono).ConfigureAwait(false); var sequence = session.NextSequence(); @@ -661,8 +664,9 @@ public sealed class AquaSynthDaemonService : IDisposable var renderId = RenderId(command.CommandId); var sampleDir = Path.Combine(options.StoreRoot, "samples"); Directory.CreateDirectory(sampleDir); - var floatPath = Path.Combine(sampleDir, $"{renderId}.f32"); - var wavPath = Path.Combine(sampleDir, $"{renderId}.wav"); + var sampleStem = StoreFileName.From(renderId); + var floatPath = Path.Combine(sampleDir, $"{sampleStem}.f32"); + var wavPath = Path.Combine(sampleDir, $"{sampleStem}.wav"); await WriteFloat32Async(floatPath, samples).ConfigureAwait(false); await WriteWavAsync(wavPath, samples, patch.Manifest.SampleRate).ConfigureAwait(false); @@ -703,8 +707,8 @@ public sealed class AquaSynthDaemonService : IDisposable var runId = $"{command.CommandId}-stream"; var verseId = "aquasynth.instrument"; - var audioStreamId = $"aquasynth.audio.{SafeFileName(command.CommandId)}"; - var controlStreamId = $"aquasynth.controls.{SafeFileName(command.CommandId)}"; + var audioStreamId = $"aquasynth.audio.{StoreFileName.From(command.CommandId)}"; + var controlStreamId = $"aquasynth.controls.{StoreFileName.From(command.CommandId)}"; if (!string.Equals(compile.Status, "succeeded", StringComparison.Ordinal)) { @@ -741,7 +745,7 @@ public sealed class AquaSynthDaemonService : IDisposable .GroupBy(frame => frame.Block) .ToDictionary(group => group.Key, group => NormalizeControls(group.Last().Controls), EqualityComparer.Default); - var streamDir = Path.Combine(options.StoreRoot, "streams", SafeFileName(runId)); + var streamDir = Path.Combine(options.StoreRoot, "streams", StoreFileName.From(runId)); Directory.CreateDirectory(streamDir); var packets = new List(); using (var stream = patch.CreateStreamingPatch()) @@ -905,7 +909,7 @@ public sealed class AquaSynthDaemonService : IDisposable try { var faustName = AquaSynthNativeCompiler.SafeFaustName(identity.FaustName); - var source = FaustEmitter.EmitScript(identity.Script, new FaustExportOptions(faustName)).Source; + var source = FaustEmitter.EmitScript(identity.Script, new FaustExportOptions(faustName, SampleRate: options.SampleRate)).Source; return compiler.TryCompileSource(identity, source, durationSeconds, out patch, out error); } catch (Exception ex) @@ -918,7 +922,7 @@ public sealed class AquaSynthDaemonService : IDisposable private async Task WriteReceiptAsync(string family, string id, T document, bool global = false) where T : class { - var filePath = Path.Combine(options.StoreRoot, family, $"{SafeFileName(id)}.cc"); + var filePath = Path.Combine(options.StoreRoot, family, $"{StoreFileName.From(id)}.cc"); Directory.CreateDirectory(Path.GetDirectoryName(filePath)!); // Opening hydrates; a receipt file holds exactly one document, so everything else goes before the write. using var cache = CultCacheMessagePack.Create(filePath); @@ -1129,21 +1133,15 @@ public sealed class AquaSynthDaemonService : IDisposable private static string BlockId(string commandId) => $"{commandId}-block"; - private static string SessionId(string patchId, string compileKey) => $"{SafeFileName(patchId)}-{compileKey[..Math.Min(12, compileKey.Length)]}"; + private static string SessionId(string patchId, string compileKey) => $"{StoreFileName.From(patchId)}-{compileKey[..Math.Min(12, compileKey.Length)]}"; - private static string SafeFileName(string value) + // `C:\a\b` -> file:///C:/a/b and `/a/b` -> file:///a/b; a bare "file:///" prefix made POSIX paths file:////a/b, which Uri reads as a UNC host. + private static string ToArtifactUri(string path) { - var builder = new StringBuilder(value.Length); - foreach (var c in value) - { - builder.Append(char.IsLetterOrDigit(c) || c is '-' or '_' or '.' ? c : '-'); - } - - return builder.ToString().Trim('-'); + var full = Path.GetFullPath(path).Replace('\\', '/'); + return full.StartsWith('/') ? $"file://{full}" : $"file:///{full}"; } - private static string ToArtifactUri(string path) => $"file:///{Path.GetFullPath(path).Replace('\\', '/')}"; - private static string Now() => DateTimeOffset.UtcNow.ToString("O"); private sealed class LiveInstrumentSession : IDisposable diff --git a/src/AquaSynth.Faust/FaustCompiler.cs b/src/AquaSynth.Faust/FaustCompiler.cs index b2b197d..a6efc54 100644 --- a/src/AquaSynth.Faust/FaustCompiler.cs +++ b/src/AquaSynth.Faust/FaustCompiler.cs @@ -22,7 +22,8 @@ public sealed record FaustValidation(string Command, bool Success, int? StatusCo public sealed record FaustWebAssemblyCompileOptions( string OutputDirectory, string BaseName = "aquasynth_patch", - bool Stereo = false); + bool Stereo = false, + int SampleRate = 48000); public sealed record FaustWebAssemblyManifest( string Name, @@ -89,7 +90,7 @@ public static class FaustCompiler string? faustPath = null, CancellationToken cancellationToken = default) { - var export = FaustEmitter.Emit(patch, new FaustExportOptions(options.BaseName, options.Stereo)); + var export = FaustEmitter.Emit(patch, new FaustExportOptions(options.BaseName, options.Stereo, SampleRate: options.SampleRate)); return await CompileWebAssemblySourceAsync(export.Source, options, faustPath, cancellationToken); } @@ -99,7 +100,7 @@ public static class FaustCompiler string? faustPath = null, CancellationToken cancellationToken = default) { - var export = FaustEmitter.EmitScript(script, new FaustExportOptions(options.BaseName, options.Stereo)); + var export = FaustEmitter.EmitScript(script, new FaustExportOptions(options.BaseName, options.Stereo, SampleRate: options.SampleRate)); return await CompileWebAssemblySourceAsync(export.Source, options, faustPath, cancellationToken); } @@ -222,12 +223,8 @@ public static class FaustCompiler _ => throw new ArgumentOutOfRangeException(nameof(language), language, null) }; - private static string SafeArtifactName(string value) - { - var sanitized = new string(value.Select(character => - char.IsAsciiLetterOrDigit(character) || character is '-' or '_' ? character : '_').ToArray()); - return string.IsNullOrWhiteSpace(sanitized) ? "aquasynth_patch" : sanitized; - } + private static string SafeArtifactName(string value) => + string.IsNullOrWhiteSpace(value) ? "aquasynth_patch" : StoreFileName.From(value); private static async Task FaustArchDirAsync(string faustPath, CancellationToken cancellationToken) { diff --git a/src/AquaSynth.Faust/FaustRenderedSpeechLossSurface.cs b/src/AquaSynth.Faust/FaustRenderedSpeechLossSurface.cs index 03f9341..2c85b65 100644 --- a/src/AquaSynth.Faust/FaustRenderedSpeechLossSurface.cs +++ b/src/AquaSynth.Faust/FaustRenderedSpeechLossSurface.cs @@ -93,7 +93,7 @@ public sealed class FaustRenderedSpeechLossSurface(FaustRenderedSpeechLossOption source.AppendLine(VoiceLine("sine", F(hz), F(gain), target, 0, tract)); } - return FaustEmitter.EmitScript(source.ToString(), new FaustExportOptions("aquasynth_speech_loss")).Source; + return FaustEmitter.EmitScript(source.ToString(), new FaustExportOptions("aquasynth_speech_loss", SampleRate: options.SampleRate)).Source; } private async Task EstimateGradientAsync( diff --git a/src/AquaSynth.Faust/IpaTrialOrchestrator.cs b/src/AquaSynth.Faust/IpaTrialOrchestrator.cs index 4f33bf4..5d4e178 100644 --- a/src/AquaSynth.Faust/IpaTrialOrchestrator.cs +++ b/src/AquaSynth.Faust/IpaTrialOrchestrator.cs @@ -124,7 +124,7 @@ public static class IpaTrialOrchestrator throw new ArgumentException("At least one IPA trial target set is required.", nameof(trialSets)); } - var batchDirectory = Path.Combine(artifactRoot, options.BatchId); + var batchDirectory = StoreFileName.Combine(artifactRoot, options.BatchId); Directory.CreateDirectory(batchDirectory); var storePath = Path.Combine(batchDirectory, options.TrialResultStoreFileName); var referenceRenderer = new PinkTromboneReferenceRenderer(); @@ -139,7 +139,8 @@ public static class IpaTrialOrchestrator targetSet.Id, targetSet.Targets.Select(target => target.Target), targetSet.Variants, - options.TimelineBlocks); + options.TimelineBlocks, + options.SampleRate); var analysis = IpaGestureExperiment.AnalyzeRound(round); var targetById = targetSet.Targets.ToDictionary(target => target.Target.Id, StringComparer.OrdinalIgnoreCase); @@ -156,12 +157,13 @@ public static class IpaTrialOrchestrator var candidateDir = Path.Combine(round.RoundDirectory, "audio", candidate.Id); Directory.CreateDirectory(candidateDir); var script = await File.ReadAllTextAsync(candidate.ScriptPath, cancellationToken).ConfigureAwait(false); - var source = FaustEmitter.EmitScript(script, new FaustExportOptions(SafeName(candidate.Id))).Source; + var referenceFixture = PinkTromboneParityFixtures.ById(target.ReferenceFixtureId); + var reference = referenceRenderer.Render(referenceFixture.Controls, target.Target.DurationSeconds + .18f); + // The vocal ladder's cell length is tied to the rate the DSP runs at, which is the reference's rate. + var source = FaustEmitter.EmitScript(script, new FaustExportOptions(candidate.Id, SampleRate: reference.SampleRate)).Source; var dspPath = Path.Combine(candidateDir, "candidate.dsp"); await File.WriteAllTextAsync(dspPath, source, cancellationToken).ConfigureAwait(false); - var referenceFixture = PinkTromboneParityFixtures.ById(target.ReferenceFixtureId); - var reference = referenceRenderer.Render(referenceFixture.Controls, target.Target.DurationSeconds + .18f); var candidateRender = await FaustCompiler.RenderAsync( source, new FaustRenderOptions(reference.SampleRate, reference.Samples.Length / (float)reference.SampleRate), @@ -274,7 +276,7 @@ public static class IpaTrialOrchestrator throw new ArgumentException("At least one IPA script candidate is required.", nameof(candidates)); } - var batchDirectory = Path.Combine(artifactRoot, options.BatchId); + var batchDirectory = StoreFileName.Combine(artifactRoot, options.BatchId); var candidateRoot = Path.Combine(batchDirectory, "agent-candidates"); var timelineRoot = Path.Combine(batchDirectory, "agent-timelines"); Directory.CreateDirectory(candidateRoot); @@ -298,7 +300,7 @@ public static class IpaTrialOrchestrator var startedAt = DateTimeOffset.UtcNow; var startedStamp = Stopwatch.GetTimestamp(); - var safeCandidateId = SafeName(candidate.CandidateId); + var safeCandidateId = StoreFileName.From(candidate.CandidateId); var candidateDir = Path.Combine(candidateRoot, safeCandidateId); Directory.CreateDirectory(candidateDir); var audioDir = Path.Combine(candidateDir, "audio"); @@ -307,16 +309,16 @@ public static class IpaTrialOrchestrator File.Copy(candidate.ScriptPath, scriptCopy, overwrite: true); var script = await File.ReadAllTextAsync(scriptCopy, cancellationToken).ConfigureAwait(false); var patch = PatchScript.Parse(script); - var timeline = ProbeTimelineReport.Build(patch, "voice", Math.Max(1, options.TimelineBlocks)); + var referenceFixture = PinkTromboneParityFixtures.ById(mapped.target.ReferenceFixtureId); + var reference = referenceRenderer.Render(referenceFixture.Controls, mapped.target.Target.DurationSeconds + .18f); + var timeline = ProbeTimelineReport.Build(patch, "voice", Math.Max(1, options.TimelineBlocks), reference.SampleRate); var timelinePath = Path.Combine(timelineRoot, $"{safeCandidateId}.csv"); await File.WriteAllTextAsync(timelinePath, ProbeTimelineReport.ToCsv(timeline), cancellationToken).ConfigureAwait(false); - var source = FaustEmitter.EmitScript(script, new FaustExportOptions(safeCandidateId)).Source; + var source = FaustEmitter.EmitScript(script, new FaustExportOptions(safeCandidateId, SampleRate: reference.SampleRate)).Source; var dspPath = Path.Combine(candidateDir, "candidate.dsp"); await File.WriteAllTextAsync(dspPath, source, cancellationToken).ConfigureAwait(false); - var referenceFixture = PinkTromboneParityFixtures.ById(mapped.target.ReferenceFixtureId); - var reference = referenceRenderer.Render(referenceFixture.Controls, mapped.target.Target.DurationSeconds + .18f); var candidateRender = await FaustCompiler.RenderAsync( source, new FaustRenderOptions(reference.SampleRate, reference.Samples.Length / (float)reference.SampleRate), @@ -458,7 +460,7 @@ public static class IpaTrialOrchestrator IReadOnlyList timeline) { var tokens = DescriptorTokens(target.Descriptor); - var expected = ExpectedSurfaces(tokens); + var expected = ExpectedSurfaces(tokens, target.Descriptor); var touched = patch.ControlSplines .Where(spline => spline.Enabled) .Select(spline => spline.SurfacePath) @@ -477,7 +479,7 @@ public static class IpaTrialOrchestrator return Math.Clamp(.5f * coverage + .25f * motion + .25f * primitive, 0, 1); } - private static string[] ExpectedSurfaces(HashSet tokens) + private static string[] ExpectedSurfaces(HashSet tokens, string descriptor) { var surfaces = new List { @@ -490,11 +492,13 @@ public static class IpaTrialOrchestrator { surfaces.AddRange( [ - "/vocal/areas/0/area/tongue_index", - "/vocal/areas/0/area/tongue_diameter", "/vocal/areas/0/area/lip_opening", "/vocal/radiation/0/aperture" ]); + if (StoryVowelAreas.IdForDescriptor(descriptor) is { } vowelId) + { + surfaces.Add($"/vocal/areas/0/area/{StoryVowelAreas.SurfaceField(vowelId)}"); + } } else { @@ -721,9 +725,6 @@ public static class IpaTrialOrchestrator return samples; } - private static string SafeName(string value) => - new(value.Select(character => char.IsLetterOrDigit(character) ? character : '_').ToArray()); - private static string Escape(string value) => value.Contains(',') || value.Contains('"') || value.Contains('\n') || value.Contains('\r') ? $"\"{value.Replace("\"", "\"\"", StringComparison.Ordinal)}\"" diff --git a/src/AquaSynth.Faust/NativeFaustRuntime.cs b/src/AquaSynth.Faust/NativeFaustRuntime.cs index bf707ef..b84e3a8 100644 --- a/src/AquaSynth.Faust/NativeFaustRuntime.cs +++ b/src/AquaSynth.Faust/NativeFaustRuntime.cs @@ -511,7 +511,7 @@ public sealed class AquaSynthNativeCompiler : IDisposable ObjectDisposedException.ThrowIf(disposed, this); var patch = PatchScript.Parse(identity.Script); var faustName = SafeFaustName(identity.FaustName); - var export = FaustEmitter.Emit(patch, new FaustExportOptions(faustName)); + var export = FaustEmitter.Emit(patch, new FaustExportOptions(faustName, SampleRate: options.SampleRate)); var duration = EstimateDuration(patch, options); var sourcePath = WriteDspSource(identity.Id, export.Source, options.DspSourceDirectory); return CompileSource(identity, faustName, export.Source, duration, sourcePath, ControlSurfaceCatalog.FromPatch(patch)); @@ -633,18 +633,10 @@ public sealed class AquaSynthNativeCompiler : IDisposable } Directory.CreateDirectory(outputRoot); - var path = Path.Combine(outputRoot, $"{SafeFileName(patchId)}.dsp"); + var path = Path.Combine(outputRoot, $"{StoreFileName.From(patchId)}.dsp"); File.WriteAllText(path, source); return path; } - - private static string SafeFileName(string value) - { - var invalid = Path.GetInvalidFileNameChars().ToHashSet(); - var chars = value.Select(ch => invalid.Contains(ch) ? '_' : ch).ToArray(); - var name = new string(chars).Trim(); - return string.IsNullOrWhiteSpace(name) ? "aquasynth_patch" : name; - } } public static class AquaSynthFaustToolchain @@ -840,16 +832,32 @@ internal sealed class FaustNativeToolchain : IDisposable public void SetControls(IntPtr dsp, IReadOnlyDictionary controls, IReadOnlyDictionary map) { - foreach (var (path, value) in controls) + // Dictionary's struct enumerator avoids boxing one through the interface on every audio block. + if (controls is Dictionary dictionary) { - if (!map.TryGetValue(path, out var zone) && - !map.TryGetValue(path.TrimStart('/'), out zone)) + foreach (var (path, value) in dictionary) { - throw new ArgumentException($"Faust control `{path}` was not exported by the compiled patch.", nameof(controls)); + WriteControl(map, path, value); } - Marshal.Copy([value], 0, zone, 1); + return; } + + foreach (var (path, value) in controls) + { + WriteControl(map, path, value); + } + } + + private static void WriteControl(IReadOnlyDictionary map, string path, float value) + { + if (!map.TryGetValue(path, out var zone) && + !map.TryGetValue(path.TrimStart('/'), out zone)) + { + throw new ArgumentException($"Faust control `{path}` was not exported by the compiled patch.", "controls"); + } + + Marshal.WriteInt32(zone, BitConverter.SingleToInt32Bits(value)); } public void Dispose() diff --git a/tests/AquaSynth.Dsl.Tests/AquaSynthDaemonServiceTests.cs b/tests/AquaSynth.Dsl.Tests/AquaSynthDaemonServiceTests.cs index 129491f..7a7f6ca 100644 --- a/tests/AquaSynth.Dsl.Tests/AquaSynthDaemonServiceTests.cs +++ b/tests/AquaSynth.Dsl.Tests/AquaSynthDaemonServiceTests.cs @@ -4,6 +4,7 @@ using GameCult.Caching.MessagePack; namespace AquaSynth.Dsl.Tests; +[Collection("NativeFaust")] public sealed class AquaSynthDaemonServiceTests { [Fact] @@ -52,7 +53,7 @@ public sealed class AquaSynthDaemonServiceTests Assert.True(File.Exists(UriToPath(result.RenderReceipt.Float32Uri))); Assert.True(File.Exists(UriToPath(result.RenderReceipt.WavUri))); Assert.Equal(result.RenderReceipt.SampleCount * sizeof(float), new FileInfo(UriToPath(result.RenderReceipt.Float32Uri)).Length); - Assert.True(File.Exists(Path.Combine(storeRoot, "sessions", $"{result.CompileReceipt.SessionId}.cc"))); + Assert.True(File.Exists(Path.Combine(storeRoot, "sessions", $"{StoreFileName.From(result.CompileReceipt.SessionId)}.cc"))); } [Fact] @@ -154,7 +155,7 @@ public sealed class AquaSynthDaemonServiceTests Assert.True(File.Exists(UriToPath(quiet.Float32Uri))); Assert.True(File.Exists(UriToPath(loud.Float32Uri))); Assert.Equal(128 * sizeof(float), new FileInfo(UriToPath(loud.Float32Uri)).Length); - Assert.True(File.Exists(Path.Combine(storeRoot, "live", $"{close.ReceiptId}.cc"))); + Assert.True(File.Exists(Path.Combine(storeRoot, "live", $"{StoreFileName.From(close.ReceiptId)}.cc"))); var liveState = await ReadLiveSessionStateAsync(storeRoot, open.SessionId); Assert.Equal(open.SessionId, liveState.SessionId); Assert.Equal("closed", liveState.Status); @@ -210,7 +211,7 @@ public sealed class AquaSynthDaemonServiceTests private static async Task ReadLiveSessionStateAsync(string storeRoot, string sessionId) { using var cache = await CultCacheMessagePack.OpenAsync( - Path.Combine(storeRoot, "live-sessions", $"{sessionId}.cc")).ConfigureAwait(false); + Path.Combine(storeRoot, "live-sessions", $"{StoreFileName.From(sessionId)}.cc")).ConfigureAwait(false); return Assert.Single(cache.GetAll()); } } diff --git a/tests/AquaSynth.Dsl.Tests/CultCachePatchDocumentTests.cs b/tests/AquaSynth.Dsl.Tests/CultCachePatchDocumentTests.cs index 2f55645..1c27b12 100644 --- a/tests/AquaSynth.Dsl.Tests/CultCachePatchDocumentTests.cs +++ b/tests/AquaSynth.Dsl.Tests/CultCachePatchDocumentTests.cs @@ -83,7 +83,8 @@ public sealed class CultCachePatchDocumentTests Path.Combine(RepositoryRoot(), "patches", "aquasynth-patch-cultcache.cc")); var fromCatalog = CultCachePatchDocumentCatalog.CreateDefault(); - Assert.Equal(fromCatalog.PatchDatabase.Entries, fromFile.PatchDatabase.Entries); + // The checked-in file predates element ids; a load mints them (derived from Path), so they are not part of the catalog's content. + Assert.Equal(fromCatalog.PatchDatabase.Entries, fromFile.PatchDatabase.Entries.Select(entry => entry with { ElementId = "" })); Assert.Equal(fromCatalog.PatchClaims.Select(claim => claim.Id), fromFile.PatchClaims.Select(claim => claim.Id)); Assert.Equal(fromCatalog.SpeechClaims.Select(claim => claim.Id), fromFile.SpeechClaims.Select(claim => claim.Id)); } diff --git a/tests/AquaSynth.Dsl.Tests/CultIndexUniquenessTests.cs b/tests/AquaSynth.Dsl.Tests/CultIndexUniquenessTests.cs new file mode 100644 index 0000000..24afc9d --- /dev/null +++ b/tests/AquaSynth.Dsl.Tests/CultIndexUniquenessTests.cs @@ -0,0 +1,110 @@ +using System.Reflection; +using AquaSynth.Dsl; +using GameCult.Caching; +using GameCult.Caching.MessagePack; + +namespace AquaSynth.Dsl.Tests; + +// CultLib #90 (7760219b): [CultIndex] means unique, enforced at commit and load; [CultRole] names a many-row selection role. +// A member that two rows can share must be a role. The only [CultIndex] left is the CommandId of a receipt: every receipt is stored +// under a key derived from its CommandId alone (AquaSynthCultNetDaemon.*ReceiptKey), so a second row with that CommandId replaces the first. +public sealed class CultIndexUniquenessTests +{ + private static readonly string[] ReceiptTypes = + [ + nameof(AquaSynthPatchCompileReceipt), nameof(AquaSynthRenderSampleReceipt), nameof(AquaSynthAutomationStreamReceipt), nameof(AquaSynthInstrumentOpenReceipt), + nameof(AquaSynthInstrumentControlReceipt), nameof(AquaSynthInstrumentBlockReceipt), nameof(AquaSynthInstrumentCloseReceipt), + ]; + + [Fact] + public void TheOnlyCultIndexMembersAreReceiptCommandIds() + { + var indexed = new[] { typeof(AquaSynthDaemonService).Assembly, typeof(SpeechRenderResult).Assembly }.Distinct() + .SelectMany(assembly => assembly.GetTypes()) + .SelectMany(type => type.GetMembers(BindingFlags.Public | BindingFlags.Instance) + .Where(member => member is PropertyInfo or FieldInfo && member.GetCustomAttribute() is not null) + .Select(member => $"{type.Name}.{member.Name}")) + .OrderBy(name => name, StringComparer.Ordinal).ToArray(); + Assert.Equal(ReceiptTypes.Select(name => $"{name}.CommandId").OrderBy(name => name, StringComparer.Ordinal), indexed); + } + + [Fact] + public async Task ManyRowsMaySharePatchAndSessionAcrossDaemonDocuments() + { + var path = TempFile(); + try + { + using (var cache = await CultCacheMessagePack.OpenAsync(path)) + { + string[] paths = []; + for (var i = 0; i < 3; i++) + { + // Three commands, one patch, one session: the shape a client that opens, drives and closes one instrument produces. + await Put(cache, $"cmd-{i}-control", new AquaSynthInstrumentControlCommand($"cmd-{i}-control", "session-1", new Dictionary())); + await Put(cache, $"cmd-{i}-block", new AquaSynthInstrumentBlockCommand($"cmd-{i}-block", "session-1")); + await Put(cache, $"cmd-{i}-close", new AquaSynthInstrumentCloseCommand($"cmd-{i}-close", "session-1")); + await Put(cache, $"sess-{i}", new AquaSynthCompiledInstrumentSession($"sess-{i}", "patch-1", $"key-{i}", "f", 1, 48000, 0, 1, 128, 0.1f, "t", paths, paths)); + await Put(cache, $"live-{i}", new AquaSynthLiveInstrumentSessionState($"live-{i}", "patch-1", "open", "t", "t", "", 48000, 0, 1, 128, 1f, 0, new Dictionary(), "", "")); + await Put(cache, $"cmd-{i}-compile", new AquaSynthPatchCompileReceipt($"cmd-{i}-compile", $"cmd-{i}", "patch-1", "session-1", "succeeded", "t", "k", 1, paths, paths)); + await Put(cache, $"cmd-{i}-render", new AquaSynthRenderSampleReceipt($"cmd-{i}-render", $"cmd-{i}", "patch-1", "session-1", "succeeded", "t", 48000, 1, 0.1f, 0, 0, "", "", "")); + await Put(cache, $"cmd-{i}-stream", new AquaSynthAutomationStreamReceipt($"cmd-{i}-stream", $"cmd-{i}", "patch-1", "session-1", "succeeded", "t", "v", "a", "c", 48000, 128, 1, [], [])); + await Put(cache, $"cmd-{i}-open", new AquaSynthInstrumentOpenReceipt($"cmd-{i}-open", $"cmd-{i}", "patch-1", "session-1", "succeeded", "t", 48000, 0, 1, 128, paths, paths)); + await Put(cache, $"cmd-{i}-ctl", new AquaSynthInstrumentControlReceipt($"cmd-{i}-ctl", $"cmd-{i}", "session-1", "applied", "t", 0)); + await Put(cache, $"cmd-{i}-blk", new AquaSynthInstrumentBlockReceipt($"cmd-{i}-blk", $"cmd-{i}", "session-1", "succeeded", "t", 0, 48000, 128, 0, 0, "", "")); + await Put(cache, $"cmd-{i}-cls", new AquaSynthInstrumentCloseReceipt($"cmd-{i}-cls", $"cmd-{i}", "session-1", "closed", "t", true)); + } + + await cache.FlushAsync(); + } + + // Reopening hydrates and revalidates: a unique index would refuse the file here. + using var reloaded = await CultCacheMessagePack.OpenAsync(path); + Assert.Equal(3, reloaded.GetAll().Count()); + Assert.Equal(3, reloaded.GetAll().Count(session => session.PatchId == "patch-1")); + Assert.Equal(3, reloaded.GetAll().Count(receipt => receipt.SessionId == "session-1" && receipt.PatchId == "patch-1")); + Assert.Equal(3, reloaded.GetAll().Count(receipt => receipt.SessionId == "session-1")); + } + finally + { + File.Delete(path); + } + } + + [Fact] + public async Task OneRowPerCommandIdPerReceiptType() + { + var path = TempFile(); + try + { + using var cache = await CultCacheMessagePack.OpenAsync(path); + string[] paths = []; + AquaSynthPatchCompileReceipt Receipt(string command, string status) => + new(AquaSynthCultNetDaemon.CompileReceiptKey(command).Value, command, "patch-1", "session-1", status, "t", "k", 1, paths, paths); + foreach (var (command, status) in new[] { ("a", "failed"), ("a", "succeeded"), ("b", "succeeded") }) + { + var receipt = Receipt(command, status); + await cache.UpsertAsync(receipt, new CultRecordHandle(AquaSynthCultNetDaemon.CompileReceiptKey(command))); + } + + // A retried command replaces its own receipt; it cannot add a second row that carries the same CommandId. + var all = cache.GetAll().OrderBy(receipt => receipt.CommandId, StringComparer.Ordinal).ToArray(); + Assert.Equal(["a", "b"], all.Select(receipt => receipt.CommandId)); + Assert.Equal("succeeded", all[0].Status); + foreach (var kind in new Func[] { AquaSynthCultNetDaemon.CompileReceiptKey, AquaSynthCultNetDaemon.RenderReceiptKey, AquaSynthCultNetDaemon.StreamReceiptKey, + AquaSynthCultNetDaemon.OpenReceiptKey, AquaSynthCultNetDaemon.ControlReceiptKey, AquaSynthCultNetDaemon.BlockReceiptKey, AquaSynthCultNetDaemon.CloseReceiptKey }) + { + Assert.NotEqual(kind("a"), kind("b")); + Assert.Equal(kind("a"), kind("a")); + } + } + finally + { + File.Delete(path); + } + } + + private static Task Put(CultCache cache, string key, T document) where T : class => + cache.UpsertAsync(document, new CultRecordHandle(new CultRecordKey(key))); + + private static string TempFile() => Path.Combine(Path.GetTempPath(), $"aquasynth-cultindex-{Guid.NewGuid():N}.cc"); +} diff --git a/tests/AquaSynth.Dsl.Tests/DingCatalogTests.cs b/tests/AquaSynth.Dsl.Tests/DingCatalogTests.cs index 4c45a01..126b93f 100644 --- a/tests/AquaSynth.Dsl.Tests/DingCatalogTests.cs +++ b/tests/AquaSynth.Dsl.Tests/DingCatalogTests.cs @@ -1,3 +1,4 @@ +using System.Text.Json; using AquaSynth.Dings; namespace AquaSynth.Dsl.Tests; @@ -8,6 +9,34 @@ public sealed class DingCatalogTests public void CuratedInstrumentsStayDistinctAndInsidePleasantnessEnvelope() => Assert.Empty(DingCatalog.ValidateCuration()); + [Fact] + public void PlayRejectionIsTheHostGateWithPinnedTexts() + { + Assert.Null(DingCatalog.PlayRejection("task.complete", "warm-bell", 0)); + Assert.Null(DingCatalog.PlayRejection("task.complete", "warm-bell", 1)); + Assert.Equal("Unknown event 'nope'.", DingCatalog.PlayRejection("nope", "warm-bell", .5f)); + Assert.Equal("Unknown instrument 'nope'.", DingCatalog.PlayRejection("task.complete", "nope", .5f)); + Assert.Equal("Unknown event 'nope'.", DingCatalog.PlayRejection("nope", "nope", 2f)); + foreach (var gain in new[] { -.01f, 1.01f, float.NaN, float.PositiveInfinity }) + Assert.Equal("Gain must be between 0 and 1.", DingCatalog.PlayRejection("task.complete", "warm-bell", gain)); + } + + [Fact] + public void HostCallsTheGateAndOwnsNoSecondCopy() + { + var raw = File.ReadAllText(Path.Combine(RepositoryRoot(), "tools", "AquaSynthDingsHost", "DingsPlaybackHost.cs")); + var host = System.Text.RegularExpressions.Regex.Replace(raw, @"//[^\n]*|/\*.*?\*/", "", System.Text.RegularExpressions.RegexOptions.Singleline); + // Source-shape check: the guard must be an unconditional early return of the gate's text. + Assert.Matches(@"if \(DingCatalog\.PlayRejection\(command\.EventId, command\.InstrumentId, command\.Value\) is \{ \} (\w+)\) return Response\(false, ""rejected"", \1\);", host); + // No second copy of the old inline conditions or texts, however the string is split. + Assert.DoesNotContain("Events.TryGetValue", host, StringComparison.Ordinal); + Assert.DoesNotContain("Instruments.TryGetValue", host, StringComparison.Ordinal); + Assert.DoesNotContain("Gain must", host, StringComparison.Ordinal); + Assert.DoesNotContain("Unknown event", host, StringComparison.Ordinal); + Assert.DoesNotContain("Unknown instrument", host, StringComparison.Ordinal); + Assert.DoesNotContain("\"Unk\"", host, StringComparison.Ordinal); + } + [Fact] public void MinimalDingsVocabularyIsComplete() { @@ -32,4 +61,73 @@ public sealed class DingCatalogTests foreach (var pair in dingEvent.Notes.Zip(dingEvent.Notes.Skip(1))) Assert.InRange(pair.Second.DelayMilliseconds - pair.First.DelayMilliseconds, 180, 320); } + + // Independent oracle: literals captured from the pre-DingCatalog.Identity derivation in AquaDaemonClient/AquaLower. + // Session ids, compile keys and store file names hang off these; they must not move without the deferred revision decision. + private static readonly Dictionary Pinned = new() + { + ["warm-bell"] = ("dings.warm-bell", "dings_warm_bell"), + ["glass-chime"] = ("dings.glass-chime", "dings_glass_chime"), + ["soft-triangle"] = ("dings.soft-triangle", "dings_soft_triangle"), + ["muted-pluck"] = ("dings.muted-pluck", "dings_muted_pluck"), + }; + private static Dictionary PinnedFaustNames => Pinned.ToDictionary(x => x.Key, x => x.Value.FaustName); + + [Fact] + public void IdentityPinsTheOldDerivedValuesForAllFourInstruments() + { + Assert.Equal(Pinned.Keys.Order(), DingCatalog.Instruments.Keys.Order()); + foreach (var instrument in DingCatalog.Instruments.Values) + { + var identity = DingCatalog.Identity(instrument); + Assert.Equal(Pinned[instrument.Id].PatchId, identity.PatchId); + Assert.Equal(Pinned[instrument.Id].FaustName, identity.FaustName); + Assert.Equal(1, identity.Revision); + } + } + + // tools/aqua-feel measures the lowered files in tools/aqua-feel/lowered-dings; regenerate them with + // `dotnet run --project tools/AquaLower -- --dings tools/aqua-feel/lowered-dings` when the catalog changes. + [Fact] + public void LoweredDingsFedToTheFeelMeterMatchTheCatalog() + { + var dir = Path.Combine(RepositoryRoot(), "tools", "aqua-feel", "lowered-dings"); + foreach (var instrument in DingCatalog.Instruments.Values) + { + var expected = FaustEmitter.EmitScript(instrument.Script, new FaustExportOptions(PinnedFaustNames[instrument.Id])).Source; + Assert.Equal(expected, File.ReadAllText(Path.Combine(dir, instrument.Id + ".dsp"))); + } + + using var doc = JsonDocument.Parse(File.ReadAllText(Path.Combine(dir, "dings.json"))); + var instruments = doc.RootElement.GetProperty("instruments").EnumerateArray().ToDictionary(e => e.GetProperty("id").GetString()!); + Assert.Equal(DingCatalog.Instruments.Keys.Order(), instruments.Keys.Order()); + foreach (var instrument in DingCatalog.Instruments.Values) + Assert.Equal(instrument.RootFrequency, instruments[instrument.Id].GetProperty("rootFrequency").GetSingle()); + var events = doc.RootElement.GetProperty("events").EnumerateArray().ToDictionary(e => e.GetProperty("id").GetString()!); + Assert.Equal(DingCatalog.Events.Keys.Order(), events.Keys.Order()); + foreach (var ev in DingCatalog.Events.Values) + { + var notes = events[ev.Id].GetProperty("notes").EnumerateArray().Select(n => (n.GetProperty("semitones").GetDouble(), n.GetProperty("delayMilliseconds").GetInt32(), n.GetProperty("gain").GetSingle())).ToArray(); + Assert.Equal(ev.Notes.Select(n => (n.Semitones, n.DelayMilliseconds, n.Gain)).ToArray(), notes); + } + } + + // Negative check on callers: reverting either call site to an inline rule leaves the catalog tests green, so read the sources. + [Theory] + [InlineData("tools/AquaSynthDingsHost/AquaDaemonClient.cs")] + [InlineData("tools/AquaLower/Program.cs")] + public void CallersDeriveIdentityOnlyThroughTheCatalog(string relativePath) + { + var source = File.ReadAllText(Path.Combine(RepositoryRoot(), relativePath)); + Assert.Contains("DingCatalog.Identity(", source, StringComparison.Ordinal); + Assert.DoesNotContain("\"dings_\"", source, StringComparison.Ordinal); + Assert.DoesNotContain("$\"dings", source, StringComparison.Ordinal); + } + + private static string RepositoryRoot() + { + var dir = new DirectoryInfo(AppContext.BaseDirectory); + while (dir is not null && !File.Exists(Path.Combine(dir.FullName, "AquaSynth.sln"))) dir = dir.Parent; + return dir?.FullName ?? throw new InvalidOperationException("AquaSynth.sln not found above the test binaries."); + } } diff --git a/tests/AquaSynth.Dsl.Tests/DingsBridgeTests.cs b/tests/AquaSynth.Dsl.Tests/DingsBridgeTests.cs index 959235d..c46f924 100644 --- a/tests/AquaSynth.Dsl.Tests/DingsBridgeTests.cs +++ b/tests/AquaSynth.Dsl.Tests/DingsBridgeTests.cs @@ -31,6 +31,20 @@ public sealed class DingsBridgeTests Assert.Equal(.7f, host.Commands.Single().Value); } + [Fact] + public async Task DeprecatedBridgeFailFastKeepsItsErrorsAndTextsAgreeWithTheHostGate() + { + var host = new FakeHostClient(); + var service = new DingService(host); + + var badEvent = await Assert.ThrowsAsync(() => service.PlayAsync("nope", "warm-bell", .5f, CancellationToken.None)); + Assert.Equal(DingCatalog.PlayRejection("nope", "warm-bell", .5f), badEvent.Message); + var badInstrument = await Assert.ThrowsAsync(() => service.PlayAsync("task.complete", "nope", .5f, CancellationToken.None)); + Assert.Equal(DingCatalog.PlayRejection("task.complete", "nope", .5f), badInstrument.Message); + await Assert.ThrowsAsync(() => service.PlayAsync("task.complete", "warm-bell", 1.5f, CancellationToken.None)); + Assert.Empty(host.Commands); + } + [Fact] public void BridgeProjectContainsNoAudioOutputOwner() { diff --git a/tests/AquaSynth.Dsl.Tests/ElementIdTests.cs b/tests/AquaSynth.Dsl.Tests/ElementIdTests.cs new file mode 100644 index 0000000..3ba1d12 --- /dev/null +++ b/tests/AquaSynth.Dsl.Tests/ElementIdTests.cs @@ -0,0 +1,104 @@ +using AquaSynth.Dsl; +using GameCult.Caching; +using GameCult.Caching.MessagePack; + +namespace AquaSynth.Dsl.Tests; + +// CultLib abd1a92e refuses to register a document whose list element type has no [CultElementId] member (CultCache.cs:1008-1010); +// opening any cache here registers every AquaSynth document type, so these tests also fail if one element type loses its id. +public sealed class ElementIdTests +{ + // Fixtures/CultCache/pre-element-ids.cc was written by the previous pin (CultLib 42dce361), before list elements had ids: + // an automation-stream command with 2 control frames and a stream receipt with 2 stream descriptors and 2 packets. + [Fact] + public async Task AStoreWrittenBeforeElementIdsReopensWithStableUniqueIds() + { + var path = CopyFixture(); + try + { + string[] first; + using (var cache = await CultCacheMessagePack.OpenAsync(path)) + { + var command = cache.GetAll().Single(); + var receipt = cache.GetAll().Single(); + Assert.Equal([0, 4], command.ControlFrames!.Select(frame => frame.Block)); + Assert.Equal(220f, command.ControlFrames![0].Controls["/freq"]); + Assert.Equal(["audio-1", "control-1"], receipt.Streams.Select(stream => stream.StreamId)); + Assert.Equal([0UL, 1UL], receipt.Packets.Select(packet => packet.Sequence)); + first = Ids(command, receipt); + Assert.All(first, id => Assert.False(string.IsNullOrEmpty(id))); + Assert.Equal(2, command.ControlFrames!.Select(frame => frame.ElementId).Distinct().Count()); + Assert.Equal(2, receipt.Packets.Select(packet => packet.ElementId).Distinct().Count()); + } + + // A second reader of the same bytes mints the same ids (load mints from the record key and element path). + using var again = await CultCacheMessagePack.OpenAsync(path); + Assert.Equal(first, Ids(again.GetAll().Single(), again.GetAll().Single())); + } + finally + { + File.Delete(path); + } + } + + [Fact] + public async Task IdsSurviveAWriteAndReload() + { + var path = CopyFixture(); + try + { + string[] first; + using (var cache = await CultCacheMessagePack.OpenAsync(path)) + { + first = Ids(cache.GetAll().Single(), cache.GetAll().Single()); + // An unrelated write rewrites the store in the current shape. + await cache.UpsertAsync(new AquaSynthInstrumentCloseCommand("c-1", "s-1"), new CultRecordHandle(new CultRecordKey("c-1"))); + await cache.FlushAsync(); + } + + using var reloaded = await CultCacheMessagePack.OpenAsync(path); + Assert.Equal(first, Ids(reloaded.GetAll().Single(), reloaded.GetAll().Single())); + } + finally + { + File.Delete(path); + } + } + + [Fact] + public async Task NewWritesKeepNaturalIdsAndDeriveThePacketIdFromItsSequence() + { + var path = Path.Combine(Path.GetTempPath(), $"aquasynth-elementid-{Guid.NewGuid():N}.cc"); + try + { + using (var cache = await CultCacheMessagePack.OpenAsync(path)) + { + var packets = new[] { new AquaSynthAutomationPacketReceipt(7, 0, 1, 128, 0.5f, 0.1f, "u", "h", "p", 0), new AquaSynthAutomationPacketReceipt(9, 1, 1, 128, 0.5f, 0.1f, "u", "h", "p", 0) }; + var streams = new[] { new AquaSynthCultMeshStreamDescriptor("audio-9", "v", "p", "audio", "f32", 48000, 1, 128, [], null) }; + await cache.UpsertAsync(new AquaSynthAutomationStreamReceipt("run", "cmd", "patch", "session", "succeeded", "t", "v", "a", "c", 48000, 128, 1, streams, packets), + new CultRecordHandle(new CultRecordKey("run"))); + await cache.FlushAsync(); + } + + using var reloaded = await CultCacheMessagePack.OpenAsync(path); + var receipt = reloaded.GetAll().Single(); + Assert.Equal(["7", "9"], receipt.Packets.Select(packet => packet.ElementId)); + Assert.Equal("audio-9", receipt.Streams.Single().StreamId); + Assert.Equal("audio-9", receipt.Streams.Single().ElementId); + } + finally + { + File.Delete(path); + } + } + + private static string[] Ids(AquaSynthAutomationStreamCommand command, AquaSynthAutomationStreamReceipt receipt) => + [.. command.ControlFrames!.Select(frame => frame.ElementId), .. receipt.Packets.Select(packet => packet.ElementId)]; + + private static string CopyFixture() + { + var path = Path.Combine(Path.GetTempPath(), $"aquasynth-preids-{Guid.NewGuid():N}.cc"); + File.Copy(Path.Combine(AppContext.BaseDirectory, "Fixtures", "CultCache", "pre-element-ids.cc"), path); + return path; + } +} diff --git a/tests/AquaSynth.Dsl.Tests/FaustRenderedSpeechLossSurfaceTests.cs b/tests/AquaSynth.Dsl.Tests/FaustRenderedSpeechLossSurfaceTests.cs index 5ee5952..d267fbc 100644 --- a/tests/AquaSynth.Dsl.Tests/FaustRenderedSpeechLossSurfaceTests.cs +++ b/tests/AquaSynth.Dsl.Tests/FaustRenderedSpeechLossSurfaceTests.cs @@ -3,6 +3,7 @@ using AquaSynth.Faust; namespace AquaSynth.Dsl.Tests; +[Collection("NativeFaust")] public sealed class FaustRenderedSpeechLossSurfaceTests { [Fact] diff --git a/tests/AquaSynth.Dsl.Tests/Fixtures/CultCache/pre-element-ids.cc b/tests/AquaSynth.Dsl.Tests/Fixtures/CultCache/pre-element-ids.cc new file mode 100644 index 0000000..6bc8bc8 Binary files /dev/null and b/tests/AquaSynth.Dsl.Tests/Fixtures/CultCache/pre-element-ids.cc differ diff --git a/tests/AquaSynth.Dsl.Tests/Fixtures/Weksa/PROVENANCE.txt b/tests/AquaSynth.Dsl.Tests/Fixtures/Weksa/PROVENANCE.txt new file mode 100644 index 0000000..4683ab0 --- /dev/null +++ b/tests/AquaSynth.Dsl.Tests/Fixtures/Weksa/PROVENANCE.txt @@ -0,0 +1 @@ +nibu-en-US.phones.json is data/phonology/fixtures/nibu-en-US.phones.json from GameCult/Weksa PR 16 (branch weksa-lowering/phones-line), commit a7bc2478e53623ce572ac6d997ba058aa7707e44, byte for byte. diff --git a/tests/AquaSynth.Dsl.Tests/Fixtures/Weksa/nibu-en-US.phones.json b/tests/AquaSynth.Dsl.Tests/Fixtures/Weksa/nibu-en-US.phones.json new file mode 100644 index 0000000..45e2e11 --- /dev/null +++ b/tests/AquaSynth.Dsl.Tests/Fixtures/Weksa/nibu-en-US.phones.json @@ -0,0 +1 @@ +{"language":"en-US","source":"ˈnibu","phones":[{"ipa":"n","stress":"none"},{"ipa":"i","stress":"primary"},{"ipa":"b","stress":"none"},{"ipa":"u","stress":"none"}]} diff --git a/tests/AquaSynth.Dsl.Tests/GlottalSourceSpectrumTests.cs b/tests/AquaSynth.Dsl.Tests/GlottalSourceSpectrumTests.cs new file mode 100644 index 0000000..d1125f6 --- /dev/null +++ b/tests/AquaSynth.Dsl.Tests/GlottalSourceSpectrumTests.cs @@ -0,0 +1,254 @@ +using AquaSynth.Dsl; +using AquaSynth.Faust; +using Xunit.Abstractions; + +namespace AquaSynth.Dsl.Tests; + +// The primitive vocal network has no resonators, so the rendered spectrum is the glottal source's own: harmonics of the +// pitch with a tilt-controlled rolloff. These tests render through the native Faust path and measure by DFT. +[Collection("NativeFaust")] +public sealed class GlottalSourceSpectrumTests(ITestOutputHelper output) +{ + private const int Rate = 48000; + private const float Pitch = 137f; + + private static string Script(float tilt, float pitch, string extra = "") => $$""" + morphology name=oral length_cm=17 diameters=.6,.8,1.2,1.5,.9 + waveguide_path name=oral_path morphology=oral loss=.998 + source_port name=folds path=oral_path pressure=.7 tension=.55 opening=.45 noise=0 impedance=.3 tilt={{tilt}} {{extra}} + radiation_load name=mouth path=oral_path aperture=.8 reflection=-.82 impedance=.28 + vocal_network name=voice paths=oral_path sources=folds radiation=mouth + vocal network=voice freq={{pitch}} gain=.2 sustain=1.5 + """; + + // One second of steady output, so every harmonic of Pitch and every other integer-Hz frequency sits on its own DFT bin. + private float[]? Render(float tilt, float pitch = Pitch, int skip = 4800, string extra = "") + { + var source = FaustEmitter.Emit(PatchScript.Parse(Script(tilt, pitch, extra)), new FaustExportOptions("glottal_spectrum")).Source; + using var compiler = new AquaSynthPatchCompiler(new AquaSynthNativeOptions(SampleRate: Rate)); + if (!compiler.TryCompileSource(new AquaSynthCompileIdentity("glottal_spectrum", "glottal_spectrum", ""), source, 1.4f, out var patch, out var error)) + { + if (error?.Contains("Faust toolchain not found", StringComparison.OrdinalIgnoreCase) == true || + error?.Contains("Faust DLL not found", StringComparison.OrdinalIgnoreCase) == true) + { + return null; + } + + Assert.Fail($"glottal spectrum compile failed: {error}"); + } + + using (patch) + { + var samples = patch!.Render(1.0f); + Assert.True(samples.Length >= Rate + skip, $"render too short: {samples.Length}"); + return samples.Skip(skip).Take(Rate).ToArray(); + } + } + + private static double Magnitude(float[] window, double hz) + { + double re = 0, im = 0; + var step = 2 * Math.PI * hz / Rate; + for (var n = 0; n < window.Length; n++) + { + re += window[n] * Math.Cos(step * n); + im -= window[n] * Math.Sin(step * n); + } + + return Math.Sqrt(re * re + im * im) * 2 / window.Length; + } + + private static double Db(double ratio) => 20 * Math.Log10(Math.Max(ratio, 1e-12)); + + private double[]? HarmonicsDb(float tilt, int count) + { + var window = Render(tilt); + if (window is null) return null; + var fundamental = Magnitude(window, Pitch); + Assert.True(fundamental > 1e-5, $"tilt {tilt}: silent fundamental {fundamental}"); + var levels = Enumerable.Range(1, count).Select(k => Db(Magnitude(window, Pitch * k) / fundamental)).ToArray(); + output.WriteLine($"tilt {tilt}: " + string.Join(" ", levels.Select(level => level.ToString("F1"))) + $" (fundamental {fundamental:E2})"); + return levels; + } + + [Fact] + public void GlottalSourceHasHarmonicsAboveTheFundamental() + { + var levels = HarmonicsDb(.5f, 8); + if (levels is null) return; + + // a sine source would leave harmonics 2..8 at the numerical floor, far below -60 dB + for (var k = 1; k < 8; k++) + { + Assert.True(levels[k] > -45, $"harmonic {k + 1} is {levels[k]:F1} dB re fundamental"); + } + } + + [Fact] + public void HigherTiltRollsHarmonicsOffMoreSteeply() + { + var bright = HarmonicsDb(.1f, 10); + var dark = HarmonicsDb(.9f, 10); + if (bright is null || dark is null) return; + + Assert.True(bright[9] - dark[9] > 12, $"harmonic 10 re fundamental: tilt .1 {bright[9]:F1} dB, tilt .9 {dark[9]:F1} dB"); + Assert.True(bright[4] - dark[4] > 6, $"harmonic 5 re fundamental: tilt .1 {bright[4]:F1} dB, tilt .9 {dark[4]:F1} dB"); + } + + [Fact] + public void BrightestHighPitchedSourceDoesNotAlias() + { + // 2744 Hz is 17.49 samples per period, and 48000 and 2744 share a factor of 8, so a naive saw folds its partials above + // Nyquist onto multiples of 8 Hz that are not harmonics of the pitch; the band-limited saw leaves them 44.8 dB down (worst bin, measured) and a naive saw 18.9 dB down. The threshold sits between. + const float pitch = 2744f; + // The tract rings for a few hundred ms after the note starts; that decaying transient is not periodic and would fill the gaps between harmonics, so the window starts after it. + var window = Render(0f, pitch, skip: 16800); + if (window is null) return; + + // The vocal tract shapes the harmonics, so the reference is the strongest of them rather than the fundamental. + var strongest = Enumerable.Range(1, 6).Max(k => Magnitude(window, pitch * k)); + var worst = Db(Enumerable.Range(100, 900).Where(n => n % 343 != 0).Max(n => Magnitude(window, 8.0 * n)) / strongest); + output.WriteLine($"tilt 0 pitch {pitch}: worst non-harmonic level 800-8000 Hz {worst:F1} dB re strongest harmonic"); + Assert.True(worst < -32, $"non-harmonic energy {worst:F1} dB re strongest harmonic"); + } + + [Fact] + public void TiltIsAnAuthoredSourceControlSurfaceDefaultingToHalf() + { + var authored = PatchScript.Parse(Script(.8f, Pitch)); + Assert.Equal(.8f, Assert.Single(authored.SourcePorts).Tilt, 5); + Assert.Contains(authored.ControlSurfaces, surface => surface.Path == "/vocal/sources/0/tilt"); + + var defaulted = PatchScript.Parse(Script(.8f, Pitch).Replace("tilt=0.8", "", StringComparison.Ordinal)); + Assert.Equal(.5f, Assert.Single(defaulted.SourcePorts).Tilt, 5); + } + + [Fact] + public void PulseAndNoiseHighPassAreAuthoredSourceFieldsDefaultingToSawAndWhite() + { + var port = Assert.Single(PatchScript.Parse(Script(.5f, Pitch, "pulse=rosenberg noise_hp=2000")).SourcePorts); + Assert.Equal(GlottalPulse.Rosenberg, port.Pulse); + Assert.Equal(2000f, port.NoiseHighPassHz); + var defaulted = Assert.Single(PatchScript.Parse(Script(.5f, Pitch)).SourcePorts); + Assert.Equal(GlottalPulse.Saw, defaulted.Pulse); + Assert.Equal(0f, defaulted.NoiseHighPassHz); + Assert.Throws(() => PatchScript.Parse(Script(.5f, Pitch, "pulse=triangle"))); + } + + [Fact] + public void OnlyTheRosenbergPulseEmitsThePhasorAndOnlyAHighPassedSourceEmitsTheNoiseFilter() + { + string Emit(string extra) => FaustEmitter.Emit(PatchScript.Parse(Script(.5f, Pitch, extra)), new FaustExportOptions("pulse_emit")).Source; + Assert.DoesNotContain("lf_sawpos", Emit(""), StringComparison.Ordinal); + Assert.Contains("lf_sawpos", Emit("pulse=rosenberg"), StringComparison.Ordinal); + Assert.DoesNotContain("fi.highpass(2, 2000", Emit(""), StringComparison.Ordinal); + Assert.Contains("fi.highpass(2, 2000", Emit("noise_hp=2000"), StringComparison.Ordinal); + } + + [Fact] + public void RosenbergPulseIsPeriodicAtThePitchAndRendersWithinSixDbOfTheSaw() + { + var saw = Render(.5f); + var pulse = Render(.5f, extra: "pulse=rosenberg"); + if (saw is null || pulse is null) return; + + double Rms(float[] w) => Math.Sqrt(w.Sum(v => (double)v * v) / w.Length); + var difference = Db(Rms(pulse) / Rms(saw)); + output.WriteLine($"rosenberg rms {Rms(pulse):E3} saw rms {Rms(saw):E3} ({difference:F2} dB)"); + // The source rms is matched, the rendered level is not: the tract passes the saw's strong upper harmonics and the pulse has none (-3.2 dB here, -2.5 dB on /ɑmi/, measured -2.54). + Assert.InRange(difference, -6, 0); + var fundamental = Magnitude(pulse, Pitch); + var between = Magnitude(pulse, Pitch * 1.5); + Assert.True(Db(between / fundamental) < -30, $"energy between harmonics {Db(between / fundamental):F1} dB re the fundamental"); + } + + // One expression compiled alone as a one-output DSP. The pulse is run at 100 Hz, which divides the rate (480 samples a period), so every sample + // sits at a known phase; the patch path scales a bare signal by a constant, so levels are read against os.sawtooth through the same path. + // Expected values are Rosenberg C written out here, independent of the emitted formula's spelling. + private float[]? RenderBare(string expression, double seconds) + { + var dsp = $"import(\"stdfaust.lib\");\nprocess = {expression};\n"; + using var compiler = new AquaSynthPatchCompiler(new AquaSynthNativeOptions(SampleRate: Rate)); + if (!compiler.TryCompileSource(new AquaSynthCompileIdentity("pulse_bare", "pulse_bare", ""), dsp, (float)seconds + .1f, out var patch, out var error)) + { + if (error?.Contains("Faust toolchain not found", StringComparison.OrdinalIgnoreCase) == true || + error?.Contains("Faust DLL not found", StringComparison.OrdinalIgnoreCase) == true) + { + return null; + } + + Assert.Fail($"bare pulse compile failed: {error}"); + } + + using (patch) return patch!.Render((float)seconds); + } + + private static double RosenbergC(double p) => + p < 0.4 ? 0.5 * (1 - Math.Cos(Math.PI * p / 0.4)) : p < 0.56 ? Math.Cos(Math.PI * 0.5 * (p - 0.4) / 0.16) : 0; + + [Fact] + public void RosenbergPulseIsTheDocumentedRosenbergCZeroMeanAtTheSawsRms() + { + const int period = 480; + var pulse = RenderBare(FaustEmitter.RosenbergPulse("100.0"), 0.5); + var saw = RenderBare("os.sawtooth(100.0)", 0.5); + if (pulse is null || saw is null) return; + var cycle = pulse.Skip(period).Take(period).ToArray(); + double Rms(IEnumerable w) => Math.Sqrt(w.Sum(v => (double)v * v) / w.Count()); + // Calibrate the path's scale on the saw (rms 1/sqrt3 at the source): read every pulse sample back at source level. + var scale = Rms(saw.Skip(period).Take(period)) / (1 / Math.Sqrt(3)); + cycle = cycle.Select(v => (float)(v / scale)).ToArray(); + + // Rosenberg C has mean 0.301859 and rms 0.372667 about it; the emitted pulse is that, zero-mean, scaled to the saw's rms. + Assert.InRange(cycle.Average(), -0.003, 0.003); + var rms = Rms(cycle); + output.WriteLine($"pulse rms {rms:F5} (saw 1/sqrt3 = {1 / Math.Sqrt(3):F5}), path scale {scale:F4}, peak {cycle.Max():F4}, floor {cycle.Min():F4}"); + Assert.InRange(Db(rms / (1 / Math.Sqrt(3))), -0.05, 0.05); + + // Shape at every phase point. The phasor's alignment to the sample grid is not part of the contract, so find the shift that fits, + // then hold every point to the closed form: opening cosine, return cosine, closed plateau. A linear opening, another scale or no return phase all miss. + double Expected(int n, int shift) => (RosenbergC(((n + shift) % period) / (double)period) - 0.301859) * 1.549; + var shift = Enumerable.Range(0, period).MinBy(k => Enumerable.Range(0, period).Sum(n => Math.Pow(cycle[n] - Expected(n, k), 2))); + var worst = Enumerable.Range(0, period).Max(n => Math.Abs(cycle[n] - Expected(n, shift))); + output.WriteLine($"best shift {shift} samples, worst deviation from Rosenberg C {worst:E2}"); + Assert.True(worst < 0.01, $"worst deviation from Rosenberg C {worst:E2} at shift {shift}"); + // The regions by value: the opening peaks at p = 0.4 at (1 - mean) * 1.549, the closed floor is -mean * 1.549 and holds for 0.44 of the period (plus the tail of the return cosine inside the tolerance). + Assert.InRange(cycle.Max(), (1 - 0.301859) * 1.549 - 0.01, (1 - 0.301859) * 1.549 + 0.01); + Assert.InRange(cycle.Min(), -0.301859 * 1.549 - 0.01, -0.301859 * 1.549 + 0.01); + Assert.InRange(cycle.Count(v => Math.Abs(v - (-0.301859 * 1.549)) < 0.01), (int)(0.44 * period) - 15, (int)(0.44 * period) + 15); + } + + [Fact] + public void NoiseHighPassCornerIsTheAuthoredHertz() + { + // The source's noise alone (pressure 0 silences the voiced part), white and through fi.highpass(2, fc): band energy against the white render tells the corner. + // A 2 kHz corner leaves almost nothing under 700 Hz, passes 3-3.8 kHz whole and takes a few dB off 1.6-2.4 kHz; a 1 kHz corner passes the middle band; a 4 kHz corner cuts it. + double Band(float[] w, double lo, double hi) + { + double sum = 0; + for (var hz = lo; hz < hi; hz += 25) sum += Math.Pow(Magnitude(w, hz), 2); + return sum; + } + + double[] Shape(string extra) + { + var window = Render(.5f, extra: "pressure=0 noise=2 " + extra); + return window is null ? [] : [Band(window, 100, 700), Band(window, 1600, 2400), Band(window, 3000, 3800)]; + } + + var white = Shape(""); + var at2k = Shape("noise_hp=2000 pulse=saw"); + if (white.Length == 0 || at2k.Length == 0) return; + var at1k = Shape("noise_hp=1000"); + var at4k = Shape("noise_hp=4000"); + output.WriteLine($"band energies (100-700, 1600-2400, 3000-3800 Hz) white {string.Join(" ", white.Select(b => b.ToString("E2")))}"); + foreach (var (name, shape) in new[] { ("1k", at1k), ("2k", at2k), ("4k", at4k) }) output.WriteLine($"hp {name}: " + string.Join(" ", shape.Select(b => b.ToString("E2")))); + double Rel(double[] shape, int band) => Db(Math.Sqrt(shape[band] / white[band])); + Assert.True(Rel(at2k, 0) < -18, $"2 kHz corner leaves {Rel(at2k, 0):F1} dB at 100-700 Hz"); + Assert.InRange(Rel(at2k, 1), -6, -1.5); + Assert.True(Rel(at2k, 2) > -1.5, $"2 kHz corner leaves {Rel(at2k, 2):F1} dB at 3-3.8 kHz"); + // A 1 kHz corner passes the 1.6-2.4 kHz band nearly whole; a 4 kHz corner cuts it hard. + Assert.True(Rel(at1k, 1) > -2, $"1 kHz corner: {Rel(at1k, 1):F1} dB at 1.6-2.4 kHz"); + Assert.True(Rel(at4k, 1) < -9, $"4 kHz corner: {Rel(at4k, 1):F1} dB at 1.6-2.4 kHz"); + } +} diff --git a/tests/AquaSynth.Dsl.Tests/LipReflectanceTests.cs b/tests/AquaSynth.Dsl.Tests/LipReflectanceTests.cs new file mode 100644 index 0000000..f2cb069 --- /dev/null +++ b/tests/AquaSynth.Dsl.Tests/LipReflectanceTests.cs @@ -0,0 +1,88 @@ +using System.Text.RegularExpressions; +using AquaSynth.Dsl; + +namespace AquaSynth.Dsl.Tests; + +// The ladder's lip termination is the open-end reflectance R(z) = -g (1-b)/(1-b z^-1) with b/(1-b)^2 = (a fs / c)^2, a = sqrt(A_lip / pi) in cm, +// c = 35000 cm/s (FaustExport.VocalLadder.cs). These tests evaluate the emitted Faust expression itself and check it against the derivation. +public sealed class LipReflectanceTests +{ + private const int Rate = 48000; + + private const string Script = """ + morphology name=oral length_cm=17.52 diameters=.55,.75,1.05,1.45,1.25,.8 tongue_index=3 tongue_diameter=1.4 constriction_index=4 constriction_diameter=4 lip_opening=4 + waveguide_path name=oral_path morphology=oral speed=350 loss=.998 + source_port name=folds path=oral_path pressure=.7 tension=.55 opening=.5 noise=0 impedance=.3 flow_scale=.12 tilt=0 + radiation_load name=mouth path=oral_path aperture=1 reflection=-1 impedance=.28 + vocal_network name=voice paths=oral_path sources=folds radiation=mouth + vocal network=voice freq=100 gain=.5 sustain=1 + """; + + private static string Emit() => FaustEmitter.Emit(PatchScript.Parse(Script), new FaustExportOptions("lip_pole", SampleRate: Rate)).Source; + + // b by bisection on the derivation b/(1-b)^2 = q, independent of the closed form. + private static double SolvePole(double areaCm2) + { + var a = Math.Sqrt(areaCm2 / Math.PI); + var q = Math.Pow(a * Rate / 35000.0, 2); + double lo = 0, hi = 1 - 1e-12; + for (var i = 0; i < 200; i++) + { + var mid = (lo + hi) / 2; + if (mid / Math.Pow(1 - mid, 2) < q) lo = mid; else hi = mid; + } + + return Math.Clamp((lo + hi) / 2, 0, .9); + } + + [Fact] + public void LipReflectanceKeepsReflectionAsDcGainAndFeedsBackThePoleInTheLastCellEquation() + { + var source = Emit(); + var last = Regex.Match(source, @"'(\w+_L\d+) = \((\w+_lip_b) \* (\w+_L\d+) \+ \(1\.0 - (\w+_lip_b)\) \* \((.+?)\) \* (\w+_R\d+)\) \* clip01"); + Assert.True(last.Success, "last-cell equation is not the one-pole reflectance"); + Assert.Equal(last.Groups[1].Value, last.Groups[3].Value); + Assert.Equal(last.Groups[2].Value, last.Groups[4].Value); + Assert.False(string.IsNullOrWhiteSpace(last.Groups[5].Value)); + } + + [Theory] + [InlineData(.3)] + [InlineData(1.0)] + [InlineData(3.14159265)] + [InlineData(8.0)] + public void EmittedPoleSolvesTheLowFrequencyLossDerivationForTheLipCellArea(double areaCm2) + { + var source = Emit(); + var q = Regex.Match(source, @"^\s*(\w+)_lip_q = (.+);$", RegexOptions.Multiline); + var b = Regex.Match(source, @"^\s*(\w+)_lip_b = (.+);$", RegexOptions.Multiline); + Assert.True(q.Success && b.Success); + var prefix = q.Groups[1].Value; + // The lip pole reads the last cell's area and nothing else of the tract. + var lastCell = Regex.Matches(source, $@"^\s*{prefix}_A(\d+) = ", RegexOptions.Multiline).Count - 1; + Assert.Contains($"{prefix}_A{lastCell}", q.Groups[2].Value); + var program = $"import(\"stdfaust.lib\");\n{prefix}_A{lastCell} = {areaCm2.ToString(System.Globalization.CultureInfo.InvariantCulture)};\n" + + $"{prefix}_lip_q = {q.Groups[2].Value};\n{prefix}_lip_b = {b.Groups[2].Value};\nprocess = {prefix}_lip_b;\n"; + using var compiler = new AquaSynthPatchCompiler(new AquaSynthNativeOptions(SampleRate: Rate)); + if (!compiler.TryCompileSource(new AquaSynthCompileIdentity("lip_pole", "lip_pole", ""), program, .05f, out var patch, out var error)) + { + if (error?.Contains("not found", StringComparison.OrdinalIgnoreCase) == true) return; + Assert.Fail($"lip pole program failed: {error}"); + } + + using (patch) + { + var samples = patch!.Render(); + Assert.Equal(SolvePole(areaCm2), samples[^1], 3); + } + } + + [Fact] + public void PoleGrowsWithTheLipAreaAndEndCorrectionOfAOneCentimetreRadiusIsAboutHalfARadius() + { + // b/(1-b) samples is the DC group delay: for a = 1 cm at 48 kHz it is 0.96 samples = 0.7 cm, which is the 0.6 a end correction scale. + Assert.True(SolvePole(.5) < SolvePole(2) && SolvePole(2) < SolvePole(6)); + var b = SolvePole(Math.PI); + Assert.InRange(b / (1 - b) * 35000.0 / Rate, .5, .8); + } +} diff --git a/tests/AquaSynth.Dsl.Tests/NasalCouplingTests.cs b/tests/AquaSynth.Dsl.Tests/NasalCouplingTests.cs new file mode 100644 index 0000000..58e3d9c --- /dev/null +++ b/tests/AquaSynth.Dsl.Tests/NasalCouplingTests.cs @@ -0,0 +1,114 @@ +using System.Globalization; +using System.Text.RegularExpressions; +using AquaSynth.Dsl; + +namespace AquaSynth.Dsl.Tests; + +// The nasal side branch: the cited nasal tract, the velopharyngeal port area, and where the scaffold's nasal and bilabial/alveolar gestures +// close the mouth. The murmur itself is measured in VocalTractFormantTests (renders). +public sealed class NasalCouplingTests +{ + // Birkholz 2005 Table 2.3 after Dang & Honda 1994, copied here independently of NasalTractAreas: 19 sections of 0.6 cm, cm^2, + // velopharyngeal end first. + private static readonly float[] CitedAreas = + [ + 1.63f, 2.07f, 2.72f, 3.59f, 4.24f, 3.26f, 3.04f, 3.04f, 2.72f, 2.5f, 2.39f, 2.39f, 1.85f, 0.76f, 1.41f, 1.74f, 1.30f, 1.74f, 0.76f, + ]; + + [Fact] + public void TheNasalTractTableIsTheCitedNineteenSections() + { + Assert.Equal(CitedAreas, NasalTractAreas.Areas.ToArray()); + Assert.Equal(11.4f, NasalTractAreas.LengthCentimeters, 4); + Assert.Equal(NasalTractAreas.LengthCentimeters, CitedAreas.Length * NasalTractAreas.SectionLengthCentimeters, 3); + } + + [Fact] + public void TheScaffoldNasalMorphologyIsTheCitedTableAtItsLength() + { + var patch = PatchScript.Parse(IpaGestureExperiment.BuildWordScript([(new IpaGestureExperimentTarget("v_m", "m", "", .2f), 1f)])); + var nasal = patch.AreaFunctions.Single(area => area.Name == "nasal").Shape; + + Assert.Equal(11.4f, nasal.LengthCentimeters, 3); + Assert.Equal(CitedAreas.Length, nasal.Diameters.Count); + for (var i = 0; i < CitedAreas.Length; i++) + { + Assert.Equal(CitedAreas[i], nasal.Diameters[i] * nasal.Diameters[i], 2); + } + } + + // The emitted velum area expression, evaluated with the admittance (opening * coupling) it reads set to a constant: the port area is + // linear in the opening with the cited maximum, 0.8 cm^2. + [Theory] + [InlineData(1.0, 0.8)] + [InlineData(0.7, 0.56)] + [InlineData(0.25, 0.2)] + public void TheVelopharyngealPortAreaIsTheOpeningTimesTheMaximumPortArea(double opening, double expectedAreaSquareCentimeters) + { + var source = FaustEmitter.Emit( + PatchScript.Parse(IpaGestureExperiment.BuildWordScript([(new IpaGestureExperimentTarget("v_m", "m", "", .2f), 1f)])), + new FaustExportOptions("velum", SampleRate: 48000)).Source; + var area = Regex.Match(source, @"^\s*(\w+)_velum_area = (.+);$", RegexOptions.Multiline); + Assert.True(area.Success); + var admittance = Regex.Match(area.Groups[2].Value, @"\b(\w+_admittance)\b"); + Assert.True(admittance.Success, area.Groups[2].Value); + var program = $"import(\"stdfaust.lib\");\n{admittance.Groups[1].Value} = {opening.ToString(CultureInfo.InvariantCulture)};\n" + + $"area = {area.Groups[2].Value};\nprocess = area;\n"; + using var compiler = new AquaSynthPatchCompiler(new AquaSynthNativeOptions(SampleRate: 48000)); + if (!compiler.TryCompileSource(new AquaSynthCompileIdentity("velum", "velum", ""), program, .05f, out var compiled, out var error)) + { + if (error?.Contains("not found", StringComparison.OrdinalIgnoreCase) == true) return; + Assert.Fail($"velum program failed: {error}"); + } + + using (compiled) + { + Assert.Equal(expectedAreaSquareCentimeters, compiled!.Render()[^1], 3); + } + } + + private static float Spline(SynthPatch patch, string namePrefix, string surfacePath) => + patch.ControlSplines.Single(spline => spline.Name.StartsWith(namePrefix, StringComparison.Ordinal) && spline.SurfacePath == surfacePath).Points[^1].Value; // a held place: the last point is the gesture's value whether or not a boundary ramp precedes it + + // Distance of a vowel_set consonant constriction from the lip plane in cm, from the constriction_index spline: the surface is a section + // index in [0, sections] and the area model reads position = index / (sections - 1). + private static double CentimetresFromLips(string ipa) + { + var patch = PatchScript.Parse(IpaGestureExperiment.BuildWordScript([(new IpaGestureExperimentTarget("v_c", ipa, "", .2f), 1f)])); + var oral = patch.AreaFunctions.Single(area => area.Name == "oral"); + var index = Spline(patch, "v_c_constriction_index", "/vocal/areas/0/area/constriction_index") * oral.Shape.Sections; + return (1 - index / (oral.Shape.Sections - 1)) * oral.Shape.LengthCentimeters; + } + + [Fact] + public void UnderVowelSetMClosesAtTheLipsAndNAtTheAlveolarRidgeInCentimetres() + { + Assert.InRange(CentimetresFromLips("m"), -.05, .05); + Assert.InRange(CentimetresFromLips("n"), 2.4, 2.7); + Assert.True(CentimetresFromLips("n") > CentimetresFromLips("m") + 2); + } + + [Fact] + public void WithoutVowelSetTheConstrictionStaysOnTheNormalizedPlaceFraction() + { + // The earlier surface semantics are kept for hand-written scripts: the place fraction is the normalized spline value itself. A place + // with a distance in the place table is that distance from the lips over the morphology's length (1 - 2.555 / 17 for /n/, 1 for /m/). + var patch = PatchScript.Parse(""" + morphology name=oral length_cm=17 diameters=.6,.8,1.2,1.6,1.3,.9 tongue_index=3 tongue_diameter=1.4 constriction_index=4 constriction_diameter=1 lip_opening=1.5 + morphology name=nasal length_cm=12 diameters=.05,.35,.6,.8 + waveguide_path name=oral_path morphology=oral loss=.998 + waveguide_path name=nasal_path morphology=nasal loss=.997 + source_port name=folds path=oral_path pressure=.7 tension=.55 opening=.45 noise=.05 impedance=.3 + branch_port name=velopharynx from=oral_path from_position=.45 to=nasal_path opening=0 coupling=1 + constriction_contact name=contact path=oral_path position=.92 opening=.5 resistance=.4 stored_pressure=.1 + radiation_load name=mouth path=oral_path aperture=.8 reflection=-.82 impedance=.28 + vocal_network name=voice paths=oral_path,nasal_path sources=folds contacts=contact branches=velopharynx radiation=mouth + phoneme_gesture name=en ipa=n start=0 dur=.16 intensity=.8 + phoneme_gesture name=em ipa=m start=.2 dur=.16 intensity=.8 + vocal network=voice freq=150 gain=.2 sustain=.6 + """); + + Assert.Equal(1 - 2.555f / 17, Spline(patch, "en_constriction_index", "/vocal/areas/0/area/constriction_index"), 5); + Assert.Equal(1f, Spline(patch, "em_constriction_index", "/vocal/areas/0/area/constriction_index"), 5); + } +} diff --git a/tests/AquaSynth.Dsl.Tests/NativeFaustRuntimeTests.cs b/tests/AquaSynth.Dsl.Tests/NativeFaustRuntimeTests.cs index d606653..6c3ff1a 100644 --- a/tests/AquaSynth.Dsl.Tests/NativeFaustRuntimeTests.cs +++ b/tests/AquaSynth.Dsl.Tests/NativeFaustRuntimeTests.cs @@ -3,6 +3,7 @@ using AquaSynth.Faust; namespace AquaSynth.Dsl.Tests; +[Collection("NativeFaust")] public sealed class NativeFaustRuntimeTests { [Fact] @@ -169,9 +170,81 @@ public sealed class NativeFaustRuntimeTests var samples = patch.Render(controls); Assert.Contains("/vocal/sources/0/pressure", controls.Keys); - Assert.DoesNotContain("/vocal/radiation/0/reflection", controls.Keys); + Assert.Contains("/vocal/radiation/0/reflection", controls.Keys); Assert.True(samples.Length > 1024); Assert.Contains(samples, sample => MathF.Abs(sample) > 0.0001f); } } + + // Two back-to-back gestures on one surface. The engine's ControlSplineTimeline merges them into one absolute curve, so + // after the loud gesture ends the quiet one owns the value; summing per-spline offsets would keep the loud tail on top. + private const string BackToBackPressure = """ + morphology name=oral length_cm=17 diameters=.6,.8,1.2,1.5,.9 + waveguide_path name=oral_path morphology=oral loss=.998 + source_port name=folds path=oral_path pressure=.5 tension=.55 opening=.45 noise=0 impedance=.3 + radiation_load name=mouth path=oral_path aperture=.8 reflection=-.82 impedance=.28 + vocal_network name=voice paths=oral_path sources=folds radiation=mouth + {0} + vocal network=voice freq=150 gain=.2 sustain=.5 + """; + + private const string LoudGesture = "control_spline name=loud surface=/vocal/sources/0/pressure points=0:1,0.1:1"; + private const string QuietGesture = "control_spline name=quiet surface=/vocal/sources/0/pressure points=0.2:.05,0.5:.05"; + + [Fact] + public void ExporterEmitsOneMergedSplinePerSurfaceWithTimelineSemantics() + { + var both = PatchScript.Parse(string.Format(BackToBackPressure, LoudGesture + "\n" + QuietGesture)); + var source = FaustEmitter.Emit(both, new FaustExportOptions("merged")).Source; + var surface = both.ControlSurfaces.Single(candidate => candidate.Path == "/vocal/sources/0/pressure"); + + var timeline = new ControlSplineTimeline(both.ControlSplines); + Assert.InRange(timeline.ValueAt(surface.Path, .35f), .04f, .06f); + + // the surface carries one spline over the union of both gestures' points, not one spline per gesture + Assert.Contains("/splines/loud_quiet/0/value", source); + Assert.Contains("/splines/loud_quiet/3/value", source); + Assert.DoesNotContain("/splines/loud/", source); + Assert.DoesNotContain("/splines/quiet/", source); + + // single-spline surfaces keep their own spline + var single = FaustEmitter.Emit(PatchScript.Parse(string.Format(BackToBackPressure, QuietGesture)), new FaustExportOptions("single")).Source; + Assert.Contains("/splines/quiet/0/value", single); + } + + [Fact] + public void MergedSurfaceSplinesRenderTheQuietGestureAfterTheLoudOneEnds() + { + var both = RenderTailRms(string.Format(BackToBackPressure, LoudGesture + "\n" + QuietGesture)); + var quietOnly = RenderTailRms(string.Format(BackToBackPressure, QuietGesture)); + if (both is null || quietOnly is null) return; + + Assert.True(quietOnly > 0, "reference render is silent"); + Assert.InRange(both.Value / quietOnly.Value, .8, 1.25); + } + + private static double? RenderTailRms(string script) + { + using var compiler = new AquaSynthPatchCompiler(); + var source = FaustEmitter.Emit(PatchScript.Parse(script), new FaustExportOptions("merge_probe")).Source; + if (!compiler.TryCompileSource(new AquaSynthCompileIdentity("merge_probe", "merge_probe", script), source, .6f, out var patch, out var error)) + { + if (error?.Contains("Faust toolchain not found", StringComparison.OrdinalIgnoreCase) == true || + error?.Contains("Faust DLL not found", StringComparison.OrdinalIgnoreCase) == true) + { + return null; + } + + Assert.Fail($"merge probe compile failed: {error}"); + } + + using (patch) + { + var samples = patch!.Render(1.0f); + var rate = 48000; + var window = samples.Skip((int)(.35 * rate)).Take((int)(.1 * rate)).ToArray(); + Assert.True(window.Length > 1000, $"render too short: {samples.Length}"); + return Math.Sqrt(window.Sum(sample => (double)sample * sample) / window.Length); + } + } } diff --git a/tests/AquaSynth.Dsl.Tests/PackageBoundaryTests.cs b/tests/AquaSynth.Dsl.Tests/PackageBoundaryTests.cs index ae12828..387cd94 100644 --- a/tests/AquaSynth.Dsl.Tests/PackageBoundaryTests.cs +++ b/tests/AquaSynth.Dsl.Tests/PackageBoundaryTests.cs @@ -15,7 +15,7 @@ public sealed class PackageBoundaryTests { var result = await RunAsync( "dotnet", - ["pack", Path.Combine(root, "src", "AquaSynth.Core", "AquaSynth.Core.csproj"), "-c", "Release", "--no-restore", "-o", outputDir]); + ["pack", Path.Combine(root, "src", "AquaSynth.Core", "AquaSynth.Core.csproj"), "-c", "Release", "--no-restore", "-o", outputDir, .. QuietBuildServers]); Assert.Equal(0, result.ExitCode); var package = Assert.Single(Directory.GetFiles(outputDir, "AquaSynth.Core.*.nupkg")); @@ -44,7 +44,7 @@ public sealed class PackageBoundaryTests { var result = await RunAsync( "dotnet", - ["pack", Path.Combine(root, "src", "AquaSynth.Faust", "AquaSynth.Faust.csproj"), "-c", "Release", "--no-restore", "-o", outputDir]); + ["pack", Path.Combine(root, "src", "AquaSynth.Faust", "AquaSynth.Faust.csproj"), "-c", "Release", "--no-restore", "-o", outputDir, .. QuietBuildServers]); Assert.Equal(0, result.ExitCode); var package = Assert.Single(Directory.GetFiles(outputDir, "AquaSynth.Faust.*.nupkg")); @@ -97,6 +97,13 @@ public sealed class PackageBoundaryTests return directory?.FullName ?? throw new InvalidOperationException("could not find repository root"); } + // `dotnet pack` leaves VBCSCompiler and MSBuild node-reuse processes running. They inherit our redirected stdout/stderr pipes, so + // ReadToEndAsync never sees EOF and the test hangs after pack has already written the nupkg (the 2026-11-11 hang). Pack without + // them, and bound every wait so a regression fails with a message instead of hanging. + private static readonly string[] QuietBuildServers = ["-p:UseSharedCompilation=false", "-nodeReuse:false", "--disable-build-servers"]; + private static readonly TimeSpan ProcessTimeout = TimeSpan.FromMinutes(5); + private static readonly TimeSpan PipeDrainGrace = TimeSpan.FromSeconds(10); + private static async Task<(int ExitCode, string Stdout, string Stderr)> RunAsync( string fileName, IReadOnlyList arguments) @@ -108,12 +115,30 @@ public sealed class PackageBoundaryTests UseShellExecute = false }; foreach (var argument in arguments) start.ArgumentList.Add(argument); + start.Environment["MSBUILDDISABLENODEREUSE"] = "1"; + start.Environment["DOTNET_CLI_USE_MSBUILD_SERVER"] = "0"; using var process = Process.Start(start) ?? throw new InvalidOperationException($"failed to start `{fileName}`"); var stdout = process.StandardOutput.ReadToEndAsync(); var stderr = process.StandardError.ReadToEndAsync(); - await process.WaitForExitAsync(); + using var timeout = new CancellationTokenSource(ProcessTimeout); + try + { + await process.WaitForExitAsync(timeout.Token); + } + catch (OperationCanceledException) + { + process.Kill(entireProcessTree: true); + throw new TimeoutException($"`{fileName} {string.Join(' ', arguments)}` did not exit within {ProcessTimeout}."); + } + + var drained = Task.WhenAll(stdout, stderr); + if (await Task.WhenAny(drained, Task.Delay(PipeDrainGrace)) != drained) + { + throw new TimeoutException( + $"`{fileName}` exited but its output pipes stayed open for {PipeDrainGrace}: a child it spawned (build server?) inherited them."); + } + return (process.ExitCode, await stdout, await stderr); } } - diff --git a/tests/AquaSynth.Dsl.Tests/PatchScriptTests.cs b/tests/AquaSynth.Dsl.Tests/PatchScriptTests.cs index 1d00e05..d2d1489 100644 --- a/tests/AquaSynth.Dsl.Tests/PatchScriptTests.cs +++ b/tests/AquaSynth.Dsl.Tests/PatchScriptTests.cs @@ -316,7 +316,7 @@ public sealed class PatchScriptTests Assert.Contains("primitive_contact_voices_0_constriction", faust); Assert.Contains("primitive_branch_voices_0_velopharynx", faust); Assert.Contains("primitive_radiation_voices_0_lip", faust); - Assert.Contains("de.fdelay1a", faust); + Assert.Contains("primitive_ladder", faust); Assert.DoesNotContain("graph_loop ~ si.bus", faust); Assert.DoesNotContain("graph_terminal_area_voices_0_area_", faust); Assert.DoesNotContain("tract_lf", faust); @@ -462,7 +462,7 @@ public sealed class PatchScriptTests var export = FaustEmitter.Emit(patch, new FaustExportOptions("primitive_vocal", DebugProbeUi: true)); Assert.Contains("primitive_radiated", export.Source); - Assert.Contains("de.fdelay1a", export.Source); + Assert.Contains("primitive_ladder", export.Source); Assert.Contains("/debug/voice_0/path/oral/delay", export.Source); Assert.Contains("/debug/voice_0/path/oral/incoming_wave", export.Source); Assert.Contains("/debug/voice_0/path/oral/outgoing_wave", export.Source); @@ -577,11 +577,32 @@ public sealed class PatchScriptTests timeline.SetFuturePoint("/vocal/nope", timeSeconds: .02f, normalizedValue: .5f, nowSeconds: .0f)); } + [Fact] + public void VowelGestureWithoutVowelSetFailsLoudly() + { + const string tract = """ + morphology name=oral length_cm=17 diameters=.6,.8,1.2,1.6,1.3,.9 tongue_index=3 tongue_diameter=1.4 constriction_index=4 constriction_diameter=1 lip_opening=1.5 + waveguide_path name=oral_path morphology=oral loss=.998 + source_port name=folds path=oral_path pressure=.7 tension=.55 opening=.45 noise=.05 impedance=.3 + constriction_contact name=contact path=oral_path position=.92 opening=.5 resistance=.4 stored_pressure=.1 + radiation_load name=mouth path=oral_path aperture=.8 reflection=-.82 impedance=.28 + vocal_network name=voice paths=oral_path sources=folds contacts=contact radiation=mouth + phoneme_gesture name=vowel ipa=a start=0 dur=.2 intensity=.7 + vocal network=voice freq=150 gain=.2 sustain=.3 + """; + var error = Assert.Throws(() => PatchScript.Parse(tract)); + Assert.StartsWith("line 7:", error.Message); + Assert.Contains("vowel_set", error.Message); + // With the vowel_set in front of it the same gesture parses. + PatchScript.Parse(tract.Replace("waveguide_path name=oral_path", "vowel_set area_function=oral source=story2008\nwaveguide_path name=oral_path")); + } + [Fact] public void PhonemeGestureDslEmitsAnatomicalControlSplines() { var patch = PatchScript.Parse(""" morphology name=oral length_cm=17 diameters=.6,.8,1.2,1.6,1.3,.9 tongue_index=3 tongue_diameter=1.4 constriction_index=4 constriction_diameter=1 lip_opening=1.5 + vowel_set area_function=oral source=story2008 morphology name=nasal length_cm=12 diameters=.05,.35,.6,.8 waveguide_path name=oral_path morphology=oral loss=.998 waveguide_path name=nasal_path morphology=nasal loss=.997 @@ -602,7 +623,9 @@ public sealed class PatchScriptTests Assert.Contains(patch.ControlSplines, spline => spline.Name.StartsWith("ma_contact", StringComparison.Ordinal) && spline.SurfacePath == "/vocal/contacts/0/opening"); Assert.Contains(patch.ControlSplines, spline => spline.Name.StartsWith("hw_source_noise", StringComparison.Ordinal) && spline.SurfacePath == "/vocal/sources/0/noise"); Assert.Contains(patch.ControlSplines, spline => spline.Name.StartsWith("hw_constriction_index", StringComparison.Ordinal) && spline.SurfacePath == "/vocal/areas/0/area/constriction_index"); - Assert.Contains(patch.ControlSplines, spline => spline.Name.StartsWith("vowel_tongue_index", StringComparison.Ordinal) && spline.SurfacePath == "/vocal/areas/0/area/tongue_index"); + Assert.Contains(patch.ControlSplines, spline => spline.Name.StartsWith("vowel_vowel_ah", StringComparison.Ordinal) && spline.SurfacePath == "/vocal/areas/0/area/vowel_ah" && spline.Points.All(point => point.Value == 1)); + Assert.Contains(patch.ControlSplines, spline => spline.Name.StartsWith("vowel_vowel_i", StringComparison.Ordinal) && spline.SurfacePath == "/vocal/areas/0/area/vowel_i" && spline.Points.All(point => point.Value == 0)); + Assert.DoesNotContain(patch.ControlSplines, spline => spline.SurfacePath == "/vocal/areas/0/area/tongue_index" && spline.Name.StartsWith("vowel_", StringComparison.Ordinal)); var catalog = ControlSurfaceCatalog.FromPatch(patch); var timeline = catalog.CreateTimeline(); @@ -613,6 +636,47 @@ public sealed class PatchScriptTests Assert.True(fricativeControls["/vocal/sources/0/noise"] > nasalControls["/vocal/sources/0/noise"]); } + [Fact] + public void BareNIsAnAlveolarNasalNotTheApproximantFallback() + { + var patch = PatchScript.Parse(""" + morphology name=oral length_cm=17 diameters=.6,.8,1.2,1.6,1.3,.9 tongue_index=3 tongue_diameter=1.4 constriction_index=4 constriction_diameter=1 lip_opening=1.5 + morphology name=nasal length_cm=12 diameters=.05,.35,.6,.8 + waveguide_path name=oral_path morphology=oral loss=.998 + waveguide_path name=nasal_path morphology=nasal loss=.997 + source_port name=folds path=oral_path pressure=.7 tension=.55 opening=.45 noise=.05 impedance=.3 + branch_port name=velopharynx from=oral_path from_position=.45 to=nasal_path opening=.01 coupling=1 + constriction_contact name=contact path=oral_path position=.92 opening=.5 resistance=.4 stored_pressure=.1 + radiation_load name=mouth path=oral_path aperture=.8 reflection=-.82 impedance=.28 + vocal_network name=voice paths=oral_path,nasal_path sources=folds contacts=contact branches=velopharynx radiation=mouth + phoneme_gesture name=en ipa=n start=0 dur=.1 + vocal network=voice freq=140 gain=.2 sustain=.3 + """); + + Assert.Equal("voiced_alveolar_nasal", patch.PhonemeGestures.Single().Descriptor); + Assert.Contains(patch.ControlSplines, spline => spline.Name.StartsWith("en_velum", StringComparison.Ordinal) && spline.SurfacePath == "/vocal/branches/0/opening"); + var catalog = ControlSurfaceCatalog.FromPatch(patch); + Assert.True(catalog.CreateTimeline().ControlValuesAt(.03f)["/vocal/branches/0/opening"] > .5f, "the velum opens for n"); + } + + [Fact] + public void WordScriptRunsGesturesBackToBackAndLeavesDescriptorsToTheDslTable() + { + var script = IpaGestureExperiment.BuildWordScript( + [ + (new IpaGestureExperimentTarget("w_n", "n", "", .1f), 1f), + (new IpaGestureExperimentTarget("w_i", "i", "", .2f), 1.2f), + (new IpaGestureExperimentTarget("w_b", "b", "", .1f), 1f) + ], frequency: 140); + + var patch = PatchScript.Parse(script); + Assert.Equal(["n", "i", "b"], patch.PhonemeGestures.Select(gesture => gesture.Ipa)); + Assert.Equal([0f, .1f, .3f], patch.PhonemeGestures.Select(gesture => gesture.StartSeconds)); + Assert.Equal(["voiced_alveolar_nasal", "voiced_close_front_unrounded_vowel", "voiced_bilabial_plosive"], patch.PhonemeGestures.Select(gesture => gesture.Descriptor)); + Assert.Equal(1.2f, patch.PhonemeGestures[1].Intensity); + Assert.Throws(() => IpaGestureExperiment.BuildWordScript([])); + } + [Fact] public void IpaGestureExperimentWritesFrozenRoundBundle() { @@ -945,7 +1009,7 @@ public sealed class PatchScriptTests var export = FaustEmitter.Emit(patch, new FaustExportOptions("tract_graph")); Assert.Contains("primitive_radiated", export.Source); - Assert.Contains("de.fdelay1a", export.Source); + Assert.Contains("primitive_ladder", export.Source); Assert.Contains("primitive_source_voices_0_source", export.Source); Assert.Contains("primitive_radiation_voices_0_lip", export.Source); Assert.DoesNotContain("acoustic_graph_radiated", export.Source); @@ -1139,7 +1203,7 @@ public sealed class PatchScriptTests Assert.Contains("primitive_path_voices_0_oral", faust); Assert.Contains("primitive_branch_voices_0_velopharynx", faust); Assert.Contains("primitive_contact_voices_0_constriction", faust); - Assert.Contains("de.fdelay1a", faust); + Assert.Contains("primitive_ladder", faust); Assert.DoesNotContain("graph_loop ~ si.bus", faust); Assert.DoesNotContain("_wg_", faust); } diff --git a/tests/AquaSynth.Dsl.Tests/PhoneTransitionTests.cs b/tests/AquaSynth.Dsl.Tests/PhoneTransitionTests.cs new file mode 100644 index 0000000..43a3817 --- /dev/null +++ b/tests/AquaSynth.Dsl.Tests/PhoneTransitionTests.cs @@ -0,0 +1,84 @@ +using AquaSynth.Dsl; +using AquaSynth.Faust; + +namespace AquaSynth.Dsl.Tests; + +// What Meta heard in tract-weak-spots-1 (2026-11-13) while every formant matched: clicks at the phone boundaries of nibu, a /b/ burst the +// length of a fricative, and a first 100 ms that climbed from silence. These pin the three causes at the control-spline and DSP-source +// level (docs/listening/tract-weak-spots-1-analysis-20261113.md); the renders are measured by tools/formant-check/ear_metrics.py. +public sealed class PhoneTransitionTests +{ + private static SynthPatch Nibu() => PatchScript.Parse(IpaGestureExperiment.BuildWordScript( + [ + (new IpaGestureExperimentTarget("w_n", "n", "", .095f), 1f), + (new IpaGestureExperimentTarget("w_i", "i", "", .18f), 1f), + (new IpaGestureExperimentTarget("w_b", "b", "", .095f), 1f), + (new IpaGestureExperimentTarget("w_u", "u", "", .18f), 1f) + ], frequency: 140)); + + [Fact] + public void NoSurfaceStepsAtAPhoneBoundary() + { + var timeline = new ControlSplineTimeline(Nibu().ControlSplines); + var worst = (Surface: "", Boundary: 0f, Step: 0f); + foreach (var boundary in new[] { .095f, .275f, .37f }) + { + foreach (var surface in timeline.SurfacePaths) + { + var step = MathF.Abs(timeline.ValueAt(surface, boundary + .0005f) - timeline.ValueAt(surface, boundary - .0005f)); + if (step > worst.Step) worst = (surface, boundary, step); + } + } + + // 0.5 ms either side of a boundary; a 30 ms ramp across the full 0..1 range moves 0.017 in that time, a step moves it by the + // whole difference (the pre-fix nibu stepped /vocal/areas/0/area/constriction_diameter by 0.77 at the 95 ms boundary). + Assert.True(worst.Step < .03f, $"{worst.Surface} steps {worst.Step:0.000} at {worst.Boundary} s"); + } + + [Fact] + public void ThePhoneAfterABoundaryArrivesAtItsFirstValueWithinTheTransition() + { + var timeline = new ControlSplineTimeline(Nibu().ControlSplines); + const string diameter = "/vocal/areas/0/area/constriction_diameter"; + var closedN = timeline.ValueAt(diameter, .05f); + Assert.True(closedN < .1f, "the /n/ constriction is shut"); + Assert.True(timeline.ValueAt(diameter, .095f + .002f) < .5f, "still closing into the vowel two ms after the boundary"); + Assert.Equal(1f, timeline.ValueAt(diameter, .095f + .035f), 2); + } + + [Fact] + public void TheFirstPhoneOfAWordStartsAtItsOwnValue() + { + var timeline = new ControlSplineTimeline(Nibu().ControlSplines); + Assert.Equal(timeline.ValueAt("/vocal/areas/0/area/constriction_diameter", 0f), timeline.ValueAt("/vocal/areas/0/area/constriction_diameter", .02f), 3); + } + + [Fact] + public void AStopOpensInTwelveMillisecondsNotAThirdOfItsGesture() + { + var timeline = new ControlSplineTimeline(Nibu().ControlSplines); + const string contact = "/vocal/contacts/0/opening"; + const float start = .275f, duration = .095f; + Assert.True(timeline.ValueAt(contact, start + duration - .013f) < .1f, "closed until 12 ms before the end"); + Assert.True(timeline.ValueAt(contact, start + duration) > .8f, "open at the end"); + var opening = 0f; + for (var t = start + .03f; t <= start + duration; t += .0005f) + { + if (timeline.ValueAt(contact, t) > .3f) { opening = t; break; } + } + + Assert.True(start + duration - opening < .012f, $"the opening crosses 0.3 only {start + duration - opening:0.000} s before the end"); + } + + [Fact] + public void ControlSmoothersStartAtTheirInputNotAtZero() + { + var source = FaustEmitter.Emit(Nibu(), new FaustExportOptions("transition_probe")).Source; + Assert.Contains("smoo_init(x) =", source); + Assert.Contains("smoo_init;", source); + // Every control surface and spline-point slider uses it; patch parameters keep si.smoo. + var remaining = source.Split('\n').Where(line => line.Contains("hslider", StringComparison.Ordinal) && line.Contains(": si.smoo;", StringComparison.Ordinal) + && (line.Contains("\"/vocal/", StringComparison.Ordinal) || line.Contains("\"/splines/", StringComparison.Ordinal))).ToArray(); + Assert.Empty(remaining); + } +} diff --git a/tests/AquaSynth.Dsl.Tests/PinkTromboneLogMelParityTests.cs b/tests/AquaSynth.Dsl.Tests/PinkTromboneLogMelParityTests.cs index 8e75b4d..0156fa4 100644 --- a/tests/AquaSynth.Dsl.Tests/PinkTromboneLogMelParityTests.cs +++ b/tests/AquaSynth.Dsl.Tests/PinkTromboneLogMelParityTests.cs @@ -4,6 +4,7 @@ using System.Text; namespace AquaSynth.Dsl.Tests; +[Collection("NativeFaust")] public sealed class PinkTromboneLogMelParityTests { private const float SmokeCosineFloor = 0.08f; diff --git a/tests/AquaSynth.Dsl.Tests/PlaceTableTests.cs b/tests/AquaSynth.Dsl.Tests/PlaceTableTests.cs new file mode 100644 index 0000000..a19ea3f --- /dev/null +++ b/tests/AquaSynth.Dsl.Tests/PlaceTableTests.cs @@ -0,0 +1,143 @@ +using System.Security.Cryptography; +using System.Text; +using AquaSynth.Dsl; +using AquaSynth.Faust; +using Xunit.Abstractions; + +namespace AquaSynth.Dsl.Tests; + +// The place table in PatchScript owns where a consonant closes (cm behind the lip plane). Text-level tests: no Faust compile. +public sealed class PlaceTableTests(ITestOutputHelper output) +{ + private static string Word(string ipa, string descriptor = "") => IpaGestureExperiment.BuildWordScript( + [(new IpaGestureExperimentTarget("w_0_a", "a", "", 0.18f), 1f), (new IpaGestureExperimentTarget("w_1_c", ipa, descriptor, 0.095f), 1f), (new IpaGestureExperimentTarget("w_2_a", "a", "", 0.18f), 1f)], 140); + + private static string Sha(string text) => Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(text)))[..16]; + + private static float ConstrictionIndex(string script, string name) => + PatchScript.Parse(script).ControlSplines + .Single(s => s.Name.StartsWith($"{name}_constriction_index", StringComparison.Ordinal) && s.SurfacePath == "/vocal/areas/0/area/constriction_index").Points[^1].Value; // the held place; a boundary ramp may precede it + + // Re-pinned on the phone-transition cut (boundary ramps, 12 ms stop release, smoo_init, vocal attack): these ids moved on purpose; the invariant + // that survives is that a word of labials writes no position surface (below), not these bytes. Faust ids re-pinned again on the PR 52 fix passes (smoo_init pole, pitch-ratio smoother, shut lips for stops, spline handles; the scripts are unchanged; the last move is the smoo_from step line only, which reverts to prev * pole + x * (1 - pole): the old ids come back when that line is put back). Pre-cut values on af0e44f: the vowel_set constriction_index spline value of /b/ and /n/ (and the hash of the whole script and of the + // Faust source for /apa/ and /aba/, which this cut must not change by a byte; the ids are a SHA-256 prefix). + private const float BIndex = 0.8333333f, NIndex = 0.7118056f; + + [Fact] + public void PlaceTableKeepsLabialAndAlveolarPositions() + { + var b = ConstrictionIndex(Word("b"), "w_1_c"); + var n = ConstrictionIndex(Word("n"), "w_1_c"); + output.WriteLine($"b {b:R} n {n:R}"); + foreach (var (ipa, scriptSha, faustSha) in new[] { ("p", "0E86DB0F40C0F12A", "358106F493D1783D"), ("b", "A63403EBAAD016FB", "AE80CF31EF1DDC9C") }) + { + var script = Word(ipa); + var faust = FaustEmitter.Emit(PatchScript.Parse(script), new FaustExportOptions("pin", SampleRate: 48000)).Source; + Assert.Equal(scriptSha, Sha(script)); + Assert.Equal(faustSha, Sha(faust)); + } + + Assert.Equal(BIndex, b); + Assert.Equal(NIndex, n); + } + + // The side stub is declared only for a word with a lateral: the script and the Faust source of /apa/ /aba/ /ana/ /ata/ are the bytes the scaffold + // gave before the lateral cut (SHA-256 prefixes taken on the head of /k/, 8d50819 plus /d/). + [Fact] + public void ExistingWordsSourcesUnchanged() + { + foreach (var (ipa, scriptSha, faustSha) in new[] + { + ("p", "0E86DB0F40C0F12A", "358106F493D1783D"), ("b", "A63403EBAAD016FB", "AE80CF31EF1DDC9C"), + ("n", "5C3FA24E40D9DE26", "B2B9C5E1015B60A9"), ("t", "45941CCFD93561B3", "0AACE6D26F648FB0"), + }) + { + var script = Word(ipa); + var faust = FaustEmitter.Emit(PatchScript.Parse(script), new FaustExportOptions("pin", SampleRate: 48000)).Source; + Assert.Equal(scriptSha, Sha(script)); + Assert.Equal(faustSha, Sha(faust)); + } + + Assert.Contains(PatchScript.SideStubBranch, Word("l")); + } + + [Fact] + public void UnknownPlaceHasNoDistance() + { + Assert.True(PatchScript.TryPlaceDistanceCentimeters("voiceless_bilabial_plosive", out var lips)); + Assert.Equal(0f, lips); + Assert.True(PatchScript.TryPlaceDistanceCentimeters("voiced_alveolar_nasal", out var ridge)); + Assert.Equal(2.555f, ridge); + // velar: between the narrowest dorsal section of /u/ (section 26, 6.97 cm from the lips) and the narrowest pharyngeal section of /a/ + // (section 13, 12.14 cm); 9.81 cm is the 0.44 fraction of the 17.52 cm tract, which sits within 0.05 cm of the centre of a ladder cell + // (their mean, 9.555 cm, is 0.3 cm from one) + Assert.True(PatchScript.TryPlaceDistanceCentimeters("voiceless_velar_plosive", out var velar)); + Assert.InRange(velar, 6.97f, 12.14f); + Assert.Equal(9.81f, velar, 3); + foreach (var descriptor in new[] { "voiced_uvular_fricative", "voiced_palatal_approximant", "voiced_plosive", "" }) + { + Assert.False(PatchScript.TryPlaceDistanceCentimeters(descriptor, out var cm), descriptor); + Assert.True(float.IsNaN(cm), $"{descriptor} claims {cm}"); + } + } + + private static bool HasPositionSurface(string script) => + PatchScript.Parse(script).ControlSurfaces.Any(surface => surface.Path == "/vocal/contacts/0/position"); + + private static float[] SitePoints(string script, string name) => + PatchScript.Parse(script).ControlSplines.Single(s => s.Name.StartsWith($"{name}_burst_site", StringComparison.Ordinal)).Points.Select(p => p.Value).ToArray(); + + private static string Faust(string script) => FaustEmitter.Emit(PatchScript.Parse(script), new FaustExportOptions("site", SampleRate: 48000)).Source; + + [Fact] + public void AlveolarBurstGoesInAtTheRidgeAndLabialBurstStaysAtTheGlottalEnd() + { + var t = Word("t", "voiceless_alveolar_plosive"); + Assert.True(HasPositionSurface(t)); + Assert.All(SitePoints(t, "w_1_c"), v => Assert.Equal(1 - 2.555f / StoryVowelAreas.MeanLengthCentimeters, v, 4)); + Assert.Contains("_injection0_site", Faust(t)); + + // a word of labials never makes the surface: no spline, and the burst joins the glottal path input as before + var p = Word("p"); + Assert.False(HasPositionSurface(p)); + Assert.DoesNotContain("_injection", Faust(p)); + } + + [Fact] + public void ALabialAfterAnotherPlosiveInTheSameWordIsPlacedAtTheGlottalEnd() + { + IpaGestureExperimentTarget Phone(string label, string ipa, float s) => new($"w_{label}", ipa, "", s); + var script = IpaGestureExperiment.BuildWordScript( + [(Phone("0_b", "b", 0.095f), 1f), (Phone("1_a", "a", 0.18f), 1f), (new IpaGestureExperimentTarget("w_2_t", "t", "voiceless_alveolar_plosive", 0.095f), 1f), (Phone("3_a", "a", 0.18f), 1f), (Phone("4_p", "p", 0.095f), 1f)], 140); + Assert.All(SitePoints(script, "w_0_b"), v => Assert.Equal(0f, v)); + Assert.All(SitePoints(script, "w_4_p").Skip(1), v => Assert.Equal(0f, v)); + // after the 30 ms boundary ramp from the previous plosive's place (the first point is that place) + Assert.All(SitePoints(script, "w_2_t").Skip(1), v => Assert.True(v > 0.8f)); + } + + [Fact] + public void ATongueSealedStopIsClosedCompletelyUntilItsContactOpensAndALabialKeepsItsOldSpline() + { + float[] Diameter(string script) => PatchScript.Parse(script).ControlSplines + .Single(s => s.Name.StartsWith("w_1_c_constriction_diameter", StringComparison.Ordinal)).Points.Select(p => p.Value).ToArray(); + var t = PatchScript.Parse(Word("t", "voiceless_alveolar_plosive")).ControlSplines + .Single(s => s.Name.StartsWith("w_1_c_constriction_diameter", StringComparison.Ordinal)).Points; + // lead-in (the vowel's open 1), arrival, closure hold, end: shut from the arrival to the hold, and the hold ends StopReleaseSeconds before the stop does + Assert.Equal(1f, t[0].Value); + Assert.Equal(0f, t[1].Value); + Assert.Equal(0f, t[2].Value); + Assert.Equal(1 - 0.012f / 0.095f, (t[2].TimeSeconds - 0.18f) / 0.095f, 3); + var b = Diameter(Word("b")); + Assert.True(b[1] > 0 && Math.Abs(b[1] - b[2]) < 1e-6f, "the lip-sealed stop keeps the 0.03 aperture spline (its source text is pinned)"); + } + + [Fact] + public void VelarBurstGoesInAtTheVelarPlaceAndFurtherBackThanTheRidge() + { + var k = Word("k", "voiceless_velar_plosive"); + Assert.True(HasPositionSurface(k)); + Assert.All(SitePoints(k, "w_1_c"), v => Assert.Equal(1 - 9.81f / StoryVowelAreas.MeanLengthCentimeters, v, 4)); + Assert.Contains("_injection0_site", Faust(k)); + Assert.Equal(1 - 9.81f / StoryVowelAreas.MeanLengthCentimeters, ConstrictionIndex(k, "w_1_c") / BIndex, 4); + } +} diff --git a/tests/AquaSynth.Dsl.Tests/ProbeTimelineSampleRateTests.cs b/tests/AquaSynth.Dsl.Tests/ProbeTimelineSampleRateTests.cs new file mode 100644 index 0000000..5756019 --- /dev/null +++ b/tests/AquaSynth.Dsl.Tests/ProbeTimelineSampleRateTests.cs @@ -0,0 +1,41 @@ +using AquaSynth.Dsl; + +namespace AquaSynth.Dsl.Tests; + +public sealed class ProbeTimelineSampleRateTests +{ + private const string TimelineScript = """ + morphology name=oral length_cm=17.52 diameters=.55,.75,1.05,1.45,1.25,.8 tongue_index=3 tongue_diameter=1.4 constriction_index=4 constriction_diameter=4 lip_opening=4 + waveguide_path name=oral_path morphology=oral speed=350 loss=.998 + source_port name=folds path=oral_path pressure=.7 tension=.55 opening=.5 noise=0 impedance=.3 flow_scale=.12 + radiation_load name=mouth path=oral_path aperture=.8 reflection=-.82 impedance=.28 + probe_timeline name=flow blocks=4 block_size=64 + vocal_network name=voice paths=oral_path sources=folds radiation=mouth probe=flow + vocal network=voice freq=140 gain=.2 sustain=.3 + """; + + [Fact] + public void ProbeTimelineReadsTheSampleRateItIsGiven() + { + var patch = PatchScript.Parse(TimelineScript + "\ncontrol_spline name=speed_step surface=/vocal/paths/0/speed interp=hold points=0:.25,0.0028:.5"); + float Delay(int block, float? rate) => (rate is { } r ? ProbeTimelineReport.Build(patch, "voice", blocks: 3, sampleRate: r) : ProbeTimelineReport.Build(patch, "voice", blocks: 3)) + .First(s => s.Signal == "delay_samples" && s.Block == block).Value; + // Block 0 (t = 0) reads speed 325 m/s: the delay is length / speed * rate, so 44.1 kHz and 48 kHz stand in the ratio of the rates. + Assert.Equal(0.1752f / 325 * 48000, Delay(0, 48000), 3); + Assert.Equal(0.1752f / 325 * 44100, Delay(0, 44100), 3); + Assert.Equal(Delay(0, 48000), Delay(0, null), 3); + // Block 2 sits at 64 * 2 / rate = 2.67 ms at 48 kHz (before the 2.8 ms speed step: 325 m/s) and 2.90 ms at 44.1 kHz (after it: 550 m/s). + Assert.Equal(0.1752f / 325 * 48000, Delay(2, 48000), 3); + Assert.Equal(0.1752f / 550 * 44100, Delay(2, 44100), 3); + } + + [Fact] + public void EmittedSourceFollowsTheRequestedSampleRate() + { + // The vocal ladder's delay-line lengths are length / speed * rate samples, so the emitted DSP carries the rate it was asked for. + var at48 = FaustEmitter.EmitScript(TimelineScript, new FaustExportOptions("rate_probe", SampleRate: 48000)).Source; + var at44 = FaustEmitter.EmitScript(TimelineScript, new FaustExportOptions("rate_probe", SampleRate: 44100)).Source; + Assert.NotEqual(at48, at44); + Assert.Equal(at48, FaustEmitter.EmitScript(TimelineScript, new FaustExportOptions("rate_probe")).Source); + } +} diff --git a/tests/AquaSynth.Dsl.Tests/RepeatLoweringTests.cs b/tests/AquaSynth.Dsl.Tests/RepeatLoweringTests.cs new file mode 100644 index 0000000..3e1c65e --- /dev/null +++ b/tests/AquaSynth.Dsl.Tests/RepeatLoweringTests.cs @@ -0,0 +1,44 @@ +using AquaSynth.Dsl; + +namespace AquaSynth.Dsl.Tests; + +// sfxr's repeat restarts the pitch slide and the arpeggio; it does not restart the envelope. Before this, `patch repeat` wrapped the +// whole age, so a repeat shorter than sustain+decay never reached the release (the 2026-11-11 explosion/golf/powerup finding). +public sealed class RepeatLoweringTests +{ + private const string Script = """ + patch repeat=0.11315 + voice + wave=sine + freq=57.5946 + sustain=0.1306122449 + decay=0.1777777778 + pitch_ramp=-0.208544 + arp_delay=0.05 + arp_mult=1.5 + """; + + [Fact] + public void RepeatWrapsPitchAndArpeggioAgeButNotTheEnvelope() + { + var source = FaustEmitter.EmitScript(Script).Source; + + Assert.Contains("age = time;", source); + Assert.DoesNotContain("floor(time /", source); + Assert.Contains("voice_0_pitch_age = voice_0_age - floor(voice_0_age / repeat_interval) * repeat_interval;", source); + Assert.Contains("0.208544", source); + Assert.Contains("* voice_0_pitch_age", source); + Assert.Contains("select2(voice_0_pitch_age <", source); + Assert.Contains("oneshot_adsr_at(voice_0_age,", source); + Assert.DoesNotContain("oneshot_adsr_at(voice_0_pitch_age", source); + } + + [Fact] + public void WithoutRepeatThereIsNoPitchAge() + { + var source = FaustEmitter.EmitScript(Script.Replace("patch repeat=0.11315\n", "")).Source; + + Assert.DoesNotContain("pitch_age", source); + Assert.DoesNotContain("repeat_interval", source); + } +} diff --git a/tests/AquaSynth.Dsl.Tests/SpeechDistributedTrainingTests.cs b/tests/AquaSynth.Dsl.Tests/SpeechDistributedTrainingTests.cs index 4c06e7a..15f564c 100644 --- a/tests/AquaSynth.Dsl.Tests/SpeechDistributedTrainingTests.cs +++ b/tests/AquaSynth.Dsl.Tests/SpeechDistributedTrainingTests.cs @@ -180,6 +180,8 @@ public sealed class SpeechDistributedTrainingTests Assert.True(after < before, $"expected CultMesh worker gradients to reduce loss; before={before}, after={after}"); Assert.Single(payloads); Assert.Equal(SpeechCultMeshRoles.RenderWorker, payloads[0].WorkerRole); + // One payload, four receipts: PayloadId is a role (many rows share it), not a unique index (CultLib #90). + Assert.Equal(4, admissions.Count(receipt => receipt.PayloadId == "payload-cultmesh-0001")); Assert.Equal(2, admissions.Count(receipt => receipt.Status == SpeechWorkerAdmissionStatus.Admitted)); Assert.Equal(2, admissions.Count(receipt => receipt.Status == SpeechWorkerAdmissionStatus.Rejected)); Assert.All(admissions.Where(receipt => receipt.Status == SpeechWorkerAdmissionStatus.Admitted), receipt => Assert.NotEmpty(receipt.AssignedRequestIds)); @@ -284,6 +286,30 @@ public sealed class SpeechDistributedTrainingTests return loss / actual.Count; } + [Fact] + public async Task OneRequestMayHaveAFailedAndAScoredResultInTheSameStore() + { + var directory = TempDirectory(); + try + { + var resultStore = Path.Combine(directory, "speech-results.cc"); + var request = SpeechDistributedTrainingCoordinator.CreateRenderRequests("batch-roles", Pipeline(), TrainingSet())[0]; + var scored = SpeechDistributedTrainingCoordinator.ScoreControlVectorRequests([request], "worker-a"); + var failed = SpeechDistributedTrainingCoordinator.ScoreControlVectorRequests([request with { CandidateControlVector = [] }], "worker-b"); + await SpeechDistributedTrainingCultCacheStore.UpsertResultsAsync(resultStore, [.. scored, .. failed]); + + // RequestId is a role: a retried request keeps its failed result beside the later scored one (CultLib #90 makes CultIndex unique). + var loaded = await SpeechDistributedTrainingCultCacheStore.ReadResultsAsync(resultStore); + Assert.Equal(2, loaded.Count(result => result.RequestId == request.RequestId)); + Assert.Contains(loaded, result => result.Status == SpeechRenderStatus.Failed); + Assert.Contains(loaded, result => result.Status == SpeechRenderStatus.Succeeded); + } + finally + { + Directory.Delete(directory, recursive: true); + } + } + private static string TempDirectory() { var path = Path.Combine(Path.GetTempPath(), $"aquasynth-speech-distributed-{Guid.NewGuid():N}"); diff --git a/tests/AquaSynth.Dsl.Tests/StopClosureAndControlRenderTests.cs b/tests/AquaSynth.Dsl.Tests/StopClosureAndControlRenderTests.cs new file mode 100644 index 0000000..973d9e5 --- /dev/null +++ b/tests/AquaSynth.Dsl.Tests/StopClosureAndControlRenderTests.cs @@ -0,0 +1,183 @@ +using AquaSynth.Dsl; +using AquaSynth.Faust; +using Xunit.Abstractions; + +namespace AquaSynth.Dsl.Tests; + +// Renders behind the tract-coarticulation findings of the PR 52 Soul pass: what the closing ramp into a stop sounds like, what the DSP +// does with a stop's 12 ms opening, and what the control smoothers and the word attack do at the start of a render. Each reads the +// rendered signal (or the DSP's own probe), not the emitted text. +[Collection("NativeFaust")] +public sealed class StopClosureAndControlRenderTests(ITestOutputHelper output) +{ + private const int Rate = 48000; + + private static AquaSynthCompiledPatch? Compile(string source, float seconds) + { + using var compiler = new AquaSynthPatchCompiler(new AquaSynthNativeOptions(SampleRate: Rate)); + if (!compiler.TryCompileSource(new AquaSynthCompileIdentity("stop_closure", "stop_closure", ""), source, seconds, out var patch, out var error)) + { + Assert.Fail($"compile failed: {error}"); + } + + return patch; + } + + private static string Word(string ipa) => IpaGestureExperiment.BuildWordScript( + [(new IpaGestureExperimentTarget("w_0_a", "a", "", 0.18f), 1f), (new IpaGestureExperimentTarget("w_1_c", ipa, "", 0.095f), 1f), (new IpaGestureExperimentTarget("w_2_a", "a", "", 0.18f), 1f)], 140); + + // Power above 4 kHz (100 Hz grid to 12 kHz) of a Hann-windowed stretch. + private static double HighPower(float[] x, double from, double to) + { + var a = (int)(from * Rate); + var n = (int)((to - from) * Rate); + var seg = Enumerable.Range(0, n).Select(i => x[a + i] * (0.5 - 0.5 * Math.Cos(2 * Math.PI * i / (n - 1)))).ToArray(); + double sum = 0; + for (var hz = 4000; hz < 12000; hz += 100) + { + double re = 0, im = 0; + for (var i = 0; i < n; i++) { var w = 2 * Math.PI * hz * i / Rate; re += seg[i] * Math.Cos(w); im -= seg[i] * Math.Sin(w); } + sum += re * re + im * im; + } + + return sum; + } + + // The 30 ms boundary ramp into a stop moves the contact, constriction and lip surfaces through every aperture between open and shut. The + // burst noise is 4 * opening * (1 - opening) times a gain, and the gain was held through the closure, so each of those passes was a 15-20 ms + // hiss before the stop. The interval is the stop's start to its release, against the same length of the vowel before it, in the >4 kHz + // band, with the glottal source silent and the burst gain kept: the voiced stops' own voicing runs through the ramp and its harmonics + // fill that band (/d/ read -11.6 dB with the source on and -52.8 with it off; with the source on it swings between -13.5 and -9.3 dB + // with f0 alone, 139.5 to 145 Hz, as the harmonics fall on and off the 100 Hz grid), so with the source on the measure cannot tell the + // voicing from the hiss. Source off, the burst gain held through the closure read +5 dB (/p/) and +14 dB (/t/, /d/) on 3b62b9c; gated, + // every stop reads -52 dB. -30 is the bound between the two. /g/ is not here: it is not in TryIpaDescriptor, so it would render the default approximant and could not fail. + [FaustTheory] + [InlineData("p")] + [InlineData("b")] + [InlineData("t")] + [InlineData("d")] + [InlineData("k")] + public void ClosingIntoAStopMakesNoHighFrequencyNoise(string ipa) + { + var parsed = PatchScript.Parse(Word(ipa)); + var voiceless = parsed with + { + ControlSplines = parsed.ControlSplines.Select(s => s.Name.StartsWith("w_1_c_source_level", StringComparison.Ordinal) + ? s with { Points = s.Points.Select(q => q with { Value = 0, OutValue = 0, InValue = 0 }).ToArray() } + : s).ToArray() + }; + using var patch = Compile(FaustEmitter.Emit(voiceless, new FaustExportOptions("stop_closure", SampleRate: Rate)).Source, 0.5f); + var x = patch!.Render(0.5f); + const double from = 0.18; + var release = PatchScript.StopReleaseStartSeconds(0.095f); + var closing = HighPower(x, from, from + release); + var vowel = HighPower(x, from - release, from); + var db = 10 * Math.Log10(closing / vowel); + output.WriteLine($"/{ipa}/ >4 kHz energy of the closing interval {db:F1} dB re the vowel's before it, source silent"); + Assert.True(db < -30, $"/{ipa}/ closes with {db:F1} dB re the vowel's own >4 kHz energy"); + } + + // The Faust lowering of a Bezier segment ignores the handle times and runs the curve on the linear fraction of the segment; the C# timeline + // solves the curve's time, so a handle longer than the segment folds it. The stop's 12 ms opening had 23.75 ms handles: the contact opening + // read 0.05 to 0.87 between 6 and 7 ms in C# and 0.46 to 0.57 in the DSP. This reads the opening through the DSP's own contact reservoir probe + // (stored pressure + (1 - opening) * resistance, both at their defaults once the other splines are dropped) and compares it with the timeline. + [FaustFact] + public void TheDspRendersTheStopOpeningAsTheTimelineEvaluatesIt() + { + var script = Word("p"); + var parsed = PatchScript.Parse(script); + var contact = parsed.ControlSplines.Where(s => s.SurfacePath == "/vocal/contacts/0/opening").ToArray(); + var alone = parsed with { ControlSplines = contact }; + var timeline = new ControlSplineTimeline(contact); + var source = FaustEmitter.Emit(alone, new FaustExportOptions("stop_opening", SampleRate: Rate, DebugProbeUi: true)).Source; + using var patch = Compile(source, 0.5f); + using var stream = patch!.CreateStreamingPatch(); + var reservoir = stream.ProbePaths.Single(p => p.EndsWith("/contact/contact/reservoir", StringComparison.Ordinal)); + var release = 0.18f + PatchScript.StopReleaseStartSeconds(0.095f); + var outputs = new[] { new float[48] }; + var dsp = new List<(float Time, float Reservoir)>(); + for (var block = 0; block < 400; block++) + { + stream.ProcessBlock([], outputs, 48); + var t = (block + 1) * 48f / Rate; + if (t >= release - 0.004f && t <= release + 0.016f) dsp.Add((t, stream.ReadProbe(reservoir))); + } + + // reservoir is linear in the opening; scale it by its own end points + var shut = dsp.First().Reservoir; + var open = dsp.Last().Reservoir; + var worst = 0f; + foreach (var (time, value) in dsp) + { + var dspOpening = (value - shut) / (open - shut); + var curve = timeline.ValueAt("/vocal/contacts/0/opening", time); + var shutValue = timeline.ValueAt("/vocal/contacts/0/opening", dsp.First().Time); + var openValue = timeline.ValueAt("/vocal/contacts/0/opening", dsp.Last().Time); + var timelineOpening = (curve - shutValue) / (openValue - shutValue); + worst = MathF.Max(worst, MathF.Abs(dspOpening - timelineOpening)); + } + + output.WriteLine($"worst difference between the DSP's opening and the timeline's over the release: {worst:F3}"); + Assert.True(worst < 0.08f, $"the DSP's stop opening departs from the timeline by {worst:F3}"); + } + + // The same fact without the DSP: every handle of an authored spline is a third of its segment, so the C# curve is the curve the lowering runs. + [Fact] + public void EveryAuthoredSplineHandleIsAThirdOfItsSegment() + { + var splines = PatchScript.Parse(Word("p")).ControlSplines.Where(s => s.Points.Count > 1).ToArray(); + Assert.NotEmpty(splines); + foreach (var spline in splines) + { + for (var i = 0; i + 1 < spline.Points.Count; i++) + { + var third = (spline.Points[i + 1].TimeSeconds - spline.Points[i].TimeSeconds) / 3; + Assert.Equal(third, spline.Points[i].OutTimeOffsetSeconds, 5); + Assert.Equal(third, spline.Points[i + 1].InTimeOffsetSeconds, 5); + } + } + } + + // Replaces the emitted patch's process with one control through the emitted smoo_init, so the smoother under test is the one the exporter + // writes. Starts at its input at sample 0, and moves at si.smoo's 22.7 ms time constant when the control moves (5 ms gave 0.89 here). + [FaustFact] + public void ASmootherStartsAtItsInputAndMovesAtSiSmoosPace() + { + var emitted = FaustEmitter.Emit(PatchScript.Parse(Word("p")), new FaustExportOptions("smoo", SampleRate: Rate)).Source; + var lines = emitted.Split('\n').Select(l => l.StartsWith("process =", StringComparison.Ordinal) ? "process = hslider(\"/vocal/sources/0/pressure\", 0.7, 0, 1, 0.001) : smoo_init;" : l); + using var patch = Compile(string.Join('\n', lines), 0.2f); + using var stream = patch!.CreateStreamingPatch(); + var first = new[] { new float[480] }; + stream.ProcessBlock([], first, 480); + output.WriteLine($"sample 0 {first[0][0]:F4}, after 10 ms {first[0][479]:F4}"); + Assert.Equal(0.7f, first[0][0], 4); + Assert.Equal(0.7f, first[0][479], 4); + var after = new[] { new float[240] }; + stream.ProcessBlock([], after, 240, new Dictionary { ["/vocal/sources/0/pressure"] = 1f }); + // 5 ms into a step from 0.7 to 1 with a 22.7 ms time constant + var expected = 0.7 + 0.3 * (1 - Math.Pow(1 - 44.1 / Rate, 0.005 * Rate)); + output.WriteLine($"5 ms after a step to 1: {after[0][239]:F4}, si.smoo would be {expected:F4}"); + Assert.InRange(after[0][239], expected - 0.01, expected + 0.01); + } + + // A word starts with a 12 ms attack, not a step: the control smoothers no longer fade it in, and a step onset would be a click. + [FaustFact] + public void AWordBeginsWithATwelveMillisecondAttack() + { + double FirstMillisecondsPeak(string script) + { + using var patch = Compile(FaustEmitter.Emit(PatchScript.Parse(script), new FaustExportOptions("attack", SampleRate: Rate)).Source, 0.4f); + var x = patch!.Render(0.4f); + var steady = x.Skip((int)(0.05 * Rate)).Take((int)(0.1 * Rate)).Max(Math.Abs); + return x.Take((int)(0.002 * Rate)).Max(Math.Abs) / steady; + } + + var script = IpaGestureExperiment.BuildWordScript([(new IpaGestureExperimentTarget("w_0_a", "a", "", 0.3f), 1f)], 140); + Assert.Contains("attack=.012", script); + var attacked = FirstMillisecondsPeak(script); + var stepped = FirstMillisecondsPeak(script.Replace("attack=.012 ", "", StringComparison.Ordinal)); + output.WriteLine($"peak of the first 2 ms re the steady peak: with the attack {attacked:F3}, without {stepped:F3}"); + Assert.True(attacked < 0.25, $"the first 2 ms reach {attacked:F2} of the steady peak"); + Assert.True(stepped > 2 * attacked, "without the attack the onset is a step"); + } +} diff --git a/tests/AquaSynth.Dsl.Tests/StoreFileNameTests.cs b/tests/AquaSynth.Dsl.Tests/StoreFileNameTests.cs new file mode 100644 index 0000000..65a8006 --- /dev/null +++ b/tests/AquaSynth.Dsl.Tests/StoreFileNameTests.cs @@ -0,0 +1,228 @@ +using AquaSynth.Faust; + +namespace AquaSynth.Dsl.Tests; + +[Collection("NativeFaust")] +public sealed class StoreFileNameTests +{ + [Fact] + public void DistinctIdsGetDistinctNames() + { + var ids = new[] { "a/b", "a b", "a:b", "a-b", "a_b", "a\\b", "a?b" }; + var names = ids.Select(StoreFileName.From).ToArray(); + Assert.Equal(ids.Length, names.Distinct(StringComparer.OrdinalIgnoreCase).Count()); + } + + [Theory] + [InlineData("dings.warm-bell")] + [InlineData("demo-stream-block")] + [InlineData("a_b.c-1")] + [InlineData("conx")] + [InlineData("com10")] + public void CleanIdsPassThroughUnchanged(string id) => Assert.Equal(id, StoreFileName.From(id)); + + [Fact] + public void RewrittenIdOutputAsAnIdDoesNotCollideWithTheOriginal() + { + var rewritten = StoreFileName.From("a b"); + Assert.NotEqual(rewritten, StoreFileName.From(rewritten)); + Assert.NotEqual(StoreFileName.From("a_b"), rewritten); + } + + [Fact] + public void IdsDifferingOnlyByCaseGetDistinctNames() + { + var names = new[] { "A", "a", "Ab", "aB", "ab" }.Select(StoreFileName.From).ToArray(); + Assert.Equal(names.Length, names.Distinct(StringComparer.OrdinalIgnoreCase).Count()); + } + + [Theory] + [InlineData("CON")] + [InlineData("nul")] + [InlineData("Aux.txt")] + [InlineData("com1")] + [InlineData("COM9.f32")] + [InlineData("lpt0")] + [InlineData("prn.cc")] + [InlineData("nul.x")] + [InlineData("LPT1.x")] + [InlineData("CON.tar.gz")] + public void WindowsDeviceStemsAreRewritten(string id) + { + var name = StoreFileName.From(id); + Assert.StartsWith("_", name); + Assert.NotEqual(id.ToLowerInvariant(), name.ToLowerInvariant()); + Assert.Matches("-[0-9a-f]{16}$", name); + var stem = name.Split('.')[0].TrimEnd(' '); + Assert.DoesNotMatch("^(?i:con|prn|aux|nul|com[0-9]|lpt[0-9])$", stem); + } + + [Theory] + [InlineData("nul ")] + [InlineData("COM\u00b9")] + [InlineData("COM1 .x")] + [InlineData("con\u00e9.x")] + [InlineData("NUL.")] + public void DeviceLookalikesNeverLeaveAReservedStem(string id) + { + var name = StoreFileName.From(id); + Assert.Matches("^[A-Za-z0-9._-]+$", name); + Assert.DoesNotMatch("^(?i:con|prn|aux|nul|com[0-9]|lpt[0-9])$", name.Split('.')[0].TrimEnd(' ')); + Assert.Matches("-[0-9a-f]{16}$", name); + } + + [Fact] + public void OverlongIdsAreBoundedAndStayDistinct() + { + var a = StoreFileName.From(new string('x', 400)); + var b = StoreFileName.From(new string('x', 399) + "y"); + Assert.True(a.Length + ".cc".Length <= 255); + Assert.NotEqual(a, b); + } + + [Fact] + public void RewrittenIdsCarryHashOfTheOriginal() + { + // sha256("a b") begins c2a5..., checked independently of the implementation. + var expected = Convert.ToHexString(System.Security.Cryptography.SHA256.HashData("a b"u8.ToArray()))[..16].ToLowerInvariant(); + Assert.Equal($"a_b-{expected}", StoreFileName.From("a b")); + } + + [Theory] + [InlineData(".")] + [InlineData("..")] + [InlineData("")] + public void DotsAndEmptyAreRefused(string id) => Assert.Throws(() => StoreFileName.From(id)); + + [Theory] + [InlineData("../..")] + [InlineData("../../etc/passwd")] + [InlineData("..\\..\\x")] + [InlineData("/abs/path")] + [InlineData("C:\\abs")] + [InlineData("a.")] + [InlineData("a/..")] + public void NoIdCanMakePathCombineEscapeTheRoot(string id) + { + var root = Path.Combine(Path.GetTempPath(), "store-root-probe", "store"); + var name = StoreFileName.From(id); + Assert.Equal(name, Path.GetFileName(name)); + Assert.NotEqual(".", name); + Assert.NotEqual("..", name); + var full = Path.GetFullPath(Path.Combine(root, name)); + Assert.Equal(Path.GetFullPath(root), Path.GetDirectoryName(full)); + } + + [Fact] + public async Task DaemonSampleFilesForDotDotCommandIdStayInsideStore() + { + var parent = Path.Combine(Path.GetTempPath(), "aquasynth-sfn-" + Guid.NewGuid().ToString("N")); + var storeRoot = Path.Combine(parent, "store"); + using var service = new AquaSynthDaemonService(new AquaSynthDaemonOptions(storeRoot)); + + // A patch that compiles reaches the samples/ write; without native Faust only the compile-failed branch runs. + var result = await service.SampleAsync(new AquaSynthInstrumentSampleCommand( + "..", "test.sine", "test_sine", + "voice wave=sine freq=440 gain=.2 attack=.001 sustain=.05 decay=.05", DurationSeconds: 0.1f)); + + Assert.Empty(Directory.GetFiles(parent)); + Assert.Empty(Directory.GetDirectories(parent).Where(d => Path.GetFileName(d) != "store")); + if (string.Equals(result.RenderReceipt.Status, "succeeded", StringComparison.Ordinal)) + { + Assert.Equal(storeRoot, Path.GetDirectoryName(Path.GetDirectoryName(UriPath(result.RenderReceipt.Float32Uri)))); + Assert.Equal(storeRoot, Path.GetDirectoryName(Path.GetDirectoryName(UriPath(result.RenderReceipt.WavUri)))); + } + } + + [Fact] + public void IpaRoundCandidatesWithCollidingSanitisedIdsKeepDistinctFilesAndStayInRoot() + { + var root = Path.Combine(Path.GetTempPath(), $"aquasynth-ipa-sfn-{Guid.NewGuid():N}"); + var result = IpaGestureExperiment.WriteRound( + root, + "round-sfn", + [new IpaGestureExperimentTarget("a/b", "p", "voiceless_bilabial_plosive", .1f), new IpaGestureExperimentTarget("a-b", "m", "voiced_bilabial_nasal", .1f)], + [new IpaGestureExperimentVariant("base")]); + + Assert.Equal(2, result.Candidates.Select(c => c.Id).Distinct(StringComparer.OrdinalIgnoreCase).Count()); + Assert.All(result.Candidates, c => Assert.StartsWith(Path.GetFullPath(root), Path.GetFullPath(c.ScriptPath))); + Assert.Throws(() => IpaGestureExperiment.WriteRound(root, "..", [new IpaGestureExperimentTarget("a", "p", "voiceless_bilabial_plosive")], [new IpaGestureExperimentVariant("base")])); + } + + [Fact] + public void IpaRoundJoinedTargetVariantIdsThatConcatenateEquallyKeepDistinctFiles() + { + var root = Path.Combine(Path.GetTempPath(), $"aquasynth-ipa-join-{Guid.NewGuid():N}"); + var result = IpaGestureExperiment.WriteRound( + root, + "round-join", + [new IpaGestureExperimentTarget("a", "p", "voiceless_bilabial_plosive", .1f), new IpaGestureExperimentTarget("a-b", "m", "voiced_bilabial_nasal", .1f)], + [new IpaGestureExperimentVariant("b-c"), new IpaGestureExperimentVariant("c")]); + + Assert.Equal(4, result.Candidates.Count); + Assert.Equal(4, result.Candidates.Select(c => c.Id).Distinct(StringComparer.OrdinalIgnoreCase).Count()); + Assert.Equal(4, result.Candidates.Select(c => c.ScriptPath).Distinct(StringComparer.OrdinalIgnoreCase).Count()); + } + + [Fact] + public void IpaRoundPairsWithSeparatorCharactersInIdsStayDistinct() + { + var root = Path.Combine(Path.GetTempPath(), $"aquasynth-ipa-pair-{Guid.NewGuid():N}"); + string[] targets = ["a", "a/b", "a-b", "a_b", "A", "a b"]; + string[] variants = ["b-c", "c", "b/c", "b_c", "B-C", "b c"]; + var result = IpaGestureExperiment.WriteRound( + root, + "round-pairs", + targets.Select(t => new IpaGestureExperimentTarget(t, "p", "voiceless_bilabial_plosive", .1f)).ToArray(), + variants.Select(v => new IpaGestureExperimentVariant(v)).ToArray()); + + Assert.Equal(36, result.Candidates.Count); + Assert.Equal(36, result.Candidates.Select(c => c.Id).Distinct(StringComparer.OrdinalIgnoreCase).Count()); + Assert.Equal(36, result.Candidates.Select(c => c.ScriptPath).Distinct(StringComparer.OrdinalIgnoreCase).Count()); + } + + [Theory] + [InlineData("../x")] + [InlineData("..\\x")] + [InlineData("/etc/x")] + [InlineData("C:\\x")] + public void BatchIdsFromTheCommandLineCannotLeaveTheArtifactRoot(string batchId) + { + var root = Path.Combine(Path.GetTempPath(), "artifact-root-probe", "artifacts"); + var full = Path.GetFullPath(StoreFileName.Combine(root, batchId)); + Assert.Equal(Path.GetFullPath(root), Path.GetDirectoryName(full)); + } + + [Theory] + [InlineData("\\\\host\\share\\x")] + [InlineData("C:x")] + [InlineData("a:b")] + [InlineData("x.")] + [InlineData("x ")] + [InlineData("x. ")] + public void WindowsAbsoluteAndTrailingDotOrSpaceIdsStayContainedAndPlain(string id) + { + var root = Path.Combine(Path.GetTempPath(), "artifact-root-probe", "artifacts"); + var name = StoreFileName.From(id); + Assert.Matches("^[A-Za-z0-9._-]+$", name); + Assert.False(name.EndsWith('.') || name.EndsWith(' ')); + Assert.Equal(Path.GetFullPath(root), Path.GetDirectoryName(Path.GetFullPath(StoreFileName.Combine(root, id)))); + } + + [Theory] + [InlineData("../x")] + [InlineData("a/b")] + [InlineData("name:with:colons")] + public void LoweredDspFileNameStaysInTheOutputDirectory(string name) + { + var outDir = Path.Combine(Path.GetTempPath(), "aqualower-probe", "out"); + var file = StoreFileName.From(name) + ".dsp"; + Assert.Equal(Path.GetFullPath(outDir), Path.GetDirectoryName(Path.GetFullPath(Path.Combine(outDir, file)))); + } + + [Fact] + public void BareDotDotBatchIdIsRefused() => + Assert.Throws(() => StoreFileName.Combine(Path.GetTempPath(), "..")); + + private static string UriPath(string uri) => new Uri(uri).LocalPath; +} diff --git a/tests/AquaSynth.Dsl.Tests/StoryVowelAreasTests.cs b/tests/AquaSynth.Dsl.Tests/StoryVowelAreasTests.cs new file mode 100644 index 0000000..e38739b --- /dev/null +++ b/tests/AquaSynth.Dsl.Tests/StoryVowelAreasTests.cs @@ -0,0 +1,74 @@ +using System.Globalization; +using AquaSynth.Dsl; + +namespace AquaSynth.Dsl.Tests; + +// StoryVowelAreas.cs is the code owner of Story (2008) Table I; docs/research/data/story2008-area-functions.csv is the cited copy. +// Nothing here says how any vowel sounds: these tests guard the transcription, the descriptor lookup and the sample-rate threading. +public sealed class StoryVowelAreasTests +{ + private static string RepoFile(params string[] relative) + { + for (var dir = new DirectoryInfo(AppContext.BaseDirectory); dir is not null; dir = dir.Parent) + { + if (File.Exists(Path.Combine(dir.FullName, "AquaSynth.sln")) || Directory.Exists(Path.Combine(dir.FullName, "docs", "research"))) + { + return Path.Combine([dir.FullName, .. relative]); + } + } + + throw new InvalidOperationException("repository root not found above " + AppContext.BaseDirectory); + } + + [Fact] + public void EveryCellOfTheCodeTableEqualsTheCitedCsv() + { + var lines = File.ReadAllLines(RepoFile("docs", "research", "data", "story2008-area-functions.csv")) + .Where(line => !line.StartsWith('#') && line.Length > 0).ToArray(); + var header = lines[0].Split(','); + Assert.Equal("section", header[0]); + Assert.Equal(StoryVowelAreas.All.Select(v => v.Ipa), header.Skip(1)); + var sections = lines.Skip(1).Where(line => char.IsDigit(line[0])).Select(line => line.Split(',')).ToArray(); + Assert.Equal(StoryVowelAreas.Sections, sections.Length); + for (var v = 0; v < StoryVowelAreas.All.Count; v++) + { + Assert.Equal(StoryVowelAreas.Sections, StoryVowelAreas.All[v].Areas.Count); + for (var s = 0; s < sections.Length; s++) + { + Assert.Equal(float.Parse(sections[s][v + 1], CultureInfo.InvariantCulture), StoryVowelAreas.All[v].Areas[s]); + } + } + } + + [Fact] + public void AsciiIdsAreUniqueAndDescriptorsSelectTheirVowelExactly() + { + Assert.Equal(11, StoryVowelAreas.All.Select(v => v.Id).Distinct().Count()); + Assert.Equal(11, StoryVowelAreas.All.Select(v => v.Descriptor).Distinct().Count()); + foreach (var vowel in StoryVowelAreas.All) + { + Assert.True(vowel.Descriptor.EndsWith("_vowel", StringComparison.Ordinal)); + Assert.Equal(vowel.Id, StoryVowelAreas.IdForDescriptor(vowel.Descriptor)); + Assert.True(PatchScript.TryIpaDescriptor(vowel.Ipa, out var descriptor)); + Assert.Equal(vowel.Descriptor, descriptor); + } + + Assert.Equal("ah", StoryVowelAreas.IdForDescriptor("voiced_open_back_unrounded_vowel")); + PatchScript.TryIpaDescriptor("a", out var a); + Assert.Equal("ah", StoryVowelAreas.IdForDescriptor(a)); + // The schwa and a vowel descriptor with no Story shape both select no vowel: the geometric-mean shape. + PatchScript.TryIpaDescriptor("ə", out var schwa); + Assert.Null(StoryVowelAreas.IdForDescriptor(schwa)); + Assert.Null(StoryVowelAreas.IdForDescriptor("voiced_close_central_unrounded_vowel")); + } + + [Fact] + public void ResamplingInterpolatesBetweenSectionCentresAndHoldsTheEnds() + { + float[] areas = [1, 3, 5, 7]; + Assert.Equal(1, StoryVowelAreas.AreaAt(areas, 0)); + Assert.Equal(7, StoryVowelAreas.AreaAt(areas, 1)); + Assert.Equal(3, StoryVowelAreas.AreaAt(areas, 0.375f), 4); + Assert.Equal(4, StoryVowelAreas.AreaAt(areas, 0.5f), 4); + } +} diff --git a/tests/AquaSynth.Dsl.Tests/StressAndReleaseRenderTests.cs b/tests/AquaSynth.Dsl.Tests/StressAndReleaseRenderTests.cs new file mode 100644 index 0000000..74a3677 --- /dev/null +++ b/tests/AquaSynth.Dsl.Tests/StressAndReleaseRenderTests.cs @@ -0,0 +1,637 @@ +using AquaSynth.Dsl; +using AquaSynth.Faust; +using Xunit.Abstractions; + +namespace AquaSynth.Dsl.Tests; + +// Renders the two cues the tract gained in this pass through the native Faust path: the source's f0 ratio (the stress pitch cue) and +// the plosive release burst. Each asserts a measured signal, not the emitted text. +[Collection("NativeFaust")] +public sealed class StressAndReleaseRenderTests(ITestOutputHelper output) +{ + private const int Rate = 48000; + + private static float[] Render(string script, float seconds, Func? edit = null) + { + var parsed = PatchScript.Parse(script); + var source = FaustEmitter.Emit(edit is null ? parsed : edit(parsed), new FaustExportOptions("stress_release", SampleRate: Rate)).Source; + using var compiler = new AquaSynthPatchCompiler(new AquaSynthNativeOptions(SampleRate: Rate)); + if (!compiler.TryCompileSource(new AquaSynthCompileIdentity("stress_release", "stress_release", ""), source, seconds, out var patch, out var error)) + { + Assert.Fail($"compile failed: {error}"); + } + + using (patch) return patch!.Render(seconds); + } + + // The stop's release time and the start of its closure (every articulator at its shut value, after the boundary ramp into it), both from the + // one owner in PatchScript rather than as fractions of the gesture; the consonant is [from, to). + private static double ReleaseAt(double from, double to) => from + PatchScript.StopReleaseStartSeconds((float)(to - from)); + private static double ClosureFrom(double from) => from + PatchScript.PhoneTransitionSeconds; + + private static double Rms(float[] x, double from, double to) + { + var a = (int)(from * Rate); + var b = Math.Min(x.Length, (int)(to * Rate)); + double sum = 0; + for (var i = a; i < b; i++) sum += (double)x[i] * x[i]; + return Math.Sqrt(sum / Math.Max(1, b - a)); + } + + private static double Peak(float[] x, double from, double to) => + x.Skip((int)(from * Rate)).Take((int)((to - from) * Rate)).Max(Math.Abs); + + // f0 by the highest autocorrelation peak between 60 and 400 Hz. + private static double F0(float[] x, double from, double to) + { + var seg = x.Skip((int)(from * Rate)).Take((int)((to - from) * Rate)).Select(v => (double)v).ToArray(); + double best = 0; var bestLag = 0; + for (var lag = Rate / 400; lag <= Rate / 60; lag++) + { + double c = 0; + for (var i = 0; i + lag < seg.Length; i++) c += seg[i] * seg[i + lag]; + if (c > best) { best = c; bestLag = lag; } + } + + return (double)Rate / bestLag; + } + + [FaustFact] + public void SourcePitchRatioScalesTheRenderedF0() + { + var f0 = (float ratio) => + { + var script = IpaGestureExperiment.BuildWordScript([(new IpaGestureExperimentTarget("w_0_a", "a", "", 0.5f, PitchRatio: ratio), 1f)], 110); + var x = Render(script, 0.6f); + return F0(x, 0.2, 0.5); + }; + var unity = f0(1f); + var up = f0(1.5f); + output.WriteLine($"f0 at ratio 1: {unity:F1}, ratio 1.5: {up:F1}"); + Assert.InRange(unity, 108, 112); + Assert.InRange(up, 163, 167); + } + + [FaustFact] + public void IntensityIsAnAmplitudeFactorOnTheRenderedLevel() + { + double Level(float intensity) + { + var script = IpaGestureExperiment.BuildWordScript([(new IpaGestureExperimentTarget("w_0_a", "a", "", 0.5f), intensity)], 110); + var x = Render(script, 0.6f); + return Rms(x, 0.3, 0.5); + } + + var unity = Level(1f); + var amplitude = WeksaWordPlan.CueFor(WeksaStress.Primary).Amplitude; + var stressed = Level(amplitude); + var db = 20 * Math.Log10(stressed / unity); + output.WriteLine($"level at the primary-stress amplitude against 1: {db:F2} dB (factor wanted {20 * Math.Log10(amplitude):F2})"); + var wanted = 20 * Math.Log10(amplitude); + Assert.InRange(db, wanted - 0.3, wanted + 0.3); + } + + [FaustFact] + public void PlosiveReleaseBurstIsAboveTheClosureAndBelowTheFollowingVowel() + { + var script = IpaGestureExperiment.BuildWordScript( + [(new IpaGestureExperimentTarget("w_0_b", "b", "", 0.095f), 1f), (new IpaGestureExperimentTarget("w_1_a", "a", "", 0.18f), 1f)], 140); + var withBurst = Render(script, 0.4f); + var withoutBurst = Render(script, 0.4f, p => p with { ControlSplines = p.ControlSplines.Where(s => !s.SurfacePath.EndsWith("/release_flow", StringComparison.Ordinal)).ToArray() }); + // Everything but the burst's noise is deterministic and shared, so the difference of the two renders is the burst alone. + var burst = withBurst.Zip(withoutBurst, (x, y) => x - y).ToArray(); + const double vowelFrom = 0.095; + var releaseFrom = ReleaseAt(0, vowelFrom); + var vowelPeak = Peak(withBurst, vowelFrom + 0.06, vowelFrom + 0.18); + var burstPeak = Peak(burst, releaseFrom, vowelFrom + 0.01); + var burstBeforeRelease = Peak(burst, 0, releaseFrom - 0.005); + var burstAfter = Peak(burst, vowelFrom + 0.04, 0.4); + output.WriteLine($"burst peak {burstPeak:F4} in the release, {burstBeforeRelease:F5} in the closure, {burstAfter:F5} in the vowel; vowel peak {vowelPeak:F4}"); + Assert.True(burstPeak > 0.004, "the burst is a transient of audible size in the release window"); + Assert.True(burstBeforeRelease < burstPeak / 20, "the closure is silent: no burst before the release"); + Assert.True(burstAfter < burstPeak / 5, "the burst is over once the vowel is open"); + Assert.True(burstPeak < vowelPeak / 2, "the burst stays well under the following vowel's peak (the b level fix)"); + } + + // The burst is noise whose amplitude is release_flow times the stored pressure through the opening, and release_flow was held at its + // gain from the plosive's gesture on, so it never ended: every later contact state made noise too. In /bin/ the "burst" alone (the + // difference of the renders with and without release_flow) peaked at 0.0302 in the final /n/ and the tail, above its 0.0226 at the + // release itself, and in /nibu/ the leading /n/ and /i/ carried it backward (a spline holds its first value before its first point). + [FaustFact] + public void PlosiveBurstExistsOnlyAroundItsOwnRelease() + { + IpaGestureExperimentTarget Phone(string label, string ipa, float seconds) => new($"w_{label}", ipa, "", seconds); + var script = IpaGestureExperiment.BuildWordScript( + [(Phone("0_n", "n", 0.095f), 1f), (Phone("1_a", "a", 0.18f), 1f), (Phone("2_b", "b", 0.095f), 1f), (Phone("3_a", "a", 0.18f), 1f), (Phone("4_n", "n", 0.095f), 1f)], 140); + var withBurst = Render(script, 0.77f); + var withoutBurst = Render(script, 0.77f, p => p with { ControlSplines = p.ControlSplines.Where(s => !s.SurfacePath.EndsWith("/release_flow", StringComparison.Ordinal)).ToArray() }); + var burst = withBurst.Zip(withoutBurst, (x, y) => x - y).ToArray(); + const double bFrom = 0.275, bTo = 0.37; + var atRelease = Peak(burst, ReleaseAt(bFrom, bTo) - 0.003, bTo + 0.02); + var leading = Peak(burst, 0, bFrom); + var trailing = Peak(burst, bTo + 0.12, 0.77); + output.WriteLine($"burst peak {atRelease:F4} at the release, {leading:F4} before the plosive (n, a), {trailing:F4} after it (a, n, tail)"); + Assert.True(atRelease > 0.004, "the burst is still there at the release"); + Assert.True(leading < atRelease / 20, "no burst noise before the plosive's own gesture"); + Assert.True(trailing < atRelease / 10, "no burst noise in the vowel, the nasal and the tail after the plosive"); + } + + // The burst gain's window, read off the spline itself: it exists only from the stop's release start, one ramp (0.15 of the release window) + // before it and one after the stop's end. A gain held through the closure made the closing ramp a hiss (ClosingIntoAStopMakesNoHighFrequencyNoise). + [Fact] + public void PlosiveBurstGainWindowIsOneRampEitherSideOfTheRelease() + { + IpaGestureExperimentTarget Phone(string label, string ipa, float seconds) => new($"w_{label}", ipa, "", seconds); + var script = IpaGestureExperiment.BuildWordScript( + [(Phone("0_n", "n", 0.095f), 1f), (Phone("1_a", "a", 0.18f), 1f), (Phone("2_b", "b", 0.095f), 1f), (Phone("3_a", "a", 0.18f), 1f), (Phone("4_n", "n", 0.095f), 1f)], 140); + var window = PatchScript.Parse(script).ControlSplines.Single(s => s.SurfacePath.EndsWith("/release_flow", StringComparison.Ordinal) && s.Points.Any(p => p.Value > 0)); + const double start = 0.275, duration = 0.095; + var release = start + PatchScript.StopReleaseStartSeconds((float)duration); + var ramp = 0.15 * (start + duration - release); + Assert.Equal((release - ramp, start + duration + ramp), (window.Points.Min(p => (double)p.TimeSeconds), window.Points.Max(p => (double)p.TimeSeconds)), new TupleTolerance(1e-4)); + } + + // The contact opening, the tongue's constriction and the lips all leave their shut value at the instant the burst gain window opens: one + // owner, StopReleaseStartSeconds. Read off the splines, so a stale constant in one of them (the contact opening 20 ms early while the + // burst keeps the owner's time) fails here and not only through a closure level that happens to cross a bound. + [Theory] + [InlineData("b", "w_2_b")] + [InlineData("t", "w_2_t")] + [InlineData("k", "w_2_k")] + public void AStopsArticulatorsOpenWhereItsBurstGainWindowOpens(string ipa, string name) + { + IpaGestureExperimentTarget Phone(string label, string symbol, float seconds) => new($"w_{label}", symbol, "", seconds); + var script = IpaGestureExperiment.BuildWordScript( + [(Phone("0_n", "n", 0.095f), 1f), (Phone("1_a", "a", 0.18f), 1f), (Phone($"2_{ipa}", ipa, 0.095f), 1f), (Phone("3_a", "a", 0.18f), 1f)], 140); + var splines = PatchScript.Parse(script).ControlSplines; + const double start = 0.275, duration = 0.095; + var release = start + PatchScript.StopReleaseStartSeconds((float)duration); + var gain = splines.Single(s => s.Name.StartsWith(name, StringComparison.Ordinal) && s.SurfacePath.EndsWith("/release_flow", StringComparison.Ordinal)); + var ramp = 0.15 * (start + duration - release); + Assert.Equal(release - ramp, gain.Points.Min(p => (double)p.TimeSeconds), 1e-4); + var paths = ipa == "b" + ? new[] { "/vocal/contacts/0/opening", "/vocal/areas/0/area/constriction_diameter", "/vocal/areas/0/area/lip_opening", "/vocal/radiation/0/aperture" } + : ["/vocal/contacts/0/opening", "/vocal/areas/0/area/constriction_diameter"]; + foreach (var path in paths) + { + var spline = splines.Single(s => s.Name.StartsWith(name, StringComparison.Ordinal) && s.SurfacePath == path); + Assert.Equal(release, spline.Points.Where(p => p.TimeSeconds < start + duration - 1e-6).Max(p => (double)p.TimeSeconds), 1e-4); + } + } + + // The nasal keeps its murmur's small lip opening where a stop shuts the lips (0.04 cm against 0): the shut-lip term is the stop's only. + // Extending it to nasals moves /m/'s first nasal formant by 30 Hz and its murmur level by 0.4 dB and no render test notices, so the + // value is read off the splines. + [Theory] + [InlineData("m", 0.04f)] + [InlineData("b", 0f)] + public void OnlyAStopShutsTheLipsCompletely(string ipa, float shut) + { + var script = IpaGestureExperiment.BuildWordScript( + [(new IpaGestureExperimentTarget("w_0_a", "a", "", 0.18f), 1f), (new IpaGestureExperimentTarget("w_1_c", ipa, "", 0.095f), 1f), (new IpaGestureExperimentTarget("w_2_a", "a", "", 0.18f), 1f)], 140); + foreach (var path in new[] { "/vocal/areas/0/area/lip_opening", "/vocal/radiation/0/aperture" }) + { + var spline = PatchScript.Parse(script).ControlSplines.Single(s => s.Name.StartsWith("w_1_c", StringComparison.Ordinal) && s.SurfacePath == path); + Assert.Equal(shut, spline.Points.Min(p => p.Value), 1e-4); + } + } + + // /ɑ C ɑ/ at the scaffold's own durations (vowel 0.18 s, consonant 0.095 s). Returns the render and the consonant's [start, end). + private (float[]? Samples, double From, double To) VowelConsonantVowel(string ipa, float frequency = 140, string descriptor = "", Func? edit = null) + { + var script = IpaGestureExperiment.BuildWordScript( + [(new IpaGestureExperimentTarget("w_0_a", "a", "", 0.18f), 1f), (new IpaGestureExperimentTarget("w_1_c", ipa, descriptor, 0.095f), 1f), (new IpaGestureExperimentTarget("w_2_a", "a", "", 0.18f), 1f)], frequency); + return (Render(script, 0.5f, edit), 0.18, 0.275); + } + + // Level of the consonant's middle 60 percent against the mean power of the steady parts of the two flanking vowels (the 70 ms before the + // consonant and 90 ms from 30 ms after it), in dB. + private static double ConsonantDbReVowels(float[] x, double from, double to) + { + var v1 = Rms(x, from - 0.08, from - 0.01); + var v2 = Rms(x, to + 0.03, to + 0.12); + var vowels = Math.Sqrt((v1 * v1 + v2 * v2) / 2); + var span = to - from; + return 20 * Math.Log10(Math.Max(1e-9, Rms(x, from + 0.2 * span, to - 0.2 * span)) / vowels); + } + + // Fujimura 1962 and later accounts put the nasal murmur well under the flanking vowels; the target band is 8 to 16 dB under (a DESIGN + // value: no primary level was retrieved, docs/research/articulatory-vocal-tract-sources.md). Two defects held it out of the band: /n/ + // read -3.8 dB while its alveolar closure began to open at half the gesture, a third before the velum and the contact did (now held to + // 0.8: /n/ -8.8, /m/ -12.8), and once the nasal junction sat on its own interface the nose took its share and both read -2.2 and -4.2 dB + // until the nasal path carried its real damping (loss .975 per cell; /m/ -11.8, /n/ -9.6). + [FaustTheory] + [InlineData("m")] + [InlineData("n")] + public void NasalMurmurIsWellBelowTheFlankingVowels(string ipa) + { + var (x, from, to) = VowelConsonantVowel(ipa); + var db = ConsonantDbReVowels(x, from, to); + output.WriteLine($"/{ipa}/ murmur {db:F1} dB re the flanking vowels"); + Assert.InRange(db, -16, -8); + } + + // The primary-stress cue as the plan applies it (amplitude and f0 ratio, same vowel length) against the unstressed vowel, over five vowels: + // the f0 rise alone moves the harmonics against F1 and the level by -1.3 to +1.8 dB by vowel, so the amplitude factor is held to the mean + // rendered result, 2 dB (Sluijter and van Heuven 1996: 1 to 3 dB overall). Measured 1.7 dB with the amplitude at 1.26 (outside the band), 2.0 dB at 1.30. + [FaustFact] + public void PrimaryStressRendersAboutTwoDecibelsLouderOnAverageOverVowels() + { + var cue = WeksaWordPlan.CueFor(WeksaStress.Primary); + var diffs = new List(); + foreach (var vowel in new[] { "i", "a", "u", "e", "o" }) + { + double Level(float ratio, float intensity) + { + var script = IpaGestureExperiment.BuildWordScript([(new IpaGestureExperimentTarget($"w_0_{vowel}", vowel, "", 0.25f, PitchRatio: ratio), intensity)], 140, 0.2f); + var x = Render(script, 0.45f); + return Rms(x, 0.12, 0.22); + } + + var flat = Level(1f, 1f); + var db = 20 * Math.Log10(Level(cue.PitchRatio, cue.Amplitude) / flat); + output.WriteLine($"/{vowel}/ stressed against unstressed: {db:F2} dB"); + diffs.Add(db); + } + + output.WriteLine($"mean {diffs.Average():F2} dB"); + Assert.InRange(diffs.Average(), 1.8, 2.3); + } + + // Standard deviation, so the slow offset a shutting tract leaves behind does not count as sound. + private static double Deviation(float[] x, double from, double to) + { + var seg = x.Skip((int)(from * Rate)).Take((int)((to - from) * Rate)).Select(v => (double)v).ToArray(); + var mean = seg.Average(); + return Math.Sqrt(seg.Sum(v => (v - mean) * (v - mean)) / seg.Length); + } + + // A word-initial plosive's closure (0.03 to 0.058 s of a 0.095 s consonant shut to 0.062 s) against the vowel after it (0.15 to 0.25 s). The + // mouth is shut, so all that gets out is the tract wall radiation of the voiced source: the voice bar, about 30 dB under the vowel for /b/ + // (a DESIGN value, no primary level retrieved; Stevens 1998 per secondary sources has voicing weaker as transglottal pressure falls), and + // near-silent for /p/, whose source is weak. The closure was 0.0000 (below 16 bits) before the wall radiation. + [FaustTheory] + [InlineData("b", -34, -26)] + [InlineData("p", -60, -38)] + [InlineData("d", -34, -26)] + public void PlosiveClosureCarriesTheVoiceBarOnlyWhenVoiced(string ipa, double lowDb, double highDb) + { + var db = VoiceBarDb(ipa); + if (double.IsNaN(db)) return; + output.WriteLine($"/{ipa}/ closure {db:F1} dB re the following vowel"); + Assert.InRange(db, lowDb, highDb); + } + + // The closure's deviation (the slow offset of a shutting tract is not sound) against the following vowel's, for a word-initial consonant. + private double VoiceBarDb(string ipa) + { + var script = IpaGestureExperiment.BuildWordScript( + [(new IpaGestureExperimentTarget("w_0_c", ipa, "", 0.095f), 1f), (new IpaGestureExperimentTarget("w_1_a", "a", "", 0.18f), 1f)], 140); + var x = Render(script, 0.4f); + return x is null ? double.NaN : 20 * Math.Log10(Math.Max(1e-9, Deviation(x, 0.03, 0.058)) / Deviation(x, 0.15, 0.25)); + } + + private static SynthPatch WithoutBurst(SynthPatch p) => + p with { ControlSplines = p.ControlSplines.Where(s => !s.SurfacePath.EndsWith("/release_flow", StringComparison.Ordinal)).ToArray() }; + + // The closure of a stop is the part after the boundary ramp into it and before its release, without the burst (the ramp is the tract still + // shutting on the vowel's decaying voicing; its noise is measured by ClosingIntoAStopMakesNoHighFrequencyNoise). + // A tongue closure at the nasal's 0.03 cm aperture leaked: /t/ read -14.3 dB re the flanking vowels against /p/ -22.1 (the lips seal /p/); + // completely closed it reads -24.3. + [Fact] + public void ATongueSealedStopClosureIsAsQuietAsTheLipSealedOne() + { + double Closure(string ipa, string descriptor) + { + var (x, from, to) = VowelConsonantVowel(ipa, descriptor: descriptor, edit: WithoutBurst); + if (x is null) return double.NaN; + var vowels = Math.Sqrt((Math.Pow(Rms(x, from - 0.08, from - 0.01), 2) + Math.Pow(Rms(x, to + 0.03, to + 0.12), 2)) / 2); + return 20 * Math.Log10(Rms(x, ClosureFrom(from), ReleaseAt(from, to)) / vowels); + } + + var p = Closure("p", "voiceless_bilabial_plosive"); + var t = Closure("t", "voiceless_alveolar_plosive"); + output.WriteLine($"closure re vowels: /p/ {p:F1} dB, /t/ {t:F1} dB"); + if (double.IsNaN(p)) return; + Assert.True(t <= p + 1, $"the /t/ closure ({t:F1} dB) is as quiet as /p/ ({p:F1} dB)"); + } + + [Fact] + public void ATongueSealedStopBurstStaysUnderHalfTheFollowingVowel() + { + var (x, from, to) = VowelConsonantVowel("t", descriptor: "voiceless_alveolar_plosive"); + var (nb, _, _) = VowelConsonantVowel("t", descriptor: "voiceless_alveolar_plosive", edit: WithoutBurst); + if (x is null || nb is null) return; + var burst = x.Zip(nb, (a, b) => a - b).ToArray(); + var peak = Peak(burst, ReleaseAt(from, to) - 0.003, to + 0.01); + var vowel = Peak(x, to + 0.06, to + 0.17); + output.WriteLine($"/t/ burst peak {peak:F4}, vowel peak {vowel:F4}, ratio {peak / vowel:F2}"); + Assert.True(peak > 0.004, "audible"); + Assert.True(peak < vowel / 2, "under half the following vowel's peak, as /b/"); + } + + // The /b/ test over the alveolar stop, by its table entry (before the entry the phone was unsupported, and a hand script approximated it). + [Fact] + public void AlveolarStopBurstExistsOnlyAroundItsOwnRelease_t() + { + IpaGestureExperimentTarget Phone(string label, string ipa, float seconds) => new($"w_{label}", ipa, "", seconds); + var script = IpaGestureExperiment.BuildWordScript( + [(Phone("0_n", "n", 0.095f), 1f), (Phone("1_a", "a", 0.18f), 1f), (Phone("2_t", "t", 0.095f), 1f), (Phone("3_a", "a", 0.18f), 1f), (Phone("4_n", "n", 0.095f), 1f)], 140); + var withBurst = Render(script, 0.77f); + var withoutBurst = Render(script, 0.77f, WithoutBurst); + if (withBurst is null || withoutBurst is null) return; + var burst = withBurst.Zip(withoutBurst, (x, y) => x - y).ToArray(); + const double tFrom = 0.275, tTo = 0.37; + var atRelease = Peak(burst, ReleaseAt(tFrom, tTo) - 0.003, tTo + 0.02); + var leading = Peak(burst, 0, tFrom); + var trailing = Peak(burst, tTo + 0.12, 0.77); + output.WriteLine($"/t/ burst peak {atRelease:F4} at the release, {leading:F5} before the plosive, {trailing:F5} after it"); + Assert.True(atRelease > 0.004, "the burst is a transient of audible size"); + Assert.True(leading < atRelease / 20, "no burst before the plosive's own gesture"); + Assert.True(trailing < atRelease / 10, "no burst after the plosive"); + } + + // Chodroff and Wilson 2014: /t/ burst peak 5216 Hz, /k/ 2868. The band 3.5 to 7 kHz is a DESIGN tolerance around the /t/ figure. + // Peak of the isolated burst's spectrum from 1 kHz up (the weak_spots.burst_spectrum definition: 20 ms Hann window from the release, 400 Hz smoothing). + [Fact] + public void StopTBurstSpectrumIsInTheLiteratureBand() + { + var (x, from, to) = VowelConsonantVowel("t"); + var (nb, _, _) = VowelConsonantVowel("t", edit: WithoutBurst); + if (x is null || nb is null) return; + var start = (int)(ReleaseAt(from, to) * Rate); + const int n = 960; + var seg = Enumerable.Range(0, n).Select(i => (x[start + i] - nb[start + i]) * (0.5 - 0.5 * Math.Cos(2 * Math.PI * i / (n - 1)))).ToArray(); + double Power(double hz) + { + double re = 0, im = 0; + for (var i = 0; i < n; i++) { var a = 2 * Math.PI * hz * i / Rate; re += seg[i] * Math.Cos(a); im -= seg[i] * Math.Sin(a); } + return re * re + im * im; + } + + var grid = Enumerable.Range(0, 180).Select(k => 1000.0 + 50 * k).ToArray(); + var power = grid.Select(Power).ToArray(); + var smooth = power.Select((_, k) => Enumerable.Range(Math.Max(0, k - 4), Math.Min(9, power.Length - Math.Max(0, k - 4))).Average(j => power[j])).ToArray(); + var peak = grid[Array.IndexOf(smooth, smooth.Max())]; + output.WriteLine($"/t/ burst spectral peak {peak:F0} Hz"); + Assert.InRange(peak, 3500, 7000); + } + + // /k/ over the /t/ tests: its own place (the velar distance in the place table), closure sealed by the tongue, burst under half the vowel. + // The spec asks /k/'s closure to be at or below /p/'s. With the voiceless source as /p/ and /t/ have it, it is not, and was not on main + // either (-14.9 dB re the vowels, /p/ -20.4): the 150 Hz voicing reaches the output through a closure that leaves only a short rear cavity. + // Silencing voicing in voiceless stops would change /p/ and /t/ (pinned byte for byte), so the criterion stays unmet (fork). + // The measure is the source-on closure in its settled part (25 ms after the 30 ms closing ramp has finished, to 5 ms before the release), + // because the source-off closure cannot see a leak: with the source off nothing drives the tract, and leaks of .02, .05 and .1 of the + // diameter scale read -21.1, -21.2 and -21.2 dB over .21 to .263 s against -21.0 sealed, while the source-on closure reads 1.8, 1.8 and + // 4.1 dB louder in the settled part. The closure + // right after the ramp is no use either: the seal completes at the end of the ramp with the vowel's tail still in the rear cavity (the + // source-off /k/ read -21.0 dB over .21 to .263 s with the ramp and -27.5 with the contact and the constriction stepped shut; main's + // 30 ms-later window read -25.7), and that decays within the first 25 ms. + // The test therefore proves its own measure first (a leak injected into the tongue's closed diameter must read 3 dB louder) and then bounds + // the sealed closure (-16.5 measured; a .005, .03 or .035 leak reads -15.1, a .02 leak -14.7). + [Fact] + public void AVelarStopClosureSealsAndTheMeasureSeesALeak() + { + static SynthPatch Leaking(SynthPatch p) => + WithoutBurst(p) with + { + ControlSplines = WithoutBurst(p).ControlSplines + .Select(s => s.Name == "w_1_c_constriction_diameter" + ? s with { Points = s.Points.Select(q => q.Value == s.Points.Min(v => v.Value) ? q with { Value = 0.1f, OutValue = 0, InValue = 0 } : q).ToArray() } + : s).ToArray() + }; + + double Closure(Func edit) + { + var (x, from, to) = VowelConsonantVowel("k", descriptor: "voiceless_velar_plosive", edit: edit); + if (x is null) return double.NaN; + var vowels = Math.Sqrt((Math.Pow(Rms(x, from - 0.08, from - 0.01), 2) + Math.Pow(Rms(x, to + 0.03, to + 0.12), 2)) / 2); + return 20 * Math.Log10(Rms(x, ClosureFrom(from) + 0.025, ReleaseAt(from, to) - 0.005) / vowels); + } + + var sealedClosure = Closure(WithoutBurst); + var leaking = Closure(Leaking); + output.WriteLine($"/k/ closure re vowels, source on, settled part: sealed {sealedClosure:F1} dB, with a 0.1 leak {leaking:F1} dB"); + if (double.IsNaN(sealedClosure)) return; + Assert.True(leaking >= sealedClosure + 3, $"the measure sees a leak: sealed {sealedClosure:F1} dB, leaking {leaking:F1} dB"); + Assert.True(sealedClosure <= -15.5, $"the /k/ closure seals: {sealedClosure:F1} dB re the vowels (measured -16.5; leaks of .005 to .1 read -15.1 to -12.4, the measure is not monotonic in the leak, so the bound sits under the weakest of them)"); + } + + [Fact] + public void AVelarStopBurstStaysUnderHalfTheFollowingVowel() + { + var (x, from, to) = VowelConsonantVowel("k", descriptor: "voiceless_velar_plosive"); + var (nb, _, _) = VowelConsonantVowel("k", descriptor: "voiceless_velar_plosive", edit: WithoutBurst); + if (x is null || nb is null) return; + var burst = x.Zip(nb, (a, b) => a - b).ToArray(); + var peak = Peak(burst, ReleaseAt(from, to) - 0.003, to + 0.01); + var vowel = Peak(x, to + 0.06, to + 0.17); + output.WriteLine($"/k/ burst peak {peak:F4}, vowel peak {vowel:F4}, ratio {peak / vowel:F2}"); + Assert.True(peak > 0.004, "audible"); + Assert.True(peak < vowel / 2, "under half the following vowel's peak, as /t/ and /b/"); + } + + [Fact] + public void VelarStopBurstExistsOnlyAroundItsOwnRelease_k() + { + IpaGestureExperimentTarget Phone(string label, string ipa, float seconds) => new($"w_{label}", ipa, "", seconds); + var script = IpaGestureExperiment.BuildWordScript( + [(Phone("0_n", "n", 0.095f), 1f), (Phone("1_a", "a", 0.18f), 1f), (Phone("2_k", "k", 0.095f), 1f), (Phone("3_a", "a", 0.18f), 1f), (Phone("4_n", "n", 0.095f), 1f)], 140); + var withBurst = Render(script, 0.77f); + var withoutBurst = Render(script, 0.77f, WithoutBurst); + if (withBurst is null || withoutBurst is null) return; + var burst = withBurst.Zip(withoutBurst, (x, y) => x - y).ToArray(); + const double kFrom = 0.275, kTo = 0.37; + var atRelease = Peak(burst, ReleaseAt(kFrom, kTo) - 0.003, kTo + 0.02); + var leading = Peak(burst, 0, kFrom); + var trailing = Peak(burst, kTo + 0.12, 0.77); + output.WriteLine($"/k/ burst peak {atRelease:F4} at the release, {leading:F5} before the plosive, {trailing:F5} after it"); + Assert.True(atRelease > 0.004, "the burst is a transient of audible size"); + Assert.True(leading < atRelease / 20, "no burst before the plosive's own gesture"); + Assert.True(trailing < atRelease / 10, "no burst after the plosive"); + } + + // Chodroff and Wilson 2014: /k/ burst peak 2868 Hz. The band 2 to 4 kHz is a DESIGN tolerance around it, fixed before the first measurement. + // The peak is judged against the literature and against /t/ measured the same way (a velar burst enters further back, so its peak is lower). + // The centroid is reported, not asserted: over this 1 to 10 kHz grid /k/ reads 4626 Hz against /t/ 3629 Hz, higher though its peak is lower, + // because the /k/ burst has broadband energy above its peak, and no literature centroid for /k/ was retrieved to judge it by. + private (double Peak, double Centroid) BurstSpectrum(string ipa, string descriptor) + { + var (x, from, to) = VowelConsonantVowel(ipa, descriptor: descriptor); + var (nb, _, _) = VowelConsonantVowel(ipa, descriptor: descriptor, edit: WithoutBurst); + if (x is null || nb is null) return (double.NaN, double.NaN); + var start = (int)(ReleaseAt(from, to) * Rate); + const int n = 960; + var seg = Enumerable.Range(0, n).Select(i => (x[start + i] - nb[start + i]) * (0.5 - 0.5 * Math.Cos(2 * Math.PI * i / (n - 1)))).ToArray(); + double Power(double hz) + { + double re = 0, im = 0; + for (var i = 0; i < n; i++) { var a = 2 * Math.PI * hz * i / Rate; re += seg[i] * Math.Cos(a); im -= seg[i] * Math.Sin(a); } + return re * re + im * im; + } + + var grid = Enumerable.Range(0, 180).Select(k => 1000.0 + 50 * k).ToArray(); + var power = grid.Select(Power).ToArray(); + var smooth = power.Select((_, k) => Enumerable.Range(Math.Max(0, k - 4), Math.Min(9, power.Length - Math.Max(0, k - 4))).Average(j => power[j])).ToArray(); + return (grid[Array.IndexOf(smooth, smooth.Max())], grid.Zip(power, (f, w) => f * w).Sum() / power.Sum()); + } + + [Fact] + public void StopKBurstPeakIsInTheLiteratureBandAndLowerThanT() + { + var (peak, centroid) = BurstSpectrum("k", "voiceless_velar_plosive"); + if (double.IsNaN(peak)) return; + var (tPeak, tCentroid) = BurstSpectrum("t", "voiceless_alveolar_plosive"); + output.WriteLine($"/k/ burst spectral peak {peak:F0} Hz, centroid {centroid:F0} Hz; /t/ peak {tPeak:F0} Hz, centroid {tCentroid:F0} Hz"); + Assert.InRange(peak, 2000, 4000); + Assert.True(peak < tPeak, $"/k/ peak {peak:F0} Hz is under /t/ {tPeak:F0} Hz"); + } + + // /d/ is /t/ with voicing, as /b/ is /p/ with voicing: the same gesture, so the splines that shape the tract are identical and the two differ + // in the voice bar through the closure and in nothing else the burst carries. Chodroff and Wilson 2014: /d/ burst peak 4878 Hz, /t/ 5216. + [Fact] + public void StopDDiffersFromStopTOnlyByVoicing() + { + static SynthPatch Word(string ipa) => PatchScript.Parse(IpaGestureExperiment.BuildWordScript( + [(new IpaGestureExperimentTarget("w_0_a", "a", "", 0.18f), 1f), (new IpaGestureExperimentTarget("w_1_c", ipa, "", 0.095f), 1f), (new IpaGestureExperimentTarget("w_2_a", "a", "", 0.18f), 1f)], 140)); + static string[] Shape(SynthPatch p) => p.ControlSplines + .Where(s => s.Name.StartsWith("w_1_c", StringComparison.Ordinal) && !s.Name.StartsWith("w_1_c_source", StringComparison.Ordinal)) + .Select(s => s.SurfacePath + "|" + s.Name[5..] + "|" + string.Join(";", s.Points.Select(q => $"{q.TimeSeconds:R}:{q.Value:R}"))) + .Order().ToArray(); + var t = Shape(Word("t")); + var d = Shape(Word("d")); + output.WriteLine($"/t/ {t.Length} consonant splines, /d/ {d.Length}"); + Assert.NotEmpty(d); + Assert.Equal(t, d); + + var tDb = VoiceBarDb("t"); + var dDb = VoiceBarDb("d"); + output.WriteLine($"closure re the following vowel: /t/ {tDb:F1} dB, /d/ {dDb:F1} dB"); + if (double.IsNaN(tDb)) return; + Assert.True(dDb >= tDb + 8, $"the voice bar lifts the /d/ closure ({dDb:F1} dB) at least 8 dB over /t/ ({tDb:F1} dB)"); + var (dPeak, _) = BurstSpectrum("d", ""); + var (tPeak, _) = BurstSpectrum("t", ""); + output.WriteLine($"burst spectral peak: /t/ {tPeak:F0} Hz, /d/ {dPeak:F0} Hz"); + Assert.True(Math.Abs(dPeak - tPeak) <= 1000, $"/d/ burst peak {dPeak:F0} Hz within 1 kHz of /t/ {tPeak:F0} Hz"); + } + + [Fact] + public void StopDBurstExistsOnlyAroundItsOwnRelease() + { + IpaGestureExperimentTarget Phone(string label, string ipa, float seconds) => new($"w_{label}", ipa, "", seconds); + var script = IpaGestureExperiment.BuildWordScript( + [(Phone("0_n", "n", 0.095f), 1f), (Phone("1_a", "a", 0.18f), 1f), (Phone("2_d", "d", 0.095f), 1f), (Phone("3_a", "a", 0.18f), 1f), (Phone("4_n", "n", 0.095f), 1f)], 140); + var withBurst = Render(script, 0.77f); + var withoutBurst = Render(script, 0.77f, WithoutBurst); + if (withBurst is null || withoutBurst is null) return; + var burst = withBurst.Zip(withoutBurst, (x, y) => x - y).ToArray(); + const double dFrom = 0.275, dTo = 0.37; + var atRelease = Peak(burst, ReleaseAt(dFrom, dTo) - 0.003, dTo + 0.02); + var leading = Peak(burst, 0, dFrom); + var trailing = Peak(burst, dTo + 0.12, 0.77); + output.WriteLine($"/d/ burst peak {atRelease:F4} at the release, {leading:F5} before the plosive, {trailing:F5} after it"); + Assert.True(atRelease > 0.004, "the burst is a transient of audible size"); + Assert.True(leading < atRelease / 20, "no burst before the plosive's own gesture"); + Assert.True(trailing < atRelease / 10, "no burst after the plosive"); + } + + // /l/ over /ɑ _ ɑ/ at the scaffold's durations. Envelope of the consonant's middle 60 percent at the harmonics of f0 (the largest power within a + // quarter of f0 of k * f0), in dB: the spectrum of a periodic source is a line spectrum, so extrema are read from the harmonic envelope. + private (double[] Hz, double[] Db) HarmonicEnvelope(float[] x, double from, double to, double f0 = 140, double maxHz = 5000) + { + var a = (int)((from + 0.2 * (to - from)) * Rate); + var n = (int)(0.6 * (to - from) * Rate); + var seg = Enumerable.Range(0, n).Select(i => x[a + i] * (0.5 - 0.5 * Math.Cos(2 * Math.PI * i / (n - 1)))).ToArray(); + double Power(double hz) + { + double re = 0, im = 0; + for (var i = 0; i < n; i++) { var w = 2 * Math.PI * hz * i / Rate; re += seg[i] * Math.Cos(w); im -= seg[i] * Math.Sin(w); } + return re * re + im * im; + } + + var hz = new List(); + var db = new List(); + for (var k = 1; k * f0 < maxHz; k++) + { + hz.Add(k * f0); + db.Add(10 * Math.Log10(Math.Max(1e-30, Enumerable.Range(-3, 7).Max(j => Power(k * f0 + j * f0 / 12))))); + } + + return (hz.ToArray(), db.ToArray()); + } + + // The lateral's own dip: the envelope with the side stub open less the same render with it shut, at its lowest between 2 and 3 kHz. The + // difference isolates the stub's antiresonance from the valleys between the vowel's formants, which the shut render has as deep. + private static (double DipHz, double Depth) LateralDip(double[] hz, double[] db, double[] shutDb) + { + var i = Enumerable.Range(0, hz.Length).Where(j => hz[j] >= 2000 && hz[j] <= 3000).MinBy(j => db[j] - shutDb[j]); + return (hz[i], shutDb[i] - db[i]); + } + + // The lateral's antiresonance: the closed side stub beside the oral path (sublingual cavity). With the stub open the 2 to 3 kHz harmonics fall at + // least 6 dB under the same render with it shut (a DESIGN criterion; no /l/ spectrum was retrieved). The lateral is continuous: the render with + // and without release_flow is the same render. + [Fact] + public void LateralHasAnAntiresonanceAndNoBurst() + { + var (x, from, to) = VowelConsonantVowel("l"); + if (x is null) return; + var (hz, db) = HarmonicEnvelope(x, from, to); + var (shut, _, _) = VowelConsonantVowel("l", edit: WithShutSideStub); + var (_, shutDb) = HarmonicEnvelope(shut!, from, to); + var (dipHz, depth) = LateralDip(hz, db, shutDb); + var (nb, _, _) = VowelConsonantVowel("l", edit: WithoutBurst); + output.WriteLine($"/l/ stub open {depth:F1} dB under stub shut at {dipHz:F0} Hz (2-3 kHz); max |with - without release_flow| {x.Zip(nb!, (a, b) => Math.Abs(a - b)).Max():G3}"); + Assert.True(depth >= 6, $"the 2-3 kHz dip is {depth:F1} dB under the shut-stub render"); + Assert.Equal(x, nb); + } + + private static SynthPatch WithShutSideStub(SynthPatch p) => + p with { ControlSplines = p.ControlSplines.Where(s => s.SurfacePath != "/vocal/branches/1/opening").ToArray() }; + + // Between the vowels and the nasals in level: -4 to -10 dB re the flanking vowels (a DESIGN band; /n/ reads -9.6, /m/ -11.8 in the same measure). + [Fact] + public void LateralLevelIsBetweenTheVowelsAndTheNasals() + { + var (x, from, to) = VowelConsonantVowel("l"); + if (x is null) return; + var db = ConsonantDbReVowels(x, from, to); + output.WriteLine($"/l/ {db:F1} dB re the flanking vowels"); + Assert.InRange(db, -10, -4); + } +} + +// Reports Skipped, not Passed, when the Faust toolchain is absent: a test that returns early reads as a pass nobody earned. +public sealed class FaustFactAttribute : FactAttribute +{ + public FaustFactAttribute() + { + if (AquaSynthFaustToolchain.Find() is null) Skip = "Faust toolchain not found"; + } +} + +public sealed class FaustTheoryAttribute : TheoryAttribute +{ + public FaustTheoryAttribute() + { + if (AquaSynthFaustToolchain.Find() is null) Skip = "Faust toolchain not found"; + } +} + +public sealed class FaustSkipTests +{ + [Fact] + public void StressTestsSkipWithoutFaust() + { + var toolchain = AquaSynthFaustToolchain.Find(); + Assert.Equal(toolchain is null, new FaustFactAttribute().Skip is not null); + Assert.Equal(toolchain is null, new FaustTheoryAttribute().Skip is not null); + } +} + +file sealed class TupleTolerance(double eps) : IEqualityComparer<(double, double)> +{ + public bool Equals((double, double) x, (double, double) y) => Math.Abs(x.Item1 - y.Item1) < eps && Math.Abs(x.Item2 - y.Item2) < eps; + public int GetHashCode((double, double) value) => 0; +} diff --git a/tests/AquaSynth.Dsl.Tests/TriggerRepeatCompositionTests.cs b/tests/AquaSynth.Dsl.Tests/TriggerRepeatCompositionTests.cs new file mode 100644 index 0000000..27d1384 --- /dev/null +++ b/tests/AquaSynth.Dsl.Tests/TriggerRepeatCompositionTests.cs @@ -0,0 +1,106 @@ +using AquaSynth.Dsl; +using AquaSynth.Faust; + +namespace AquaSynth.Dsl.Tests; + +// A consumer writes `trigger` on a voice (envelope restarts on a grid) and `patch repeat` (sfxr pitch/arpeggio restart). These render +// the composition and assert what a listener-side measurement sees: the trigger restarts the envelope, the repeat restarts the pitch +// slide inside each trigger window and never cuts the envelope. Sound is not judged here; only the structure of the render. +[Collection("NativeFaust")] +public sealed class TriggerRepeatCompositionTests +{ + private const int Rate = 48000; + + private const string Voice = """ + voice + wave=sine + freq=200 + trigger=0.5 + attack=0.001 + sustain=0.12 + decay=0.1 + pitch_ramp=2.0 + """; + + private static float[]? Render(string script, float seconds) + { + var source = FaustEmitter.Emit(PatchScript.Parse(script), new FaustExportOptions("trigger_repeat", SampleRate: Rate)).Source; + using var compiler = new AquaSynthPatchCompiler(new AquaSynthNativeOptions(SampleRate: Rate)); + if (!compiler.TryCompileSource(new AquaSynthCompileIdentity("trigger_repeat", "trigger_repeat", ""), source, seconds, out var patch, out var error)) + { + if (error?.Contains("Faust toolchain not found", StringComparison.OrdinalIgnoreCase) == true || + error?.Contains("Faust DLL not found", StringComparison.OrdinalIgnoreCase) == true) + { + return null; + } + + Assert.Fail($"compile failed: {error}"); + } + + using (patch) return patch!.Render(seconds); + } + + private static double Rms(float[] x, double from, double to) + { + var a = (int)(from * Rate); + var b = Math.Min(x.Length, (int)(to * Rate)); + double sum = 0; + for (var i = a; i < b; i++) sum += (double)x[i] * x[i]; + return Math.Sqrt(sum / Math.Max(1, b - a)); + } + + // Zero-crossing rate over a window, in Hz: a pitch measure that needs no search range. + private static double CrossingHz(float[] x, double from, double to) + { + var a = (int)(from * Rate); + var b = Math.Min(x.Length, (int)(to * Rate)); + var crossings = 0; + for (var i = a + 1; i < b; i++) if ((x[i - 1] < 0) != (x[i] < 0)) crossings++; + return crossings / 2.0 / ((b - a) / (double)Rate); + } + + [Fact] + public void TriggerRestartsTheEnvelopeWhileRepeatInsideTheWindowDoesNot() + { + // repeat 0.05 is shorter than sustain + decay (0.22): the envelope must still run to its release inside each trigger window. + var x = Render("patch repeat=0.05\n" + Voice, 1.1f); + if (x is null) return; + + Assert.True(Rms(x, 0.00, 0.10) > 0.1, "first hit sounds"); + Assert.True(Rms(x, 0.35, 0.48) < 0.01, "envelope released before the next trigger; repeat did not hold it open"); + Assert.True(Rms(x, 0.50, 0.60) > 0.1, "trigger restarts the envelope"); + Assert.True(Rms(x, 0.85, 0.98) < 0.01, "second window releases too"); + Assert.True(Rms(x, 1.00, 1.10) > 0.1, "third trigger"); + } + + [Fact] + public void RepeatRestartsThePitchSlideInsideEachTriggerWindowAndTriggerRestartsItToo() + { + var withRepeat = Render("patch repeat=0.05\n" + Voice, 0.6f); + var without = Render(Voice, 0.6f); + if (withRepeat is null || without is null) return; + + // Without repeat the slide keeps climbing across the window; with repeat the pitch falls back at each 0.05 s boundary. + var early = CrossingHz(without, 0.00, 0.04); + var late = CrossingHz(without, 0.12, 0.16); + Assert.True(late > early * 1.15, $"control: slide climbs without repeat ({early:F0} -> {late:F0} Hz)"); + + var repeatedEarly = CrossingHz(withRepeat, 0.00, 0.04); + var repeatedLate = CrossingHz(withRepeat, 0.10, 0.14); + Assert.InRange(repeatedLate / repeatedEarly, 0.9, 1.1); + + // The trigger at 0.5 restarts age; the repeat grid inside the new window starts from the trigger, not from absolute time. + var afterTrigger = CrossingHz(withRepeat, 0.50, 0.54); + Assert.InRange(afterTrigger / repeatedEarly, 0.9, 1.1); + } + + [Fact] + public void TriggerAndRepeatLowerToDistinctAges() + { + var source = FaustEmitter.EmitScript("patch repeat=0.05\n" + Voice).Source; + + Assert.Contains("voice_0_age = wrap01(age / 0.5) * 0.5;", source); + Assert.Contains("voice_0_pitch_age = voice_0_age - floor(voice_0_age / repeat_interval) * repeat_interval;", source); + Assert.Contains("oneshot_adsr_at(voice_0_age,", source); + } +} diff --git a/tests/AquaSynth.Dsl.Tests/VocalTractFormantTests.cs b/tests/AquaSynth.Dsl.Tests/VocalTractFormantTests.cs new file mode 100644 index 0000000..775c07d --- /dev/null +++ b/tests/AquaSynth.Dsl.Tests/VocalTractFormantTests.cs @@ -0,0 +1,593 @@ +using AquaSynth.Dsl; +using AquaSynth.Faust; +using Xunit.Abstractions; + +namespace AquaSynth.Dsl.Tests; + +// The vocal_network resonator is a Kelly-Lochbaum ladder, so the same glottal source gives different vowels as the area +// function moves. These tests render through the native Faust path with a 10 Hz glottal source (a 10 Hz line spectrum +// samples the tract's response densely) and read formants as local maxima of the harmonic lines, flattened by the saw's 1/f. +// Expected values are the ladder prototype's impulse-response peaks (proto/kl.mjs, the same area model and constants). +[Collection("NativeFaust")] +public sealed class VocalTractFormantTests(ITestOutputHelper output) +{ + private const int Rate = 48000; + private const float Pitch = 10f; + + private static string Script(float tongueIndex, float tongueDiameter, float lipOpening, float velum = 0f) => $$""" + morphology name=oral length_cm=17 diameters=.55,.75,1.05,1.45,1.25,.8 tongue_index={{tongueIndex}} tongue_diameter={{tongueDiameter}} constriction_index=4 constriction_diameter=4 lip_opening={{lipOpening}} + morphology name=nasal length_cm=12 diameters=.04,.28,.52,.72 + waveguide_path name=oral_path morphology=oral loss=.998 + waveguide_path name=nasal_path morphology=nasal loss=.997 + source_port name=folds path=oral_path pressure=.7 tension=.55 opening=.5 noise=0 impedance=.3 flow_scale=.12 tilt=0 + branch_port name=velopharynx from=oral_path from_position=.45 to=nasal_path opening={{velum}} coupling=1 + radiation_load name=mouth path=oral_path aperture=.8 reflection=-1 impedance=.28 + radiation_load name=nostril path=nasal_path aperture=.8 reflection=-1 impedance=.28 + vocal_network name=voice paths=oral_path,nasal_path sources=folds branches=velopharynx radiation=mouth,nostril + vocal network=voice freq={{Pitch}} min_freq=5 gain=.5 sustain=1.5 + """; + + // Three seconds of the 10 Hz source would be wasteful: one second after the attack is ten whole periods, so every + // harmonic of Pitch sits exactly on a DFT bin. + private float[]? Render(string script, Func? edit = null) + { + var parsed = PatchScript.Parse(script); + var source = FaustEmitter.Emit(edit is null ? parsed : edit(parsed), new FaustExportOptions("vocal_formants", SampleRate: Rate)).Source; + using var compiler = new AquaSynthPatchCompiler(new AquaSynthNativeOptions(SampleRate: Rate)); + if (!compiler.TryCompileSource(new AquaSynthCompileIdentity("vocal_formants", "vocal_formants", ""), source, 1.4f, out var patch, out var error)) + { + if (error?.Contains("Faust toolchain not found", StringComparison.OrdinalIgnoreCase) == true || + error?.Contains("Faust DLL not found", StringComparison.OrdinalIgnoreCase) == true) + { + return null; + } + + Assert.Fail($"vocal formant compile failed: {error}"); + } + + using (patch) + { + var samples = patch!.Render(1.0f); + Assert.True(samples.Length >= Rate + 4800, $"render too short: {samples.Length}"); + return samples.Skip(4800).Take(Rate).ToArray(); + } + } + + // Harmonic line magnitudes of the saw, multiplied by frequency to undo its 1/f slope. + private static double[] Lines(float[] window) + { + var n = 1 << 16; + var re = new double[n]; + var im = new double[n]; + for (var i = 0; i < window.Length; i++) re[i] = window[i] * (0.5 - 0.5 * Math.Cos(2 * Math.PI * i / window.Length)); + Fft(re, im); + var lines = new double[(int)(5000 / Pitch) + 1]; + for (var k = 1; k < lines.Length; k++) + { + var bin = (int)Math.Round(k * Pitch * n / Rate); + lines[k] = Math.Sqrt(re[bin] * re[bin] + im[bin] * im[bin]) * k; + } + + return lines; + } + + // Local maxima of the flattened lines that are tallest within +-40 Hz and above 10% of the tallest, in ascending Hz. + private static double[] Peaks(float[] window) + { + var lines = Lines(window); + var span = (int)(40 / Pitch); + var floor = 0.1 * lines.Max(); + var peaks = new List(); + for (var k = (int)(150 / Pitch); k < lines.Length - span; k++) + { + if (lines[k] < floor) continue; + var tallest = true; + for (var j = k - span; j <= k + span; j++) tallest &= lines[j] <= lines[k]; + if (tallest) peaks.Add(k * Pitch); + } + + return peaks.ToArray(); + } + + private static void Fft(double[] re, double[] im) + { + var n = re.Length; + for (int i = 1, j = 0; i < n; i++) + { + var bit = n >> 1; + for (; (j & bit) != 0; bit >>= 1) j ^= bit; + j ^= bit; + if (i < j) { (re[i], re[j]) = (re[j], re[i]); (im[i], im[j]) = (im[j], im[i]); } + } + + for (var len = 2; len <= n; len <<= 1) + { + var angle = -2 * Math.PI / len; + for (var i = 0; i < n; i += len) + { + for (var k = 0; k < len / 2; k++) + { + double wr = Math.Cos(angle * k), wi = Math.Sin(angle * k); + var a = i + k; + var b = i + k + len / 2; + var vr = re[b] * wr - im[b] * wi; + var vi = re[b] * wi + im[b] * wr; + (re[b], im[b]) = (re[a] - vr, im[a] - vi); + (re[a], im[a]) = (re[a] + vr, im[a] + vi); + } + } + } + } + + public static TheoryData Vowels => new() + { + { "i", 3f, .15f, 2.2f, 271, 2313 }, + { "schwa", 2f, 1.2f, .6f, 497, 1503 }, + { "a", 1.5f, .5f, 2.2f, 731, 1257 }, + }; + + private double[]? FirstTwo(string name, float ti, float td, float lo) + { + var window = Render(Script(ti, td, lo)); + if (window is null) return null; + var peaks = Peaks(window); + output.WriteLine($"{name}: peaks " + string.Join(" ", peaks.Select(p => p.ToString("F0")))); + Assert.True(peaks.Length >= 2, $"{name}: fewer than two spectral peaks"); + return peaks; + } + + [Theory] + [MemberData(nameof(Vowels))] + public void VowelAreaFunctionsPlaceF1AndF2WhereThePrototypePlacedThem(string name, float ti, float td, float lo, double f1, double f2) + { + var peaks = FirstTwo(name, ti, td, lo); + if (peaks is null) return; + + // F1 within 20%: the harmonic-line reading of a low F1 shifts with the output highpass (i reads 230-260 Hz for the 271 Hz impulse peak). + Assert.InRange(peaks[0], f1 * .8, f1 * 1.2); + Assert.InRange(peaks[1], f2 * .92, f2 * 1.08); + } + + [Fact] + public void VowelsDifferInTheirFormants() + { + var i = FirstTwo("i", 3f, .15f, 2.2f); + var schwa = FirstTwo("schwa", 2f, 1.2f, .6f); + var a = FirstTwo("a", 1.5f, .5f, 2.2f); + if (i is null || schwa is null || a is null) return; + + // F1 rises i < schwa < a as the jaw opens; F2 falls i > schwa > a as the constriction moves back. + Assert.True(i[0] + 100 < schwa[0] && schwa[0] + 100 < a[0], $"F1 {i[0]} {schwa[0]} {a[0]}"); + Assert.True(i[1] > schwa[1] + 300 && schwa[1] > a[1] + 100, $"F2 {i[1]} {schwa[1]} {a[1]}"); + } + + [Fact] + public void LadderStaysBoundedForASecondWithTheVelumOpen() + { + foreach (var (ti, td, lo, velum) in new[] { (3f, .15f, 2.2f, 0f), (1.5f, .5f, 2.2f, 1f), (2f, 1.2f, .6f, 1f) }) + { + var window = Render(Script(ti, td, lo, velum)); + if (window is null) return; + var quarter = window.Length / 4; + var rms = Enumerable.Range(0, 4).Select(q => Math.Sqrt(window.Skip(q * quarter).Take(quarter).Sum(x => (double)x * x) / quarter)).ToArray(); + output.WriteLine($"velum {velum} ti {ti}: peak {window.Max(Math.Abs):F3} rms by quarter {string.Join(" ", rms.Select(r => r.ToString("E2")))}"); + Assert.All(window, sample => Assert.True(float.IsFinite(sample) && Math.Abs(sample) < 4, $"sample {sample}")); + Assert.True(rms[0] > 1e-4, $"silent: rms {rms[0]}"); + Assert.True(rms[3] < 2 * rms[0] + 1e-6 && rms[3] > rms[0] / 2, $"rms drifted {rms[0]:E2} -> {rms[3]:E2}"); + } + } + + private static double Rms(float[] window) => Math.Sqrt(window.Sum(x => (double)x * x) / window.Length); + + // Rms of the part of the window above a frequency (Parseval over the bins of a zero-padded transform). The tract wall radiates a voice bar + // below about 300 Hz with the mouth shut and the velum shut, so "the nose is the only door" is read where the wall barely reaches. + private static double RmsAbove(float[] window, double hz) => RmsBetween(window, hz, Rate / 2.0); + + private static double RmsBetween(float[] window, double lowHz, double highHz) + { + var n = 1 << 17; + var re = new double[n]; + var im = new double[n]; + for (var i = 0; i < window.Length; i++) re[i] = window[i] * (0.5 - 0.5 * Math.Cos(2 * Math.PI * i / window.Length)); + Fft(re, im); + double power = 0; + for (var k = (int)(lowHz * n / Rate); k < Math.Min(n / 2, (int)(highHz * n / Rate)); k++) power += re[k] * re[k] + im[k] * im[k]; + var windowPower = Enumerable.Range(0, window.Length).Sum(i => Math.Pow(0.5 - 0.5 * Math.Cos(2 * Math.PI * i / window.Length), 2)); + return Math.Sqrt(2 * power / n / windowPower); + } + + // The wall term's band, read from the render of a shut mouth and a shut velum (the wall is then the only path out). Behaviour, not + // brick walls: the 300 Hz low-pass is a second-order roll-off and the 90 Hz high-pass a second-order shelf, so these compare band levels. + private float[]? ShutMouthShutVelum() => Render(MouthShutScript(0f)); + + private static string MouthShutScript(float velum) => Script(1.5f, .5f, 2.2f, velum) + .Replace("constriction_diameter=4", "constriction_diameter=.001", StringComparison.Ordinal) + .Replace("diameters=.04,.28,.52,.72", "diameters=.9,1.3,1.5,1.2", StringComparison.Ordinal); + + // The velum-open junction's nasal coefficient, (2 Yv - sum Y) / sum Y: with the mouth shut the first resonance the nose lets out sits at + // 520 Hz (400 Hz when the coefficient is (Yv - sum Y) / sum Y, and output above 600 Hz falls from 39 to 24 times the shut-velum level). + [Fact] + public void TheNasalReflectionCoefficientPlacesTheFirstResonanceOfAShutMouthWithTheVelumOpen() + { + var open = Render(MouthShutScript(1f)); + if (open is null) return; + + var first = Peaks(open)[0]; + output.WriteLine($"mouth shut, velum open: first peak {first:F0} Hz"); + Assert.InRange(first, 460, 580); + } + + [Fact] + public void TheWallBarRollsOffBetweenItsTwoCornersAndOpensAboveTheSlowDrift() + { + var shut = ShutMouthShutVelum(); + if (shut is null) return; + + var drift = RmsBetween(shut, 5, 40); + var bar = RmsBetween(shut, 90, 200); + var upper = RmsBetween(shut, 230, 300); + var above = RmsAbove(shut, 600); + output.WriteLine($"wall bar band levels: 5-40 Hz {drift:E2}, 90-200 Hz {bar:E2}, 230-300 Hz {upper:E2}, above 600 Hz {above:E2}"); + + // The high-pass keeps the slow pressure drift out: measured 5-40 Hz at 0.4 of the bar, and 3.2 of it with the corner at 20 Hz. + Assert.True(drift < bar, $"sub-90 Hz drift {drift:E2} vs the 90-200 Hz bar {bar:E2}"); + // The low-pass at 300 Hz still passes the 230-300 Hz harmonics: measured 0.14 of the bar, and 0.07 of it with the corner at 150 Hz. + Assert.True(upper > bar / 10, $"230-300 Hz {upper:E2} vs the 90-200 Hz bar {bar:E2}"); + // The bar is not a brick wall but it is a bar: the low end stands well over what leaks above 600 Hz. + Assert.True(bar > 10 * above, $"bar {bar:E2} vs above 600 Hz {above:E2}"); + } + + [Fact] + public void WithTheMouthClosedOnlyAnOpenVelumLetsSoundOutThroughTheNose() + { + // constriction_diameter .001 at tract index 4 shuts the oral ladder; the only way out is the junction into the nasal ladder. A shut + // velum is opening 0: the port area is linear in the opening, and a flow source into a nearly lossless closed tract leaves through even + // a 0.008 cm^2 port (opening .01 measured rms 1.0e-2 against 2.1e-2 open). + var shut = Render(MouthShutScript(0f)); + var open = Render(MouthShutScript(1f)); + if (shut is null || open is null) return; + + output.WriteLine($"mouth shut, above 600 Hz: velum closed rms {RmsAbove(shut, 600):E2}, velum open rms {RmsAbove(open, 600):E2}; open peaks {string.Join(" ", Peaks(open).Select(p => p.ToString("F0")))}"); + Assert.True(RmsAbove(open, 600) > 5 * RmsAbove(shut, 600), $"nasal output rms {RmsAbove(open, 600):E2} vs velum-closed {RmsAbove(shut, 600):E2}"); + } + + // The authored vowel gestures (the Story vowel weights, reached through the IPA word script) rendered end to end through the + // exporter, merged splines and the ladder; the source is a bright 10 Hz saw so the tract's response is sampled densely. + private float[]? GestureWindow(string ipa) + { + var script = IpaGestureExperiment.BuildWordScript([(new IpaGestureExperimentTarget($"v_{ipa}", ipa, "", 1.5f), 1f)], Pitch, 0.1f) + .Replace("noise=.04", "noise=0 tilt=1", StringComparison.Ordinal) + .Replace($"freq={Pitch}", $"freq={Pitch} min_freq=5", StringComparison.Ordinal); + return Render(script); + } + + private double[]? GesturePeaks(string ipa) + { + var window = GestureWindow(ipa); + if (window is null) return null; + var peaks = LpcPeaks(window); + output.WriteLine($"gesture {ipa}: peaks " + string.Join(" ", peaks.Select(p => p.ToString("F0")))); + Assert.True(peaks.Length >= 2, $"{ipa}: fewer than two spectral peaks"); + return peaks; + } + + // Formant candidates as the peaks of an LPC envelope (order 12 on the 12 kHz decimation of a 0.25 s steady stretch, pre-emphasis + // .97), the same recipe the render measurements use: the envelope sees through the harmonic lines of the voiced source. + private static double[] LpcPeaks(float[] window) + { + const int Decimate = 4, Order = 12; + var taps = Enumerable.Range(0, 4 * Decimate + 1).Select(n => 0.5 - 0.5 * Math.Cos(2 * Math.PI * (n + 1) / (4 * Decimate + 2))).ToArray(); + var norm = taps.Sum(); + var x = window.Skip(Rate / 4).Take(Rate / 4).Select(v => (double)v).ToArray(); + var mean = x.Average(); + var z = new List(); + for (var n = 2 * Decimate; n < x.Length - 2 * Decimate; n += Decimate) + { + double acc = 0; + for (var k = 0; k < taps.Length; k++) acc += taps[k] * (x[n + k - 2 * Decimate] - mean); + z.Add(acc / norm); + } + + var y = new double[z.Count]; + for (var n = 0; n < y.Length; n++) y[n] = (z[n] - (n > 0 ? .97 * z[n - 1] : 0)) * (0.54 - 0.46 * Math.Cos(2 * Math.PI * n / (y.Length - 1))); + var r = Enumerable.Range(0, Order + 1).Select(lag => Enumerable.Range(0, y.Length - lag).Sum(n => y[n] * y[n + lag])).ToArray(); + r[0] *= 1 + 1e-9; + var a = new double[Order + 1]; + a[0] = 1; + var err = r[0]; + for (var m = 1; m <= Order; m++) + { + var acc = r[m]; + for (var k = 1; k < m; k++) acc += a[k] * r[m - k]; + var kappa = -acc / err; + var next = (double[])a.Clone(); + for (var k = 1; k < m; k++) next[k] = a[k] + kappa * a[m - k]; + next[m] = kappa; + a = next; + err *= 1 - kappa * kappa; + } + + var fs = Rate / (double)Decimate; + double Envelope(double hz) + { + double re = 0, im = 0; + for (var k = 0; k <= Order; k++) { re += a[k] * Math.Cos(2 * Math.PI * hz / fs * k); im -= a[k] * Math.Sin(2 * Math.PI * hz / fs * k); } + return 1 / Math.Sqrt(re * re + im * im); + } + + var peaks = new List(); + var previous = Envelope(150); + var current = Envelope(160); + for (var hz = 170.0; hz < 4500; hz += 10) + { + var next = Envelope(hz); + if (current > previous && current >= next) peaks.Add(hz - 10); + (previous, current) = (current, next); + } + + return peaks.ToArray(); + } + + // Tube-chain F1/F2 of the Story 2008 area functions at 17.52 cm (tools/formant-check/tube_formants.py, an independent frequency-domain + // transfer-matrix estimate: i 251/2063, ɑ 650/913, u 271/761). The envelope of the 10 Hz render is read against those with 10% bands. + public static TheoryData GestureVowels => new() + { + { "i", 251, 2063 }, + { "a", 650, 913 }, + }; + + [Theory] + [MemberData(nameof(GestureVowels))] + public void AuthoredVowelGesturesPlaceF1AndF2NearTheStoryTubeChain(string ipa, double f1, double f2) + { + var peaks = GesturePeaks(ipa); + if (peaks is null) return; + + Assert.InRange(peaks[0], f1 * .9, f1 * 1.1); + Assert.InRange(peaks[1], f2 * .9, f2 * 1.1); + } + + [Fact] + public void AuthoredRoundedBackVowelPlacesF1AndF2NearTheStoryTubeChain() + { + // With a radiating highpass that rose with aperture (540 Hz at aperture 1) the envelope read u's F1 at 450 Hz. + var peaks = GesturePeaks("u"); + if (peaks is null) return; + + Assert.InRange(peaks[0], 271 * .85, 350); + Assert.InRange(peaks[1], 761 * .9, 761 * 1.1); + } + + [Fact] + public void AuthoredVowelGesturesDifferInTheirFormants() + { + var i = GesturePeaks("i"); + var u = GesturePeaks("u"); + var a = GesturePeaks("a"); + if (i is null || u is null || a is null) return; + + Assert.True(a[0] > i[0] + 250, $"F1 i {i[0]} a {a[0]}"); + // F2 (tube chain): i 2063 > a 913 > u 761. + Assert.True(i[1] > a[1] + 800 && a[1] > u[1] + 100, $"F2 i {i[1]} a {a[1]} u {u[1]}"); + } + + [Fact] + public void RoundedBackVowelIsAudibleNextToTheCloseFrontVowel() + { + // The rendered /ˈnibu/ had /u/ 29 dB under /i/: the gesture scaled the radiating aperture (an output gain) by the lip area + // the ladder already carries. Measured with the earlier grid-searched shapes: -4.3 dB; with the old aperture scale -22.7 dB. + var i = GestureWindow("i"); + var u = GestureWindow("u"); + if (i is null || u is null) return; + + var a = GestureWindow("a"); + if (a is not null) output.WriteLine($"a rms {Rms(a):E2}"); + var gap = 20 * Math.Log10(Rms(u) / Rms(i)); + var uPeaks = LpcPeaks(u); + output.WriteLine($"u rms {Rms(u):E2} i rms {Rms(i):E2} gap {gap:F1} dB; u peaks {string.Join(" ", uPeaks.Select(p => p.ToString("F0")))}"); + Assert.InRange(gap, -10, 3); + Assert.True(uPeaks[1] < 1000, $"u F2 {uPeaks[1]}"); + } + + // ---- Story 2008 measured vowel shapes, set through the vowel_* weight surfaces ---- + + private static string StoryScript(IReadOnlyDictionary weights, float lipOpening = 4f) => $$""" + morphology name=oral length_cm=17.52 diameters=.55,.75,1.05,1.45,1.25,.8 tongue_index=3 tongue_diameter=1.4 constriction_index=4 constriction_diameter=4 lip_opening={{lipOpening}} + vowel_set area_function=oral source=story2008 + morphology name=nasal length_cm=12 diameters=.04,.28,.52,.72 + waveguide_path name=oral_path morphology=oral speed=350 loss=.998 + waveguide_path name=nasal_path morphology=nasal speed=350 loss=.997 + source_port name=folds path=oral_path pressure=.7 tension=.55 opening=.5 noise=0 impedance=.3 flow_scale=.12 tilt=0 + branch_port name=velopharynx from=oral_path from_position=.45 to=nasal_path opening=0 coupling=1 + radiation_load name=mouth path=oral_path aperture=1 reflection=-1 impedance=.28 + radiation_load name=nostril path=nasal_path aperture=.8 reflection=-1 impedance=.28 + vocal_network name=voice paths=oral_path,nasal_path sources=folds branches=velopharynx radiation=mouth,nostril + {{string.Join("\n", weights.Select(w => $"control_spline name=w_{w.Key} surface=/vocal/areas/0/area/vowel_{w.Key} interp=linear points=0:{w.Value.ToString(System.Globalization.CultureInfo.InvariantCulture)},4:{w.Value.ToString(System.Globalization.CultureInfo.InvariantCulture)}"))}} + vocal network=voice freq={{Pitch}} min_freq=5 gain=.5 sustain=1.5 + """; + + private double[]? StoryPeaks(string id, float lipOpening = 4f) + { + var window = Render(StoryScript(new Dictionary { [id] = 1f }, lipOpening)); + if (window is null) return null; + var peaks = Peaks(window); + output.WriteLine($"story {id}: peaks " + string.Join(" ", peaks.Select(p => p.ToString("F0"))) + " lpc " + string.Join(" ", LpcPeaks(window).Select(p => p.ToString("F0")))); + Assert.True(peaks.Length >= 2, $"{id}: fewer than two spectral peaks"); + return peaks; + } + + // F1/F2 (F3) of the Story 2008 area functions at 17.52 cm from tools/formant-check/tube_formants.py (an independent + // transfer-matrix estimate in the frequency domain): python3 tools/formant-check/tube_formants.py docs/research/data/story2008-area-functions.csv --length-cm 17.52 + // + // Bands: the front vowels (i ih e eh ae) are held to 6% on F1 and F2. The back and rounded vowels (uh ah aw o oo_short u) are held to 10%: + // the measured disagreement between two lip radiation models (the ladder's one-pole Levine-Schwinger-form reflectance with g = 1, the tube + // chain's baffled piston R||L) plus the ladder's cell quantisation. The nasal junction used to take the reflection coefficients of the + // interface one cell further on (sum over cells 11 and 12 instead of 10 and 11), which put the wrong step in the pharynx and read the + // back vowels' F1 12 to 26% high (uh +14.8%, ah +10.8%, aw +12.4%, o +25.8%, oo_short +13.0% against this table, five tests skipped on those + // numbers); the ladder now agrees with the numpy copy (tools/formant-check/ladder_formants.py --filtered --lip-reflection -1) to 10 Hz. + public static TheoryData StoryTubeChain => new() + { + { "i", 251, 2063, .06 }, { "ih", 346, 1987, .06 }, { "e", 432, 1827, .06 }, { "eh", 527, 1571, .06 }, { "ae", 729, 1523, .06 }, + { "uh", 549, 809, .10 }, { "ah", 650, 913, .10 }, { "aw", 516, 692, .10 }, { "o", 477, 787, .10 }, { "oo_short", 478, 850, .10 }, + { "u", 271, 761, .10 }, + }; + + [Theory] + [MemberData(nameof(StoryTubeChain))] + public void LadderReproducesTheTubeChainFormantsOfEachStoryVowel(string id, double f1, double f2, double band) + { + var peaks = StoryPeaks(id); + if (peaks is null) return; + + Assert.InRange(peaks[0], f1 * (1 - band), f1 * (1 + band)); + Assert.InRange(peaks[1], f2 * (1 - band), f2 * (1 + band)); + } + + [Fact] + public void NearCloseVowelsSitApartFromTheCloseVowelsInF1() + { + var i = StoryPeaks("i"); + var ih = StoryPeaks("ih"); + var u = StoryPeaks("u"); + var oo = StoryPeaks("oo_short"); + if (i is null || ih is null || u is null || oo is null) return; + + Assert.True(ih[0] >= i[0] + 60, $"F1 i {i[0]} ih {ih[0]}"); + Assert.True(oo[0] >= u[0] + 60, $"F1 u {u[0]} oo_short {oo[0]}"); + } + + [Fact] + public void BlendingTwoVowelsHalfAndHalfIsTheCellwiseGeometricMeanOfTheirAreas() + { + var blend = Render(StoryScript(new Dictionary { ["i"] = .5f, ["u"] = .5f })); + if (blend is null) return; + + // Reference: one custom shape on the 24-cell grid (round(17.52e-2 * 48000 / 350)), the geometric mean of i and u at each cell + // centre, selected by its default weight 0 (a lone shape at zero weight is itself). The blend must render the same lines. + const int Cells = 24; + var i = StoryVowelAreas.All.Single(v => v.Id == "i").Areas; + var u = StoryVowelAreas.All.Single(v => v.Id == "u").Areas; + var mean = Enumerable.Range(0, Cells).Select(c => (float)Math.Sqrt(StoryVowelAreas.AreaAt(i, (c + .5f) / Cells) * (double)StoryVowelAreas.AreaAt(u, (c + .5f) / Cells))).ToArray(); + var reference = Render(StoryScript(new Dictionary()), patch => patch with + { + AreaFunctions = patch.AreaFunctions.Select(a => a.Name == "oral" ? a with { Vowels = [new VowelAreaShape("mean", mean)] } : a).ToArray() + }); + if (reference is null) return; + + var blendPeaks = Peaks(blend); + var referencePeaks = Peaks(reference); + output.WriteLine($"blend peaks {string.Join(" ", blendPeaks.Select(p => p.ToString("F0")))}; reference {string.Join(" ", referencePeaks.Select(p => p.ToString("F0")))}"); + Assert.Equal(referencePeaks.Take(3), blendPeaks.Take(3)); + var iPeaks = StoryPeaks("i")!; + Assert.True(blendPeaks[1] < iPeaks[1] - 100, $"blend F2 {blendPeaks[1]} against i {iPeaks[1]}"); + } + + [Fact] + public void WeightsThatSumToZeroGiveTheGeometricMeanShapeNotNaN() + { + var zero = Render(StoryScript(new Dictionary())); + if (zero is null) return; + + Assert.All(zero, sample => Assert.True(float.IsFinite(sample))); + Assert.True(Rms(zero) > 1e-4, $"silent: {Rms(zero)}"); + // Independent route to the same shape: all eleven weights at 1 take the normalised-blend branch, whose result is the geometric mean. + var reference = Render(StoryScript(StoryVowelAreas.All.ToDictionary(v => v.Id, _ => 1f))); + if (reference is null) return; + + Assert.Equal(Peaks(reference).Take(3), Peaks(zero).Take(3)); + } + + [Fact] + public void LipOpeningCapsTheLipCellOfAStoryVowel() + { + var open = StoryPeaks("ah", 4f); + var capped = StoryPeaks("ah", .4f); + if (open is null || capped is null) return; + + output.WriteLine($"ah lip cap 4 cm F1 {open[0]}; 0.4 cm F1 {capped[0]}"); + Assert.True(capped[0] < open[0] - 80, $"lip cap .4 cm F1 {capped[0]} vs open {open[0]}"); + } + + // ---- Nasal coupling: the murmur of /m/ and /n/ against the nostril transfer of tools/formant-check/tube_formants.py --nasal ---- + + // The independent tube chain (Story 2008 geometric-mean oral shape, port after section 22 of 44, A_vp 0.56 cm^2 = the gesture's 0.7 opening, + // the 19-section nasal table, rigid closed oral end after section 44 for /m/ and section 37 for /n/, 17.52 cm): N1 325 / 352 Hz, transfer + // zeros (short circuit of the oral side branch) 882 / 1136 Hz; the quarter-wave c / 4L of the uniform branch would say 999 / 1465 Hz. + private const double TubeM1 = 325, TubeN1 = 352, TubeMZero = 882, TubeNZero = 1136; + + // Smoothed line spectrum in dB (flattened by the saw's 1/f) of a steady 10 Hz render, indexed by line number k (k * Pitch Hz). + private static double[] SmoothedDb(float[] window) + { + var lines = Lines(window); + var db = lines.Select(line => 20 * Math.Log10(Math.Max(1e-12, line))).ToArray(); + return db.Select((_, k) => k < 2 || k > db.Length - 3 ? db[k] : db.Skip(k - 2).Take(5).Average()).ToArray(); + } + + // First dip above the first peak: the first local minimum of the smoothed spectrum, 200-2000 Hz, at least 3 dB under the lower of the + // maxima on either side of it. + private static double FirstDip(double[] db) + { + for (var k = (int)(200 / Pitch); k < (int)(2000 / Pitch); k++) + { + if (!(db[k] < db[k - 1] && db[k] <= db[k + 1])) continue; + var left = db[k]; for (var j = k; j > 0 && db[j] >= left; j--) left = db[j]; + var right = db[k]; for (var j = k; j < db.Length - 1 && db[j] >= right; j++) right = db[j]; + if (Math.Min(left, right) - db[k] >= 3) return k * Pitch; + } + + return double.NaN; + } + + private (double N1, double Dip)? Murmur(string ipa) + { + var window = GestureWindow(ipa); + if (window is null) return null; + var n1 = Peaks(window)[0]; + var dip = FirstDip(SmoothedDb(window)); + output.WriteLine($"/{ipa}/ murmur: N1 {n1:F0} Hz, first dip {dip:F0} Hz"); + return (n1, dip); + } + + [Fact] + public void TheMurmurOfMAndNHasItsFirstPeakBelow400HzNearTheTubeChain() + { + var m = Murmur("m"); + var n = Murmur("n"); + if (m is null || n is null) return; + + Assert.InRange(m.Value.N1, 250, 400); + Assert.InRange(n.Value.N1, 250, 400); + Assert.InRange(m.Value.N1, TubeM1 * .85, TubeM1 * 1.15); + Assert.InRange(n.Value.N1, TubeN1 * .85, TubeN1 * 1.15); + } + + [Fact] + public void TheFirstZeroOfTheNasalMurmurSitsWhereTheOralSideBranchPutsItAndRisesWhenTheBranchIsShorter() + { + var m = Murmur("m"); + var n = Murmur("n"); + if (m is null || n is null) return; + + Assert.InRange(m.Value.Dip, TubeMZero * .85, TubeMZero * 1.15); + Assert.InRange(n.Value.Dip, TubeNZero * .85, TubeNZero * 1.15); + // /n/ closes the mouth 2.6 cm from the lips, /m/ at the lips: a shorter branch beyond the port, a higher zero. + Assert.True(n.Value.Dip >= 1.15 * m.Value.Dip, $"/n/ dip {n.Value.Dip} Hz against /m/ dip {m.Value.Dip} Hz"); + } + + [Fact] + public void AnOralClosureWithTheVelumShutLeavesTheMurmurSilentAndOpeningTheVelumLetsItOut() + { + var open = GestureWindow("m"); + if (open is null) return; + + // The same /m/ with the velum spline held shut: the gesture's own velum control is the only door, so remove it. + var shut = Render(IpaGestureExperiment.BuildWordScript([(new IpaGestureExperimentTarget("v_m", "m", "", 1.5f), 1f)], Pitch, 0.1f) + .Replace("noise=.04", "noise=0 tilt=1", StringComparison.Ordinal) + .Replace($"freq={Pitch}", $"freq={Pitch} min_freq=5", StringComparison.Ordinal), + patch => patch with { ControlSplines = patch.ControlSplines.Where(spline => spline.SurfacePath != "/vocal/branches/0/opening").ToArray() }); + if (shut is null) return; + + output.WriteLine($"/m/ above 600 Hz: velum open rms {RmsAbove(open, 600):E2}, velum shut rms {RmsAbove(shut, 600):E2}"); + Assert.True(RmsAbove(open, 600) > 10 * RmsAbove(shut, 600), $"open {RmsAbove(open, 600):E2} shut {RmsAbove(shut, 600):E2}"); + } +} diff --git a/tests/AquaSynth.Dsl.Tests/WeksaPhonesTests.cs b/tests/AquaSynth.Dsl.Tests/WeksaPhonesTests.cs new file mode 100644 index 0000000..02e7ccb --- /dev/null +++ b/tests/AquaSynth.Dsl.Tests/WeksaPhonesTests.cs @@ -0,0 +1,212 @@ +using System.Text.RegularExpressions; +using AquaSynth.Dsl; + +namespace AquaSynth.Dsl.Tests; + +// Fixtures/Weksa/nibu-en-US.phones.json is Weksa PR 16's fixture (see PROVENANCE.txt); the malformed cases below are +// edits of it, not invented phones. +public sealed class WeksaPhonesTests +{ + private static string Fixture() => File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "Fixtures", "Weksa", "nibu-en-US.phones.json")); + + private static string Line(string phones, string extra = "") => + $$"""{"language":"en-US","source":"ˈnibu",{{extra}}"phones":{{phones}}}"""; + + private static string Ph(string ipa, string stress) => $$"""{"ipa":"{{ipa}}","stress":"{{stress}}"}"""; + + [Fact] + public void AcceptsNibuFixtureAsNIBUWithPrimaryStressOnI() + { + var word = WeksaPhonesReader.Parse(Fixture()); + Assert.Equal("en-US", word.Language); + Assert.Equal("ˈnibu", word.Source); + Assert.Equal(["n", "i", "b", "u"], word.Phones.Select(p => p.Ipa)); + Assert.Equal([WeksaStress.None, WeksaStress.Primary, WeksaStress.None, WeksaStress.None], word.Phones.Select(p => p.Stress)); + var plan = WeksaWordPlan.From(word, "nibu"); + Assert.True(plan.Renderable); + Assert.Equal(["n", "i", "b", "u"], plan.Phones.Select(p => p.Target.Ipa)); + } + + public static IEnumerable Refused() + { + yield return ["not json at all"]; + yield return [Fixture0().Replace("}]}", "}]")]; // truncated + yield return [Fixture0() + " trailing"]; // trailing garbage + yield return ["null"]; + yield return [Line($"[{Ph("n", "tertiary")}]")]; // unknown stress value + yield return [Line($"[{Ph("n", "Primary")}]")]; // stress is exact, not case-folded + yield return [Line("""[{"ipa":"n"}]""")]; // missing stress + yield return [Line("""[{"stress":"none"}]""")]; // missing ipa + yield return ["""{"language":"en-US","phones":[{"ipa":"n","stress":"none"}]}"""]; // missing source + yield return ["""{"source":"n","phones":[{"ipa":"n","stress":"none"}]}"""]; // missing language + yield return ["""{"language":"en-US","source":"n"}"""]; // missing phones + yield return [Line($"[{Ph("n", "none")}]", "\"extra\":1,")]; // extra top-level key + yield return [Line("""[{"ipa":"n","stress":"none","dur":90}]""")]; // extra phone key + yield return [Line("""[{"ipa":5,"stress":"none"}]""")]; // wrong type + yield return [Line("""[{"ipa":"n","stress":1}]""")]; // stress as number + yield return [Line("""[{"ipa":null,"stress":"none"}]""")]; + yield return [Line("""[{"ipa":"n","stress":null}]""")]; + yield return [Line("""[null]""")]; + yield return [Line("""[{"ipa":"","stress":"none"}]""")]; + yield return [Line("{}")]; // phones not a list + yield return [Line("[]")]; // empty phones + } + + private static string Fixture0() => File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "Fixtures", "Weksa", "nibu-en-US.phones.json")).Trim(); + + [Theory] + [MemberData(nameof(Refused))] + public void RefusesMalformedInput(string json) => Assert.Throws(() => WeksaPhonesReader.Parse(json)); + + [Fact] + public void ReportsPhonesTheTractCannotMakeAndRendersNothing() + { + var word = WeksaPhonesReader.Parse(Line($"[{Ph("n", "none")},{Ph("ŋ", "primary")},{Ph("b", "none")},{Ph("θ", "none")}]")); + var plan = WeksaWordPlan.From(word, "x"); + Assert.False(plan.Renderable); + Assert.Equal(["ŋ", "θ"], plan.Unsupported.Select(p => p.Ipa)); + Assert.Empty(plan.Phones); + } + + [Fact] + public void WeksaWordPlanAcceptsT() + { + var word = WeksaPhonesReader.Parse(Line($"[{Ph("ɑ", "none")},{Ph("t", "none")},{Ph("ɑ", "none")}]")); + var plan = WeksaWordPlan.From(word, "ata"); + Assert.True(plan.Renderable); + Assert.Empty(plan.Unsupported); + // t names a plosive, not the approximant GestureDescriptor falls back to for a phone the table lacks + Assert.True(PatchScript.TryIpaDescriptor("t", out var descriptor)); + Assert.Equal("voiceless_alveolar_plosive", descriptor); + var patch = PatchScript.Parse(IpaGestureExperiment.BuildWordScript(plan.Phones.ToArray(), 140)); + Assert.Contains(patch.ControlSplines, s => s.SurfacePath == "/vocal/contacts/0/release_flow" && s.Name.StartsWith("ata_1_t", StringComparison.Ordinal)); + } + + [Fact] + public void WeksaWordPlanAcceptsD() + { + var word = WeksaPhonesReader.Parse(Line($"[{Ph("ɑ", "none")},{Ph("d", "none")},{Ph("ɑ", "none")}]")); + var plan = WeksaWordPlan.From(word, "ada"); + Assert.True(plan.Renderable, string.Join(",", plan.Unsupported.Select(p => p.Ipa))); + Assert.Empty(plan.Unsupported); + // d names a plosive, not the approximant GestureDescriptor falls back to for a phone the table lacks + Assert.True(PatchScript.TryIpaDescriptor("d", out var descriptor)); + Assert.Equal("voiced_alveolar_plosive", descriptor); + var patch = PatchScript.Parse(IpaGestureExperiment.BuildWordScript(plan.Phones.ToArray(), 140)); + Assert.Contains(patch.ControlSplines, s => s.SurfacePath == "/vocal/contacts/0/release_flow" && s.Name.StartsWith("ada_1_d", StringComparison.Ordinal)); + } + + // The lateral gate passed (a closed side stub on the oral path gives a dip: LateralHasAnAntiresonanceAndNoBurst), so /l/ is in the table, as a + // lateral and never as the approximant GestureDescriptor falls back to. + [Fact] + public void LateralGateOutcomeIsRecorded() + { + var word = WeksaPhonesReader.Parse(Line($"[{Ph("ɑ", "none")},{Ph("l", "none")},{Ph("ɑ", "none")}]")); + var plan = WeksaWordPlan.From(word, "ala"); + Assert.True(plan.Renderable, string.Join(",", plan.Unsupported.Select(p => p.Ipa))); + Assert.Empty(plan.Unsupported); + Assert.True(PatchScript.TryIpaDescriptor("l", out var descriptor)); + Assert.Equal("voiced_alveolar_lateral_approximant", descriptor); + Assert.NotEqual("voiced_alveolar_approximant", descriptor); + var script = IpaGestureExperiment.BuildWordScript(plan.Phones.ToArray(), 140); + var patch = PatchScript.Parse(script); + Assert.Contains(patch.BranchPorts, b => b.Name == PatchScript.SideStubBranch); + Assert.Contains(patch.ControlSplines, s => s.SurfacePath == "/vocal/branches/1/opening" && s.Name.StartsWith("ala_1_l", StringComparison.Ordinal)); + // continuous, no burst: the lateral has no release_flow spline at all + Assert.DoesNotContain(patch.ControlSplines, s => s.SurfacePath == "/vocal/contacts/0/release_flow" && s.Name.StartsWith("ala_1_l", StringComparison.Ordinal)); + } + + // A lateral gesture on a scaffold without the side stub is refused, not rendered as an approximant. + [Fact] + public void ALateralWithoutTheSideStubIsRefused() + { + var script = IpaGestureExperiment.BuildWordScript([(new IpaGestureExperimentTarget("w_0_a", "a", "", 0.18f), 1f)], 140) + + "\nphoneme_gesture name=w_hand ipa=l descriptor=voiced_alveolar_lateral_approximant start=.2 dur=.1 intensity=1"; + var error = Assert.Throws(() => PatchScript.Parse(script)); + Assert.Contains(PatchScript.SideStubBranch, error.Message); + } + + [Fact] + public void WeksaWordPlanAcceptsK() + { + var word = WeksaPhonesReader.Parse(Line($"[{Ph("ɑ", "none")},{Ph("k", "none")},{Ph("ɑ", "none")}]")); + var plan = WeksaWordPlan.From(word, "aka"); + Assert.True(plan.Renderable, string.Join(",", plan.Unsupported.Select(p => p.Ipa))); + Assert.Empty(plan.Unsupported); + Assert.True(PatchScript.TryIpaDescriptor("k", out var descriptor)); + Assert.Equal("voiceless_velar_plosive", descriptor); + var patch = PatchScript.Parse(IpaGestureExperiment.BuildWordScript(plan.Phones.ToArray(), 140)); + Assert.Contains(patch.ControlSplines, s => s.SurfacePath == "/vocal/contacts/0/release_flow" && s.Name.StartsWith("aka_1_k", StringComparison.Ordinal)); + } + + [Fact] + public void NearCloseVowelsRenderAsVowelsWithTheirOwnStoryShape() + { + var word = WeksaPhonesReader.Parse(Line($"[{Ph("b", "none")},{Ph("ɪ", "primary")},{Ph("n", "none")},{Ph("b", "none")},{Ph("ʊ", "none")}]")); + var plan = WeksaWordPlan.From(word, "x"); + Assert.True(plan.Renderable); + Assert.Equal([WeksaWordPlan.ConsonantSeconds, WeksaWordPlan.VowelSeconds * WeksaWordPlan.CueFor(WeksaStress.Primary).Duration, WeksaWordPlan.ConsonantSeconds, WeksaWordPlan.ConsonantSeconds, WeksaWordPlan.VowelSeconds], + plan.Phones.Select(p => p.Target.DurationSeconds)); + var patch = PatchScript.Parse(IpaGestureExperiment.BuildWordScript(plan.Phones.ToArray(), 140)); + var ih = patch.ControlSplines.Where(s => s.Name.StartsWith("x_1_ɪ_vowel_", StringComparison.Ordinal)).ToDictionary(s => s.SurfacePath, s => s.Points[^1].Value); + Assert.Equal(1f, ih["/vocal/areas/0/area/vowel_ih"]); + Assert.Equal(0f, ih["/vocal/areas/0/area/vowel_i"]); + var oo = patch.ControlSplines.Where(s => s.Name.StartsWith("x_4_ʊ_vowel_", StringComparison.Ordinal)).ToDictionary(s => s.SurfacePath, s => s.Points[^1].Value); + Assert.Equal(1f, oo["/vocal/areas/0/area/vowel_oo_short"]); + Assert.Equal(0f, oo["/vocal/areas/0/area/vowel_u"]); + } + + private static WeksaWordPlan Plan(WeksaStress onI) => WeksaWordPlan.From(new WeksaPhones("en-US", "ˈnibu", + [new("n", WeksaStress.None), new("i", onI), new("b", WeksaStress.None), new("u", WeksaStress.None)]), "w"); + + [Fact] + public void StressIsLongerLouderAndHigherOnTheMarkedPhoneOnly() + { + var none = Plan(WeksaStress.None).Phones; + var primary = Plan(WeksaStress.Primary).Phones; + var secondary = Plan(WeksaStress.Secondary).Phones; + foreach (var k in new[] { 0, 2, 3 }) + { + Assert.Equal(none[k].Target, primary[k].Target); + Assert.Equal(none[k].Intensity, primary[k].Intensity); + } + + Assert.Equal(WeksaWordPlan.VowelSeconds, none[1].Target.DurationSeconds); + Assert.Equal(1f, none[1].Target.PitchRatio); + Assert.Equal(1f, none[1].Intensity); + Assert.Equal(WeksaWordPlan.VowelSeconds * 220f / 160f, primary[1].Target.DurationSeconds, 5); + Assert.Equal(1.06f, primary[1].Target.PitchRatio); + Assert.Equal(1.30f, primary[1].Intensity); + // Secondary sits strictly between unstressed and primary on every cue. + Assert.InRange(secondary[1].Target.DurationSeconds, none[1].Target.DurationSeconds + 1e-4f, primary[1].Target.DurationSeconds - 1e-4f); + Assert.InRange(secondary[1].Target.PitchRatio, 1.001f, 1.059f); + Assert.InRange(secondary[1].Intensity, 1.001f, 1.299f); + } + + [Fact] + public void StressReachesTheScriptAsDurationF0RatioAndSourceLevelAndShiftsLaterPhones() + { + var patch = (WeksaStress s) => PatchScript.Parse(IpaGestureExperiment.BuildWordScript(Plan(s).Phones.ToArray(), 140)); + var none = patch(WeksaStress.None); + var primary = patch(WeksaStress.Primary); + float First(SynthPatch p, string prefix, string surface) => + p.ControlSplines.Single(sp => sp.Name.StartsWith(prefix, StringComparison.Ordinal) && sp.SurfacePath == surface).Points[^1].Value; + + // pitch_ratio surface range 0.5..2: normalized (1.06 - .5) / 1.5; unstressed holds 1 -> (1 - .5) / 1.5. + Assert.Equal((1.06f - .5f) / 1.5f, First(primary, "w_1_i_source_pitch", "/vocal/sources/0/pitch_ratio"), 4); + Assert.Equal((1f - .5f) / 1.5f, First(none, "w_1_i_source_pitch", "/vocal/sources/0/pitch_ratio"), 4); + Assert.Equal(First(none, "w_1_i_source_level", "/vocal/sources/0/flow_scale") * 1.30f, First(primary, "w_1_i_source_level", "/vocal/sources/0/flow_scale"), 4); + // The b after the stressed vowel starts later by the vowel's extra length. + float Start(SynthPatch p) => p.PhonemeGestures.Single(g => g.Ipa == "b").StartSeconds; + Assert.Equal(WeksaWordPlan.VowelSeconds * (220f / 160f - 1f), Start(primary) - Start(none), 4); + } + + [Fact] + public void PhonesTheWordPlanAcceptsAreExactlyTheDslTable() + { + foreach (var ipa in new[] { "n", "i", "b", "u", "a", "ə", "ɪ", "ʊ", "e", "ɛ", "æ", "ʌ", "ɑ", "ɔ", "o" }) + Assert.True(PatchScript.TryIpaDescriptor(ipa, out var descriptor) && descriptor.EndsWith("_vowel", StringComparison.Ordinal) == "iuaəɪʊeɛæʌɑɔo".Contains(ipa)); + foreach (var ipa in new[] { "ŋ", "θ", "ɹ" }) + Assert.False(PatchScript.TryIpaDescriptor(ipa, out _)); + } +} diff --git a/tools/AquaLower/AquaLower.csproj b/tools/AquaLower/AquaLower.csproj new file mode 100644 index 0000000..fcd7f18 --- /dev/null +++ b/tools/AquaLower/AquaLower.csproj @@ -0,0 +1,16 @@ + + + + Exe + net10.0 + enable + enable + false + + + + + + + + diff --git a/tools/AquaLower/Program.cs b/tools/AquaLower/Program.cs new file mode 100644 index 0000000..018a52c --- /dev/null +++ b/tools/AquaLower/Program.cs @@ -0,0 +1,58 @@ +using System.Text.Json; +using AquaSynth.Dings; +using AquaSynth.Dsl; + +// Lowers .aqua patches to Faust source for the static playground (tools/aqua-playground). +// usage: AquaLower = ... +// A .aqua entry goes through FaustEmitter.EmitScript, the same lowering the daemon uses; a .dsp entry +// (the thruster) is already Faust and is copied verbatim. Writes /.dsp, and +// catalog.json for the browser (the one non-CultNet xeno at this boundary). +// usage: AquaLower --dings : lowers each DingCatalog instrument to /.dsp and writes dings.json +// (instruments with their root frequency, events with their notes) so aqua-feel can mix an event the way DingsPlaybackHost does. +if (args.Length == 2 && args[0] == "--dings") +{ + var dingDir = Path.GetFullPath(args[1]); + Directory.CreateDirectory(dingDir); + var instruments = new List(); + foreach (var ins in DingCatalog.Instruments.Values.OrderBy(i => i.Id, StringComparer.Ordinal)) + { + // 48000 (the FaustExportOptions default) is the Ding catalog contract: DingCatalogTests pins the sources at that rate. + var export = FaustEmitter.EmitScript(ins.Script, new FaustExportOptions(DingCatalog.Identity(ins).FaustName)); + File.WriteAllText(Path.Combine(dingDir, ins.Id + ".dsp"), export.Source); + instruments.Add(new { id = ins.Id, rootFrequency = ins.RootFrequency, dsp = ins.Id + ".dsp", controlPath = "/ding/frequency", warnings = export.Warnings }); + Console.WriteLine($"{ins.Id}: {export.Source.Length} chars, {export.Warnings.Count} warnings"); + } + var events = DingCatalog.Events.Values.OrderBy(e => e.Id, StringComparer.Ordinal) + .Select(e => new { id = e.Id, meaning = e.Meaning, notes = e.Notes.Select(n => new { semitones = n.Semitones, delayMilliseconds = n.DelayMilliseconds, gain = n.Gain }) }); + File.WriteAllText(Path.Combine(dingDir, "dings.json"), JsonSerializer.Serialize(new { instruments, events, noteSeconds = 1.5 }, new JsonSerializerOptions { WriteIndented = true })); + return 0; +} +if (args.Length < 3) { Console.Error.WriteLine("usage: AquaLower = ..."); return 2; } +var outDir = Path.GetFullPath(args[0]); +var root = Path.GetFullPath(args[1]); +Directory.CreateDirectory(outDir); +var entries = new List(); +foreach (var spec in args.Skip(2)) +{ + var eq = spec.IndexOf('='); + var name = spec[..eq]; + var rel = spec[(eq + 1)..]; + var path = Path.Combine(root, rel); + var text = File.ReadAllText(path); + string source; + var warnings = new List(); + if (path.EndsWith(".aqua", StringComparison.Ordinal)) + { + // 48000 (the FaustExportOptions default): AquaLower lowers for the 48 kHz hosts. + var export = FaustEmitter.EmitScript(text, new FaustExportOptions(name)); + source = export.Source; + warnings.AddRange(export.Warnings); + } + else source = text; + var dspFile = StoreFileName.From(name) + ".dsp"; + File.WriteAllText(Path.Combine(outDir, dspFile), source); + entries.Add(new { name, origin = rel.Replace('\\', '/'), kind = path.EndsWith(".aqua", StringComparison.Ordinal) ? "aqua" : "faust", dsp = dspFile, warnings }); + Console.WriteLine($"{name}: {source.Length} chars from {rel}, {warnings.Count} warnings"); +} +File.WriteAllText(Path.Combine(outDir, "catalog.json"), JsonSerializer.Serialize(entries, new JsonSerializerOptions { WriteIndented = true })); +return 0; diff --git a/tools/AquaSynthDingsHost/AquaDaemonClient.cs b/tools/AquaSynthDingsHost/AquaDaemonClient.cs index 2d5a15e..aa4de12 100644 --- a/tools/AquaSynthDingsHost/AquaDaemonClient.cs +++ b/tools/AquaSynthDingsHost/AquaDaemonClient.cs @@ -17,7 +17,8 @@ public sealed class AquaDaemonClient : IAsyncDisposable { EnsureStarted(); var id = $"ding-{Guid.NewGuid():N}"; - var envelope = new { command = "instrument.sample", payload = new { commandId = id, patchId = $"dings.{instrument.Id}", faustName = $"dings_{instrument.Id.Replace('-', '_')}", script = instrument.Script, durationSeconds = 1.5f, gain, controls = new Dictionary { ["/ding/frequency"] = frequency }, revision = 1 } }; + var identity = DingCatalog.Identity(instrument); + var envelope = new { command = "instrument.sample", payload = new { commandId = id, patchId = identity.PatchId, faustName = identity.FaustName, script = instrument.Script, durationSeconds = 1.5f, gain, controls = new Dictionary { ["/ding/frequency"] = frequency }, revision = identity.Revision } }; await process!.StandardInput.WriteLineAsync(JsonSerializer.Serialize(envelope, json)); await process.StandardInput.FlushAsync(cancellationToken); var line = await process.StandardOutput.ReadLineAsync(cancellationToken) ?? throw new InvalidOperationException("Aqua daemon closed before returning a render receipt."); diff --git a/tools/AquaSynthDingsHost/DingsPlaybackHost.cs b/tools/AquaSynthDingsHost/DingsPlaybackHost.cs index d0ffae3..afd6843 100644 --- a/tools/AquaSynthDingsHost/DingsPlaybackHost.cs +++ b/tools/AquaSynthDingsHost/DingsPlaybackHost.cs @@ -63,9 +63,9 @@ public sealed class DingsPlaybackHost : IAsyncDisposable private async Task PlayAsync(DingsCommand command, CancellationToken cancellationToken) { - if (!DingCatalog.Events.TryGetValue(command.EventId, out var dingEvent)) return Response(false, "rejected", $"Unknown event '{command.EventId}'."); - if (!DingCatalog.Instruments.TryGetValue(command.InstrumentId, out var instrument)) return Response(false, "rejected", $"Unknown instrument '{command.InstrumentId}'."); - if (!float.IsFinite(command.Value) || command.Value is < 0 or > 1) return Response(false, "rejected", "Gain must be between 0 and 1."); + if (DingCatalog.PlayRejection(command.EventId, command.InstrumentId, command.Value) is { } rejection) return Response(false, "rejected", rejection); + var dingEvent = DingCatalog.Events[command.EventId]; + var instrument = DingCatalog.Instruments[command.InstrumentId]; var playbackId = $"ding-{Guid.NewGuid():N}"; foreach (var note in dingEvent.Notes) { diff --git a/tools/AquaSynthDingsMcp/DingService.cs b/tools/AquaSynthDingsMcp/DingService.cs index 4ca715e..6d53486 100644 --- a/tools/AquaSynthDingsMcp/DingService.cs +++ b/tools/AquaSynthDingsMcp/DingService.cs @@ -6,6 +6,9 @@ public sealed class DingService(IDingsHostClient host) { public async Task PlayAsync(string eventId, string instrumentId, float gain, CancellationToken cancellationToken) { + // DEPRECATED fail-fast path: the Host enforces the real gate (DingCatalog.PlayRejection, called by DingsPlaybackHost.PlayAsync). + // Kept for one window because its exception types and the NaN pass-through to the Host's rejection are observable; see + // docs/dings-patch-identity-authority-map.md, Migration plan step 4. Remove only in a separate PR. if (!DingCatalog.Events.TryGetValue(eventId, out var dingEvent)) throw new ArgumentException($"Unknown event '{eventId}'."); if (!DingCatalog.Instruments.TryGetValue(instrumentId, out var instrument)) throw new ArgumentException($"Unknown instrument '{instrumentId}'."); if (gain is < 0 or > 1) throw new ArgumentOutOfRangeException(nameof(gain), "Gain must be between 0 and 1."); diff --git a/tools/FaustNativeRender/FaustNativeRender.csproj b/tools/FaustNativeRender/FaustNativeRender.csproj new file mode 100644 index 0000000..9dd3508 --- /dev/null +++ b/tools/FaustNativeRender/FaustNativeRender.csproj @@ -0,0 +1,12 @@ + + + Exe + net10.0 + enable + enable + true + + + + + diff --git a/tools/FaustNativeRender/Program.cs b/tools/FaustNativeRender/Program.cs new file mode 100644 index 0000000..c533296 --- /dev/null +++ b/tools/FaustNativeRender/Program.cs @@ -0,0 +1,55 @@ +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using AquaSynth.Faust; + +// Offline render of one Faust source through the daemon's native libfaust path (AquaSynthPatchCompiler, -vec -lv 1) +// to a mono float32 WAV, for diffing against aqua-feel's faustwasm render. 48 kHz, 512-frame blocks, like render.js. +// usage: FaustNativeRender [control=value ...] +const int Rate = 48000, Frames = 512; +if (args.Length < 3) { Console.Error.WriteLine("usage: FaustNativeRender [control=value ...]"); return 2; } +var dspPath = Path.GetFullPath(args[0]); +var outPath = Path.GetFullPath(args[1]); +var total = (int)Math.Round(double.Parse(args[2]) * Rate); +var source = File.ReadAllText(dspPath); +Directory.CreateDirectory(Path.GetDirectoryName(outPath)!); + +using var compiler = new AquaSynthPatchCompiler(new AquaSynthNativeOptions(SampleRate: Rate)); +var name = Path.GetFileNameWithoutExtension(dspPath); +if (!compiler.TryCompileSource(new AquaSynthCompileIdentity(name, name, ""), source, (float)total / Rate, out var patch0, out var cerr)) +{ + Console.Error.WriteLine("COMPILE FAILED: " + cerr); + return 1; +} +using var patch = patch0!; +if (patch.Manifest.OutputCount != 1) { Console.Error.WriteLine($"expected 1 output, patch has {patch.Manifest.OutputCount}"); return 1; } +var ctl = new Dictionary(); +foreach (var spec in args.Skip(3)) +{ + var eq = spec.IndexOf('='); + var key = spec[..eq]; + var hits = patch.ControlPaths.Where(p => p.TrimStart('/') == key.TrimStart('/') || p.EndsWith("/" + key.TrimStart('/'))).ToList(); + if (hits.Count != 1) { Console.Error.WriteLine($"control \"{key}\" matches {hits.Count} of: {string.Join(", ", patch.ControlPaths)}"); return 1; } + var path = hits[0]; + ctl[path] = float.Parse(spec[(eq + 1)..], System.Globalization.CultureInfo.InvariantCulture); +} + +using var stream = patch.CreateStreamingPatch(); +var buf = GC.AllocateArray(Frames, pinned: true); +var ptrs = GC.AllocateArray(1, pinned: true); +unsafe { ptrs[0] = (IntPtr)Unsafe.AsPointer(ref buf[0]); } +var outP = Marshal.UnsafeAddrOfPinnedArrayElement(ptrs, 0); +var pcm = new float[total]; +for (var at = 0; at < total; at += Frames) +{ + stream.ProcessBlock(Frames, IntPtr.Zero, outP, ctl); + Array.Copy(buf, 0, pcm, at, Math.Min(Frames, total - at)); +} +using (var w = new BinaryWriter(File.Create(outPath))) +{ + var db = total * 4; + w.Write("RIFF"u8); w.Write(36 + db); w.Write("WAVEfmt "u8); w.Write(16); w.Write((short)3); w.Write((short)1); + w.Write(Rate); w.Write(Rate * 4); w.Write((short)4); w.Write((short)32); w.Write("data"u8); w.Write(db); + foreach (var s in pcm) w.Write(s); +} +Console.WriteLine($"{outPath} frames={total} controls={ctl.Count}"); +return 0; diff --git a/tools/FaustRender/FaustRender.csproj b/tools/FaustRender/FaustRender.csproj new file mode 100644 index 0000000..9dd3508 --- /dev/null +++ b/tools/FaustRender/FaustRender.csproj @@ -0,0 +1,12 @@ + + + Exe + net10.0 + enable + enable + true + + + + + diff --git a/tools/FaustRender/Program.cs b/tools/FaustRender/Program.cs new file mode 100644 index 0000000..1f0861b --- /dev/null +++ b/tools/FaustRender/Program.cs @@ -0,0 +1,90 @@ +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Security.Cryptography; +using System.Text.Json; +using AquaSynth.Faust; + +// Offline probe: render one Faust source with a swept `rpm` control to mono 16-bit WAVs through the +// native libfaust path, and write manifest.json (hashes, levels, toolchain). It measures peak, rms and +// NaN only; it cannot say how anything sounds. +// usage: FaustRender