Take management: move takes onto the clip instance
Take management should be per clip instance — deleting a take from one half of a comped split shouldn't pull it out from under the other half. That's cleanest if the takes themselves live on the instance rather than the clip, so they do now. AudioClipType::TakeFolder is gone entirely. A clip is plain Sampled/Midi content again, and an instance with a non-empty `takes` list simply OVERRIDES it with whichever take is active. Splitting already clones the instance, so each half gets its own take list for free — no index remapping across instances, no copy-on-write, no shared-state surprise — and comping still works, because the halves can still each select a different take. It collapsed machinery too: resolve() moved from the clip to the instance, and owns_audio_pool_index went back to a one-liner. Management (DeleteTakeAction, DeleteUnusedTakesAction, RenameTakeAction): - Right-click a clip with more than one take: `Delete "<active take>"` and "Delete Unused Takes". Deletion is named after the take that's PLAYING rather than being a generic entry, so you pick the victim by selecting it — one clear act instead of hunting a small trash icon in a list (which is where this started, and it was fiddly). - Double-click a take in the dropdown to rename it in place. - What happens to the selection on delete is the subtle part, and there's a test per case: deleting a take BELOW the active one shifts the selection down so you keep hearing the same take; deleting the ACTIVE take lands on whatever slid into its place (not silently back to take 1); deleting the LAST take steps back one. The only take can't be deleted at all — the menu item isn't offered. - Deleted takes' audio stays in the pool: undo has to put it back, and the other half of a split may still be playing it. Fixes: - A recording that stopped before the loop came round wasn't joining an existing take list — it landed as a separate overlapping clip. Trigger- on-wrap is right for the FIRST recording, but once takes exist there, a further run is plainly another take however short. The engine can't know that (it's document state), so the editor passes `force_takes` with the start-recording command and the run is cut and padded to the region even with zero wraps. This forced cycle_loop_len and `wrapped` apart on the MIDI side: the region length has to be known from the start, but the clip should only pin to full-region length AFTER a pass completes, or the bar jumps to full width the moment you hit record. - Recording a second take left BOTH sounding. append_cycle_takes tore down the recording's backend clip by looking it up in clip_instance_to_backend_map — but on the audio path the recording instance isn't in that map yet; it's only added during promotion, which the append path skips. The event already carries the engine's clip id, so it's handed over explicitly now. - The take badge is hidden when there's only one take — no choice to make.
This commit is contained in:
@@ -645,8 +645,10 @@ impl Engine {
|
||||
// every pass and — because this block also resizes the backend clip instance
|
||||
// below — shrank the instance back to nothing, so the notes just merged into it
|
||||
// were never scheduled and you overdubbed against silence.
|
||||
// Pinned only once a pass has actually completed — until then the bar still grows.
|
||||
let duration = recording
|
||||
.cycle_loop_len
|
||||
.filter(|_| recording.wrapped)
|
||||
.unwrap_or(current_time - recording.start_time);
|
||||
// In separate-takes mode the preview shows only the pass being played now — the
|
||||
// earlier passes are alternative takes, not layers, so drawing them all on top of
|
||||
@@ -1490,9 +1492,9 @@ impl Engine {
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
Command::StartRecording(track_id, start_time) => {
|
||||
Command::StartRecording(track_id, start_time, force_takes) => {
|
||||
// Start recording on the specified track
|
||||
self.handle_start_recording(track_id, start_time);
|
||||
self.handle_start_recording(track_id, start_time, force_takes);
|
||||
}
|
||||
Command::StopRecording => {
|
||||
// Stop the current recording
|
||||
@@ -1510,9 +1512,9 @@ impl Engine {
|
||||
recording.resume();
|
||||
}
|
||||
}
|
||||
Command::StartMidiRecording(track_id, clip_id, start_time) => {
|
||||
Command::StartMidiRecording(track_id, clip_id, start_time, force_takes) => {
|
||||
// Start MIDI recording on the specified track
|
||||
self.handle_start_midi_recording(track_id, clip_id, start_time);
|
||||
self.handle_start_midi_recording(track_id, clip_id, start_time, force_takes);
|
||||
}
|
||||
Command::StopMidiRecording => {
|
||||
eprintln!("[ENGINE] Received StopMidiRecording command");
|
||||
@@ -3264,7 +3266,7 @@ impl Engine {
|
||||
}
|
||||
|
||||
/// Handle starting a recording
|
||||
fn handle_start_recording(&mut self, track_id: TrackId, start_time: Beats) {
|
||||
fn handle_start_recording(&mut self, track_id: TrackId, start_time: Beats, force_takes: bool) {
|
||||
use crate::io::WavWriter;
|
||||
use std::env;
|
||||
|
||||
@@ -3348,6 +3350,7 @@ impl Engine {
|
||||
loop_len_frames: (le - ls).max(0) as usize,
|
||||
lead_pad_frames: (self.playhead - ls).max(0) as usize,
|
||||
wrap_count: 0,
|
||||
force_takes,
|
||||
}
|
||||
})
|
||||
} else {
|
||||
@@ -3552,12 +3555,23 @@ impl Engine {
|
||||
}
|
||||
|
||||
/// Handle starting MIDI recording
|
||||
fn handle_start_midi_recording(&mut self, track_id: TrackId, clip_id: MidiClipId, start_time: Beats) {
|
||||
fn handle_start_midi_recording(&mut self, track_id: TrackId, clip_id: MidiClipId, start_time: Beats, force_takes: bool) {
|
||||
// Check if track exists and is a MIDI track
|
||||
if let Some(crate::audio::track::TrackNode::Midi(_)) = self.project.get_track_mut(track_id) {
|
||||
// Create MIDI recording state
|
||||
let mut recording_state = MidiRecordingState::new(track_id, clip_id, start_time);
|
||||
|
||||
// Note the cycle region up front. `wrapped` stays false until a pass actually completes,
|
||||
// so the clip bar still grows with the playhead through the first pass.
|
||||
if self.loop_enabled {
|
||||
if let Some((ls, le)) = self.loop_region {
|
||||
if le > ls {
|
||||
recording_state.cycle_loop_len = Some(le - ls);
|
||||
recording_state.force_takes = force_takes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Inject any notes currently held on this track (pressed during count-in pre-roll)
|
||||
// so they start at t=0 of the recording rather than being lost
|
||||
if let Some(held) = self.midi_held_notes.get(&track_id) {
|
||||
@@ -3598,12 +3612,13 @@ impl Engine {
|
||||
|
||||
// ---- Separate takes: one pool clip per cycle pass ----
|
||||
//
|
||||
// Only when the transport actually wrapped; a recording that stopped inside the first
|
||||
// pass is an ordinary single recording and falls through to the merge path below, just
|
||||
// as it does for audio.
|
||||
if let (true, Some(loop_len)) =
|
||||
(self.cycle_midi_separate_takes, recording.cycle_loop_len)
|
||||
{
|
||||
// Normally only when the transport actually wrapped: a recording that stopped inside the
|
||||
// first pass is an ordinary single recording and falls through to the merge path below,
|
||||
// just as it does for audio. `force_takes` overrides that, because the region already
|
||||
// holds takes and this is another one however short it ran.
|
||||
let takes_mode = self.cycle_midi_separate_takes
|
||||
&& (recording.wrapped || recording.force_takes);
|
||||
if let (true, Some(loop_len)) = (takes_mode, recording.cycle_loop_len) {
|
||||
let loop_start = recording.start_time; // a cycle recording is anchored at the region
|
||||
let passes = recording.pass_count();
|
||||
let buckets = recording.notes_by_pass(passes);
|
||||
@@ -3667,10 +3682,11 @@ impl Engine {
|
||||
|
||||
let notes = recording.get_notes().to_vec();
|
||||
let note_count = notes.len();
|
||||
// A cycle MIDI recording is anchored at the region start and every pass overdubs into
|
||||
// the same clip (MERGE), so the clip is exactly one region long — not however long the
|
||||
// user held the record button, which would run past the loop end.
|
||||
let recording_duration = match recording.cycle_loop_len {
|
||||
// A cycle MIDI recording that came round is anchored at the region start and every pass
|
||||
// overdubs into the same clip (MERGE), so the clip is exactly one region long — not
|
||||
// however long the user held the record button, which would run past the loop end. One
|
||||
// that stopped inside the first pass is just an ordinary recording.
|
||||
let recording_duration = match recording.cycle_loop_len.filter(|_| recording.wrapped) {
|
||||
Some(loop_len) => loop_len,
|
||||
None => end_time - recording.start_time,
|
||||
};
|
||||
@@ -4206,8 +4222,11 @@ impl EngineController {
|
||||
}
|
||||
|
||||
/// Start recording on a track
|
||||
pub fn start_recording(&mut self, track_id: TrackId, start_time: Beats) {
|
||||
let _ = self.command_tx.push(Command::StartRecording(track_id, start_time));
|
||||
/// `force_takes`: cut takes even if the transport never wraps, because the cycle region already
|
||||
/// holds takes and this recording is another one. Whether that's so is document state, so only
|
||||
/// the editor can answer it.
|
||||
pub fn start_recording(&mut self, track_id: TrackId, start_time: Beats, force_takes: bool) {
|
||||
let _ = self.command_tx.push(Command::StartRecording(track_id, start_time, force_takes));
|
||||
}
|
||||
|
||||
/// Stop the current recording
|
||||
@@ -4226,8 +4245,9 @@ impl EngineController {
|
||||
}
|
||||
|
||||
/// Start MIDI recording on a track
|
||||
pub fn start_midi_recording(&mut self, track_id: TrackId, clip_id: MidiClipId, start_time: Beats) {
|
||||
let _ = self.command_tx.push(Command::StartMidiRecording(track_id, clip_id, start_time));
|
||||
/// `force_takes`: see [`EngineController::start_recording`].
|
||||
pub fn start_midi_recording(&mut self, track_id: TrackId, clip_id: MidiClipId, start_time: Beats, force_takes: bool) {
|
||||
let _ = self.command_tx.push(Command::StartMidiRecording(track_id, clip_id, start_time, force_takes));
|
||||
}
|
||||
|
||||
/// Stop the current MIDI recording
|
||||
|
||||
@@ -24,9 +24,20 @@ pub struct CycleRecordInfo {
|
||||
/// punch-in (record while already rolling); take 1 gets this much silence prepended so it still
|
||||
/// spans the whole region.
|
||||
pub lead_pad_frames: usize,
|
||||
/// How many times the transport wrapped during this recording. Zero means the user stopped
|
||||
/// before completing a pass, which stays an ordinary single recording.
|
||||
/// How many times the transport wrapped during this recording. Zero normally means the user
|
||||
/// stopped before completing a pass, which stays an ordinary single recording — unless
|
||||
/// `force_takes` says otherwise.
|
||||
pub wrap_count: usize,
|
||||
/// Cut takes even if the transport never wrapped.
|
||||
///
|
||||
/// Set when the region already holds takes: a further recording there is another take, however
|
||||
/// short, and it gets padded out to the region like any partial pass. Without this a run that
|
||||
/// stopped before the loop came round would land as a separate overlapping clip instead of
|
||||
/// joining the take list.
|
||||
///
|
||||
/// The editor decides this at record start, because whether takes already exist is document
|
||||
/// state the engine can't see.
|
||||
pub force_takes: bool,
|
||||
}
|
||||
|
||||
/// Min/max waveform peaks for a finished buffer of interleaved samples.
|
||||
@@ -130,7 +141,7 @@ impl RecordingState {
|
||||
/// single recording, which keeps the existing path untouched).
|
||||
pub fn slice_takes(&self) -> Option<Vec<Vec<f32>>> {
|
||||
let cycle = self.cycle?;
|
||||
if cycle.wrap_count == 0 || cycle.loop_len_frames == 0 {
|
||||
if (cycle.wrap_count == 0 && !cycle.force_takes) || cycle.loop_len_frames == 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
@@ -295,13 +306,19 @@ pub struct MidiRecordingState {
|
||||
active_notes: HashMap<u8, ActiveMidiNote>,
|
||||
/// Completed notes: (time_offset, note, velocity, duration) — all times in beats
|
||||
pub completed_notes: Vec<(Beats, u8, u8, Beats)>,
|
||||
/// The cycle region's length in beats, when recording into a cycle.
|
||||
/// The cycle region's length in beats, if one was armed at record start.
|
||||
///
|
||||
/// A cycle MIDI recording is anchored at the region start (`start_time == loop_start`), which is
|
||||
/// what makes MERGE fall out for free: the transport always wraps back into the region, so every
|
||||
/// note's offset already lands inside `[0, loop_len)` and successive passes overdub onto each
|
||||
/// other with no folding needed. Set only if the transport actually wrapped.
|
||||
/// other with no folding needed.
|
||||
pub cycle_loop_len: Option<Beats>,
|
||||
/// Whether the transport actually came round. Distinct from `cycle_loop_len`, which only says a
|
||||
/// region was armed: the clip only pins to the full region once a pass has completed, so until
|
||||
/// then the bar still grows with the playhead.
|
||||
pub wrapped: bool,
|
||||
/// Cut takes even if the transport never wrapped — see [`CycleRecordInfo::force_takes`].
|
||||
pub force_takes: bool,
|
||||
/// Which cycle pass is currently being recorded (0-based). Bumped at each wrap.
|
||||
current_pass: usize,
|
||||
/// The pass each completed note belongs to, parallel to `completed_notes`.
|
||||
@@ -320,6 +337,8 @@ impl MidiRecordingState {
|
||||
active_notes: HashMap::new(),
|
||||
completed_notes: Vec::new(),
|
||||
cycle_loop_len: None,
|
||||
wrapped: false,
|
||||
force_takes: false,
|
||||
current_pass: 0,
|
||||
note_pass: Vec::new(),
|
||||
}
|
||||
@@ -450,9 +469,10 @@ impl MidiRecordingState {
|
||||
self.note_on(note, velocity, region_start);
|
||||
}
|
||||
|
||||
// The transport wrapped, so this is a cycle recording: the clip spans the whole region
|
||||
// rather than however long the user happened to hold the record button.
|
||||
// A pass has completed, so from here the clip spans the whole region rather than however long
|
||||
// the user happens to hold the record button.
|
||||
self.cycle_loop_len = Some(region_end - region_start);
|
||||
self.wrapped = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -462,6 +482,14 @@ mod cycle_tests {
|
||||
|
||||
/// A recording state holding `audio_data`, armed for cycle recording. Mono, 100 Hz, so a frame
|
||||
/// is a sample and 5 frames is 50 ms (exactly the min-take threshold).
|
||||
fn rec_forced(audio: Vec<f32>, loop_len_frames: usize, lead_pad_frames: usize, wraps: usize) -> RecordingState {
|
||||
let mut r = rec(audio, loop_len_frames, lead_pad_frames, wraps);
|
||||
if let Some(c) = r.cycle.as_mut() {
|
||||
c.force_takes = true;
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
fn rec(audio: Vec<f32>, loop_len_frames: usize, lead_pad_frames: usize, wraps: usize) -> RecordingState {
|
||||
let mut r = RecordingState::new(
|
||||
0,
|
||||
@@ -480,6 +508,7 @@ mod cycle_tests {
|
||||
loop_len_frames,
|
||||
lead_pad_frames,
|
||||
wrap_count: wraps,
|
||||
force_takes: false,
|
||||
});
|
||||
r
|
||||
}
|
||||
@@ -492,6 +521,18 @@ mod cycle_tests {
|
||||
assert!(r.slice_takes().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn force_takes_makes_a_partial_pass_a_take() {
|
||||
// Recording over a region that already holds takes: this run is another take however short
|
||||
// it ran, so it's cut and padded like any partial pass rather than landing as a separate
|
||||
// overlapping clip. Two real frames of an 8-frame region -> one take, silence for the rest.
|
||||
let takes = rec_forced(vec![1.0, 2.0], 8, 0, 0)
|
||||
.slice_takes()
|
||||
.expect("forced takes");
|
||||
assert_eq!(takes.len(), 1);
|
||||
assert_eq!(takes[0], vec![1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn takes_are_cut_at_exact_loop_multiples() {
|
||||
// 12 frames of audio, 4-frame loop, started at the region start => 3 clean takes.
|
||||
|
||||
@@ -142,7 +142,9 @@ pub enum Command {
|
||||
|
||||
// Recording commands
|
||||
/// Start recording on a track (track_id, start_time)
|
||||
StartRecording(TrackId, Beats),
|
||||
/// (track, start_time, force_takes — cut takes even if the transport never wraps, because the
|
||||
/// region already holds takes and this is another one)
|
||||
StartRecording(TrackId, Beats, bool),
|
||||
/// Stop the current recording
|
||||
StopRecording,
|
||||
/// Pause the current recording
|
||||
@@ -152,7 +154,8 @@ pub enum Command {
|
||||
|
||||
// MIDI Recording commands
|
||||
/// Start MIDI recording on a track (track_id, clip_id, start_time)
|
||||
StartMidiRecording(TrackId, MidiClipId, Beats),
|
||||
/// (track, clip, start_time, force_takes — see [`Command::StartRecording`])
|
||||
StartMidiRecording(TrackId, MidiClipId, Beats, bool),
|
||||
/// Stop the current MIDI recording
|
||||
StopMidiRecording,
|
||||
|
||||
|
||||
Reference in New Issue
Block a user