diff --git a/sdk/drafts/tapekeys/LICENSE b/sdk/drafts/tapekeys/LICENSE new file mode 100644 index 0000000000..250cae34a6 --- /dev/null +++ b/sdk/drafts/tapekeys/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/tapekeys/README.md b/sdk/drafts/tapekeys/README.md new file mode 100644 index 0000000000..d76f6f76d1 --- /dev/null +++ b/sdk/drafts/tapekeys/README.md @@ -0,0 +1,62 @@ +# Tapekeys + +Version 0.1.0-experimental · by @jrs1717 · a draft: bit for bit against its model in `dsp_host`; heard in RAM trials and through the test procedure on the author's MKII (package fe5b9a6a). + +## Overview + +Tapekeys turns a track into a tape-replay keyboard, on FX1 or FX2. Each trig starts a note on its own strip of tape: +- **The get-up:** the note starts a little flat and rises to pitch as the pinch roller grabs (GTUP). +- **Its own tape:** every note plays slightly flat by its own amount and at its own level (SPRD), as if each key had its own worn tape. +- **The swell:** SWEL brings the note in, from instant to half a second. +- **The tape runs out:** TAPE seconds after the trig the note ends, cut or faded (END), however long you hold it. +- **The mechanics:** MECH adds the running machine under the note: a little hiss and rumble, the motor's faint hum, a soft tick and a slight level wobble at each turn of the capstan, the tape's sparse scrape, and a clunk when the tape runs out and springs back. CLIK, on the setup page, is the roller's thunk at each trig, with its own level. + +TONE darkens it and MIX blends it with the dry track. It works best on held notes of strings, choirs, flutes or +organs played chromatically. Before the track's first trig it passes the track dry. For the deck's own drift, flutter +and hiss, put Cassettebox in the other slot. + +## Controls + +| Page | Knob | Name | Range | Default | What it does | +|---|---|---|---|---|---| +| Main | A | TAPE | 1s-12s | 8s | The tape's length: the note ends this long after its trig. | +| Main | B | SWEL | 0-127 | 30 | The swell: 0 comes in at once (2 ms), 127 over about half a second. | +| Main | C | GTUP | 0-127 | 40 | The get-up: how flat a note starts (up to 4 %) before rising to pitch over about 60 ms. | +| Main | D | SPRD | 0-127 | 50 | Each note's own tape: up to about 12 cents flat and 2 dB up or down, new on every trig. | +| Main | E | MECH | 0-127 | 30 | The running machine: a little hiss and rumble, the motor's hum, capstan ticks and wobble (about 5 a second), the tape's scrape, and a clunk when the tape runs out. | +| Main | F | MIX | 0-127 | 127 | Dry/tape blend. 127 is the tape alone. | +| Setup | A | END | CUT, FADE | CUT | How the tape runs out: CUT stops the note in 5 ms, FADE lets it die over a quarter second. | +| Setup | B | TONE | 0-127 | 90 | A low-pass from 1.5 kHz at 0 to 16 kHz at 127. | +| Setup | C | CLIK | 0-127 | 30 | The roller's thunk at each trig: it rises over about 3 ms (no click) and dies over 40 ms. 0 none. | + +- The notes are always slightly flat (by up to SPRD's amount), never sharp: a tape can lag the key but not run ahead + of it. Tune the track a few cents up if you want SPRD's notes centred. +- On FX1 a long TAPE leaves less room for SPRD's pitch: at 8 s a note is at most about 7 cents flat, at 12 s about 5. + +## Usage + +On an audio track: hold FUNC and press FX1 (or FX2), turn LEVEL to TAPEKEYS and press YES. + +## Quick tutorial + +1. Load a held string or choir sample on a Flex track, pick TAPEKEYS on FX1 and play a few notes chromatically. +2. Turn SPRD up: each note comes out a little different. Turn GTUP up for a slower, wobblier start. +3. Set TAPE to 2s and hold a long note: it stops after two seconds. Set END to FADE, then TAPE back to 8s. + +## Compatibility and limitations + +- Works on FX1 or FX2 of any track; several at once. +- It follows each trig of its own track: a track with no trigs (a live input played by hand) passes dry. +- Draft: not yet heard or tested on a unit. + +## Screens and audio + +Captured from the author's MKII over USB, package fe5b9a6a in a RAM trial on the development twin: + +![FX1 main page titled TAPEKEYS with knobs TAPE (8s), SWEL, GTUP, SPRD, MECH and MIX.](media/ot-controls.png) + +![FX1 SETUP page with an effect list ending in Tapekeys, selected; controls CUT (END), TONE and CLIK.](media/ot-location.png) + +## Tests and measurements + +See [TESTING.md](TESTING.md). diff --git a/sdk/drafts/tapekeys/TESTING.md b/sdk/drafts/tapekeys/TESTING.md new file mode 100644 index 0000000000..e203477c71 --- /dev/null +++ b/sdk/drafts/tapekeys/TESTING.md @@ -0,0 +1,109 @@ +# Tapekeys 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. + +## The model and the engine + +- `python3 test_reference.py`: 22 tests pass (5 December 2026): dry before the first trig, the trig at its exact + sample, a split block the same as an unsplit one, the tape running out to exact silence (CUT and FADE), FADE slower + than CUT, a retrig restarting the tape, the swell, the get-up starting flat and settling in tune, SPRD's notes flat + and different, the offset kept inside the ring for every TAPE, mechanics silent at 0 and before the first trig, the + thump then head noise, MIX 0 nearly dry, FX1 reading its own trig word and not FX2's. +- `python3 verify.py`: every gate passes. engine.asm is bit for bit the model in dsp_host: dry before any trig; one + note running out (CUT and FADE, with and without the split at its sample); a long note at the most spread; trigs + every 30 blocks with every knob swept (with and without the split); a trig every block; an FX1 slot reading its own + word and ignoring FX2's; a knob word with LFO fraction bits; garbage state; two seeds. Silence in with MECH 0 is + exact silence, a note's end is exact silence with MECH full, and on all four FX1 bases the guard sees no write + outside the slot's 3,072 words. dsp_host has no per-block trig word, so verify.py wraps the engine in a fixture that + writes the trig word on a schedule before each call; the engine's bytes are unchanged. +- Cost: 325 executed instructions a sample while notes play, 334 with a trig every block (about stock SPRING REV's worst; the record comes with the performance audit). +- The author's first trials (5 December 2026, package af219ba6, RAM twin 117eb07f): "The mechanics is cool, but the click at the beginning is a bit distracting". Changed: MECH is now only the running tape (head hiss plus a low rumble); the onset is CLIK, its own setup knob, a thunk through two low-passes that rises over about 3 ms instead of starting at its peak. Then (package 2252be8e): "Mech seems a bit noisy, I think thinking it would be a little noise with some more mechanical artifacts"; the author chose all four offered: the hiss 12 dB and the rumble about 8 dB down, and MECH now brings the motor's 48 Hz hum, a tick and a slight level wobble at each capstan turn, the tape's sparse scrape, and a clunk when the tape runs out. At MECH full the bed under a note is -42 dBFS RMS (was -34), its ticks peak about -30 dBFS, the clunk about -23 dBFS. +- Found while adding them: the engine's scrape test kept the noise word's sign in the accumulator's extension, so about half of all samples scraped (the model was right; verify.py caught it at 1 LSB); and the run-out cases rendered 0.54 s, shorter than their 1 s tape, so they never reached its end (now 2.2 s). +- Found while writing: MIX 127 let the dry through at -84 dB (now the tape alone), and the gate's fixtures shared one + image file name in dsp_gate, so one case ran another's fixture (each fixture now names its own image). + +## On the unit + +### Test procedure: tapekeys 0.1.0-experimental, 2026-12-06 + +Build: revision `0f3c46f806b5`, package `tapekeys.mwrm` SHA-256 `fe5b9a6a8d8aa80cbe1f2440fb0137d9c7649c5ffd22c2349a89a3fb7e483c7d`. `npm run test:procedure -- tapekeys --package /../tapekeys.mwrm --share /tmp/modwerk-share.sock --no-emulator --soak-pc-channel 11 --soak-auto-channel 11 --out `, 581 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 tapekeys` | 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 /../tapekeys.mwrm` | logs/07-unit-pass.log | +| 8 | Soak | unit | passed: Part 1 on A01: back on A01 from A02, Part 1 on A01 reloaded; working = saved | `node scripts/device.mjs try /../tapekeys.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 /../tapekeys.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 fe5b9a6a, Tapekeys on T1 FX1, T2 FX2 and T5 FX1 at once (FX1 and FX2, both cores) while the pattern played: the defaults; END FADE, TONE 0, CLIK 127; TAPE 1s with SWEL, GTUP, SPRD and MECH at 127 and MIX 64 (notes running out every second); TAPE swept to 12s while playing. Health after each: 0 stalls, 0 errors, no crash, CPU 700-729. After STOP: stall none recorded, latest entry 44.7 us late (0 in the guard's window). The main and setup pages match the README; every label fits (TAPE, SWEL, GTUP, SPRD, MECH, MIX; CUT/FADE, TONE, CLIK). The sound not judged here (the author's stage; heard in four RAM trials). STOP, Part reloaded (working = saved), the trial rolled back | `device try`, `pick:`, `png:`, `health`, `stats`, `stall`, `late`, `reload` | media/ot-controls.png, media/ot-location.png, hand-setup.png, hand-main.png | +| 12 | Submitter's own stage | unit | to do: the author's Keep and power cycle on fe5b9a6a | | | + +#### 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 | +|---|---|---|---| +| FX1 and FX2 on both cores | the ring sits in the slot's buffer; FX1's is 3,072 words | step 7 (six placed at once); step 11 (T1 FX1, T2 FX2, T5 FX1); verify.py's guard on all four FX1 bases | passed: no crash, stall or dropped frame; CPU 727, core 0 peak 769, core 1 peak 634 per mille | +| Notes running out while playing | the run-out, the return clunk and FADE every second | step 11: TAPE 1s, MECH 127, END FADE | passed: no stall or crash; the clunk's sound is the author's to judge | +| TAPE moved while a note plays | the head's room cap is drawn at the trig | step 11 (swept 1s to 12s); test_reference.py: the offset stays under every TAPE's cap | passed | +| Trig splits | stock splits calls at the trig's sample | verify.py with and without the split; test_reference.py: split = unsplit | passed in dsp_host | +| The frame-cost curve, playing | a DSP effect does no ColdFire work per frame | step 9: `device:price --curve T1,T3,T6,T7` | rest 0, first and each 3.5 points within the curve's run-to-run spread (T1 -2.6, T3 -3.3, T6 +3.5, T7 -2.5); empty and medium patterns admitted, longest frame 85.5 % | +| Persistence | knob bytes saved in the Part | step 12 | the author's stage | + +#### 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 fe5b9a6a8d8a, , test:procedure steps 6, 7, 8, 9, 10; the developer's stage . +Edge cases: the table above, one row a case (or "Edge cases: none, because …"). + +## Stock flows + +Not tested. + +## Performance + +Not measured on a unit. + +## Reported by the author after the Keep (package fe5b9a6a) + +Reported by jrs1717 on the flashed base, one trig at the pattern's start and a long sample, while turning main and setup +knobs: "seems to be an issue with volume cutting out after changing values while playing with a play trig"; the sound +stayed silent through later trigs until stop and play. + +Cause, as far as software can see it: the engine placed each trig by a count of the block's frames carried from call +to call. If the dispatcher's calls ever stop summing to 16 frames a block (a call left out, say), every later trig +falls outside its call and is missed; the note runs out once and stays silent, and only a restart of the sequencer +(where the count and the trig line up again) brings it back. Which event moved the count on the unit is not known. + +Fixed: each call now places the trig by itself, from the dispatcher's a flag (a = 0: the block's first sub-block, from +frame 0; a = 1: the block's last frames, from 16 - n7), so nothing carries from call to call; a call with no frames +returns at once. test_reference.py: a block whose a = 0 call never came still finds its trig and the later ones; an +empty call. verify.py: every gate passes again, bit for bit. + +The author tried that build (e5fb1d2c) in a RAM trial: it still cut out ("something is definitely wrong, but I can't +pinpoint it. Volume keeps cutting out"). Next suspect: the trig bit in only one of the block's two calls (the +finding says it is set in the trig's frame, not in which call), so a trig landing mid-block was missed by both +builds. Fixed: a latch fires each trig once, whichever calls carry its bit: at its sample in its call, at once when +already past, at the next call's start when it lands after this call. test_reference.py 24 pass (the bit in the first +call only, in the second only; one firing while the bit stays); verify.py bit for bit. Not yet tested on a unit. + diff --git a/sdk/drafts/tapekeys/engine.asm b/sdk/drafts/tapekeys/engine.asm new file mode 100644 index 0000000000..15a0493378 --- /dev/null +++ b/sdk/drafts/tapekeys/engine.asm @@ -0,0 +1,712 @@ +; TAPEKEYS -- an original tape-replay keyboard effect for FX1 or FX2 of any track. +; Copyright (c) 2026 jrs1717, MIT (LICENSE). Written by gen_engine.py from reference.py. +; +; The track goes into a stereo ring in the slot's buffer (1500 frames of L and R, +; the write frame counting down, frame 0 repeated after the ring as a guard; +; 3002 words, which an FX1 slot's 3,072 holds). Each trig of the track (its frame +; word, r6+$18 on FX1 and r6+$12 on FX2: bit 15 in the trig's block, the low +; nibble its sample; a counter of the block's frames places it, as stock splits +; the calls at that sample) starts a note: the read head starts on the input +; and falls behind it by the note's own offset (flat, limited so the head stays +; in the ring for the whole TAPE) plus a decaying get-up (GTUP: up to 4 % flat, +; about 60 ms); the swell (SWEL) brings it in; TAPE seconds later the note is +; cut (5 ms) or faded (250 ms, END), to exact silence. MECH adds the running +; machine under the note (a little hiss and rumble, the motor's hum, capstan +; ticks and wobble, the tape's scrape) and a clunk when the tape runs out; +; CLIK the roller's rounded thunk at the trig. TONE: a one-pole low-pass. +; MIX blends with the dry track. Before the first trig the track passes dry. +; +; Knobs (value<<16): TAPE r6+0 (0-11: 1-12 s), SWEL r6+1, GTUP r6+2, SPRD r6+3, +; MECH r6+4, MIX r6+5; END slot 6 (r6+$c bits 16-23), TONE slot 7 (bits 8-15), +; CLIK slot 8 (r6+$d bits 16-23). +; +; State (X, offsets from r7; init clears $00..$3c): see reference.py. Every +; product is the signed mpy/mac x0,y1 form; labels are tk and two characters. +; CYCLES_FORWARD_BRANCHES +init: + clr a + move r7,r5 + move #>$ffffff,m5 + do #$3d,>tki0 + move a,x:(r5)+ +tki0: + nop + move x:>$213,r4 + move #>$ffffff,m4 + nop + move x:(r4),x0 + move x0,x:(r7+$00) + move x0,a + move #>$4000,x0 + sub x0,a + clr b + move #>$1,x0 + tst a + tlt x0,b + move b1,x:(r7+$01) + move x:(r7+$00),r4 + clr a + move #>3002,x0 + do x0,>tki1 + move a,y:(r4)+ +tki1: + nop + rts +proc: + move a1,x:(r7+$3a) + move n7,b + tst b + bgt tkp1 + rts +tkp1: + move #>$ffffff,m4 + move #>$ffffff,m5 +; the knobs, every call + move x:(r6+$0),a + and #>$7f0000,a + move #>$0b0000,x0 + cmp x0,a + tgt x0,a + asr #16,a,a + move a1,x:(r7+$1d) + move #>$1,x0 + add x0,a + move a1,x0 + move #>44100,y1 + mpy x0,y1,a + asr a + move a0,x:(r7+$12) +; SWEL: k = slow + u^4 (fast - slow), u = 1 - SWEL + move #>$7f0000,a + move x:(r6+$1),b + and #>$7f0000,b + move b1,x0 + sub x0,a + move a1,x0 + move a1,y1 + mpy x0,y1,a + move a1,x0 + move a1,y1 + mpy x0,y1,a + move a1,x0 + move #>$017209,y1 + mpy x0,y1,a + move #>$00017c,x0 + add x0,a + move a1,x:(r7+$13) + move x:(r6+$2),a + and #>$7f0000,a + move a1,x:(r7+$1b) + move x:(r6+$3),a + and #>$7f0000,a + move a1,x:(r7+$1c) + move x:(r6+$4),a + and #>$7f0000,a + move a1,x0 + move #>$00f5c3,y1 + mpy x0,y1,a + move a1,x:(r7+$17) + move #>$0ccccd,y1 + mpy x0,y1,a + move a1,x:(r7+$2c) + move #>$008312,y1 + mpy x0,y1,a + move a1,x:(r7+$35) + move #>$066666,y1 + mpy x0,y1,a + move a1,x:(r7+$36) + move #>4800,y1 + mpy x0,y1,a + move a1,x:(r7+$33) + move #>$051eb8,y1 + mpy x0,y1,a + move a1,x:(r7+$34) + move #>$133333,y1 + mpy x0,y1,a + move a1,x:(r7+$37) + move x:(r6+$d),a + and #>$7f0000,a + move a1,x0 + move #>$19999a,y1 + mpy x0,y1,a + move a1,x:(r7+$18) +; MIX: 127 is the tape alone, else mx + mx / 128 + move x:(r6+$5),a + and #>$7f0000,a + tfr a,b + asr #7,b,b + add a,b + move #>$7fffff,y0 + move #>$7f0000,x0 + cmp x0,a + teq y0,b + move b1,x:(r7+$19) + move b1,x0 + move #>$7fffff,a + sub x0,a + move a1,x:(r7+$1a) + move x:(r6+$c),a + and #>$00ff00,a + asl #8,a,a + move a1,x0 + move #>$5a45d2,y1 + mpy x0,y1,a + move #>$18a13c,x0 + add x0,a + move a1,x:(r7+$15) + move a1,x0 + move #>$7fffff,a + sub x0,a + move a1,x:(r7+$16) + move #>$00949c,b + move #>$0002f9,y0 + move x:(r6+$c),a + and #>$7f0000,a + tne y0,b + move b1,x:(r7+$14) +; the trig: the word of this slot; its sample against where this call starts in the block (the a = 0 call +; at 0, the a = 1 call at 16 - n7), so no count runs from call to call + move x:(r7+$01),b + tst b + move x:(r6+$12),a + beq tkw1 + move x:(r6+$18),a +tkw1: + move a1,x:(r7+$25) + and #>$f,a + move x:(r7+$3a),b + tst b + beq tkw2 + move #>$10,b + move n7,x0 + sub x0,b + move b1,x0 + sub x0,a +tkw2: + move a1,x:(r7+$24) +; a trig fires once: at its sample in this call, at once when past, at the next call's start when later + move x:(r7+$25),a + and #>$008000,a + bne tkb1 + clr a + move a,x:(r7+$3b) + move x:(r7+$3c),a + tst a + beq tkno + clr b + move b,x:(r7+$3c) + move b,x:(r7+$24) + bra tkfi +tkb1: + move x:(r7+$3b),a + tst a + bne tkno + move x:(r7+$24),a + clr b + tst a + tlt b,a + move a1,x:(r7+$24) + move n7,x0 + cmp x0,a + blt tkfr + move #>$1,x0 + move x0,x:(r7+$3c) + bra tkno +tkfr: + move #>$1,x0 + move x0,x:(r7+$3b) + clr b + move b,x:(r7+$3c) +tkfi: + move x:(r7+$24),a + tst a + beq tkt0 + move a1,x:(r7+$1f) + bsr tksg +tkt0: + bsr tktr + move n7,a + move x:(r7+$24),x0 + sub x0,a + move a1,x:(r7+$1f) + bsr tksg + rts +tkno: + move n7,x0 + move x0,x:(r7+$1f) + bsr tksg + rts +; a note starts: its offset (flat, at most the TAPE's cap) and level from two noise words +tktr: + move x:(r7+$10),x0 + move #>$01f2d5,y1 + mpy x0,y1,a + asr a + move a0,a + move #>$0002b5,x0 + add x0,a + move a1,x:(r7+$10) + move a1,x:(r7+$2b) + move x:(r7+$1d),a + move #>129,b + move #>1,x0 + cmp x0,a + move #>64,y0 + teq y0,b + move #>2,x0 + cmp x0,a + move #>43,y0 + teq y0,b + move #>3,x0 + cmp x0,a + move #>32,y0 + teq y0,b + move #>4,x0 + cmp x0,a + move #>25,y0 + teq y0,b + move #>5,x0 + cmp x0,a + move #>21,y0 + teq y0,b + move #>6,x0 + cmp x0,a + move #>18,y0 + teq y0,b + move #>7,x0 + cmp x0,a + move #>16,y0 + teq y0,b + move #>8,x0 + cmp x0,a + move #>14,y0 + teq y0,b + move #>9,x0 + cmp x0,a + move #>12,y0 + teq y0,b + move #>10,x0 + cmp x0,a + move #>11,y0 + teq y0,b + move #>11,x0 + cmp x0,a + move #>10,y0 + teq y0,b + move x:(r7+$1c),x0 + move #>29,y1 + mpy x0,y1,a + move a1,y1 + move x:(r7+$2b),a + and #>$7fffff,a + move a1,x0 + mpy x0,y1,a + cmp b,a + tgt b,a + move a1,x:(r7+$04) + move x:(r7+$10),x0 + move #>$01f2d5,y1 + mpy x0,y1,a + asr a + move a0,a + move #>$0002b5,x0 + add x0,a + move a1,x:(r7+$10) + move x:(r7+$1c),x0 + move #>$1020c5,y1 + mpy x0,y1,a + move a1,y1 + move x:(r7+$10),x0 + mpy x0,y1,a + move #>$400000,x0 + add x0,a + move a1,x:(r7+$0f) + clr a + move a,x:(r7+$03) + move a,x:(r7+$06) + move a,x:(r7+$09) + move x:(r7+$1b),x0 + move x0,x:(r7+$05) + move #>$7fffff,x0 + move x0,x:(r7+$07) + move x0,x:(r7+$0a) + move #>$1,x0 + move x0,x:(r7+$08) + move #>$7fffff,x0 + move x0,x:(r7+$2e) + move x:(r7+$18),x0 + move x0,x:(r7+$38) + clr a + move a,x:(r7+$30) + rts +; a run of frames: the tape's end, the envelope's rate, the samples, the count +tksg: + move x:(r7+$09),a + move x:(r7+$12),x0 + cmp x0,a + blt tkg1 + move x:(r7+$07),a + tst a + beq tkg1 + clr b + move b,x:(r7+$07) + move #>$7fffff,x0 + move x0,x:(r7+$0a) + move x0,x:(r7+$2e) + move x:(r7+$37),x0 + move x0,x:(r7+$38) +tkg1: + move x:(r7+$07),a + tst a + bne tkg2 + move x:(r7+$06),a + move #>$002000,x0 + cmp x0,a + bge tkg2 + clr a + move a,x:(r7+$06) +tkg2: + move x:(r7+$13),b + move x:(r7+$07),a + tst a + bne tkg3 + move x:(r7+$14),b +tkg3: + move b1,x:(r7+$1e) + move #>$1,n0 + move x:(r7+$1f),x0 + do x0,>tksl +; the frame into the ring (frame 0 also into the guard) + move x:(r0),x1 + move x:(r0+n0),y0 + move x1,x:(r7+$20) + move y0,x:(r7+$21) + move x:(r7+$02),a + asl a + move x:(r7+$00),x0 + add x0,a + move a1,r4 + nop + move x1,y:(r4)+ + move y0,y:(r4) + move x:(r7+$02),a + tst a + bne tks1 + move x:(r7+$00),a + move #>3000,x0 + add x0,a + move a1,r5 + nop + move x1,y:(r5)+ + move y0,y:(r5) +tks1: + move x:(r7+$10),x0 + move #>$01f2d5,y1 + mpy x0,y1,a + asr a + move a0,a + move #>$0002b5,x0 + add x0,a + move a1,x:(r7+$10) +; the get-up and the note's offset move the head back; it stops at the ring's end + move x:(r7+$05),x0 + move #>164,y1 + mpy x0,y1,a + move a1,x:(r7+$23) + move #>$7ff39e,y1 + mpy x0,y1,a + move a1,x:(r7+$05) + move x:(r7+$03),a + move x:(r7+$04),x0 + add x0,a + move x:(r7+$23),x0 + add x0,a + move #>$5da000,x0 + cmp x0,a + tgt x0,a + move a1,x:(r7+$03) + tfr a,b + asr #12,b,b + move b1,b + move x:(r7+$02),x0 + add x0,b + tfr b,a + move #>1500,x0 + sub x0,a + tge a,b + move x:(r7+$03),a + and #>$000fff,a + asl #11,a,a + move a1,x:(r7+$26) + move a1,x0 + move #>$7fffff,a + sub x0,a + move a1,x:(r7+$27) + asl b + move x:(r7+$00),x0 + add x0,b + move b1,r4 + nop +; the head: frames i and i + 1, interpolated + move y:(r4)+,x0 + move x:(r7+$27),y1 + mpy x0,y1,a + move y:(r4)+,x0 + mpy x0,y1,b + move x:(r7+$26),y1 + move y:(r4)+,x0 + mac x0,y1,a + move y:(r4),x0 + mac x0,y1,b + move a,x:(r7+$28) + move b,x:(r7+$29) +; TONE + move x:(r7+$0d),x0 + move x:(r7+$16),y1 + mpy x0,y1,a + move x:(r7+$28),x0 + move x:(r7+$15),y1 + mac x0,y1,a + move a,x:(r7+$0d) + move x:(r7+$0e),x0 + move x:(r7+$16),y1 + mpy x0,y1,a + move x:(r7+$29),x0 + move x:(r7+$15),y1 + mac x0,y1,a + move a,x:(r7+$0e) +; the envelope toward its target; the note's gain + move x:(r7+$07),a + move x:(r7+$06),x0 + sub x0,a + move a,x0 + move x:(r7+$1e),y1 + move x:(r7+$06),a + mac x0,y1,a + move a,x:(r7+$06) + move x:(r7+$06),x0 + move x:(r7+$0f),y1 + mpy x0,y1,a + move a1,x:(r7+$2a) +; the motor's hum: a 48 Hz sine's shape and a little of its octave + move x:(r7+$2f),a + move #>$004755,x0 + add x0,a + move a1,x:(r7+$2f) + move a1,x0 + move x0,a + abs a + move #>$7fffff,b + sub a,b + move b,y1 + mpy x0,y1,a + asl #2,a,a + move a,x:(r7+$39) + move x:(r7+$2f),a + asl a + move a1,x0 + move x0,a + abs a + move #>$7fffff,b + sub a,b + move b,y1 + mpy x0,y1,a + asl #2,a,a + move a,x0 + move #>$333333,y1 + mpy x0,y1,a + move x:(r7+$39),x0 + add x0,a + move a,x:(r7+$39) +; the tape's scrape: a tick of random size when the noise falls under MECH's threshold + move x:(r7+$10),b + and #>$3fffff,b + move b1,y0 + move x:(r7+$33),x0 + move x:(r7+$31),b + move x:(r7+$10),a + and #>$7fffff,a + move a1,a + cmp x0,a + tlt y0,b + move b1,x:(r7+$31) + move x:(r7+$32),b + move x:(r7+$10),a + and #>$7fffff,a + move a1,a + cmp x0,a + tlt y0,b + move b1,x:(r7+$32) +; the capstan: a tick at each turn + move x:(r7+$30),a + move #>$0003f0,x0 + add x0,a + tfr a,b + move #>$400000,x0 + sub x0,b + sub x0,b + tge b,a + move a1,x:(r7+$30) + move #>$4ccccd,y0 + move x:(r7+$31),b + tge y0,b + move b1,x:(r7+$31) + move x:(r7+$32),b + tge y0,b + move b1,x:(r7+$32) + move x:(r7+$31),x0 + move #>$7f6bbb,y1 + mpy x0,y1,a + move a1,x:(r7+$31) + move x:(r7+$32),x0 + move #>$7beb86,y1 + mpy x0,y1,a + move a1,x:(r7+$32) +; and the level's wobble with its turn + move x:(r7+$30),a + asl a + move #>$400000,x0 + sub x0,a + sub x0,a + move a1,x0 + move x0,a + abs a + move #>$7fffff,b + sub a,b + move b,y1 + mpy x0,y1,a + asl #2,a,a + move a,x0 + move x:(r7+$34),y1 + mpy x0,y1,a + move a1,y1 + move x:(r7+$2a),x0 + mpy x0,y1,a + add x0,a + move a1,x:(r7+$2a) +; mechanics: head noise and the thump's noise, filtered; the thump decays + move x:(r7+$0b),x0 + move #>$4864ec,y1 + mpy x0,y1,a + move x:(r7+$10),x0 + move #>$379b13,y1 + mac x0,y1,a + move a,x:(r7+$0b) + move x:(r7+$0c),x0 + move #>$7d4b21,y1 + mpy x0,y1,a + move x:(r7+$10),x0 + move #>$02b4de,y1 + mac x0,y1,a + move a,x:(r7+$0c) + move x:(r7+$2d),x0 + move #>$7d4b21,y1 + mpy x0,y1,a + move x:(r7+$0c),x0 + move #>$02b4de,y1 + mac x0,y1,a + move a,x:(r7+$2d) + move x:(r7+$0a),x0 + move #>$7fed6e,y1 + mpy x0,y1,a + move a1,x:(r7+$0a) + move x:(r7+$2e),x0 + move #>$7ed821,y1 + mpy x0,y1,a + move a1,x:(r7+$2e) + move a1,x0 + move x:(r7+$0a),a + sub x0,a + move a1,x0 + move x:(r7+$38),y1 + mpy x0,y1,a + move a1,y1 + move x:(r7+$2d),x0 + mpy x0,y1,a + asl #3,a,a + move a,x:(r7+$22) + move x:(r7+$06),x0 + move x:(r7+$2c),y1 + mpy x0,y1,b + move b1,x:(r7+$23) + move x:(r7+$17),y1 + mpy x0,y1,a + move a1,y1 + move x:(r7+$0b),x0 + move x:(r7+$22),a + mac x0,y1,a + move x:(r7+$23),y1 + move x:(r7+$0c),x0 + mac x0,y1,a + move x:(r7+$06),x0 + move x:(r7+$35),y1 + mpy x0,y1,b + move b1,y1 + move x:(r7+$39),x0 + mac x0,y1,a + move x:(r7+$06),x0 + move x:(r7+$36),y1 + mpy x0,y1,b + move b1,y1 + move x:(r7+$31),b + move x:(r7+$32),x0 + sub x0,b + move b1,x0 + mpy x0,y1,b + move b1,y1 + move x:(r7+$0b),x0 + mac x0,y1,a + move a,x:(r7+$22) +; out: the note x 2 + the mechanics, blended with the dry; before the first trig, the dry + move x:(r7+$0d),x0 + move x:(r7+$2a),y1 + mpy x0,y1,a + asl a + move a,x0 + move x:(r7+$22),a + add x0,a + move a,x0 + move x:(r7+$19),y1 + mpy x0,y1,a + move x:(r7+$20),x0 + move x:(r7+$1a),y1 + mac x0,y1,a + move x:(r7+$20),x1 + move x:(r7+$08),b + tst b + teq x1,a + move a,x:(r0)+ + move x:(r7+$0e),x0 + move x:(r7+$2a),y1 + mpy x0,y1,a + asl a + move a,x0 + move x:(r7+$22),a + add x0,a + move a,x0 + move x:(r7+$19),y1 + mpy x0,y1,a + move x:(r7+$21),x0 + move x:(r7+$1a),y1 + mac x0,y1,a + move x:(r7+$21),x1 + move x:(r7+$08),b + tst b + teq x1,a + move a,x:(r0)+ + move x:(r7+$02),a + move #>$1,x0 + sub x0,a + move #>1499,x0 + tlt x0,a + move a1,x:(r7+$02) +tksl: + nop + move x:(r7+$09),a + move x:(r7+$1f),x0 + add x0,a + move #>$400000,x0 + cmp x0,a + tgt x0,a + move a1,x:(r7+$09) + rts diff --git a/sdk/drafts/tapekeys/gen_engine.py b/sdk/drafts/tapekeys/gen_engine.py new file mode 100644 index 0000000000..5766d704cc --- /dev/null +++ b/sdk/drafts/tapekeys/gen_engine.py @@ -0,0 +1,253 @@ +#!/usr/bin/env python3 +"""Writes engine.asm from reference.py's constants: python3 -B gen_engine.py. verify.py holds the result to the +model bit for bit; edit this file, not engine.asm.""" +import pathlib +import sys + +HERE = pathlib.Path(__file__).resolve().parent +sys.path.insert(0, str(HERE)) +import reference as R # noqa: E402 + +# temporaries past the model's state words +KC, SEG, IL, IR, NZ, GG, OFF, WORD, F, NFT, TMPL, TMPR, GG2, R1T = (0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, + 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b) +T1, AFL = 0x39, 0x3a +STATE_WORDS = 0x3d + + +def h(v): + return "$%06x" % (v & 0xffffff) + + +def X(off): + return "x:(r7+$%02x)" % off + + +def L(*ins): + return "".join(" " + i + "\n" for i in ins) + + +HEADER = f"""; TAPEKEYS -- an original tape-replay keyboard effect for FX1 or FX2 of any track. +; Copyright (c) 2026 jrs1717, MIT (LICENSE). Written by gen_engine.py from reference.py. +; +; The track goes into a stereo ring in the slot's buffer ({R.NF} frames of L and R, +; the write frame counting down, frame 0 repeated after the ring as a guard; +; {R.R} words, which an FX1 slot's 3,072 holds). Each trig of the track (its frame +; word, r6+$18 on FX1 and r6+$12 on FX2: bit 15 in the trig's block, the low +; nibble its sample; a counter of the block's frames places it, as stock splits +; the calls at that sample) starts a note: the read head starts on the input +; and falls behind it by the note's own offset (flat, limited so the head stays +; in the ring for the whole TAPE) plus a decaying get-up (GTUP: up to 4 % flat, +; about 60 ms); the swell (SWEL) brings it in; TAPE seconds later the note is +; cut (5 ms) or faded (250 ms, END), to exact silence. MECH adds the running +; machine under the note (a little hiss and rumble, the motor's hum, capstan +; ticks and wobble, the tape's scrape) and a clunk when the tape runs out; +; CLIK the roller's rounded thunk at the trig. TONE: a one-pole low-pass. +; MIX blends with the dry track. Before the first trig the track passes dry. +; +; Knobs (value<<16): TAPE r6+0 (0-11: 1-12 s), SWEL r6+1, GTUP r6+2, SPRD r6+3, +; MECH r6+4, MIX r6+5; END slot 6 (r6+$c bits 16-23), TONE slot 7 (bits 8-15), +; CLIK slot 8 (r6+$d bits 16-23). +; +; State (X, offsets from r7; init clears $00..${STATE_WORDS - 1:02x}): see reference.py. Every +; product is the signed mpy/mac x0,y1 form; labels are tk and two characters. +; CYCLES_FORWARD_BRANCHES +""" + + +def lcg(): + """The noise word: n = n K + C (mod 2^24).""" + return L(f"move {X(R.NOISE)},x0", f"move #>{h(R.LCG_K)},y1", "mpy x0,y1,a", "asr a", "move a0,a", + f"move #>{h(R.LCG_C)},x0", "add x0,a", f"move a1,{X(R.NOISE)}") + + +def wave(): + """a = 4 x0 (1 - |x0|), a sine's shape (move it out with limiting).""" + return L("move x0,a", "abs a", "move #>$7fffff,b", "sub a,b", "move b,y1", "mpy x0,y1,a", "asl #2,a,a") + + +def init(): + s = "init:\n" + L("clr a", "move r7,r5", "move #>$ffffff,m5", f"do #${STATE_WORDS:x},>tki0", "move a,x:(r5)+") + s += "tki0:\n" + L("nop", "move x:>$213,r4", "move #>$ffffff,m4", "nop", "move x:(r4),x0", + f"move x0,{X(R.BASE)}", "move x0,a", "move #>$4000,x0", "sub x0,a", "clr b", + "move #>$1,x0", "tst a", "tlt x0,b", f"move b1,{X(R.FX1)}", + f"move {X(R.BASE)},r4", "clr a", f"move #>{R.R},x0", "do x0,>tki1", "move a,y:(r4)+") + s += "tki1:\n" + L("nop", "rts") + return s + + +def knobs(): + s = "; the knobs, every call\n" + s += L("move x:(r6+$0),a", "and #>$7f0000,a", "move #>$0b0000,x0", "cmp x0,a", "tgt x0,a", + "asr #16,a,a", f"move a1,{X(R.TAPE)}", "move #>$1,x0", "add x0,a", "move a1,x0", + "move #>44100,y1", "mpy x0,y1,a", "asr a", f"move a0,{X(R.TS)}") + s += "; SWEL: k = slow + u^4 (fast - slow), u = 1 - SWEL\n" + s += L("move #>$7f0000,a", "move x:(r6+$1),b", "and #>$7f0000,b", "move b1,x0", "sub x0,a", + "move a1,x0", "move a1,y1", "mpy x0,y1,a", "move a1,x0", "move a1,y1", "mpy x0,y1,a", + "move a1,x0", f"move #>{h(R.KS_D)},y1", "mpy x0,y1,a", f"move #>{h(R.KS_SLOW)},x0", "add x0,a", + f"move a1,{X(R.KS)}") + s += L("move x:(r6+$2),a", "and #>$7f0000,a", f"move a1,{X(R.G0)}", + "move x:(r6+$3),a", "and #>$7f0000,a", f"move a1,{X(R.SPR)}", + "move x:(r6+$4),a", "and #>$7f0000,a", "move a1,x0", f"move #>{h(R.HEADK)},y1", "mpy x0,y1,a", + f"move a1,{X(R.HM)}", f"move #>{h(R.RUMK)},y1", "mpy x0,y1,a", f"move a1,{X(R.RM)}", + f"move #>{h(R.HUMK)},y1", "mpy x0,y1,a", f"move a1,{X(R.HUM)}", f"move #>{h(R.TICKK)},y1", + "mpy x0,y1,a", f"move a1,{X(R.TKL)}", f"move #>{R.SCRK},y1", "mpy x0,y1,a", f"move a1,{X(R.THR)}", + f"move #>{h(R.WDK)},y1", "mpy x0,y1,a", f"move a1,{X(R.WD)}", f"move #>{h(R.CLUNKK)},y1", + "mpy x0,y1,a", f"move a1,{X(R.CM)}", + "move x:(r6+$d),a", "and #>$7f0000,a", "move a1,x0", f"move #>{h(R.THK)},y1", "mpy x0,y1,a", + f"move a1,{X(R.TM)}") + s += "; MIX: 127 is the tape alone, else mx + mx / 128\n" + s += L("move x:(r6+$5),a", "and #>$7f0000,a", "tfr a,b", "asr #7,b,b", "add a,b", + "move #>$7fffff,y0", "move #>$7f0000,x0", "cmp x0,a", "teq y0,b", f"move b1,{X(R.MX)}", + "move b1,x0", "move #>$7fffff,a", "sub x0,a", f"move a1,{X(R.DRY)}") + s += L("move x:(r6+$c),a", "and #>$00ff00,a", "asl #8,a,a", "move a1,x0", f"move #>{h(R.KT_D)},y1", "mpy x0,y1,a", + f"move #>{h(R.KT_LO)},x0", "add x0,a", f"move a1,{X(R.KT)}", "move a1,x0", "move #>$7fffff,a", + "sub x0,a", f"move a1,{X(R.NKT)}", + f"move #>{h(R.KCUT)},b", f"move #>{h(R.KFADE)},y0", "move x:(r6+$c),a", "and #>$7f0000,a", + "tne y0,b", f"move b1,{X(R.KE)}") + return s + + +def trig_and_segments(): + s = "; the trig: the word of this slot; its sample against where this call starts in the block (the a = 0 call\n" + s += "; at 0, the a = 1 call at 16 - n7), so no count runs from call to call\n" + s += L(f"move {X(R.FX1)},b", "tst b", "move x:(r6+$12),a", "beq tkw1", "move x:(r6+$18),a") + s += "tkw1:\n" + L(f"move a1,{X(WORD)}", "and #>$f,a", f"move {X(AFL)},b", "tst b", "beq tkw2", "move #>$10,b", + "move n7,x0", "sub x0,b", "move b1,x0", "sub x0,a") + s += "tkw2:\n" + L(f"move a1,{X(OFF)}") + s += "; a trig fires once: at its sample in this call, at once when past, at the next call's start when later\n" + s += L(f"move {X(WORD)},a", f"and #>{h(R.TRIG)},a", "bne tkb1", "clr a", f"move a,{X(R.LATCH)}", + f"move {X(R.PEND)},a", "tst a", "beq tkno", "clr b", f"move b,{X(R.PEND)}", f"move b,{X(OFF)}", + "bra tkfi") + s += "tkb1:\n" + L(f"move {X(R.LATCH)},a", "tst a", "bne tkno", f"move {X(OFF)},a", "clr b", "tst a", + "tlt b,a", f"move a1,{X(OFF)}", "move n7,x0", "cmp x0,a", "blt tkfr", "move #>$1,x0", + f"move x0,{X(R.PEND)}", "bra tkno") + s += "tkfr:\n" + L("move #>$1,x0", f"move x0,{X(R.LATCH)}", "clr b", f"move b,{X(R.PEND)}") + s += "tkfi:\n" + L(f"move {X(OFF)},a", "tst a", "beq tkt0", f"move a1,{X(SEG)}", "bsr tksg") + s += "tkt0:\n" + L("bsr tktr", "move n7,a", f"move {X(OFF)},x0", "sub x0,a", f"move a1,{X(SEG)}", "bsr tksg", + "rts") + s += "tkno:\n" + L("move n7,x0", f"move x0,{X(SEG)}", "bsr tksg", "rts") + return s + + +def trig(): + s = "; a note starts: its offset (flat, at most the TAPE's cap) and level from two noise words\n" + s += "tktr:\n" + lcg() + L(f"move a1,{X(R1T)}") + s += L(f"move {X(R.TAPE)},a", f"move #>{R.CAPS[0]},b") + for t in range(1, 12): + s += L(f"move #>{t},x0", "cmp x0,a", f"move #>{R.CAPS[t]},y0", "teq y0,b") + s += L(f"move {X(R.SPR)},x0", f"move #>{R.SPRK},y1", "mpy x0,y1,a", "move a1,y1", f"move {X(R1T)},a", + "and #>$7fffff,a", "move a1,x0", "mpy x0,y1,a", "cmp b,a", "tgt b,a", f"move a1,{X(R.O)}") + s += lcg() + s += L(f"move {X(R.SPR)},x0", f"move #>{h(R.LVK)},y1", "mpy x0,y1,a", "move a1,y1", f"move {X(R.NOISE)},x0", + "mpy x0,y1,a", "move #>$400000,x0", "add x0,a", f"move a1,{X(R.LVL)}", + "clr a", f"move a,{X(R.D)}", f"move a,{X(R.ENV)}", f"move a,{X(R.CNT)}", f"move {X(R.G0)},x0", + f"move x0,{X(R.G)}", "move #>$7fffff,x0", f"move x0,{X(R.TGT)}", f"move x0,{X(R.TH)}", "move #>$1,x0", + f"move x0,{X(R.STARTED)}", "move #>$7fffff,x0", f"move x0,{X(R.TA)}", + f"move {X(R.TM)},x0", f"move x0,{X(R.TLV)}", "clr a", f"move a,{X(R.CP)}", "rts") + return s + + +def segment(): + s = "; a run of frames: the tape's end, the envelope's rate, the samples, the count\n" + s += "tksg:\n" + L(f"move {X(R.CNT)},a", f"move {X(R.TS)},x0", "cmp x0,a", "blt tkg1", f"move {X(R.TGT)},a", + "tst a", "beq tkg1", "clr b", f"move b,{X(R.TGT)}", "move #>$7fffff,x0", + f"move x0,{X(R.TH)}", f"move x0,{X(R.TA)}", f"move {X(R.CM)},x0", f"move x0,{X(R.TLV)}") + s += "tkg1:\n" + L(f"move {X(R.TGT)},a", "tst a", "bne tkg2", f"move {X(R.ENV)},a", f"move #>{h(R.SILENT)},x0", + "cmp x0,a", "bge tkg2", "clr a", f"move a,{X(R.ENV)}") + s += "tkg2:\n" + L(f"move {X(R.KS)},b", f"move {X(R.TGT)},a", "tst a", "bne tkg3", f"move {X(R.KE)},b") + s += "tkg3:\n" + L(f"move b1,{X(KC)}", "move #>$1,n0", f"move {X(SEG)},x0", "do x0,>tksl") + s += sample() + s += "tksl:\n" + L("nop", f"move {X(R.CNT)},a", f"move {X(SEG)},x0", "add x0,a", "move #>$400000,x0", "cmp x0,a", + "tgt x0,a", f"move a1,{X(R.CNT)}", "rts") + return s + + +def sample(): + s = "; the frame into the ring (frame 0 also into the guard)\n" + s += L("move x:(r0),x1", "move x:(r0+n0),y0", f"move x1,{X(IL)}", f"move y0,{X(IR)}", f"move {X(R.W)},a", + "asl a", f"move {X(R.BASE)},x0", "add x0,a", "move a1,r4", "nop", "move x1,y:(r4)+", "move y0,y:(r4)", + f"move {X(R.W)},a", "tst a", "bne tks1", f"move {X(R.BASE)},a", f"move #>{R.GUARD},x0", "add x0,a", + "move a1,r5", "nop", "move x1,y:(r5)+", "move y0,y:(r5)") + s += "tks1:\n" + lcg() + s += "; the get-up and the note's offset move the head back; it stops at the ring's end\n" + s += L(f"move {X(R.G)},x0", f"move #>{R.GDEP},y1", "mpy x0,y1,a", f"move a1,{X(GG)}", f"move #>{h(R.KG)},y1", + "mpy x0,y1,a", f"move a1,{X(R.G)}", f"move {X(R.D)},a", f"move {X(R.O)},x0", "add x0,a", + f"move {X(GG)},x0", "add x0,a", f"move #>{h(R.DMAXQ)},x0", "cmp x0,a", "tgt x0,a", + f"move a1,{X(R.D)}", "tfr a,b", "asr #12,b,b", "move b1,b", f"move {X(R.W)},x0", "add x0,b", + "tfr b,a", f"move #>{R.NF},x0", "sub x0,a", "tge a,b", + f"move {X(R.D)},a", "and #>$000fff,a", "asl #11,a,a", f"move a1,{X(F)}", "move a1,x0", + "move #>$7fffff,a", "sub x0,a", f"move a1,{X(NFT)}", + "asl b", f"move {X(R.BASE)},x0", "add x0,b", "move b1,r4", "nop") + s += "; the head: frames i and i + 1, interpolated\n" + s += L("move y:(r4)+,x0", f"move {X(NFT)},y1", "mpy x0,y1,a", "move y:(r4)+,x0", "mpy x0,y1,b", + f"move {X(F)},y1", "move y:(r4)+,x0", "mac x0,y1,a", "move y:(r4),x0", "mac x0,y1,b", + f"move a,{X(TMPL)}", f"move b,{X(TMPR)}") + s += "; TONE\n" + for st, tmp in ((R.FL, TMPL), (R.FR, TMPR)): + s += L(f"move {X(st)},x0", f"move {X(R.NKT)},y1", "mpy x0,y1,a", f"move {X(tmp)},x0", f"move {X(R.KT)},y1", + "mac x0,y1,a", f"move a,{X(st)}") + s += "; the envelope toward its target; the note's gain\n" + s += L(f"move {X(R.TGT)},a", f"move {X(R.ENV)},x0", "sub x0,a", "move a,x0", f"move {X(KC)},y1", + f"move {X(R.ENV)},a", "mac x0,y1,a", f"move a,{X(R.ENV)}", f"move {X(R.ENV)},x0", f"move {X(R.LVL)},y1", + "mpy x0,y1,a", f"move a1,{X(GG2)}") + s += "; the motor's hum: a 48 Hz sine's shape and a little of its octave\n" + s += L(f"move {X(R.HP)},a", f"move #>{h(R.HINC)},x0", "add x0,a", f"move a1,{X(R.HP)}", "move a1,x0") + wave() + s += L(f"move a,{X(T1)}", f"move {X(R.HP)},a", "asl a", "move a1,x0") + wave() + s += L("move a,x0", f"move #>{h(R.H2K)},y1", "mpy x0,y1,a", f"move {X(T1)},x0", "add x0,a", f"move a,{X(T1)}") + s += "; the tape's scrape: a tick of random size when the noise falls under MECH's threshold\n" + s += L(f"move {X(R.NOISE)},b", "and #>$3fffff,b", "move b1,y0", f"move {X(R.THR)},x0") + for st in (R.TK, R.TK2): + s += L(f"move {X(st)},b", f"move {X(R.NOISE)},a", "and #>$7fffff,a", "move a1,a", "cmp x0,a", "tlt y0,b", f"move b1,{X(st)}") + s += "; the capstan: a tick at each turn\n" + s += L(f"move {X(R.CP)},a", f"move #>{h(R.CINC)},x0", "add x0,a", "tfr a,b", "move #>$400000,x0", "sub x0,b", + "sub x0,b", "tge b,a", f"move a1,{X(R.CP)}", f"move #>{h(R.CAPA)},y0", f"move {X(R.TK)},b", "tge y0,b", + f"move b1,{X(R.TK)}", f"move {X(R.TK2)},b", "tge y0,b", f"move b1,{X(R.TK2)}") + for st, k in ((R.TK, R.KTK), (R.TK2, R.KTK2)): + s += L(f"move {X(st)},x0", f"move #>{h(k)},y1", "mpy x0,y1,a", f"move a1,{X(st)}") + s += "; and the level's wobble with its turn\n" + s += L(f"move {X(R.CP)},a", "asl a", "move #>$400000,x0", "sub x0,a", "sub x0,a", "move a1,x0") + wave() + s += L("move a,x0", f"move {X(R.WD)},y1", "mpy x0,y1,a", "move a1,y1", f"move {X(GG2)},x0", "mpy x0,y1,a", + "add x0,a", f"move a1,{X(GG2)}") + s += "; mechanics: head noise and the thump's noise, filtered; the thump decays\n" + for st, nk, k in ((R.HL, R.NKH, R.KH), (R.TL, R.NKTL, R.KTL)): + s += L(f"move {X(st)},x0", f"move #>{h(nk)},y1", "mpy x0,y1,a", f"move {X(R.NOISE)},x0", f"move #>{h(k)},y1", + "mac x0,y1,a", f"move a,{X(st)}") + s += L(f"move {X(R.TL2)},x0", f"move #>{h(R.NKTL)},y1", "mpy x0,y1,a", f"move {X(R.TL)},x0", + f"move #>{h(R.KTL)},y1", "mac x0,y1,a", f"move a,{X(R.TL2)}", + f"move {X(R.TH)},x0", f"move #>{h(R.KTH)},y1", "mpy x0,y1,a", f"move a1,{X(R.TH)}", + f"move {X(R.TA)},x0", f"move #>{h(R.KTA)},y1", "mpy x0,y1,a", f"move a1,{X(R.TA)}", + "move a1,x0", f"move {X(R.TH)},a", "sub x0,a", "move a1,x0", f"move {X(R.TLV)},y1", "mpy x0,y1,a", + "move a1,y1", f"move {X(R.TL2)},x0", "mpy x0,y1,a", "asl #3,a,a", f"move a,{X(NZ)}", + f"move {X(R.ENV)},x0", f"move {X(R.RM)},y1", "mpy x0,y1,b", f"move b1,{X(GG)}", + f"move {X(R.HM)},y1", "mpy x0,y1,a", "move a1,y1", f"move {X(R.HL)},x0", f"move {X(NZ)},a", + "mac x0,y1,a", f"move {X(GG)},y1", f"move {X(R.TL)},x0", "mac x0,y1,a", + f"move {X(R.ENV)},x0", f"move {X(R.HUM)},y1", "mpy x0,y1,b", "move b1,y1", f"move {X(T1)},x0", + "mac x0,y1,a", f"move {X(R.ENV)},x0", f"move {X(R.TKL)},y1", "mpy x0,y1,b", "move b1,y1", + f"move {X(R.TK)},b", f"move {X(R.TK2)},x0", "sub x0,b", "move b1,x0", "mpy x0,y1,b", "move b1,y1", + f"move {X(R.HL)},x0", "mac x0,y1,a", f"move a,{X(NZ)}") + s += "; out: the note x 2 + the mechanics, blended with the dry; before the first trig, the dry\n" + for st, inp in ((R.FL, IL), (R.FR, IR)): + s += L(f"move {X(st)},x0", f"move {X(GG2)},y1", "mpy x0,y1,a", "asl a", "move a,x0", f"move {X(NZ)},a", + "add x0,a", "move a,x0", f"move {X(R.MX)},y1", "mpy x0,y1,a", f"move {X(inp)},x0", + f"move {X(R.DRY)},y1", "mac x0,y1,a", f"move {X(inp)},x1", f"move {X(R.STARTED)},b", "tst b", + "teq x1,a", "move a,x:(r0)+") + s += L(f"move {X(R.W)},a", "move #>$1,x0", "sub x0,a", f"move #>{R.NF - 1},x0", "tlt x0,a", + f"move a1,{X(R.W)}") + return s + + +def proc(): + s = "proc:\n" + L(f"move a1,{X(AFL)}", "move n7,b", "tst b", "bgt tkp1", "rts") + s += "tkp1:\n" + L("move #>$ffffff,m4", "move #>$ffffff,m5") + return s + knobs() + trig_and_segments() + + +def main(): + src = HEADER + init() + proc() + trig() + segment() + (HERE / "engine.asm").write_text(src) + print("wrote engine.asm,", src.count("\n"), "lines") + + +if __name__ == "__main__": + main() diff --git a/sdk/drafts/tapekeys/manifest.py b/sdk/drafts/tapekeys/manifest.py new file mode 100644 index 0000000000..0848bcf563 --- /dev/null +++ b/sdk/drafts/tapekeys/manifest.py @@ -0,0 +1,85 @@ +"""TAPEKEYS -- an original tape-replay keyboard effect for FX1 or FX2 of any track. + +Each trig of the track starts a note on its own strip of tape. The track plays through a short stereo ring whose read +head starts on the input at the trig and falls behind it: the note starts flat and rises to pitch as the pinch roller +grabs (GTUP), plays slightly flat by its own random amount and at its own level (SPRD), swells in (SWEL) and ends +when the tape runs out, TAPE seconds after the trig (END: cut or faded). MECH adds the running tape (head hiss and a +low rumble) and CLIK the roller's thunk at each trig; TONE rolls off the top; MIX blends with the dry track. Before the first trig the track +passes dry. Works on FX1 or FX2: the ring is 3,002 words, inside an FX1 slot's 3,072. + +verify.py holds engine.asm (written by gen_engine.py) to reference.py's model bit for bit in dsp_host. Not yet heard +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("tapekeys_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 +D = dict(zip(_ref.KNOBS, _ref.DEFAULTS)) +_blank = dict(active=False, formatter=P, doc="not used") + +MODULE = Module( + name="tapekeys", + key="TAPEKEYS", + kind=Kind.DSP_EFFECT, + doc="Tape-replay keyboard on FX1 or FX2: each trig a note on its own tape, flat get-up, its own offset, a swell, and the tape running out.", + category=Category.TRACK, author="jrs1717", author_url="https://github.com/jrs1717", + proof=Proof.RENDER, + proof_note="verify.py bit-exact against reference.py in dsp_host; not yet heard on a unit", + + menu=MenuEntry( + fx2_id=0x0a, + donor_desc=0x400d58b8, # DARK REV; every drawn slot is written below + abbr=b"TKEY", + fullname=b"TAPEKEYS", + build_tag=False, + ), + mode_slot=6, + + params=( + Param(b"TAPE", D["TAPE"], 12, active=True, formatter=W, labels=_ref.TAPE_LABELS, + doc="the tape's length: the note ends 1 to 12 seconds after its trig"), + Param(b"SWEL", D["SWEL"], 128, active=True, formatter=P, + doc="the swell: how slowly a note comes in, from 2 ms to half a second"), + Param(b"GTUP", D["GTUP"], 128, active=True, formatter=P, + doc="the get-up: how flat a note starts (up to 4 %) before the roller brings it to pitch"), + Param(b"SPRD", D["SPRD"], 128, active=True, formatter=P, + doc="each note's own tape: up to about 12 cents flat and 2 dB up or down, new on every trig"), + Param(b"MECH", D["MECH"], 128, active=True, formatter=P, + doc="the running machine: a little hiss and rumble, the motor's hum, capstan ticks and wobble, the tape's scrape, and a clunk when the tape runs out"), + Param(b"MIX", D["MIX"], 128, active=True, formatter=P, + doc="dry/tape blend; 127 the tape alone"), + Param(b"END", D["END"], 2, active=True, formatter=S, labels=_ref.END_NAMES, + doc="how the tape runs out: CUT stops the note in 5 ms, FADE lets it die over a quarter second"), + Param(b"TONE", D["TONE"], 128, active=True, formatter=P, + doc="darkens the tape: a low-pass from 1.5 kHz at 0 to 16 kHz at 127"), + Param(b"CLIK", D["CLIK"], 128, active=True, formatter=P, + doc="the roller's thunk at each trig, rounded (no click); 0 none"), + Param(b"", 0, 128, **_blank), + Param(b"", 0, 128, **_blank), + Param(b"", 0, 128, **_blank), + ), + + dsp=DspSection( + asm="modules/tapekeys/engine.asm", + priority=10, + bus_role=BusRole.NONE, + ybase=YBase.NEVER, + r7_latch_slot=None, + gate_label=None, + ), + # 3,002 words from the slot's base: the ring (1,500 stereo frames) and a guard frame; it fits an FX1 slot. + claims=Claims(stock_instance_buffer=True, buffer_words=_ref.R), + + harness=Harness(layout_char=None, is_server=False), + + gates=(Gate("modules/tapekeys/verify.py", remix_arg=False),), + dear={k: v for k, v in zip(_ref.KNOBS, _ref.DEAREST)}, +) diff --git a/sdk/drafts/tapekeys/media/ot-controls.png b/sdk/drafts/tapekeys/media/ot-controls.png new file mode 100644 index 0000000000..e07177835f Binary files /dev/null and b/sdk/drafts/tapekeys/media/ot-controls.png differ diff --git a/sdk/drafts/tapekeys/media/ot-location.png b/sdk/drafts/tapekeys/media/ot-location.png new file mode 100644 index 0000000000..72b6e351e2 Binary files /dev/null and b/sdk/drafts/tapekeys/media/ot-location.png differ diff --git a/sdk/drafts/tapekeys/octamod.module.json b/sdk/drafts/tapekeys/octamod.module.json new file mode 100644 index 0000000000..af864c099d --- /dev/null +++ b/sdk/drafts/tapekeys/octamod.module.json @@ -0,0 +1,165 @@ +{ + "schemaVersion": 2, + "id": "tapekeys", + "key": "TAPEKEYS", + "name": "Tapekeys", + "version": "0.1.0-experimental", + "category": "effects", + "author": { + "github": "jrs1717", + "credits": [ + "jrs1717" + ] + }, + "nativeManifest": "manifest.py", + "presentation": { + "label": "TAPEKEYS", + "family": "Tape", + "summary": "A tape-replay keyboard for FX1 or FX2: each trig a note on its own tape, with a flat get-up, its own pitch and level, a swell, and the tape running out.", + "overview": "Each trig of the track starts a note on its own strip of tape: it starts flat and rises to pitch, plays slightly flat at its own level, swells in, and ends when the tape runs out. MECH adds the roller thump and head noise.", + "highlights": [ + "Every note slightly different, as each key had its own tape", + "The tape runs out after 1 to 12 seconds, cut or faded", + "A flat get-up and a swell at every trig" + ], + "usage": [ + "FUNC+FX1 or FX2, choose TAPEKEYS; play held strings, choir or flute chromatically." + ] + }, + "controls": [ + { + "name": "TAPE", + "default": 7, + "count": 12, + "doc": "The tape's length: the note ends 1 to 12 seconds after its trig.", + "labels": [ + "1s", + "2s", + "3s", + "4s", + "5s", + "6s", + "7s", + "8s", + "9s", + "10s", + "11s", + "12s" + ] + }, + { + "name": "SWEL", + "default": 30, + "count": 128, + "doc": "The swell: from instant (2 ms) to about half a second.", + "labels": null + }, + { + "name": "GTUP", + "default": 40, + "count": 128, + "doc": "The get-up: how flat a note starts (up to 4 %) before rising to pitch.", + "labels": null + }, + { + "name": "SPRD", + "default": 50, + "count": 128, + "doc": "Each note's own tape: up to about 12 cents flat and 2 dB up or down.", + "labels": null + }, + { + "name": "MECH", + "default": 30, + "count": 128, + "doc": "The running machine: a little hiss and rumble, the motor's hum, capstan ticks and wobble, the tape's scrape, and a clunk when the tape runs out.", + "labels": null + }, + { + "name": "MIX", + "default": 127, + "count": 128, + "doc": "Dry/tape blend; 127 the tape alone.", + "labels": null + }, + { + "name": "END", + "default": 0, + "count": 2, + "doc": "CUT stops the note in 5 ms; FADE lets it die over a quarter second.", + "labels": [ + "CUT", + "FADE" + ] + }, + { + "name": "TONE", + "default": 90, + "count": 128, + "doc": "A low-pass from 1.5 kHz at 0 to 16 kHz at 127.", + "labels": null + }, + { + "name": "CLIK", + "default": 30, + "count": 128, + "doc": "The roller's thunk at each trig, rounded so it doesn't click; 0 none.", + "labels": null + } + ], + "compatibility": { + "firmware": "1.40C", + "location": "FX1 / FX2", + "conflicts": [], + "uiClaims": [], + "limitations": [ + "Draft: bit for bit against its model in dsp_host; not yet heard or tested on a unit." + ], + "effectId": 10 + }, + "resources": { + "recorded": "Not measured", + "storage": { + "label": "Storage", + "display": "Not measured", + "value": null, + "unit": "unknown", + "method": "unmeasured", + "conditions": "No measurements exist yet. Add exact conditions and source evidence when available.", + "source": "TESTING.md" + }, + "processing": { + "label": "CPU / DSP load", + "display": "Not measured", + "value": null, + "unit": "unknown", + "method": "unmeasured", + "conditions": "No measurements exist yet. Add exact conditions and source evidence when available.", + "source": "TESTING.md" + } + }, + "tests": { + "report": "TESTING.md", + "summary": "No tests have been run. Add tests.qualification with worst-case cycles, exact memory and attributed owner-reviewed hardware evidence before submitting. This scaffold cannot pass publication checks.", + "hardwareStatus": "untested", + "evidenceRevision": "b8deefc88b2c3e5f3c6158e364eb741df1924e1d", + "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" + ] + }, + "license": { + "spdx": "MIT", + "file": "LICENSE", + "declaration": "Replace with accurate authorship, attribution and original/licensed source and media rights. Never include Elektron firmware, extracted routines or tables." + }, + "media": [], + "access": { + "location": "Describe the exact OT page or menu where the module is selected or enabled.", + "steps": [ + "Describe prerequisites, track/machine selection and the exact button/menu sequence." + ], + "screenshots": [] + } +} diff --git a/sdk/drafts/tapekeys/qualification.example.json b/sdk/drafts/tapekeys/qualification.example.json new file mode 100644 index 0000000000..f24ba06cd4 --- /dev/null +++ b/sdk/drafts/tapekeys/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/tapekeys/reference.py b/sdk/drafts/tapekeys/reference.py new file mode 100644 index 0000000000..4d2051ad68 --- /dev/null +++ b/sdk/drafts/tapekeys/reference.py @@ -0,0 +1,317 @@ +# SPDX-License-Identifier: MIT +"""Tapekeys, the reference model: an original tape-replay keyboard effect for FX1 or FX2 of any track. + +Each trig of the track starts a note on its own strip of tape. The effect plays the track through a short stereo ring +with a read head that starts on the input at the trig and then falls behind it: +- the get-up: the note starts flat (up to 4 %, GTUP) and rises to pitch over about 60 ms, as the pinch roller grabs; +- the note's own offset: each note plays slightly flat by its own random amount and at its own level (SPRD), as each + key had its own worn tape; the offset is limited so the head never outruns the ring over the whole TAPE length; +- a swell (SWEL) brings the note in, from 2 ms to half a second; +- the tape runs out: TAPE seconds after the trig the note ends, cut or faded (END); +- mechanics: MECH, the running machine under the note: a little hiss and rumble, the motor's hum, a soft tick and a + slight level wobble at each turn of the capstan, the tape's sparse scrape, and a clunk when the tape runs out and + springs back; CLIK, the roller's thunk at each trig, rounded (it rises over about 3 ms and dies over 40 ms); +- TONE rolls off the top; MIX blends with the dry track. + +Before the track's first trig it passes dry. Integer arithmetic as the DSP does it: engine.asm matches it bit for bit. +""" +import math + +FS = 44100.0 +Q = 1 << 23 + +KNOBS = ("TAPE", "SWEL", "GTUP", "SPRD", "MECH", "MIX", "END", "TONE", "CLIK") +COUNTS = (12, 128, 128, 128, 128, 128, 2, 128, 128) +DEFAULTS = (7, 30, 40, 50, 30, 127, 0, 90, 30) +DEAREST = (11, 127, 127, 127, 127, 64, 1, 127, 127) +END_SLOT = 6 +TAPE_LABELS = tuple(f"{i + 1}s" for i in range(12)) +END_NAMES = ("CUT", "FADE") + +NF = 1500 # frames in the ring (L then R); a guard frame after it repeats frame 0 +GUARD = 2 * NF +R = 2 * NF + 2 # the buffer words the effect claims (an FX1 slot gives 3,072) +DMAXQ = (NF - 2) << 12 # the head's furthest delay, frames in Q12 +TRIG = 0x8000 # the track's frame word: bit 15 in a trig's frame, the low nibble its sample +FULL = 0x7fffff + + +def q23(v): + return max(-Q, min(Q - 1, int(round(v * Q)))) + + +def onepole(hz): + return 1.0 - math.exp(-2 * math.pi * hz / FS) + + +GDEP = 164 # the get-up's start: 164 / 4096 of a frame a sample, 4 % flat +KG = q23(math.exp(-1 / (0.06 * FS))) # the get-up decays over about 60 ms +SPRK = 29 # SPRD: a note's offset up to 28 / 4096 a sample (about 12 cents) +LVK = q23(0.126) # SPRD: a note's level up to +-0.125 about 0.5 (about +-2 dB) +GT = int(GDEP / (1 - KG / Q)) + 1 # the most the get-up adds to the delay +CAPN = (DMAXQ - GT) // 44100 +CAPS = tuple(CAPN // (t + 1) for t in range(12)) # a note's offset at most, so the head stays in the ring +KS_SLOW = q23(1 / (0.5 * FS)) +KS_D = q23(1 / (0.002 * FS)) - KS_SLOW # SWEL: k = slow + (1 - SWEL)^4 (fast - slow) +KCUT = q23(1 / (0.005 * FS)) +KFADE = q23(1 / (0.25 * FS)) +KT_LO = q23(onepole(1500)) +KT_D = q23(onepole(16000)) - KT_LO +KH = q23(onepole(4000)) # head noise: white noise through 4 kHz +NKH = FULL - KH +KTL = q23(onepole(150)) # the rumble: noise through 150 Hz; the thunk: through it twice +NKTL = FULL - KTL +KTH = q23(math.exp(-1 / (0.04 * FS))) # the thunk dies over about 40 ms +KTA = q23(math.exp(-1 / (0.0025 * FS))) # and rises over about 3 ms (the difference of two decays) +HEADK = q23(0.0075) +RUMK = q23(0.1) +THK = q23(0.2) +HUMK = q23(0.004) # the motor: a 48 Hz hum and a little of its octave +H2K = q23(0.4) +HINC = int(round(48 / FS * (1 << 24))) +CINC = int(round(5.3 / FS * (1 << 23))) # the capstan turns about 5.3 times a second +CAPA = q23(0.6) # its tick's size; a scrape's is random, up to 0.5 +KTK = q23(math.exp(-1 / (0.005 * FS))) # a tick dies over 5 ms and rises over 0.7 ms +KTK2 = q23(math.exp(-1 / (0.0007 * FS))) +TICKK = q23(0.05) +SCRK = 4800 # scrapes: up to 4800 / 2^23 a sample (about 25 a second) +WDK = q23(0.04) # the capstan's level wobble: up to +-4 % +CLUNKK = q23(0.15) # the return clunk's size at MECH full +ONE = FULL +LCG_K = 0x01f2d5 +LCG_C = 0x0002b5 +SILENT = 0x2000 # a faded note under this is cut to 0 + + +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 wave(t): + """4 t (1 - |t|): a sine's shape from a phase in [-1, 1).""" + return lim(w56(mpy(t, lim(ld(ONE) - abs(ld(t)))) << 2)) + + +# state words, X:(r7 + offset) +BASE, FX1, W, D, O, G, ENV, TGT, STARTED, CNT, TH, HL, TL, FL, FR, LVL, NOISE, POS = range(18) +TS, KS, KE, KT, NKT, HM, TM, MX, DRY, G0, SPR, TAPE = range(0x12, 0x1e) +RM, TL2, TA, HP, CP, TK, TK2, THR, WD, HUM, TKL, CM, TLV = range(0x2c, 0x39) +LATCH, PEND = 0x3b, 0x3c +STATE = 0x3d + + +class Model: + def __init__(self, base=0x4000): + self.x = [0] * STATE + self.x[BASE] = base + self.x[FX1] = 1 if base < 0x4000 else 0 + self.y = [0] * R + + def lcg(self): + x = self.x + x[NOISE] = (s24(x[NOISE]) * LCG_K + LCG_C) & 0xffffff + return x[NOISE] + + def knobs(self, p): + x = self.x + t = min((p.get(0, 0) & 0x7f0000) >> 16, 11) + x[TAPE] = t + x[TS] = (t + 1) * 44100 + u = (127 << 16) - (p.get(1, 0) & 0x7f0000) + u2 = hi(mpy(u, u)) + u4 = hi(mpy(u2, u2)) + x[KS] = KS_SLOW + hi(mpy(u4, KS_D)) + x[G0] = p.get(2, 0) & 0x7f0000 + x[SPR] = p.get(3, 0) & 0x7f0000 + m = p.get(4, 0) & 0x7f0000 + x[HM] = hi(mpy(m, HEADK)) + x[RM] = hi(mpy(m, RUMK)) + x[HUM] = hi(mpy(m, HUMK)) + x[TKL] = hi(mpy(m, TICKK)) + x[THR] = hi(mpy(m, SCRK)) + x[WD] = hi(mpy(m, WDK)) + x[CM] = hi(mpy(m, CLUNKK)) + x[TM] = hi(mpy(p.get(0xd, 0) & 0x7f0000, THK)) + mx = p.get(5, 0) & 0x7f0000 + x[MX] = FULL if mx == 0x7f0000 else mx + (mx >> 7) # 127 is the tape alone + x[DRY] = FULL - x[MX] + x[KT] = KT_LO + hi(mpy((p.get(0xc, 0) & 0x00ff00) << 8, KT_D)) + x[NKT] = FULL - x[KT] + x[KE] = KFADE if p.get(0xc, 0) & 0x7f0000 else KCUT + + def trig(self): + x = self.x + r1 = self.lcg() + r2 = self.lcg() + o = hi(mpy(r1 & 0x7fffff, hi(mpy(x[SPR], SPRK)))) + x[O] = min(o, CAPS[x[TAPE]]) + x[LVL] = 0x400000 + hi(mpy(r2, hi(mpy(x[SPR], LVK)))) + x[D], x[G], x[ENV], x[TGT], x[CNT], x[TH], x[STARTED] = 0, x[G0], 0, FULL, 0, FULL, 1 + x[TA] = FULL + x[TLV] = x[TM] + x[CP] = 0 + + def segment(self, frames, out): + x = self.x + if x[CNT] >= x[TS] and x[TGT]: + x[TGT] = 0 # the tape runs out: it springs back with a clunk + x[TH] = x[TA] = FULL + x[TLV] = x[CM] + if x[TGT] == 0 and x[ENV] < SILENT: + x[ENV] = 0 + k = x[KS] if x[TGT] else x[KE] + for l, r in frames: + out.append(self.sample(l, r, k)) + x[CNT] = min(x[CNT] + len(frames), 0x400000) + + def sample(self, l, r, k): + x, y = self.x, self.y + w = x[W] + y[2 * w], y[2 * w + 1] = l, r + if w == 0: + y[GUARD], y[GUARD + 1] = l, r + n = self.lcg() + gd = hi(mpy(x[G], GDEP)) + x[G] = hi(mpy(x[G], KG)) + x[D] = min(x[D] + x[O] + gd, DMAXQ) + i = w + (x[D] >> 12) + if i >= NF: + i -= NF + f = (x[D] & 0xfff) << 11 + nf = FULL - f + wl = lim(mac(mpy(y[2 * i], nf), y[2 * i + 2], f)) + wr = lim(mac(mpy(y[2 * i + 1], nf), y[2 * i + 3], f)) + x[FL] = lim(mac(mpy(x[FL], x[NKT]), wl, x[KT])) + x[FR] = lim(mac(mpy(x[FR], x[NKT]), wr, x[KT])) + x[ENV] = lim(mac(ld(x[ENV]), lim(ld(x[TGT]) - ld(x[ENV])), k)) + g = hi(mpy(x[ENV], x[LVL])) + x[HP] = (x[HP] + HINC) & 0xffffff + hum = lim(ld(wave(s24(x[HP]))) + mpy(wave(s24(x[HP] << 1)), H2K)) + c = x[CP] + CINC + turn = c >= 1 << 23 + x[CP] = c - (1 << 23) if turn else c + if (n & 0x7fffff) < x[THR]: + x[TK] = x[TK2] = n & 0x3fffff + if turn: + x[TK] = x[TK2] = CAPA + x[TK] = hi(mpy(x[TK], KTK)) + x[TK2] = hi(mpy(x[TK2], KTK2)) + wob = hi(mpy(wave(s24((x[CP] << 1) - (1 << 23))), x[WD])) + g = g + hi(mpy(g, wob)) + x[HL] = lim(mac(mpy(x[HL], NKH), n, KH)) + x[TL] = lim(mac(mpy(x[TL], NKTL), n, KTL)) + x[TL2] = lim(mac(mpy(x[TL2], NKTL), x[TL], KTL)) + x[TH] = hi(mpy(x[TH], KTH)) + x[TA] = hi(mpy(x[TA], KTA)) + thump = lim(w56(mpy(x[TL2], hi(mpy(x[TH] - x[TA], x[TLV]))) << 3)) + a = mac(mac(ld(thump), x[HL], hi(mpy(x[ENV], x[HM]))), x[TL], hi(mpy(x[ENV], x[RM]))) + a = mac(a, hum, hi(mpy(x[ENV], x[HUM]))) + nz = lim(mac(a, x[HL], hi(mpy(x[TK] - x[TK2], hi(mpy(x[ENV], x[TKL])))))) + out = [] + for dry, wet in ((l, x[FL]), (r, x[FR])): + v = lim(w56(mpy(wet, g) << 1)) + v = lim(ld(v) + ld(nz)) + out.append(lim(mac(mpy(dry, x[DRY]), v, x[MX])) if x[STARTED] else dry) + x[W] = w - 1 if w else NF - 1 + return tuple(out) + + def call(self, frames, p, first=False): + """first: the dispatcher's a = 0 call, frames 0 up to the split; otherwise the a = 1 call, the block's last + frames (16 - n on). Each call places the trig by itself: no count runs from call to call.""" + x = self.x + n = len(frames) + if n == 0: + return [] + self.knobs(p) + word = p.get(0x18 if x[FX1] else 0x12, 0) + # A trig fires once, whichever calls carry its bit: at its sample when it lands in this call, at once when it + # is already past, and at the next call's start when it lands after this one (that call may not carry it). + fire = None + if word & TRIG: + if not x[LATCH]: + off = max((word & 15) - (0 if first else 16 - n), 0) + if off < n: + fire, x[LATCH], x[PEND] = off, 1, 0 + else: + x[PEND] = 1 + else: + x[LATCH] = 0 + if x[PEND]: + fire, x[PEND] = 0, 0 + out = [] + if fire is not None: + off = fire + if off: + self.segment(frames[:off], out) + self.trig() + self.segment(frames[off:], out) + else: + self.segment(frames, out) + return out + + +def pblock(values, raw=None): + 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, raws=None, base=0x4000): + """raws: {block: {offset: word}} written for that block only (a trig's frame word, say). split: the block's first + call has this many frames, the second the rest (stock splits at a trig's sample).""" + 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) + rw = dict(raw or {}) + rw.update((raws or {}).get(blk, {})) + p = pblock(cur, rw) + lo = blk * frames + fr = list(zip(left[lo:lo + frames], right[lo:lo + frames])) + for first, part in ([(True, fr[:split]), (False, fr[split:])] if split else [(False, fr)]): + if not part: + continue + for l, r in m.call(part, p, first): + out_l.append(l) + out_r.append(r) + return out_l, out_r, m + + +def trigs(every, sample=0, count=None, start=0, word=0x12): + """raws for a trig every `every` blocks, at `sample`, on the FX2 word (0x12) or FX1's (0x18).""" + out, b, k = {}, start, 0 + while count is None and b < 100000 or count is not None and k < count: + out[b] = {word: TRIG | sample} + b += every + k += 1 + return out diff --git a/sdk/drafts/tapekeys/resource-impact.example.json b/sdk/drafts/tapekeys/resource-impact.example.json new file mode 100644 index 0000000000..8f7457a054 --- /dev/null +++ b/sdk/drafts/tapekeys/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/tapekeys/test_reference.py b/sdk/drafts/tapekeys/test_reference.py new file mode 100644 index 0000000000..291aa0fd28 --- /dev/null +++ b/sdk/drafts/tapekeys/test_reference.py @@ -0,0 +1,222 @@ +# SPDX-License-Identifier: MIT +"""Tests of Tapekeys' reference model.""" +import math +import unittest + +import reference as R + +FS = 44100 + + +def tone(hz, n, amp=0.5): + return [int(amp * R.Q * math.sin(2 * math.pi * hz * i / FS)) for i in range(n)] + + +def knobs(**kw): + k = list(R.DEFAULTS) + for name, v in kw.items(): + k[R.KNOBS.index(name.upper())] = v + return tuple(k) + + +def run(sig, k, tr=None, split=0, base=0x4000, sig_r=None): + return R.render(sig, sig_r or sig, {0: k}, split=split, raws=tr, base=base) + + +def zc_freq(s, a, b): + seg = s[a:b] + ups = [i for i in range(1, len(seg)) if seg[i - 1] < 0 <= seg[i]] + return FS * (len(ups) - 1) / (ups[-1] - ups[0]) + + +def rms(s): + return math.sqrt(sum(v * v for v in s) / max(1, len(s))) / R.Q + + +class Basics(unittest.TestCase): + def test_dry_before_first_trig(self): + x = tone(330, 16 * 200) + l, r, _ = run(x, knobs(mech=127, sprd=127, gtup=127)) + self.assertEqual(l, x) + + def test_ring_fits_fx1(self): + self.assertLessEqual(R.R, 3072) + + def test_caps_keep_head_in_ring(self): + for t in range(12): + total = R.CAPS[t] * (t + 1) * 44100 + R.GT + self.assertLessEqual(total, R.DMAXQ) + + def test_counts(self): + for d, c in zip(R.DEFAULTS, R.COUNTS): + self.assertLess(d, c) + for name in R.KNOBS + R.END_NAMES + R.TAPE_LABELS: + self.assertLessEqual(len(name), 4) + + +class Notes(unittest.TestCase): + def test_trig_at_its_sample(self): + # SWEL 0, nothing else: the note's envelope starts at the trig's sample, not the block's start + x = [R.Q // 2] * (16 * 40) + k = knobs(swel=0, gtup=0, sprd=0, mech=0, tone=127) + l, _, _ = run(x, k, R.trigs(10 ** 6, sample=9, count=1, start=5)) + self.assertEqual(l[5 * 16 + 8], x[0]) # still dry the sample before + self.assertLess(abs(l[5 * 16 + 9]), x[0] // 4) # the note starts from silence at its sample + + def test_trig_split_same_as_unsplit(self): + x = tone(220, 16 * 300) + tr = R.trigs(40, sample=6) + k = knobs(mech=80) + a = run(x, k, tr)[0] + b = run(x, k, tr, split=6)[0] + self.assertEqual(a, b) + + def test_trig_found_without_the_first_call(self): + # a block whose a = 0 call never came: the a = 1 call alone still places its trig, and later trigs too + x = tone(220, 16 * 400) + k = knobs(swel=0, gtup=0, sprd=0, mech=0, clik=0, tape=0) + m = R.Model() + out = [] + for blk in range(400): + fr = list(zip(x[blk * 16:blk * 16 + 16], x[blk * 16:blk * 16 + 16])) + raw = {0x12: R.TRIG | 6} if blk % 100 == 3 else {} + p = R.pblock(k, raw) + if blk == 3: + out += [None] * 6 + out += m.call(fr[6:], p, False) # the a = 0 call is missing + else: + out += m.call(fr[:6], p, True) + m.call(fr[6:], p, False) + self.assertEqual(m.x[R.STARTED], 1) + self.assertGreater(rms([v[0] for v in out[303 * 16 + 400:303 * 16 + 4000]]), 0.1) # the trig at block 303 heard + + def test_trig_bit_in_one_call_only(self): + # the bit only in the block's first call, its sample in the second: the trig still fires, at the second's start + for first_only in (True, False): + m = R.Model() + x = tone(220, 16 * 300) + k = knobs(swel=0, gtup=0, sprd=0, mech=0, clik=0, tape=0) + for blk in range(300): + fr = list(zip(x[blk * 16:blk * 16 + 16], x[blk * 16:blk * 16 + 16])) + trig = blk == 7 + pa = R.pblock(k, {0x12: R.TRIG | 9} if trig and first_only else {}) + pb = R.pblock(k, {0x12: R.TRIG | 9} if trig and not first_only else {}) + m.call(fr[:9], pa, True) + m.call(fr[9:], pb, False) + if blk == 7: + self.assertEqual(m.x[R.STARTED], 1) + + def test_trig_fires_once_while_its_bit_stays(self): + m = R.Model() + x = tone(220, 16) + p = R.pblock(knobs(sprd=127), {0x12: R.TRIG}) + m.call(list(zip(x, x)), p) + m.call(list(zip(x, x)), p) + self.assertEqual(m.x[R.CNT], 32) # not restarted by the second call + + def test_empty_call(self): + m = R.Model() + self.assertEqual(m.call([], R.pblock(R.DEFAULTS, {0x12: R.TRIG})), []) + + def test_tape_runs_out_to_exact_silence(self): + x = tone(220, FS * 3) + for end, quiet in ((0, int(1.9 * FS)), (1, FS * 3 - 4000)): # after the return clunk; FADE's 250 ms by 2 s + k = knobs(tape=0, end=end, mech=127) + l, _, _ = run(x, k, R.trigs(10 ** 6, count=1)) + self.assertGreater(rms(l[FS // 2:FS - 2000]), 0.1) + self.assertFalse(any(l[quiet:]), R.END_NAMES[end]) + + def test_fade_slower_than_cut(self): + x = tone(220, FS * 2) + cut = run(x, knobs(tape=0, end=0, mech=0), R.trigs(10 ** 6, count=1))[0] + fade = run(x, knobs(tape=0, end=1, mech=0), R.trigs(10 ** 6, count=1))[0] + self.assertGreater(rms(fade[FS + 2000:FS + 6000]), 4 * rms(cut[FS + 2000:FS + 6000])) + + def test_retrig_restarts_the_tape(self): + x = tone(220, FS * 4) + l, _, _ = run(x, knobs(tape=0, mech=0), R.trigs(FS * 2 // 16, count=2)) + self.assertGreater(rms(l[FS * 2 + 8000:FS * 3 - 2000]), 0.1) + + def test_swell(self): + x = tone(220, FS) + fast = run(x, knobs(swel=0, mech=0, gtup=0), R.trigs(10 ** 6, count=1))[0] + slow = run(x, knobs(swel=127, mech=0, gtup=0), R.trigs(10 ** 6, count=1))[0] + self.assertGreater(rms(fast[200:2000]), 3 * rms(slow[200:2000])) + self.assertGreater(rms(slow[30000:40000]), 0.25) + + +class Pitch(unittest.TestCase): + def test_getup_starts_flat(self): + x = tone(440, FS) + l, _, _ = run(x, knobs(gtup=127, sprd=0, mech=0, swel=0, tone=127), R.trigs(10 ** 6, count=1)) + early, late = zc_freq(l, 50, 1500), zc_freq(l, 20000, 40000) + self.assertLess(early, 435) + self.assertAlmostEqual(late, 440, delta=0.5) + + def test_spread_flat_and_varies(self): + x = tone(440, FS * 6) + k = knobs(tape=11, sprd=127, gtup=0, mech=0, swel=0, tone=127) + tr = R.trigs(FS // 16, count=6) + l, _, _ = run(x, k, tr) + f = [zc_freq(l, i * FS + 8000, i * FS + 40000) for i in range(6)] + self.assertTrue(all(v <= 440.05 for v in f)) + self.assertGreater(max(f) - min(f), 0.2) + + def test_offset_respects_ring(self): + m = R.Model() + x = tone(440, 16) + for t in range(12): + for _ in range(20): + m.call(list(zip(x, x)), R.pblock(knobs(tape=t, sprd=127), {0x12: R.TRIG})) + self.assertLessEqual(m.x[R.O], R.CAPS[t]) + + +class Mechanics(unittest.TestCase): + def test_mech_zero_is_silent_with_silence_in(self): + z = [0] * (FS * 2) + l, r, _ = run(z, knobs(mech=0, clik=0), R.trigs(400)) + self.assertFalse(any(l + r)) + + def test_mech_noise_under_note_and_thump(self): + z = [0] * FS + l, _, _ = run(z, knobs(mech=127, clik=127, swel=0, tape=11), R.trigs(10 ** 6, count=1)) + self.assertGreater(rms(l[100:1500]), rms(l[20000:30000])) # the thunk, then the running tape + self.assertGreater(rms(l[20000:30000]), 0) + + def test_clik_apart_from_mech(self): + z = [0] * FS + tr = R.trigs(10 ** 6, count=1) + none = run(z, knobs(mech=127, clik=0, swel=0, tape=11), tr)[0] + full = run(z, knobs(mech=127, clik=127, swel=0, tape=11), tr)[0] + only = run(z, knobs(mech=0, clik=127, swel=0, tape=11), tr)[0] + self.assertGreater(rms(full[0:2000]), 2 * rms(none[0:2000])) # CLIK makes the onset (6 dB over MECH full) + self.assertGreater(rms(none[8000:16000]), 0) # MECH still runs under the note + self.assertFalse(any(only[FS * 7 // 10:])) # the thunk is exactly 0 by 0.7 s + + def test_thunk_rises_without_a_click(self): + z = [0] * 4000 + s = run(z, knobs(mech=0, clik=127, tape=11), R.trigs(10 ** 6, count=1))[0] + peak = max(abs(v) for v in s) + self.assertLess(max(abs(v) for v in s[:12]), peak / 8) # it starts near 0, not at its peak + + def test_no_noise_before_trig(self): + z = [0] * FS + l, _, _ = run(z, knobs(mech=127, clik=127)) + self.assertFalse(any(l)) + + +class Mix(unittest.TestCase): + def test_mix_zero_is_nearly_dry(self): + x = tone(220, FS) + l, _, _ = run(x, knobs(mix=0, mech=0), R.trigs(10 ** 6, count=1)) + self.assertLess(max(abs(a - b) for a, b in zip(l[100:], x[100:])), 300) + + def test_fx1_reads_its_word(self): + x = tone(220, 16 * 200) + l, _, _ = run(x, knobs(), R.trigs(10 ** 6, count=1, start=3, word=0x18), base=0x1000) + self.assertNotEqual(l, x) + l2, _, _ = run(x, knobs(), R.trigs(10 ** 6, count=1, start=3, word=0x12), base=0x1000) + self.assertEqual(l2, x) # FX1 ignores the FX2 word + + +if __name__ == "__main__": + unittest.main() diff --git a/sdk/drafts/tapekeys/verify.py b/sdk/drafts/tapekeys/verify.py new file mode 100644 index 0000000000..45a35687c2 --- /dev/null +++ b/sdk/drafts/tapekeys/verify.py @@ -0,0 +1,176 @@ +#!/usr/bin/env python3 +"""Tapekeys' 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 + +No firmware is read: the harness is sdk/octatrack/tools/dsp_gate.py, a synthetic image and dsp_host. dsp_host +writes knob words but no trig word, so the trig cases run the engine inside a test fixture: its own init and proc +in front of the engine's (renamed), which write the trig word into the frame record on a schedule (a trig every P +blocks from block S, C of them, at sample s) before each call, counting blocks on the dispatcher's a = 1 call. +The engine's bytes are the same. 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, the state stays inside X:(r7+$00..$3c). + 2. Bit for bit the model: dry before any trig; one note running out (CUT and FADE), with and without the split + at its sample; a long note at the most spread; trigs every few blocks with every knob swept; an FX1 slot + reading its own word (and not FX2's); knob words with LFO fraction bits; garbage state before init; two seeds. + 3. Silence: MECH 0 with silence in is exact silence; a note's end is exact silence (MECH full); the FX1 slot's + buffer stays under $4000 (the guard). + 4. The cycle meter, printed for TESTING.md. +""" +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 = 0x3c +FOREVER = 0x7fffff + + +def fixture(start, every, count, sample, word_off): + """engine.asm inside a fixture that writes the trig word on a schedule.""" + eng = SOURCE.read_text().replace("\ninit:\n", "\ntkin:\n").replace("\nproc:\n", "\ntkpr:\n") + word = R.TRIG | sample + L = lambda *i: "".join(" " + s + "\n" for s in i) # noqa: E731 + fx = "init:\n" + L(f"move #>{start},x0", "move x0,x:(r7+$41)", f"move #>{count},x0", "move x0,x:(r7+$42)", + "bra tkin") + fx += "proc:\n" + L("clr b", "move b1,x1", "move x:(r7+$41),b", "tst b", "bne tkf1", "move x:(r7+$42),b", + "tst b", "ble tkf1", f"move #>${word:06x},x1") + fx += "tkf1:\n" + L(f"move x1,x:(r6+${word_off:x})", "tst a", "beq tkf3", "move x:(r7+$41),b", "tst b", + "bne tkf2", "move x:(r7+$42),b", "move #>$1,x0", "sub x0,b", "move b1,x:(r7+$42)", + f"move #>{every},b") + fx += "tkf2:\n" + L("move #>$1,x0", "sub x0,b", "move b1,x:(r7+$41)") + fx += "tkf3:\n" + L("bra tkpr") + head, _, body = eng.partition("; CYCLES_FORWARD_BRANCHES\n") + return head + "; CYCLES_FORWARD_BRANCHES\n" + fx + body + + +def main(): + G.require_tools(HERE) + gate = G.Gates("tapekeys") + with G.workdir("tapekeys-verify-") as tmp: + work = pathlib.Path(tmp) + asm = G.assemble(SOURCE, work) + cache = {} + + def fixed(start, every, count, sample, word_off=0x12): + key = (start, every, count, sample, word_off) + if key not in cache: + p = work / ("fx_%d_%d_%d_%d_%x.asm" % key) + p.write_text(fixture(*key)) + cache[key] = G.assemble(p, work) + return cache[key] + + if "--fx-audit" in sys.argv: + plan, renders = sys.argv[sys.argv.index("--fx-audit") + 1:][:2] + a = fixed(0, 2756, FOREVER, 0) # a note every 2 s, the audit's signals played through it + for path in G.render_fx_audit(a, plan, renders, R.DEAREST[:5] + (127,) + R.DEAREST[6:]): + 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 = 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.lower().startswith(a.lower())] + gate("1 no label is a prefix of another (dsp_asm resolves labels by prefix)", not clashes, str(clashes)) + offsets = G.state_offsets(SOURCE.read_text()) + gate("1 state inside X:(r7+$00..$3c)", bool(offsets) and offsets[-1] <= STATE_LAST, + f"(highest ${offsets[-1]:02x})" if offsets else "(no state)") + + # 2. bit for bit the model + def same(name, inst, sched=None, split=0, base=0x4000, word_off=0x12, **kw): + raw_model = kw.pop("raw_model", None) + a = fixed(*sched, word_off) if sched else asm + tag = "img_%x_%s" % (base, "_".join(map(str, sched + (word_off,))) if sched else "plain") + kw["image"] = a.image(x={G.ALLOC_AT: base}, name=tag) # its own file: images share a default name + r = G.run(a, [inst], split=split or None, **kw) + raws = R.trigs(sched[1], sched[3], sched[2], sched[0], word=word_off) if sched else None + rows = inst.rows or {0: inst.knobs} + want = R.render(inst.left, inst.right, rows, split=split, raws=raws, raw=raw_model, base=base) + gate.same(name, r.outputs[0], want[:2]) + return r + + n = 16 * 1500 + nlong = 16 * 6000 # 2.2 s: a 1 s tape runs out and its clunk dies + tl, ll = G.tone(220, 0.8, nlong), G.noise(nlong, 0.6, 3) + nl, nr = G.noise(n, 0.6, 1), G.noise(n, 0.6, 2) + t = G.tone(220, 0.8, n) + k1 = (0,) + R.DEFAULTS[1:] # TAPE 1 s: the note runs out inside the render + k1f = k1[:6] + (1,) + k1[7:] + same("2 dry before any trig, defaults", G.Instance(nl, nr, knobs=R.DEFAULTS)) + same("2 dry before any trig, dearest", G.Instance(t, nl, knobs=R.DEAREST)) + same("2 one note, TAPE 1 s, CUT", G.Instance(tl, ll, knobs=k1), sched=(3, 1, 1, 9)) + same("2 one note, TAPE 1 s, CUT, the split at its sample", G.Instance(tl, ll, knobs=k1), sched=(3, 1, 1, 9), split=9) + same("2 one note, TAPE 1 s, FADE, MECH full (the return clunk)", G.Instance(tl, ll, knobs=k1f[:4] + (127,) + k1f[5:]), sched=(0, 1, 1, 0)) + meters = {} + far = (11, 0, 127, 127, 60, 127, 0, 127) + meters["a long note, every feature"] = same("2 a long note: TAPE 12 s, SPRD and GTUP full", G.Instance(t, t, knobs=far), + sched=(1, 1, 1, 3)).meter + rows, blk = {0: R.DEFAULTS}, 40 + for slot in range(9): + for v in ((0, 11, 5) if slot == 0 else ((0, 1, 0) if slot == 6 else (0, 127, 64))): + row = list(R.DEFAULTS) + row[slot] = v + rows[blk] = tuple(row) + blk += 40 + rows[blk] = R.DEAREST + meters["notes every 30 blocks, knobs swept"] = same("2 a note every 30 blocks, every knob swept", + G.Instance(nl, nr, rows=rows), sched=(2, 30, FOREVER, 6)).meter + same("2 the same with the split at the trigs' sample", G.Instance(nl, nr, rows=rows), sched=(2, 30, FOREVER, 6), split=6) + meters["a note every block"] = same("2 a note every block (DEAREST)", G.Instance(t, nl, knobs=R.DEAREST), + sched=(0, 1, FOREVER, 12), split=12).meter + same("2 an FX1 slot reads its own word (r6+$18)", G.Instance(t, nr, knobs=k1), sched=(2, 1, 1, 4), base=0x1000, + word_off=0x18) + same("2 an FX1 slot ignores FX2's word", G.Instance(t, nr, knobs=k1), sched=(2, 1, 1, 4), base=0x1000) + word = (R.DEFAULTS[1] << 16) | 0xb700 + same("2 a knob word with an LFO's fraction bits", G.Instance(nl, nr, knobs=R.DEFAULTS), sched=(1, 40, FOREVER, 2), + pwords=[(0, 1, word)], raw_model={1: word}) + same("2 garbage state before init", G.Instance(nl, nr, rows=rows), sched=(2, 30, FOREVER, 6), dirty=0x5a5a5a) + gate.two_seeds("2 the same output from state filled with two seeds: nothing read before it is written", + fixed(2, 30, FOREVER, 6), [G.Instance(nl, nr, rows=rows), G.Instance(t, nl, knobs=R.DEAREST)]) + + # 3. silence and the slot's bounds + z = [0] * n + quiet = tuple(0 if s in (4, 8) else v for s, v in enumerate(R.DEAREST)) + a = fixed(0, 20, FOREVER, 5) + (l, r), = G.run(a, [G.Instance(z, z, knobs=quiet)], image=a.image(name="zero"), tag="zero").outputs + gate("3 MECH and CLIK 0: silence in, silence out, notes playing", not any(l + r)) + a = fixed(0, 1, 1, 0) + (l, r), = G.run(a, [G.Instance(tl, tl, knobs=(0, 0, 127, 127, 127, 127, 0, 127, 127))], image=a.image(name="end"), tag="end").outputs + q = int(1.9 * G.FS) + gate("3 a note's end is exact silence after its clunk (TAPE 1 s, CUT, MECH and CLIK full)", + len(l) > q + 1000 and any(l[:G.FS // 2]) and not any(l[q:] + r[q:])) + for base in (0x1000, 0x1c00, 0x2800, 0x3400): + a = fixed(0, 25, FOREVER, 0, 0x18) + res = G.run(a, [G.Instance(t, nl, knobs=far)], image=a.image(x={G.ALLOC_AT: base}, name="g%x" % base), + guard=True, tag="guard") + gate(f"3 an FX1 slot at ${base:04x} writes inside its 3,072 words", not res.stray[0] and not res.clobbers[0], + str(res.stray[0])[:120]) + + # 4. cost + print("[INFO] cycle meter: " + ", ".join(f"{k} {v}" for k, v in meters.items()) + " executed instructions a sample") + print(f"[INFO] dsp_gate.py {G.provenance(HERE)['dsp_gate.py']}") + gate.finish() + + +if __name__ == "__main__": + main()