Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
01495ef
engine: a stereo or wider file imported onto a mono track with as man…
BlueJayLouche Nov 21, 2026
88948ba
engine, ui: Split to Mono Tracks (clip.split_to_mono, the clip menu):…
BlueJayLouche Nov 21, 2026
b179c1f
ui: a file dropped from the file manager lands where it is let go, as…
BlueJayLouche Nov 21, 2026
07f3097
engine: second refutation review of Split to Mono Tracks (ADR 0009): …
BlueJayLouche Nov 21, 2026
f9b191a
ui, engine: third refutation review of the file-manager drop routing …
BlueJayLouche Nov 21, 2026
60f21b9
ui, engine: fourth refutation review (ADR 0009): a drop holding a ses…
BlueJayLouche Nov 21, 2026
87d52c3
ui: fifth refutation review of the drop routing (ADR 0009): files dro…
BlueJayLouche Nov 21, 2026
5b94b37
ui: sixth refutation review of the drop routing (ADR 0009): a test th…
BlueJayLouche Nov 21, 2026
fa4968b
engine, ui: maintainer review of #767: import keeps a file's channels…
BlueJayLouche Nov 22, 2026
13179db
ui, engine: refutation review of the #767 rework (ADR 0009): whether …
BlueJayLouche Nov 22, 2026
8fc9842
ui: second refutation review of the #767 rework (ADR 0009): a drop at…
BlueJayLouche Nov 22, 2026
3aa2ea1
ui: third refutation review of the #767 rework (ADR 0009): the mixer'…
BlueJayLouche Nov 22, 2026
e3aad37
ui: fourth refutation review of the #767 rework (ADR 0009): a drop wh…
BlueJayLouche Nov 22, 2026
22d235d
ui: fifth refutation review of the #767 rework (ADR 0009): a drop who…
BlueJayLouche Nov 22, 2026
d851acc
ui: sixth refutation review of the #767 rework (ADR 0009): a drop who…
BlueJayLouche Nov 22, 2026
96879b6
Merge branch 'main' into import-split-mono
derekmwright Nov 23, 2026
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
206 changes: 206 additions & 0 deletions crates/engine/src/cmd/clip_split.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
//! Split to Mono Tracks (`clip.split_to_mono`): each channel of the selected stereo or wider audio
//! clips on a new mono track below its track, the originals muted, in one undo step: two
//! microphones recorded onto one stereo track come apart this way. The clips of one track share
//! its new tracks. An in-memory source splits in memory (each channel is written to `Audio Files`
//! on save, as a recording is); a source played from its file (ADR 0035) gets one mono WAV per
//! channel written beside that file, played from disk in turn.

use super::*;
use crate::cmd;
use serde_json::json;
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::sync::Arc;
use transit_audio_io::{AudioBuffer, FileFormat};
use transit_model::disk::DiskSource;
use transit_model::{ChannelFormat, ClipContent, ClipId, SourceAudio, SourceId, TrackId, TrackKind};

const ID: &str = "clip.split_to_mono";

pub fn specs() -> Vec<CommandSpec> {
vec![cmd!(
"clip.split_to_mono",
"Split to Mono Tracks",
["Clip"],
None,
"{clips?} — each channel of the stereo or wider audio clips on a new mono track below its track; the originals are muted",
has_selection,
split_to_mono
)]
}

/// The most channels a clip splits into, one track each, so a hostile file's channel count never
/// becomes tens of thousands of tracks: a MADI-sized recording (64) fits, a wider one is refused.
const MAX_CHANNELS: usize = 64;

/// Channel `ch` of `n` in a split's names: `L` and `R` for stereo, else its number from 1 (a
/// file's channels carry no layout here; an eight-track field recording is not 7.1).
fn split_label(n: usize, ch: usize) -> String {
match (n, ch) {
(2, 0) => "L".into(),
(2, 1) => "R".into(),
_ => (ch + 1).to_string(),
}
}

/// A channel's audio, ready to become a source.
enum Mono {
Memory(SourceAudio),
/// The file written for it, and its audio played from that file.
Disk(PathBuf, Arc<SourceAudio>),
}

/// One source's split: its id, its BWF time stamp, and each channel's name and audio.
type Split = (SourceId, u64, Vec<(String, Mono)>);

