feat(crucible): keep every captured take
With Keep takes on (default), each capture is written to <folder>/takes/ as a WAV named by time, tempo and length, so a take is not lost when the next capture replaces it. Files are never overwritten: a clash gets a numbered suffix. Saving runs off the UI thread and the status line shows the path or the error.
This commit is contained in:
1 parent
839e46aec9
commit
6d868a63c4
8 files changed
+114
-4
No files matched your search
@@ -34,6 +34,52 @@ pub fn stamp(unix: u64) -> String {
|
||||
)
|
||||
}
|
||||
|
||||
/// Writes a captured take's loop to `root/takes/` right away, so it survives a reload or a
|
||||
/// crash. Never overwrites: a name already taken gets a counter. Returns the file written.
|
||||
pub fn save_take(take: &Take, root: &Path, unix: u64) -> std::io::Result<PathBuf> {
|
||||
let folder = root.join("takes");
|
||||
std::fs::create_dir_all(&folder)?;
|
||||
let base = format!(
|
||||
"{}_{:.0}bpm_{}bar{}",
|
||||
stamp(unix),
|
||||
take.tempo(),
|
||||
take.bars,
|
||||
if take.bars == 1 { "" } else { "s" }
|
||||
);
|
||||
let wave = wav::Wave {
|
||||
channels: 2,
|
||||
rate: take.rate.round() as u32,
|
||||
samples: take.audio.clone(),
|
||||
root: None,
|
||||
frame: None,
|
||||
};
|
||||
let bytes = wav::encode(&wave);
|
||||
for n in 1..1000 {
|
||||
let name = if n == 1 {
|
||||
format!("{base}.wav")
|
||||
} else {
|
||||
format!("{base}_{n}.wav")
|
||||
};
|
||||
let path = folder.join(name);
|
||||
match std::fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(&path)
|
||||
{
|
||||
Ok(mut file) => {
|
||||
std::io::Write::write_all(&mut file, &bytes)?;
|
||||
return Ok(path);
|
||||
}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
Err(std::io::Error::new(
|
||||
std::io::ErrorKind::AlreadyExists,
|
||||
"too many takes with the same name",
|
||||
))
|
||||
}
|
||||
|
||||
pub fn export(
|
||||
take: &Take,
|
||||
settings: &Settings,
|
||||
|
||||
@@ -5,7 +5,7 @@ mod player;
|
||||
mod ring;
|
||||
mod settings;
|
||||
mod take;
|
||||
pub use export::{export, stamp, Exported};
|
||||
pub use export::{export, save_take, stamp, Exported};
|
||||
pub use player::{Clip, Player, Queues};
|
||||
pub use ring::{Marker, Recorder, Ring, Transport, MARKERS};
|
||||
pub use settings::{default_folder, Division, Mode, Settings};
|
||||
|
||||
@@ -64,6 +64,8 @@ pub struct Settings {
|
||||
pub trim: bool,
|
||||
/// Also write a wavetable from the selected slice.
|
||||
pub wavetable: bool,
|
||||
/// Save every captured take's loop to the `takes` subfolder at once.
|
||||
pub keep_takes: bool,
|
||||
/// Export folder; empty selects the default.
|
||||
pub folder: String,
|
||||
}
|
||||
@@ -78,6 +80,7 @@ impl Default for Settings {
|
||||
normalize: true,
|
||||
trim: true,
|
||||
wavetable: false,
|
||||
keep_takes: true,
|
||||
folder: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user