Cycle recording phase 1: transport loop region
Adds a cycle (loop) region: a range on the timeline ruler that the transport wraps at during playback. This is the substrate for GarageBand-style multi-take cycle recording (phases 2 and 3). The region is authored in BEATS so it stays put musically across tempo changes. It lives on the Document (saved in the .beam, serde-defaulted so old files load) and is edited through SetCycleRegionAction, so it is undoable and marks the document modified like any other edit. Backend: - Engine gains loop_region/loop_enabled plus a wrap at the single playhead-advance point in process(). The wrap is phase-preserving (modulo, so an overshoot larger than the loop can't strand the playhead outside the region) and gated on playhead >= 0 so a count-in pre-roll never wraps. - Sounding voices are deliberately NOT reset at the wrap the way Command::Seek does, since that would chop sustain and reverb tails at every pass. - MidiRecordingState::wrap_at_cycle writes note-offs for held notes at the region end and re-opens them at the region start, so a key held across the boundary can't end up with a negative duration or hang. - loop_bounds_frozen freezes the region's sample bounds for the duration of an *audio* recording. Phrased positively on audio so future multi-track recording inherits it: MIDI is beat-segmented and tempo-invariant, but audio is segmented geometrically and we have no time-stretch, so cross-tempo audio takes wouldn't be compable anyway. - Command::Play jumps to loop_start when starting from outside the region; starting inside it plays from where you are. Editor: - Cycle lane along the bottom of the ruler (bottom, so it doesn't cover the bar numbers), painted inside render_ruler under the ticks. Only exists while looping is armed; with cycle off the ruler is entirely the playhead scrubber, as before. - Drag to create/move/resize with a three-zone hit test, previewed locally and committed as ONE action on release. Driven off raw pointer state rather than an egui Response: the lane sits inside the timeline's content response, and a second widget on the same pixels just contests hover every frame. - snap_to_grid/quantize_grid_size take a min_grid_px "visual coarseness" parameter instead of a hardcoded constant, with two named profiles: SNAP_PX_FINE for the playhead and clip edges, SNAP_PX_CYCLE (coarser) for the cycle region, so loops land on bars rather than odd subdivisions. - Cycle toggle button using the Lucide repeat glyph. Cargo.lock picks up the 1.0.9-alpha version bump it missed. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
9f67a82e3e
commit
f957b01dcf
@@ -33,6 +33,15 @@ pub struct Engine {
|
||||
playing: bool,
|
||||
channels: u32,
|
||||
|
||||
/// Transport cycle (loop) region, authored in BEATS so it survives tempo changes.
|
||||
/// Sample bounds are derived from the tempo map at the wrap check.
|
||||
loop_region: Option<(Beats, Beats)>,
|
||||
/// Whether the transport wraps at the end of `loop_region`.
|
||||
loop_enabled: bool,
|
||||
/// Cycle-region sample bounds frozen for the duration of an **audio** recording.
|
||||
/// See `loop_bounds_samples` for why. `None` = derive live from the tempo map.
|
||||
loop_bounds_frozen: Option<(i64, i64)>,
|
||||
|
||||
// Lock-free communication
|
||||
command_rx: rtrb::Consumer<Command>,
|
||||
midi_command_rx: Option<rtrb::Consumer<Command>>,
|
||||
@@ -155,6 +164,9 @@ impl Engine {
|
||||
sample_rate,
|
||||
playing: false,
|
||||
channels,
|
||||
loop_region: None,
|
||||
loop_enabled: false,
|
||||
loop_bounds_frozen: None,
|
||||
command_rx,
|
||||
midi_command_rx: None,
|
||||
event_tx,
|
||||
@@ -508,6 +520,38 @@ impl Engine {
|
||||
// Update playhead (convert total samples to frames)
|
||||
self.playhead += (output.len() / self.channels as usize) as i64;
|
||||
|
||||
// Cycle/loop wrap. Gated on playhead >= 0 so a count-in pre-roll never wraps.
|
||||
// Sounding voices are deliberately left alone — no `stop_all_notes()` /
|
||||
// `reset_all_graphs()` like Command::Seek does, since that would chop sustain and
|
||||
// reverb tails at every wrap.
|
||||
if self.loop_enabled && self.playhead >= 0 {
|
||||
if let Some((ls_beats, le_beats)) = self.loop_region {
|
||||
// Sample bounds are frozen while audio is recording (see loop_bounds_samples).
|
||||
let (ls, le) = self.loop_bounds_samples(ls_beats, le_beats);
|
||||
if le > ls && self.playhead >= le {
|
||||
// Phase-preserving wrap. Modulo (not a single subtraction) so an overshoot
|
||||
// larger than the loop — e.g. after a tempo change shrank it — can't strand
|
||||
// the playhead outside the region.
|
||||
self.playhead = ls + (self.playhead - ls) % (le - ls);
|
||||
|
||||
// A MIDI recording in progress stores note times as offsets from its
|
||||
// start, and the playhead just jumped backwards — so any note still held
|
||||
// across the boundary needs its note-off written at the region end (and
|
||||
// re-opened at the region start if the key is still down). Otherwise it
|
||||
// would get a negative duration, or never close at all.
|
||||
if let Some(ref mut rec) = self.midi_recording_state {
|
||||
rec.wrap_at_cycle(le_beats, ls_beats);
|
||||
}
|
||||
|
||||
if let Some(ref mut dr) = self.disk_reader {
|
||||
dr.send(crate::audio::disk_reader::DiskReaderCommand::Seek {
|
||||
frame: self.playhead.max(0) as u64,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update atomic playhead for UI reads (clamped to 0; negative = count-in pre-roll)
|
||||
self.playhead_atomic
|
||||
.store(self.playhead.max(0) as u64, Ordering::Relaxed);
|
||||
@@ -679,6 +723,8 @@ impl Engine {
|
||||
format!("Recording write error: {}", e)
|
||||
));
|
||||
self.recording_state = None;
|
||||
// Audio recording is over — let the cycle region track tempo again.
|
||||
self.loop_bounds_frozen = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -783,12 +829,71 @@ impl Engine {
|
||||
None
|
||||
}
|
||||
|
||||
/// Convert a beats position to a sample position using the current tempo map.
|
||||
///
|
||||
/// The cycle region is authored in beats (so it survives tempo changes); its sample bounds are
|
||||
/// derived here at the wrap check rather than cached, which keeps it correct across tempo edits
|
||||
/// with no invalidation bookkeeping.
|
||||
fn beats_to_samples(&self, beats: Beats) -> i64 {
|
||||
(self.tempo_map.beats_to_seconds(beats).seconds_to_f64() * self.sample_rate as f64) as i64
|
||||
}
|
||||
|
||||
/// Sample bounds of the cycle region.
|
||||
///
|
||||
/// These are FROZEN while an audio recording is in flight (see `loop_bounds_frozen`, captured
|
||||
/// in `handle_start_recording`), so a tempo change mid-take cannot resize the loop.
|
||||
///
|
||||
/// Why freeze only for audio: take segmentation for audio is geometric in samples — every pass
|
||||
/// is exactly `loop_len` frames, which is what lets us pad all takes to a uniform length and
|
||||
/// comp between them. A tempo change would resize `loop_len` mid-session and break that. And
|
||||
/// since we have no time-stretching, audio takes captured at two different tempos could never
|
||||
/// be comped together anyway, so honoring the change would buy nothing and cost the invariant.
|
||||
///
|
||||
/// MIDI is deliberately unaffected: it is segmented in *beats*, which are tempo-invariant, so
|
||||
/// changing tempo during a MIDI-only recording is fully supported (slow down a hard passage and
|
||||
/// keep stacking takes). The freeze is keyed on *audio* being recorded, not on "not MIDI", so
|
||||
/// when multi-track recording lands it will correctly freeze if ANY recorded track is audio.
|
||||
fn loop_bounds_samples(&self, ls_beats: Beats, le_beats: Beats) -> (i64, i64) {
|
||||
if let Some(frozen) = self.loop_bounds_frozen {
|
||||
return frozen;
|
||||
}
|
||||
(self.beats_to_samples(ls_beats), self.beats_to_samples(le_beats))
|
||||
}
|
||||
|
||||
/// Handle a command from the UI thread
|
||||
fn handle_command(&mut self, cmd: Command) {
|
||||
match cmd {
|
||||
Command::Play => {
|
||||
// Starting playback from outside the cycle region jumps to its start — otherwise
|
||||
// you'd play forward from wherever the playhead happened to be and only fall into
|
||||
// the loop if you happened to cross its end. Inside the region we start where we
|
||||
// are, so you can still audition from the middle of a loop.
|
||||
//
|
||||
// A negative playhead is a count-in pre-roll that was deliberately placed *before*
|
||||
// the region, so leave it alone.
|
||||
if self.loop_enabled && self.playhead >= 0 {
|
||||
if let Some((ls_beats, le_beats)) = self.loop_region {
|
||||
let (ls, le) = self.loop_bounds_samples(ls_beats, le_beats);
|
||||
if le > ls && (self.playhead < ls || self.playhead >= le) {
|
||||
self.playhead = ls;
|
||||
self.playhead_atomic.store(ls.max(0) as u64, Ordering::Relaxed);
|
||||
if let Some(ref mut dr) = self.disk_reader {
|
||||
dr.send(crate::audio::disk_reader::DiskReaderCommand::Seek {
|
||||
frame: ls.max(0) as u64,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
self.playing = true;
|
||||
}
|
||||
Command::SetLoopRegion(region) => {
|
||||
// Stored in beats; sample bounds are derived at the wrap check.
|
||||
self.loop_region = region;
|
||||
}
|
||||
Command::SetLoopEnabled(enabled) => {
|
||||
self.loop_enabled = enabled;
|
||||
}
|
||||
Command::Stop => {
|
||||
self.playing = false;
|
||||
self.playhead = 0;
|
||||
@@ -1309,6 +1414,12 @@ impl Engine {
|
||||
// Stop any active recording
|
||||
self.recording_state = None;
|
||||
|
||||
// Clear the cycle region — it's a document property, and the new document will
|
||||
// push its own (or none) once loaded.
|
||||
self.loop_region = None;
|
||||
self.loop_enabled = false;
|
||||
self.loop_bounds_frozen = None;
|
||||
|
||||
// Clear all project data
|
||||
self.project = Project::new(self.sample_rate);
|
||||
|
||||
@@ -3034,6 +3145,19 @@ impl Engine {
|
||||
use crate::io::WavWriter;
|
||||
use std::env;
|
||||
|
||||
// Freeze the cycle region's sample bounds for the duration of this AUDIO recording, so a
|
||||
// tempo change mid-take can't resize the loop and break the uniform-take invariant that
|
||||
// take segmentation and comping depend on. See `loop_bounds_samples`. (MIDI-only
|
||||
// recordings never take this path, so they stay free to change tempo.)
|
||||
if self.loop_enabled {
|
||||
if let Some((ls_beats, le_beats)) = self.loop_region {
|
||||
self.loop_bounds_frozen = Some((
|
||||
self.beats_to_samples(ls_beats),
|
||||
self.beats_to_samples(le_beats),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// Check if track exists and is an audio track
|
||||
if let Some(crate::audio::track::TrackNode::Audio(_)) = self.project.get_track_mut(track_id) {
|
||||
// Generate a unique temp file path
|
||||
@@ -3118,6 +3242,9 @@ impl Engine {
|
||||
fn handle_stop_recording(&mut self) {
|
||||
eprintln!("[STOP_RECORDING] handle_stop_recording called");
|
||||
|
||||
// Audio is no longer recording, so the cycle region can track the tempo map again.
|
||||
self.loop_bounds_frozen = None;
|
||||
|
||||
// Check if we have an active MIDI recording first
|
||||
if self.midi_recording_state.is_some() {
|
||||
eprintln!("[STOP_RECORDING] Detected active MIDI recording, delegating to handle_stop_midi_recording");
|
||||
@@ -3342,6 +3469,20 @@ impl EngineController {
|
||||
let _ = self.command_tx.push(Command::Pause);
|
||||
}
|
||||
|
||||
/// Set the cycle region the transport loops over (None clears it).
|
||||
///
|
||||
/// Authored in beats so it survives tempo changes. Note the region's sample bounds are frozen
|
||||
/// while an audio recording is in flight, so a call made mid-take won't resize the loop until
|
||||
/// recording stops (see `Engine::loop_bounds_samples`).
|
||||
pub fn set_loop_region(&mut self, region: Option<(Beats, Beats)>) {
|
||||
let _ = self.command_tx.push(Command::SetLoopRegion(region));
|
||||
}
|
||||
|
||||
/// Enable/disable wrapping at the end of the cycle region.
|
||||
pub fn set_loop_enabled(&mut self, enabled: bool) {
|
||||
let _ = self.command_tx.push(Command::SetLoopEnabled(enabled));
|
||||
}
|
||||
|
||||
/// Stop playback and reset to beginning
|
||||
pub fn stop(&mut self) {
|
||||
let _ = self.command_tx.push(Command::Stop);
|
||||
|
||||
@@ -261,4 +261,27 @@ impl MidiRecordingState {
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle a transport cycle wrap during MIDI recording.
|
||||
///
|
||||
/// Note times are stored as offsets from `start_time`, and the playhead jumps *backwards* at a
|
||||
/// wrap — so a note still held across the boundary would otherwise get a nonsensical (negative)
|
||||
/// duration, or never be closed at all. Write its note-off at `region_end` (exactly as
|
||||
/// `close_active_notes` does when recording stops), then re-open it at `region_start` so a key
|
||||
/// the player is still physically holding keeps being captured in the next pass. Mirrors the
|
||||
/// way `handle_start_midi_recording` re-injects already-held notes at the recording start.
|
||||
pub fn wrap_at_cycle(&mut self, region_end: Beats, region_start: Beats) {
|
||||
// Snapshot the held notes (close_active_notes drains them and loses the velocities).
|
||||
let held: Vec<(u8, u8)> = self
|
||||
.active_notes
|
||||
.values()
|
||||
.map(|n| (n.note, n.velocity))
|
||||
.collect();
|
||||
|
||||
self.close_active_notes(region_end);
|
||||
|
||||
for (note, velocity) in held {
|
||||
self.note_on(note, velocity, region_start);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,6 +113,13 @@ pub enum Command {
|
||||
/// Enable/disable an automation lane (track_id, lane_id, enabled)
|
||||
SetAutomationLaneEnabled(TrackId, AutomationLaneId, bool),
|
||||
|
||||
// Transport cycle (loop) region
|
||||
/// Set the cycle region the transport loops over, in beats (None clears it).
|
||||
/// Authored in beats so it survives tempo changes.
|
||||
SetLoopRegion(Option<(Beats, Beats)>),
|
||||
/// Enable/disable wrapping at the cycle region's end.
|
||||
SetLoopEnabled(bool),
|
||||
|
||||
// Recording commands
|
||||
/// Start recording on a track (track_id, start_time)
|
||||
StartRecording(TrackId, Beats),
|
||||
|
||||
Reference in New Issue
Block a user