fn split_to_mono(e: &mut Engine, p: &Value) -> Result<Value> {
let mut ids = clip_ids_param(e, p);
let mut seen = std::collections::HashSet::new();
ids.retain(|id| seen.insert(*id));
let s = e.session();
// The clips to split on each track, in track order, with their sources' channel counts.
let mut by_track: BTreeMap<usize, (TrackId, Vec<(ClipId, SourceId, usize)>)> = BTreeMap::new();
for id in &ids {
let (t, c) = s.find_clip(*id).ok_or_else(|| bad(ID, format!("unknown clip {}", id.0)))?;
let ClipContent::Audio { source, .. } = c.content else { continue };
let n = s.pool.get(source).map_or(0, |a| a.buffer.num_channels());
if n > MAX_CHANNELS {
return Err(bad(ID, format!("{} has {n} channels: a clip splits into at most {MAX_CHANNELS} tracks", c.name)));
}
if let (true, Some(i)) = (n > 1, s.track_index(t)) {
by_track.entry(i).or_insert((t, Vec::new())).1.push((*id, source, n));
}
}
if by_track.is_empty() {
return Err(bad(ID, "no stereo or wider audio clip is selected"));
}
// Muting the originals edits their tracks: refused before any file is written.
if let Some(tr) = by_track.values().filter_map(|(t, _)| s.track(*t)).find(|t| t.freeze.is_some()) {
return Err(bad(ID, format!("{} is frozen: unfreeze it to split its clips", tr.name)));
}
// Every channel's audio before the session changes, so a refusal leaves it as it was and no
// file written for it behind.
let mut written = Vec::new();
let split = match prepare(s, &by_track, &mut written) {
Ok(split) => split,
Err(err) => {
crate::io::split_files::remove(&written);
return Err(err);
}
};
let s = e.session_mut();
let mut sources: BTreeMap<(SourceId, usize), SourceId> = BTreeMap::new();
for (src, stamp, monos) in split {
for (ch, (name, mono)) in monos.into_iter().enumerate() {
let id = match mono {
// The extension keeps the label: a source is named after its file's stem.
Mono::Memory(audio) => crate::io::add_source_audio(s, &format!("{name}.wav"), Arc::new(audio), None, FileFormat::Wav),
// The path written, absolute as a recording's is: a save brings it into the session folder.
Mono::Disk(path, audio) => crate::io::add_disk_source(s, &format!("{name}.wav"), audio, &path.to_string_lossy()),
};
if let Some(sx) = s.sources.last_mut() {
sx.time_reference = stamp;
}
sources.insert((src, ch), id);
}
}
let (mut new_tracks, mut new_clips) = (Vec::new(), Vec::new());
for (t, clips) in by_track.into_values() {
let Some(tr) = s.track(t).cloned() else { continue };
let n = clips.iter().map(|c| c.2).max().unwrap_or(0);
let mut lanes = Vec::with_capacity(n);
for ch in 0..n {
let nt = s.add_track(TrackKind::Audio, ChannelFormat::Mono, Some(&format!("{} {}", tr.name, split_label(n, ch))));
// Taken out before `t` is found: a new track goes in before a Master, which may sit above `t`.
if let Some(from) = s.track_index(nt) {
let moved = s.tracks.remove(from);
let at = s.track_index(t).map_or(s.tracks.len(), |at| at + 1 + ch);
s.tracks.insert(at.min(s.tracks.len()), moved);
}
if let Some(m) = s.track_mut(nt) {
m.folder = tr.folder;
m.mixer.volume_db = tr.mixer.volume_db;
m.mixer.output = tr.mixer.output.clone();
m.mixer.sends = tr.mixer.sends.clone();
}
lanes.push(nt);
new_tracks.push(nt);
}
for (cid, src, channels) in clips {
let Some(orig) = s.find_clip(cid).map(|(_, c)| c.clone()) else { continue };
let ClipContent::Audio { offset, .. } = orig.content else { continue };
for (ch, &lane) in lanes.iter().enumerate().take(channels) {
let Some(&mono) = sources.get(&(src, ch)) else { continue };
let mut c = orig.clone();
c.id = s.new_clip_id();
c.name = format!("{}.{}", orig.name, split_label(channels, ch));
c.content = ClipContent::Audio { source: mono, offset };
c.group = None;
new_clips.push(c.id);
// Pushed as they are: the original's layering on its track stays as it was.
if let Some(pl) = s.track_mut(lane).and_then(|l| l.playlist_mut()) {
pl.clips.push(c);
pl.sort();
}
}
if let Some(c) = s.find_clip_mut(cid) {
c.muted = true;
}
}
}
Ok(json!({"tracks": new_tracks, "clips": new_clips}))
}

