Cycle recording: MIDI separate-takes mode, and append to existing folders
Completes the cycle-recording spec. Three related pieces: MIDI separate takes (Preferences > Audio > "Cycle MIDI recording"): - Each pass becomes its own MIDI clip, folded into a take folder — the same shape audio always gets — instead of merging into one clip. Merge stays the default. - Notes are bucketed by the pass they were played in. The pass counter bumps BETWEEN close_active_notes and the re-note_on at a wrap, so a key held across the boundary has its sounding half filed under the pass that's ending and its re-opened half under the pass that's beginning. Put the bump on either side of that pair and the whole note lands in one pass; there's a test named for exactly that. - A silent INTERIOR pass still yields an empty take, so take N is always pass N — otherwise the numbering silently shifts and "take 3" stops meaning "the third time round". A TRAILING empty pass is dropped: that's what hitting stop shortly after a wrap gives you, a stop artifact rather than a take you played. (Audio already behaved this way via its short-final-take rule.) - Still triggers on the wrap: stop inside the first pass and it's an ordinary single recording, whatever the preference says. Append to an existing take folder (AppendTakesAction): - Cycle-recording over a region that already holds a take folder now ADDS to that folder rather than dropping a second clip on top of it, which stranded the new takes in an overlapping clip you couldn't audition against the old. - "Same region" means same start AND same loop length: resize the cycle region and you get a fresh folder, rather than takes of a different length appended to an existing one, which would break the uniform-take invariant that comping-via-split depends on. - The recording's own clip/instance are throwaway scaffolding here (the takes already live in the backend pools), so they're discarded; the single AppendTakesAction is the whole undoable step. Don't play the region you're recording over: - While recording into a MIDI track, every clip on that track is silenced EXCEPT the one being recorded into. A take folder already sitting in the cycle region was otherwise playing its active take underneath you on every pass, fighting the part you were trying to record. - The recording clip itself is exempt, because in merge mode that's precisely what you want to hear: the overdub you've been building up. Other tracks are untouched.
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
//! Append freshly-recorded takes to an existing take folder.
|
||||
//!
|
||||
//! Cycle-recording over a region that already holds a take folder should *add* to that folder, not
|
||||
//! drop a second clip on top of it. Otherwise the takes from your second attempt are stranded in a
|
||||
//! separate, overlapping clip and you can't audition them against the first.
|
||||
//!
|
||||
//! The recorded content already exists in the backend pools by the time this runs (the engine put it
|
||||
//! there at stop), so this action only touches the document — plus the one backend clip the instance
|
||||
//! plays, which has to be repointed at the newly-active take.
|
||||
|
||||
use crate::action::{Action, BackendClipInstanceId, BackendContext};
|
||||
use crate::clip::{AudioClipType, AudioTake};
|
||||
use crate::document::Document;
|
||||
use crate::layer::AnyLayer;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Action that appends takes to a take-folder clip and selects the last of them.
|
||||
pub struct AppendTakesAction {
|
||||
layer_id: Uuid,
|
||||
/// The instance whose folder is being extended (and whose active take changes).
|
||||
instance_id: Uuid,
|
||||
clip_id: Uuid,
|
||||
/// The takes to add, in recording order.
|
||||
new_takes: Vec<AudioTake>,
|
||||
|
||||
// Stored during execute for rollback.
|
||||
old_take_count: usize,
|
||||
old_active_take: Option<usize>,
|
||||
executed: bool,
|
||||
}
|
||||
|
||||
impl AppendTakesAction {
|
||||
pub fn new(layer_id: Uuid, instance_id: Uuid, clip_id: Uuid, new_takes: Vec<AudioTake>) -> Self {
|
||||
Self {
|
||||
layer_id,
|
||||
instance_id,
|
||||
clip_id,
|
||||
new_takes,
|
||||
old_take_count: 0,
|
||||
old_active_take: None,
|
||||
executed: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Swap the instance's backend clip to whatever take the document now says is active.
|
||||
///
|
||||
/// Same remove + re-add as `SetActiveTakeAction` — there's no in-place pool-swap command.
|
||||
fn resync(&self, backend: &mut BackendContext, document: &Document) -> Result<(), String> {
|
||||
let instance = document
|
||||
.get_layer(&self.layer_id)
|
||||
.and_then(|l| match l {
|
||||
AnyLayer::Audio(al) => al.clip_instances.iter().find(|ci| ci.id == self.instance_id),
|
||||
_ => None,
|
||||
})
|
||||
.cloned()
|
||||
.ok_or_else(|| format!("Clip instance {} not found", self.instance_id))?;
|
||||
|
||||
let existing: Option<BackendClipInstanceId> = backend
|
||||
.clip_instance_to_backend_map
|
||||
.get(&self.instance_id)
|
||||
.copied();
|
||||
let track_id = backend.layer_to_track_map.get(&self.layer_id).copied();
|
||||
if let (Some(backend_id), Some(track_id)) = (existing, track_id) {
|
||||
backend.remove_clip_instance(track_id, backend_id, self.instance_id);
|
||||
}
|
||||
|
||||
backend.add_clip_instance(document, &self.layer_id, &instance)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Action for AppendTakesAction {
|
||||
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
let clip = document
|
||||
.audio_clips
|
||||
.get_mut(&self.clip_id)
|
||||
.ok_or_else(|| format!("Audio clip {} not found", self.clip_id))?;
|
||||
|
||||
let AudioClipType::TakeFolder { takes, .. } = &mut clip.clip_type else {
|
||||
return Err("Can only append takes to a take folder".to_string());
|
||||
};
|
||||
|
||||
// Only record the pre-state on the first execute; a redo must not overwrite it with the
|
||||
// post-state left behind by the previous run.
|
||||
if !self.executed {
|
||||
self.old_take_count = takes.len();
|
||||
}
|
||||
takes.extend(self.new_takes.iter().cloned());
|
||||
// Renumber so the names stay in step with the take indices the badge shows.
|
||||
for (i, take) in takes.iter_mut().enumerate() {
|
||||
take.name = format!("Take {}", i + 1);
|
||||
}
|
||||
let new_active = takes.len() - 1;
|
||||
|
||||
let layer = document
|
||||
.get_layer_mut(&self.layer_id)
|
||||
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
|
||||
let AnyLayer::Audio(audio_layer) = layer else {
|
||||
return Err("Take folders only exist on audio layers".to_string());
|
||||
};
|
||||
let instance = audio_layer
|
||||
.clip_instances
|
||||
.iter_mut()
|
||||
.find(|ci| ci.id == self.instance_id)
|
||||
.ok_or_else(|| format!("Clip instance {} not found", self.instance_id))?;
|
||||
|
||||
if !self.executed {
|
||||
self.old_active_take = instance.active_take;
|
||||
}
|
||||
// Land on the take just recorded, GarageBand-style.
|
||||
instance.active_take = Some(new_active);
|
||||
self.executed = true;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
if let Some(clip) = document.audio_clips.get_mut(&self.clip_id) {
|
||||
if let AudioClipType::TakeFolder { takes, .. } = &mut clip.clip_type {
|
||||
takes.truncate(self.old_take_count);
|
||||
for (i, take) in takes.iter_mut().enumerate() {
|
||||
take.name = format!("Take {}", i + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(AnyLayer::Audio(audio_layer)) = document.get_layer_mut(&self.layer_id) {
|
||||
if let Some(instance) = audio_layer
|
||||
.clip_instances
|
||||
.iter_mut()
|
||||
.find(|ci| ci.id == self.instance_id)
|
||||
{
|
||||
instance.active_take = self.old_active_take;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn description(&self) -> String {
|
||||
format!("Record {} take(s)", self.new_takes.len())
|
||||
}
|
||||
|
||||
fn execute_backend(
|
||||
&mut self,
|
||||
backend: &mut BackendContext,
|
||||
document: &Document,
|
||||
) -> Result<(), String> {
|
||||
self.resync(backend, document)
|
||||
}
|
||||
|
||||
fn rollback_backend(
|
||||
&mut self,
|
||||
backend: &mut BackendContext,
|
||||
document: &Document,
|
||||
) -> Result<(), String> {
|
||||
self.resync(backend, document)
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ pub mod reorder_clip_instances;
|
||||
pub mod paint_bucket;
|
||||
pub mod remove_effect;
|
||||
pub mod set_cycle_region;
|
||||
pub mod append_takes;
|
||||
pub mod set_active_take;
|
||||
pub mod set_document_properties;
|
||||
pub mod set_instance_properties;
|
||||
@@ -53,6 +54,7 @@ pub mod resize_text_box;
|
||||
|
||||
pub use add_clip_instance::AddClipInstanceAction;
|
||||
pub use set_cycle_region::SetCycleRegionAction;
|
||||
pub use append_takes::AppendTakesAction;
|
||||
pub use set_active_take::SetActiveTakeAction;
|
||||
pub use add_effect::AddEffectAction;
|
||||
pub use add_layer::AddLayerAction;
|
||||
|
||||
@@ -932,6 +932,46 @@ impl Document {
|
||||
self.get_clip_duration(clip_id).map(crate::clip::ClipDuration::Seconds)
|
||||
}
|
||||
|
||||
/// Find an existing take folder on `layer_id` that a new cycle recording should be appended to.
|
||||
///
|
||||
/// Cycle-recording over a region that already holds a take folder should ADD to that folder
|
||||
/// rather than drop a second clip on top of it — otherwise the new takes are stranded in a
|
||||
/// separate, overlapping clip and can't be auditioned against the ones already there.
|
||||
///
|
||||
/// "The same region" means the instance starts at `loop_start` and its folder was recorded
|
||||
/// against the same loop length. Matching the length too means resizing the cycle region starts
|
||||
/// a fresh folder rather than appending takes of a different length to an existing one, which
|
||||
/// would break the uniform-take invariant comping depends on.
|
||||
///
|
||||
/// `exclude` is the in-progress recording's own instance, which is on the layer but isn't a
|
||||
/// candidate. Returns (instance id, clip id).
|
||||
pub fn take_folder_at(
|
||||
&self,
|
||||
layer_id: &Uuid,
|
||||
loop_start: Beats,
|
||||
loop_len: Beats,
|
||||
exclude: &Uuid,
|
||||
) -> Option<(Uuid, Uuid)> {
|
||||
let Some(AnyLayer::Audio(audio_layer)) = self.get_layer(layer_id) else {
|
||||
return None;
|
||||
};
|
||||
const EPS: f64 = 1e-6;
|
||||
audio_layer.clip_instances.iter().find_map(|ci| {
|
||||
if ci.id == *exclude || (ci.timeline_start - loop_start).beats_to_f64().abs() > EPS {
|
||||
return None;
|
||||
}
|
||||
let clip = self.audio_clips.get(&ci.clip_id)?;
|
||||
match clip.clip_type {
|
||||
crate::clip::AudioClipType::TakeFolder { recorded_loop_beats, .. }
|
||||
if (recorded_loop_beats - loop_len).beats_to_f64().abs() < EPS =>
|
||||
{
|
||||
Some((ci.id, ci.clip_id))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Resolve a [`ContentTime`] (a trim bound) against the clip it belongs to.
|
||||
///
|
||||
/// The clip is the only thing that knows whether its content is measured in seconds or beats, so
|
||||
|
||||
Reference in New Issue
Block a user