diff --git a/CHANGELOG.md b/CHANGELOG.md index 76ef9746..7e628288 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/app/commands.hpp b/app/commands.hpp index 5fa35eaa..2de046f9 100644 --- a/app/commands.hpp +++ b/app/commands.hpp @@ -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 @@ -902,6 +908,9 @@ struct Status { // `section_kit_missed_entry` naming the entry, so the main context reads it late. std::atomic section_kit_done{0}; std::atomic 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 section_kit_staged{0}; std::atomic section_kit_missed{0}; std::atomic section_kit_missed_entry{0}; // Bumped as the last thing Cmd::project_loaded does: the main context takes the loaded diff --git a/app/engine.cpp b/app/engine.cpp index 89981f43..eb75ba63 100644 --- a/app/engine.cpp +++ b/app/engine.cpp @@ -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 @@ -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) { @@ -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. @@ -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); } diff --git a/app/engine.hpp b/app/engine.hpp index cebd4ffd..9c183d42 100644 --- a/app/engine.hpp +++ b/app/engine.hpp @@ -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 @@ -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]; diff --git a/app/storage.cpp b/app/storage.cpp index 339ccc16..98e690f4 100644 --- a/app/storage.cpp +++ b/app/storage.cpp @@ -1447,6 +1447,8 @@ void Storage::forget_song_kits() { // What the browser showed and what a section asked for belong to the project that was. for (int i = kSongKitFirst; i < kKitBrowserSlots; i++) kit_slots_[i] = KitSlot{}; sk_failed_entry_ = -1; + live_kit_ = 0; + live_kit_edited_ = false; } void Storage::start_new() { @@ -1552,7 +1554,10 @@ void Storage::note_command(Cmd cmd, int live_bank, int target_bank) { if (mutates_pattern(cmd) && project_index_valid(bank, 0)) { bank_dirty_[bank] = true; } - if (mutates_kit(cmd)) kit_dirty_ = true; + if (mutates_kit(cmd)) { + kit_dirty_ = true; + if (live_kit_ != 0) live_kit_edited_ = true; + } if (mutates_project(cmd)) project_dirty_ = true; if (mutates_perform(cmd)) perform_dirty_ = true; } @@ -1639,9 +1644,13 @@ void Storage::on_card(bool usable) { collect_asked_ = false; if (job_ == Job::sample_epoch || job_ == Job::sample_read || job_ == Job::sample_convert || job_ == Job::sample_resample || job_ == Job::sample_chunk || job_ == Job::sample_widen) { - if (sample_track_ >= 0) pool_.fail(sample_track_); + if (sample_track_ >= 0) { + if (staging_) pool_.release(staging_slot(sample_track_)); + else pool_.fail(sample_track_); + } sample_track_ = -1; zone_load_ = false; + staging_ = false; } // A project load cut off half way has already written its first files into RAM: the files // read so far are the new project's, the rest still the old one's, and the audio side was @@ -2070,6 +2079,13 @@ void Storage::service() { return; } + // A held section kit's files, ahead of its boundary (start_stage), once nothing the + // playing project asked for is waiting. + if (stage_queue_ != 0 && sk_busy_ && !read_in_flight_) { + start_stage(); + return; + } + // A project's budget, applied once every track has been through the swap above. It cannot be // applied earlier: SamplePool::set_budget refuses to shrink below what is already reserved // rather than stranding a loaded sample, and until the clears have run the previous project's @@ -2405,7 +2421,14 @@ void Storage::service_section_kits() { if (status.section_kit_done.load(std::memory_order_acquire) != sk_hand_) return; sk_busy_ = false; const uint8_t landed = status.section_kit_landed.load(std::memory_order_relaxed); - if (landed != 0) section_kit_landed(landed == 2); + // Only what is still staged here: a sample job on the track since (start_next_sample) + // cleared it, and with it what the audio side took. + const uint8_t taken = + uint8_t(status.section_kit_staged.load(std::memory_order_relaxed) & staged_); + if (landed != 0) section_kit_landed(landed == 2, taken); + stage_drop(uint8_t(staged_ & ~taken)); + staged_ = 0; + stage_queue_ = 0; sk_entry_ = -1; } // A project being replaced or renamed, or a sample read owning the card: not this pass. @@ -2462,17 +2485,83 @@ void Storage::service_section_kits() { command.c = sk_hand_; command.d = late ? 1 : 0; post(command); + // What to stage for it: a file a track plays whole that differs from the one it plays now. + // A late kit lands at once, before anything could load. + stage_queue_ = 0; + if (late) return; + const Kit &next = engine_->section_kit_inbox(); + const Kit &now = project.kit; + for (int t = 0; t < kTracks; t++) { + const KitTrack &want = next.tracks[t]; + const bool plain = (want.zone_count == 0 && want.sample[0] != '\0') || + zone_plays_as_file(want); + if (plain && !zones_same_files(now.tracks[t], want)) { + stage_queue_ = uint8_t(stage_queue_ | (1u << t)); + } + } } -void Storage::section_kit_landed(bool late) { +void Storage::start_stage() { + for (int t = 0; t < kTracks; t++) { + if ((stage_queue_ & (1u << t)) == 0) continue; + stage_queue_ = uint8_t(stage_queue_ & ~(1u << t)); + // A range left from a stage that was never posted is nobody's. + if ((staged_ & (1u << t)) == 0) pool_.release(staging_slot(t)); + const KitTrack &want = engine_->section_kit_inbox().tracks[t]; + sample_track_ = t; + staging_ = true; + stage_hand_ = sk_hand_; + zone_load_ = false; + stage_overviews_[t].ready = false; + if (zone_plays_as_file(want)) { + start_sample_file(want.zones[0].path, (want.zones[0].flags & kZoneFlagLocal) != 0); + } else { + start_sample_file(want.sample, false); + } + return; + } +} + +void Storage::stage_drop(uint8_t tracks) { + // Never taken by the audio side (it holds no staged sample past letting go of its kit), so + // the ranges are free at once. + for (int t = 0; t < kTracks; t++) { + if ((tracks & (1u << t)) != 0) pool_.release(staging_slot(t)); + } +} + +void Storage::section_kit_landed(bool late, uint8_t taken) { if (late) sk_lates_++; - // "kit landed K2" or "kit late K2", for the emulator, which cannot hear it. - char line[20]; + // "kit landed K2" or "kit late K2", for the emulator, which cannot hear it; "kit landed K2 + // staged 1" when files loaded ahead played from the boundary. + int staged_count = 0; + for (int t = 0; t < kTracks; t++) staged_count += (taken >> t) & 1; + char line[32]; char *out = line; - append_text(out, late ? "kit late K" : "kit landed K", line + sizeof line - 2); + const char *end = line + sizeof line - 2; + append_text(out, late ? "kit late K" : "kit landed K", end); *out++ = char('0' + sk_slot_); + if (staged_count > 0) { + append_text(out, " staged ", end); + append_number(out, uint32_t(staged_count), end); + } *out = '\0'; enaga::log(line); + // The song kit playing, for SAVE's word on it (live_kit_edited). + live_kit_ = sk_slot_; + live_kit_edited_ = false; + // The staged files are the tracks' own now: the audio side took them before it answered, + // so the ranges they replaced (and a stream's file) are read by nothing. + for (int t = 0; t < kTracks; t++) { + if ((taken & (1u << t)) == 0) continue; + end_stream(t); + pool_.swap(t, staging_slot(t)); + pool_.release(staging_slot(t)); + overviews_[t] = stage_overviews_[t]; + file_ms_[t] = stage_ms_[t]; + file_rate_[t] = stage_rate_[t]; + stage_landings_++; + } // As a recall from the browser (run_kit_load), after the fact: the audio side has the new // sounds, and a track whose files differ goes through the ordinary swap. Until its file is // in it plays nothing new, which is why a KIT that changes samples is not on the bar yet. @@ -2481,7 +2570,7 @@ void Storage::section_kit_landed(bool late) { for (int t = 0; t < kTracks; t++) { const bool same = zones_same_files(kit.tracks[t], landed.tracks[t]); zones_copy(kit.tracks[t], landed.tracks[t]); - if (same) continue; + if (same || (taken & (1u << t)) != 0) continue; sample_queue_ = uint8_t(sample_queue_ | (1u << t)); } unfreeze_all(); @@ -2696,6 +2785,8 @@ void Storage::run_kit_save() { } void Storage::run_kit_load() { + live_kit_ = 0; // the kit playing is the browser's now, not a section's + live_kit_edited_ = false; const int slot = kit_load_slot_; kit_load_slot_ = -1; begin_job(Job::kit_load); @@ -3114,6 +3205,12 @@ void Storage::step_sample_epoch() { // stream too, if it played one: clear_sample stopped the reading before the epoch moved. end_stream(track); pool_.release(track); + // A staged file for the track was let go with the clear (Engine, Cmd::clear_sample), so its + // range is nobody's either; the boundary loads the kit's file after it, as before preloading. + if ((staged_ & (1u << track)) != 0) { + staged_ = uint8_t(staged_ & ~(1u << track)); + pool_.release(staging_slot(track)); + } // A track the kit names no file for is finished here: back on the placeholder (Cmd::clear_sample), // its memory given back. That is how a project load takes the previous project's sample off a track. @@ -3279,7 +3376,7 @@ void Storage::step_sample_convert() { return; } if (drawing()) { - overview_builder_.begin(overviews_[track], out_frames_); + overview_builder_.begin(staging_ ? stage_overviews_[track] : overviews_[track], out_frames_); note_file_info(track, out_frames_, uint32_t(ENAGA_SAMPLE_RATE), format_.rate); } } @@ -3425,7 +3522,7 @@ void Storage::step_sample_chunk() { whole = "NO ROOM"; } // A zone never streams (the design: a zone must fit in RAM). - if (whole != nullptr && zone_load_) { + if (whole != nullptr && (zone_load_ || staging_)) { fail_sample(whole); return; } @@ -3445,7 +3542,8 @@ void Storage::step_sample_chunk() { // A stream's picture is on the whole file's scale, so its playhead lines up; only the // head is read now, so only the head is drawn (OverviewBuilder::finish_head). if (drawing()) { - overview_builder_.begin(overviews_[track], streaming_ ? stream_total_ : out_frames_); + overview_builder_.begin(staging_ ? stage_overviews_[track] : overviews_[track], + streaming_ ? stream_total_ : out_frames_); note_file_info(track, streaming_ ? stream_total_ : out_frames_, streaming_ ? format_.rate : uint32_t(ENAGA_SAMPLE_RATE), format_.rate); } @@ -3697,6 +3795,10 @@ void Storage::hand_sample_over() { } const int channels = int(format_.channels); + if (staging_) { + hand_stage_over(track, channels); + return; + } if (out_done_ == 0) { pool_.fail(track); finish_job("EMPTY"); @@ -3728,6 +3830,34 @@ void Storage::hand_sample_over() { finish_job(nullptr); } +// A stage's handover (preload, app/storage.hpp): the staged range, its slice points and picture +// wait beside the playing ones, and Cmd::stage_sample tells the audio side where it is. A kit let +// go meanwhile, or a file the stage cannot use, gives the range back without a word. +void Storage::hand_stage_over(int track, int channels) { + staging_ = false; + const int slot = staging_slot(track); + if (!sk_busy_ || sk_hand_ != stage_hand_ || out_done_ == 0 || out_done_ > 0xFFFFFFu || + !pool_.finish(slot, out_done_, channels, uint32_t(ENAGA_SAMPLE_RATE))) { + pool_.release(slot); + finish_job(nullptr); + return; + } + onsets_.finish(out_done_, engine_->staged_slices_for_load(track)); + overview_builder_.finish(out_done_); + staged_ = uint8_t(staged_ | (1u << track)); + Command command; + command.cmd = Cmd::stage_sample; + command.track = uint8_t(track); + command.a = uint8_t(channels); + command.b = stage_hand_; + command.u = pool_.slot(slot).offset; + command.c = uint8_t(out_done_ & 0xFFu); + command.d = uint8_t((out_done_ >> 8) & 0xFFu); + command.e = uint8_t((out_done_ >> 16) & 0xFFu); + post(command); + finish_job(nullptr); +} + // The handover: an offset, a frame count and a channel count, and nothing else crosses. The // audio side checks all three against the region before it forms a pointer. void Storage::post_set_sample(int track, uint32_t offset, uint32_t frames, int channels) { @@ -3752,8 +3882,8 @@ bool Storage::drawing() const { return !zone_load_ || zone_index_ == 0; } // track's range. Sets out_offset_. False when it does not fit. bool Storage::reserve_out(int track, uint32_t bytes) { if (!zone_load_) { - if (!pool_.reserve(track, bytes)) return false; - out_offset_ = pool_.slot(track).offset; + if (!pool_.reserve(load_slot(track), bytes)) return false; + out_offset_ = pool_.slot(load_slot(track)).offset; return true; } const SampleSlot &slot = pool_.slot(track); @@ -3773,10 +3903,17 @@ void Storage::fail_sample(const char *why) { return; } if (zone_load_) log_zones(0, why); - if (sample_track_ >= 0) pool_.fail(sample_track_); + const bool stage = staging_; + staging_ = false; + if (sample_track_ >= 0) { + // A stage that cannot be had leaves the file to load after the boundary, as before + // preloading, and says nothing: the track plays on. + if (stage) pool_.release(staging_slot(sample_track_)); + else pool_.fail(sample_track_); + } sample_track_ = -1; zone_load_ = false; - finish_job(why); + finish_job(stage ? nullptr : why); } // One log line a zone track's load, `zones ` or `zones ` (n of them in), so a diff --git a/app/storage.hpp b/app/storage.hpp index ee624936..15df65a8 100644 --- a/app/storage.hpp +++ b/app/storage.hpp @@ -491,6 +491,17 @@ class Storage { // slot (1-8) that failed, for `KIT 3 EMPTY`. uint8_t section_kit_lates() const { return sk_lates_; } uint8_t section_kit_errors() const { return sk_errors_; } + // Preload (review 2026-11-30, proposal 9): the tracks whose file for the held section kit is + // loaded and handed over (Cmd::stage_sample), and how many such files have played from a + // boundary since start. Tests and the `kit landed` log line. + uint8_t staged_tracks() const { return staged_; } + uint32_t staged_landings() const { return stage_landings_; } + // The song kit a section landed last (1-8), 0 after a project load or a recall from the kit + // browser, and whether the kit was edited since it landed. SAVE writes the kit as it plays + // into KIT.BIN and leaves KIT_n.BIN as stored, so the section's next start plays the stored + // kit: SAVE says so when edits would be left out of it (review 2026-11-30, proposal 9). + uint8_t live_song_kit() const { return live_kit_; } + bool live_kit_edited() const { return live_kit_ != 0 && live_kit_edited_; } uint8_t section_kit_error() const { return sk_error_slot_; } // --- your own sounds (core/usersounds.hpp) ----------------------------- @@ -616,8 +627,11 @@ class Storage { void note_file_info(int track, uint32_t frames, uint32_t frames_rate, uint32_t file_rate) { if (track < 0 || track >= kTracks || frames_rate == 0) return; // 32-bit only (a 64-bit divide drags libgcc's unwinder into the link, see progress()). - file_ms_[track] = frames / frames_rate * 1000u + frames % frames_rate * 1000u / frames_rate; - file_rate_[track] = file_rate; + // A staged file's (preload, below) waits beside the playing one's until its kit lands. + uint32_t *ms = staging_ ? stage_ms_ : file_ms_; + uint32_t *rate = staging_ ? stage_rate_ : file_rate_; + ms[track] = frames / frames_rate * 1000u + frames % frames_rate * 1000u / frames_rate; + rate[track] = file_rate; } // True while a PROJECT load is in flight -- the job that rewrites pattern memory a pass at a @@ -815,7 +829,29 @@ class Storage { // song kit it was for, and whether it was late; the Status counters last seen. void service_section_kits(); void forget_song_kits(); - void section_kit_landed(bool late); + void section_kit_landed(bool late, uint8_t taken); + + // Preload (review 2026-11-30, proposal 9). A held section kit's files that differ from the + // playing ones are loaded, one job each, into the tracks' staging slots (core/samplepool.hpp) + // during the section before, and handed over with Cmd::stage_sample. The audio side takes + // them at the boundary with the kit (Status::section_kit_staged); the main context then + // swaps the slots and releases the ranges they replaced, which nothing reads any more. A + // file that would stream, a zone track, a file that does not fit beside the playing one, or + // a kit dropped: nothing is staged, and the file loads after the boundary as before. + void start_stage(); + void hand_stage_over(int track, int channels); + void stage_drop(uint8_t tracks); // ranges staged and never taken + int load_slot(int track) const { return staging_ ? staging_slot(track) : track; } + uint8_t stage_queue_ = 0; // tracks to stage for the held kit + uint8_t staged_ = 0; // staged and posted, waiting for the boundary + uint8_t stage_hand_ = 0; // the hand-off the job in flight stages for + bool staging_ = false; // the sample job in flight is a stage + uint32_t stage_landings_ = 0; + uint8_t live_kit_ = 0; + bool live_kit_edited_ = false; + WaveOverview stage_overviews_[kTracks]; + uint32_t stage_ms_[kTracks] = {}; + uint32_t stage_rate_[kTracks] = {}; bool sk_busy_ = false; // posted, and the audio side has not answered yet uint8_t sk_hand_ = 0; int sk_entry_ = -1; // the entry of the last kit posted, -1 none diff --git a/app/ui_perfmix.hpp b/app/ui_perfmix.hpp index 21591225..0da9ae6d 100644 --- a/app/ui_perfmix.hpp +++ b/app/ui_perfmix.hpp @@ -901,7 +901,19 @@ case MenuItem::save: storage_.request_save(); close_menu(false); - notice("SAVING"); + // Edits made while a section's song kit plays are saved with the project, and + // the song kit stays as stored, so the section brings it back without them + // (review 2026-11-30, proposal 9): said, not guessed at. + if (storage_.live_kit_edited()) { + char *out = notice_buffer_; + append(out, "SAVING K"); + *out++ = char('0' + storage_.live_song_kit()); + append(out, " UNCHANGED"); + *out = 0; + notice(notice_buffer_); + } else { + notice("SAVING"); + } break; case MenuItem::save_as: if (!storage_.card_usable()) { diff --git a/core/samplepool.cpp b/core/samplepool.cpp index b21facc0..32779906 100644 --- a/core/samplepool.cpp +++ b/core/samplepool.cpp @@ -208,6 +208,13 @@ const SampleSlot &SamplePool::slot(int index) const { return slot_ok(index) ? slots_[index] : kNoSlot; } +void SamplePool::swap(int a, int b) { + if (!slot_ok(a) || !slot_ok(b) || a == b) return; + const SampleSlot held = slots_[a]; + slots_[a] = slots_[b]; + slots_[b] = held; +} + // --- reporting ------------------------------------------------------------ uint32_t SamplePool::used() const { diff --git a/core/samplepool.hpp b/core/samplepool.hpp index 543be4e8..f5a58ca0 100644 --- a/core/samplepool.hpp +++ b/core/samplepool.hpp @@ -58,8 +58,14 @@ enum class SampleState : uint8_t { empty = 0, loading, ready, failed, count }; // Slots 0-7 are the tracks' samples. Slots 8-15 (2026-11-23) are the tracks' FROZEN loops // (app/engine.hpp, "Freeze"): a second range per track, so freezing a SAMPLE or GRAIN track // keeps its own sample loaded and unfreezing is instant. The budget is shared by both. -constexpr int kPoolSlots = 2 * kTracks; +// Slots 16-23 (review 2026-11-30, proposal 9) are the tracks' STAGED samples: the file a +// section's KIT names, loaded during the section before so it plays from the boundary. Nothing +// plays a staged range until the audio side takes it at the boundary; then `swap` makes it the +// track's own and the range it replaced waits in the staging slot until the audio side has let +// it go (app/storage.cpp, "preload"). +constexpr int kPoolSlots = 3 * kTracks; constexpr int freeze_slot(int track) { return kTracks + track; } +constexpr int staging_slot(int track) { return 2 * kTracks + track; } const char *sample_state_name(SampleState state); // "", "LOAD", "OK", "FAIL" @@ -111,6 +117,10 @@ class SamplePool { const SampleSlot &slot(int index) const; + // Trade two slots' records, ranges and states, moving no bytes: a staged sample becomes the + // track's own and the track's old range sits in the staging slot until it is released. + void swap(int a, int b); + uint32_t budget() const { return budget_; } uint32_t used() const; uint32_t free_bytes() const { return budget_ - used(); } diff --git a/docs/CONTROLS.md b/docs/CONTROLS.md index 3f1f9848..f541b690 100644 --- a/docs/CONTROLS.md +++ b/docs/CONTROLS.md @@ -748,7 +748,11 @@ a CHAIN entry opens its SECTION view; the list's own keys are unchanged. - **A KIT is read from the card one entry ahead** and lands on its entry's first step. One the entry could not get in time (the chain's first entry as it starts, a slow card) lands at once and says `KIT LATE`; an empty or damaged song kit says `KIT K3 NOT READ` and the entry plays - with the kit it has. A track whose sample differs is silent until its file is in. + with the kit it has. A sample that differs loads during the entry before, into the track's + staging range beside the playing one, and plays from the boundary (review 2026-11-30, proposal + 9); one that does not fit beside it, would stream, or is a zone track's is silent until it is + in, after the boundary. SAVE with a song kit playing that was edited since it landed says + `SAVING K2 UNCHANGED`: the save keeps the edit in KIT.BIN, not in KIT_2.BIN. - `--` leaves that thing as it is, so an entry with every field `--` plays exactly as a chain entry always has. NRG, BPM and TRN leave `--` on any turn, at ENERGY 0, the tempo playing and TRANSPOSE 0. diff --git a/docs/DESIGN_NOTES.md b/docs/DESIGN_NOTES.md index e82181e7..06caa6c3 100644 --- a/docs/DESIGN_NOTES.md +++ b/docs/DESIGN_NOTES.md @@ -394,14 +394,26 @@ context (`Storage::service_section_kits`) reads the kit of `chain_next_entry` in holds it and applies it in `chain_entry_started` before SCN and the morph (`take_kit`, the kit_recall body), so the section's scene morphs from the new kit. An entry that starts without its kit bumps `Status::section_kit_missed`; main then reads it late and it lands at once (`KIT LATE`). -A held kit is dropped on a chain edit and a project load. Samples the kit names that differ from -the loaded ones reload after the boundary (the kit-load path); preloading them is a later step. +A held kit is dropped on a chain edit, the chain turned off and a project load. Samples the kit +names that differ from the loaded ones are staged (review 2026-11-30, proposal 9): once the kit is +read ahead, `Storage::start_stage` loads each plain file into the track's staging slot +(core/samplepool.hpp, `staging_slot`, slots 16-23 in the same 64 MiB budget) and posts +`Cmd::stage_sample`; the engine keeps it with the held kit and applies it in +`section_kit_let_go` after `take_kit`, reporting the tracks in `Status::section_kit_staged`. The +main context then swaps the pool records and releases the replaced range, which nothing reads +since the audio side changed pointers before it answered. A staged slice-point inbox keeps the +playing file's points and picture untouched until then. Not staged, and loaded after the boundary +as before: a zone track, a file that would stream, one that does not fit beside the playing one, +and anything when the kit is dropped (its ranges go back at once). A sample job on a staged track +(clear_sample) lets the staged file go on both sides. `tests/scenario_sectionkit.sh` stores K2, sets entry 2 to it and checks it lands on time. What SAVE stores (review 2026-11-30, B7; the design doc's "Saving"): a landed kit is RAM's kit and marks KIT.BIN dirty, so a save writes the kit that played last as KIT.BIN and never touches KIT_n.BIN. An edit made while a section's kit plays is in RAM's kit only; the next entry naming that kit reads KIT_n.BIN again and the edit is gone from what plays (a save before then keeps it -in KIT.BIN). Writing edits back to the song kit is the review's proposal 9, not decided. +in KIT.BIN). SAVE says it when the live song kit was edited since it landed +(`SAVING K2 UNCHANGED`, `Storage::live_kit_edited`). Roland chose 2026-11-30 to keep song kits +as stored: SAVE never writes KIT_n.BIN; KIT's third page stores one on purpose. **Song switching (2026-11-30).** NEXT is push A on PROJECTS, the knob that turns the cursor (push D there collects the open project's samples). RAM holds one project, so nothing is double-buffered: NEXT (`Storage::queue_next`) only remembers a name, and the transport's stop opens it through the diff --git a/docs/FEATURES.md b/docs/FEATURES.md index da81671e..a68987c9 100644 --- a/docs/FEATURES.md +++ b/docs/FEATURES.md @@ -168,7 +168,8 @@ FM4 and GRAIN machines. | Chain of up to 64 entries, repeats X1-X16 | v0.1.0 | ○ (3b, 12) | | | **Song mode**: per-entry mute recall, SONG plays once and stops | v0.1.0 | ○ (13i) | | | **Song sections**: a chain entry's SCN, MRP (SNAP, RIDE, BACK, SNAP A), NRG, BPM, TRN and MUT CAP, applied as it starts; the SECTION view (push A on CHAIN) with quick fill (hold OK); saved in PROJECT.BIN section 10 | v0.6.0 | ○ (W71), E (scenario_sections.sh) | BPM is skipped under an outside clock. Quick fill writes SNAP or SNAP A, whichever side the morph is nearer. TRN (W82) is byte 7, which older builds ignore: they play the entry untransposed | -| **Kit per section**: a section's KIT (third SECTION page, knob A) recalls one of eight song kits K1-K8, KIT_1..8.BIN in the project folder, on the entry's first beat; the next entry's kit is read ahead; the KIT browser's third page stores and loads the song kits | Unreleased | ○ (W105), E (scenario_sectionkit.sh) | A kit read too late lands at once and says `KIT LATE`. A sample the kit needs and memory does not hold loads after the boundary, silent until it is in. Song kits need a saved project; SAVE AS and rename carry them. Quick fill leaves KIT `--` | +| **Kit per section**: a section's KIT (third SECTION page, knob A) recalls one of eight song kits K1-K8, KIT_1..8.BIN in the project folder, on the entry's first beat; the next entry's kit is read ahead; the KIT browser's third page stores and loads the song kits | Unreleased | ○ (W105), E (scenario_sectionkit.sh) | A kit read too late lands at once and says `KIT LATE`. A sample the kit needs loads during the section before when it fits beside the playing one (see below). Song kits need a saved project; SAVE AS and rename carry them. Quick fill leaves KIT `--` | +| **Section kit samples load ahead** (review 2026-11-30, proposal 9): a song kit's differing sample loads during the section before into the track's staging range and plays from the boundary; SAVE says `SAVING K2 UNCHANGED` after an edit to the live song kit | this PR | ○ (W125) | Zone tracks, streamed files and files that do not fit beside the playing one load after the boundary (host: test_storage, test_samplepool) | | **Next song**: push A on PROJECTS queues a project as NEXT; the transport's stop (SONG END or STOP) opens it while the tails ring, and it starts by itself under the internal clock once its samples are in | Unreleased | ○ (W106), E (scenario_nextsong.sh) | Under an outside clock it opens on the outside STOP and plays on the outside START. Unsaved edits take a second push. Writes nothing; no set list file yet | | **ARR REC**: a jam written into the chain a pass at a time (a repeat when nothing changed, a new section entry with MUT CAP, SCN, MRP SNAP/SNAP A, NRG, TRN, BPM when something did; X16 split; 64 entries); ARR UNDO swaps the chain from before back in, one level | v0.6.0 | ○ (W74), E (scenario_arrrec.sh) | A pass is what played at its start (12 ticks of grace); a cut pass counts from half. No BPM under an outside clock. Track Launch: see the next row | | **ARR REC keeps Track Launch**: a pass with a borrowed lane is CAPTUREd into the first free slot of the bank without switching, and the entry names it (`LAUNCH IN A05`); the same mix in a row shares the slot; no slot or too many locks: the playing pattern alone (`LAUNCH NOT KEPT`) | v0.6.0 | ○ (W76), E (scenario_arrlaunch.sh) | Parts replay from the slot's step 1 (a NOW or grid launch replays shifted). A slot a queued change or launch is about to land on is never taken. ARR UNDO empties the captured slots (not one edited since or playing), and such a take cannot be redone (W82) | diff --git a/docs/MANUAL.md b/docs/MANUAL.md index 5cf5a817..b768b007 100644 --- a/docs/MANUAL.md +++ b/docs/MANUAL.md @@ -1455,13 +1455,17 @@ C BPM, D TRN) and **KIT** (A KIT). Every field reads `--` until you turn it, and machine, a sample, a whole drum kit for the drop. Store up to eight on the KIT page's third page (below, "The kit library"), then pick one with KIT. STRATA reads it from the card while the entry before plays, so it lands on the first step of its entry and the beat goes on. The tracks it -changes start their new sound there; a track whose sample is another file is silent until that -file is in. If the kit could not be read in time, it lands at once and STRATA says `KIT LATE`. +changes start their new sound there. A sample the kit names that is another file loads during the +entry before too, beside the one playing, and plays from the section's first step; one that does +not fit in the free sample memory beside it, a file long enough to stream, or a track with zones +loads after the boundary instead and is silent until it is in. If the kit could not be read in +time, it lands at once and STRATA says `KIT LATE`. A section kit makes the project UNSAVED, as a recall does. A save writes the kit that is playing as the project's own kit (KIT.BIN), whichever section brought it, and leaves the song kits as they are. An edit made while a section's kit plays belongs to the project's kit, not to the song kit: -the next entry that names that song kit brings the stored one back. To keep an edit in a song -kit, store it again on KIT's third page (`FUNC+DOWN`). +the next entry that names that song kit brings the stored one back, and SAVE says so +(`SAVING K2 UNCHANGED`). To keep an edit in a song kit, store it again on KIT's third page +(`FUNC+DOWN`). A push on a knob puts its field back to `--`. UP/DOWN go to the previous or next entry without leaving the view, and OK or CLR go back to the list. **Hold OK** to fill the fields from what diff --git a/docs/UNIT_CHECKLIST.md b/docs/UNIT_CHECKLIST.md index 790f3f24..bc515268 100644 --- a/docs/UNIT_CHECKLIST.md +++ b/docs/UNIT_CHECKLIST.md @@ -3774,3 +3774,10 @@ EDIT, the ninth page, OPS: the second knob reads ACC `64`. - Notes without an accent do not change as ACC turns. `____` - A project saved before this build with an FM4 track and accents: opens with ACC `0` and sounds as it did. `____` + +**W125. A section kit's sample on the first step (4 min).** The W105 chain, with K2's SAMPLE +track on a different file from the kit's own (a loud one-shot on step 1 is easiest to hear). +- CHAIN on, PLAY. Entry 2's first step plays K2's file, with no gap; entry 1 plays its own file + up to the boundary. Again on the next pass. `____` +- On entry 2, turn a knob of any track, then FUNC + HOME, OK: `SAVING K2 UNCHANGED`. Let the + chain come round to entry 2 again: the stored K2 plays, without the edit. `____` diff --git a/tests/scenario_prep_slice.sh b/tests/scenario_prep_slice.sh index 375cec52..ce92cda1 100755 --- a/tests/scenario_prep_slice.sh +++ b/tests/scenario_prep_slice.sh @@ -13,7 +13,7 @@ # OK -> `prep slice add `: halfway to the next point # push C, OK -> the slice plays, and OK chops at the playhead: # `prep slice add chop` -# CLR, CLR -> `POINT REMOVED` +# push B, push B -> `POINT REMOVED` # screen key LEFT, OK, OK -> TRIM, `ZONES`, `TRACK MENU` # # The menu opens on OK held 500 ms, longer than emulate.py's --key holds, so this drives the @@ -163,11 +163,11 @@ d.check(chop is not None and chop.endswith("chop"), "OK while it played chopped f"log={d.log[-12:]}") d.tap(PUSH_C) d.go(30 * M) -d.tap(CLR) +d.tap(PUSH_B) d.go(30 * M) -d.tap(CLR) +d.tap(PUSH_B) d.check(wait_prefix("POINT ", limit=60 * M) is not None and - any(line.endswith(" REMOVED") for line in d.log), "CLR twice removed a point", + any(line.endswith(" REMOVED") for line in d.log), "push B twice removed a point", f"log={d.log[-12:]}") d.go(1000 * M) # the beat plays the sliced loop diff --git a/tests/scenario_sectionkit.sh b/tests/scenario_sectionkit.sh index 65e6a307..be1be1ac 100755 --- a/tests/scenario_sectionkit.sh +++ b/tests/scenario_sectionkit.sh @@ -17,7 +17,7 @@ # PROJECT, SELECT_RIGHT, OK -> CHAIN, entry 2 of 6 # push A, SELECT_RIGHT x2 -> SECT 2/6, KIT PAGE # turn A +2, CLR -> K2, CHAIN -# FUNC + HOME, DOWN to CHAIN, OK -> CHAIN ON: SECTION 2 K2, kit landed K2 +# FUNC + HOME, SET, DOWN to CHAIN, OK -> CHAIN ON: SECTION 2 K2, kit landed K2 # # Keys (third_party/enaga/include/enaga/abi.h): F5 = 22, F1 = 19, F4 = 27, F8 = 28, OK = 23, # RIGHT = 18, STEP 2 = 33, DOWN = 25, CLR = 30, F3 = 20, SELECT_RIGHT = 9, push A = 1. A chord is @@ -61,13 +61,16 @@ keys+=(--key 19@1040000000 --key 25@1045000000 --key 30@1120000000) keys+=(--key 20@1180000000 --key 9@1240000000 --key 23@1300000000 --key 1@1360000000) keys+=(--key 9@1420000000 --key 9@1480000000 --turn 0:2@1540000000 --key 30@1600000000) keys+=(--key 19@1680000000 --key 27@1685000000) -# DOWN from SAVE to CHAIN, counted from the FUNC menu's own order (app/ui_perfmix.hpp, MenuItem; -# tools/menu_rows.py), so a row added above it does not move the walk off CHAIN. -downs=$(python3 -B tools/menu_rows.py app/ui_perfmix.hpp MenuItem chain) +# SET's screen key jumps to the FUNC menu's settings, then DOWN to CHAIN, counted from the menu's +# own order (app/ui_perfmix.hpp, MenuItem; tools/menu_rows.py), so a row added above it does not +# move the walk off CHAIN. From SET the walk stays short whatever the DO part holds. +keys+=(--key 9@1740000000) +downs=$(( $(python3 -B tools/menu_rows.py app/ui_perfmix.hpp MenuItem chain) - + $(python3 -B tools/menu_rows.py app/ui_perfmix.hpp MenuItem click) )) for ((i = 0; i < downs; i++)); do - keys+=(--key "25@$((1740000000 + i * 40000000))") + keys+=(--key "25@$((1780000000 + i * 40000000))") done -keys+=(--key "23@$((1740000000 + downs * 40000000 + 20000000))") +keys+=(--key "23@$((1780000000 + downs * 40000000 + 20000000))") echo "running the emulator for $CYCLES cycles: a song kit, a section naming it, the chain ..." python3 -B tools/enaga_cached.py emulate "$STAGE" --cycles "$CYCLES" "${keys[@]}" \ diff --git a/tests/scenario_sections.sh b/tests/scenario_sections.sh index 8d24a316..1a5703b0 100755 --- a/tests/scenario_sections.sh +++ b/tests/scenario_sections.sh @@ -18,7 +18,7 @@ # turn B +1, push B -> NRG -- # turn A +1 -> MRP SNAP # CLR -> CHAIN -# FUNC + HOME, DOWN to CHAIN, OK -> CHAIN ON: SECTION 2 S3 SNAP +# FUNC + HOME, SET, DOWN to CHAIN, OK -> CHAIN ON: SECTION 2 S3 SNAP # # Keys (third_party/enaga/include/enaga/abi.h): F5 = 22, F3 = 20, SELECT_RIGHT = 9, OK = 23, # push A = 1, push B = 0, CLR = 30, F1 = 19, F4 = 27, DOWN = 25. Encoders: A 0, B 1, D 3. A chord @@ -55,13 +55,16 @@ keys=(--key 22@300000000 --key 20@380000000 --key 9@440000000 --key 23@500000000 keys+=(--key 1@560000000 --turn 3:3@620000000 --key 9@680000000) keys+=(--turn 1:1@740000000 --key 0@800000000 --turn 0:1@860000000 --key 30@920000000) keys+=(--key 19@1000000000 --key 27@1005000000) -# DOWN from SAVE to CHAIN, counted from the FUNC menu's own order (app/ui_perfmix.hpp, MenuItem; -# tools/menu_rows.py), so a row added above it does not move the walk off CHAIN. -downs=$(python3 -B tools/menu_rows.py app/ui_perfmix.hpp MenuItem chain) +# SET's screen key jumps to the FUNC menu's settings, then DOWN to CHAIN, counted from the menu's +# own order (app/ui_perfmix.hpp, MenuItem; tools/menu_rows.py), so a row added above it does not +# move the walk off CHAIN. From SET the walk stays short whatever the DO part holds. +keys+=(--key 9@1060000000) +downs=$(( $(python3 -B tools/menu_rows.py app/ui_perfmix.hpp MenuItem chain) - + $(python3 -B tools/menu_rows.py app/ui_perfmix.hpp MenuItem click) )) for ((i = 0; i < downs; i++)); do - keys+=(--key "25@$((1060000000 + i * 40000000))") + keys+=(--key "25@$((1100000000 + i * 40000000))") done -keys+=(--key "23@$((1060000000 + downs * 40000000 + 20000000))") +keys+=(--key "23@$((1100000000 + downs * 40000000 + 20000000))") echo "running the emulator for $CYCLES cycles: a section's fields, then the chain plays it ..." python3 -B tools/enaga_cached.py emulate "$STAGE" --cycles "$CYCLES" "${keys[@]}" \ diff --git a/tests/test_samplepool.cpp b/tests/test_samplepool.cpp index 2b43804a..ee529e58 100644 --- a/tests/test_samplepool.cpp +++ b/tests/test_samplepool.cpp @@ -876,7 +876,8 @@ TEST(samplepool_a_freeze_slot_shares_the_budget_and_not_the_tracks_count) { SamplePool pool; pool.init(16 * kMib); CHECK_EQ(freeze_slot(0), kTracks); - CHECK_EQ(freeze_slot(kTracks - 1), kPoolSlots - 1); + CHECK_EQ(freeze_slot(kTracks - 1), 2 * kTracks - 1); + CHECK_EQ(staging_slot(kTracks - 1), kPoolSlots - 1); CHECK(pool.reserve(3, 6 * kMib)); // track 4's sample CHECK(pool.reserve(freeze_slot(3), 6 * kMib)); // and its frozen loop, both held CHECK(pool.finish(freeze_slot(3), 1000, 1, 48000)); @@ -937,3 +938,29 @@ TEST(samplepool_trim_between_two_ranges_leaves_a_usable_hole) { CHECK_EQ(pool.slot(2).offset, 2u * kMib); CHECK(sane(pool)); } + +// Review 2026-11-30, proposal 9: a section's sample is staged beside the track's own and swapped +// in at the boundary, no bytes moved; the old range is then the staging slot's, released later. +TEST(samplepool_a_staged_sample_swaps_in_and_the_old_range_waits_to_be_released) { + SamplePool pool; + pool.init(16 * kMib); + CHECK(pool.reserve(2, 4 * kMib)); + CHECK(pool.finish(2, 1000, 1, 48000)); + const uint32_t old_offset = pool.slot(2).offset; + CHECK(pool.reserve(staging_slot(2), 3 * kMib)); + CHECK(pool.finish(staging_slot(2), 700, 2, 48000)); + const uint32_t new_offset = pool.slot(staging_slot(2)).offset; + CHECK(new_offset != old_offset); + CHECK_EQ(pool.count_in(SampleState::ready), 1); // the screen counts tracks only + pool.swap(2, staging_slot(2)); + CHECK_EQ(pool.slot(2).offset, new_offset); + CHECK_EQ(pool.slot(2).frames, 700u); + CHECK_EQ(int(pool.slot(2).channels), 2); + CHECK_EQ(pool.slot(staging_slot(2)).offset, old_offset); + CHECK_EQ(pool.used(), 7u * kMib); + pool.release(staging_slot(2)); + CHECK_EQ(pool.used(), 3u * kMib); + CHECK(pool.slot(staging_slot(2)).state == SampleState::empty); + pool.swap(2, kPoolSlots); // out of range: nothing + CHECK_EQ(pool.slot(2).offset, new_offset); +} diff --git a/tests/test_storage.cpp b/tests/test_storage.cpp index ac822968..aa05f65f 100644 --- a/tests/test_storage.cpp +++ b/tests/test_storage.cpp @@ -497,6 +497,8 @@ const Expect kTable[] = { // 1,076-byte write. {Cmd::clear_sample, kDirtyKit, "clear_sample"}, {Cmd::set_sample, kDirtyKit, "set_sample"}, + // A section's staged sample reaches the kit only when its kit lands, which marks the kit then. + {Cmd::stage_sample, 0u, "stage_sample"}, // A streamed file is the same swap's second half. Attaching its stream changes nothing on // the card: it is the card side telling the audio side the stream is open. {Cmd::set_stream, kDirtyKit, "set_stream"}, @@ -6471,6 +6473,88 @@ TEST(storage_a_section_kit_lands_on_its_entrys_boundary) { pump(2); } +// Review 2026-11-30, proposal 9: a section kit's file that differs from the playing one loads +// during the section before, into the track's staging slot, and plays from the boundary: no +// placeholder at the section's start, and the old file plays until then. +TEST(storage_a_section_kits_sample_is_loaded_ahead_and_plays_from_the_boundary) { + section_kit_project(2); + FakeHost &host = FakeHost::the(); + make_wav("/SAMPLES/A.WAV", 48000, 1, 16, 3000); + make_wav("/SAMPLES/B.WAV", 48000, 2, 16, 5000); + kit_sample(kLoopTrack, "/SAMPLES/B.WAV"); + CHECK(storage().request_kit_save(Storage::kSongKitFirst + 1)); + CHECK(kit_settle()); + kit_sample(kLoopTrack, "/SAMPLES/A.WAV"); + CHECK(load_sample(kLoopTrack)); + CHECK_EQ(slot_of(kLoopTrack).frames, 3000u); + const uint32_t landings = storage().staged_landings(); + + host.set_running(true); + sk_tick(); + post(Cmd::set_chain, 0, 0, 1); + // Entry 1 plays A while B loads beside it. + for (int i = 0; i < 60 && storage().staged_tracks() == 0; i++) sk_tick(); + CHECK_EQ(int(storage().staged_tracks()), 1 << kLoopTrack); + CHECK_EQ(int(engine().chain().entry), 0); + CHECK_EQ(slot_of(kLoopTrack).frames, 3000u); + CHECK(slot_of(kLoopTrack).state == SampleState::ready); + CHECK(storage().samples().slot(staging_slot(kLoopTrack)).state == SampleState::ready); + const uint32_t used = storage().samples().used(); + for (int i = 0; i < 400 && engine().chain().entry != 1; i++) sk_tick(); + CHECK_EQ(int(engine().chain().entry), 1); + pump(2); + // B is the track's own, never through the placeholder, and A's range is given back. + CHECK_EQ(storage().staged_landings(), landings + 1); + CHECK(slot_of(kLoopTrack).state == SampleState::ready); + CHECK_EQ(slot_of(kLoopTrack).frames, 5000u); + CHECK_EQ(int(slot_of(kLoopTrack).channels), 2); + CHECK(storage().samples().slot(staging_slot(kLoopTrack)).state == SampleState::empty); + CHECK(storage().samples().used() < used); + CHECK(std::strcmp(engine().project().kit.tracks[kLoopTrack].sample, "/SAMPLES/B.WAV") == 0); + CHECK(storage().has_overview(kLoopTrack)); + CHECK(storage().last_error() == nullptr); + // K2 plays: an edit now is the project's and not K2's, which SAVE says (live_kit_edited). + CHECK_EQ(int(storage().live_song_kit()), 2); + CHECK(!storage().live_kit_edited()); + storage().note_command(Cmd::set_param, 0, -1); + CHECK(storage().live_kit_edited()); + // Nothing queued to load again after the boundary. + CHECK(settle()); + CHECK_EQ(slot_of(kLoopTrack).frames, 5000u); + CHECK_EQ(storage().staged_landings(), landings + 1); + host.set_running(false); + post(Cmd::set_chain, 0, 0, 0); + pump(2); +} + +// A kit dropped before its boundary (here the chain turned off) takes its staged file with it: +// the range is given back and the track keeps playing its own. +TEST(storage_a_staged_sample_is_given_back_when_its_kit_is_dropped) { + section_kit_project(2); + FakeHost &host = FakeHost::the(); + make_wav("/SAMPLES/A.WAV", 48000, 1, 16, 3000); + make_wav("/SAMPLES/B.WAV", 48000, 1, 16, 5000); + kit_sample(kLoopTrack, "/SAMPLES/B.WAV"); + CHECK(storage().request_kit_save(Storage::kSongKitFirst + 1)); + CHECK(kit_settle()); + kit_sample(kLoopTrack, "/SAMPLES/A.WAV"); + CHECK(load_sample(kLoopTrack)); + const uint32_t used = storage().samples().used(); + host.set_running(true); + sk_tick(); + post(Cmd::set_chain, 0, 0, 1); + for (int i = 0; i < 60 && storage().staged_tracks() == 0; i++) sk_tick(); + CHECK_EQ(int(storage().staged_tracks()), 1 << kLoopTrack); + post(Cmd::set_chain, 0, 0, 0); + for (int i = 0; i < 20; i++) sk_tick(); + CHECK_EQ(int(storage().staged_tracks()), 0); + CHECK(storage().samples().slot(staging_slot(kLoopTrack)).state == SampleState::empty); + CHECK_EQ(storage().samples().used(), used); + CHECK_EQ(slot_of(kLoopTrack).frames, 3000u); + host.set_running(false); + pump(2); +} + TEST(storage_a_section_kit_the_first_entry_wants_lands_late_once) { section_kit_project(0, 3); FakeHost &host = FakeHost::the();