/// Channel `ch` of in-memory `audio` as mono audio, its peaks kept (rebuilt when there are none).
fn mono_audio(audio: &SourceAudio, ch: usize) -> SourceAudio {
let buffer = AudioBuffer { sample_rate: audio.buffer.sample_rate, channels: audio.buffer.channels.get(ch).cloned().into_iter().collect() };
match audio.peaks.get(ch) {
Some(p) => SourceAudio { buffer, peaks: vec![p.clone()], disk: None },
None => SourceAudio::new(buffer),
}
}

/// Each source's channels, in memory or as one mono WAV per channel written beside its file (a
/// number added while a name is taken). The files are opened to play from disk only once every
/// one is complete; each path created is in `written`, for the caller to remove on a failure.
fn prepare(
s: &transit_model::Session,
by_track: &BTreeMap<usize, (TrackId, Vec<(ClipId, SourceId, usize)>)>,
written: &mut Vec<PathBuf>,
) -> Result<Vec<Split>> {
let mut out: Vec<(SourceId, u64, Vec<(String, Option<SourceAudio>)>, Option<Arc<DiskSource>>)> = Vec::new();
for (_, clips) in by_track.values() {
for &(_, src, n) in clips {
if out.iter().any(|o| o.0 == src) {
continue;
}
let (Some(meta), Some(audio)) = (s.source(src), s.pool.get(src)) else { continue };
let names: Vec<String> = (0..n).map(|ch| format!("{}.{}", meta.name, split_label(n, ch))).collect();
let channels = match &audio.disk {
Some(disk) => {
crate::io::split_files::write(disk, &names, written)?;
names.into_iter().map(|name| (name, None)).collect()
}
None => names.into_iter().enumerate().map(|(ch, name)| (name, Some(mono_audio(audio, ch)))).collect(),
};
out.push((src, meta.time_reference, channels, audio.disk.clone()));
}
}
let mut files = written.iter();
out.into_iter()
.map(|(src, stamp, channels, disk)| {
let monos = channels
.into_iter()
.map(|(name, mem)| match (mem, &disk) {
(Some(audio), _) => Ok((name, Mono::Memory(audio))),
(None, Some(disk)) => {
let p = files.next().ok_or_else(|| EngineError::Io(format!("{name}: its file was not written")))?;
Ok((name, Mono::Disk(p.clone(), crate::io::split_files::open(p, disk.rate())?)))
}
(None, None) => Err(EngineError::Io(format!("{name}: no audio"))),
})
.collect::<Result<Vec<_>>>()?;
Ok((src, stamp, monos))
})
.collect()
}
4 changes: 4 additions & 0 deletions crates/engine/src/cmd/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ fn commit_import(e: &mut Engine, p: &Value) -> Result<Value> {
/// audio track or errors when none is selected.
fn import_audio_target(e: &Engine, p: &Value) -> Result<Option<TrackId>> {
if let Some(t) = track_param(e, "file.import_audio", p, "track")? {
// As `file.commit_import`: audio never lands on a MIDI, instrument or other track.
if let Some(tr) = e.session().track(t).filter(|tr| tr.kind != TrackKind::Audio) {
return Err(bad("file.import_audio", format!("{} is not an audio track", tr.name)));
}
return Ok(Some(t));
}
if bool_or(p, "new_tracks", true) {
Expand Down
2 changes: 2 additions & 0 deletions crates/engine/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod clip;
pub(crate) mod clip_bounce;
mod clip_list;
mod clip_more;
mod clip_split;
mod device_pages;
mod edit;
mod edit_more;
Expand Down Expand Up @@ -188,6 +189,7 @@ pub fn command_specs() -> &'static [CommandSpec] {
v.extend(track_more::specs());
v.extend(clip_more::specs());
v.extend(clip_bounce::specs());
v.extend(clip_split::specs());
v.extend(clip_list::specs());
v.extend(event_more::specs());
v.extend(setup_more::specs());
Expand Down
1 change: 1 addition & 0 deletions crates/engine/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,7 @@ pub(crate) use session::{DecodedFiles, decode_file_streamed, file_key, open_sess
use session::{replace_with, write_synced};

pub(crate) mod disk;
pub(crate) mod split_files;
pub use disk::{add_disk_source, copy_path};
mod disk_reads;
pub use disk_reads::{DiskReads, unreadable_line};
Expand Down
83 changes: 83 additions & 0 deletions crates/engine/src/io/split_files.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//! The files of Split to Mono Tracks (`clip.split_to_mono`) for a take played from its file (ADR
//! 0035): one mono float WAV per channel written beside it, opened to play from disk, and removed
//! again when the split fails.

use super::*;
use std::io::Write;
use transit_audio_io::{BitDepth, FileEncoder};
use transit_model::disk::DiskSource;

/// The file `path`, written by [`write`], as a source played from disk at `rate`.
pub(crate) fn open(path: &Path, rate: u32) -> Result<Arc<SourceAudio>> {
let len = std::fs::metadata(path).map_err(|err| EngineError::Io(format!("{}: {err}", path.display())))?.len();
disk::open_disk_audio(path, len, rate).map_err(EngineError::Io)
}

/// Removes the files a failed split wrote, and any peaks sidecar begun beside one.
pub(crate) fn remove(paths: &[PathBuf]) {
for p in paths {
let _ = std::fs::remove_file(p);
let _ = std::fs::remove_file(transit_model::disk::sidecar_path(p));
}
}

/// One mono float WAV per channel of `disk`'s file, beside it, named `names` (`<name>.wav`, or
/// `<name>-2.wav` and on while that is taken), in one pass over the file. Each path is pushed to
/// `written` as soon as it is created.
pub(crate) fn write(disk: &DiskSource, names: &[String], written: &mut Vec<PathBuf>) -> Result<()> {
let io = |p: &Path, err: &dyn std::fmt::Display| EngineError::Io(format!("{}: {err}", p.display()));
// A reader that failed once reads nothing more: opened anew, as before an offline render, so a
// take whose file is back splits again.
disk.check().map_err(|why| io(&disk.path(), &why))?;
let dir = disk.path().parent().map(Path::to_path_buf).unwrap_or_default();
let opts = EncodeOptions { format: FileFormat::Wav, bit_depth: BitDepth::Float32, dither: false, bwf: None, bitrate_kbps: None, title: None };
let mut encoders = Vec::with_capacity(names.len());
for name in names {
let stem = crate::io::sanitize_name(name);
let mut found = None;
for i in 1..1000 {
let p = dir.join(if i == 1 { format!("{stem}.wav") } else { format!("{stem}-{i}.wav") });
crate::file_access::check_write(&p).map_err(EngineError::Io)?;
match std::fs::OpenOptions::new().write(true).create_new(true).open(&p) {
Ok(f) => {
found = Some((p, f));
break;
}
Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {}
Err(err) => return Err(io(&p, &err)),
}
}
let (path, file) = found.ok_or_else(|| EngineError::Io(format!("{}: no file name free for {stem}.wav", dir.display())))?;
written.push(path.clone());
let enc = FileEncoder::new(std::io::BufWriter::new(file), &opts, 1, disk.rate(), None).map_err(|err| io(&path, &err))?;
enc.check_room(disk.frames()).map_err(|err| io(&path, &err))?;
encoders.push((path, enc));
}
const BLOCK: usize = 64 * 1024;
let frames = usize::try_from(disk.frames()).unwrap_or(usize::MAX);
let mut block = vec![vec![0.0f32; BLOCK]; encoders.len()];
let unread = || EngineError::Io(format!("{}: the file could not be read", disk.path().display()));
disk.with_sync(|read| {
let mut at = 0;
while at < frames {
let len = BLOCK.min(frames - at);
// Frame by frame across the channels, so the reader passes over the file once.
for i in 0..len {
for (ch, out) in block.iter_mut().enumerate() {
out[i] = read(ch, at + i).ok_or_else(unread)?;
}
}
for ((path, enc), out) in encoders.iter_mut().zip(&block) {
enc.write(std::slice::from_ref(out), len).map_err(|err| io(path, &err))?;
}
at += len;
}
Ok::<(), EngineError>(())
})?;
for (path, enc) in encoders {
let mut w = enc.finish().map_err(|err| io(&path, &err))?;
w.flush().map_err(|err| io(&path, &err))?;
w.into_inner().map_err(|err| io(&path, &err.error()))?.sync_all().map_err(|err| io(&path, &err))?;
}
Ok(())
}
Loading
Loading