Skip to content
Open
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
16 changes: 14 additions & 2 deletions crates/os-app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19970,7 +19970,16 @@ mod tests {
let t1 = os_engine::sample::load(&dir.join("01 Track 1.wav")).expect("track file");
assert!(loud(&t1), "the plugin channel's track is not silent");
let _ = std::fs::remove_dir_all(&dir);
// Consolidate: the clip is muted, a new audio clip carries the sound.
// Consolidate: the clip is muted, a new audio clip carries the sound. The project's Rendered folder has a gap in
// its numbering (an earlier render was deleted): the new file takes the gap and neither earlier file changes.
let project = std::env::temp_dir().join(format!("os-consolidate-{}", std::process::id()));
let rendered = project.join("Rendered");
let _ = std::fs::remove_dir_all(&project);
std::fs::create_dir_all(&rendered).unwrap();
app.state.session.path = Some(project.join("Song.flp"));
let (first, third) = (rendered.join("Consolidated 1.wav"), rendered.join("Consolidated 3.wav"));
std::fs::write(&first, b"first render").unwrap();
std::fs::write(&third, b"third render").unwrap();
app.state.playlist.sel = vec![0];
pl::run(&mut app, pl::Cmd::Consolidate).unwrap();
let m = finish_playlist_render(&mut app);
Expand All @@ -19980,9 +19989,12 @@ mod tests {
assert!(clips[0].muted && !clips[1].muted, "source muted, render audible");
assert_eq!(clips[1].length, 4 * ppq);
let path = m.trim_start_matches("Consolidated to ").to_string();
assert_eq!(std::fs::read(&first).unwrap(), b"first render", "an earlier render was overwritten");
assert_eq!(std::fs::read(&third).unwrap(), b"third render", "an earlier render was overwritten");
assert_eq!(std::path::Path::new(&path), rendered.join("Consolidated 2.wav"));
let r = os_engine::sample::load(std::path::Path::new(&path)).expect("render file");
assert!(loud(&r), "the consolidated file is not silent");
let _ = std::fs::remove_file(&path);
let _ = std::fs::remove_dir_all(&project);
}

/// Tools menu (spec: ui/playlist.md §10): quick quantize moves clip starts onto the snap grid (one undo step);
Expand Down
69 changes: 60 additions & 9 deletions crates/os-app/src/playlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4673,9 +4673,9 @@ pub fn quantize_starts(s: &mut State) -> String {
/// Both go through the export's path (`App::start_export_of`), so hosted instruments and effects play.
#[derive(Debug, Clone)]
pub enum Render {
/// Consolidate: the clips `used` (arranger indices) over `a..b` render to a file; they are muted and a clip of it
/// goes on `track` at `a`.
Consolidate { a: u32, b: u32, used: Vec<usize>, track: u16 },
/// Consolidate: the clips `used` (arranger indices) over `a..b` render to `file` (claimed empty beforehand,
/// `claim_consolidated`); they are muted and a clip of it goes on `track` at `a`.
Consolidate { a: u32, b: u32, used: Vec<usize>, track: u16, file: PathBuf },
/// Export all playlist tracks: `todo` tracks are still to render, one at a time, from `audible` clips, each file
/// `end` ticks long, into `dir`.
Tracks { todo: Vec<u16>, audible: Vec<Clip>, end: u32, dir: PathBuf, written: usize },
Expand Down Expand Up @@ -4794,14 +4794,34 @@ pub fn consolidate(app: &mut App) -> String {
if let Err(e) = std::fs::create_dir_all(&dir) {
return format!("Consolidate: {e}");
}
let n = std::fs::read_dir(&dir).map(|d| d.count()).unwrap_or(0) + 1;
let job = render_job(s, &dir, &format!("Consolidated {n}"), b - a);
let file = match claim_consolidated(&dir) {
Ok(f) => f,
Err(e) => return format!("Consolidate: {e}"),
};
let stem = file.file_stem().map_or_else(String::new, |n| n.to_string_lossy().into_owned());
let job = render_job(s, &dir, &stem, b - a);
let track = free_track(s, track_after_used(s), a, b);
app.playlist_render = Some(Render::Consolidate { a, b, used: use_idx, track });
app.playlist_render = Some(Render::Consolidate { a, b, used: use_idx, track, file });
app.start_export_of(job, p, true);
"Consolidating...".into()
}

/// The first free `Consolidated N.wav` in `dir`, created empty for the render to write into. A clip may still play
/// any earlier render, so the number is never taken from a count of the folder (a deleted render leaves a gap that
/// count lands on); the file is created only if it is not there, which also keeps two renders into the same folder at
/// once (two projects, two Lemon Studios) apart.
fn claim_consolidated(dir: &Path) -> std::io::Result<PathBuf> {
let mut n = 1u32;
loop {
let path = dir.join(format!("Consolidated {n}.wav"));
match std::fs::File::create_new(&path) {
Ok(_) => return Ok(path),
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists && n < u32::MAX => n += 1,
Err(e) => return Err(e),
}
}
}

/// Places a finished Consolidate's file: the source clips muted and a clip of the render on `track`, one undo step.
fn consolidate_place(s: &mut State, a: u32, b: u32, used: &[usize], track: u16, file: &Path) -> String {
let path = file.to_string_lossy().into_owned();
Expand Down Expand Up @@ -4850,11 +4870,18 @@ impl App {
/// tracks goes on to the next track.
pub(crate) fn playlist_render_done(&mut self, r: Render, result: Result<(f64, Vec<PathBuf>), String>) {
let msg = match (r, result) {
(Render::Consolidate { .. }, Err(e)) => format!("Consolidate: {e}"),
// The claimed file holds nothing of use when the render failed: it goes, so it does not take a number.
(Render::Consolidate { file, .. }, Err(e)) => {
let _ = std::fs::remove_file(file);
format!("Consolidate: {e}")
}
(Render::Tracks { .. }, Err(e)) => format!("Export tracks: {e}"),
(Render::Consolidate { a, b, used, track }, Ok((_, files))) => match files.first() {
(Render::Consolidate { a, b, used, track, file }, Ok((_, files))) => match files.first() {
Some(f) => consolidate_place(&mut self.state, a, b, &used, track, f),
None => "Consolidate: no file was written".into(),
None => {
let _ = std::fs::remove_file(file);
"Consolidate: no file was written".into()
}
},
(Render::Tracks { todo, audible, end, dir, written }, Ok(_)) => {
self.playlist_render = Some(Render::Tracks { todo, audible, end, dir, written: written + 1 });
Expand Down Expand Up @@ -5425,6 +5452,30 @@ mod tests {
app
}

/// Consolidate's file takes the first free number, a gap left by a deleted render included, and never an existing
/// file; renders claiming at the same moment each get their own.
#[test]
fn consolidate_claims_a_free_file() {
let dir = std::env::temp_dir().join(format!("os-consolidate-claim-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("Consolidated 1.wav"), b"one").unwrap();
std::fs::write(dir.join("Consolidated 3.wav"), b"three").unwrap();
assert_eq!(claim_consolidated(&dir).unwrap(), dir.join("Consolidated 2.wav"));
assert_eq!(claim_consolidated(&dir).unwrap(), dir.join("Consolidated 4.wav"));
assert_eq!(std::fs::read(dir.join("Consolidated 1.wav")).unwrap(), b"one");
assert_eq!(std::fs::read(dir.join("Consolidated 3.wav")).unwrap(), b"three");
let start = std::sync::Barrier::new(8);
let mut got: Vec<PathBuf> = std::thread::scope(|t| {
let hs: Vec<_> = (0..8).map(|_| t.spawn(|| { start.wait(); claim_consolidated(&dir).unwrap() })).collect();
hs.into_iter().map(|h| h.join().unwrap()).collect()
});
got.sort();
got.dedup();
assert_eq!(got.len(), 8, "two renders claimed the same file");
let _ = std::fs::remove_dir_all(&dir);
}

/// A solo belongs to the arrangement it was made in: shown another one, soloing there starts afresh rather than
/// putting the first arrangement's track states on this one's tracks.
#[test]
Expand Down
Loading