diff --git a/sdk/drafts/grainbox/LICENSE b/sdk/drafts/grainbox/LICENSE new file mode 100644 index 0000000000..250cae34a6 --- /dev/null +++ b/sdk/drafts/grainbox/LICENSE @@ -0,0 +1,30 @@ +MIT License + +Copyright (c) 2026 jrs1717 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +--- + +This licence covers this module's own source code and documentation only. It +does not extend to any Elektron Music Machines firmware, which is not +distributed here and remains the property of its copyright holder. Nothing in +this module is endorsed by, supported by, or affiliated with Elektron. +Octatrack and Elektron are trademarks of Elektron Music Machines MAV AB, used +only to identify the hardware this module targets. diff --git a/sdk/drafts/grainbox/README.md b/sdk/drafts/grainbox/README.md new file mode 100644 index 0000000000..28e7b0930c --- /dev/null +++ b/sdk/drafts/grainbox/README.md @@ -0,0 +1,74 @@ +# Grainbox + +Version 0.1.0-experimental · by @jrs1717 · the test procedure passed on the author's MKII (package 192662ab, steps 1-11); the author kept it, played it and power-cycled: "Everything power cycled fine, sounds great". + +## Overview + +Grainbox is a granular texture effect for an audio track's FX2 slot. It keeps the last third of a second of the track and, on a note grid at the project's tempo, plays it as grains of 10 to 130 ms with three read heads: +- **CLOUD** sprays grains from the recent audio into an immediate wash. +- **ORBIT** takes grains from a window that sweeps slowly back and forth, for hypnotic drones. +- **SPARK** fires grains together as rhythmic onsets, each a pluck that decays. + +Each type has variations A to D. Every grain fades in over its first quarter and out over its last on a rounded curve, is slightly detuned, and takes its own place in the stereo field; each head takes a new grain as it comes round, so textures are click-free. Textures swell in and fade out at TAIL's pace. TONE darkens the grains, BLUR smears them into a reverb-like wash, and FDBK feeds them back so a cloud builds and evolves; at 127 it freezes the moment into an endless pad. MIX sets how far the grains replace the track, and TAIL how long a texture lingers. + +## Controls + +| Page | Knob | Name | Range | Default | What it does | +|---|---|---|---|---|---| +| Main | A | TIME | 1/64, 1/32, 1/16, 1/8, 1/4, 1/2 | 1/16 | The grid at the project's tempo. Textures start only on it and last a slice; SPARK's onsets divide it. | +| Main | B | ACT | 0-127 | 64 | How often a slice plays as grains: 0 never, 127 every slice (a continuous texture). | +| Main | C | SHAP | 0-127 | 32 | The grain's length: 10 ms at 0 to 130 ms at 127. When a head plays slower than normal grains stop growing, so there is still room to spray: about 90 ms with a half-speed head (CLOUD D, ORBIT B and C, SPARK), 54 ms with a quarter-speed one (ORBIT D, SPARK B and D at times). The grain glides to a new SHAP value over about 0.1 s, so a turn or a lock never clicks. | +| Main | D | RPT | 1-8 | 4 | CLOUD: how far back grains are sprayed, in eighths of the capture ring; the reach stops short of the ring's far end by a few grains, so at long grains the top steps sound the same. ORBIT: how wide the sweep is, in grain lengths. SPARK: onsets a slice. | +| Main | E | MIX | 0-127 | 64 | Up to 96 the track plays dry between textures and ducks under each one; from 96 to 127 the dry fades out, so 127 is the grains alone. | +| Main | F | TAIL | 0-127 | 64 | How slowly a texture swells in and how long it lingers after it ends (about 50 ms at 0, 3 s at 127); how long a SPARK pluck rings. | +| Setup | A | TYPE | CLOUD, ORBIT, SPARK | CLOUD | Which kind of texture (above). | +| Setup | B | TONE | 0-127 | 127 | The grains' brightness: a low-pass from about 200 Hz at 0 to fully open at 127, on their stereo spread too. It sits before the feedback, so with FDBK each pass gets darker. | +| Setup | C | VAR | A-D | A | CLOUD: A plain, B an octave shimmer, C one head reversed, D deep (an octave and a fifth down). ORBIT: A a fifth and an octave up, B an octave down and up, C dark (an octave and a fourth down), D random speeds. SPARK: A a major arpeggio, B octaves, C stacked fifths, D random steps. | +| Setup | D | WIDE | 0-127 | 0 | Stereo: each of CLOUD's and ORBIT's grains takes a new place within this width; SPARK's onsets ping-pong. | +| Setup | E | BLUR | 0-127 | 0 | Diffusion: smears the grains through two all-pass stages into a reverb-like wash. It sits before the feedback, so with FDBK each pass gets denser. The diffusion is mono, so the stereo spread narrows to half at 127. | +| Setup | F | FDBK | 0-127 | 0 | Feedback: up to about 90 % of the grains' level goes back into the capture ring, so a cloud builds on itself and evolves. At 127 the ring freezes: nothing new is recorded and the grains play the held moment on every slice as an endless pad, even after the track stops, until you turn it down. | + +## Usage + +Select an audio track, hold FUNC and press FX2, turn LEVEL to GRAINBOX at the end of the list and press YES. Press FX2 for the main page (TIME, ACT, SHAP, RPT, MIX, TAIL); hold FUNC and press FX2 for the setup page (TYPE, TONE, VAR, WIDE, BLUR, FDBK). + +## Quick tutorial + +1. Put Grainbox on FX2 of a track playing a pad or a drum loop. At the defaults it is CLOUD A at 1/16 with 40 ms grains. +2. Turn ACT and MIX to 127 to hear only the grains, then hold FUNC and press FX2 and step TYPE through CLOUD, ORBIT and SPARK. +3. Raise SHAP for longer grains, RPT for a wider spray or sweep (or more SPARK onsets), and TAIL to let textures swell and linger. +4. On the setup page, raise BLUR to smear the grains and FDBK to let them build; turn FDBK to 127 to freeze the moment, and TONE down to darken it. +5. Turn MIX back to 64 to blend the grains with the track again. + +## Compatibility and limitations + +- FX2 only: the capture ring needs an FX2 slot's buffer (15,870 words, stopping short of Sidechain Compressor's key window). On FX1 it passes the track dry. +- Textures follow the project's tempo but not yet the pattern's steps, track speed or swing: free-running tempo slices until the base has a step clock for effects ([issue #2491](https://github.com/repeat98/modwerk/issues/2491)). +- After a second of silent input no texture starts; after four seconds any texture or tail stops. The watch listens to the track's own input, so feedback cannot keep a stopped track going. +- It prefers effect id 10, as Fuzzbox, Glitchbox, Loopbox and Midiverb do: over USB the base gives each its own id; in a card set only one of them fits. +- Declared incompatible with Analog BD, which currently requires the stock DSP effects, as every custom DSP effect is. + +## Tests and measurements + +See [TESTING.md](TESTING.md). + +## Authorship and licences + +Original code by @jrs1717 under the MIT licence in LICENSE. It starts from the author's own Loopbox engine. Inspired by granular effects in general, with no product's code, tables or names. + +## Screens and audio + +![FX2 SETUP page with an effect list ending in Grainbox, selected; knobs labelled ORBIT, TONE, VAR, WIDE, BLUR and FDBK.](media/ot-location.png) + +Hold FUNC and press FX2: Grainbox is chosen at the end of the effect list, with TYPE (here ORBIT), TONE and VAR on the top row and WIDE, BLUR and FDBK below. + +![FX2 main page titled Grainbox with knobs TIME, ACT, SHAP, RPT, MIX and TAIL.](media/ot-controls.png) + +Press FX2 for the main page: TIME, ACT and SHAP on top, RPT, MIX and TAIL below. + +Captured over USB on the author's Octatrack (OS 1.40C, base ddb56b72); see `media/capture.json`. No audio preview yet. + +## Develop + +- Hear it: `npm run module:dev -- grainbox --cycles 408 --cycles-kind executed --state 130 --buffer 15870 --slots fx2`. +- Check it: `npm run module:doctor -- --draft grainbox`; `python3 -B verify.py` holds the engine to `reference.py` in `dsp_host`, and `python3 -B test_reference.py` tests the model. diff --git a/sdk/drafts/grainbox/TESTING.md b/sdk/drafts/grainbox/TESTING.md new file mode 100644 index 0000000000..e70a9761f2 --- /dev/null +++ b/sdk/drafts/grainbox/TESTING.md @@ -0,0 +1,141 @@ +# Grainbox testing + +Record what you ran, on which build and which unit, and what you saw. "Not tested" is a valid answer; never write a result you did not observe, and keep emulator runs apart from hardware ones. + +## On the unit + +### Test procedure: grainbox 0.1.0-experimental, 2026-12-04 + +Build: revision `66016cdcf30b`, package `grainbox.mwrm` SHA-256 `192662abdcc6a8a2c69e2f0c15270d9924174d7ff027b419eaaae80bd96af959`. `npm run test:procedure -- grainbox --package /../grainbox.mwrm --share /tmp/modwerk-share.sock --no-emulator --soak-pc-channel 11 --soak-auto-channel 11 --out `, 603 s. Emulator results are not hardware evidence; only what ran on the unit is. + +| # | Step | Layer | Result | Command | Evidence | +|---|---|---|---|---|---| +| 1 | Software checks | host | passed | `npm run module:doctor -- --draft grainbox` | logs/01-software.log | +| 2 | Beside every other module | contract | not run: a draft is judged beside every other module by module:doctor --draft (its compatibility line, step 1) | | | +| 3 | Name the build | host | passed | | logs/03-build.log | +| 4 | DSP cost model | contract | passed | `node scripts/cycle-scan.mjs check` | logs/04-cost-model.log | +| 5 | Emulator rehearsal | emulator | not run: no emulator session: left out with --no-emulator | | | +| 6 | Read the unit | unit | passed: Part 1 on A01: working = saved, saved once so that RELOAD works at the end | `node scripts/device.mjs status --socket /tmp/modwerk-share.sock`
`node scripts/octatrack-part-state.mjs read --socket /tmp/modwerk-share.sock --out /part-start.json`
`node scripts/device.mjs do inventory --socket /tmp/modwerk-share.sock` | logs/06-unit-read.log | +| 7 | Scripted unit pass | unit | passed | `npm run device:test -- --share /tmp/modwerk-share.sock --testing-entry /device-test.md /../grainbox.mwrm` | logs/07-unit-pass.log | +| 8 | Soak | unit | passed: Part 1 on A01: transport stopped, back on A01 from A04, Part 1 on A01 reloaded; working = saved | `node scripts/device.mjs try /../grainbox.mwrm --seconds 420 --socket /tmp/modwerk-share.sock`
`node scripts/octatrack-soak.mjs --seconds 120 --programs 0-3 --pc-channel 11 --auto-channel 11 --log /soak.json`
`node scripts/octatrack-part-state.mjs restore --socket /tmp/modwerk-share.sock --start /part-start.json` | logs/08-soak.log | +| 9 | Cost on the unit | unit | passed | `npm run device:price -- --share /tmp/modwerk-share.sock --out /price.json --scene 'the bank, pattern and Part the unit was on, transport stopped (test:procedure)' --curve T1,T3,T6,T7 /../grainbox.mwrm` | logs/09-cost-unit.log | +| 10 | Put back | unit | passed: Part 1 on A01: nothing to put back; working = saved | `node scripts/device.mjs do inventory --socket /tmp/modwerk-share.sock`
`node scripts/octatrack-part-state.mjs restore --socket /tmp/modwerk-share.sock --start /part-start.json` | logs/10-restore.log | +| 11 | Agent by hand | unit | passed: on 192662ab, Grainbox on T1 and T2 FX2 (both core 1) while the pattern played: ACT 64, MIX 127, FDBK 80, WIDE 127; FDBK 127 (freeze) then the transport stopped for 8 s; FDBK back to 60 and playing; TONE 0 with WIDE 127; BLUR 127 with FDBK 126; SHAP swept end to end five times in 1.2 s; ORBIT D with SHAP 127 and RPT 8. Health after each: 0 stalls, 0 errors, no crash, CPU 390-694. After STOP: stall none recorded; the late-entry guard counted 132 entries in its 80-190 us window, each waited (longest wait 109.9 us), latest entry 142.7 us late; no skipped frame. The sound not judged (the author's stage). STOP, Part reloaded, the trial rolled back | `device try`, `pick:T1:FX2:GRAINBOX`, `pick:T2:FX2:GRAINBOX`, `png:`, `health`, `stats`, `stall`, `late`, `reload` | freeze-stopped.png | +| 12 | Submitter's own stage | unit | passed (reported by the author): kept on the flashed base, played, Part saved and reloaded, power cycle; see below | | | + +#### Edge cases for this change + +Derive them from what the change touches and run each one (docs/TEST_PROCEDURE.md#edge-cases). A start, from the manifest and the changed files: + +- an effect slot: FX1 and FX2, both DSP cores, eight instances at once; one edited while another plays (docs/MODULE_QUALIFICATION.md#essential-persistence-and-instance-checks--8-october-2026) +- persistence: saved in a Part, then a Part reload, a project reload and a power cycle; removed while a saved Part still names it (docs/OCTATRACK_MODULE_EDGE_CASES.md#copying-and-saving) +- the loader: install, replace, remove and Undo; a set the base refuses rolls back whole; the pool full; a trial that expires with a page open (docs/OCTATRACK_HARDWARE_TRAPS.md#dsp-loader-dynamic-dsp-code) +- DSP code: DSP FULL; both cores; the receiver window; pipeline stalls the instruction count hides (docs/OCTATRACK_HARDWARE_TRAPS.md#coldfire-and-dsp-load) +- the USB link: USB unplugged mid-transfer; the host silent until the base rolls back; a reconnect (docs/OCTATRACK_HARDWARE_TRAPS.md#usb-and-ep0) +- load and admission: the pick at the 85 % line with the delays quiet and open; the guard counters while playing (docs/OCTATRACK_HARDWARE_TRAPS.md#coldfire-and-dsp-load) + +| Case | Why this change makes it a risk | How run | Result | +|---|---|---|---| +| FDBK 127 with the transport stopped, past 4 s | fix 1: the silence watch no longer ends or gates a frozen texture | trial, freeze then STOP for 8 s | passed: no stall, error or crash; whether it keeps sounding is the author's to hear (not recorded over USB) | +| FDBK 127 then 60 | the writer stops and starts again | trial, playing | passed: no stall, error or crash | +| SHAP swept end to end, fast | fix 2: the grain glides 16 samples a call | trial, five sweeps in 1.2 s | passed: no stall, error or crash; clicks are the author's to hear | +| ORBIT D, SHAP 127, RPT 8 | fix 3: quarter-speed heads, the 2368 cap | trial, 8 s playing; test_quarter_speed_heads_get_five_grains_of_ring in the model | passed: no stall, error or crash | +| TONE 0 with WIDE 127 | fix 4: the side through TONE's low-pass | trial, 5 s playing; test_tone_darkens_the_side_too in the model | passed: no stall, error or crash | +| BLUR 127 with FDBK 126 and WIDE 127 | the loudest feedback path; BLUR narrows the side | trial, 6 s playing | passed: no stall, error or crash | +| Two instances on one core (T1, T2), all of the above | the dearest per-sample path twice on core 1 | trial | passed: core 1 696/706 per mille after STOP; the late-entry guard waited 132 entries (longest 109.9 us), nothing skipped | +| Five instances at once (step 7) | the cost of the fixes | test:procedure step 7 | passed: core 1 peak 862, core 0 peak 769 per mille (limit 900) | +| Saved in a Part; Part reload and power cycle | persistence of the pick and its knobs | the author's stage on 192662ab | passed, as reported: kept, played, Part saved and reloaded, power-cycled ("Everything power cycled fine, sounds great") | + +#### Left for the submitter at their unit + +- how it sounds and plays, and timing by ear +- MIDI, scenes and recording with it +- it beside the other modules installed on their unit +- two instances with different settings, on both FX slots and both DSP cores where it can (docs/MODULE_QUALIFICATION.md) +- Part reload, project save and reload, and a power cycle with it saved: what survives (the Keep and the power cycle are theirs) + +#### For the pull request + +Tested on a unit: package 192662abdcc6, MKII, OS 1.40C, development twin 117eb07f of base ddb56b72, test:procedure steps 6 to 11; the developer's stage on 192662ab: kept, played, Part reload and power cycle survived. +Edge cases: the table above, one row a case (or "Edge cases: none, because …"). + +The author's trial of 192662ab before this run (4 December): "sounds good". + +#### The author's stage on 192662ab (as reported) + +4 December 2026, the author's MKII on their flashed base ddb56b72 (OS 1.40C): package 192662ab kept in place of 7ee24439 beside the card's 27 other modules (`npm run device -- try grainbox.mwrm --seconds 60 --accept`: no crash, stall, freeze or skipped track; the kept set otherwise unchanged). The first attempt was refused, loader refusal 7 RUNTIME_IN_USE, while the bank on screen named the older Grainbox; on another bank it went in. The author played it, saved and reloaded the Part and power-cycled: "Everything power cycled fine, sounds great". + +### Test procedure: grainbox 0.1.0-experimental, 2026-12-04 + +Build: revision `b6bdce07f397`, package `grainbox.mwrm` SHA-256 `7ee24439c27ae9d29e8f094f6c5d2ff0e202f76af74611de4be08823c1b1a3f0`. `npm run test:procedure -- grainbox --package /../grainbox.mwrm --share /tmp/modwerk-share.sock --no-emulator --soak-pc-channel 11 --soak-auto-channel 11 --out `, 599 s. Emulator results are not hardware evidence; only what ran on the unit is. + +| # | Step | Layer | Result | Command | Evidence | +|---|---|---|---|---|---| +| 1 | Software checks | host | passed | `npm run module:doctor -- --draft grainbox` | logs/01-software.log | +| 2 | Beside every other module | contract | not run: a draft is judged beside every other module by module:doctor --draft (its compatibility line, step 1) | | | +| 3 | Name the build | host | passed | | logs/03-build.log | +| 4 | DSP cost model | contract | passed | `node scripts/cycle-scan.mjs check` | logs/04-cost-model.log | +| 5 | Emulator rehearsal | emulator | not run: no emulator session: left out with --no-emulator | | | +| 6 | Read the unit | unit | passed: Part 1 on A01: working = saved, saved once so that RELOAD works at the end | `node scripts/device.mjs status --socket /tmp/modwerk-share.sock`
`node scripts/octatrack-part-state.mjs read --socket /tmp/modwerk-share.sock --out /part-start.json`
`node scripts/device.mjs do inventory --socket /tmp/modwerk-share.sock` | logs/06-unit-read.log | +| 7 | Scripted unit pass | unit | passed | `npm run device:test -- --share /tmp/modwerk-share.sock --testing-entry /device-test.md /../grainbox.mwrm` | logs/07-unit-pass.log | +| 8 | Soak | unit | passed: Part 1 on A01: transport stopped, back on A01 from A03, Part 1 on A01 reloaded; working = saved | `node scripts/device.mjs try /../grainbox.mwrm --seconds 420 --socket /tmp/modwerk-share.sock`
`node scripts/octatrack-soak.mjs --seconds 120 --programs 0-3 --pc-channel 11 --auto-channel 11 --log /soak.json`
`node scripts/octatrack-part-state.mjs restore --socket /tmp/modwerk-share.sock --start /part-start.json` | logs/08-soak.log | +| 9 | Cost on the unit | unit | passed | `npm run device:price -- --share /tmp/modwerk-share.sock --out /price.json --scene 'the bank, pattern and Part the unit was on, transport stopped (test:procedure)' --curve T1,T3,T6,T7 /../grainbox.mwrm` | logs/09-cost-unit.log | +| 10 | Put back | unit | passed: Part 1 on A01: nothing to put back; working = saved | `node scripts/device.mjs do inventory --socket /tmp/modwerk-share.sock`
`node scripts/octatrack-part-state.mjs restore --socket /tmp/modwerk-share.sock --start /part-start.json` | logs/10-restore.log | +| 11 | Agent by hand | unit | passed: on 7ee24439, Grainbox on T1 and T2 FX2 (both core 1) while the pattern played: ACT 127, MIX 127, FDBK 90; FDBK 127 (freeze) and back to 60; BLUR 127 with FDBK 126; TONE 0; SHAP 127 on CLOUD A and CLOUD D; health after each: 0 stalls, 0 errors, no crash, CPU 629-704; after STOP stall none recorded, latest entry 76.0 us late (0 in the guard's 80-190 us window). Main and setup pages match the README (main.png, setup.png); STOP, Part reloaded, the trial rolled back. MAIN and CUE not recorded over USB: the recorder is not built on this computer | `device try`, `pick:T1:FX2:GRAINBOX`, `pick:T2:FX2:GRAINBOX`, `png:`, `health`, `stats`, `stall`, `late`, `reload` | main.png, setup.png, freeze.png, blur-fdbk.png | +| 12 | Submitter's own stage | unit | passed (reported by the author): kept on the flashed base, played, Part saved and reloaded, power cycle; see below | | | + +#### Edge cases for this change + +Derive them from what the change touches and run each one (docs/TEST_PROCEDURE.md#edge-cases). A start, from the manifest and the changed files: + +- an effect slot: FX1 and FX2, both DSP cores, eight instances at once; one edited while another plays (docs/MODULE_QUALIFICATION.md#essential-persistence-and-instance-checks--8-october-2026) +- persistence: saved in a Part, then a Part reload, a project reload and a power cycle; removed while a saved Part still names it (docs/OCTATRACK_MODULE_EDGE_CASES.md#copying-and-saving) +- the loader: install, replace, remove and Undo; a set the base refuses rolls back whole; the pool full; a trial that expires with a page open (docs/OCTATRACK_HARDWARE_TRAPS.md#dsp-loader-dynamic-dsp-code) +- DSP code: DSP FULL; both cores; the receiver window; pipeline stalls the instruction count hides (docs/OCTATRACK_HARDWARE_TRAPS.md#coldfire-and-dsp-load) +- the USB link: USB unplugged mid-transfer; the host silent until the base rolls back; a reconnect (docs/OCTATRACK_HARDWARE_TRAPS.md#usb-and-ep0) +- load and admission: the pick at the 85 % line with the delays quiet and open; the guard counters while playing (docs/OCTATRACK_HARDWARE_TRAPS.md#coldfire-and-dsp-load) + +| Case | Why this change makes it a risk | How run | Result | +|---|---|---|---| +| FDBK 127 freezes, then 60 records again | new: the writer stops while the heads keep reading | trial, T1 playing, setup F to 127 for 6 s, then 60 for 4 s | passed: no stall, error or crash; the sound not judged (the author's stage) | +| BLUR 127 with FDBK 126 | the loudest feedback path, through the all-passes | trial, 6 s playing | passed: no stall, error or crash; CPU 704 | +| TONE 0 | the low-pass at its darkest coefficient | trial, 5 s playing | passed: no stall, error or crash | +| SHAP 127 on CLOUD A, then CLOUD D | the longest grain (130 ms) and the slow-head cap (89 ms) | trial, 5 s each, playing | passed: no stall, error or crash | +| Two instances on one core (T1, T2) | two rings and two sets of BLUR lines; the cost of both | trial, both picked and played | passed: core 1 602/612 per mille after STOP, no stall | +| Two on core 0 (T5, T6), meters while playing | the first run's all-at-once peak (955 per mille, step 7, run A below) | diagnostic trial, 8 readings defaults then 6 at ACT 0 | passed: core 0 peaks 620-761, averages 608-755; the rerun's all-at-once peak 771 | +| FX1 | the ring needs an FX2-sized buffer | `--slots fx2`: not offered on FX1; verify.py's FX1-sized slot passes dry | not run on the unit: FX2 only by declaration | +| Saved in a Part; Part, project reload and power cycle | persistence of the pick and its knobs | not run: needs a Keep (the author's stage) | not run | + +#### Left for the submitter at their unit + +- how it sounds and plays, and timing by ear +- MIDI, scenes and recording with it +- it beside the other modules installed on their unit +- two instances with different settings, on both FX slots and both DSP cores where it can (docs/MODULE_QUALIFICATION.md) +- Part reload, project save and reload, and a power cycle with it saved: what survives (the Keep and the power cycle are theirs) + +#### For the pull request + +Tested on a unit: package 7ee24439c27a, MKII, OS 1.40C, development twin 117eb07f of base ddb56b72, test:procedure steps 6 to 11; the developer's stage on 7ee24439: kept, played, Part reload and power cycle survived. +Edge cases: the table above, one row a case (or "Edge cases: none, because …"). + +Run A, the same build an hour earlier, stopped at step 7: the all-at-once stage (Grainbox on T1, T2, T3, T5 and T6 while the pattern played) read core 0 peaking at 955 per mille, over the stage's 900 (average 719). Two earlier attempts stopped before it: Part 1 differed from its saved copy (reloaded), then T1's saved FX2 named a kept module the twin shows as MISSING (set to DELAY and saved, with the author's OK). The diagnostic trial above (two instances on core 0, playing) never peaked over 761, and the rerun's all-at-once peak was 771: recorded as a one-off transient in that stage, not reproduced. + +#### The author's stage (as reported) + +4 December 2026, the author's MKII on their flashed base ddb56b72 (OS 1.40C): package 7ee24439 kept beside the card's 27 other modules (`npm run device -- try grainbox.mwrm --seconds 60 --accept`: no crash, stall, freeze or skipped track; the kept set otherwise unchanged). The author picked it, played it, saved the Part, reloaded it and power-cycled: "everything survived". They first reported MIX 0 not giving a dry track; it was their scene B holding MIX at full, and with that off MIX works ("it's working fine"). The engine settles to within one LSB of dry about 18 ms after MIX reaches 0 (dsp_host, MIX 127 then 0 with FDBK, BLUR and TONE set). + +1. 3 December 2026, the author's MKII on development twin 117eb07f of base ddb56b72 (OS 1.40C), two RAM trials of 120 s with `npm run module:dev -- grainbox --cycles 363 --state 123 --buffer 15870 --slots fx2`, Grainbox picked on T1 FX2 and checked running, each rolled back with no crash, stall, freeze or skipped track logged. Reported: MIX 127 still sounded like a lot of the original (the dry was gone; the grains at speed 1 from 30-130 ms back shadowed the track), CLOUD D distorted (its 7-bit crush), and asked for ambient improvements. Changed, with the author's OK: CLOUD sprays over up to the whole ring and detunes one head a call by up to 15 cents; CLOUD D deep instead of crushed; squared grain fades; ACT 127 every slice; FDBK (slot 11, replacing DGRD); a stereo place per grain with WIDE. verify.py's burst gate now renders 5 s (room for FDBK and TAIL at their longest; still exact silence at the end). Three RAM trials of the ambient build (`--cycles 382 --state 130`), each rolled back clean. The author then asked to replace DIRT (not needed for ambient) with TONE, a low-pass on the grains, and for FDBK 127 to freeze the ring: done. verify.py adds a freeze case per TYPE. Two RAM trials of that build (`--cycles 372 --state 130`), rolled back clean. The author then asked for three more ambient changes: textures swell in at TAIL's rate (SPARK still plucks), grains to 130 ms (about 90 ms with a slower head, so the spray keeps an eighth of the ring), and BLUR (all-passes before the feedback point) in place of RAND, its lines taking the buffer's last 1,022 words. The first BLUR build (three stages, 414.8 worst, declared 415) was refused by the unit's loader at verify, refusal 6 RUNTIME_CYCLES; nothing was installed. BLUR went to two stages (467 and 359 samples), worst 400.8. Not yet trialled on the unit. + + +## Stock flows + +Not tested. List the stock flows you compared with and without the module (docs/module-guides/README.md, Leave stock flows alone). + +## Sound quality + +Not tested. npm run fx:audit checks aliasing, DC, clipping and idle behaviour (docs/module-guides/effects.md, Sound quality). + +## Performance + +`python3 -B perf.py` in dsp_host, then `npm run perf:audit -- check evidence/performance.json --stamp`, 3 December 2026: static loop 363 words; measured unsplit 355.8 to 369.1, worst 407.9 (the dearest ORBIT setting with a split of 1 frame); stress clean (eight instances, per-core peaks 1612.0 and 1510.4, 0 clobbers, 0 stray writes). 1.30x SPRING REV (314.19), justified in the record (evidence/performance.json, after the review fixes). diff --git a/sdk/drafts/grainbox/engine.asm b/sdk/drafts/grainbox/engine.asm new file mode 100644 index 0000000000..4f3008fbf8 --- /dev/null +++ b/sdk/drafts/grainbox/engine.asm @@ -0,0 +1,1824 @@ +; GRAINBOX -- an original granular texture effect for FX2 of any track. +; Copyright (c) 2026 jrs1717, MIT (LICENSE). +; +; The track's mono sum, at a quarter of full scale, goes into a ring of +; 14,848 words in the FX2 slot's own buffer; BLUR's two all-pass lines take +; the next 871 of the last 1,022 (it stops at +$3DFD, so Sidechain Compressor's key window +; stays free). On an FX1-sized slot +; (allocator base under $4000) the frames pass dry. +; +; A slice is TIME (1/64 to 1/2) at the project's tempo (tempo24 at r6+$13), +; played as 1-32 chunks of at most 7,400 samples. At each slice boundary ACT is +; the chance that the slice plays as grains. A grain is SHAP's length (441 to +; 5,733 samples, 10 to 130 ms; 3,946 at most with a head under 15/16 speed) +; with a fade in and out (rising over its first quarter, falling over its last, squared); every call a new +; piece of recent audio, a grain and 64 samples or more behind the writer, +; becomes the newest window, and three read heads take it as they wrap: +; CLOUD heads at a third each, sprayed at random over RPT eighths of the ring: +; A plain, B two an octave up, C one reversed, D deep (an octave and a +; fifth down); one head a call detuned up to 15 cents around its rate +; ORBIT heads at 1/2, 1/4, 1/4, the window sweeping back and forth over RPT +; grain lengths (once in 8, 2, 4 or 16 chunks for A-D), an eighth of +; it at random; heads 1 and 2 a fifth and an octave up (A), an octave +; down and up (B), an octave and a fourth down (C), random (D) +; SPARK RPT onsets a slice: every head on the newest window at its start, at +; the onset's step (A a major arpeggio, B octaves, C stacked fifths, D +; random), head 1 reversed and head 2 an octave down; the level whole +; at each onset, decaying at TAIL's rate +; From silence, heads 1 and 2 start a third and two thirds into the grain, so +; they wrap apart. Each grain's fade is squared (no corner at its ends); with +; WIDE, each head takes a new random place whenever it takes a new grain +; (CLOUD, ORBIT). FDBK (slot 11) writes up to 0.45 of the last sample's grains (twice the ring's +; level: a loop gain of about 0.9) +; into the ring beside the input; the silence watch hears the input alone. +; Every head wraps at the grain's length and, at a wrap, takes the newest +; window: there its fade, min(p, W - p) x K (K = 2^25 / F, F half the grain), +; is nothing, so heads change grains without a click; a head still past the +; end after a wrap (the grain shrank under it) lands at the start. Each head's +; gain moves an eighth of the way to its target a call: TYPE's, or nothing +; while the writer nears a window it still plays, where it takes the newest +; window once faded out. A chunk's last event takes what is left of it, so +; SPARK keeps the grid. A texture starting from silence sets every head on the +; newest window at once. The level G swells in at TAIL's rate when a texture +; starts (SPARK: whole at each onset, falling at TAIL's rate) and, when +; it ends, falls at TAIL's rate (about 50 ms to -60 dB at 0, 3 s at 127) while +; the heads play on; one starting over a tail picks up from there. WIDE spreads +; heads 1 and 2 left and right (CLOUD, ORBIT) or ping-pongs SPARK's onsets. +; Should the writer come within 32 samples of a window still sounding during a +; tail, the ring freezes. After a second of silence in no texture starts; after +; four, any texture or tail stops and the ring records again. TONE is a one-pole +; low-pass on the grains (0.03 + 0.97 t^2) before the feedback point; FDBK 127 +; stops the writer, so the grains circulate over the held ring (freeze). +; +; Knobs (value<<16): page 1 TIME r6+0 (0-5), ACT r6+1, SHAP r6+2, RPT r6+3 +; (0-7: 1-8), MIX r6+4, TAIL r6+5; page 2 TYPE slot 6 (r6+$c bits 16-23, 0-2), +; TONE slot 7, VAR slot 8 (r6+$d bits 16-23, 0-3), WIDE slot 9, BLUR slot 10, +; FDBK slot 11. MIX glides per sample and snaps within 64 LSB. Between textures +; the track plays dry (a tail echoes over it at MIX); while one plays the dry +; ducks by D x G (D = 4/3 MIX, whole at 96); from 96 to 127 the dry fades out +; everywhere: 127 is the grains alone. +; +; State (X, offsets from r7; init clears $00..$81): +; $00 ring base (Y) $01 live (an FX2-sized slot) $02 write index +; $03 the write step (1; 0 while a tail froze the ring) $04 samples captured $05 LFSR $06 playing +; $07 samples into the slice (after an event: how late it is) $08 a chunk +; $09 events left in the chunk $0a event number $0b head 0's window (Y) +; $0d/$0e head 0 position hi/lo $0f head 0 rate (Q21) +; $10 the newest window (Y) $11 its W, the grain less one $12 head 0's rate before detune $13 K, the heads' fade slope +; $14 free $15 G, the level $16 the grain (SHAP) $17 G's rate +; $18 a tail playing $19 samples to the next event $1a/$1b MIX target/run $1c the dry's gain +; $1d TYPE $1e VAR $1f ACT $20 G's target $21 RPT $22 TIME $23 tempo24 +; $24-$27 the speeds T0 (Q21) $28-$2b SPARK A's steps $2c seeded $2d head 1's rate before detune $2e/$2f the frame's L/R +; $30 free $32 TAIL's decay $33 the fade's share (1/2: full a quarter in) $34 the chunk now $35 ORBIT's sweep step +; $36-$3b ORBIT's speeds for A-C $3c/$3d the side's gains (heads 1 and 2) $3e head 2's rate before detune $3f P (1 in a texture) +; $40 chunks in a slice of TIME $41 the chunk's place in it $42 chunks left in the texture +; $43 D, the grains' gain $44 K, the dry's ceiling $45 events in a chunk (1; SPARK RPT) +; $46 1 while FDBK 127 freezes the ring $47 the writer's step this call (0 frozen) $48 BLUR's output $49 TONE's low-pass state +; $4a TONE's coefficient $4b free $4c/$4d WIDE's ping-pong gains $4e WIDE $4f BLUR $50 FDBK (x 0.45) +; $51 TONE $52 scratch $53 samples of silence in $54 the call's loudest input +; $55-$57 the heads' faded reads (scratch) $58/$59 BLUR's lines' places (Y) $5a the side's low-pass state $5b the ring a window needs ahead of the writer (2 or 3 grains) $5c the side's narrowing (BLUR) $5d-$5f free; $60-$62 head 1: position hi/lo, rate $63 the head detuned next $64 its gain $65-$67 head 2: the same $69 its gain +; $6b head 0's gain $6c ORBIT's sweep phase $6d an event's length $6e the newest window's ring index +; $6f-$72 SPARK B's steps $73-$76 SPARK C's steps $77 the side $78 head 1's window (Y) $7a head 2's window (Y) +; $7b-$7d the window each head last had $7e-$80 each head's place (pan) $81 head 0's side gain +; Nothing above $81 is touched. Every product is the signed mpy/mac x0,y1 +; form. Labels are gb and two characters, so none is another's prefix. +; CYCLES_FORWARD_BRANCHES +init: + clr a + move r7,r5 + move #>$ffffff,m5 + do #$82,>gbi0 + move a,x:(r5)+ +gbi0: + nop + move x:>$213,r4 + move #>$ffffff,m4 + nop + move x:(r4),x0 + move x0,x:(r7+$00) + move x0,x:(r7+$0b) + move x0,x:(r7+$78) + move x0,x:(r7+$7a) + move x0,x:(r7+$10) + move x0,a + move #>$4000,x0 + sub x0,a + clr b + move #>$1,x0 + tst a + tpl x0,b + move b,x:(r7+$01) +; BLUR's all-pass lines after the ring (467 at R, 359 at R+512), cleared; an FX2-sized slot only. + tst b + beq gbi2 + move x:(r7+$00),a + add #>14848,a + move a1,x:(r7+$58) + move a1,r3 + add #>512,a + move a1,x:(r7+$59) + move #>$ffffff,m3 + clr a + do #871,>gbi1 + move a,y:(r3)+ +gbi1: + nop +gbi2: + move #>$1,x0 + move x0,x:(r7+$03) + move #>$123456,x0 + move x0,x:(r7+$05) + move #>$080000,x0 + move x0,x:(r7+$24) + move #>$100000,x0 + move x0,x:(r7+$25) + move #>$400000,x0 + move x0,x:(r7+$26) + move #>$600000,x0 + move x0,x:(r7+$27) + move #>$300000,x0 + move x0,x:(r7+$36) + move #>$400000,x0 + move x0,x:(r7+$37) + move #>$100000,x0 + move x0,x:(r7+$38) + move #>$400000,x0 + move x0,x:(r7+$39) + move #>$100000,x0 + move x0,x:(r7+$3a) + move #>$180000,x0 + move x0,x:(r7+$3b) + move #>$200000,x0 + move x0,x:(r7+$28) + move #>$285146,x0 + move x0,x:(r7+$29) + move #>$2ff222,x0 + move x0,x:(r7+$2a) + move #>$400000,x0 + move x0,x:(r7+$2b) + move #>$100000,x0 + move x0,x:(r7+$6f) + move #>$200000,x0 + move x0,x:(r7+$70) + move #>$400000,x0 + move x0,x:(r7+$71) + move #>$200000,x0 + move x0,x:(r7+$72) + move #>$200000,x0 + move x0,x:(r7+$73) + move #>$300000,x0 + move x0,x:(r7+$74) + move #>$400000,x0 + move x0,x:(r7+$75) + move #>$600000,x0 + move x0,x:(r7+$76) + rts + +proc: + move x:(r7+$01),a + tst a + beq gbzz + move #>$ffffff,m4 + move #>$ffffff,m5 +; TIME: slot 0, at most 5 + move x:(r6+$0),a + and #>$7f0000,a + asr #$10,a,a + move #>$5,x0 + cmp x0,a + tgt x0,a + move a1,x:(r7+$22) +; ACT, tempered: 126 plays about 69 % of the slices, 127 every slice + move x:(r6+$1),a + and #>$7fffff,a + move a1,x0 + move #>$5a0000,y1 + mpy x0,y1,a + move x0,b + move #>$7f0000,y0 + cmp y0,b + move #>$7fffff,y1 + tge y1,a + move a,x:(r7+$1f) +; SHAP's grain is set a call below; a grain fades over all of itself: share 1/2. + move #>$400000,x0 + move x0,x:(r7+$33) +; RPT: slot 3, 0-7, one more repeat + move x:(r6+$3),a + and #>$7f0000,a + asr #$10,a,a + move #>$7,x0 + cmp x0,a + tgt x0,a + add #>$1,a + move a1,x:(r7+$21) +; TAIL: slot 5; the decay a sample (about 50 ms at 0, 3 s at 127), c_min + (c_max - c_min)(1 - t)^3 + move x:(r6+$5),a + and #>$7fffff,a + move a1,x1 + move #>$7fffff,a + sub x1,a + move a,x0 + move x0,y1 + mpy x0,y1,a + move a,y1 + mpy x0,y1,a + move a,x0 + move #>$0064ea,y1 + mpy x0,y1,a + move #>$0001b6,y0 + add y0,a + move a,x:(r7+$32) +; MIX: 127 and above exactly full wet + move x:(r6+$4),a + and #>$7fffff,a + move a1,x0 + move x0,a + move #>$7f0000,y0 + cmp y0,a + move #>$7fffff,y1 + tge y1,a + move a1,x:(r7+$1a) +; TYPE: slot 6, at most 2 + move x:(r6+$c),a + and #>$7f0000,a + asr #$10,a,a + move #>$2,x0 + cmp x0,a + tgt x0,a + move a1,x:(r7+$1d) +; VAR: slot 8, at most 3 + move x:(r6+$d),a + and #>$7f0000,a + asr #$10,a,a + move #>$3,x0 + cmp x0,a + tgt x0,a + move a1,x:(r7+$1e) +; TONE slot 7 (r6+$c bits 8-15), WIDE slot 9 (r6+$d bits 8-15), BLUR slot 10 (r6+$e bits 16-23), +; FDBK slot 11 (r6+$e bits 8-15), each as v<<16. + move x:(r6+$c),a + and #>$00ff00,a + asl #8,a,a + move a1,x:(r7+$51) + move x:(r6+$d),a + and #>$00ff00,a + asl #8,a,a + move a1,x:(r7+$4e) + move x:(r6+$e),a + and #>$7f0000,a + move a1,x:(r7+$4f) + move x:(r6+$e),a + and #>$00ff00,a + asl #8,a,a + move a1,x0 + move #>$39999a,y1 + mpy x0,y1,a + move a,x:(r7+$50) +; FDBK at 127 freezes the ring: nothing new recorded ($46 = 1). + move x:(r6+$e),a + and #>$00ff00,a + clr b + move #>$1,y0 + move #>$7f00,x0 + cmp x0,a + teq y0,b + move b1,x:(r7+$46) +; tempo24, clean; none or under 30 BPM is 120, over 300 is 300 + move x:(r6+$13),b + asr #$8,b,b + move b1,x0 + move x0,b + move #>$2d0,x0 + cmp x0,b + move #>$b40,x0 + tlt x0,b + move #>$1c20,x0 + cmp x0,b + tgt x0,b + move b1,x:(r7+$23) +; L = TIME's samples: 2 x samples-per-note x tempo24 over 2 tempo24 + move x:(r7+$22),b + move #>$0,a + move #>$791fd0,a0 + move #>$1,x0 + cmp x0,b + blt gbtd + move #>$0,a + move #>$f23fa0,a0 + move #>$2,x0 + cmp x0,b + blt gbtd + move #>$1,a + move #>$e47f40,a0 + move #>$3,x0 + cmp x0,b + blt gbtd + move #>$3,a + move #>$c8fe80,a0 + move #>$4,x0 + cmp x0,b + blt gbtd + move #>$7,a + move #>$91fd00,a0 + move #>$5,x0 + cmp x0,b + blt gbtd + move #>$f,a + move #>$23fa00,a0 +gbtd: + move x:(r7+$23),x0 + bsr gbdi + move x0,b + move #>$1,x1 + move #>7400,x0 + cmp x0,b + ble gbh1 + asr #$1,b,b + move b1,y0 + move y0,b + move x1,a + asl #1,a,a + move a1,x1 +gbh1: + cmp x0,b + ble gbh2 + asr #$1,b,b + move b1,y0 + move y0,b + move x1,a + asl #1,a,a + move a1,x1 +gbh2: + cmp x0,b + ble gbh3 + asr #$1,b,b + move b1,y0 + move y0,b + move x1,a + asl #1,a,a + move a1,x1 +gbh3: + cmp x0,b + ble gbh4 + asr #$1,b,b + move b1,y0 + move y0,b + move x1,a + asl #1,a,a + move a1,x1 +gbh4: + cmp x0,b + ble gbh5 + asr #$1,b,b + move b1,y0 + move y0,b + move x1,a + asl #1,a,a + move a1,x1 +gbh5: + move b1,x:(r7+$08) + move x1,x:(r7+$40) +; The first call after init starts MIX on its target and the clock on L. + move x:(r7+$2c),a + tst a + bne gbsd + move x:(r7+$1a),x0 + move x0,x:(r7+$1b) + move #>$7fffff,x0 + move x0,x:(r7+$1c) + move x:(r7+$08),x0 + move x0,x:(r7+$19) + move #>$1,x0 + move x0,x:(r7+$2c) +gbsd: + move x:(r7+$1a),a + move x:(r7+$1b),x0 + sub x0,a + abs a + move #>64,x0 + cmp x0,a + move x:(r7+$1b),b + move x:(r7+$1a),y0 + tlt y0,b + move b1,x:(r7+$1b) +; The tail: over below -72 dB; the writer kept off every window still sounding (the newest, each head's with a +; gain) by a freeze. + move x:(r7+$18),a + tst a + beq gbt9 + move x:(r7+$15),a + move #>$002000,x0 + cmp x0,a + bge gbt5 + bsr gbqt + bra gbt9 +gbt5: + move x:(r7+$03),a + tst a + beq gbt9 + clr b + move x:(r7+$10),a + bsr gbdd + move #>32,x0 + cmp x0,a + bgt gbf1 + move #>$1,b +gbf1: + move x:(r7+$6b),a + tst a + beq gbf2 + move x:(r7+$0b),a + bsr gbdd + move #>32,x0 + cmp x0,a + bgt gbf2 + move #>$1,b +gbf2: + move x:(r7+$64),a + tst a + beq gbf3 + move x:(r7+$78),a + bsr gbdd + move #>32,x0 + cmp x0,a + bgt gbf3 + move #>$1,b +gbf3: + move x:(r7+$69),a + tst a + beq gbf4 + move x:(r7+$7a),a + bsr gbdd + move #>32,x0 + cmp x0,a + bgt gbf4 + move #>$1,b +gbf4: + tst b + beq gbt9 + clr a + move a,x:(r7+$03) +gbt9: +; Neither a glitch nor a tail, and frozen: record again, from nothing captured. + move x:(r7+$03),a + tst a + bne gbt8 + move x:(r7+$06),a + tst a + bne gbt8 + move x:(r7+$18),a + tst a + bne gbt8 + move #>$1,x0 + move x0,x:(r7+$03) + clr a + move a,x:(r7+$04) +gbt8: +; Four seconds of silence in (the transport stopped): no loop, no tail, and the ring records again. Not while +; FDBK 127 freezes the ring: the held moment plays on. + move x:(r7+$46),b + tst b + bne gbz1 + move x:(r7+$53),a + move #>176400,x0 + cmp x0,a + blt gbz1 + bsr gbqt + clr b + move b,x:(r7+$06) + move b,x:(r7+$04) + move #>$1,x0 + move x0,x:(r7+$03) +gbz1: +; SHAP: the grain, 441 + 5292 s samples (10 to 130 ms), W one less, and its fade. A window needs k grains of +; ring ahead of the writer ($5b): 2, or 3 with a head under 15/16 speed, or 5 under 3/8 (last call's rates); +; the grain stops where the spray would keep less than an eighth of the ring (5920, 3946, 2368). It moves toward +; SHAP's value 16 samples a call at most (a turn or a lock never clicks), and to a lower cap at once. + move x:(r6+$2),a + and #>$7fffff,a + move a1,x0 + move #>5292,y1 + mpy x0,y1,a + move #>441,x0 + add x0,a + move a1,x1 + move x:(r7+$0f),a + abs a + move x:(r7+$62),b + abs b + cmp b,a + tgt b,a + move x:(r7+$67),b + abs b + cmp b,a + tgt b,a + move #>5920,x0 + move #>2,y1 + move #>$1e0000,b + cmp b,a + bge gbg3 + move #>3946,x0 + move #>3,y1 + move #>$0c0000,b + cmp b,a + bge gbg3 + move #>2368,x0 + move #>5,y1 +gbg3: + move x1,a + cmp x0,a + tgt x0,a + move a1,x1 + move x:(r7+$16),b + tst b + beq gbg5 + add #>16,b + move x1,a + cmp b,a + bgt gbg6 + sub #>32,b + cmp b,a + blt gbg6 +gbg5: + move x1,b +gbg6: + cmp x0,b + tgt x0,b + move b1,x0 + mpy x0,y1,a + asr #1,a,a + move a0,x1 + move x1,x:(r7+$5b) + move b,a + move a1,x:(r7+$16) + sub #>$1,a + move a1,x:(r7+$11) + move a1,x0 + bsr gbfa +; A slice boundary: the next event, the end of a texture, or the decision. + move x:(r7+$07),a + move x:(r7+$19),x0 + cmp x0,a + blt gbnv + bsr gbev +gbnv: +; The newest window, a call's grain: g + 64 + j behind the writer. The reach is RPT grains (SPARK two), at most +; R - MARGIN - $5b (at least an eighth of the ring); CLOUD and SPARK take j at random within it, ORBIT sweeps it back and forth (and an eighth at random). + move x:(r7+$16),y1 + move x:(r7+$1d),b + move #>$2,x0 + cmp x0,b + bne gbk1 + move y1,a + asl #1,a,a + bra gbk2 +gbk1: + tst b + bne gbk7 + move #>1856,y1 +gbk7: + move x:(r7+$21),x0 + mpy x0,y1,a + asr #1,a,a + move a0,x0 + move x0,a +gbk2: + move x:(r7+$5b),x0 + move #>13696,b + sub x0,b + cmp b,a + tgt b,a + move a1,x1 + move x:(r7+$1d),b + move #>$1,x0 + cmp x0,b + bne gbk3 + move n7,x0 + move x:(r7+$35),y1 + mpy x0,y1,a + asr #1,a,a + move a0,x0 + move x:(r7+$6c),a + add x0,a + and #>$7fffff,a + move a1,x:(r7+$6c) + move #>$400000,x0 + cmp x0,a + blt gbk4 + move #>$7fffff,b + sub a,b + tfr b,a +gbk4: + asl #1,a,a + move a1,x0 + move x1,a + asr #3,a,a + move a1,y0 + move x1,a + sub y0,a + move a1,y1 + mpy x0,y1,a + move a1,x:(r7+$52) + bsr gbrn + move y0,y1 + mpy x0,y1,a + move x:(r7+$52),x0 + add x0,a + move a1,x0 + bra gbk5 +gbk3: + bsr gbrn + move x1,y1 + mpy x0,y1,a + move a1,x0 +gbk5: + move x:(r7+$16),a + add x0,a + add #>64,a + move a1,x0 + move x:(r7+$02),a + sub x0,a + move #>14848,x0 + tst a + bge gbk6 + add x0,a +gbk6: + move a1,x:(r7+$6e) + move x:(r7+$00),x0 + add x0,a + move a1,x:(r7+$10) +; One head a call detuned at random around its rate before detune (CLOUD, ORBIT): the grains blur, up to 15 cents. + move x:(r7+$1d),a + move #>$2,x0 + cmp x0,a + beq gbj9 + move x:(r7+$63),a + move a1,x1 + move a,b + add #>$1,b + move #>$3,x0 + cmp x0,b + blt gbj1 + clr b +gbj1: + move b1,x:(r7+$63) + bsr gbrn + move x0,a + sub #>$400000,a + asl #1,a,a + move a,x0 + move #>$011d26,y1 + mpy x0,y1,a + move a1,y1 + move x1,a + tst a + bne gbj2 + move x:(r7+$12),x0 + mpy x0,y1,a + add x0,a + move a1,x:(r7+$0f) + bra gbj9 +gbj2: + move #>$1,y0 + cmp y0,a + bne gbj3 + move x:(r7+$2d),x0 + mpy x0,y1,a + add x0,a + move a1,x:(r7+$62) + bra gbj9 +gbj3: + move x:(r7+$3e),x0 + mpy x0,y1,a + add x0,a + move a1,x:(r7+$67) +gbj9: +; Each head's gain an eighth of the way to its target a call: TYPE's ($55-$57, gbtg), or nothing while the writer +; nears a window the head still plays; close enough, while a loop plays, the head takes the newest window, faded out. + bsr gbtg + move x:(r7+$0b),x0 + move x:(r7+$10),a + cmp x0,a + beq gba1 + move x0,a + bsr gbdd + move #>1088,x0 + cmp x0,a + bgt gbb1 + clr b + move b,x:(r7+$55) +gbb1: + move x:(r7+$06),b + tst b + beq gba1 + move #>64,x0 + cmp x0,a + bgt gba1 + move x:(r7+$10),x0 + move x0,x:(r7+$0b) + clr b + move b,x:(r7+$0d) + move b,x:(r7+$0e) +gba1: + move x:(r7+$55),a + move x:(r7+$6b),x0 + sub x0,a + asr #3,a,a + add x0,a + move a1,x:(r7+$6b) + move x:(r7+$78),x0 + move x:(r7+$10),a + cmp x0,a + beq gba2 + move x0,a + bsr gbdd + move #>1088,x0 + cmp x0,a + bgt gbb2 + clr b + move b,x:(r7+$56) +gbb2: + move x:(r7+$06),b + tst b + beq gba2 + move #>64,x0 + cmp x0,a + bgt gba2 + move x:(r7+$10),x0 + move x0,x:(r7+$78) + clr b + move b,x:(r7+$60) + move b,x:(r7+$61) +gba2: + move x:(r7+$56),a + move x:(r7+$64),x0 + sub x0,a + asr #3,a,a + add x0,a + move a1,x:(r7+$64) + move x:(r7+$7a),x0 + move x:(r7+$10),a + cmp x0,a + beq gba3 + move x0,a + bsr gbdd + move #>1088,x0 + cmp x0,a + bgt gbb3 + clr b + move b,x:(r7+$57) +gbb3: + move x:(r7+$06),b + tst b + beq gba3 + move #>64,x0 + cmp x0,a + bgt gba3 + move x:(r7+$10),x0 + move x0,x:(r7+$7a) + clr b + move b,x:(r7+$65) + move b,x:(r7+$66) +gba3: + move x:(r7+$57),a + move x:(r7+$69),x0 + sub x0,a + asr #3,a,a + add x0,a + move a1,x:(r7+$69) +; The side's gains: none for SPARK (its onsets ping-pong); CLOUD and ORBIT give a head a new place within WIDE +; whenever it has taken a new grain, each at its gain now. + move x:(r7+$1d),a + move #>$2,x0 + cmp x0,a + bne gbsx + clr a + move a,x:(r7+$81) + move a,x:(r7+$3c) + move a,x:(r7+$3d) + bra gbsy +gbsx: + move x:(r7+$0b),x0 + move x:(r7+$7b),a + cmp x0,a + beq gbm1 + move x0,x:(r7+$7b) + bsr gbrn + move x0,a + sub #>$400000,a + asl #1,a,a + move a,x0 + move x:(r7+$4e),y1 + mpy x0,y1,a + move a,x:(r7+$7e) +gbm1: + move x:(r7+$78),x0 + move x:(r7+$7c),a + cmp x0,a + beq gbm2 + move x0,x:(r7+$7c) + bsr gbrn + move x0,a + sub #>$400000,a + asl #1,a,a + move a,x0 + move x:(r7+$4e),y1 + mpy x0,y1,a + move a,x:(r7+$7f) +gbm2: + move x:(r7+$7a),x0 + move x:(r7+$7d),a + cmp x0,a + beq gbm3 + move x0,x:(r7+$7d) + bsr gbrn + move x0,a + sub #>$400000,a + asl #1,a,a + move a,x0 + move x:(r7+$4e),y1 + mpy x0,y1,a + move a,x:(r7+$80) +gbm3: +; BLUR's diffusion is mono: the side narrows to half at BLUR 127. + move x:(r7+$4f),a + asr #1,a,a + move a1,x0 + move #>$7fffff,a + sub x0,a + move a1,x:(r7+$5c) + move x:(r7+$6b),x0 + move x:(r7+$7e),y1 + mpy x0,y1,a + move a,x0 + move x:(r7+$5c),y1 + mpy x0,y1,a + move a,x:(r7+$81) + move x:(r7+$64),x0 + move x:(r7+$7f),y1 + mpy x0,y1,a + move a,x0 + move x:(r7+$5c),y1 + mpy x0,y1,a + move a,x:(r7+$3c) + move x:(r7+$69),x0 + move x:(r7+$80),y1 + mpy x0,y1,a + move a,x0 + move x:(r7+$5c),y1 + mpy x0,y1,a + move a,x:(r7+$3d) +gbsy: +; P: 1 while a glitch plays, else 0 (a tail echoes over the whole track). + move x:(r7+$06),b + move #>$7fffff,x0 + clr a + tst b + tne x0,a + move a1,x:(r7+$3f) +; TONE: the grains' low-pass coefficient, 0.03 + 0.97 t^2 (about 200 Hz at 0, whole at 127). + move x:(r7+$51),x0 + move x0,y1 + mpy x0,y1,a + move a,x0 + move #>$7bffff,y1 + mpy x0,y1,a + move #>$040000,x0 + add x0,a + move a1,x:(r7+$4a) +; The writer's step: none while frozen. + move x:(r7+$03),a + move x:(r7+$46),b + tst b + beq gbz5 + clr a +gbz5: + move a1,x:(r7+$47) + move x:(r7+$00),a + move x:(r7+$02),x0 + add x0,a + move a1,r4 + move #>$39ff,m4 + move x:(r7+$47),n4 + move #>$39ff,m5 + move x:(r7+$58),r1 + move #>$1d2,m1 + move x:(r7+$59),r3 + move #>$166,m3 + move #>$1,n0 + do n7,>gbsl +; MIX glides; D = 4/3 MIX (whole at 96), K = 4 (1 - MIX) (the dry's ceiling: whole up to 96, none at 127) + move x:(r7+$1a),a + move x:(r7+$1b),x0 + sub x0,a + asr #6,a,a + add x0,a + move a1,x:(r7+$1b) + move x:(r7+$1b),x0 + move #>$2aaaab,y1 + mpy x0,y1,a + add x0,a + move a,x:(r7+$43) + move #>$7fffff,a + sub x0,a + asl #2,a,a + move a,x:(r7+$44) +; The frame, and the ring at a quarter of the mono sum. + move x:(r0),x1 + move x:(r0+n0),y0 + move x1,x:(r7+$2e) + move y0,x:(r7+$2f) + move x1,a + add y0,a + asr #2,a,a + move a,x1 +; The input's loudest sample this call, for the silence watch (the track's, not the feedback's). + move x1,a + abs a + move x:(r7+$54),b + cmp a,b + tlt a,b + move b1,x:(r7+$54) +; The ring: the input plus FDBK's share of the last sample's grains. + move x:(r7+$31),x0 + move x:(r7+$50),y1 + mpy x0,y1,a + add x1,a + move a,y:(r4) + move (r4)+n4 +; Head 0: position += rate, wrapped into [0, W) (W the newest window's); at a wrap it takes the newest +; window, where its fade is nothing; faded by min(p, W - p) x K; an interpolated read, faded, to $55. + move x:(r7+$0d),a + move x:(r7+$0e),a0 + move x:(r7+$0f),b + asr #21,b,b + add b,a + move x:(r7+$11),x0 + clr b + cmp x0,a + tge x0,b + sub b,a + move b,x1 + clr b + cmp x0,a + tge b,a + tst a + tlt x0,b + add b,a + add x1,b + move x:(r7+$0b),b + move x:(r7+$10),y1 + tne y1,b + move b1,x:(r7+$0b) + move b1,r5 + move a1,x:(r7+$0d) + move a0,x:(r7+$0e) + move a1,n5 + move a0,b + lsr b + move b1,y0 + move x0,b + sub a,b + cmp b,a + tgt b,a + move a1,x0 + move x:(r7+$13),y1 + mpy x0,y1,a + asl #22,a,a + move a,x0 + move x0,y1 + mpy x0,y1,a + move a,x1 + move (r5)+n5 + move y:(r5)+,a + move y:(r5),b + sub a,b + move b,x0 + move y0,y1 + mac x0,y1,a + move a1,y1 + move x1,x0 + mpy x0,y1,a + move a,x:(r7+$55) +; Head 1: position += rate, wrapped into [0, W) (W the newest window's); at a wrap it takes the newest +; window, where its fade is nothing; faded by min(p, W - p) x K; an interpolated read, faded, to $56. + move x:(r7+$60),a + move x:(r7+$61),a0 + move x:(r7+$62),b + asr #21,b,b + add b,a + move x:(r7+$11),x0 + clr b + cmp x0,a + tge x0,b + sub b,a + move b,x1 + clr b + cmp x0,a + tge b,a + tst a + tlt x0,b + add b,a + add x1,b + move x:(r7+$78),b + move x:(r7+$10),y1 + tne y1,b + move b1,x:(r7+$78) + move b1,r5 + move a1,x:(r7+$60) + move a0,x:(r7+$61) + move a1,n5 + move a0,b + lsr b + move b1,y0 + move x0,b + sub a,b + cmp b,a + tgt b,a + move a1,x0 + move x:(r7+$13),y1 + mpy x0,y1,a + asl #22,a,a + move a,x0 + move x0,y1 + mpy x0,y1,a + move a,x1 + move (r5)+n5 + move y:(r5)+,a + move y:(r5),b + sub a,b + move b,x0 + move y0,y1 + mac x0,y1,a + move a1,y1 + move x1,x0 + mpy x0,y1,a + move a,x:(r7+$56) +; Head 2: position += rate, wrapped into [0, W) (W the newest window's); at a wrap it takes the newest +; window, where its fade is nothing; faded by min(p, W - p) x K; an interpolated read, faded, to $57. + move x:(r7+$65),a + move x:(r7+$66),a0 + move x:(r7+$67),b + asr #21,b,b + add b,a + move x:(r7+$11),x0 + clr b + cmp x0,a + tge x0,b + sub b,a + move b,x1 + clr b + cmp x0,a + tge b,a + tst a + tlt x0,b + add b,a + add x1,b + move x:(r7+$7a),b + move x:(r7+$10),y1 + tne y1,b + move b1,x:(r7+$7a) + move b1,r5 + move a1,x:(r7+$65) + move a0,x:(r7+$66) + move a1,n5 + move a0,b + lsr b + move b1,y0 + move x0,b + sub a,b + cmp b,a + tgt b,a + move a1,x0 + move x:(r7+$13),y1 + mpy x0,y1,a + asl #22,a,a + move a,x0 + move x0,y1 + mpy x0,y1,a + move a,x1 + move (r5)+n5 + move y:(r5)+,a + move y:(r5),b + sub a,b + move b,x0 + move y0,y1 + mac x0,y1,a + move a1,y1 + move x1,x0 + mpy x0,y1,a + move a,x:(r7+$57) +; The heads at their gains; and the side: each head at its place within WIDE. + move x:(r7+$55),x0 + move x:(r7+$6b),y1 + mpy x0,y1,a + move x:(r7+$56),x0 + move x:(r7+$64),y1 + mac x0,y1,a + move x:(r7+$57),x0 + move x:(r7+$69),y1 + mac x0,y1,a +; TONE: a one-pole low-pass on the grains' sum (a), before the feedback point (each pass through FDBK darker); +; t stays in y0 through BLUR. + move x:(r7+$49),y0 + sub y0,a + move a,x0 + move x:(r7+$4a),y1 + mpy x0,y1,a + add y0,a + move a1,x:(r7+$49) + move a1,y0 +; BLUR: two all-passes in series (w = in + g z, out = z - g w, g 0.625), on r1 and r3. + move y0,a + move #>$500000,y1 + move y:(r1),b + move b,x0 + mac x0,y1,a + move a,x0 + move x0,y:(r1)+ + mpy x0,y1,a + sub a,b + tfr b,a + move y:(r3),b + move b,x0 + mac x0,y1,a + move a,x0 + move a,y:(r3)+ + mpy x0,y1,a + sub a,b +; BLUR's share of them: t + BLUR (d - t), to $48. + move b,x0 + move x0,a + sub y0,a + move a,x0 + move x:(r7+$4f),y1 + mpy x0,y1,a + add y0,a + move a1,x:(r7+$48) +; The side: each head at its place, through TONE's low-pass as the grains are ($5a its state). + move x:(r7+$55),x0 + move x:(r7+$81),y1 + mpy x0,y1,a + move x:(r7+$56),x0 + move x:(r7+$3c),y1 + mac x0,y1,a + move x:(r7+$57),x0 + move x:(r7+$3d),y1 + mac x0,y1,a + move x:(r7+$5a),y0 + sub y0,a + move a,x0 + move x:(r7+$4a),y1 + mpy x0,y1,a + add y0,a + move a1,x:(r7+$5a) + move a1,x:(r7+$77) +; The dry ducks under the loop as its level G rises: 1 - D x G x P, at most K, so from MIX 96 to 127 the dry +; fades out everywhere. G stays in y0 to the end of the sample. + move x:(r7+$15),y0 + move y0,y1 + move x:(r7+$3f),x0 + mpy x0,y1,a + move a,x0 + move x:(r7+$43),y1 + mpy x0,y1,a + move a,x0 + move #>$7fffff,a + sub x0,a + move x:(r7+$44),x0 + cmp x0,a + tgt x0,a + move a,x:(r7+$1c) +; The grains: BLUR's output x 2, half the mono sum, as loud as the dry track for a mono sample; at G. + move x:(r7+$48),a + asl #1,a,a + move a,x0 + move y0,y1 + mpy x0,y1,a + move a,x:(r7+$31) + move a,x0 + move x:(r7+$43),y1 + mpy x0,y1,a + move a,x1 +; The side at G and D, x 2. + move x:(r7+$77),x0 + move y0,y1 + mpy x0,y1,a + move a,x0 + move x:(r7+$43),y1 + mpy x0,y1,a + asl #1,a,a + move a,x:(r7+$77) +; G toward its target at its rate: TAIL's, rising as a texture swells in and falling as it fades. + move x:(r7+$20),a + sub y0,a + move a,x0 + move x:(r7+$17),y1 + mpy x0,y1,a + add y0,a + move a1,x:(r7+$15) +; Left and right: the loop at D (x1) x WIDE's ping-pong + dry x its gain, plus and minus the side. + move x1,x0 + move x:(r7+$4c),y1 + mpy x0,y1,a + move x:(r7+$2e),x0 + move x:(r7+$1c),y1 + mac x0,y1,a + move x:(r7+$77),x0 + add x0,a + move a,x:(r0)+ + move x1,x0 + move x:(r7+$4d),y1 + mpy x0,y1,a + move x:(r7+$2f),x0 + move x:(r7+$1c),y1 + mac x0,y1,a + move x:(r7+$77),x0 + sub x0,a + move a,x:(r0)+ +gbsl: + nop + move r1,x:(r7+$58) + move r3,x:(r7+$59) + move r4,a + move x:(r7+$00),x0 + sub x0,a + move a1,x:(r7+$02) + move n7,x0 + move x:(r7+$07),a + add x0,a + move a1,x:(r7+$07) +; The silence watch: samples since the input last reached 64 LSB (at most $100000). + move x:(r7+$53),b + add x0,b + move #>$100000,x1 + cmp x1,b + tgt x1,b + move x:(r7+$54),a + move #>64,y1 + cmp y1,a + move #>$0,y0 + tge y0,b + move b1,x:(r7+$53) + clr a + move a,x:(r7+$54) + move x:(r7+$03),b + tst b + beq gbzz + move x:(r7+$04),a + add x0,a + move #>14832,y0 + cmp y0,a + tgt y0,a + move a1,x:(r7+$04) +; Back to linear addressing, as the code after us expects: m4 and m5 held the ring's modulus, m1 and m3 +; BLUR's lines. +gbzz: + move #>$ffffff,m4 + move #>$ffffff,m5 + move #>$ffffff,m1 + move #>$ffffff,m3 + rts + +; A slice boundary. +gbev: + move x:(r7+$07),a + move x:(r7+$19),x0 + sub x0,a + cmp x0,a + move #>$0,y0 + tge y0,a + move a1,x:(r7+$07) + move x:(r7+$06),a + tst a + beq gbiw + move x:(r7+$09),a + sub #>$1,a + move a1,x:(r7+$09) + tst a + beq gbce + move x:(r7+$0a),a + add #>$1,a + move a1,x:(r7+$0a) + bsr gbsr + rts +; A chunk is over: the glitch goes on through its slice of TIME, or ends. +gbce: + bsr gbgp + move x:(r7+$42),a + sub #>$1,a + move a1,x:(r7+$42) + tst a + bgt gbcs +; The glitch is over. +gbst: + clr a + move a,x:(r7+$06) + move a,x:(r7+$20) + move a,x:(r7+$63) + move a,x:(r7+$68) + move #>$1,x0 + move x0,x:(r7+$18) + move x:(r7+$32),x0 + move x0,x:(r7+$17) + bra gbid +; Idle at a chunk's end. +gbiw: + bsr gbgp +gbid: + move x:(r7+$08),x0 + move x0,x:(r7+$19) +; Decided only on TIME's grid, and not after a second of silence in; while FDBK 127 freezes the ring, every +; slice plays, silence or not (an endless pad). + move x:(r7+$41),a + tst a + bne gbix + move x:(r7+$46),b + tst b + bne gbv5 + move x:(r7+$53),a + move #>44100,y0 + cmp y0,a + bge gbix +gbv5: + move x:(r7+$04),a + cmp x0,a + blt gbix + bsr gbrn + move x:(r7+$1f),a + cmp x0,a + bgt gbv6 + move x:(r7+$46),b + tst b + beq gbix +gbv6: +; A tail froze the ring: end it, record again, and wait for a fresh chunk. + move x:(r7+$03),a + tst a + bne gbfr + bsr gbqt + clr a + move a,x:(r7+$04) + move #>$1,x0 + move x0,x:(r7+$03) + bra gbix +gbfr: + move x:(r7+$40),x0 + move x0,x:(r7+$42) +; A chunk starts. +gbcs: +; Fresh when nothing sounds (no texture, no tail): then every head takes the newest window at once. $52: scratch. + move x:(r7+$06),a + move x:(r7+$18),x0 + add x0,a + move a1,x:(r7+$52) + move #>$1,x0 + move x0,x:(r7+$06) + move x0,x:(r7+$03) + clr a + move a,x:(r7+$0a) + move a,x:(r7+$18) + move #>$7fffff,x0 + move x0,x:(r7+$20) + move x:(r7+$32),x0 + move x0,x:(r7+$17) +; Events in the chunk: one; SPARK RPT onsets; the event's length a chunk / that. + move #>$1,x0 + move x0,x:(r7+$45) + move x:(r7+$1d),a + move #>$2,x0 + cmp x0,a + bne gbr1 + move x:(r7+$21),x0 + move x0,x:(r7+$45) +gbr1: + move x:(r7+$08),a + move a1,x:(r7+$34) + asr #23,a,a + move x:(r7+$45),x0 + bsr gbdi + move x0,x:(r7+$6d) + move #>$200000,x0 + move x0,x:(r7+$0f) + move x0,x:(r7+$62) + move x0,x:(r7+$67) + move x:(r7+$1d),a + tst a + beq gbcl + move #>$1,x0 + cmp x0,a + beq gbor + bra gbf0 +; CLOUD: B two heads an octave up; C one head reversed; D deep. +gbcl: + move x:(r7+$1e),a + move #>$1,x0 + cmp x0,a + bne gbc2 + move #>$400000,x0 + move x0,x:(r7+$62) + move x0,x:(r7+$67) + bra gbc3 +gbc2: + move #>$2,x0 + cmp x0,a + bne gbc4 + move #>$e00000,x0 + move x0,x:(r7+$62) + bra gbc3 +gbc4: + move #>$3,x0 + cmp x0,a + bne gbc3 + move #>$100000,x0 + move x0,x:(r7+$62) + move #>$155b81,x0 + move x0,x:(r7+$67) +gbc3: + bra gbf0 +; ORBIT: heads 1 and 2 at VAR's speeds (D random); the sweep's step, +; back and forth once in 2^(3, 1, 2, 4) chunks. +gbor: + move x:(r7+$1e),a + move #>$3,x0 + cmp x0,a + beq gbo4 + asl #1,a,a + move a1,n1 + move #>$ffffff,m1 + lua (r7+$36),r1 + nop + move x:(r1+n1),x0 + move x0,x:(r7+$62) + lua (r7+$37),r1 + nop + move x:(r1+n1),x0 + move x0,x:(r7+$67) + bra gbo5 +gbo4: + bsr gbt0 + move x0,x:(r7+$62) + bsr gbt0 + move x0,x:(r7+$67) +gbo5: +gbo6: + move x:(r7+$1e),b + move x:(r7+$34),a + move #>$0,x0 + cmp x0,b + bne gbw5 + asl #3,a,a +gbw5: + move #>$1,x0 + cmp x0,b + bne gbw6 + asl #1,a,a +gbw6: + move #>$2,x0 + cmp x0,b + bne gbw7 + asl #2,a,a +gbw7: + move #>$3,x0 + cmp x0,b + bne gbw8 + asl #4,a,a +gbw8: + move a1,x0 + move #>$1,a + move #>$0,a0 + bsr gbdi + move x0,x:(r7+$35) +; From silence: every gain at TYPE's at once; every head but SPARK's on the newest window, on time. +gbf0: + move x:(r7+$0f),x0 + move x0,x:(r7+$12) + move x:(r7+$62),x0 + move x0,x:(r7+$2d) + move x:(r7+$67),x0 + move x0,x:(r7+$3e) + move x:(r7+$52),a + tst a + bne gbse + bsr gbtg + move x:(r7+$55),x0 + move x0,x:(r7+$6b) + move x:(r7+$56),x0 + move x0,x:(r7+$64) + move x:(r7+$57),x0 + move x0,x:(r7+$69) + move x:(r7+$1d),a + move #>$2,x0 + cmp x0,a + beq gbse + move x:(r7+$10),x0 + move x0,x:(r7+$0b) + clr a + move a,x:(r7+$0e) + move x:(r7+$07),a + move x:(r7+$0f),b + tst b + bpl gbp1 + move x:(r7+$11),a + sub #>$1,a + move x:(r7+$07),x0 + sub x0,a +gbp1: + move a1,x:(r7+$0d) + move x:(r7+$10),x0 + move x0,x:(r7+$78) + clr a + move a,x:(r7+$61) + move x:(r7+$62),b + tst b + bmi gbv2 + move x:(r7+$11),x0 + move #>$2aaaab,y1 + mpy x0,y1,b + move x:(r7+$07),a + move b1,x0 + add x0,a + bra gbp2 +gbv2: + move x:(r7+$11),a + sub #>$1,a + move x:(r7+$07),x0 + sub x0,a +gbp2: + move a1,x:(r7+$60) + move x:(r7+$10),x0 + move x0,x:(r7+$7a) + clr a + move a,x:(r7+$66) + move x:(r7+$67),b + tst b + bmi gbv3 + move x:(r7+$11),x0 + move #>$555555,y1 + mpy x0,y1,b + move x:(r7+$07),a + move b1,x0 + add x0,a + bra gbp3 +gbv3: + move x:(r7+$11),a + sub #>$1,a + move x:(r7+$07),x0 + sub x0,a +gbp3: + move a1,x:(r7+$65) +gbse: + move x:(r7+$45),x0 + move x0,x:(r7+$09) + bsr gbsr +gbix: + rts +; The next chunk of TIME's slice. +gbgp: + move x:(r7+$41),a + add #>$1,a + move x:(r7+$40),x0 + cmp x0,a + move #>$0,y0 + tge y0,a + move a1,x:(r7+$41) + rts + +; An event within the chunk: SPARK's next onset, on time (every head on the newest window at its start, at the +; onset's step, the third an octave down; the level whole and decaying at TAIL's rate), its ping-pong; or the chunk's +; one event (CLOUD, ORBIT: no ping-pong, WIDE spreads heads 1 and 2 in the call). +gbsr: + move x:(r7+$1d),a + move #>$2,x0 + cmp x0,a + beq gbs1 + clr a + move a,x:(r7+$52) + move x:(r7+$6d),x0 + move x0,x:(r7+$19) + bsr gbrm + bra gbw2 +gbs1: + move x:(r7+$1e),a + move #>$3,x0 + cmp x0,a + bne gbs2 + bsr gbt0 + bra gbs5 +gbs2: + move #>$28,x1 + tst a + beq gbs3 + move #>$6f,x1 + move #>$1,x0 + cmp x0,a + beq gbs3 + move #>$73,x1 +gbs3: + move x:(r7+$0a),a + and #>$3,a + add x1,a + move a1,n1 + move r7,r1 + move #>$ffffff,m1 + nop + move x:(r1+n1),x0 +gbs5: + move x0,x:(r7+$55) +gbs6: + move x:(r7+$10),x0 + move x0,x:(r7+$0b) + move x:(r7+$07),x0 + move x0,x:(r7+$0d) + clr a + move a,x:(r7+$0e) + move x:(r7+$55),x0 + move x0,x:(r7+$0f) + move x:(r7+$10),x0 + move x0,x:(r7+$78) + move x:(r7+$07),x0 + move x0,x:(r7+$60) + clr a + move a,x:(r7+$61) + move x:(r7+$55),x0 + move x0,x:(r7+$62) + move x:(r7+$10),x0 + move x0,x:(r7+$7a) + move x:(r7+$07),x0 + move x0,x:(r7+$65) + clr a + move a,x:(r7+$66) + move x:(r7+$55),x0 + move x0,x:(r7+$67) + move x:(r7+$55),a + neg a + move a,x:(r7+$62) + move x:(r7+$11),a + sub #>$1,a + move x:(r7+$07),x0 + sub x0,a + move a1,x:(r7+$60) + move x:(r7+$55),a + asr #1,a,a + move a1,x:(r7+$67) + move #>$7fffff,x0 + move x0,x:(r7+$15) + clr a + move a,x:(r7+$20) + move x:(r7+$32),x0 + move x0,x:(r7+$17) + move x:(r7+$6d),x0 + move x0,x:(r7+$19) + bsr gbrm +gbs9: +; WIDE: the repeat's side, + on even repeats and - on odd. + move x:(r7+$4e),a + move x:(r7+$0a),b + and #>$1,b + tst b + beq gbw1 + neg a +gbw1: + move a,x:(r7+$52) +gbw2: + move x:(r7+$52),x0 + move #>$7fffff,y0 + move #>$7fffff,a + sub x0,a + cmp y0,a + tge y0,a + move a,x:(r7+$4d) + move #>$7fffff,b + add x0,b + cmp y0,b + tge y0,b + move b,x:(r7+$4c) + rts + +; The next LFSR word; x0 = a random fraction. +; x0 = a random speed from T0. +gbt0: + bsr gbsp + move x0,n1 + lua (r7+$24),r1 + move #>$ffffff,m1 + nop + move x:(r1+n1),x0 + rts + +; x0 = a random index into T0, 0-3. +gbsp: + bsr gbrn + move #>$4,y1 + mpy x0,y1,a + move a1,x0 + rts + +; The heads' fade for a window of x0 + 1: F = (x0 + 1) x SHAP's share, at least 64, at most half the window (so a +; short window still reaches full level), never under 5; K = 2^25 / F. +gbfa: + move x0,a + add #>$1,a + move a1,x1 + move a1,x0 + move x:(r7+$33),y1 + mpy x0,y1,a + move a1,x0 + move x0,a + move #>64,y0 + cmp y0,a + tlt y0,a + move x1,b + asr #1,b,b + cmp b,a + tgt b,a + move #>5,y0 + cmp y0,a + tlt y0,a + move a1,x0 + move #>$4,a + move #>$0,a0 + bsr gbdi + move x0,x:(r7+$13) + rts + +; The heads' gains for TYPE into $55-$57: CLOUD a third each; ORBIT and SPARK 1/2, 1/4, 1/4. +gbtg: + move x:(r7+$1d),a + tst a + bne gbu1 + move #>$2aaaab,x0 + move x0,x:(r7+$55) + move x0,x:(r7+$56) + move x0,x:(r7+$57) + rts +gbu1: + move #>$400000,x0 + move x0,x:(r7+$55) + move #>$200000,x0 + move x0,x:(r7+$56) + move x0,x:(r7+$57) + rts + +; The chunk's last event ($09 = 1) takes what is left of it: $19 = chunk - (events - 1) x $19. +gbrm: + move x:(r7+$09),a + move #>$1,x0 + cmp x0,a + bne gbq1 + move x:(r7+$45),a + sub #>$1,a + move a1,x0 + move x:(r7+$19),y1 + mpy x0,y1,a + asr #1,a,a + move a0,x0 + move x:(r7+$34),a + sub x0,a + move a1,x:(r7+$19) +gbq1: + rts + +; Nothing sounding: the level, its target and rate, and the tail's flag at rest. +gbqt: + clr a + move a,x:(r7+$18) + move a,x:(r7+$15) + move a,x:(r7+$20) + move a,x:(r7+$17) + rts + +; a = how far the writer has to go to reach a window starting at a (Y): (a - base) - write, wrapped into [0, R). +gbdd: + move x:(r7+$00),x0 + sub x0,a + move x:(r7+$02),x0 + sub x0,a + move #>14848,x0 + tst a + bge gbdz + add x0,a +gbdz: + rts + +gbrn: + move x:(r7+$05),a + lsr a + bcc gbrc + move #>$e10000,x0 + eor x0,a +gbrc: + move a1,x:(r7+$05) + and #>$7fffff,a + move a1,x0 + rts + +; x0 = floor(a / (2 x0)): a the 48-bit dividend (effects.md, Traps). +gbdi: + andi #$fe,ccr + do #24,>gbdl + div x0,a +gbdl: + move a0,x0 + rts diff --git a/sdk/drafts/grainbox/evidence/cpu-price.json b/sdk/drafts/grainbox/evidence/cpu-price.json new file mode 100644 index 0000000000..daa24471ea --- /dev/null +++ b/sdk/drafts/grainbox/evidence/cpu-price.json @@ -0,0 +1,146 @@ +{ + "module": "grainbox", + "version": "0.1.0-experimental", + "base": "development twin 117eb07f of base ddb56b72, MKII, OS 1.40C", + "scene": "the bank, pattern and Part the unit was on, transport stopped (test:procedure)", + "inputs": { + "transport": "playing" + }, + "seconds": 8, + "readings": 3, + "measuredAt": "2026-12-04T01:46:47.890Z", + "result": { + "id": "grainbox", + "version": "file", + "packageSha256": "192662abdcc6a8a2c69e2f0c15270d9924174d7ff027b419eaaae80bd96af959", + "before": { + "frame": 53.8, + "frameMax": 88.1, + "frames": 67181, + "skipped": 0, + "cpu": 66.5, + "dsp1to4": 39.8, + "dsp5to8": 49, + "dsp1to4Peak": 41.4, + "dsp5to8Peak": 50.2, + "readings": 3, + "spread": { + "frame": 0.7, + "cpu": 0.7, + "dsp1to4": 1.9, + "dsp5to8": 0.1, + "frameMax": 2.7, + "dsp1to4Peak": 0.3, + "dsp5to8Peak": 0.8 + } + }, + "atRest": { + "frame": 53.6, + "frameMax": 86.4, + "frames": 67134, + "skipped": 0, + "cpu": 66.2, + "dsp1to4": 40.1, + "dsp5to8": 49.2, + "dsp1to4Peak": 41.5, + "dsp5to8Peak": 50.2, + "readings": 3, + "spread": { + "frame": 0, + "cpu": 0.1, + "dsp1to4": 3.8, + "dsp5to8": 0, + "frameMax": 0.9, + "dsp1to4Peak": 0.8, + "dsp5to8Peak": 0.5 + } + }, + "atRestAdds": { + "frame": -0.2, + "cpu": -0.3, + "dsp1to4": 0.3, + "dsp5to8": 0.2, + "dsp1to4Peak": 0.1, + "dsp5to8Peak": 0 + }, + "inUse": { + "frame": 52.6, + "frameMax": 86.4, + "frames": 67209, + "skipped": 0, + "cpu": 65.2, + "dsp1to4": 53.6, + "dsp5to8": 47.8, + "dsp1to4Peak": 56.4, + "dsp5to8Peak": 50.1, + "readings": 3, + "spread": { + "frame": 0.7, + "cpu": 0.8, + "dsp1to4": 0.2, + "dsp5to8": 0, + "frameMax": 1.9, + "dsp1to4Peak": 0.5, + "dsp5to8Peak": 0.4 + } + }, + "inUseAdds": { + "frame": -1.2, + "cpu": -1.3, + "dsp1to4": 13.8, + "dsp5to8": -1.2, + "dsp1to4Peak": 15, + "dsp5to8Peak": -0.1 + }, + "inUseOn": "T1 FX2", + "curve": { + "tracks": [ + "T1", + "T3", + "T6", + "T7" + ], + "points": [ + 86.4, + 84.6, + 85.7, + 85.1, + 83.2 + ], + "adds": [ + -1.8, + -0.7, + -1.3, + -3.2 + ], + "first": 0, + "each": 0, + "notes": [], + "admission": [ + { + "pattern": "empty", + "number": 2, + "track": "T1", + "seconds": 8, + "admitted": true, + "frameMax": 36.5, + "frame": 27.9, + "dropped": 0, + "stalls": 0 + }, + { + "pattern": "medium", + "number": 1, + "track": "T1", + "seconds": 8, + "admitted": true, + "frameMax": 86, + "frame": 53, + "dropped": 0, + "stalls": 0 + } + ] + }, + "rolledBack": true + } +} diff --git a/sdk/drafts/grainbox/evidence/frame-cost.json b/sdk/drafts/grainbox/evidence/frame-cost.json new file mode 100644 index 0000000000..4ffd8b9b2d --- /dev/null +++ b/sdk/drafts/grainbox/evidence/frame-cost.json @@ -0,0 +1,55 @@ +{ + "module": "grainbox", + "version": "0.1.0-experimental", + "base": "development twin 117eb07f of base ddb56b72, MKII, OS 1.40C", + "scene": "the bank, pattern and Part the unit was on, transport stopped (test:procedure)", + "seconds": 8, + "measuredAt": "2026-12-04T01:46:47.890Z", + "tracks": [ + "T1", + "T3", + "T6", + "T7" + ], + "rest": 0, + "first": 0, + "each": 0, + "points": [ + 86.4, + 84.6, + 85.7, + 85.1, + 83.2 + ], + "adds": [ + -1.8, + -0.7, + -1.3, + -3.2 + ], + "admission": [ + { + "pattern": "empty", + "number": 2, + "track": "T1", + "seconds": 8, + "admitted": true, + "frameMax": 36.5, + "frame": 27.9, + "dropped": 0, + "stalls": 0 + }, + { + "pattern": "medium", + "number": 1, + "track": "T1", + "seconds": 8, + "admitted": true, + "frameMax": 86, + "frame": 53, + "dropped": 0, + "stalls": 0 + } + ], + "notes": [] +} diff --git a/sdk/drafts/grainbox/evidence/performance.json b/sdk/drafts/grainbox/evidence/performance.json new file mode 100644 index 0000000000..f04c03cbbd --- /dev/null +++ b/sdk/drafts/grainbox/evidence/performance.json @@ -0,0 +1,34 @@ +{ + "schema": 1, + "module": "grainbox", + "version": "0.1.0-experimental", + "sourceSha256": "25938c5dcff97b02f28a0b4921d769d7997712e32b8d34ecedfa56d27002107f", + "kind": "dsp", + "cycles": { + "unit": "instructions/sample", + "static": 363, + "measured": 407.9, + "instancesPerCore": 4, + "method": "static: perf.py, by the rule tools/build/cycle_count.py prices straight-line code (one cycle a word): the per-sample loop between `do n7,>gbsl` and gbsl, 363 words; it has no branch, bsr, nested do or rep, so every sample runs every word. measured: perf.py in dsp_host on verify.py's synthetic image, executed instructions a sample from dsp_host's meter, 80 cases (the defaults, the dearest setting for each TYPE, a moving pattern, each unsplit and with a trig split at every position 1-15): unsplit 355.8 to 369.1 (dearest ORBIT); worst 407.9, the dearest ORBIT setting with a split of 1 frame, where the per-call setup (the grain and its fade, the newest window's pick, a head's detune, each head's gain glide and new place, events) runs twice a block." + }, + "stock": { + "comparator": "SPRING REV", + "comparatorWorst": 314.19, + "dearestWorst": 330.94, + "baseline": "E-Verb's record (perf.py bench on the original image): SPRING REV, the cost target for new FX, worst 314.19 with trig splits (257.50 unsplit); DJ EQ, the dearest stock effect, 330.94 (293.38 unsplit), through fixed, moving and trig-split-1-15 cases. Not re-run for Grainbox; its own cases match those (fixed, moving, a split at every position 1-15).", + "stockSha256": "164f31224bf61181e3f50e7dec40df9afcae5b16dbf6e4c0d0cc5e986af0a84e", + "justification": "No stock effect plays a captured ring as grains; SPRING REV is the cost target for a new FX and, like Grainbox, reads and writes a delay buffer every sample. Grainbox's worst, 407.90, is 1.30x SPRING REV's 314.19 and 1.23x DJ EQ's 330.94; unsplit 369.1 against 257.50 and 293.38. Per sample it writes the input and FDBK's share of the grains into a 14,848-word ring and runs three read heads, each with its own position, wrap, changeover, interpolated read and squared fade; then the heads' sum and a three-head stereo side, TONE's low-pass on the grains and on their stereo side, BLUR's two all-passes and its mix, the level, the dry's duck and two outputs. The third head, the rounded grains, the feedback, the per-grain stereo and BLUR's diffuser (about 27 a sample) are what put it over SPRING REV; the author chose each of them on the unit for ambient textures, knowing the cost. A third all-pass stage (414.8 worst) was refused on the unit at verify (loader refusal 6: executed cycles are charged at 7/4 against a slot's 726, so about 414 is the most that installs); it was dropped to fit with margin. The code review's fixes added TONE's low-pass on the side (about 8 a sample); the TONE, BLUR and side stretch was reworked to keep its values in registers, saving 7, so the worst stays under what the base admits (executed cycles charged at 7/4 against a slot's 726)." + }, + "stress": { + "instancesPerCore": 4, + "tracks": 8, + "lfosPerTrack": 3, + "lockedSlots": 12, + "seconds": 2, + "guard": true, + "dirty": true, + "clobbers": 0, + "hangs": 0, + "method": "perf.py --stress: the load is driven straight into dsp_host on verify.py's synthetic image with both cores: eight Grainbox instances, four a core, each on its own stem (decaying noise bursts, a full-scale square, a 20 Hz-20 kHz sweep, full-scale noise, impulses, full-scale DC steps, silence, a three-note chord) for 2 s plus a 2 s tail; all twelve parameter words given a new value every block through the moving pattern, trig splits of 1, 3, 7, 8, 12 and 15 frames, -guard 0x4000 -guard-shared and -dirty: dsp_host exit 0, 0 stray writes, 0 clobbers, per-core peaks 1612.0 and 1510.4 of 3120. dsp_host hands every instance the same allocator entry, so the eight rings overlap here; on a unit each slot has its own buffer." + } +} diff --git a/sdk/drafts/grainbox/manifest.py b/sdk/drafts/grainbox/manifest.py new file mode 100644 index 0000000000..37c35ea1dd --- /dev/null +++ b/sdk/drafts/grainbox/manifest.py @@ -0,0 +1,87 @@ +"""GRAINBOX -- an original granular texture effect for FX2 of any track. + +The track's mono sum goes into a capture ring in the FX2 slot's buffer. At each slice (TIME at the project's +tempo) ACT decides whether the slice plays as grains: three read heads take grains of SHAP's length from the recent +audio, as CLOUD (a wash sprayed over RPT grain lengths), ORBIT (a window sweeping slowly back and forth, for drones) +or SPARK (RPT plucked onsets a slice), each with four variations (VAR, page 2). MIX sets how far the grains replace +the track, TAIL how long a texture lingers, TONE how dark it is, FDBK how much of it feeds back (127 freezes it). Timing follows the tempo word only: slices are free-running at the +tempo, not locked to the pattern's steps or swing. + +verify.py holds engine.asm to reference.py's model bit for bit in dsp_host. Not yet run on a unit. +""" +import importlib.util +import pathlib + +from remix.schema import (Category, Claims, Proof, BusRole, DspSection, Formatter, Gate, Harness, Kind, + MenuEntry, Module, Param, YBase) + +# Under its own name: every module's manifest loads in one process, and another may have a reference.py too. +_spec = importlib.util.spec_from_file_location("grainbox_ref", pathlib.Path(__file__).with_name("reference.py")) +_ref = importlib.util.module_from_spec(_spec) +_spec.loader.exec_module(_ref) + +P, S, W = Formatter.PLAIN, Formatter.STEPPED, Formatter.WIDE_STEPPED + +MODULE = Module( + name="grainbox", + key="GRAINBOX", + kind=Kind.DSP_EFFECT, + doc="Granular texture effect on FX2: CLOUD, ORBIT, SPARK grains of the track at the tempo.", + category=Category.TRACK, author="jrs1717", author_url="https://github.com/jrs1717", + proof=Proof.RENDER, + proof_note="test:procedure steps 1-11 on the author's MKII (package 192662ab) and the author's Keep, Part reload and power cycle; verify.py bit-exact against reference.py in dsp_host", + + menu=MenuEntry( + fx2_id=0x0a, + donor_desc=0x400d58b8, # DARK REV; every drawn slot is written below + abbr=b"GRAN", + fullname=b"GRAINBOX", + build_tag=False, + ), + + params=( + Param(b"TIME", 2, 6, active=True, formatter=W, labels=_ref.TIME_LABELS, + doc="the grid at the tempo, 1/64 to 1/2: textures start on it and last a slice; SPARK's onsets divide it"), + Param(b"ACT", 64, 128, active=True, formatter=P, + doc="the share of slices that play as grains: 0 none, 127 every slice"), + Param(b"SHAP", 32, 128, active=True, formatter=P, + doc="the grain's length: 10 ms at 0 to 130 ms at 127 (about 90 ms at most with a half-speed head, 54 ms with a quarter-speed one); it glides to a new value"), + Param(b"RPT", 3, 8, active=True, formatter=W, labels=tuple(str(i) for i in range(1, 9)), + doc="CLOUD's spray, 1-8 eighths of the ring back; ORBIT's sweep, 1-8 grain lengths; SPARK's onsets a slice"), + Param(b"MIX", 64, 128, active=True, formatter=P, + doc="how far the grains take over: 96 replaces the track at the texture's peak; 127 the grains alone"), + Param(b"TAIL", 64, 128, active=True, formatter=P, + doc="how slowly a texture swells in and how long it lingers, and a SPARK pluck rings: 0 about 50 ms, 127 about 3 s"), + Param(b"TYPE", 0, 3, active=True, formatter=S, labels=_ref.TYPE_NAMES, + doc="CLOUD a wash of sprayed grains, ORBIT a sweeping drone, SPARK rhythmic plucked onsets"), + Param(b"TONE", 127, 128, active=True, formatter=P, + doc="the grains' brightness: a low-pass from about 200 Hz at 0 to open at 127; each pass through FDBK darker"), + Param(b"VAR", 0, 4, active=True, formatter=S, labels=_ref.VAR_NAMES, + doc="CLOUD plain, octave shimmer, reversed, deep; ORBIT fifths, octaves, dark, random; SPARK arpeggio, octaves, fifths, random"), + Param(b"WIDE", 0, 128, active=True, formatter=P, + doc="stereo: each of CLOUD's and ORBIT's grains a new place within this width; SPARK's onsets ping-pong"), + Param(b"BLUR", 0, 128, active=True, formatter=P, + doc="diffusion: smears the grains into a wash through two all-passes; with FDBK each pass denser; 0 clear"), + Param(b"FDBK", 0, 128, active=True, formatter=P, + doc="feedback: up to about 90 % of the grains' level back into the ring, so a cloud builds on itself; 127 freezes the ring (an endless pad of the moment, every slice, even after the track stops); 0 none"), + ), + mode_slot=6, + + dsp=DspSection( + asm="modules/grainbox/engine.asm", + priority=10, + bus_role=BusRole.NONE, + ybase=YBase.NEVER, + r7_latch_slot=None, + gate_label=None, + ), + # 15,870 words from the FX2 slot's base, ending at +$3DFD: the ring (14,848) and BLUR's lines; Sidechain Compressor's key window stays free. + claims=Claims(stock_instance_buffer=True, buffer_words=_ref.BUFFER), + + harness=Harness(layout_char=None, is_server=False), + + gates=(Gate("modules/grainbox/verify.py", remix_arg=False),), + # the shortest slices, a texture every slice, the longest grains, SPARK's eight onsets (its dearest), full wet + dear={"TIME": 0, "ACT": 127, "SHAP": 127, "RPT": 7, "MIX": 127, "TAIL": 127, "TYPE": 2, "TONE": 127, "VAR": 3, + "WIDE": 127, "BLUR": 127, "FDBK": 126}, +) diff --git a/sdk/drafts/grainbox/media/LICENSE.md b/sdk/drafts/grainbox/media/LICENSE.md new file mode 100644 index 0000000000..301a45727b --- /dev/null +++ b/sdk/drafts/grainbox/media/LICENSE.md @@ -0,0 +1,12 @@ +# OT UI capture rights and attribution + +These original LCD captures were taken on the module author's own Octatrack on 2, 3 and 4 December 2026, read over USB from the +running Modwerk base (its screen capture request), for Modwerk module documentation. Credit: jrs1717. + +LicenseRef-OT-UI-Documentation covers the original capture/documentation contribution for use in Modwerk module +documentation. Underlying Octatrack interface artwork, labels and other Elektron material retain their owners' rights; +this declaration does not grant a licence to those underlying works or assert that they are MIT/CC licensed. Reviewer +verification of reuse rights is required before publication and is not automatic legal clearance. + +Only rendered PNG screenshots and capture metadata belong in this folder. No firmware, raw LCD/RAM dump, card, +recording or private log is included. diff --git a/sdk/drafts/grainbox/media/capture.json b/sdk/drafts/grainbox/media/capture.json new file mode 100644 index 0000000000..6b2668ed22 --- /dev/null +++ b/sdk/drafts/grainbox/media/capture.json @@ -0,0 +1,22 @@ +{ + "firmware": "1.40C", + "baseIdentity": "ddb56b72d1ac0b606f5969bc4984b6b22662e87d1b1d56a480ef0da1aac53b49", + "packageSha256": "7ee24439c27ae9d29e8f094f6c5d2ff0e202f76af74611de4be08823c1b1a3f0", + "setup": "The author's Octatrack on its flashed Modwerk base ddb56b72, Grainbox 0.1.0-experimental (package 7ee24439) kept beside 27 modules; the author's project, pattern A01, a track's FX2 on Grainbox with TYPE ORBIT and the other knobs as the author set them; transport stopped. The author pressed the keys; the agent read each frame over USB. imageSha256 is the MAIN image of base 0.8.5 ddb56b72, as recorded for Fuzzbox's, Glitchbox's and Loopbox's captures on the same base.", + "method": "python3 -B scripts/device-send.py png:FILE through npm run device -- serve: the base's on-demand screen frame (Screen capability), 256x128, unmodified.", + "screens": [ + { + "path": "media/ot-location.png", + "sha256": "66eab57cb2eb58a5b8398bdef30aedf3338da68339f6b800f13cee5e14bbbcbf", + "keys": "TRACK, FUNC+FX2", + "shows": "FX2 SETUP: Grainbox selected at the end of the effect list; TYPE (ORBIT), TONE, VAR, WIDE, BLUR and FDBK" + }, + { + "path": "media/ot-controls.png", + "sha256": "c764a2f3e9a5e2ce100e54117e2f9341fae87e2979f03ed3f5c04c4b6ed48a94", + "keys": "FX2", + "shows": "FX2 main page: TIME, ACT, SHAP, RPT, MIX, TAIL" + } + ], + "imageSha256": "2f7286525f0a4e84fc70fc6d48f8dab2171b6afd386be59a40a8b96eecfe3237" +} diff --git a/sdk/drafts/grainbox/media/ot-controls.png b/sdk/drafts/grainbox/media/ot-controls.png new file mode 100644 index 0000000000..923de7339c Binary files /dev/null and b/sdk/drafts/grainbox/media/ot-controls.png differ diff --git a/sdk/drafts/grainbox/media/ot-location.png b/sdk/drafts/grainbox/media/ot-location.png new file mode 100644 index 0000000000..d914729d5b Binary files /dev/null and b/sdk/drafts/grainbox/media/ot-location.png differ diff --git a/sdk/drafts/grainbox/octamod.module.json b/sdk/drafts/grainbox/octamod.module.json new file mode 100644 index 0000000000..37df63a6a4 --- /dev/null +++ b/sdk/drafts/grainbox/octamod.module.json @@ -0,0 +1,377 @@ +{ + "schemaVersion": 2, + "id": "grainbox", + "key": "GRAINBOX", + "name": "Grainbox", + "version": "0.1.0-experimental", + "category": "effects", + "author": { + "github": "jrs1717", + "credits": [ + "jrs1717" + ] + }, + "nativeManifest": "manifest.py", + "presentation": { + "label": "GRAN", + "family": "Granular", + "summary": "Granular textures on FX2 from the live track: washes of sprayed grains, sweeping drones, or rhythmic plucked onsets, at the project's tempo.", + "overview": "Grainbox keeps the last third of a second of the track and plays it as grains of 10 to 130 ms with three read heads, on a note grid at the project's tempo. CLOUD sprays grains from the recent audio into a wash, ORBIT sweeps a window slowly back and forth for drones, SPARK fires grains as rhythmic plucks. MIX blends it with the track and TAIL lets a texture linger.", + "highlights": [ + "Three types, four variations each: CLOUD, ORBIT and SPARK", + "Grains of 10-130 ms with soft rounded fades that swell in at TAIL's pace", + "BLUR smears the grains into a wash; TONE darkens them", + "FDBK feeds the grains back so clouds build; at 127 it freezes them into an endless pad" + ], + "usage": [ + "Select a track, press FUNC + FX2 and choose GRAINBOX at the end of the list.", + "Start with TYPE CLOUD, ACT 64, SHAP 32 and MIX 64 on a pad or a drum loop, then try ORBIT and SPARK." + ] + }, + "controls": [ + { + "name": "TIME", + "default": 2, + "count": 6, + "doc": "The grid at the tempo, 1/64 to 1/2: textures start on it and last a slice; SPARK's onsets divide it.", + "labels": [ + "1/64", + "1/32", + "1/16", + "1/8", + "1/4", + "1/2" + ] + }, + { + "name": "ACT", + "default": 64, + "count": 128, + "doc": "The share of slices that play as grains: 0 none, 127 every slice.", + "labels": null + }, + { + "name": "SHAP", + "default": 32, + "count": 128, + "doc": "The grain's length: 10 ms at 0 to 130 ms at 127 (about 90 ms at most with a half-speed head, 54 ms with a quarter-speed one); it glides to a new value.", + "labels": null + }, + { + "name": "RPT", + "default": 3, + "count": 8, + "doc": "CLOUD's spray, 1-8 eighths of the ring back; ORBIT's sweep, 1-8 grain lengths; SPARK's onsets a slice.", + "labels": [ + "1", + "2", + "3", + "4", + "5", + "6", + "7", + "8" + ] + }, + { + "name": "MIX", + "default": 64, + "count": 128, + "doc": "How far the grains take over: 96 replaces the track at the texture's peak; 127 the grains alone.", + "labels": null + }, + { + "name": "TAIL", + "default": 64, + "count": 128, + "doc": "How slowly a texture swells in and how long it lingers, and a SPARK pluck rings: 0 about 50 ms, 127 about 3 s.", + "labels": null + }, + { + "name": "TYPE", + "default": 0, + "count": 3, + "doc": "CLOUD a wash of sprayed grains, ORBIT a sweeping drone, SPARK rhythmic plucked onsets.", + "labels": [ + "CLOUD", + "ORBIT", + "SPARK" + ] + }, + { + "name": "TONE", + "default": 127, + "count": 128, + "doc": "The grains' brightness: a low-pass from about 200 Hz at 0 to open at 127; each pass through FDBK darker.", + "labels": null + }, + { + "name": "VAR", + "default": 0, + "count": 4, + "doc": "CLOUD plain, octave shimmer, reversed, deep; ORBIT fifths, octaves, dark, random; SPARK arpeggio, octaves, fifths, random.", + "labels": [ + "A", + "B", + "C", + "D" + ] + }, + { + "name": "WIDE", + "default": 0, + "count": 128, + "doc": "Stereo: each of CLOUD's and ORBIT's grains a new place within this width; SPARK's onsets ping-pong.", + "labels": null + }, + { + "name": "BLUR", + "default": 0, + "count": 128, + "doc": "Diffusion: smears the grains into a wash through two all-passes; with FDBK each pass denser; 0 clear.", + "labels": null + }, + { + "name": "FDBK", + "default": 0, + "count": 128, + "doc": "Feedback: up to about 90 % of the grains' level back into the ring, so a cloud builds on itself; 127 freezes the ring (an endless pad of the moment, every slice, even after the track stops); 0 none.", + "labels": null + } + ], + "compatibility": { + "firmware": "1.40C", + "location": "FX2", + "conflicts": [ + "analog-bassdrum" + ], + "uiClaims": [], + "limitations": [ + "FX2 only: the capture ring needs an FX2 slot's buffer (15,870 words). On FX1 it passes the track dry.", + "Mono capture: the grains are the track's mono sum, spread across the stereo field.", + "FDBK 127 freezes the ring on purpose: the texture holds, even after the transport stops, until FDBK is turned down.", + "Not run on a unit: Grainbox beside Sidechain Compressor (its key window starts right after the ring), and TIME 1/2 at 30 BPM; each is checked only in dsp_host.", + "Analog BD currently requires stock DSP effects; Grainbox is declared incompatible with it like other custom DSP effects.", + "Its worst case is 1.30x stock SPRING REV (three grain heads, the feedback path and BLUR); four a core fit within a core's budget." + ], + "effectId": 10 + }, + "resources": { + "recorded": "2026-12-04", + "storage": { + "label": "DSP program / state", + "display": "2142 words / 130-word state", + "value": null, + "unit": "see display", + "method": "static", + "conditions": "The USB package 192662ab (ABI 6): 2,142 program words on each DSP core it runs on; 130 words of state per instance in its own r7 block (init clears them); 15,870 words of the FX2 slot's 16K Y delay buffer per instance (the capture ring, then the two BLUR lines in its last 1,022 words), stopping short of Sidechain Compressor's key window; 1,196 bytes of ColdFire code and data (the effect's descriptor and menu), no bss.", + "source": "TESTING.md" + }, + "processing": { + "label": "DSP instruction peak", + "display": "408 instructions/sample per instance", + "value": null, + "unit": "see display", + "method": "emulator", + "conditions": "perf:audit record (perf.py in dsp_host): worst 407.9 executed instructions a sample at the dearest ORBIT setting with a trig split of 1 frame, unsplit 355.8 to 369.1, static loop floor 363; SPRING REV 314.19 and DJ EQ, the dearest stock effect, 330.94 on the same stock image. Executed instructions, not chip cycles. On the author's MKII the DSP load of T1-T4 rose 13.8 points (peak 15) with it on T1 FX2.", + "source": "evidence/performance.json" + }, + "cpuPrice": { + "value": 0, + "unit": "percent-of-frame", + "measure": "frame-interrupt", + "tool": "npm run device:price -- --write", + "date": "2026-12-04", + "conditions": "the bank, pattern and Part the unit was on, transport stopped (test:procedure); base development twin 117eb07f of base ddb56b72, MKII, OS 1.40C; 3 readings of 8 s a state. Frame interrupt 53.8 % before, 53.6 % installed and used nowhere, 52.6 % on T1 FX2; CPU meter 66.5 % before, 66.2 % at rest, 65.2 % in use.", + "source": "evidence/cpu-price.json" + }, + "impact": { + "conditions": "One instance at the dearest setting in dsp_host; on the author's MKII (twin 117eb07f of base ddb56b72) the frame-interrupt price on the test bank (Mang112 95 bank A, Part 1, pattern A01), and five instances admitted at once while playing (device:test).", + "cpu": { + "level": "minimal", + "basis": "measured-comparison", + "rationale": "No ColdFire work besides the effect's descriptor: on the author's MKII the frame interrupt read 0.2 points lower installed and unused and 1.2 lower in use (within the meter's noise), and the instance curve on T1, T3, T6 and T7 read first 0, each 0; admitted alone on the empty and the medium pattern (longest frame 86 %).", + "source": "evidence/cpu-price.json" + }, + "dsp": { + "level": "high", + "basis": "measured-comparison", + "rationale": "407.9 executed instructions a sample worst case (1.30 of SPRING REV's 314.19, 1.23 of DJ EQ's 330.94): a ring write, a feedback path with two all-pass BLUR stages and TONE's low-pass, and three grain heads, each with its own position, rounded fade, detune, pan and gliding gain, every sample. Four a core is 1,632 of 3,120. On the unit one instance raised its core's load about 14 points; five were admitted at once, the base refusing three for budget.", + "source": "evidence/performance.json" + }, + "memory": { + "level": "low", + "basis": "source-estimate", + "rationale": "2,142 program words once a core, 130 state words and a 15,870-word FX2 delay buffer per instance (the slot's own stock buffer, reused), 1,196 bytes of ColdFire code and data.", + "source": "TESTING.md" + } + } + }, + "tests": { + "report": "TESTING.md", + "summary": "On the author's MKII (OS 1.40C, twin 117eb07f of base ddb56b72): npm run test:procedure on package 192662ab built from 66016cdc passed steps 1-11 (both cores, five instances admitted at once, Part switches, a 120 s soak, the price and the curve played; by hand the freeze with the transport stopped, BLUR with FDBK 126, SHAP swept fast, ORBIT D); the author kept 192662ab beside 27 modules, played, saved and reloaded the Part and power-cycled: \"Everything power cycled fine, sounds great\". verify.py is bit-exact against reference.py in dsp_host for every TYPE and VAR; perf:audit worst 407.9 instructions a sample, 1.30x SPRING REV (evidence/performance.json).", + "hardwareStatus": "reported", + "evidenceRevision": "66016cdcf30bf4bdf5965162416cbc8872e4b4e8", + "gates": [ + "Worst-case cycles under parameter modulation and maximum load", + "Exact memory region and maximum-instance totals", + "Owner-reviewed real-hardware evidence with actual coverage and limitations" + ], + "qualification": { + "moduleVersion": "0.1.0-experimental", + "sourceSha256": "346b5d2d317fde94f9fcbc361e8123012de6956fce715343dbf34db166c103ad", + "cycles": [ + { + "processor": "dsp", + "worstCase": 408, + "maxConfiguration": 1632, + "maxInstances": 8, + "budget": 3120, + "unit": "cycles/sample", + "method": "static", + "conditions": { + "parameterExtremes": "408 cycles a sample covers the measured worst case in dsp_host, 407.9 executed instructions at the dearest ORBIT setting (TIME 1/64, ACT, SHAP, MIX, TAIL, TONE, WIDE and BLUR at 127, FDBK 126, RPT 8, VAR D) with a trig split of 1 frame (the per-call setup runs twice a block), over the defaults, the dearest setting for each TYPE and a moving pattern, unsplit and with a split at every position 1-15 (perf.py, 80 cases). The static floor of the loop alone is 363. Executed instructions, not chip cycles.", + "parameterModulation": "Knob words reach the DSP as value<<16 every call; MIX glides per sample, SHAP glides 16 samples a call, the rest are read once a call, so modulation costs nothing extra. perf.py's stress run changes every parameter word every block (every slot swept, TIME, RPT, TYPE and VAR stepped): 0 clobbers, 0 hangs. verify.py renders a knob word with an LFO's fraction bits and every knob swept while playing, bit for bit.", + "modeSwitching": "TYPE and VAR are read once a call and take effect at the next grain; their cost is inside the measured per-call setup. verify.py sweeps both while playing bit for bit; perf.py's moving case steps them; on the unit 12 Part switches ran their own effects and the soak changed programs while it ran. There is no bypass: MIX 0 leaves the track.", + "maxLoad": "4 instances a core, 8 on both: 4 x 408 = 1,632 of the 3,120 cycles a core can spend a sample. Stress in dsp_host: 4 a core, per-core peaks 1612.0 and 1510.4. On the unit five were admitted at once while playing; the base refused three to keep each core within budget.", + "inputConditions": "perf.py: stereo noise at 0.7 of full scale; the stress stems are decaying bursts, a full-scale square, a 20 Hz-20 kHz sweep, full-scale noise, impulses, full-scale DC steps, silence and a chord. The sample loop has no data-dependent branch, so the input changes no cost; verify.py checks exact silence out for silence in, a burst's tail ending in silence, FDBK below 127 decaying, and FDBK 127 holding the frozen ring." + }, + "report": "evidence/performance.json" + } + ], + "memory": { + "regions": [ + { + "name": "DSP program, the package's effect on each core it runs on", + "space": "dsp-p", + "words": 2142, + "wordBits": 24, + "bytes": 6426, + "scope": "shared" + }, + { + "name": "DSP state per instance, the package's state words", + "space": "dsp-x", + "words": 130, + "wordBits": 24, + "bytes": 390, + "scope": "instance" + }, + { + "name": "ColdFire code and data of the USB package", + "space": "cpu-ram", + "words": 1196, + "wordBits": 8, + "bytes": 1196, + "scope": "shared" + }, + { + "name": "FX2 slot delay buffer, the stock allocator's 16K Y block reused as the capture ring and the BLUR lines", + "space": "dsp-y", + "words": 15870, + "wordBits": 24, + "bytes": 47610, + "scope": "instance" + } + ], + "perInstanceBytes": 48000, + "sharedBytes": 7622, + "maxInstances": 8, + "totalBytes": 391622, + "conditions": "From the header of the USB package 192662ab (ABI 6), checked against the source: the effect uses its own r7 block X:(r7+$00..$81) (verify.py's state gate) and the FX2 slot's stock 16K Y delay buffer, 15,870 words of it (modulus $3dfd, so Sidechain Compressor's key window stays free). No stack beyond the effect's own bsr calls, no other Y memory, no scratch outside the r7 block.", + "report": "TESTING.md" + }, + "imageSha256": "192662abdcc6a8a2c69e2f0c15270d9924174d7ff027b419eaaae80bd96af959", + "hardware": { + "kind": "functional", + "status": "reported", + "model": "MKII", + "testedOn": "2026-12-04", + "tester": "jrs1717", + "sourceRevision": "66016cdcf30bf4bdf5965162416cbc8872e4b4e8", + "imageSha256": "192662abdcc6a8a2c69e2f0c15270d9924174d7ff027b419eaaae80bd96af959", + "summary": "On the author's Octatrack MKII, OS 1.40C, 4 December 2026: npm run test:procedure on package 192662ab built from 66016cdc, on development twin 117eb07f of base ddb56b72 with the test bank, passed software checks, the DSP cost model, reading the unit, the scripted pass (self-test, both DSP cores, five instances at once with three refused for budget, Part switches), a 120 s soak with MIDI programs and AUTO changes, the price (the curve played, admitted on the empty and the medium pattern, longest frame 86 %) and putting the Part back; by hand, on T1 and T2 FX2 while playing, FDBK 127 (freeze) with the transport stopped for 8 s, BLUR 127 with FDBK 126, TONE 0 with WIDE 127, SHAP swept fast and ORBIT D with RPT 8, with no stall, error or crash. The author's stage on the same package, on the flashed base: kept beside 27 modules, played, the Part saved and reloaded and the unit power-cycled; reported \"Everything power cycled fine, sounds great\".", + "limitations": [ + "Not run: beside every other module on the unit; module:doctor judges its compatibility with every module.", + "Not run: an emulator rehearsal.", + "Not recorded: MAIN and CUE over USB (the recorder is not built on the author's computer); dropouts were checked by frame timing (late entries, skipped frames, stalls) and by the author's ear.", + "Not run on the unit: TIME 1/2 below 42 BPM (the 32-chunk path); verify.py holds it bit for bit at 30 BPM in dsp_host.", + "Not tested: MIDI, scenes and recording with it, beyond the soak's program changes, scenes and crossfader.", + "Not tested beside Sidechain Compressor on the unit; the ring stops short of its key window by design.", + "The screens were captured on package 7ee24439; no control or label changed since." + ], + "report": "TESTING.md" + }, + "documentation": { + "tutorial": { + "title": "Quick tutorial", + "steps": [ + "Put Grainbox on FX2 of a track playing a pad or a drum loop. At the defaults it is CLOUD A at 1/16 with 40 ms grains.", + "Turn ACT and MIX to 127 to hear only the grains, then hold FUNC and press FX2 and step TYPE through CLOUD, ORBIT and SPARK.", + "Raise SHAP for longer grains, RPT for a wider spray or sweep (or more SPARK onsets), and TAIL to let textures swell and linger.", + "On the setup page, raise BLUR to smear the grains and FDBK to let them build; turn FDBK to 127 to freeze the moment, and TONE down to darken it.", + "Turn MIX back to 64 to blend the grains with the track again." + ] + }, + "screenshots": [ + "media/ot-location.png", + "media/ot-controls.png" + ], + "screenshotStyle": "black-and-white" + } + } + }, + "license": { + "spdx": "MIT", + "file": "LICENSE", + "declaration": "Original code by jrs1717 under the MIT licence in LICENSE: an original granular design inspired by granular effects in general, starting from the author's own Loopbox and Glitchbox engines; no third-party code, tables, samples or names. Never includes Elektron firmware, extracted routines or tables." + }, + "media": [ + { + "path": "media/ot-location.png", + "captureType": "hardware", + "credit": "jrs1717 (USB screen capture on the author's unit)", + "license": "LicenseRef-OT-UI-Documentation", + "source": "original", + "caption": "Hold FUNC and press FX2: Grainbox is chosen at the end of the effect list, with TYPE (here ORBIT), TONE and VAR on the top row and WIDE, BLUR and FDBK below.", + "alt": "FX2 SETUP page with an effect list ending in Grainbox, selected; knobs labelled ORBIT, TONE, VAR, WIDE, BLUR and FDBK.", + "otUi": { + "page": "FX2 SETUP", + "shows": "location", + "firmware": "1.40C", + "moduleVersion": "0.1.0-experimental", + "setup": "The author's Octatrack on its flashed Modwerk base ddb56b72, Grainbox 0.1.0-experimental (package 7ee24439) kept beside 27 modules; the author's project, pattern A01, a track's FX2 on Grainbox with TYPE ORBIT and the other knobs as the author set them; transport stopped. The author pressed the keys; the agent read each frame over USB. imageSha256 is the MAIN image of base 0.8.5 ddb56b72, as recorded for Fuzzbox's, Glitchbox's and Loopbox's captures on the same base.", + "imageSha256": "2f7286525f0a4e84fc70fc6d48f8dab2171b6afd386be59a40a8b96eecfe3237" + } + }, + { + "path": "media/ot-controls.png", + "captureType": "hardware", + "credit": "jrs1717 (USB screen capture on the author's unit)", + "license": "LicenseRef-OT-UI-Documentation", + "source": "original", + "caption": "Press FX2 for the main page: TIME, ACT and SHAP on top, RPT, MIX and TAIL below.", + "alt": "FX2 main page titled Grainbox with knobs TIME, ACT, SHAP, RPT, MIX and TAIL.", + "otUi": { + "page": "FX2", + "shows": "controls", + "firmware": "1.40C", + "moduleVersion": "0.1.0-experimental", + "setup": "The author's Octatrack on its flashed Modwerk base ddb56b72, Grainbox 0.1.0-experimental (package 7ee24439) kept beside 27 modules; the author's project, pattern A01, a track's FX2 on Grainbox with TYPE ORBIT and the other knobs as the author set them; transport stopped. The author pressed the keys; the agent read each frame over USB. imageSha256 is the MAIN image of base 0.8.5 ddb56b72, as recorded for Fuzzbox's, Glitchbox's and Loopbox's captures on the same base.", + "imageSha256": "2f7286525f0a4e84fc70fc6d48f8dab2171b6afd386be59a40a8b96eecfe3237" + } + } + ], + "access": { + "location": "FX2 SETUP page of any audio track (the effect list).", + "steps": [ + "Select an audio track with its TRACK key.", + "Hold FUNC and press FX2 to open the effect's SETUP, turn LEVEL to GRAINBOX and press YES.", + "Press FX2 for TIME, ACT, SHAP, RPT, MIX and TAIL; hold FUNC and press FX2 again for TYPE (A), TONE (B), VAR (C), WIDE (D), BLUR (E) and FDBK (F)." + ], + "screenshots": [ + "media/ot-location.png", + "media/ot-controls.png" + ] + } +} diff --git a/sdk/drafts/grainbox/perf.py b/sdk/drafts/grainbox/perf.py new file mode 100644 index 0000000000..3f4f858278 --- /dev/null +++ b/sdk/drafts/grainbox/perf.py @@ -0,0 +1,123 @@ +#!/usr/bin/env python3 +"""Grainbox's performance measurements for evidence/performance.json, in dsp_host. + + python3 -B perf.py (the tools verify.py finds; several minutes) + python3 -B perf.py --stress (the stress run only) + +static the words of the per-sample loop, from the assembled symbols: everything between `do n7,>gbsl` and gbsl, + priced as sdk/octabam/tools/build/cycle_count.py prices straight-line code, one cycle a word. The loop has + no branch, no bsr, no nested do or rep (verify.py marks it CYCLES_FORWARD_BRANCHES), so every sample runs + every word. +measured executed instructions a sample from dsp_host's meter, one instance on verify.py's synthetic image: + the defaults, the dearest setting for each TYPE (TIME 1/64, ACT, SHAP, MIX, TAIL, TONE, WIDE, BLUR at 127, + FDBK 126 (the most still recording), RPT 8, VAR D) and a moving pattern (every slot swept by triangles, TYPE and VAR stepped, + everything jumped every 37 blocks), each unsplit and with a trig split at every position 1-15. +stress eight instances, four a core, each on its own stem (bursts, a full-scale square, a sweep, noise, + impulses, DC steps, silence, a chord), all twelve parameter words given a new value every block, + trig splits of 1, 3, 7, 8, 12 and 15 frames, under -guard 0x4000 -guard-shared and -dirty. dsp_host + hands every instance the same allocator entry, so the eight rings overlap here (on a unit each slot has + its own buffer); dsp_host writes a word a block into the instance at core 1 position 2 + (dsp_gate.HARNESS_WORD). +Then npm run perf:audit -- check evidence/performance.json --stamp (docs/MODULE_PERFORMANCE.md). +""" +import math +import pathlib +import random +import sys + +HERE = pathlib.Path(__file__).resolve().parent +sys.path.insert(0, str(HERE)) +import verify as V # noqa: E402 (finds sdk/octatrack/tools) + +G, R = V.G, V.R +Q = R.Q +BLOCKS = 4096 +SECONDS = 2 + + +def static(asm): + """The per-sample loop's words: from after `do n7,>gbsl` (two words) to gbsl.""" + syms = asm.syms + lines = G.listing(asm) + do = [addr for addr, line in lines.items() if line.mnemonic.lower() == "do" and "n7" in line.operands.lower() + and addr < syms["gbsl"]] + return syms["gbsl"] - (max(do) + 2) + + +def moving(blocks, seed=1): + """Every slot swept by triangles an eighth of a turn apart (TIME 0-5, RPT 0-7, TYPE 0-2, VAR 0-3 by their + steps), everything jumped at random every 37 blocks.""" + rnd = random.Random(seed) + rows = {} + for b in range(blocks): + tri = [min(127, 128 - abs(((b + 32 * k) % 256) - 128)) for k in range(12)] + row = list(tri) + row[0] = (b // 96) % 6 + row[3] = (b // 48) % 8 + row[6] = (b // 128) % 3 + row[8] = (b // 64) % 4 + if b % 37 == 0: + row = [rnd.randrange(128) for _ in range(12)] + row[0], row[3], row[6], row[8] = rnd.randrange(6), rnd.randrange(8), rnd.randrange(3), rnd.randrange(4) + rows[b] = tuple(row) + return rows + + +def dearest(t): + row = list(R.DEAREST) + row[R.TYPE_SLOT] = t + return tuple(row) + + +def stems(n, live): + rnd = random.Random(9) + on = lambda i, v: v if i < live else 0 # noqa: E731 + return [ + [on(i, round(0.9 * Q * (rnd.random() * 2 - 1) * math.exp(-(i % 11025) / 1500))) for i in range(n)], + [on(i, Q - 1 if (i // 110) % 2 else -Q) for i in range(n)], + [on(i, round(0.9 * Q * math.sin(2 * math.pi * (20 * (1000 ** (i / live))) * i / G.FS))) for i in range(n)], + [on(i, round(Q * (rnd.random() * 2 - 1))) for i in range(n)], + [on(i, Q - 1 if i % 4410 == 0 else 0) for i in range(n)], + [on(i, (Q - 1) if (i // 8820) % 2 else -Q) for i in range(n)], + [0] * n, + [on(i, round(0.3 * Q * sum(math.sin(2 * math.pi * f * i / G.FS) for f in (220, 277.2, 329.6)))) + for i in range(n)], + ] + + +def main(): + G.require_tools(HERE) + with G.workdir("grainbox-perf-") as tmp: + work = pathlib.Path(tmp) + asm = G.assemble(V.SOURCE, work) + if "--stress" not in sys.argv: + print(f"static: {static(asm)} cycles/sample (the per-sample loop's words)", flush=True) + sig = G.noise(16 * BLOCKS, 0.7, 5) + cases = {"defaults": dict(knobs=R.DEFAULTS), "moving": dict(rows=moving(BLOCKS))} + for t, name in enumerate(R.TYPE_NAMES): + cases[f"dearest {name}"] = dict(knobs=dearest(t)) + worst = (0.0, "") + for name, knobs in cases.items(): + found = {} + for split in range(16): + found[split] = G.run(asm, [G.Instance(sig, sig, **knobs)], split=split or None, tag="m").meter + if found[split] > worst[0]: + worst = (found[split], f"{name}, split {split}") + print(f"measured {name}: unsplit {found[0]:.2f}, worst with a split {max(found[s] for s in range(1, 16)):.2f}", + flush=True) + print(f"measured worst: {worst[0]:.2f} executed instructions a sample ({worst[1]})", flush=True) + + n = G.FS * SECONDS * 2 + insts = [G.Instance(s, s, rows=moving(n // 16, seed=k + 11), split=split, core=k // 4) + for k, (s, split) in enumerate(zip(stems(n, G.FS * SECONDS), (1, 3, 7, 8, 12, 15, 0, 0)))] + r = G.run(asm, insts, dirty=0x5a, guard="0x4000", extra=("-guard-shared",), check=False, tag="stress", timeout=3600) + print(f"stress: dsp_host exit {r.returncode}; meters {r.meters}; clobbers {sum(r.clobbers)}, stray {sum(r.stray)}") + for k, (core, pos) in enumerate(r.layout): + note = " (dsp_host's own word lands here)" if r.harness_word(k) else "" + print(f" instance {k}: core {core} pos {pos}, {r.per_instance[k]} a sample, {r.nonzero[k]} non-zero samples{note}") + if r.returncode: + print(r.stdout[-2000:]) + + +if __name__ == "__main__": + main() diff --git a/sdk/drafts/grainbox/qualification.example.json b/sdk/drafts/grainbox/qualification.example.json new file mode 100644 index 0000000000..f24ba06cd4 --- /dev/null +++ b/sdk/drafts/grainbox/qualification.example.json @@ -0,0 +1,84 @@ +{ + "moduleVersion": "0.1.0", + "sourceSha256": "REPLACE_WITH_NATIVE_SOURCE_SHA256", + "imageSha256": "REPLACE_WITH_LOCAL_TESTED_IMAGE_SHA256", + "cycles": [ + { + "processor": "dsp", + "worstCase": null, + "maxConfiguration": null, + "maxInstances": null, + "budget": null, + "unit": "cycles/sample", + "method": "emulator", + "conditions": { + "parameterExtremes": "Describe every minimum, maximum and branch boundary tested.", + "parameterModulation": "Describe simultaneous LFOs, p-locks, MIDI CC and scene sweeps; justify each inapplicable mechanism.", + "modeSwitching": "Describe repeated/interrupted mode, bypass and Part changes.", + "maxLoad": "Describe maximum voices/instances, both cores, FX slots, streaming and USB/MIDI load.", + "inputConditions": "Describe silence, impulses, full-scale inputs, feedback and other worst-case signal conditions." + }, + "report": "TESTING.md" + } + ], + "memory": { + "regions": [ + { + "name": "Replace with each code, state, table, buffer, stack/heap and padding allocation", + "space": "dsp-p", + "words": null, + "wordBits": 24, + "bytes": null, + "scope": "shared" + } + ], + "perInstanceBytes": null, + "sharedBytes": null, + "maxInstances": null, + "totalBytes": null, + "conditions": "State exact build options, alignment, accounting convention, allocator/stack bounds and reused stock/shared memory; justify every absent allocation class.", + "report": "TESTING.md" + }, + "hardware": { + "status": "pending", + "model": "MKII", + "testedOn": "YYYY-MM-DD", + "tester": "Your credited tester handle; no email", + "project": { + "name": "Your local stress project", + "sha256": "REPLACE_WITH_PROJECT_FINGERPRINT", + "recipe": "Record generator/tool revision and command, local source template fingerprint, sample recipe, all settings and modifications needed to reproduce this stress project. Keep the project/card/firmware local." + }, + "durationMinutes": null, + "audioTracks": 8, + "midiTracks": null, + "conditions": { + "parameterExtremes": "Describe every minimum, maximum and branch boundary tested.", + "parameterModulation": "Describe simultaneous LFOs, p-locks, MIDI CC and scene sweeps; justify each inapplicable mechanism.", + "modeSwitching": "Describe repeated/interrupted mode, bypass and Part changes.", + "maxLoad": "Describe maximum voices/instances, both cores, FX slots, streaming and USB/MIDI load.", + "inputConditions": "Describe silence, impulses, full-scale inputs, feedback and other worst-case signal conditions." + }, + "checks": { + "audioContinuity": "failed", + "transport": "failed", + "controls": "failed", + "memoryIntegrity": "failed", + "recovery": "failed" + }, + "report": "TESTING.md", + "maxInstances": null + }, + "documentation": { + "tutorial": { + "title": "Quick tutorial", + "steps": [ + "Explain setup and exactly where to select or enable the module.", + "Demonstrate the main controls with a useful example setting.", + "Explain what to hear/observe and how to stop, reset or bypass." + ] + }, + "screenshots": [], + "screenshotStyle": "black-and-white" + } +} diff --git a/sdk/drafts/grainbox/reference.py b/sdk/drafts/grainbox/reference.py new file mode 100644 index 0000000000..88f9061516 --- /dev/null +++ b/sdk/drafts/grainbox/reference.py @@ -0,0 +1,581 @@ +"""Grainbox's reference, imported by verify.py and test_reference.py. + +Model is engine.asm instruction by instruction, on 24-bit words and a 56-bit accumulator, so dsp_host's render +must match it to the bit (verify.py). The design is original: a capture ring in the FX2 slot's buffer, a slice +clock at the project tempo, and at each slice a decision (ACT) to play the recent audio as grains with three read +heads: CLOUD (a wash of grains from a spray of recent audio), ORBIT (grains from a window that sweeps slowly back +and forth, for drones) or SPARK (grains set off together as rhythmic onsets on the grid, each a pluck that decays), +each with variations A-D. A grain is a window of SHAP's length with a full fade in and out; every call a new piece +of recent audio becomes the newest window, and each head takes it as it wraps, so every grain comes from somewhere +else. No product's code, tables or names. +""" +import math + +FS = 44100.0 +Q = 1 << 23 + +KNOBS = ("TIME", "ACT", "SHAP", "RPT", "MIX", "TAIL", "TYPE", "TONE", "VAR", "WIDE", "BLUR", "FDBK") +TYPE_SLOT, VAR_SLOT = 6, 8 +DEFAULTS = (2, 64, 32, 3, 64, 64, 0, 127, 0, 0, 0, 0) +DEAREST = (0, 127, 127, 7, 127, 127, 2, 127, 3, 127, 127, 126) # FDBK 126: the most feedback still recording (127 freezes) +TYPE_NAMES = ("CLOUD", "ORBIT", "SPARK") +VAR_NAMES = ("A", "B", "C", "D") +TIME_LABELS = ("1/64", "1/32", "1/16", "1/8", "1/4", "1/2") +CLOUD, ORBIT, SPARK = 0, 1, 2 + +BUFFER = 15870 # the FX2 slot's buffer, ending at +$3DFD +R = 14848 # the ring: the buffer's first 14848 words (29 x 512, so the diffuser's lines below align) +DIFF = ((0, 467), (512, 359)) # BLUR's all-pass lines after the ring: (offset from R, length) +AP = 0x500000 # their coefficient, 0.625 +FRESH_MAX = R - 16 # captured samples counted up to this +LMAX = 7400 # a chunk: a slice longer than this plays as 2, 4, 8, 16 or 32 chunks that fit (two and a margin in the ring) +# 2 x samples-per-note x tempo24 for each TIME (samples a beat = 63,504,000 / tempo24): (high word, low word) +TIME_DIVIDENDS = ((0, 0x791fd0), (0, 0xf23fa0), (1, 0xe47f40), (3, 0xc8fe80), (7, 0x91fd00), (15, 0x23fa00)) +ONE = 0x200000 # a rate of 1 (Q21) +T0 = (0x080000, 0x100000, 0x400000, 0x600000) # speeds: 0.25, 0.5, 2, 3 (Q21) +SPARK_A = (0x200000, 0x285146, 0x2ff222, 0x400000) # SPARK's steps (Q21): A a major arpeggio up +SPARK_B = (0x100000, 0x200000, 0x400000, 0x200000) # B octaves: down, home, up, home +SPARK_C = (0x200000, 0x300000, 0x400000, 0x600000) # C fifths stacked: 1, 1.5, 2, 3 +# ORBIT's second and third heads for VAR A-C (Q21): a fifth and an octave up; an octave down and up; an octave and a fourth down +ORBITS = (0x300000, 0x400000, 0x100000, 0x400000, 0x100000, 0x180000) +ORBIT_SWEEP = (3, 1, 2, 4) # ORBIT's sweep, back and forth once in 2^n chunks, for VAR A-D +GRAIN_MIN, GRAIN_RANGE = 441, 5292 # SHAP: a grain of 10 ms at 0 to 130 ms at 127 +SLOW = 0x1e0000 # a head under 15/16 speed plays its grain longer than the grain: it needs three grains of ring +QSPEED = 0x0c0000 # a head under 3/8 speed (quarter speed) plays its grain four times as long: five grains of ring +CAP_FAST, CAP_SLOW, CAP_QUARTER = 5920, 3946, 2368 # the grain's most, so the spray keeps an eighth of the ring: (R - MARGIN - R/8) / 2, 3, 5 +GLIDE = 16 # SHAP's grain moves toward its value at most this many samples a call (a turn or a lock never clicks) +DEEP = (0x200000, 0x100000, 0x155b81) # CLOUD D, deep: 1, an octave down, a fifth down (Q21) +DETUNE = 0x011d26 # a grain's detune: up to 15 cents either way (2^(15/1200) - 1, Q23) +FEED = 0x39999a # FDBK's most: 0.45 of the grains back into the ring (at 127 it freezes the ring instead) +TONE_MIN, TONE_RANGE = 0x040000, 0x7bffff # TONE's low-pass coefficient: 0.03 + 0.97 t^2 (Q23) +SPRAY = 1856 # CLOUD's spray: RPT eighths of the ring +BASES = (0x12, 0x2d, 0x3e) # the heads' rates before detune (CLOUD, ORBIT) +PREV, PANS, SIDE0 = (0x7b, 0x7c, 0x7d), (0x7e, 0x7f, 0x80), 0x81 # per grain: the window a head last had, its pan; head 0's side gain +REVERSE = 0xe00000 # -1 (Q21) +ACT_SCALE = 0x5a0000 # ACT 127 plays about 70 % of the slices +THIRD = 0x2aaaab # 1/3: D = MIX + MIX/3, whole at MIX 96 +TAIL_MIN, TAIL_RANGE = 0x1b6, 0x64ea # the tail's decay a sample: about 3 s to -60 dB at TAIL 127, 50 ms at 0 +TAIL_END = 0x2000 # -72 dB: the tail is over +SILENT, SILENT_STOP, SILENT_MAX = 44100, 176400, 0x100000 # the silence watch: no new loop, then nothing +FADE_MIN = 64 # a head's fade is at least 64 samples, or half its window if that is shorter, and never under 5: +FADE_FLOOR = 5 # K = 2^25 / F stays a word; F >= W / 64 keeps min(p, W - p) x K x 2^22 a 56-bit sum +NEAR = 64 # a head whose window the writer comes this close to takes the newest window at once +ZONE = NEAR + 1024 # ... after fading out from here, over 64 blocks +MARGIN = 1152 # the spray stops short of the ring's far end by three grains (read at 3x) and this +HALF, QUARTER = 0x400000, 0x200000 +SEED = 0x123456 +LFSR_TAPS = 0xe10000 + + +def notes(): + """The constants' formulas, for test_reference.py.""" + return { + "dividends": [2 * round(63504000 / d) for d in (16, 8, 4, 2, 1, 0.5)], + "T0": [round(r * (1 << 21)) for r in (0.25, 0.5, 2, 3)], + "SPARK_A": [round(2 ** (s / 12) * (1 << 21)) for s in (0, 4, 7, 12)], + "ORBITS": [round(r * (1 << 21)) for r in (1.5, 2, 0.5, 2, 0.5, 0.75)], + } + + +# ---- the fixed-point model ------------------------------------------------- + +def s24(v): + v &= 0xffffff + return v - (1 << 24) if v & 0x800000 else v + + +def w56(a): + a &= (1 << 56) - 1 + return a - (1 << 56) if a >> 55 else a + + +def lim(a): + return max(-0x800000, min(0x7fffff, a >> 24)) + + +def hi(a): + return s24(a >> 24) + + +def ld(x): + return s24(x) << 24 + + +def mac(a, x, y): + return w56(a + 2 * s24(x) * s24(y)) + + +def mpy(x, y): + return mac(0, x, y) + + +def div(n, d): + """The 24-step div routine: floor(N / 2D), N the 48-bit dividend (effects.md, Traps).""" + return n // (2 * d) + + +def stepped(word, top): + return min((word & 0x7f0000) >> 16, top) + + +# Each head's words: window start (Y), position high and low, rate, gain, and the scratch word its faded read goes +# to. Every head wraps at the newest window's length (PW). +HEADS = ((0x0b, 0x0d, 0x0e, 0x0f, 0x6b, 0x55), + (0x78, 0x60, 0x61, 0x62, 0x64, 0x56), + (0x7a, 0x65, 0x66, 0x67, 0x69, 0x57)) +PHASE, SWEEP = 0x6c, 0x35 # ORBIT's sweep: its phase (Q23) and its step a sample +PS, PW = 0x10, 0x11 # the newest window: start (Y) and W, which every head takes at its next wrap + + +class Model: + """One instance of engine.asm on a buffer at `base`. call(frames, p) runs one proc on [(left, right)] with the + parameter block p (offset -> word) and returns [(left, right)].""" + + def __init__(self, base=0x4000, ring=None): + self.x = [0] * 0x82 + self.ring = ring if ring is not None else [0] * R + self.lines = [[0] * n for _o, n in DIFF] # BLUR's all-pass lines (cleared by init) + self.taps = [0, 0] + x = self.x + x[0x00] = x[0x0b] = x[0x78] = x[0x7a] = x[PS] = base + x[0x01] = 1 if ld(base) - ld(0x4000) >= 0 else 0 + x[0x03] = 1 + x[0x05] = SEED + x[0x24:0x28] = list(T0) + x[0x28:0x2c] = list(SPARK_A) + x[0x36:0x3c] = [s24(v) for v in ORBITS] + x[0x6f:0x73] = list(SPARK_B) + x[0x73:0x77] = list(SPARK_C) + + def rand(self): + x = self.x + v = x[0x05] & 0xffffff + carry = v & 1 + v >>= 1 + if carry: + v ^= LFSR_TAPS + x[0x05] = s24(v) + return v & 0x7fffff + + def distance(self, start): + """How far the writer has to go to reach a window starting at start (Y).""" + x = self.x + d = (start - x[0x00]) - x[0x02] + if d < 0: + d += R + return d + + def call(self, frames, p): + x = self.x + if x[0x01] == 0: + return list(frames) + x[0x22] = stepped(p[0x0], 5) + a1 = p[0x1] & 0x7fffff + x[0x1f] = 0x7fffff if a1 >= 0x7f0000 else lim(mpy(a1, ACT_SCALE)) # ACT 127: every slice + x[0x33] = HALF # a grain fades over all of itself: in to its middle, out to its end + x[0x21] = stepped(p[0x3], 7) + 1 + t5 = p[0x5] & 0x7fffff + s = lim(ld(0x7fffff) - ld(t5)) + x[0x32] = lim(mpy(lim(mpy(s, lim(mpy(s, s)))), TAIL_RANGE) + ld(TAIL_MIN)) + m = p[0x4] & 0x7fffff + x[0x1a] = 0x7fffff if m >= 0x7f0000 else m + x[0x1d] = stepped(p[0xc], 2) + x[0x1e] = stepped(p[0xd], 3) + x[0x51] = (p[0xc] & 0x00ff00) << 8 # TONE, slot 7 + x[0x46] = 1 if (p[0xe] & 0x00ff00) == 0x7f00 else 0 # FDBK at 127: freeze, nothing new recorded + x[0x4e] = (p[0xd] & 0x00ff00) << 8 # WIDE, slot 9 + x[0x4f] = p[0xe] & 0x7f0000 # BLUR, slot 10: the diffused share + x[0x50] = lim(mpy((p[0xe] & 0x00ff00) << 8, FEED)) # FDBK, slot 11: the grains back into the ring + t = s24(p.get(0x13, 0)) >> 8 + t = s24(t) + if t < 0x2d0: + t = 0xb40 + if t > 0x1c20: + t = 0x1c20 + x[0x23] = t + h, l = TIME_DIVIDENDS[x[0x22]] + length = div((h << 24) | l, t) + chunks = 1 + for _ in range(5): # up to 32 chunks: a 1/2 at 30 BPM still fits + if length > LMAX: + length >>= 1 + chunks <<= 1 + x[0x08] = length # a chunk + x[0x40] = chunks # chunks in a slice of TIME + if x[0x2c] == 0: + x[0x1b] = x[0x1a] + x[0x1c] = 0x7fffff + x[0x19] = x[0x08] + x[0x2c] = 1 + if abs(ld(x[0x1a]) - ld(x[0x1b])) < ld(64): + x[0x1b] = x[0x1a] + if x[0x18]: + if x[0x15] < TAIL_END: + self.quiet() + elif x[0x03]: # the writer kept off every window still sounding by a freeze + near = self.distance(x[PS]) <= 32 + for st, _ph, _pl, _rt, gn, _t in HEADS: + if x[gn] != 0 and self.distance(x[st]) <= 32: + near = True + if near: + x[0x03] = 0 + if x[0x03] == 0 and x[0x06] == 0 and x[0x18] == 0: + x[0x03] = 1 + x[0x04] = 0 + if x[0x53] >= SILENT_STOP and not x[0x46]: # four seconds of silence in: no loop, no tail, record again (not frozen) + self.quiet() + x[0x06] = x[0x04] = 0 + x[0x03] = 1 + gt = GRAIN_MIN + hi(mpy(p[0x2] & 0x7fffff, GRAIN_RANGE)) # SHAP: the grain's length + rmin = min(abs(x[rt]) for rt in (0x0f, 0x62, 0x67)) # the slowest head (last call's rates) + k, cap = (5, CAP_QUARTER) if rmin < QSPEED else (3, CAP_SLOW) if rmin < SLOW else (2, CAP_FAST) + if gt > cap: + gt = cap + g = x[0x16] + if g == 0: # the first call: at once + g = gt + elif gt > g + GLIDE: + g += GLIDE + elif gt < g - GLIDE: + g -= GLIDE + else: + g = gt + if g > cap: # a slower head: the cap applies at once, the ring must hold it + g = cap + x[0x16] = g + x[0x5b] = k * g # the ring a window needs ahead of the writer, past the reach + x[PW] = g - 1 + self.fade(x[PW]) + if x[0x07] >= x[0x19]: + self.event() + n = len(frames) + self.pick(n) + if x[0x1d] != SPARK: # one head a call detuned at random around its rate: grains blur + h = x[0x63] + x[0x63] = 0 if h == 2 else h + 1 + j = hi(mpy(lim(w56((ld(self.rand()) - ld(0x400000)) << 1)), DETUNE)) + base = x[BASES[h]] + x[HEADS[h][3]] = hi(mpy(base, j) + ld(base)) + for h, (st, ph, pl, _rt, gn, _t) in enumerate(HEADS): + # each head's gain an eighth of the way to its target a call: TYPE's, or nothing while the writer nears a + # window it still plays (then, close enough, it takes the newest window, faded out by then) + tgt = self.target(h) + if x[st] != x[PS]: + d = self.distance(x[st]) + if d <= ZONE: + tgt = 0 + if x[0x06] and d <= NEAR: + x[st], x[ph], x[pl] = x[PS], 0, 0 + x[gn] = hi(ld(x[gn]) + ((ld(tgt) - ld(x[gn])) >> 3)) + if x[0x1d] == SPARK: # the side: none for SPARK (its onsets ping-pong) + x[SIDE0] = x[0x3c] = x[0x3d] = 0 + else: # CLOUD, ORBIT: each grain a new place within WIDE, at the gains now + for h, (st, *_r) in enumerate(HEADS): + if x[st] != x[PREV[h]]: + x[PREV[h]] = x[st] + x[PANS[h]] = lim(mpy(lim(w56((ld(self.rand()) - ld(0x400000)) << 1)), x[0x4e])) + narrow = lim(ld(0x7fffff) - ld(x[0x4f] >> 1)) # BLUR's diffusion is mono: the side narrows to half at 127 + x[SIDE0] = lim(mpy(lim(mpy(x[0x6b], x[PANS[0]])), narrow)) + x[0x3c] = lim(mpy(lim(mpy(x[0x64], x[PANS[1]])), narrow)) + x[0x3d] = lim(mpy(lim(mpy(x[0x69], x[PANS[2]])), narrow)) + # P: 1 while a loop plays, else 0 (a tail echoes over the whole track) + x[0x3f] = 0x7fffff if x[0x06] else 0 + # TONE: the grains' low-pass coefficient, 0.03 (about 200 Hz) at 0 to whole at 127, on a squared curve + t = x[0x51] + x[0x4a] = hi(mpy(lim(mpy(t, t)), TONE_RANGE) + ld(TONE_MIN)) + x[0x47] = 0 if x[0x46] else x[0x03] # the writer's step: none while frozen + out = [self.sample(left, right) for left, right in frames] + x[0x07] += n + x[0x53] = min(x[0x53] + n, SILENT_MAX) + if x[0x54] >= 64: + x[0x53] = 0 + x[0x54] = 0 + if x[0x03]: + x[0x04] = min(x[0x04] + n, FRESH_MAX) + return out + + def quiet(self): + """Nothing sounding: the level, its target and rate, and the tail's flag at rest.""" + x = self.x + x[0x18] = x[0x15] = x[0x20] = x[0x17] = 0 + + def event(self): + x = self.x + x[0x07] -= x[0x19] # what is left is how late this event is + if x[0x07] >= x[0x19]: + x[0x07] = 0 + if x[0x06]: + x[0x09] -= 1 + if x[0x09] != 0: + x[0x0a] += 1 + self.setrep() + return + self.chunk() # a chunk is over + x[0x42] -= 1 + if x[0x42] > 0: # the loop goes on through its slice of TIME + self.start() + return + x[0x06] = 0 # the loop is over: its heads loop on into the tail, steady in pitch + x[0x18] = 1 + x[0x20] = 0 + x[0x17] = x[0x32] + x[0x63] = x[0x68] = 0 + else: + self.chunk() + x[0x19] = x[0x08] + if x[0x41] != 0: # decided only on TIME's grid + return + if x[0x53] >= SILENT and not x[0x46]: # and not after a second of silence in (frozen: the held ring plays on) + return + if x[0x04] < x[0x08]: + return + r = self.rand() + if x[0x1f] - r <= 0 and not x[0x46]: # frozen: every slice, an endless pad + return + if x[0x03] == 0: # a tail froze the ring: end it, record again, wait for a fresh chunk + self.quiet() + x[0x04] = 0 + x[0x03] = 1 + return + x[0x42] = x[0x40] + self.start() + + def chunk(self): + x = self.x + x[0x41] += 1 + if x[0x41] >= x[0x40]: + x[0x41] = 0 + + def fade(self, w): + """The heads' fade for a window of w + 1: F = (w + 1) x SHAP's share, at least 64, at most half the window (so a + short window still reaches full level), never under 5; K = 2^25 / F.""" + x = self.x + f = hi(mpy(w + 1, x[0x33])) + if f < FADE_MIN: + f = FADE_MIN + h = (w + 1) >> 1 + if f > h: + f = h + if f < FADE_FLOOR: + f = FADE_FLOOR + x[0x13] = div(4 << 24, f) + + def target(self, h): + """Head h's gain for TYPE: CLOUD a third each; ORBIT and SPARK 1/2, 1/4, 1/4.""" + if self.x[0x1d] == CLOUD: + return THIRD + return (HALF, QUARTER, QUARTER)[h] + + def speed(self): + """A random index into T0.""" + return hi(mpy(self.rand(), 4)) + + def pick(self, n): + """The newest window, a call's grain: somewhere in the recent audio, at least a grain and 64 samples behind the + writer. CLOUD sprays it at random over RPT eighths of the ring, SPARK over two grains; ORBIT sweeps it back and + forth over RPT grains, with an eighth of them at random. The spray stops three grains and MARGIN short of the + ring's far end.""" + x = self.x + g = x[0x16] + if x[0x1d] == CLOUD: + reach = x[0x21] * SPRAY + elif x[0x1d] == ORBIT: + reach = x[0x21] * g + else: + reach = 2 * g + top = R - x[0x5b] - MARGIN + if reach > top: + reach = top + if x[0x1d] == ORBIT: + x[PHASE] = (x[PHASE] + n * x[SWEEP]) & 0x7fffff + ph = x[PHASE] + tri = ph << 1 if ph < HALF else (0x7fffff - ph) << 1 + j = hi(mpy(tri, reach - (reach >> 3))) + hi(mpy(self.rand(), reach >> 3)) + else: + j = hi(mpy(self.rand(), reach)) + s = x[0x02] - (g + 64 + j) + if s < 0: + s += R + x[0x6e] = s + x[PS] = x[0x00] + s + + def start(self): + """A chunk of the texture starts. One starting from silence sets every head on the newest window at once; one + starting while heads still sound (the next chunk, or over a tail) leaves each head to take it at its next wrap.""" + x = self.x + fresh = x[0x06] == 0 and x[0x18] == 0 + late = x[0x07] + x[0x06] = x[0x03] = 1 # it always records: its windows lie behind the writer + x[0x0a] = x[0x18] = 0 + x[0x20] = 0x7fffff + x[0x17] = x[0x32] # it swells in at TAIL's rate, as it will fade (SPARK plucks at once below) + kind, var = x[0x1d], x[0x1e] + x[0x45] = 1 # SPARK: RPT onsets a chunk + if kind == SPARK: + x[0x45] = x[0x21] + x[0x34] = x[0x08] + x[0x6d] = div(2 * x[0x08], x[0x45]) + x[0x0f] = x[0x62] = x[0x67] = ONE + if kind == CLOUD: + if var == 1: + x[0x62] = x[0x67] = 0x400000 # B: two heads an octave up, a shimmer + elif var == 2: + x[0x62] = s24(REVERSE) # C: one head reversed + elif var == 3: + x[0x62], x[0x67] = DEEP[1], DEEP[2] # D: deep, an octave and a fifth down + elif kind == ORBIT: + if var == 3: + x[0x62] = x[0x24 + self.speed()] + x[0x67] = x[0x24 + self.speed()] + else: + x[0x62] = x[0x36 + 2 * var] + x[0x67] = x[0x37 + 2 * var] + x[SWEEP] = div(1 << 24, x[0x34] << ORBIT_SWEEP[var]) + x[0x12], x[0x2d], x[0x3e] = x[0x0f], x[0x62], x[0x67] # the rates before detune + if fresh: # from silence: every gain at TYPE's at once + x[0x6b], x[0x64], x[0x69] = self.target(0), self.target(1), self.target(2) + if fresh and kind != SPARK: # from silence: every head on the newest window now, on time, heads 1 and 2 + for h, (st, ph, pl, rt, _gn, _t) in enumerate(HEADS): # a third and two thirds into it (so they wrap apart) + x[st], x[pl] = x[PS], 0 + x[ph] = late + (0, hi(mpy(x[PW], THIRD)), hi(mpy(x[PW], 0x555555)))[h] + if x[rt] < 0: + x[ph] = x[PW] - 1 - late + x[0x09] = x[0x45] + self.setrep() + + def setrep(self): + """An event within the chunk: SPARK's next onset, on time (every head on the newest window at its start, at + the onset's step, the level whole and decaying at TAIL's rate), its ping-pong; or the chunk's one event.""" + x = self.x + k = x[0x0a] + if x[0x1d] == SPARK: + var = x[0x1e] + if var == 3: + r = x[0x24 + self.speed()] + else: + r = x[(0x28, 0x6f, 0x73)[var] + (k & 3)] + for st, ph, pl, rt, _gn, _t in HEADS: + x[st], x[ph], x[pl] = x[PS], x[0x07], 0 + x[rt] = r + x[0x62] = lim(-ld(r)) # the second reversed, from the grain's end + x[0x60] = x[PW] - 1 - x[0x07] + x[0x67] = r >> 1 # the third an octave down + x[0x15] = 0x7fffff + x[0x20] = 0 + x[0x17] = x[0x32] + p = x[0x4e] if k & 1 == 0 else lim(-ld(x[0x4e])) # WIDE: onsets ping-pong + else: # CLOUD and ORBIT: no ping-pong (WIDE spreads heads 1 and 2, in the call) + p = 0 + per = x[0x6d] + x[0x19] = per + if x[0x09] == 1: # the chunk's last event takes what is left of it: the texture keeps the grid + x[0x19] = x[0x34] - (x[0x45] - 1) * per + x[0x52] = p + a = ld(0x7fffff) - ld(p) + if a - ld(0x7fffff) >= 0: + a = ld(0x7fffff) + b = ld(0x7fffff) + ld(p) + if b - ld(0x7fffff) >= 0: + b = ld(0x7fffff) + x[0x4d], x[0x4c] = lim(a), lim(b) + + def head(self, st, ph, pl, rt, gn, out): + """One read head: its position wraps inside the newest window's length at its rate, and at a wrap it takes the + newest window, where its fade is nothing; faded by how far it is from the window's ends; an interpolated read; + the faded read to its scratch word.""" + x, ring = self.x, self.ring + a = ld(x[ph]) + (x[pl] & 0xffffff) + a = w56(a + (ld(x[rt]) >> 21)) + w = x[PW] + fwd = ld(w) if a - ld(w) >= 0 else 0 + a -= fwd + back = ld(w) if a < 0 else 0 + a += back + if a - ld(w) >= 0: # still past the end (the window shrank under it): its start + a = 0 + if fwd + back != 0: + x[st] = x[PS] + x[ph], x[pl] = hi(a), a & 0xffffff + fh = (a & 0xffffff) >> 1 + b = ld(w) - a # the fade: min(p, W - p) x K + m = b if a - b > 0 else a + e = lim(w56(mpy(hi(m), x[0x13]) << 22)) + e = lim(mpy(e, e)) # squared: a grain starts and ends without a corner + i = (x[st] - x[0x00] + hi(a)) % R + s0, s1 = ring[i], ring[(i + 1) % R] + v = hi(mac(ld(s0), lim(ld(s1) - ld(s0)), fh)) + x[out] = lim(mpy(e, v)) + + def sample(self, left, right): + x, ring = self.x, self.ring + x[0x1b] = hi(((ld(x[0x1a]) - ld(x[0x1b])) >> 6) + ld(x[0x1b])) + m = x[0x1b] + x[0x43] = lim(mpy(m, THIRD) + ld(m)) # D: the loop's gain, whole at MIX 96 + x[0x44] = lim(w56((ld(0x7fffff) - ld(m)) << 2)) # K: the dry's ceiling, none at MIX 127 + x[0x2e], x[0x2f] = left, right + u = lim((ld(left) + ld(right)) >> 2) + x[0x54] = max(x[0x54], abs(u)) # the input's mono word: the silence watch hears the track, not the feedback + ring[x[0x02]] = lim(ld(u) + mpy(x[0x31], x[0x50])) # with FDBK's share of the last sample's grains + x[0x02] = (x[0x02] + x[0x47]) % R + for h in HEADS: + self.head(*h) + acc = mac(mac(mpy(x[0x55], x[0x6b]), x[0x56], x[0x64]), x[0x57], x[0x69]) + y = x[0x49] # TONE: a one-pole low-pass on the grains' sum (before the feedback point) + x[0x49] = hi(mpy(lim(acc - ld(y)), x[0x4a]) + ld(y)) + g = x[0x15] # the loop's level + # the dry ducks under the loop as its level rises + a = ld(0x7fffff) - ld(lim(mpy(lim(mpy(x[0x3f], g)), x[0x43]))) + if a - ld(x[0x44]) > 0: + a = ld(x[0x44]) + x[0x1c] = lim(a) + # BLUR: two all-passes in series (w = in + g z, out = z - g w), then BLUR's share of them + t = x[0x49] + a = ld(t) + for i, line in enumerate(self.lines): + z = line[self.taps[i]] + a = a + mpy(z, AP) + wv = lim(a) + line[self.taps[i]] = wv + self.taps[i] = (self.taps[i] + 1) % len(line) + a = ld(z) - mpy(wv, AP) + d = lim(a) + x[0x48] = hi(mpy(lim(ld(d) - ld(t)), x[0x4f]) + ld(t)) + sd = mac(mac(mpy(x[0x55], x[SIDE0]), x[0x56], x[0x3c]), x[0x57], x[0x3d]) # the side: each head at its place + x[0x5a] = hi(mpy(lim(sd - ld(x[0x5a])), x[0x4a]) + ld(x[0x5a])) # through TONE's low-pass, as the grains are + x[0x77] = x[0x5a] + w = lim(w56(ld(x[0x48]) << 1)) # x 2: half the mono sum, level with a mono sample + wet = lim(mpy(w, g)) + x[0x31] = wet + wd = lim(mpy(wet, x[0x43])) + sg = lim(w56(mpy(lim(mpy(x[0x77], g)), x[0x43]) << 1)) + x[0x15] = hi(mpy(lim(ld(x[0x20]) - ld(g)), x[0x17]) + ld(g)) # the level toward its target + return (lim(mac(mpy(wd, x[0x4c]), left, x[0x1c]) + ld(sg)), + lim(mac(mpy(wd, x[0x4d]), right, x[0x1c]) - ld(sg))) + + +def pblock(values, raw=None): + """dsp_host's parameter block for slot values (0-127) and raw words {offset: word} written after.""" + v = list(values) + [0] * (12 - len(values)) + p = {i: (v[i] & 0x7f) << 16 for i in range(6)} + for w in range(3): + p[0xc + w] = ((v[6 + 2 * w] & 0x7f) << 16) | ((v[7 + 2 * w] & 0x7f) << 8) + p.update(raw or {}) + return p + + +def render(left, right, knob_rows, frames=16, split=0, raw=None, base=0x4000): + m = Model(base) + out_l, out_r = [], [] + cur = knob_rows.get(0, DEFAULTS) + for blk in range((len(left) + frames - 1) // frames): + cur = knob_rows.get(blk, cur) + p = pblock(cur, raw) + lo = blk * frames + fr = list(zip(left[lo:lo + frames], right[lo:lo + frames])) + for part in ([fr[:split], fr[split:]] if split else [fr]): + if not part: + continue + for l, r in m.call(part, p): + out_l.append(l) + out_r.append(r) + return out_l, out_r, m diff --git a/sdk/drafts/grainbox/resource-impact.example.json b/sdk/drafts/grainbox/resource-impact.example.json new file mode 100644 index 0000000000..8f7457a054 --- /dev/null +++ b/sdk/drafts/grainbox/resource-impact.example.json @@ -0,0 +1,21 @@ +{ + "conditions": "Describe the active workload, expensive modes, changing controls, instance count, and whether shared/stock allocations are included.", + "cpu": { + "level": null, + "basis": "source-estimate", + "rationale": "Choose minimal, low, moderate or high from source inspection or a documented workload comparison. Explain the ColdFire work and cite the record; do not invent a utilization percentage.", + "source": "TESTING.md" + }, + "dsp": { + "level": null, + "basis": "source-estimate", + "rationale": "Choose a relative demand tier covering the expensive DSP modes and modulation. Minimal needs an explanation of the small or absent added DSP path.", + "source": "TESTING.md" + }, + "memory": { + "level": null, + "basis": "source-estimate", + "rationale": "Choose a relative footprint tier covering code, state, tables and buffers. Distinguish shared reservations and reused stock buffers from extra per-instance memory.", + "source": "TESTING.md" + } +} diff --git a/sdk/drafts/grainbox/test_reference.py b/sdk/drafts/grainbox/test_reference.py new file mode 100644 index 0000000000..4d64d553dc --- /dev/null +++ b/sdk/drafts/grainbox/test_reference.py @@ -0,0 +1,457 @@ +#!/usr/bin/env python3 +"""Grainbox's host test: no DSP tools, no firmware. reference.py's constants against their formulas, and the +model's behaviour: textures happen as often as ACT asks, every TYPE and VAR changes the sound, grains come from +somewhere new each time, silence stays silent, ACT 0 and MIX 0 leave the track alone. verify.py holds engine.asm to the same model +in dsp_host. + + python3 -B test_reference.py +""" +import pathlib +import random +import re +import sys +import unittest + +HERE = pathlib.Path(__file__).resolve().parent +sys.path.insert(0, str(HERE)) +import reference as R # noqa: E402 + +N = 16 * 2500 +FS = 44100 + + +def noise(seed, n=N, amp=0.5): + rnd = random.Random(seed) + return [round(amp * (rnd.random() * 2 - 1) * (R.Q - 1)) for _ in range(n)] + + +def knobs(**kw): + row = list(R.DEFAULTS) + names = {"time": 0, "act": 1, "shap": 2, "rpt": 3, "mix": 4, "tail": 5, "type": 6, "tone": 7, "var": 8, + "wide": 9, "blur": 10, "fdbk": 11} + for name, value in kw.items(): + row[names[name]] = value + return tuple(row) + + +def tempo(bpm): + return {0x13: (bpm * 24) << 8} + + +class Constants(unittest.TestCase): + def test_constants_round_their_formulas(self): + n = R.notes() + self.assertEqual([(h << 24) | l for h, l in R.TIME_DIVIDENDS], n["dividends"]) + self.assertEqual(list(R.T0), n["T0"]) + self.assertEqual(list(R.SPARK_A), n["SPARK_A"]) + self.assertEqual(list(R.ORBITS), n["ORBITS"]) + + def test_engine_carries_the_same_hex(self): + text = (HERE / "engine.asm").read_text().lower() + for w in [l for _, l in R.TIME_DIVIDENDS] + list(R.T0) + list(R.ORBITS) + list(R.SPARK_A) + list(R.SPARK_B) + list(R.SPARK_C) + [ + R.REVERSE, R.DETUNE, R.FEED, R.SEED, R.LFSR_TAPS, R.ACT_SCALE, R.TAIL_MIN, + R.TAIL_RANGE, R.TAIL_END, R.THIRD]: + self.assertIn(f"#>${w:06x}", text) + + def test_the_loop_and_a_chunk_fit_the_ring(self): + self.assertLessEqual(2 * R.LMAX, R.FRESH_MAX) + + def test_labels(self): + labels = [l.lower() for l in re.findall(r"^([A-Za-z_][A-Za-z0-9_]*):", (HERE / "engine.asm").read_text(), re.M)] + self.assertFalse([(a, b) for a in labels for b in labels if a != b and b.startswith(a)]) + self.assertEqual(len(labels), len(set(labels))) + + +class Behaviour(unittest.TestCase): + def test_slice_lengths_follow_the_tempo(self): + for bpm, time, want in ((120, 2, 5512), (120, 0, 1378), (90, 2, 7350), (60, 3, 5512)): + m = R.Model() + m.call([(0, 0)], R.pblock(knobs(time=time), tempo(bpm))) + self.assertEqual(m.x[0x08], want, (bpm, time)) + + def test_the_slowest_half_note_fits_the_ring(self): + m = R.Model() + m.call([(0, 0)], R.pblock(knobs(time=5), tempo(30))) + self.assertLessEqual(m.x[0x08], R.LMAX) + self.assertEqual(m.x[0x40], 32) + + def test_mix_zero_and_act_zero_leave_the_track(self): + x = noise(1) + for row in (knobs(act=0), knobs(act=127, mix=0, tail=0)): + out = R.render(x, x, {0: row})[0] + self.assertLessEqual(max(abs(a - b) for a, b in zip(x, out)), 1, row) + + def test_act_sets_how_often(self): + def starts(act): + m, n = R.Model(), [0] + start = m.start + m.start = lambda: (n.__setitem__(0, n[0] + 1), start()) + x = noise(2) + row = R.pblock(knobs(act=act, time=1)) + for b in range(0, N, 16): + m.call([(x[b + i], x[b + i]) for i in range(16)], row) + return n[0] + self.assertEqual(starts(0), 0) + self.assertGreater(starts(127), starts(40)) + + def test_every_type_and_variation_sounds_different(self): + x = noise(3) + outs = {} + for t in range(3): + for v in range(4): + outs[(t, v)] = R.render(x, x, {0: knobs(act=127, time=1, mix=127, type=t, var=v)})[0] + keys = list(outs) + for i, a in enumerate(keys): + for b in keys[i + 1:]: + self.assertNotEqual(outs[a], outs[b], (a, b)) + + def test_a_head_is_silent_at_the_window_ends(self): + # The fade by position: at either end of the window a head gives almost nothing, so a wrap is silent. + m = R.Model() + m.x[0x11] = 1000 + m.x[0x33] = 0x100000 + m.fade(1000) + m.ring[:] = [0x200000] * R.R + for pos, most in ((0, 0), (999, 0x40000), (500, 0x7fffff)): + m.x[0x0d], m.x[0x0e], m.x[0x0f] = pos, 0, 0 + m.head(*R.HEADS[0]) + self.assertLessEqual(abs(m.x[0x55]), most, pos) + self.assertGreater(m.x[0x55], 0x100000) # in the middle it is whole + + def test_a_head_past_a_shrunk_window_lands_on_its_start(self): + # The newest window shrank under a head (RPT or TIME turned): after the wrap it is still + # past the end, so it lands at the start, where the fade is nothing; never a negative, inverted fade. + m = R.Model() + m.x[R.PW] = 986 + m.x[0x33] = 0x100000 + m.fade(986) + m.ring[:] = [0x200000] * R.R + m.x[0x0d], m.x[0x0e], m.x[0x0f] = 5000, 0, R.ONE + m.head(*R.HEADS[0]) + self.assertEqual(m.x[0x0d], 0) + self.assertGreaterEqual(m.x[0x55], 0) + + def test_short_windows_reach_full_level(self): + # A window shorter than two minimum fades fades over half of itself, so its middle is whole. + m = R.Model() + m.x[R.PW] = 20 + m.x[0x33] = 0x020000 + m.fade(20) + m.ring[:] = [0x200000] * R.R + m.x[0x0d], m.x[0x0e], m.x[0x0f] = 9, 0, 0 + m.head(*R.HEADS[0]) + self.assertGreater(m.x[0x55], 0x1c0000) + + def test_a_chunk_lasts_its_grid_while_looping(self): + # The chunk's last event takes what is left, so a loop's events add up to the chunk exactly (no drift). + for kind, var in ((R.SPARK, 1), (R.CLOUD, 0)): + m = R.Model() + x = noise(13, 16 * 400) + lengths, setrep = [], m.setrep + def watched(): + setrep() + lengths.append((m.x[0x09], m.x[0x19], m.x[0x34])) + m.setrep = watched + row = R.pblock(knobs(act=127, time=0, rpt=7, type=kind, var=var)) + for b in range(0, len(x), 16): + m.call([(x[b + i], x[b + i]) for i in range(16)], row) + if lengths and lengths[-1][0] == 1: + break + self.assertEqual(sum(l for _, l, _c in lengths), lengths[0][2], kind) + + def test_a_head_fades_out_before_a_forced_changeover(self): + # A slow head the writer is about to reach: its gain falls toward nothing inside the zone, and at the last + # moment it takes the newest window at its start. + m = R.Model() + m.x[0x1d] = R.CLOUD + m.x[0x06] = 1 # a loop playing, with no event due during the test + m.x[0x19], m.x[0x2c] = 0x100000, 1 + m.x[0x64] = R.QUARTER + m.x[0x02] = 1000 + m.x[0x78] = m.x[0x00] + 1000 + 500 # 500 samples ahead of the writer: inside the zone + m.x[R.PS] = m.x[0x00] + 100 + m.x[0x60] = 300 + p = R.pblock(knobs(act=0, type=R.CLOUD)) + gains = [] + for _ in range(28): + m.call([(0, 0)] * 16, p) + gains.append(m.x[0x64]) + self.assertTrue(all(b <= a for a, b in zip(gains, gains[1:]))) + self.assertLess(gains[-1], R.QUARTER // 20) + m.call([(0, 0)] * 16, p) # within NEAR of the writer now + self.assertEqual((m.x[0x78], m.x[0x60]), (m.x[R.PS], 0)) + + def test_the_grain_follows_shap(self): + # The first call after init has every rate 0, the slowest class: SHAP 127 is capped at CAP_QUARTER there. + for shap, want in ((0, 441), (127, R.CAP_QUARTER)): + m = R.Model() + m.call([(0, 0)], R.pblock(knobs(shap=shap))) + self.assertEqual(m.x[0x16], want, shap) + self.assertEqual(m.x[R.PW], want - 1) + + def test_grains_come_from_somewhere_new(self): + # Every call a new piece of recent audio is the newest window, behind the writer by a grain and 64 samples at + # least and the reach at most; the heads change over to it at their own wraps, so their windows differ. + m = R.Model() + x = noise(20, 16 * 1500) + row = R.pblock(knobs(act=127, time=1, rpt=3, shap=40, type=R.CLOUD)) + starts, behind, mixed = set(), [], False + for b in range(0, len(x), 16): + m.call([(x[b + i], x[b + i]) for i in range(16)], row) + starts.add(m.x[R.PS]) + d = (m.x[0x02] - m.x[0x6e]) % R.R + behind.append(d) + if m.x[0x06] and len({m.x[st] for st, *_ in R.HEADS}) > 1: + mixed = True + g = m.x[0x16] + self.assertGreater(len(starts), 1000) + top = R.R - m.x[0x5b] - R.MARGIN + self.assertTrue(all(g + 64 - 16 <= d <= g + 64 + min(4 * R.SPRAY, top) + 16 for d in behind), (min(behind), max(behind))) + self.assertTrue(mixed) + + def test_orbit_sweeps_back_and_forth(self): + m = R.Model() + x = noise(21, 16 * 4000) + row = R.pblock(knobs(act=127, time=1, rpt=7, shap=40, type=R.ORBIT, var=1)) + phases = [] + for b in range(0, len(x), 16): + m.call([(x[b + i], x[b + i]) for i in range(16)], row) + phases.append(m.x[R.PHASE]) + self.assertGreater(m.x[R.SWEEP], 0) + self.assertTrue(any(b < a for a, b in zip(phases, phases[1:]))) # it wrapped: a whole sweep went by + self.assertGreater(max(phases), 0x600000) + + def test_spark_onsets_set_every_head_and_pluck(self): + # Each onset: every head on the newest window at its start (as late as the event), at the onset's step (the + # second reversed, the third an octave down), the level whole and falling at TAIL's rate. + m = R.Model() + x = noise(22, 16 * 800) + row = R.pblock(knobs(act=127, time=2, rpt=3, type=R.SPARK, var=0)) + seen, setrep = [], m.setrep + def watched(): + setrep() + seen.append((m.x[0x15], m.x[0x20], [m.x[st] for st, *_ in R.HEADS], m.x[R.PS], + [m.x[rt] for *_a, rt, _g, _o in R.HEADS], m.x[0x0a])) + m.setrep = watched + for b in range(0, len(x), 16): + m.call([(x[b + i], x[b + i]) for i in range(16)], row) + self.assertTrue(seen) + for level, target, starts, ps, rates, k in seen: + self.assertEqual((level, target), (0x7fffff, 0)) + self.assertEqual(set(starts), {ps}) + self.assertEqual(rates, [R.SPARK_A[k & 3], -R.SPARK_A[k & 3], R.SPARK_A[k & 3] >> 1]) + + def test_cloud_d_is_deep_and_clean(self): + m = R.Model() + x = noise(23, 16 * 600) + row = R.pblock(knobs(act=127, time=1, type=R.CLOUD, var=3)) + for b in range(0, len(x), 16): + m.call([(x[b + i], x[b + i]) for i in range(16)], row) + if m.x[0x06]: + break + self.assertEqual((m.x[0x12], m.x[0x2d], m.x[0x3e]), R.DEEP) + + def test_act_127_plays_every_slice(self): + m = R.Model() + m.call([(0, 0)], R.pblock(knobs(act=127))) + self.assertEqual(m.x[0x1f], 0x7fffff) + + def test_grains_detune_a_little(self): + # One head a call wanders around its rate before detune, never more than 15 cents. + m = R.Model() + x = noise(24, 16 * 900) + row = R.pblock(knobs(act=127, time=1, type=R.CLOUD, var=0)) + ratios = set() + for b in range(0, len(x), 16): + m.call([(x[b + i], x[b + i]) for i in range(16)], row) + if m.x[0x06]: + for base, (_st, _ph, _pl, rt, _g, _o) in zip(R.BASES, R.HEADS): + ratios.add(m.x[rt] / m.x[base]) + self.assertGreater(len(ratios), 10) + self.assertTrue(all(abs(r - 1) <= 2 ** (15 / 1200) - 1 + 1e-6 for r in ratios)) + + def test_feedback_feeds_the_ring_but_not_the_silence_watch(self): + # FDBK writes a share of the last sample's grains into the ring; the silence watch still hears only the input. + m = R.Model() + m.x[0x31], m.x[0x50] = 0x400000, R.FEED + m.x[0x2c] = 1 + before = m.x[0x02] + m.sample(0, 0) + self.assertEqual(m.ring[before], R.lim(R.mpy(0x400000, R.FEED))) + self.assertEqual(m.x[0x54], 0) + + def test_tone_darkens_the_grains(self): + # TONE 127 is (nearly) open; TONE 0 keeps far less of a bright texture's energy. + x = noise(26, 16 * 1500) + energy = {} + for t in (0, 127): + l, r, _m = R.render(x, x, {0: knobs(act=127, time=1, mix=127, type=R.CLOUD, tone=t)}) + energy[t] = sum(v * v for v in l) + self.assertGreater(energy[127], 0) + self.assertLess(energy[0], energy[127] / 4) + + def test_fdbk_127_freezes_the_ring(self): + # At 127 the writer stops: the ring holds the moment and the grains keep playing from it. + m = R.Model() + x = noise(27, 16 * 1200) + frozen = None + for b in range(0, len(x), 16): + k = 127 if b >= 16 * 800 else 90 + out = m.call([(x[b + i], x[b + i]) for i in range(16)], R.pblock(knobs(act=127, time=1, mix=127, fdbk=k))) + if b == 16 * 800: + frozen = (m.x[0x02], list(m.ring)) + # Only the word the stopped writer rests on changes, and no head sounds there (each fades out within ZONE of it). + self.assertEqual(m.x[0x02], frozen[0]) + changed = [i for i, (a, b) in enumerate(zip(m.ring, frozen[1])) if a != b] + self.assertTrue(set(changed) <= {frozen[0]}, changed[:5]) + self.assertTrue(any(l for l, _r in out)) + + def test_a_texture_swells_in_at_tails_rate(self): + # CLOUD and ORBIT rise as slowly as they fade: at TAIL 127 the level is still low 50 ms in. + levels = {} + for tail in (0, 127): + m = R.Model() + x = noise(28, 16 * 2000) + row = R.pblock(knobs(act=127, time=1, type=R.CLOUD, tail=tail)) + started = None + for b in range(0, len(x), 16): + m.call([(x[b + i], x[b + i]) for i in range(16)], row) + if m.x[0x06] and started is None: + started = b + if started is not None and b - started >= 2205: + break + levels[tail] = m.x[0x15] + self.assertGreater(levels[0], 0x780000) + self.assertLess(levels[127], 0x200000) + + def test_grains_reach_130_ms_unless_a_head_is_slow(self): + # SHAP 127: 130 ms when every head runs at full speed or faster; a half-speed head (CLOUD D) caps it so the + # spray keeps an eighth of the ring. + for var, want in ((0, R.GRAIN_MIN + R.hi(R.mpy(0x7f0000, R.GRAIN_RANGE))), (3, R.CAP_SLOW)): + m = R.Model() + x = noise(29, 16 * 900) + row = R.pblock(knobs(act=127, time=1, shap=127, type=R.CLOUD, var=var)) + for b in range(0, len(x), 16): + m.call([(x[b + i], x[b + i]) for i in range(16)], row) + self.assertAlmostEqual(m.x[0x16], want, delta=1) + self.assertGreaterEqual(R.R - m.x[0x5b] - R.MARGIN, R.R // 8) + + def test_quarter_speed_heads_get_five_grains_of_ring(self): + # ORBIT D's random speeds include a quarter: the grain is capped so five grains and an eighth of the ring fit, + # and no sounding head is forced out by the writer at SHAP 127, RPT 8. + m = R.Model() + x = noise(31, 16 * 4000) + row = R.pblock(knobs(act=127, time=2, shap=127, rpt=7, type=R.ORBIT, var=3)) + cut = 0 + for b in range(0, len(x), 16): + rmin = min(abs(m.x[rt]) for rt in (0x0f, 0x62, 0x67)) # the cap reads the rates the call starts with + m.call([(x[b + i], x[b + i]) for i in range(16)], row) + if rmin < R.QSPEED: + self.assertLessEqual(m.x[0x16], R.CAP_QUARTER) + if m.x[0x06]: + for st, *_r in R.HEADS: + if m.distance(m.x[st]) <= R.NEAR: + cut += 1 + self.assertGreaterEqual(R.R - 5 * R.CAP_QUARTER - R.MARGIN, R.R // 8) + self.assertLess(cut, 20) + + def test_shap_glides(self): + # A jump in SHAP moves the grain 16 samples a call at most (a lock or a turn never clicks). + m = R.Model() + x = noise(32, 16 * 400) + for b in range(0, len(x), 16): + shap = 0 if b < 16 * 200 else 127 + before = m.x[0x16] + m.call([(x[b + i], x[b + i]) for i in range(16)], R.pblock(knobs(act=127, time=1, shap=shap))) + if b > 16 * 2: + self.assertLessEqual(abs(m.x[0x16] - before), R.GLIDE) + + def test_the_freeze_is_endless(self): + # FDBK 127 at ACT 64: after the input stops, the frozen texture still plays every slice, well past 4 s. + m = R.Model() + x = noise(33, 16 * 1500) + [0] * (16 * 16000) + row = lambda f: R.pblock(knobs(act=64, time=1, mix=127, fdbk=f)) + playing = 0 + for b in range(0, len(x), 16): + f = 90 if b < 16 * 1200 else 127 + out = m.call([(x[b + i], x[b + i]) for i in range(16)], row(f)) + if b > 16 * 14000 and any(l for l, _r in out): + playing += 1 + self.assertGreater(playing, 1500) + + def test_tone_darkens_the_side_too(self): + # With WIDE up, TONE 0 leaves far less of the side's energy than TONE 127. + x = noise(34, 16 * 1500) + side = {} + for t in (0, 127): + l, r, _m = R.render(x, x, {0: knobs(act=127, time=1, mix=127, wide=127, type=R.CLOUD, tone=t)}) + side[t] = sum((a - b) ** 2 for a, b in zip(l, r)) + self.assertGreater(side[127], 0) + self.assertLess(side[0], side[127] / 4) + + def test_blur_diffuses_without_gain(self): + # BLUR 127 changes the grains (the all-passes smear them) but keeps their energy within a few dB. + x = noise(30, 16 * 1500) + out = {} + for b in (0, 127): + l, r, _m = R.render(x, x, {0: knobs(act=127, time=1, mix=127, type=R.CLOUD, blur=b)}) + out[b] = l + e0, e1 = sum(v * v for v in out[0]), sum(v * v for v in out[127]) + self.assertNotEqual(out[0], out[127]) + self.assertTrue(0.5 < e1 / e0 < 2, e1 / e0) + + def test_each_grain_gets_its_own_place(self): + m = R.Model() + x = noise(25, 16 * 1500) + row = R.pblock(knobs(act=127, time=1, type=R.CLOUD, wide=127)) + pans = set() + for b in range(0, len(x), 16): + m.call([(x[b + i], x[b + i]) for i in range(16)], row) + pans.add(tuple(m.x[h] for h in R.PANS)) + self.assertGreater(len(pans), 20) + + def test_tail_rings_on_after_a_loop(self): + # Loops, then none from block 680: the last loop ends within a slice; TAIL 0 is a short release (gone a few + # blocks on), and longer TAILs ring on for longer. + x = noise(7) + def ringing(tail, frm): + rows = {0: knobs(act=127, time=1, mix=96, tail=tail), 680: knobs(act=0, time=1, mix=96, tail=tail)} + out = R.render(x, x, rows)[0] + return sum(1 for a, b in zip(x[16 * frm:], out[16 * frm:]) if abs(a - b) > 2) + self.assertEqual(ringing(0, 900), 0) + self.assertGreater(ringing(40, 900), 0) + self.assertGreater(ringing(127, 900), ringing(40, 900)) + + def test_it_falls_silent_after_the_input_stops(self): + x = noise(8, FS * 7) + x = [v if i < FS else 0 for i, v in enumerate(x)] + for t in range(3): + out = R.render(x, x, {0: knobs(act=127, tail=127, type=t, mix=127, time=2)})[0] + self.assertFalse(any(out[FS * 6:]), t) + + def test_a_loop_never_starts_on_a_frozen_ring(self): + x = noise(9, 16 * (FS * 6 // 16)) + m, seen = R.Model(), [] + start = m.start + def watched(): + seen.append(m.x[0x03] == 1) + start() + m.start = watched + froze = False + row = R.pblock(knobs(act=40, tail=127, time=4, rpt=0, type=R.CLOUD), tempo(95)) + for b in range(0, len(x), 16): + m.call([(x[b + i], x[b + i]) for i in range(16)], row) + froze = froze or (m.x[0x03] == 0) + # Grainbox's newest window moves with the writer and its heads keep taking it, so it seldom needs to freeze; + # when it does, no texture starts on the frozen ring. + self.assertTrue(seen and all(seen)) + + def test_silence_stays_silent(self): + z = [0] * N + for t in range(3): + out = R.render(z, z, {0: R.DEAREST[:6] + (t,) + R.DEAREST[7:]}) + self.assertFalse(any(out[0]) or any(out[1]), t) + + +if __name__ == "__main__": + unittest.main() diff --git a/sdk/drafts/grainbox/verify.py b/sdk/drafts/grainbox/verify.py new file mode 100644 index 0000000000..e56074d848 --- /dev/null +++ b/sdk/drafts/grainbox/verify.py @@ -0,0 +1,200 @@ +#!/usr/bin/env python3 +"""Grainbox's render gate: engine.asm assembled and run in dsp_host, held to reference.py's model. + + python3 -B verify.py (needs the DSP tools; a few minutes) + python3 -B verify.py --fx-audit PLAN RENDERS [--type N --var N] + +No firmware is read: the harness is sdk/octatrack/tools/dsp_gate.py, a synthetic image and dsp_host, whose +allocator hands the instance Y:$4000 (an FX2-sized slot). Knobs arrive as value << 16; TYPE is slot 6 and VAR +slot 8 (bits 16-23 of r6+$c and r6+$d); the tempo word, tempo24 << 8, is r6+$13. What this cannot see: the +composed image, the real dispatcher and allocator, the panel and the sound on a unit. + +Gates: + 1. Every instruction decodes as written (signed mpy/mac x0,y1, no rep, no mpysu/macsu), no label is a + prefix of another (case-blind), the state stays inside X:(r7+$00..$81), and proc returns with every + modifier register it set to a modulus linear again. + 2. Bit for bit the model: every TYPE and VAR looping at every slice, the defaults at three tempos, TIME 1/4 + and 1/2 through every chunk, every + knob swept, a tempo change while playing, a 5-frame trig split every block, a knob word carrying an LFO's + fraction bits, garbage state before init, state from two seeds, and an FX1-sized slot (dry). + 3. Silence in is exact silence out; a burst's tail ends in exact silence; after the input stops, every type falls + silent within 6 s at TAIL 127, and every TYPE and VAR at TIME 1/4 within 4.2 s (the silence watch). + 4. The cycle meter, printed for TESTING.md: every TYPE and VAR, the dearest setting, TAIL's ring-freeze path. +""" +import pathlib +import re +import sys + +HERE = pathlib.Path(__file__).resolve().parent +sys.path.insert(0, str(HERE)) + + +def _library(): + for base in HERE.parents: + if (base / "sdk/octatrack/tools/dsp_gate.py").is_file(): + return base / "sdk/octatrack/tools" + sys.exit("[FAIL] sdk/octatrack/tools/dsp_gate.py not found: run this inside a Modwerk checkout") + + +sys.path.insert(0, str(_library())) +import dsp_gate as G # noqa: E402 +import reference as R # noqa: E402 + +SOURCE = HERE / "engine.asm" +STATE_LAST = 0x81 + + +def knobs(base=R.DEFAULTS, **kw): + row = list(base) + [0] * (12 - len(base)) + names = {"time": 0, "act": 1, "shap": 2, "rpt": 3, "mix": 4, "tail": 5, "type": R.TYPE_SLOT, "tone": 7, + "var": R.VAR_SLOT, "wide": 9, "blur": 10, "fdbk": 11} + for name, value in kw.items(): + row[names[name]] = value + return tuple(row) + + +def tempo(bpm): + return (bpm * 24) << 8 + + +def main(): + G.require_tools(HERE) + gate = G.Gates("grainbox") + with G.workdir("grainbox-verify-") as tmp: + work = pathlib.Path(tmp) + asm = G.assemble(SOURCE, work) + if "--fx-audit" in sys.argv: + plan, renders = sys.argv[sys.argv.index("--fx-audit") + 1:][:2] + t = int(sys.argv[sys.argv.index("--type") + 1]) if "--type" in sys.argv else 0 + v = int(sys.argv[sys.argv.index("--var") + 1]) if "--var" in sys.argv else 3 + for path in G.render_fx_audit(asm, plan, renders, knobs(R.DEAREST, type=t, var=v)): + print(path.name, flush=True) + return + + # 1. decodes as written + problems = G.trap_problems(asm, forms=("x0,y1",)) + gate("1 decodes as written: signed mpy/mac x0,y1, no rep, no mpysu/macsu", not problems, "; ".join(problems[:4])) + labels = [l.lower() for l in re.findall(r"^([A-Za-z_][A-Za-z0-9_]*):", SOURCE.read_text(), re.M)] + clashes = [(a, b) for a in labels for b in labels if a != b and b.startswith(a)] + [l for l in set(labels) if labels.count(l) > 1] + gate("1 no label is a prefix of another, case-blind (dsp_asm resolves labels by prefix)", not clashes, str(clashes)) + # Every modifier register proc sets to a modulus is linear again where proc returns (gbzz): the code that + # runs after an effect addresses with them (every published effect resets them; dsp_host cannot hear it). + text = SOURCE.read_text() + bent = sorted(set(re.findall(r"move\s+#>?\$(?!ffffff)[0-9a-fA-F]+,(m[0-7])", text))) + tail = text[text.index("\ngbzz:"):] + tail = tail[:tail.index("rts")] + unreset = [m for m in bent if not re.search(r"move\s+#>?\$ffffff," + m + r"\b", tail)] + gate("1 proc leaves every modifier register it bends linear again (" + ", ".join(bent) + ")", bool(bent) and not unreset, str(unreset)) + # lua (Rn+aa) takes a 7-bit signed offset: dsp_asm wraps a larger one without a word (a table at $58 + # read 40 words below r7 before this gate). + far = [l.strip() for l in text.splitlines() if re.search(r"\blua\s+\(r[0-7]\+\$([0-9a-fA-F]+)\)", l) + and int(re.search(r"\(r[0-7]\+\$([0-9a-fA-F]+)\)", l).group(1), 16) > 0x3f] + gate("1 every lua (Rn+aa) offset is within its 7 bits (at most $3f)", not far, "; ".join(far)) + offsets = G.state_offsets(SOURCE.read_text()) + gate("1 state inside X:(r7+$00..$81)", bool(offsets) and offsets[-1] <= STATE_LAST, f"(highest ${offsets[-1]:02x})") + + # 2. bit for bit the model + n = 16 * 3000 + nl, nr = G.noise(n, 0.6, 1), G.noise(n, 0.6, 2) + tl = [a + b for a, b in zip(G.tone(220, 0.4, n), G.noise(n, 0.1, 3))] + + def same(name, inst, raw=None, **kw): + r = G.run(asm, [inst], split=kw.get("split"), pwords=kw.get("pwords"), dirty=kw.get("dirty"), + image=kw.get("image")) + rows = inst.rows or {0: inst.knobs} + want = R.render(inst.left, inst.right, rows, split=kw.get("split") or 0, raw=raw, base=kw.get("base", 0x4000)) + gate.same(name, r.outputs[0], want[:2]) + return r + + def tempo_words(bpm): + return dict(pwords=[(0, 0x13, tempo(bpm))], raw={0x13: tempo(bpm)}) + + meters = {} + for t, tname in enumerate(R.TYPE_NAMES): + for v, vname in enumerate(R.VAR_NAMES): + row = knobs(time=0, act=127, rpt=3, shap=64, type=t, var=v) + meters[tname + vname] = same(f"2 {tname} {vname} looping at every slice", G.Instance(tl, nl, knobs=row)).meter + for bpm in (90, 120, 174): + same(f"2 the defaults at {bpm} BPM", G.Instance(nl, nr, knobs=R.DEFAULTS), **tempo_words(bpm)) + rows, block = {0: knobs(time=1, act=110)}, 40 + for slot, values in (("time", (0, 5, 4, 1)), ("act", (0, 127, 100)), ("shap", (0, 127, 40)), ("rpt", (0, 7, 2)), + ("mix", (0, 127, 100)), ("type", (1, 2, 0)), ("var", (1, 2, 3, 0)), + ("tone", (0, 90)), ("wide", (127, 60)), ("blur", (127, 30)), ("fdbk", (126, 0))): + for value in values: + rows[block] = knobs(rows[max(rows)], **{slot: value}) + block += 120 + same("2 every knob swept while glitching", G.Instance(nl, nr, rows=rows)) + long_n = 16 * 14000 + ll, lr = G.noise(long_n, 0.6, 5), G.noise(long_n, 0.6, 6) + for time, label in ((4, "1/4"), (5, "1/2")): + same(f"2 TIME {label} at 120 BPM: glitches on its grid, through every chunk of the slice", + G.Instance(ll, lr, knobs=knobs(time=time, act=64)), **tempo_words(120)) + for t, tname in ((2, "SPARK"), (0, "CLOUD")): + same(f"2 {tname}, TIME 1/2 at 30 BPM: 32 chunks, each within the ring's reach", + G.Instance(ll, lr, knobs=knobs(time=5, act=127, rpt=1, type=t, var=1)), **tempo_words(30)) + frz2 = list(knobs(time=4, rpt=0, act=40, type=1)) + frz2[5] = 127 + same("2 TAIL 127, TIME 1/4, RPT 1: a glitch due while the tail froze the ring ends it and records again", + G.Instance(ll, lr, knobs=tuple(frz2)), **tempo_words(95)) + for t, tname in enumerate(R.TYPE_NAMES): + rows2 = {0: knobs(R.DEAREST, type=t, time=1, act=110), 400: knobs(R.DEAREST, type=t, time=1, act=0)} + same(f"2 {tname}: TONE open, WIDE, BLUR and FDBK 126 at full, textures then a tail", G.Instance(tl, nl, rows=rows2)) + rows3 = {0: knobs(type=t, time=1, act=127, fdbk=90, tone=40), 300: knobs(type=t, time=1, act=127, fdbk=127, tone=40), + 600: knobs(type=t, time=1, act=127, fdbk=60, tone=0)} + same(f"2 {tname}: TONE dark, FDBK 90, then 127 freezes the ring mid-texture, then 60 records again", + G.Instance(tl, nl, rows=rows3)) + same("2 a tempo change while playing (raw word every call)", G.Instance(nl, nr, knobs=knobs(time=0, act=127)), + pwords=[(0, 0x13, tempo(140))], raw={0x13: tempo(140)}) + same("2 a trig split every block", G.Instance(nl, nr, rows=rows), split=5) + word = (127 << 16) | 0xb700 + same("2 a knob word with an LFO's fraction bits", G.Instance(nl, nr, knobs=knobs(act=127, time=0)), + pwords=[(0, 1, word)], raw={1: word}) + same("2 garbage state before init", G.Instance(nl, nr, rows=rows), dirty=0x5a5a5a) + gate.two_seeds("2 the same output from state filled with two seeds: nothing read before it is written", + asm, [G.Instance(nl, nr, rows=rows)]) + same("2 an FX1-sized slot passes the track dry", G.Instance(nl, nr, knobs=knobs(act=127, time=0)), + image=asm.image(x={G.ALLOC_AT: 0x2000}, name="fx1"), base=0x2000) + + # 3. silence + z = [0] * n + for t, tname in enumerate(R.TYPE_NAMES): + (l, r), = G.run(asm, [G.Instance(z, z, knobs=knobs(R.DEAREST, type=t))], tag="zero").outputs + gate(f"3 {tname}: silence in, silence out at the dearest setting", not any(l + r)) + # 5 s: at the dearest setting FDBK keeps the grains circulating while TAIL 127 fades (about 3 s to -60 dB, then + # cut at -72 dB), so the tail must end in exact silence within that, not within 3 s. + m = G.FS * 5 + x = G.burst(4410, m) + (l, r), = G.run(asm, [G.Instance(x, x, knobs=knobs(R.DEAREST, type=1))], tag="tail").outputs + gate("3 a burst's tail ends in exact silence, FDBK and TAIL at their longest", not any(l[-G.FS // 2:] + r[-G.FS // 2:])) + + stop = [v if i < G.FS else 0 for i, v in enumerate(G.noise(G.FS * 8, 0.6, 4))] + for t, tname in enumerate(R.TYPE_NAMES): + frow = list(knobs(R.DEAREST, type=t, time=2, act=40)) + frow[5] = 127 + (l, r), = G.run(asm, [G.Instance(stop, stop, knobs=tuple(frow))], tag="stop").outputs + gate(f"3 {tname}: falls silent within 6 s after the input stops, TAIL 127", not any(l[G.FS * 7:] + r[G.FS * 7:])) + + quiet = [v if i < G.FS * 2 else 0 for i, v in enumerate(G.noise(16 * (G.FS * 7 // 16), 0.6, 7))] + for t, tname in enumerate(R.TYPE_NAMES): + for v, vname in enumerate(R.VAR_NAMES): + q = knobs(time=4, act=64, type=t, var=v) + q = q[:5] + (127,) + q[6:] + r = G.run(asm, [G.Instance(quiet, quiet, knobs=q)], pwords=[(0, 0x13, tempo(95))], tag="quiet") + want = R.render(quiet, quiet, {0: q}, raw={0x13: tempo(95)}) + (l, rr), = r.outputs + ok = (l, rr) == tuple(want[:2]) and not any(l[int(G.FS * 6.2):] + rr[int(G.FS * 6.2):]) + gate(f"3 {tname} {vname}: TIME 1/4, TAIL 127, 95 BPM: bit for bit, and silent 4.2 s after the input stops", ok) + + # 4. cost: also at the dearest setting and on TAIL's ring-freeze path (TIME 1/4, RPT 1, TAIL 127) + for t, tname in enumerate(R.TYPE_NAMES): + meters["dear " + tname] = G.run(asm, [G.Instance(tl, nl, knobs=knobs(R.DEAREST, type=t))], tag="dear").meter + frz = list(knobs(R.DEAREST, type=t, time=4, rpt=0, act=40)) + frz[5] = 127 + meters["freeze " + tname] = G.run(asm, [G.Instance(tl, nl, knobs=tuple(frz))], tag="freeze").meter + print("[INFO] cycle meter, looping at every slice: " + ", ".join(f"{k} {v}" for k, v in meters.items())) + print(f"[INFO] dsp_gate.py {G.provenance(HERE)['dsp_gate.py']}") + gate.finish() + + +if __name__ == "__main__": + main()