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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ the project file format or the controls.
longer, 1,615 bytes; older builds read it). A scene or a lock made on a PERC track that was
once a synth, and a user sound saved from one, can carry old bytes into the page: set NSE to 0
and save again.
- **STEMS.** A FUNC menu row under CATCH. OK records every track and the send returns to the
card at once, for mixing on a computer: `/RECORD/STEMS_<n>.WAV` holds the eight tracks as 16
channels of 16 bits (after each track's inserts, fader and pan; before the drum bus and the
master), `/RECORD/STEMS_<n>_FX.WAV` the delay, reverb and chorus returns in stereo 16-bit.
From a stopped transport the row starts the music too, and STOP ends the pass (`SAVED STEMS
3`). `tools/split_stems.py` turns the 16-channel file into eight stereo WAVs. A JAM REC can
run at the same time.
- **CATCH.** A FUNC menu row under ARR REC. With REC off, the pads, KEYS and MIDI IN are still
stamped and kept; CATCH writes the last pass and the one before into the pattern, exactly as
REC would have (QUANTIZE, micro-timing, LEN, chords, REC MODE), every track played at once, as
one undo level (`CATCH T7 12 NOTES`). At most 96 notes a press. Nothing is saved: the notes
are kept from PLAY until they are caught.
- **OPN: open hats from the step's length.** HAT's CYM page has a fourth knob, OPN. Above 0, a
step longer than one step plays open with OPN as its decay, and the next hit on the track cuts
it, so an open hat needs no locks. A roll's hits and ENERGY's ghosts stay closed. GENERATE's
Expand Down
5 changes: 5 additions & 0 deletions app/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "paint.hpp" // the drawing kit: lines, arcs, the small font, afterglow
#include "spectrum.hpp" // the output's spectrum for MIXER's master pages
#include "record.hpp" // live recording's stamping and quantizing, used from here only
#include "catchring.hpp" // CATCH: notes played with REC off, kept for one press
#include "sounds.hpp" // SOUNDS: the factory sounds for one track
#include "usersounds.hpp" // and your own, saved on the card
#include "arppresets.hpp" // ARPS: the factory arps for one track
Expand Down Expand Up @@ -956,6 +957,8 @@ class Strata : public enaga::App {
uint16_t project_steps_ = 0;
uint32_t jam_lost_seen_ = 0; // JAM REC's dropped frames last said
uint32_t jam_lost_ms_ = 0;
uint32_t stems_lost_seen_ = 0;
bool stems_saw_run_ = false; // STEMS: the transport has run since the stems started
char notice_buffer_[24] = {}; // notice() keeps the pointer, so dynamic text
// needs somewhere of its own to live

Expand Down Expand Up @@ -1356,6 +1359,8 @@ class Strata : public enaga::App {
};
static constexpr int kRecHolds = 16;
RecHold rec_holds_[kRecHolds] = {};
// CATCH's notes, played with REC off (core/catchring.hpp).
CatchRing catch_;
int rec_hold_next_ = 0;
// On a chord track, the pass each step was last recorded in, plus 1 (0: not this take).
// A note landing on a step already recorded in its own pass is another note of the chord
Expand Down
6 changes: 6 additions & 0 deletions app/commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,10 @@ enum class Cmd : uint8_t {
// project file.
jam_start,
jam_stop,
// STEMS, the same protocol for two files (app/storage.hpp, "stems"): every track's stem and
// the returns, from Mixer's stem planes; Status::stems_epoch moves once writing has stopped.
stems_start,
stems_stop,

// One field of one zone's sound (core/zones.hpp, ZoneField), from ZONES in the track menu:
// step: the zone, a: the field, u: the value as the record holds it. The record in the kit is
Expand Down Expand Up @@ -807,6 +811,8 @@ struct Status {
// JAM REC. Bumped (with release) by every Cmd::jam_stop once the audio side has stopped
// writing the jam's stream, so the card side may close it the moment it sees the change.
std::atomic<uint8_t> jam_epoch{0};
// STEMS, the same as jam_epoch for Cmd::stems_stop.
std::atomic<uint8_t> stems_epoch{0};

// Where a SAMPLE or GRAIN track is in its sample, for EDIT's waveform (core/overview.hpp,
// WaveView): the window or the grain bracket, the slice starts, and up to eight playheads or
Expand Down
26 changes: 26 additions & 0 deletions app/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2877,6 +2877,19 @@ bool Engine::apply_samples(const Command &command, int track, int) {
jam_on_ = true;
break;

case Cmd::stems_start:
stems_on_ = true;
mixer_.set_stems(true);
break;

case Cmd::stems_stop:
// As jam_stop: stop writing, then say so.
stems_on_ = false;
mixer_.set_stems(false);
stems_epoch_ = uint8_t(stems_epoch_ + 1u);
status_.stems_epoch.store(stems_epoch_, std::memory_order_release);
break;

case Cmd::jam_stop:
// Stop writing FIRST, then say so: the card side closes the file the moment it sees
// the epoch move. Bumped even when nothing was being written, so a stop the card side
Expand Down Expand Up @@ -4287,6 +4300,8 @@ namespace {
constexpr float kInverseCount[kArpMaxRatchet + 1] = {1.0f, 1.0f, 0.5f, 1.0f / 3.0f, 0.25f};
// A block's length as a reciprocal: how many blocks a spacing holds.
constexpr float kInverseBlock = 1.0f / float(ENAGA_BLOCK_FRAMES);
static_assert(StemRing::kFrames == ENAGA_BLOCK_FRAMES, "a stem slot holds one block");
static_assert(StemRing::kPlanes == 2 * Mixer::kStems, "the stems ring carries every stem");

} // namespace

Expand Down Expand Up @@ -5346,6 +5361,17 @@ void Engine::process(const float *in, float *out, uint32_t frames) {
// ones that fit are kept.
if (jam_on_) jam_.write(out, frames);

// STEMS: the mixer's stem planes into the ring (core/stemring.hpp); the main context packs
// them into the two files. A full ring drops the block and counts it.
if (stems_on_) {
const float *planes[StemRing::kPlanes];
for (int s = 0; s <= kTracks; s++) {
planes[2 * s] = mixer_.stem(s, 0);
planes[2 * s + 1] = mixer_.stem(s, 1);
}
stems_ring_.push(planes, int(frames));
}

// The input's level, every block, for the RESAMPLE screen's meter: one pass of the master's
// own peak loop over the interleaved buffer (L and R alike, so one plane of 2n samples).
if (in != nullptr) {
Expand Down
17 changes: 17 additions & 0 deletions app/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "buses.hpp"
#include "capture.hpp" // resampling's take buffer
#include "frozen.hpp" // freeze: a track playing a loop of itself
#include "stemring.hpp" // STEMS: the stem planes to the main context
#include "grain.hpp" // GRAIN, reachable at last: a track can host any machine
#include "commands.hpp"
#include "cpuguard.hpp" // sheds POLY voices and grains when the block nears its budget
Expand Down Expand Up @@ -420,6 +421,16 @@ class Engine {
// For tests: is the audio side writing it?
bool jam_attached() const { return jam_on_; }

// STEMS' two files (app/storage.hpp, "stems"): the eight tracks as one 16-channel file, T1's
// left and right first, and the send returns as a stereo one. Written in audio() from the
// mixer's stem planes while Cmd::stems_start is in force, as the jam's is.
enaga::wav::Writer &stems_writer() { return stems_; }
enaga::wav::Writer &stems_returns_writer() { return stems_returns_; }
const enaga::wav::Writer &stems_writer() const { return stems_; }
const enaga::wav::Writer &stems_returns_writer() const { return stems_returns_; }
bool stems_attached() const { return stems_on_; }
StemRing &stems_ring() { return stems_ring_; }

private:
void apply_commands();
void apply(const Command &command);
Expand Down Expand Up @@ -670,6 +681,12 @@ class Engine {
enaga::wav::Writer jam_;
bool jam_on_ = false; // writing every block into jam_
uint8_t jam_epoch_ = 0; // what Status::jam_epoch holds
enaga::wav::Writer stems_;
enaga::wav::Writer stems_returns_;
bool stems_on_ = false;
uint8_t stems_epoch_ = 0;
StemRing stems_ring_; // audio() to the main context, which writes the files


// The hat choke is a pair, and the pair is tracks 3 and 4 of the DEFAULT kit. Machine
// switching makes that an assumption rather than a fact, so it is re-derived on every
Expand Down
186 changes: 186 additions & 0 deletions app/storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "factory.hpp"
#include "zones.hpp"
#include "masterloops.h" // strata_stem_pack16

#include "engine.hpp"

Expand All @@ -25,6 +26,7 @@ constexpr const char *kKeyNewSeq = "NEWSEQ";
constexpr const char *kKeyTakeSeq = "TAKESEQ";
// JAM REC's files the same way: /RECORD/JAM_<n>.WAV.
constexpr const char *kKeyJamSeq = "JAMSEQ";
constexpr const char *kKeyStemsSeq = "STEMSEQ";

constexpr const char *kDefaultName = "DEFAULT";

Expand Down Expand Up @@ -291,6 +293,11 @@ bool Storage::init(Engine &engine) {
if (audition_head_ == nullptr || audition_ring_ == nullptr) return false;
jam_ring_ = new uint8_t[kJamRingBytes];
if (jam_ring_ == nullptr) return false;
stems_ring_ = new uint8_t[kStemsRingBytes];
stems_returns_ring_ = new uint8_t[kStemsReturnsRingBytes];
stems_slots_ = new float[kStemsBlocks * uint32_t(StemRing::kSlotFloats)];
if (stems_ring_ == nullptr || stems_returns_ring_ == nullptr || stems_slots_ == nullptr) return false;
engine_->stems_ring().attach(stems_slots_, kStemsBlocks);

pool_.init(kSampleBudgetDefaultMib << 20);
engine_->set_sample_region(region_, kRegionBytes);
Expand Down Expand Up @@ -1903,6 +1910,7 @@ void Storage::service() {
service_audition();
// And JAM REC: a full card or a stop must not wait behind a job either.
service_jam();
service_stems();

// Freeze next, whatever job runs and with or without a card: it is RAM only, and a loop
// whose slot is waiting to be given back should not wait behind a save.
Expand Down Expand Up @@ -4446,6 +4454,183 @@ void Storage::close_jam_for_shutdown() {
close_jam();
}

// --- stems -----------------------------------------------------------------------
//
// app/storage.hpp, "stems", has the layout; the protocol is the jam's.

bool Storage::request_stems_start() {
if (stems_state_ == JamState::recording) return true;
if (stems_state_ == JamState::closing) {
stems_message_ = "STEMS BUSY";
return false;
}
if (!card_usable_) {
stems_message_ = "NO CARD";
return false;
}
const int32_t next = settings_.get(kKeyStemsSeq, 0) + 1;
{
char *out = stems_name_;
const char *end = stems_name_ + sizeof stems_name_ - 1;
append_text(out, "STEMS_", end);
append_number(out, uint32_t(next), end);
append_text(out, ".WAV", end);
*out = '\0';
}
{
char *out = stems_path_;
const char *end = stems_path_ + sizeof stems_path_ - 1;
append_text(out, "/RECORD/", end);
append_text(out, stems_name_, end);
*out = '\0';
}
{
char *out = stems_returns_path_;
const char *end = stems_returns_path_ + sizeof stems_returns_path_ - 1;
append_text(out, "/RECORD/STEMS_", end);
append_number(out, uint32_t(next), end);
append_text(out, "_FX.WAV", end);
*out = '\0';
}
enaga::wav::Format tracks{};
tracks.encoding = enaga::wav::pcm;
tracks.channels = uint16_t(2 * kTracks);
tracks.rate = uint32_t(ENAGA_SAMPLE_RATE);
tracks.bits = 16;
enaga::wav::Format returns = tracks;
returns.channels = 2;
returns.bits = 16;
enaga::wav::Writer &a = engine_->stems_writer();
enaga::wav::Writer &b = engine_->stems_returns_writer();
int32_t created = a.create(stems_path_, tracks, stems_ring_, kStemsRingBytes);
if (created >= 0) created = b.create(stems_returns_path_, returns, stems_returns_ring_, kStemsReturnsRingBytes);
if (created < 0) {
a.close();
b.close();
// A half-made pair is no use: neither is kept.
enaga::files::remove(stems_path_);
enaga::files::remove(stems_returns_path_);
char *out = stems_text_;
const char *end = stems_text_ + sizeof stems_text_ - 1;
append_text(out, "STEMS: ", end);
append_text(out, created == enaga::wav::not_wav ? "BAD FORMAT" : enaga::files::error_name(created), end);
*out = '\0';
stems_message_ = stems_text_;
stems_name_[0] = '\0';
return false;
}
settings_.set(kKeyStemsSeq, next);
settings_.save();
stems_epoch_ = engine_->status().stems_epoch.load(std::memory_order_acquire);
stems_frames_ = 0;
stems_stop_reason_ = nullptr;
engine_->stems_ring().clear(); // the producer is idle until stems_start lands
post(Cmd::stems_start);
stems_state_ = JamState::recording;
return true;
}

void Storage::request_stems_stop() {
if (stems_state_ == JamState::recording) stop_stems(nullptr);
}

uint32_t Storage::stems_lost_frames() const {
const uint32_t a = engine_->stems_writer().frames_lost();
const uint32_t b = engine_->stems_returns_writer().frames_lost();
return (a > b ? a : b) + engine_->stems_ring().lost();
}

// What audio() has queued, packed into 16-bit frames and handed to the two Writers. A Writer
// whose ring is full drops what does not fit and counts it (frames_lost).
void Storage::drain_stems() {
StemRing &ring = engine_->stems_ring();
enaga::wav::Writer &a = engine_->stems_writer();
enaga::wav::Writer &b = engine_->stems_returns_writer();
int frames = 0;
for (const float *slot = ring.front(frames); slot != nullptr; slot = ring.front(frames)) {
constexpr int kF = StemRing::kFrames;
for (int t = 0; t < kTracks; t++)
strata_stem_pack16(stems_pcm_ + 2 * t, slot + (2 * t) * kF, slot + (2 * t + 1) * kF,
2 * kTracks, frames);
a.write_bytes(stems_pcm_, uint32_t(frames) * 2u * 2u * kTracks);
strata_stem_pack16(stems_pcm_, slot + (2 * kTracks) * kF, slot + (2 * kTracks + 1) * kF, 2,
frames);
b.write_bytes(stems_pcm_, uint32_t(frames) * 2u * 2u);
ring.pop();
}
}

void Storage::stop_stems(const char *why) {
stems_stop_reason_ = why;
post(Cmd::stems_stop);
stems_state_ = JamState::closing;
}

void Storage::close_stems() {
enaga::wav::Writer &a = engine_->stems_writer();
enaga::wav::Writer &b = engine_->stems_returns_writer();
stems_frames_ = a.frames();
const uint32_t lost = stems_lost_frames();
const int32_t closed_a = a.close();
const int32_t closed_b = b.close();
const int32_t closed = closed_a < 0 ? closed_a : closed_b;
stems_state_ = JamState::idle;
char *out = stems_text_;
const char *end = stems_text_ + sizeof stems_text_ - 1;
if (stems_stop_reason_ != nullptr) {
append_text(out, stems_stop_reason_, end);
} else if (closed < 0) {
append_text(out, "STEMS: ", end);
append_text(out, enaga::files::error_name(closed), end);
} else if (lost > 0) {
// The card fell behind: the files are there, with gaps. Said rather than SAVED.
append_text(out, "STEMS: CARD TOO SLOW", end);
} else {
append_text(out, "SAVED ", end);
for (const char *c = stems_name_; *c != '\0' && *c != '.' && out < end; c++) {
*out++ = *c == '_' ? ' ' : *c;
}
}
*out = '\0';
stems_message_ = stems_text_;
}

void Storage::service_stems() {
switch (stems_state_) {
case JamState::idle:
return;
case JamState::closing: {
// Once audio() has stopped (the epoch moved), what it queued goes in, then the close.
const uint8_t now = engine_->status().stems_epoch.load(std::memory_order_acquire);
drain_stems();
if (now != stems_epoch_) {
drain_stems();
close_stems();
}
return;
}
case JamState::recording: {
drain_stems();
enaga::wav::Writer &a = engine_->stems_writer();
stems_frames_ = a.frames();
const enaga::files::Stream::Status sa = a.status();
const enaga::files::Stream::Status sb = engine_->stems_returns_writer().status();
const int32_t state = sa.state < 0 ? sa.state : sb.state;
if (state < 0) {
stop_stems(state == ENAGA_FILE_FULL ? "CARD FULL: STEMS SAVED" : "CARD ERROR: STEMS CUT");
} else if (sa.position >= stems_limit_) {
stop_stems("STEMS: FILE FULL");
}
return;
}
}
}

void Storage::close_stems_for_shutdown() {
if (stems_state_ == JamState::idle) return;
close_stems();
}

// --- streaming ----------------------------------------------------------------
//
// app/storage.hpp, "streaming: a file too big for the pool", has the protocol.
Expand Down Expand Up @@ -4587,6 +4772,7 @@ void Storage::save_for_shutdown() {
// this file that waits.
// The jam first: without its header the file says it holds nothing.
close_jam_for_shutdown();
close_stems_for_shutdown();
if (!card_usable_) return;
// Nor while a project load is half done: RAM is part the project being read and part the one
// before (on_card says more), and both are whole on the card. An autosave of the mix would
Expand Down
Loading