diff --git a/changelog/2026-11-24-digitakt-ii-chord-recording.md b/changelog/2026-11-24-digitakt-ii-chord-recording.md new file mode 100644 index 0000000000..a6a0eaa119 --- /dev/null +++ b/changelog/2026-11-24-digitakt-ii-chord-recording.md @@ -0,0 +1 @@ +Digitakt II base: chords recorded across linked voices. Live recording on a lead track (SETTINGS > VOICES above 1) now records each note of a chord as a step on the track that plays it, so a recorded chord plays back as a chord and each note can be edited like any step. A note the instrument starts playing a moment before the recording sees it is recorded on the track playing it, not skipped. While linked, the voice tracks take a copy of the lead's sound and of its track's TRIG page defaults, length, swing and speed, kept up to date a part at a time and saved with the project, so their own steps sound exactly like the lead; they keep that sound when the setting returns to 1. Notes from the keys and MIDI fill the group from the lead up whatever the pattern plays there, and the pattern's notes of the lead never land on a voice track with steps of its own. Checked in the emulator and, as reported by the author, on a unit (OS 1.17, Morpha on the lead): chords recorded with the pattern running land on every linked track, sound like the lead and edit like any step. diff --git a/sdk/machines/digitakt-ii/elekloader/base.json b/sdk/machines/digitakt-ii/elekloader/base.json index cf361633d5..1475cdb942 100644 --- a/sdk/machines/digitakt-ii/elekloader/base.json +++ b/sdk/machines/digitakt-ii/elekloader/base.json @@ -52,6 +52,7 @@ "../modwerk/machines-entry.s", "../modwerk/voices.c", "../modwerk/voices-entry.s", + "../modwerk/voices-record-entry.s", "../modwerk/fw-bindings.s" ], "subscribe": [ @@ -114,6 +115,13 @@ "op": "jsr", "target": "core_voice_event_entry" }, + { + "addr": "0x40048fb2", + "len": 6, + "stockSha256": "ed5086557423e0a010a460981e782edce14c7e9c57265e741ebacc985a1d8aa3", + "op": "jsr", + "target": "core_voice_record_entry" + }, { "addr": "0x40066df2", "len": 6, diff --git a/sdk/machines/digitakt-ii/elekloader/modwerk/VOICES.md b/sdk/machines/digitakt-ii/elekloader/modwerk/VOICES.md index 489742de56..d41bef1467 100644 --- a/sdk/machines/digitakt-ii/elekloader/modwerk/VOICES.md +++ b/sdk/machines/digitakt-ii/elekloader/modwerk/VOICES.md @@ -4,9 +4,10 @@ A lead track plays its notes on the tracks after it, so notes overlap instead of ## What the musician does -SETTINGS shows a row named after the track you are on, `T1 VOICES` to `T16 VOICES`, from 1 (stock) to 8. LEFT and RIGHT change it. With 3 on track 1, tracks 1, 2 and 3 form a group: each new note track 1 plays goes to the first of them, from the lead on, that holds no note, so the lead takes a chord's first note (the trig keys send a chord from the lowest note up); with all three busy, to the next one in turn. +SETTINGS shows a row named after the track you are on, `T1 VOICES` to `T16 VOICES`, from 1 (stock) to 8. LEFT and RIGHT change it. With 3 on track 1, tracks 1, 2 and 3 form a group: each new note track 1 plays from the keys or MIDI goes to the first of them, from the lead on, that holds no note from the keys or MIDI, so the lead takes a chord's first note (the trig keys send a chord from the lowest note up) whatever the pattern plays there; with all three busy, to the next one in turn. The pattern's notes of the lead play on the lead, or, while it holds a note, on a voice track with no steps of its own. -- The voice tracks belong to the lead while linked: they play the lead's sound with its machine, with each trig's own locks, and follow knob turns made on the lead. Their own trigs are silent while linked (the base sends their notes to no track), so their own machine never sounds between or after the lead's notes. Their own sounds, trigs and pages are not changed: back at 1 they play as before. +- The voice tracks belong to the lead while linked: their sound in the kit becomes a copy of the lead's (machine, filter, amp, effects, everything) and their track's TRIG page defaults, length, swing and speed the lead's, kept up to date and saved with the project; they follow knob turns made on the lead. Their own steps play and their own key presses are silent. Back at 1 they keep the copied sound. +- Live recording on the lead puts each note on the track that plays it: a chord records as one step on each of its tracks. - A muted lead plays nothing. A muted voice track is skipped. - A track that is already a voice of an earlier lead leads nothing, and a group ends at track 16. - The setting is the project's: saved with it (project archive tag `VOC1`), reset to 1 everywhere by a project saved without it. @@ -33,7 +34,17 @@ Because the record names its sound, a note moved to another track plays the lead **Knobs.** The per-track parameter targets are 71 int16 a track from `0x80003362` (142 bytes a track; the smoother at `0x400d9406` follows them each frame from `0x80003340`). A note-on loads the note's sound and locks into its track's targets. Once a frame, before the transfer, `core_voice_frame` compares the lead's targets with the last frame's and copies the values that changed to its voice tracks. In a frame where a sequencer note stayed on the lead, it only takes the lead's new values, so a trig lock on the lead does not spread to the voices; notes from the keys or MIDI carry no locks and never hold a turn back. As a safety net, each frame one voice track of the group that holds no note is copied from the lead in full, so the next note there starts from the lead's settings. Found on a unit (Morpha, build `e6ffc45b`): while chords were played and knobs turned, later chord notes no longer sounded like the lead; the frames skipped for every lead note had swallowed turns. -**Sound chip.** The machine wire's per-track types (`machine-frame.c`) give a voice track its lead's type for as long as it is linked, so an added machine's player plays every note its lead sends there, and the track's own machine never takes over its voices mid-note. A voice track's own note records go to track 16 (none) at the same entry. +**Live recording.** The sequencer records a live note with `0x40048fb2` (arguments on the stack: sequencer, track, note, velocity, a flag, step, …; it looks up the track's object with `0x4003f304` and writes the step through `0x40059988` and `0x400597c8`). Found with a write watch on pattern 0 while a tap was recorded: the step word at `pattern + 0x6a5 * track + 2 * step` (here `0x11`, then `0x391`), then the step's bytes 0x100, 0x180, … 0x400 further; the frame chain from `0x400597c8` led back through `0x400599ba` and `0x40048fb2` to the event handler at `0x40031a60`. The routine runs about 40,000 instructions before the frame plays the note (the held-note tables were written after the step). The base's jsr replaces its first 6 bytes (`linkw %fp,#-68; moveq #15,%d0`, SHA-256 `ed508655…`); `core_voice_record` changes the track argument of a lead's note to the group track it will play on (as `pick` chooses, skipping tracks other recorded notes have taken) and leaves a pending entry that `core_voice_event` uses to send the note there. When the frame plays a note from the keys or MIDI before the routine has seen it (no pending entry), it marks the track it chose as holding a fresh note of that pitch, and the routine records the note there. Any other note the group already holds (the keys sending a held note again) is not recorded (track 16: the routine returns at once). Each chord note therefore lands as an ordinary step on its own track, editable like any step. + +A linked voice track's own steps play while it is linked: a sequencer note-on on a voice track keeps its track. To make them sound like the lead, the base copies the lead's sound in the kit (`0x426532ec + 0x450 * track`, 0x450 bytes) and its pattern track's settings (`+0x480` to `+0x48f`: the TRIG page defaults the note record's constructor at `0x400d6d24` falls back on when a step sets none, then length, swing and speed) to each voice track, in full when linking, then one voice a frame and a sixth of the sound at a time (only the words that differ are written), so the frame interrupt stays short. Pointing the step's note record at the lead's sound (`+0x3c`) was tried first and is not enough: on a unit the recorded chord notes played louder, unfiltered and without envelope; in the emulator a voice track's own note played 9 dB over the lead's and held flat, still 9 dB over with the sound copied but not the track's settings, and at the lead's level with its decay with both (rms 0.045 against 0.043). Its own key presses still go to no track. + +On the unit (`11fa7318`, the record hook with the pointed sound, the pattern running): of four linked tracks only tracks 1 and 3 recorded, and the sound went wrong while recording. In the emulator the sequencer plays only its first step, so neither could be reproduced; the fill order above (live notes ignore what the pattern plays on a track) and the pattern notes' rule (never on a voice track with steps of its own) answer the likely cause, pattern notes taking track 2. + +On the unit (`f83435c3`, those two rules and the copied sound and settings, the pattern running, reported by the author): recorded notes sounded like the lead, but some of the linked tracks again recorded nothing, and changing pages on a linked track lagged (up to about a second) while the pattern ran, not on the lead. The likely cause of the missing notes: the recording routine skipped a note the frame had already played, taking it for the keys sending a held note again; the fresh marks above record it instead. The lag's cause is not confirmed: the idle copy now compares a sixth of a sound a frame instead of all of it, and writes a track's targets only where they differ. Neither could be reproduced in the emulator, whose sequencer stops after its first step, but one emulator run recorded a chord's second note after the frame had played it (held-note tables written before the step), on the track playing it. With both changes (`800a4a6f`, table below) the author reported every linked track recording its note and no lag. + +Emulator (track 1 leading 3, rotating test build): REC + PLAY, three taps of trig key 1: the first recorded on track 1 and played there, the second on track 2, the third on track 3 (step words `0x391`, held-note source 2 on the same track). Playback of recorded steps could not be checked: after STOP and PLAY the emulator's sequencer played only its first step. Every track at 1: the project's pattern and the default note as before. + +**Sound chip.** The machine wire's per-track types (`machine-frame.c`) give a voice track its lead's type for as long as it is linked, so an added machine's player plays every note its lead sends there, and the track's own machine never takes over its voices mid-note. A voice track's own key presses go to track 16 (none) at the same entry; its own steps keep their track. ## Checked @@ -56,8 +67,9 @@ On a unit (reported by the author, 23 November 2026), Digitakt II OS 1.17 with t | `063eea9a` (voices rotating, a voice track's own machine between borrowed notes) | Chords from the keys: each note and each key release moved to another track, each sounding a different wave; letting go of the second key of a two-key chord left a note sounding that jumped to the first key's pitch. Save, load another project and come back: the track kept its settings. | | `e6ffc45b` (voice tracks reserved for the lead, filled from the lead, copied at linking; held pitch from the keys dropped) | Chords play; after a while of playing and turning knobs, later chord notes no longer had the lead's settings. | | `27dfadf8` (turns copied with key and MIDI notes, idle voices copied in full) | Chords with VOICES 3-4 and knobs turned while playing: every chord note keeps the lead's settings. | +| `800a4a6f` (chord recording: voice tracks copy the lead's sound and settings a part at a time, fresh marks) | Chords from the keys recorded with the pattern running and VOICES 3-4: every linked track got its note, and the recorded notes sound like the lead. Page changes on a linked track while the pattern runs: no lag. A recorded note on a voice track edits like any step. Recorded timing fine with quantize on. Live chords, knobs and Morpha's FOLD as before. | -Not checked yet: sequencer trigs on a linked lead on a unit, MIDI chords, muted voices, a stock machine (SAMPLE) as the lead on a unit. +Not checked yet: MIDI chords, muted voices, a stock machine (SAMPLE) as the lead on a unit. ## Open diff --git a/sdk/machines/digitakt-ii/elekloader/modwerk/voices-record-entry.s b/sdk/machines/digitakt-ii/elekloader/modwerk/voices-record-entry.s new file mode 100644 index 0000000000..122df832cb --- /dev/null +++ b/sdk/machines/digitakt-ii/elekloader/modwerk/voices-record-entry.s @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* Linked voices, live recording (OS 1.17): the sequencer's live-record routine at 0x40048fb2 takes (sequencer, track, + * note, velocity, flag, step, ...) on the stack and records the note on that track's step. Its first 6 bytes + * (linkw %fp,#-68; moveq #15,%d0) become the base's jsr: voices.c may change the track argument to the voice track the + * note will play on, then this replays the two instructions on the caller's stack, as stock ran them, and resumes at + * 0x40048fb8. Scratch registers d0, d1, a0 and a1 carry nothing into the routine (its arguments are on the stack). */ + .text + .even + .globl core_voice_record_entry + +core_voice_record_entry: + lea -16(%sp),%sp + movem.l %d0-%d1/%a0-%a1,(%sp) + pea 24(%sp) /* the arguments: our return address and the caller's lie in between */ + jsr core_voice_record + addq.l #4,%sp + movem.l (%sp),%d0-%d1/%a0-%a1 + lea 16(%sp),%sp + movea.l (%sp)+,%a0 /* back to 0x40048fb8, with the stack as the caller left it */ + link.w %fp,#-68 + moveq #15,%d0 + jmp (%a0) + + .section .note.GNU-stack,"",@progbits diff --git a/sdk/machines/digitakt-ii/elekloader/modwerk/voices.c b/sdk/machines/digitakt-ii/elekloader/modwerk/voices.c index a81d448935..79f2624ea2 100644 --- a/sdk/machines/digitakt-ii/elekloader/modwerk/voices.c +++ b/sdk/machines/digitakt-ii/elekloader/modwerk/voices.c @@ -6,12 +6,23 @@ * sequencer trigs, the keyboard and MIDI alike. The base looks at each record as the frame takes it * (core_voice_event_entry, at 0x4002dff4) and moves a lead's note-on to the next free track of its group, and a * note-off to the track that holds that note. The record already names the lead's sound (+0x3c, or a sound lock's), - * so the voice track plays the lead's sound with that trig's own locks; nothing in the kit changes. + * so the voice track plays the lead's sound with that trig's own locks. * - * While linked, a voice track belongs to its lead: it plays only the notes its lead sends, always with the lead's - * machine (machine-frame.c sends the sound chip the lead's type for it), and its own notes (its trigs) go to no - * track, so its own machine and sample never sound over, between or after its lead's notes. Its own sound and - * pattern stay as they are and play again once VOICES puts it back on its own. + * While linked, a voice track belongs to its lead: its sound in the kit is a copy of the lead's (machine, filter, amp, + * effects, everything), kept up to date each frame and saved with the kit, and it plays the notes its lead sends, + * always with the lead's machine (machine-frame.c sends the sound chip the lead's type for it). Its own steps play + * too, so chords recorded across the group play back as recorded with the lead's sound; its own key presses go to no + * track. It keeps the copied sound once VOICES puts it back on its own. (Pointing a step's note record at the lead's + * sound instead was tried: part of a note's settings come from its own track, so it played louder and unfiltered.) + * + * Notes from the keys and MIDI fill the group from the lead up (the first note on the lead, the next on the next + * track), whatever the pattern plays there; the pattern's notes of the lead play on the lead, or, while it holds a + * note, on a voice track with no steps of its own, so they never land on recorded chord notes. + * + * Live recording: the sequencer's live-record routine (0x40048fb2, core_voice_record_entry) records a lead's note + * from the keys or MIDI on the track that will play it, so each note of a chord lands as an ordinary step on its own + * track (editable afterwards like any step). The routine runs before the frame plays the note, so it picks the track + * and leaves a pending entry; the frame then sends that note to the same track. * * Record fields used (read from the sequencer's constructor at 0x400d6d24 and the frame's note handling from * 0x4002e1b2): +0x00 kind (0 and 1 take the note path), +0x04 1 for a note-on, +0x10 the track, +0x14 the note's @@ -26,7 +37,8 @@ * track holding no note is copied from the lead in full. * * The setting is the project's (core_project_archive, tag VOC1), saved with the project and reset by a project - * saved without it. With every track at 1 nothing here changes a note. Emulator-checked only; see VOICES.md. + * saved without it. With every track at 1 nothing here changes a note. VOICES.md records what was checked in the + * emulator and on a unit. */ #include #include "mk2/core.h" @@ -39,8 +51,22 @@ enum { PATTERN_TRACK_VIEW = 0x30, NONE = 0xff, SEQUENCER = 1, /* a record's source (+0x14) for the sequencer's notes */ + SOUND_STRIDE = 0x450, + SOUND_WORDS = SOUND_STRIDE / 4, + PENDING = 8, /* recorded notes the frame has not played yet */ + PENDING_FRAMES = 64, /* after which a pending entry is dropped (the frame plays it within a few) */ + FRESH_FRAMES = 64, /* how long a note the frame played first waits for live recording */ + SOUND_SLICES = 6, /* the idle copy compares a voice's sound with the lead's a sixth at a time */ + SLICE_WORDS = SOUND_WORDS / SOUND_SLICES, + TRACK_STRIDE = 0x6a5, /* a pattern's track data; its step words first, two bytes a step */ + STEPS = 128, + TRACK_SETTINGS = 0x480, /* a pattern track's settings after its steps */ + TRACK_SETTINGS_BYTES = 16, }; +#define KIT_SOUNDS 0x426532ecu /* the kit's track sounds (a record's +0x3c without a sound lock), SOUND_STRIDE apart */ +#define CURRENT_PATTERN (*(const volatile uint32_t *)0x4316501cu) + #define TARGETS ((volatile int16_t *)0x80003362u) /* track t's value j at [VALUES * t + j] */ #define NOTE_SOURCE ((const volatile uint32_t *)0x47db4310u) /* the source of the note each track holds, 0 none */ #define NOTE_PITCH ((const volatile int32_t *)0x47db4560u) /* its pitch, -1 none */ @@ -51,10 +77,92 @@ static uint8_t voices[TRACKS]; /* the project's setting, 0 or 1 for a track static uint8_t lead_of[TRACKS]; /* each track's lead, NONE outside a group */ static uint8_t next_voice[TRACKS]; /* per lead: where the search for a free voice starts */ static uint8_t sync_next[TRACKS]; /* per lead: the voice track the idle copy looks at next */ +static uint8_t sync_slice[TRACKS]; /* per lead: the part of the sound the idle copy compares */ static volatile uint16_t absorb; /* leads whose own note set its targets this frame */ static volatile uint16_t leads; /* leads with a group; written last by regroup */ static int16_t last[TRACKS][VALUES]; +/* A note the live-record routine put on a track of its group, for the frame to play there. The task writes an entry + * and sets `used` last; the frame interrupt clears it when it plays the note or the entry ages out. */ +static struct { + volatile uint8_t used; + uint8_t lead; + uint8_t track; + int8_t pitch; + uint16_t age; +} pending[PENDING]; + +/* Per track: a note from the keys or MIDI the frame put there without a pending entry, so before live recording saw + * it (on a unit with the pattern running the frame can come first). Its recording then lands on this track. The frame + * interrupt writes it; the task clears it when it records the note. */ +static struct { + volatile uint8_t age; /* frames since the note started, 0 for none */ + int8_t pitch; +} fresh[TRACKS]; + +static int reserved(unsigned int track) +{ + for (unsigned int k = 0; k < PENDING; ++k) { + if (pending[k].used && pending[k].track == track) { + return 1; + } + } + return 0; +} + +/* Words `first` to `end` of a track's sound in the kit made a copy of another's, a word at a time and only where they + * differ. Its own function, indexed, so the compiler does not reuse a post-incremented address register in one + * instruction (sdk/runtime/m68k_codegen.py refuses that). */ +static __attribute__((noinline)) void copy_sound(unsigned int to, unsigned int from, unsigned int first, + unsigned int end) +{ + volatile uint32_t *const d = (volatile uint32_t *)(KIT_SOUNDS + SOUND_STRIDE * to); + const volatile uint32_t *const s = (const volatile uint32_t *)(KIT_SOUNDS + SOUND_STRIDE * from); + for (unsigned int k = first; k < end; ++k) { + const uint32_t w = s[k]; + if (d[k] != w) { + d[k] = w; + } + } +} + +/* A track's settings in the current pattern made the lead's: the TRIG page defaults its steps fall back on (the note + * record's constructor at 0x400d6d24 reads them from +0x480 to +0x48b when a step sets no value of its own), then its + * length, swing and speed (+0x48c to +0x48f), so the voice's own steps play as the lead's would and stay in time with + * them. Without this a recorded chord note on a voice track took that track's own defaults (emulator: 9 dB louder and + * no envelope). */ +static void copy_track_settings(unsigned int to, unsigned int from) +{ + const uint32_t pattern = CURRENT_PATTERN; + if (!pattern) { + return; + } + volatile uint8_t *const d = (volatile uint8_t *)(pattern + TRACK_STRIDE * to + TRACK_SETTINGS); + const volatile uint8_t *const s = (const volatile uint8_t *)(pattern + TRACK_STRIDE * from + TRACK_SETTINGS); + for (unsigned int k = 0; k < TRACK_SETTINGS_BYTES; ++k) { + const uint8_t b = s[k]; + if (d[k] != b) { + d[k] = b; + } + } +} + +/* Whether a track has steps of its own in the current pattern (a trig on any step). */ +static int has_steps(unsigned int track) +{ + const uint32_t pattern = CURRENT_PATTERN; + if (!pattern) { + return 0; + } + const volatile uint8_t *const trigs = (const volatile uint8_t *)(pattern + TRACK_STRIDE * track); + for (unsigned int s = 0; s < STEPS; ++s) { + if (trigs[2 * s + 1] & 5u) { + return 1; + } + } + return 0; +} + static void regroup(void) { uint16_t found = 0; @@ -80,8 +188,13 @@ static void regroup(void) } found |= (uint16_t)(1u << lead); next_voice[lead] = (uint8_t)lead; - /* The voices start as exact copies of the lead (every parameter, its sample slot included), so each note of - * a chord sounds alike whichever track plays it; later turns on the lead follow in core_voice_frame. */ + /* The voices start as exact copies of the lead, their sounds in the kit and their parameter targets (every + * parameter, its sample slot included), so each note of a chord sounds alike whichever track plays it; later + * turns on the lead follow in core_voice_frame. */ + for (unsigned int v = lead + 1; v < t; ++v) { + copy_sound(v, lead, 0, SOUND_WORDS); + copy_track_settings(v, lead); + } for (unsigned int j = 0; j < VALUES; ++j) { const int16_t value = TARGETS[VALUES * lead + j]; last[lead][j] = value; @@ -93,18 +206,26 @@ static void regroup(void) leads = found; } -/* The track a note-on of `lead` goes to: the first track of the group from the lead on that holds no note and is not - * muted, so the lead takes the first note of a chord (the trig keys and most MIDI senders send a chord's notes from - * the lowest up) and each next track the next; with every track busy, the next one in turn that is not muted, which - * ends its note as stock ends a track's note for a new one. */ -static unsigned int pick(unsigned int lead, uint32_t mutes) +static unsigned int group_size(unsigned int lead) { unsigned int size = 0; while (lead + size < TRACKS && lead_of[lead + size] == lead) { ++size; } + return size; +} + +/* The track a note-on of `lead` from the keys or MIDI goes to: the first track of the group from the lead on that holds + * no note from the keys or MIDI (a pattern note there gives way), is not taken by a note just recorded and is not + * muted, so the lead takes the first note of a chord (the trig keys and most MIDI senders send a chord's notes from the + * lowest up) and each next track the next; with every track busy, the next one in turn that is not muted, which ends + * its note as stock ends a track's note for a new one. */ +static unsigned int pick(unsigned int lead, uint32_t mutes) +{ + const unsigned int size = group_size(lead); for (unsigned int t = lead; t < lead + size; ++t) { - if (!((mutes >> t) & 1u) && !NOTE_SOURCE[t]) { + const uint32_t held = NOTE_SOURCE[t]; + if (!((mutes >> t) & 1u) && (!held || held == SEQUENCER) && !reserved(t)) { return t; } } @@ -119,6 +240,23 @@ static unsigned int pick(unsigned int lead, uint32_t mutes) return lead; } +/* The track a pattern note of `lead` goes to: the lead, or, while the lead holds a note, the first free voice track + * with no steps of its own (recorded chord notes there keep their track to themselves), so a long note rings on; with + * none, the lead, which ends its note as stock does. */ +static unsigned int pick_pattern(unsigned int lead, uint32_t mutes) +{ + if (!NOTE_SOURCE[lead]) { + return lead; + } + const unsigned int size = group_size(lead); + for (unsigned int t = lead + 1; t < lead + size; ++t) { + if (!((mutes >> t) & 1u) && !NOTE_SOURCE[t] && !reserved(t) && !has_steps(t)) { + return t; + } + } + return lead; +} + /* The pitch a record's note-off names, read as the frame's note-off path compares it with the held pitch * (0x4002e670): byte 2 of the trig's lock row, or of the record's own row (+0x24) when it has none. */ static int32_t record_pitch(const uint8_t *record) @@ -138,8 +276,12 @@ void core_voice_event(uint8_t *record) return; } if (!((leads >> lead) & 1u)) { - /* A linked voice track's own note (its trigs): to no track (16), so only its lead's notes play there. */ - if (lead_of[lead] != NONE) { + if (lead_of[lead] == NONE) { + return; + } + /* A linked voice track's own steps (chord notes recorded there) play as they are: its sound is the lead's + * copy. Its own key presses go to no track (16). */ + if (*(const uint32_t *)(record + 0x14) != SEQUENCER) { *(uint32_t *)(record + 0x10) = TRACKS; } return; @@ -154,6 +296,7 @@ void core_voice_event(uint8_t *record) * In a group that note is still ringing on its own track, so a note-on from the keys or MIDI (any source but * the sequencer, whose repeated notes should ring over) for a pitch a track of the group holds goes nowhere. */ const uint32_t source = *(const uint32_t *)(record + 0x14); + to = NONE; if (source != SEQUENCER) { const int32_t pitch = record_pitch(record); for (unsigned int t = lead; t < TRACKS && lead_of[t] == lead; ++t) { @@ -162,8 +305,24 @@ void core_voice_event(uint8_t *record) return; } } + /* A note live recording has just put on a track of the group plays on that track. */ + for (unsigned int k = 0; k < PENDING; ++k) { + if (pending[k].used && pending[k].lead == lead && pending[k].pitch == pitch) { + to = pending[k].track; + pending[k].used = 0; + break; + } + } + if (to == NONE) { + /* Not recorded yet: if live recording sees it later, it records it on the track it plays on. */ + to = pick(lead, mutes); + fresh[to].pitch = (int8_t)pitch; + fresh[to].age = 1; + } + } + if (to == NONE) { + to = pick_pattern(lead, mutes); } - to = pick(lead, mutes); } else { const uint32_t source = *(const uint32_t *)(record + 0x14); const int32_t pitch = record_pitch(record); @@ -186,12 +345,65 @@ void core_voice_event(uint8_t *record) } } -/* A whole track's targets from a lead's last values. Its own function, indexed, so the compiler does not reuse a - * post-incremented address register in one instruction (sdk/runtime/m68k_codegen.py refuses that). */ +/* Live recording of a lead's note (core_voice_record_entry, the live-record routine at 0x40048fb2): args points at + * its arguments (sequencer, track, note, velocity, flag, step, ...). The note is recorded on the track of the group it + * will play on, chosen as the frame will choose it, and a pending entry tells the frame so. A note the frame has + * already played (a fresh entry) is recorded on the track playing it. Any other note the group already holds (the keys + * sending a held note again) is not recorded (track 16: the routine returns at once). */ +void core_voice_record(uint32_t *args) +{ + const uint32_t lead = args[1]; + if (!leads || lead >= TRACKS || !((leads >> lead) & 1u)) { + return; + } + const int32_t pitch = (int32_t)args[2]; + for (unsigned int t = lead; t < TRACKS && lead_of[t] == lead; ++t) { + if (fresh[t].age && fresh[t].pitch == pitch) { + fresh[t].age = 0; + args[1] = t; + return; + } + } + for (unsigned int t = lead; t < TRACKS && lead_of[t] == lead; ++t) { + if (NOTE_SOURCE[t] && NOTE_SOURCE[t] != SEQUENCER && NOTE_PITCH[t] == pitch) { + args[1] = TRACKS; + return; + } + } + unsigned int free = PENDING; + for (unsigned int k = 0; k < PENDING; ++k) { + if (!pending[k].used) { + free = k; + } else if (pending[k].lead == lead && pending[k].pitch == pitch) { + args[1] = TRACKS; /* recorded already, not played yet */ + return; + } + } + const uint32_t mutes = MUTES_OVERRIDE ? ~MUTES_OVERRIDE : MUTES; + if ((mutes >> lead) & 1u) { + return; /* a muted lead plays nothing: record as stock does */ + } + const unsigned int to = pick(lead, mutes); + args[1] = to; + if (free < PENDING) { + pending[free].lead = (uint8_t)lead; + pending[free].track = (uint8_t)to; + pending[free].pitch = (int8_t)pitch; + pending[free].age = 0; + pending[free].used = 1; + } +} + +/* A whole track's targets from a lead's last values, writing only those that differ. Its own function, indexed, so + * the compiler does not reuse a post-incremented address register in one instruction (sdk/runtime/m68k_codegen.py + * refuses that). */ static __attribute__((noinline)) void copy_values(volatile int16_t *to, const volatile int16_t *from) { for (unsigned int j = 0; j < VALUES; ++j) { - to[j] = from[j]; + const int16_t value = from[j]; + if (to[j] != value) { + to[j] = value; + } } } @@ -199,6 +411,16 @@ void core_voice_frame(void) { const uint16_t taken = absorb; absorb = 0; + for (unsigned int k = 0; k < PENDING; ++k) { + if (pending[k].used && ++pending[k].age > PENDING_FRAMES) { + pending[k].used = 0; + } + } + for (unsigned int t = 0; t < TRACKS; ++t) { + if (fresh[t].age && ++fresh[t].age > FRESH_FRAMES) { + fresh[t].age = 0; + } + } for (unsigned int lead = 0; lead < TRACKS; ++lead) { if (!((leads >> lead) & 1u)) { continue; @@ -230,9 +452,27 @@ void core_voice_frame(void) if (v <= lead || v >= end) { v = lead + 1; } - sync_next[lead] = (uint8_t)(v + 1 < end ? v + 1 : lead + 1); - if (v < end && !NOTE_SOURCE[v]) { - copy_values(TARGETS + VALUES * v, last[lead]); + unsigned int slice = sync_slice[lead]; + if (slice >= SOUND_SLICES) { + slice = 0; + } + if (v + 1 < end) { + sync_next[lead] = (uint8_t)(v + 1); + } else { + /* Every voice had this part of its sound compared: the next part from the first voice on. */ + sync_next[lead] = (uint8_t)(lead + 1); + sync_slice[lead] = (uint8_t)((slice + 1) % SOUND_SLICES); + } + if (v < end) { + /* The voice's sound in the kit and its pattern track's settings follow the lead's (its own steps load them + * when they play). A sixth of the sound a frame, so a change reaches every voice within a few dozen + * frames and the frame interrupt stays short. (On a unit with the whole sound compared every frame, page + * changes on a linked track grew slow while the pattern ran; with a sixth a frame, not.) */ + copy_sound(v, lead, SLICE_WORDS * slice, SLICE_WORDS * (slice + 1)); + copy_track_settings(v, lead); + if (!NOTE_SOURCE[v]) { + copy_values(TARGETS + VALUES * v, last[lead]); + } } } }