Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ the project file format or the controls.
SAMPLE track's SRC page opens PREP on its file. A take put on a SAMPLE track with RESAMPLE's
OK opens PREP on the take, to trim its silence. RESAMPLE has a fifth length, **FREE**: OK
starts the take at once, with the transport running or stopped, and OK again ends it.
- **A section kit's samples play from its first step.** A sample a song kit names that differs
from the playing one loads during the section before, beside it, and plays from the boundary
instead of after a silent gap. A file too long for RAM, a zone track or a file that does not
fit beside the playing one still loads after the boundary. SAVE says `SAVING K2 UNCHANGED`
when the kit playing was edited since its section brought it: the save keeps the edit in the
project's kit, and the song kit stays as stored.
- **Next song.** On PROJECTS, push A queues the project under the cursor as the next song
(`NEXT FIRST`, shown on every screen). When the transport stops, at the end of a SONG chain or
on STOP, it opens while the last song's delay and reverb ring out, and under the internal clock
Expand Down
9 changes: 9 additions & 0 deletions app/commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ enum class Cmd : uint8_t {
// is 16.7 M frames -- 349 s of mono at 48 kHz, well past the 30 s above which docs/SPEC.md
// streams from the card instead of loading into RAM)
set_sample,
// A section's sample, staged (review 2026-11-30, proposal 9): set_sample's fields for a range
// in the track's staging slot that nothing plays yet, b: the section_kit hand-off it is for.
// The audio side keeps it with the held kit and takes it, slice points from
// Engine::staged_slices_for_load, when the kit lands (Status::section_kit_staged). A
// clear_sample on the track, or the kit dropped, lets it go unplayed.
stage_sample,
// Streaming (app/storage.hpp, "Streaming"). `set_stream` hands over a file too big for the
// pool: its head (resident, like a sample) and how to read the rest, from
// Engine::stream_for_load(track) -- more than 16 bytes, so through that inbox, written by the
Expand Down Expand Up @@ -902,6 +908,9 @@ struct Status {
// `section_kit_missed_entry` naming the entry, so the main context reads it late.
std::atomic<uint8_t> section_kit_done{0};
std::atomic<uint8_t> section_kit_landed{0};
// The tracks (bit t) whose staged sample (Cmd::stage_sample) the landed kit took, written
// before section_kit_done like section_kit_landed. 0 when the kit was dropped.
std::atomic<uint8_t> section_kit_staged{0};
std::atomic<uint8_t> section_kit_missed{0};
std::atomic<uint8_t> section_kit_missed_entry{0};
// Bumped as the last thing Cmd::project_loaded does: the main context takes the loaded
Expand Down
123 changes: 80 additions & 43 deletions app/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2383,6 +2383,10 @@ bool Engine::apply_arrangement(const Command &command, int, int) {
// Stopping a chain leaves the pattern it reached playing, rather than jumping
// back: a performer turning the chain off wants to stay where they are.
chain_.active = false;
// And a kit held for its next entry goes, with a file staged for it (review
// 2026-11-30, proposal 9): that memory is the playing samples' until the chain
// runs again and asks anew.
if (section_kit_entry_ >= 0) section_kit_let_go(0);
}
song_over_ = false;
// And record it, which nothing did: `ProjectSettings::chain_enabled` round-tripped
Expand Down Expand Up @@ -2636,6 +2640,51 @@ bool Engine::apply_project(const Command &command, int, int) {
return true;
}


// Cmd::set_sample's body: a range of the region to both of the track's readers, with the slice
// points and zones of its inboxes.
void Engine::apply_set_sample(int track, uint32_t offset, uint32_t frames, int channels) {
// At most 2^26 int16, so the product cannot overflow a uint32_t.
const uint32_t bytes = frames * uint32_t(channels) * uint32_t(sizeof(int16_t));

// Every field is checked against the region BEFORE a pointer is formed. The offset
// comes from the other side of the ring, and one past the end would be a read into
// whatever follows 64 MiB of DDR. The subtraction is written to avoid a wrap, as
// core/project.cpp's section bounds are. The even-offset test is not pedantry: a
// misaligned 16-bit read on the C674x does not fault, it silently reads the wrong
// address (core/pattern.hpp).
const bool usable = sample_region_ != nullptr && frames != 0 &&
offset % sizeof(int16_t) == 0 &&
offset <= sample_region_bytes_ &&
bytes <= sample_region_bytes_ - offset;
if (!usable) {
status_.bump_refused();
return;
}
const int16_t *data = sample_region_ + offset / sizeof(int16_t);
stop_stream(track);
pool_[track].sampler.set_sample(data, frames, channels, rate_);
pool_[track].sampler.set_slice_points(slice_inbox_[track]);
status_.slice_points[track].store(uint8_t(pool_[track].sampler.point_count()),
std::memory_order_relaxed);
// Zones (core/zones.hpp): the sampler checks the layout against the buffer itself.
// GRAIN plays zone 1, the track's `sample`, so a zone track hands it that file alone.
pool_[track].sampler.set_zones(zone_inbox_[track]);
const ZoneLayout &zones = zone_inbox_[track];
if (pool_[track].sampler.zone_count() > 0 && zones.frames[0] >= 4u) {
pool_[track].grain.set_sample(data + size_t(zones.base[0]) * size_t(channels),
zones.frames[0], channels, rate_);
} else {
pool_[track].grain.set_sample(data, frames, channels, rate_);
}
// SLC MAN against the file's length: zone 1's when it is one zone. Two or more zones
// do not slice.
if (pool_[track].sampler.zone_count() < 2) {
check_slice_man(track, pool_[track].sampler.zone_count() == 1 ? zones.frames[0]
: frames);
}
}

// Samples, streams and the audition.
bool Engine::apply_samples(const Command &command, int track, int) {
switch (command.cmd) {
Expand All @@ -2662,62 +2711,38 @@ bool Engine::apply_samples(const Command &command, int track, int) {
// A streamed sample stops being read here too, BEFORE the epoch: the card side closes
// the track's stream once it sees the bump.
stop_stream(track);
staged_[track].valid = false; // a staged sample is the old file's successor no more
// Bumped even for a track that hosts no Sampler. The loader waits for this change
// before it releases the range, so a track that was never reading anything must
// still report that it has stopped, or the wait never ends.
status_.bump_sample_epoch(track);
break;

case Cmd::set_sample: {
case Cmd::set_sample:
// To both readers, for the reason in clear_sample: the track's sample belongs to the
// track, and SAMPLE and GRAIN are two ways of playing it. Refusing a track that did
// not host a Sampler is why GRAIN never got a card sample.
// 24 bits of frames: 16.7 M frames, which is 349 s of mono at 48 kHz and well past
// the 30 s above which docs/SPEC.md streams from the card instead.
const uint32_t frames = uint32_t(command.c) | uint32_t(command.d) << 8 |
uint32_t(command.e) << 16;
const int channels = command.a == 2 ? 2 : 1;
// At most 2^26 int16, so the product cannot overflow a uint32_t.
const uint32_t bytes = frames * uint32_t(channels) * uint32_t(sizeof(int16_t));

// Every field is checked against the region BEFORE a pointer is formed. The offset
// comes from the other side of the ring, and one past the end would be a read into
// whatever follows 64 MiB of DDR. The subtraction is written to avoid a wrap, as
// core/project.cpp's section bounds are. The even-offset test is not pedantry: a
// misaligned 16-bit read on the C674x does not fault, it silently reads the wrong
// address (core/pattern.hpp).
const bool usable = sample_region_ != nullptr && frames != 0 &&
command.u % sizeof(int16_t) == 0 &&
command.u <= sample_region_bytes_ &&
bytes <= sample_region_bytes_ - command.u;
if (!usable) {
status_.bump_refused();
break;
}
const int16_t *data = sample_region_ + command.u / sizeof(int16_t);
stop_stream(track);
pool_[track].sampler.set_sample(data, frames, channels, rate_);
pool_[track].sampler.set_slice_points(slice_inbox_[track]);
status_.slice_points[track].store(uint8_t(pool_[track].sampler.point_count()),
std::memory_order_relaxed);
// Zones (core/zones.hpp): the sampler checks the layout against the buffer itself.
// GRAIN plays zone 1, the track's `sample`, so a zone track hands it that file alone.
pool_[track].sampler.set_zones(zone_inbox_[track]);
const ZoneLayout &zones = zone_inbox_[track];
if (pool_[track].sampler.zone_count() > 0 && zones.frames[0] >= 4u) {
pool_[track].grain.set_sample(data + size_t(zones.base[0]) * size_t(channels),
zones.frames[0], channels, rate_);
} else {
pool_[track].grain.set_sample(data, frames, channels, rate_);
}
// SLC MAN against the file's length: zone 1's when it is one zone. Two or more zones
// do not slice.
if (pool_[track].sampler.zone_count() < 2) {
check_slice_man(track, pool_[track].sampler.zone_count() == 1 ? zones.frames[0]
: frames);
apply_set_sample(track, command.u,
uint32_t(command.c) | uint32_t(command.d) << 8 |
uint32_t(command.e) << 16,
command.a == 2 ? 2 : 1);
break;

case Cmd::stage_sample:
// Kept for the held kit it was staged for, and only that one: one for a kit since
// dropped is let go here, and the main context releases its range when it sees the
// kit was not taken (Status::section_kit_staged).
if (section_kit_entry_ >= 0 && command.b == section_kit_hand_) {
StagedSample &staged = staged_[track];
staged.offset = command.u;
staged.frames = uint32_t(command.c) | uint32_t(command.d) << 8 |
uint32_t(command.e) << 16;
staged.channels = command.a == 2 ? 2 : 1;
staged.valid = true;
}
break;
}

case Cmd::set_stream: {
// The head is checked against the region exactly as set_sample's buffer is.
Expand Down Expand Up @@ -3579,13 +3604,25 @@ void Engine::chain_entry_started() {
}

void Engine::section_kit_let_go(uint8_t landed) {
uint8_t staged = 0;
if (landed != 0) {
section_kit_wanted_ = -1;
take_kit(section_kit_);
params_from_kit();
perform_dirty_ = true; // as Cmd::kit_recall: else the old kit goes on playing
// The samples the main context loaded during the section before (Cmd::stage_sample),
// so the section's first hit plays its own file; the rest reload after the boundary.
for (int t = 0; t < kTracks; t++) {
if (!staged_[t].valid) continue;
slice_inbox_[t] = staged_slice_inbox_[t];
zone_inbox_[t].count = 0;
apply_set_sample(t, staged_[t].offset, staged_[t].frames, staged_[t].channels);
staged = uint8_t(staged | (1u << t));
}
}
for (int t = 0; t < kTracks; t++) staged_[t].valid = false;
section_kit_entry_ = -1;
status_.section_kit_staged.store(staged, std::memory_order_relaxed);
status_.section_kit_landed.store(landed, std::memory_order_relaxed);
status_.section_kit_done.store(section_kit_hand_, std::memory_order_release);
}
Expand Down
14 changes: 14 additions & 0 deletions app/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ class Engine {
// And its zones (core/zones.hpp, ZoneLayout): where each file sits in the buffer and how it
// plays, count 0 for a plain file. The same inbox rule as the slice points.
ZoneLayout &zones_for_load(int track) { return zone_inbox_[track]; }
// A staged sample's slice points (Cmd::stage_sample), written by the main context before
// the post and read by the audio side when the kit it was staged for lands.
SlicePoints &staged_slices_for_load(int track) { return staged_slice_inbox_[track]; }

// The track's stream. The MAIN context opens and closes it, the AUDIO context reads it, and
// never both at once: the audio side reads only between a Cmd::stream_attach and its own
Expand Down Expand Up @@ -625,6 +628,17 @@ class Engine {
StreamInfo stream_inbox_[kTracks]{};
SlicePoints slice_inbox_[kTracks]{};
ZoneLayout zone_inbox_[kTracks]{};
SlicePoints staged_slice_inbox_[kTracks]{};
// Cmd::stage_sample's fields, per track, while the kit they are for is held.
struct StagedSample {
uint32_t offset = 0;
uint32_t frames = 0;
uint8_t channels = 1;
bool valid = false;
};
StagedSample staged_[kTracks]{};
// set_sample's body, for set_sample and for a staged sample taken at a section boundary.
void apply_set_sample(int track, uint32_t offset, uint32_t frames, int channels);
StreamState stream_state_[kTracks]{};
StreamFifo fifos_[kTracks];
enaga::files::Stream streams_[kTracks];
Expand Down
Loading