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:
Skyler Lehmkuhl
2026-07-13 02:13:06 -04:00
co-authored by Claude Opus 4.8
parent 9f67a82e3e
commit f957b01dcf
10 changed files with 643 additions and 24 deletions
@@ -14,6 +14,7 @@ pub mod move_clip_instances;
pub mod reorder_clip_instances;
pub mod paint_bucket;
pub mod remove_effect;
pub mod set_cycle_region;
pub mod set_document_properties;
pub mod set_instance_properties;
pub mod set_layer_properties;
@@ -50,6 +51,7 @@ pub mod set_text_content;
pub mod resize_text_box;
pub use add_clip_instance::AddClipInstanceAction;
pub use set_cycle_region::SetCycleRegionAction;
pub use add_effect::AddEffectAction;
pub use add_layer::AddLayerAction;
pub use add_shape::AddShapeAction;
@@ -0,0 +1,93 @@
//! Set the transport cycle (loop) region.
//!
//! The cycle region is document state (it's saved in the `.beam`), so changing it goes through the
//! action system like any other edit: it's undoable and it marks the document modified.
//!
//! The region is stored in **beats** so it stays put musically across tempo changes. Callers commit
//! one action per gesture (e.g. on drag release, or a toggle click) rather than one per frame —
//! the timeline previews the drag from its own local state, exactly like a clip drag does.
use crate::action::{Action, BackendContext};
use crate::document::Document;
use daw_backend::Beats;
/// Action that sets the cycle region and/or whether the transport loops over it.
#[derive(Clone)]
pub struct SetCycleRegionAction {
old_region: Option<(Beats, Beats)>,
old_enabled: bool,
new_region: Option<(Beats, Beats)>,
new_enabled: bool,
}
impl SetCycleRegionAction {
/// Build from the document's current state and the desired new region/enabled flag.
pub fn new(
document: &Document,
new_region: Option<(Beats, Beats)>,
new_enabled: bool,
) -> Self {
Self {
old_region: document.cycle_region,
old_enabled: document.cycle_enabled,
new_region,
new_enabled,
}
}
/// Toggle looping on/off, leaving the region itself alone.
pub fn toggle_enabled(document: &Document) -> Self {
Self::new(document, document.cycle_region, !document.cycle_enabled)
}
/// True if this action would not actually change anything (lets callers skip a no-op undo entry).
pub fn is_noop(&self) -> bool {
self.old_region == self.new_region && self.old_enabled == self.new_enabled
}
}
impl Action for SetCycleRegionAction {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
document.cycle_region = self.new_region;
document.cycle_enabled = self.new_enabled;
Ok(())
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
document.cycle_region = self.old_region;
document.cycle_enabled = self.old_enabled;
Ok(())
}
fn description(&self) -> String {
"Set cycle region".to_string()
}
fn execute_backend(
&mut self,
backend: &mut BackendContext,
_document: &Document,
) -> Result<(), String> {
let controller = match backend.audio_controller.as_mut() {
Some(c) => c,
None => return Ok(()),
};
controller.set_loop_region(self.new_region);
controller.set_loop_enabled(self.new_enabled);
Ok(())
}
fn rollback_backend(
&mut self,
backend: &mut BackendContext,
_document: &Document,
) -> Result<(), String> {
let controller = match backend.audio_controller.as_mut() {
Some(c) => c,
None => return Ok(()),
};
controller.set_loop_region(self.old_region);
controller.set_loop_enabled(self.old_enabled);
Ok(())
}
}
@@ -201,6 +201,17 @@ pub struct Document {
#[serde(default)]
pub time_signature: TimeSignature,
/// Transport cycle (loop) region, as `(start, end)` in **beats**.
///
/// Authored in beats so it stays put musically when the tempo changes. `None` = no region set.
/// Saved with the project; `#[serde(default)]` keeps older `.beam` files loading.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cycle_region: Option<(Beats, Beats)>,
/// Whether the transport loops over `cycle_region`.
#[serde(default)]
pub cycle_enabled: bool,
/// Master track (master bus + tempo automation lane).
/// Stored separately from the root layer tree; shown in timeline when
/// `show_master_track` is enabled in the editor state.
@@ -297,6 +308,8 @@ impl Default for Document {
height: 1080.0,
framerate: 60.0,
time_signature: TimeSignature::default(),
cycle_region: None,
cycle_enabled: false,
master_layer: {
let mut ml = GroupLayer::new_master(120.0);
ml.layer.id = uuid::Uuid::new_v4();