Compare commits

..
58 changed files with 257 additions and 1258 deletions
+2 -2
View File
@@ -53,8 +53,8 @@ User-visible behaviour = a command in `crates/engine/src/cmd/*` (id, label, menu
Parity is measured, not guessed: `crates/engine/catalog/menus.txt` lists the incumbent's menu leaves (names only). `cargo xtask parity` writes `docs/parity-checklist.md` (generated; the hand-written assessment is `docs/target-app-parity.md`); a test enforces a floor that only rises.
## Running and looking at the app
- `cargo run --release -p soundcraft -- --demo --control 0` (demo session + control channel on a free port; the port is printed).
- Drive it: JSON lines over TCP, e.g. `{"id":1,"method":"engine.execute","params":{"command":"track.new","params":{"count":2,"format":"stereo"}}}`, then `{"id":2,"method":"ui.screenshot","params":{"path":"/tmp/shot.png"}}`. Methods: `crates/ui-egui/src/control.rs`, docs: `docs/control-protocol.md`.
- `cargo run --release -p soundcraft -- --demo --control 0` (demo session + control channel on a free port; the port and the launch's `SOUNDCRAFT_CONTROL_TOKEN` are printed, or export the token first so both sides share it).
- Drive it: JSON lines over TCP, e.g. `{"id":1,"method":"engine.execute","params":{"command":"track.new","params":{"count":2,"format":"stereo"}},"token":"…"}`, then `{"id":2,"method":"ui.screenshot","params":{"path":"/tmp/shot.png"},"token":"…"}`. Methods: `crates/ui-egui/src/control.rs`, docs: `docs/control-protocol.md`.
- Offscreen UI render (no window, no focus stealing): `cargo run -p soundcraft-ui-egui --example ui_shot -- out.png [script.jsonl]`.
- Headless: `soundcraft-cli run --demo --cmd 'mix.volume={"track":"Kick","db":-6}' --bounce out.wav`; MCP: `soundcraft-cli mcp [--connect PORT]`.
- **For UI work, look at the result** (render, read the PNG) and compare with `plan/protools/screenshots/`.
Generated
+1 -1
View File
@@ -4304,6 +4304,7 @@ dependencies = [
"craft-update",
"eframe",
"egui",
"getrandom 0.3.4",
"log",
"muda",
"objc2 0.6.5",
@@ -4374,7 +4375,6 @@ dependencies = [
"clap",
"clap_complete",
"clap_mangen",
"log",
"same-file",
"serde",
"serde_json",
+2 -1
View File
@@ -214,7 +214,8 @@ soundcraft-cli run --demo \
--cmd 'mix.insert={"track":"Bass","plugin":"eq_7band"}' \
--bounce mix.wav
# Drive the running app.
# Drive the running app (the token keeps other local accounts off the loopback port).
export SOUNDCRAFT_CONTROL_TOKEN=$(head -c 32 /dev/urandom | od -An -tx1 | tr -d ' \n')
soundcraft --demo --control 7801 &
soundcraft-cli app --port 7801 transport.play
soundcraft-cli app --port 7801 ui.screenshot '{"path":"shot.png"}'
-1
View File
@@ -25,7 +25,6 @@ soundcraft-dsp.workspace = true
serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
log.workspace = true
clap.workspace = true
same-file.workspace = true
+1 -17
View File
@@ -15,7 +15,6 @@ pub(crate) fn convert(args: &ConvertArgs, out: &mut dyn Write) -> Result<(), Cli
let bytes = std::fs::read(input).map_err(|source| CliError::File { path: input.clone(), source })?;
let ext = input.extension().and_then(std::ffi::OsStr::to_str);
let (info, mut buf) = soundcraft_audio_io::decode(&bytes, ext).map_err(|source| CliError::Audio { path: input.clone(), source })?;
let source_rate = buf.sample_rate;
if let Some(rate) = rate {
buf.channels = soundcraft_dsp::offline::resample(&buf.channels, buf.sample_rate, rate);
buf.sample_rate = rate;
@@ -31,28 +30,13 @@ pub(crate) fn convert(args: &ConvertArgs, out: &mut dyn Write) -> Result<(), Cli
let Some(format) = soundcraft_audio_io::FileFormat::from_write_extension(&out_ext) else {
return Err(ConvertError::OutputFormat { output: output.clone(), ext: out_ext }.into());
};
// A BWF source keeps its `bext` (written for WAV output only). The time reference counts
// samples, so it moves to the output rate.
let bwf = info.bwf.map(|mut b| {
b.time_reference = at_rate(b.time_reference, source_rate, buf.sample_rate);
b
});
let opts = soundcraft_audio_io::EncodeOptions { format, bit_depth, dither, bwf };
let opts = soundcraft_audio_io::EncodeOptions { format, bit_depth, dither, bwf: None };
let bytes = soundcraft_audio_io::encode(&buf, &opts).map_err(|source| CliError::Audio { path: output.clone(), source })?;
write_replacing(&args.write_path(), &bytes).map_err(|source| CliError::File { path: output.clone(), source })?;
outln!(out, "wrote {} ({} ch, {} Hz, {:.2} s)", output.display(), buf.num_channels(), buf.sample_rate, buf.duration_secs());
Ok(())
}
/// The sample position `pos` at `from` Hz as the nearest sample at `to` Hz.
fn at_rate(pos: u64, from: u32, to: u32) -> u64 {
if from == 0 || from == to {
return pos;
}
let scaled = (u128::from(pos) * u128::from(to) + u128::from(from) / 2) / u128::from(from);
u64::try_from(scaled).unwrap_or(u64::MAX)
}
/// Writes `bytes` to a new file next to `target`, flushes it and renames it over `target`, so `target`
/// (the input, for --in-place) holds either its old or its new contents. The new file is removed when
/// writing or renaming fails.
-1
View File
@@ -11,7 +11,6 @@ mod catalog;
mod convert;
mod error;
mod info;
pub mod logger;
mod mcp;
mod session;
-49
View File
@@ -1,49 +0,0 @@
//! The command line's logger. Without one, every `log::warn!` and `log::error!` in the workspace
//! was dropped: a plugin that failed to load just vanished from `engine.clap_plugins`, and nothing
//! said why. Warnings and errors now go to standard error as `warning: <message>` and
//! `error: <message>`, like the CLI's own errors; standard output keeps only the results.
//! `RUST_LOG` stays a desktop app variable (docs/cli.md), so the level is fixed.
use std::io::Write;
struct StderrLogger;
impl log::Log for StderrLogger {
fn enabled(&self, metadata: &log::Metadata<'_>) -> bool {
metadata.level() <= log::Level::Warn
}
fn log(&self, record: &log::Record<'_>) {
if self.enabled(record.metadata()) {
// A closed standard error is not worth failing a command over.
let _ = std::io::stderr().write_all(line(record.level(), &record.args().to_string()).as_bytes());
}
}
fn flush(&self) {}
}
/// One record as the CLI prints it.
fn line(level: log::Level, message: &str) -> String {
let label = if level == log::Level::Error { "error" } else { "warning" };
format!("{label}: {}\n", message.strip_suffix('\n').unwrap_or(message))
}
/// Installs the logger. Once per process: `main` calls it, not `run`, which tests call many times.
pub fn install() {
static LOGGER: StderrLogger = StderrLogger;
if log::set_logger(&LOGGER).is_ok() {
log::set_max_level(log::LevelFilter::Warn);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn records_print_like_the_cli_errors() {
assert_eq!(line(log::Level::Warn, "cannot load CLAP plugin x.clap: bad file\n"), "warning: cannot load CLAP plugin x.clap: bad file\n");
assert_eq!(line(log::Level::Error, "panic: boom"), "error: panic: boom\n");
}
}
-1
View File
@@ -3,7 +3,6 @@
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
fn main() {
soundcraft_cli::logger::install();
// `args_os`, not `args`: a non-Unicode argument must reach clap, not panic before anything is printed.
let code = soundcraft_cli::run(std::env::args_os().skip(1), &mut std::io::stdout(), &mut std::io::stderr());
// The subcommand's engine and its plugin instances are gone by now. This is process teardown,
+1 -29
View File
@@ -5,7 +5,7 @@
use std::path::{Path, PathBuf};
use std::process::Command;
use soundcraft_audio_io::{AudioBuffer, BitDepth, BwfInfo, EncodeOptions, FileFormat};
use soundcraft_audio_io::{AudioBuffer, BitDepth, EncodeOptions, FileFormat};
fn tmpdir(tag: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("sc-cli-convert-{tag}-{}", std::process::id()));
@@ -93,31 +93,3 @@ fn convert_dithers_when_reducing_depth_unless_told_not_to() {
assert!(steps16(&plain).iter().all(|s| *s == 0.0), "--no-dither still dithered");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn convert_keeps_the_bwf_time_stamp() {
let dir = tmpdir("bext");
let src = dir.join("take.wav");
// 01:00:00:00 at 48 kHz.
let bwf = BwfInfo {
description: "take 3".into(),
originator: "recorder".into(),
origination_date: "2026-11-03".into(),
origination_time: "10:00:00".into(),
time_reference: 172_800_000,
..Default::default()
};
let buf = AudioBuffer::new(48_000, 1, 4800);
let opts = EncodeOptions { format: FileFormat::Wav, bit_depth: BitDepth::Int24, dither: false, bwf: Some(bwf.clone()) };
std::fs::write(&src, soundcraft_audio_io::encode(&buf, &opts).unwrap()).unwrap();
let bext = |p: &Path| soundcraft_audio_io::probe(&std::fs::read(p).unwrap(), None).unwrap().bwf;
let out = dir.join("16.wav");
convert(&src, &out, &["--bit-depth", "16"]);
assert_eq!(bext(&out), Some(bwf.clone()));
// Resampled, the stamp still points at 01:00:00:00: 158,760,000 samples at 44.1 kHz.
let out = dir.join("44k.wav");
convert(&src, &out, &["--rate", "44100"]);
assert_eq!(bext(&out), Some(BwfInfo { time_reference: 158_760_000, ..bwf }));
let _ = std::fs::remove_dir_all(&dir);
}
-19
View File
@@ -88,25 +88,6 @@ fn assert_refused(got: &Invoke, port: u16) {
assert!(got.stderr.starts_with(&prefix) && got.stderr.contains(" (os error ") && got.stderr.ends_with(")\n"), "{}", got.stderr);
}
// ---- logging ----
/// Warnings SoundCraft logs (a plugin that fails to load, a backup it can't write) go to standard
/// error. Without a logger they were dropped.
#[test]
fn logged_warnings_reach_standard_error() {
let dir = sandbox("log");
let first = invoke(&dir, &["run", "--save", "s.scraft"]);
assert_eq!(first.code, 0, "{}", first.stderr);
// A file where the backups folder should go: the next save that changes the session can't
// back the old one up.
std::fs::write(dir.join("Backups"), b"not a folder").unwrap();
let again = invoke(&dir, &["run", "--in", "s.scraft", "--cmd", r#"track.new={"count":1}"#, "--save", "s.scraft", "--in-place"]);
assert_eq!(again.code, 0, "{}", again.stderr);
assert!(again.stderr.contains("warning: no backup of"), "{}", again.stderr);
assert!(!again.stdout.contains("no backup"), "results only on standard output: {}", again.stdout);
let _ = std::fs::remove_dir_all(&dir);
}
// ---- unknown flags ----
#[test]
+2
View File
@@ -26,6 +26,8 @@ egui = { workspace = true, features = ["default_fonts", "monochrome_emoji_fonts"
serde_json.workspace = true
log.workspace = true
chrono.workspace = true
# The per-launch control token (already in the graph through egui's ahash).
getrandom = "0.3"
[target.'cfg(all(unix, not(any(target_os = "macos", target_os = "linux"))))'.dependencies]
winit = { version = "0.30", default-features = false, features = ["x11"] }
+73 -9
View File
@@ -3,8 +3,13 @@
//!
//! Lines are bounded ([`MAX_LINE`]): a longer one is refused and the connection closed, without
//! buffering the rest. So is the number of connections served at once ([`MAX_CONNECTIONS`]).
//!
//! Every request carries the launch's secret as `"token"` (`SOUNDCRAFT_CONTROL_TOKEN`, or one made
//! here and printed after the port): the port is loopback, which any local account can reach, and
//! a request acts with this process's authority.
use serde_json::{Value, json};
use soundcraft_automation::CONTROL_TOKEN_VAR;
use soundcraft_ui_egui::ControlRequest;
use std::io::{BufRead, BufReader, Read, Write};
use std::net::{TcpListener, TcpStream};
@@ -28,9 +33,20 @@ const MAX_CONNECTIONS: usize = 16;
const MAX_QUEUED_REQUESTS: usize = MAX_CONNECTIONS;
/// Bind the loopback control listener and return the request receiver. Port 0 picks a free one and the actual port is
/// printed; bind or spawn failures are logged instead of panicking.
pub fn start(port: u16, ctx: egui::Context) -> Receiver<ControlRequest> {
/// printed; bind or spawn failures are logged instead of panicking. Requests must carry `token`; with `None` one is
/// made for this launch and printed after the port, so only the launching process learns it.
pub fn start(port: u16, token: Option<String>, ctx: egui::Context) -> Receiver<ControlRequest> {
let (tx, rx) = sync_channel::<ControlRequest>(MAX_QUEUED_REQUESTS);
let (token, made) = match token {
Some(t) => (t, false),
None => match new_token() {
Ok(t) => (t, true),
Err(e) => {
log::error!("control server not started: no {CONTROL_TOKEN_VAR} and no random token: {e}");
return rx;
}
},
};
let listener = match TcpListener::bind(("127.0.0.1", port)) {
Ok(l) => l,
Err(e) => {
@@ -41,13 +57,29 @@ pub fn start(port: u16, ctx: egui::Context) -> Receiver<ControlRequest> {
let actual = listener.local_addr().map(|a| a.port()).unwrap_or(port);
log::info!("control server listening on 127.0.0.1:{actual}");
println!("SOUNDCRAFT_CONTROL_PORT={actual}");
let spawned = std::thread::Builder::new().name("soundcraft-control".into()).spawn(move || accept_loop(listener, tx, ctx));
if made {
println!("{CONTROL_TOKEN_VAR}={token}");
}
let spawned = std::thread::Builder::new().name("soundcraft-control".into()).spawn(move || accept_loop(listener, tx, ctx, Arc::from(token)));
if let Err(e) = spawned {
log::error!("control thread failed: {e}");
}
rx
}
/// 32 random bytes as hex, from the OS.
fn new_token() -> Result<String, getrandom::Error> {
let mut bytes = [0u8; 32];
getrandom::fill(&mut bytes)?;
Ok(bytes.iter().map(|b| format!("{b:02x}")).collect())
}
/// `token` equals `expected`, compared in time that doesn't depend on where they differ.
fn token_matches(token: Option<&str>, expected: &str) -> bool {
let Some(token) = token else { return false };
token.len() == expected.len() && token.bytes().zip(expected.bytes()).fold(0u8, |acc, (a, b)| acc | (a ^ b)) == 0
}
/// A connection's place among [`MAX_CONNECTIONS`], given back when the connection ends (also when
/// its thread can't be started, or panics).
struct Slot(Arc<AtomicUsize>);
@@ -60,7 +92,7 @@ impl Drop for Slot {
/// Accept until the listener closes, admitting at most [`MAX_CONNECTIONS`] peers; excess peers get one error line.
/// Each admitted connection is served on its own thread, releasing its slot on exit.
fn accept_loop(listener: TcpListener, tx: SyncSender<ControlRequest>, ctx: egui::Context) {
fn accept_loop(listener: TcpListener, tx: SyncSender<ControlRequest>, ctx: egui::Context, token: Arc<str>) {
let open = Arc::new(AtomicUsize::new(0));
for mut stream in listener.incoming().flatten() {
if open.fetch_add(1, Ordering::SeqCst) >= MAX_CONNECTIONS {
@@ -71,9 +103,10 @@ fn accept_loop(listener: TcpListener, tx: SyncSender<ControlRequest>, ctx: egui:
let slot = Slot(Arc::clone(&open));
let tx = tx.clone();
let ctx = ctx.clone();
let token = Arc::clone(&token);
let _ = std::thread::Builder::new().name("soundcraft-control-conn".into()).spawn(move || {
let _slot = slot;
serve(stream, tx, ctx);
serve(stream, tx, ctx, &token);
});
}
}
@@ -103,8 +136,9 @@ fn read_bounded_line(reader: &mut impl BufRead, buf: &mut Vec<u8>, max: usize) -
}
/// Answer newline-delimited JSON requests for one connection: bound each line, forward it to the UI thread with a 90
/// s reply deadline, and stop at EOF, invalid UTF-8 or a closed request queue. Bad JSON still gets an error reply.
fn serve(stream: TcpStream, tx: SyncSender<ControlRequest>, ctx: egui::Context) {
/// s reply deadline, and stop at EOF, invalid UTF-8, a closed request queue or a wrong `token`. Bad JSON still gets
/// an error reply.
fn serve(stream: TcpStream, tx: SyncSender<ControlRequest>, ctx: egui::Context, token: &str) {
let Ok(read) = stream.try_clone() else { return };
let mut out = stream;
let mut reader = BufReader::new(read);
@@ -126,6 +160,11 @@ fn serve(stream: TcpStream, tx: SyncSender<ControlRequest>, ctx: egui::Context)
let reply = match serde_json::from_str::<Value>(line) {
Ok(msg) => {
let id = msg.get("id").cloned().unwrap_or(Value::Null);
if !token_matches(msg.get("token").and_then(Value::as_str), token) {
let error = format!("unauthorized: the request's `token` is not this app's {CONTROL_TOKEN_VAR}");
let _ = writeln!(out, "{}", json!({"ok": false, "id": id, "error": error}));
break;
}
let method = msg.get("method").and_then(Value::as_str).unwrap_or("").to_string();
let params = msg.get("params").cloned().unwrap_or(json!({}));
let (req, rrx) = ControlRequest::new(method, params);
@@ -185,12 +224,14 @@ mod tests {
assert!(state + request.to_string().len() <= MAX_LINE);
}
const TOKEN: &str = "s3cret";
/// The control server on a loopback port, with a stand-in UI that echoes each method.
fn serve_echo() -> std::net::SocketAddr {
let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap();
let addr = listener.local_addr().unwrap();
let (tx, rx) = sync_channel::<ControlRequest>(MAX_QUEUED_REQUESTS);
std::thread::spawn(move || accept_loop(listener, tx, egui::Context::default()));
std::thread::spawn(move || accept_loop(listener, tx, egui::Context::default(), Arc::from(TOKEN)));
std::thread::spawn(move || {
for req in rx {
let _ = req.reply.send(json!({"ok": true, "method": req.method}));
@@ -204,7 +245,7 @@ mod tests {
let addr = serve_echo();
let mut c = TcpStream::connect(addr).unwrap();
c.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
c.write_all(b"{\"id\":3,\"method\":\"ping\"}\r\n\n{\"id\":4,\"method\":\"pong\"}\n").unwrap();
c.write_all(b"{\"id\":3,\"method\":\"ping\",\"token\":\"s3cret\"}\r\n\n{\"id\":4,\"method\":\"pong\",\"token\":\"s3cret\"}\n").unwrap();
let mut r = BufReader::new(c);
for (id, method) in [(3, "ping"), (4, "pong")] {
let mut line = String::new();
@@ -214,6 +255,29 @@ mod tests {
}
}
#[test]
fn a_request_without_the_token_is_refused_and_the_connection_closed() {
let addr = serve_echo();
for request in [r#"{"id":1,"method":"ping"}"#, r#"{"id":1,"method":"ping","token":"s3cre"}"#, r#"{"id":1,"method":"ping","token":"S3CRET"}"#]
{
let mut c = TcpStream::connect(addr).unwrap();
c.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
writeln!(c, "{request}").unwrap();
let mut r = BufReader::new(c);
let mut line = String::new();
r.read_line(&mut line).unwrap();
let v: Value = serde_json::from_str(&line).unwrap();
assert_eq!((v["id"].as_i64(), v["ok"].as_bool()), (Some(1), Some(false)), "{line}");
assert!(v["error"].as_str().unwrap_or("").starts_with("unauthorized"), "{line}");
assert!(v.get("method").is_none(), "the request was not forwarded: {line}");
line.clear();
assert_eq!(r.read_line(&mut line).unwrap(), 0, "the connection is closed after a refusal");
}
assert!(token_matches(Some(TOKEN), TOKEN));
assert!(!token_matches(None, TOKEN));
assert_eq!(new_token().unwrap().len(), 64);
}
#[test]
fn extra_connections_are_refused() {
let addr = serve_echo();
+1 -7
View File
@@ -166,13 +166,7 @@ pub fn rotate(dir: &Path) -> std::io::Result<(PathBuf, File)> {
/// One formatted log line: `<timestamp> <LEVEL> [<thread>] <target>: <message>\n`.
pub fn format_line(ts: &str, level: log::Level, thread: &str, target: &str, message: &str) -> String {
let message = message.strip_suffix('\n').unwrap_or(message);
// Control characters are written escaped: a message quoting a session file (a plugin id, a
// path) must not start a line of its own in the log or steer the terminal.
let mut escaped = String::with_capacity(message.len());
for c in message.chars() {
if c.is_control() { escaped.extend(c.escape_default()) } else { escaped.push(c) }
}
format!("{ts} {level:<5} [{thread}] {target}: {escaped}\n")
format!("{ts} {level:<5} [{thread}] {target}: {message}\n")
}
/// Where formatted lines go besides standard error: kept in memory until a file is attached,
+2 -9
View File
@@ -158,15 +158,8 @@ fn a_line_carries_time_level_thread_target_and_message() {
format_line("2026-10-08T07:59:17.728Z", log::Level::Warn, "main", "soundcraft_mix", "eq_7band: the plugin did not accept its stored state"),
"2026-10-08T07:59:17.728Z WARN [main] soundcraft_mix: eq_7band: the plugin did not accept its stored state\n"
);
// A message with breaks of its own stays one record; the record still ends in exactly one newline.
assert!(
format_line("t", log::Level::Error, "w", "x", "a\nb\n").ends_with("x: a\\nb\n"),
"a break inside the message is escaped, the final one dropped"
);
assert!(
format_line("t", log::Level::Error, "w", "x", "plugin \u{1b}[2J\r\n2026 INFO forged")
.ends_with("x: plugin \\u{1b}[2J\\r\\n2026 INFO forged\n")
);
// A multi-line message keeps its lines; the record still ends in exactly one newline.
assert!(format_line("t", log::Level::Error, "w", "x", "a\nb\n").ends_with("x: a\nb\n"));
}
#[test]
+4 -3
View File
@@ -3,8 +3,9 @@
//! Usage: `soundcraft [--demo] [--control <port>] [--no-audio] [session.scraft | audio files…]`; `soundcraft --help` lists the flags and `SOUNDCRAFT_CONTROL_PORT`.
//!
//! `--control <port>` (or `SOUNDCRAFT_CONTROL_PORT`) starts a localhost JSON-lines control server
//! (`0` picks a free port and prints it): `{"id":1,"method":"ui.inspect","params":{}}` →
//! `{"id":1,"ok":true,"result":…}`. See `soundcraft_ui_egui::control` for the methods.
//! (`0` picks a free port and prints it): `{"id":1,"method":"ui.inspect","params":{},"token":"…"}` →
//! `{"id":1,"ok":true,"result":…}`, where `token` is `SOUNDCRAFT_CONTROL_TOKEN` (or the one made for
//! this launch, printed after the port). See `soundcraft_ui_egui::control` for the methods.
//!
//! `log` records go to standard error and `<settings dir>/logs/soundcraft.log` (`RUST_LOG` sets the
//! levels); see [`logging`].
@@ -260,7 +261,7 @@ fn main() -> eframe::Result {
app.extra.ui_scale = prefs.ui_scale;
app.ui = prefs;
if let Some(port) = control_port {
let rx = control_server::start(port, cc.egui_ctx.clone());
let rx = control_server::start(port, soundcraft_automation::control_token(), cc.egui_ctx.clone());
app = app.with_control(rx);
}
for f in &files {
+16 -3
View File
@@ -151,18 +151,31 @@ impl RemoteError {
type Connection = (TcpStream, BufReader<TcpStream>);
/// The variable carrying the control channel's secret: the app refuses every request whose
/// `token` differs from it, so another local account can't drive the app through the loopback
/// port. The app makes one per launch when the variable is unset (printed after the port line).
pub const CONTROL_TOKEN_VAR: &str = "SOUNDCRAFT_CONTROL_TOKEN";
/// [`CONTROL_TOKEN_VAR`] from the environment; a blank value counts as unset.
pub fn control_token() -> Option<String> {
std::env::var(CONTROL_TOKEN_VAR).ok().map(|t| t.trim().to_owned()).filter(|t| !t.is_empty())
}
/// A TCP client for a running app's control channel.
pub struct Remote {
/// The target as `host:port`, for connecting and for messages.
addr: String,
/// Sent with every request; without one the app refuses, naming the variable.
token: Option<String>,
conn: Option<Connection>,
next_id: u64,
}
impl Remote {
/// A client for the app at `target`; it connects on the first request.
/// A client for the app at `target`, authenticating with [`control_token`]; it connects on the
/// first request.
pub fn new(target: &ControlTarget) -> Self {
Remote { addr: target.to_string(), conn: None, next_id: 1 }
Remote { addr: target.to_string(), token: control_token(), conn: None, next_id: 1 }
}
/// The open connection, connecting first when there is none.
@@ -178,7 +191,7 @@ impl Remote {
fn roundtrip(&mut self, method: &str, params: &Value) -> Result<Value, (RemoteError, bool)> {
let id = self.next_id;
self.next_id += 1;
let line = json!({"id": id, "method": method, "params": params}).to_string();
let line = json!({"id": id, "method": method, "params": params, "token": self.token}).to_string();
let (w, r) = self.connection().map_err(|e| (e, false))?;
writeln!(w, "{line}").map_err(|e| (e.into(), false))?;
let mut buf = String::new();
-1
View File
@@ -27,7 +27,6 @@ use clap_sys::ext::note_ports::{CLAP_EXT_NOTE_PORTS, clap_note_port_info, clap_p
use clap_sys::ext::params::{
CLAP_EXT_PARAMS, clap_host_params, clap_param_clear_flags, clap_param_info, clap_param_rescan_flags, clap_plugin_params,
};
use clap_sys::ext::render::{CLAP_EXT_RENDER, CLAP_RENDER_OFFLINE, CLAP_RENDER_REALTIME, clap_plugin_render};
use clap_sys::ext::state::{CLAP_EXT_STATE, clap_host_state, clap_plugin_state};
use clap_sys::factory::plugin_factory::{CLAP_PLUGIN_FACTORY_ID, clap_plugin_factory};
use clap_sys::fixedpoint::{CLAP_BEATTIME_FACTOR, CLAP_SECTIME_FACTOR};
+2 -15
View File
@@ -7,7 +7,6 @@ use super::{
clap_host, clap_plugin_audio_ports, clap_plugin_entry, clap_plugin_factory, clap_plugin_gui, clap_plugin_latency, clap_plugin_note_ports,
clap_plugin_params, clap_plugin_state, clap_version_is_compatible, read_cstr, scan,
};
use super::{CLAP_EXT_RENDER, clap_plugin_render};
// ---- loaded binaries -----------------------------------------------------------------------
@@ -43,16 +42,6 @@ fn loaded() -> &'static Mutex<HashMap<PathBuf, Arc<Bundle>>> {
L.get_or_init(|| Mutex::new(HashMap::new()))
}
/// A libloading error with the system's reason. libloading's own text is only "dlopen failed"
/// (or "dlsym failed", "LoadLibraryExW failed"); the reason (`dlerror`, the Windows error) is its
/// source.
fn with_reason(e: &libloading::Error) -> String {
match std::error::Error::source(e) {
Some(why) => format!("{e}: {why}"),
None => e.to_string(),
}
}
impl Bundle {
/// Loads (once per path, then cached for the life of the process) and initialises a `.clap`.
pub fn load(path: &Path) -> Result<Arc<Bundle>, ClapError> {
@@ -70,12 +59,12 @@ impl Bundle {
let exe = scan::bundle_executable(path).ok_or_else(|| err("no loadable binary".into()))?;
// SAFETY: loading a shared library runs its static initialisers. That is inherent to
// hosting plugins; we only load files found in CLAP plugin folders or named explicitly.
let lib = unsafe { libloading::Library::new(&exe) }.map_err(|e| err(with_reason(&e)))?;
let lib = unsafe { libloading::Library::new(&exe) }.map_err(|e| err(e.to_string()))?;
// SAFETY: `clap_entry` is the CLAP-mandated exported data symbol of type
// `const clap_plugin_entry`; the symbol's address is therefore a `*const clap_plugin_entry`.
let entry: *const clap_plugin_entry = match unsafe { lib.get::<*const clap_plugin_entry>(b"clap_entry\0") } {
Ok(sym) => *sym,
Err(e) => return Err(err(format!("not a CLAP plugin (no clap_entry): {}", with_reason(&e)))),
Err(e) => return Err(err(format!("not a CLAP plugin: {e}"))),
};
if entry.is_null() {
return Err(err("null clap_entry".into()));
@@ -189,7 +178,6 @@ impl Bundle {
latency: std::ptr::null(),
notes: std::ptr::null(),
state_ext: std::ptr::null(),
render: std::ptr::null(),
gui: Arc::new(GuiLink::default()),
raw: Vec::with_capacity(EVENT_CAP),
out_params: Vec::with_capacity(EVENT_CAP),
@@ -217,7 +205,6 @@ impl Bundle {
inst.latency = inst.extension::<clap_plugin_latency>(CLAP_EXT_LATENCY);
inst.notes = inst.extension::<clap_plugin_note_ports>(CLAP_EXT_NOTE_PORTS);
inst.state_ext = inst.extension::<clap_plugin_state>(CLAP_EXT_STATE);
inst.render = inst.extension::<clap_plugin_render>(CLAP_EXT_RENDER);
let gui = inst.extension::<clap_plugin_gui>(CLAP_EXT_GUI);
inst.gui = Arc::new(GuiLink {
inner: Mutex::new(GuiInner { plugin, gui, state: inst.state_ext, alive: true, created: false, visible: false, window: None }),
-12
View File
@@ -8,7 +8,6 @@ use super::{
clap_plugin, clap_plugin_audio_ports, clap_plugin_latency, clap_plugin_note_ports, clap_plugin_params, clap_plugin_state, clap_process,
read_name,
};
use super::{CLAP_RENDER_OFFLINE, CLAP_RENDER_REALTIME, clap_plugin_render};
// ---- audio buffers -------------------------------------------------------------------------
@@ -100,7 +99,6 @@ pub(crate) struct Instance {
pub(super) latency: *const clap_plugin_latency,
pub(super) notes: *const clap_plugin_note_ports,
pub(super) state_ext: *const clap_plugin_state,
pub(super) render: *const clap_plugin_render,
pub(super) gui: Arc<GuiLink>,
pub(super) raw: Vec<RawEvent>,
/// Parameter values the plugin reported through output events in the current `process`.
@@ -160,16 +158,6 @@ impl Instance {
unsafe { load(self.plugin, &stream) }
}
/// `clap_plugin_render.set`: offline (no realtime pressure) or realtime. `false` without the
/// extension or when the plugin can't switch.
pub fn set_render_mode(&self, offline: bool) -> bool {
// SAFETY: the extension pointer, when non-null, points to a static vtable in the plugin.
let Some(set) = (unsafe { self.render.as_ref() }).and_then(|r| r.set) else { return false };
let mode = if offline { CLAP_RENDER_OFFLINE } else { CLAP_RENDER_REALTIME };
// SAFETY: valid instance, called from the thread that owns it.
unsafe { set(self.plugin, mode) }
}
/// Runs `on_main_thread` if the plugin asked for a callback.
pub fn service_callback(&mut self) {
if self.state.callback.swap(false, Ordering::Relaxed)
-13
View File
@@ -262,17 +262,4 @@ mod tests {
assert!(instantiate("clap:does.not.exist").is_err());
let _ = std::fs::remove_dir_all(&d);
}
/// A binary the system refuses to load reports the system's reason (missing library, wrong
/// glibc, not an ELF file), not just "dlopen failed".
#[test]
fn a_binary_that_does_not_load_says_why() {
let d = fresh_private_dir("soundcraft-clap-reason");
let f = d.join("garbage.clap");
std::fs::write(&f, b"\x7fELF this is not a plugin \x00\x01\x02").unwrap();
let msg = load_bundle(&f).err().map(|e| e.to_string()).unwrap_or_default();
let reason = msg.split_once("failed: ").map_or("", |(_, r)| r.trim());
assert!(!reason.is_empty(), "no reason in: {msg}");
let _ = std::fs::remove_dir_all(&d);
}
}
-5
View File
@@ -178,11 +178,6 @@ impl Plugin for ClapPlugin {
}
}
fn set_offline(&mut self, offline: bool) {
// `clap.render`: plugins without it render the same way either way.
self.inst.set_render_mode(offline);
}
fn reset(&mut self) {
self.inst.reset();
self.queue.retain(|e| matches!(e, Event::Param { .. }));
+5 -23
View File
@@ -9,8 +9,7 @@
//! - `org.soundcraft.test.wide16`, `.wide17ports`, `.wide65ch`: effects advertising wide layouts
//! (16 ports of 64 channels, 17 ports of 2 channels, one port of 65 channels per direction).
//! `process` writes 1.0 into every output channel it was given when the host's buffers match the
//! advertised layout (2.0 once the host set the offline render mode through `clap.render`), -1.0
//! when they do not.
//! advertised layout, -1.0 when they do not.
//!
//! The gain plugin also implements `clap.state` (gain and mode as two little-endian f64s) and a
//! floating-only `clap.gui` without a real window: showing it "edits" the gain to 0.25 in the GUI,
@@ -28,7 +27,6 @@ use clap_sys::ext::latency::*;
use clap_sys::ext::log::*;
use clap_sys::ext::note_ports::*;
use clap_sys::ext::params::*;
use clap_sys::ext::render::*;
use clap_sys::factory::plugin_factory::*;
use clap_sys::host::clap_host;
use clap_sys::id::clap_id;
@@ -104,8 +102,6 @@ struct Fx {
gui_created: bool,
gui_visible: bool,
gui_edit_pending: bool,
/// `clap.render` set the offline mode (wide fixtures only).
offline: bool,
}
unsafe fn fx<'a>(p: *const clap_plugin) -> &'a mut Fx {
@@ -193,8 +189,8 @@ fn apply(f: &mut Fx, h: &clap_event_header) {
}
}
/// Fill every output channel with `fit` when all ports have the wide layout, else -1.0.
unsafe fn fill_wide(pr: &clap_process, ports: u32, channels: u32, n: usize, fit: f32) {
/// Fill every output channel with 1.0 when all ports have the wide layout, else -1.0.
unsafe fn fill_wide(pr: &clap_process, ports: u32, channels: u32, n: usize) {
let fits = |bufs: *const clap_audio_buffer, n: u32| {
n == ports && (0..n as usize).all(|i| unsafe { &*bufs.add(i) }.channel_count == channels)
};
@@ -204,7 +200,7 @@ unsafe fn fill_wide(pr: &clap_process, ports: u32, channels: u32, n: usize, fit:
for c in 0..out.channel_count as usize {
let o = unsafe { *out.data32.add(c) };
for i in 0..n {
unsafe { *o.add(i) = if ok { fit } else { -1.0 } };
unsafe { *o.add(i) = if ok { 1.0 } else { -1.0 } };
}
}
}
@@ -256,7 +252,7 @@ unsafe extern "C" fn process(p: *const clap_plugin, pr: *const clap_process) ->
let ev = unsafe { &*pr.in_events };
let count = unsafe { (ev.size.unwrap())(pr.in_events) };
if let Some((ports, channels)) = f.wide {
unsafe { fill_wide(pr, ports, channels, n, if f.offline { 2.0 } else { 1.0 }) };
unsafe { fill_wide(pr, ports, channels, n) };
return CLAP_PROCESS_CONTINUE;
}
if pr.audio_outputs_count != 1 {
@@ -547,17 +543,6 @@ static GUI: clap_plugin_gui = clap_plugin_gui {
hide: Some(gui_hide),
};
unsafe extern "C" fn render_hard_realtime(_p: *const clap_plugin) -> bool {
false
}
unsafe extern "C" fn render_set(p: *const clap_plugin, mode: clap_plugin_render_mode) -> bool {
unsafe { fx(p) }.offline = mode == CLAP_RENDER_OFFLINE;
true
}
static RENDER: clap_plugin_render = clap_plugin_render { has_hard_realtime_requirement: Some(render_hard_realtime), set: Some(render_set) };
unsafe extern "C" fn get_extension(p: *const clap_plugin, id: *const c_char) -> *const c_void {
let id = unsafe { CStr::from_ptr(id) };
let synth = unsafe { fx(p) }.synth;
@@ -573,8 +558,6 @@ unsafe extern "C" fn get_extension(p: *const clap_plugin, id: *const c_char) ->
&STATE as *const _ as *const c_void
} else if id == CLAP_EXT_GUI && !synth {
&GUI as *const _ as *const c_void
} else if id == CLAP_EXT_RENDER && unsafe { fx(p) }.wide.is_some() {
&RENDER as *const _ as *const c_void
} else {
std::ptr::null()
}
@@ -640,7 +623,6 @@ unsafe extern "C" fn factory_create(_f: *const clap_plugin_factory, host: *const
gui_created: false,
gui_visible: false,
gui_edit_pending: false,
offline: false,
});
let raw = Box::into_raw(b);
unsafe {
-13
View File
@@ -130,19 +130,6 @@ fn layouts_beyond_the_host_limits_are_rejected_not_truncated() {
}
}
#[test]
fn offline_renders_set_the_render_mode() {
fixture_dir();
for (offline, want) in [(false, 1.0), (true, 2.0)] {
let mut p = soundcraft_clap_host::create("clap:org.soundcraft.test.wide16").unwrap();
p.set_offline(offline);
p.prepare(44_100.0, 64, 2);
let mut io = vec![vec![0.5f32; 64]; 2];
p.process(&mut io, 64);
assert!(io.iter().flatten().all(|&x| x == want), "offline {offline}: {:?}", &io[0][..4]);
}
}
#[test]
fn many_instances_create_and_drop_cleanly() {
fixture_dir();
-5
View File
@@ -195,11 +195,6 @@ pub struct Transport {
pub trait Plugin: Send {
fn info(&self) -> &'static PluginInfo;
fn prepare(&mut self, sample_rate: f32, max_block: usize, channels: usize);
/// The plugin renders offline (a bounce: faster or slower than realtime, no deadline) rather
/// than in realtime. Hosted plugins pass it on (VST3 `processMode`, CLAP `clap.render`): some
/// use better algorithms then, or wait for what they load instead of playing on without it.
/// Called before `prepare`; plugins are realtime until told otherwise.
fn set_offline(&mut self, _offline: bool) {}
fn reset(&mut self);
/// Sets a parameter (clamped into range; NaN becomes the default). `false` if `id` is unknown.
fn set_param(&mut self, id: &str, value: f32) -> bool;
+1 -15
View File
@@ -14,7 +14,7 @@ pub fn specs() -> Vec<CommandSpec> {
"AudioSuite Process",
[],
None,
"{process: <built-in plugin id (engine.plugins)> | normalize | reverse | invert | gain | duplicate | time_stretch | pitch_shift | varispeed | speed_ramp | signal_generator, params?: {..}, clips?, tracks?, start?, end?} — signal_generator fills start..end (default: the selection) with a new clip on each audio track in `tracks` (default: the selected tracks); speed_ramp takes change (0 slow down, 1 speed up), selection (0 fit to, 1 extend) and fades (1 on, 0 off)",
"{process: <plugin id> | normalize | reverse | invert | gain | duplicate | time_stretch | pitch_shift | varispeed | speed_ramp | signal_generator, params?: {..}, clips?, tracks?, start?, end?} — signal_generator fills start..end (default: the selection) with a new clip on each audio track in `tracks` (default: the selected tracks); speed_ramp takes change (0 slow down, 1 speed up), selection (0 fit to, 1 extend) and fades (1 on, 0 off)",
has_selection,
process
)]
@@ -55,9 +55,6 @@ fn process(e: &mut Engine, p: &Value) -> Result<Value> {
| "speed_ramp"
| "dc_offset_removal"
);
if !known && soundcraft_mix::is_third_party(&name) {
return Err(bad("audiosuite.process", format!("`{name}` is a hosted plugin; AudioSuite runs built-in plugins only (engine.plugins)")));
}
if !known {
return Err(bad("audiosuite.process", format!("unknown process `{name}`")));
}
@@ -231,17 +228,6 @@ mod tests {
(crate::Engine::new(s), id)
}
#[test]
fn hosted_plugin_ids_are_refused_with_the_reason() {
let (mut e, id) = gained_clip();
for plugin in ["clap:com.example.reverb", "vst3:00112233445566778899AABBCCDDEEFF", "au:aufx:dely:appl"] {
let err = e.execute("audiosuite.process", &json!({"process": plugin, "clips": [id.0]})).unwrap_err().to_string();
assert!(err.contains("built-in"), "{plugin}: {err}");
}
let spec = specs().into_iter().find(|s| s.id == "audiosuite.process").unwrap();
assert!(spec.params.contains("<built-in plugin id"), "the help says which ids it takes: {}", spec.params);
}
#[test]
fn invert_and_duplicate_keep_clip_gain() {
let (mut e, id) = gained_clip();
+15 -51
View File
@@ -598,7 +598,6 @@ fn write_current(e: &mut Engine, p: &Value, all: bool) -> Result<Value> {
}
/// Clip-gain envelope value in dB. An empty envelope contributes nothing, and the last point holds.
#[cfg(test)]
fn env_db(env: &[(i64, f32)], at: i64) -> f32 {
let mut prev: Option<(i64, f32)> = None;
for &(t, v) in env {
@@ -613,38 +612,6 @@ fn env_db(env: &[(i64, f32)], at: i64) -> f32 {
prev.map_or(0.0, |(_, v)| v)
}
/// Repeated lookups in one clip-gain envelope, each O(log n) instead of a walk from its start.
/// Gives exactly what walking the points in stored order gives, sorted or not: the first point
/// at or after `at` is the first whose running maximum position reaches `at`.
struct EnvLookup<'a> {
env: &'a [(i64, f32)],
/// Largest position among `env[..=i]`.
reach: Vec<i64>,
}
impl<'a> EnvLookup<'a> {
fn new(env: &'a [(i64, f32)]) -> Self {
let reach = env
.iter()
.scan(i64::MIN, |m, &(t, _)| {
*m = (*m).max(t);
Some(*m)
})
.collect();
EnvLookup { env, reach }
}
/// Value in dB at `at` (see `env_db`).
fn db(&self, at: i64) -> f32 {
let i = self.reach.partition_point(|&m| m < at);
let Some(&(t, v)) = self.env.get(i) else { return self.env.last().map_or(0.0, |&(_, v)| v) };
match i.checked_sub(1).and_then(|p| self.env.get(p)) {
Some(&(pt, pv)) if t > pt => pv + (v - pv) * ((at - pt) as f32 / (t - pt) as f32),
_ => v,
}
}
}
/// Fold the volume `lane` into the gain envelope of clip `c` over the selection `r`.
fn bake_volume_into_clip(c: &mut soundcraft_model::Clip, lane: &soundcraft_model::AutomationLane, base: f32, r: soundcraft_time::Range) {
let delta = |at: i64| lane.value_at(at, base) - base;
@@ -654,41 +621,39 @@ fn bake_volume_into_clip(c: &mut soundcraft_model::Clip, lane: &soundcraft_model
return;
}
let rel = |at: i64| at - c.start;
let old_env = std::mem::take(&mut c.gain_env);
let env = EnvLookup::new(&old_env);
let env_db = |at: i64| env.db(at);
let rel0 = rel(sel0);
let rel1 = rel(sel1);
// Later writes win, so the selection anchors replace any point they land on.
let mut pts: Vec<(i64, f32)> = old_env.iter().copied().filter(|&(at, _)| at < rel0.saturating_sub(1) || (sel1 < c.end() && at > rel1)).collect();
let mut pts: Vec<(i64, f32)> =
c.gain_env.iter().copied().filter(|&(at, _)| at < rel0.saturating_sub(1) || (sel1 < c.end() && at > rel1)).collect();
if rel0 > 0 {
pts.push((rel0 - 1, env_db(rel0 - 1)));
pts.push((rel0 - 1, env_db(&c.gain_env, rel0 - 1)));
}
pts.push((rel0, env_db(rel0) + delta(sel0)));
pts.push((rel0, env_db(&c.gain_env, rel0) + delta(sel0)));
for pt in lane.points.iter().filter(|pt| pt.at > sel0 && pt.at < sel1) {
let at = rel(pt.at);
pts.push((at, env_db(at) + delta(pt.at)));
pts.push((at, env_db(&c.gain_env, at) + delta(pt.at)));
}
// The clip's own gain points inside the selection keep their shape, offset by the lane.
for &(at, _) in old_env.iter().filter(|&&(at, _)| at > rel0 && at < rel1) {
pts.push((at, env_db(at) + delta(c.start.saturating_add(at))));
for &(at, _) in c.gain_env.iter().filter(|&&(at, _)| at > rel0 && at < rel1) {
pts.push((at, env_db(&c.gain_env, at) + delta(c.start.saturating_add(at))));
}
if sel1 - 1 > sel0 {
let at = rel(sel1 - 1);
pts.push((at, env_db(at) + delta(sel1 - 1)));
pts.push((at, env_db(&c.gain_env, at) + delta(sel1 - 1)));
}
if sel1 < c.end() {
pts.push((rel1, env_db(rel1)));
pts.push((rel1, env_db(&c.gain_env, rel1)));
}
pts.sort_by_key(|p| p.0);
let mut baked = Vec::new();
let mut env = Vec::new();
for p in pts {
if baked.last().is_some_and(|q: &(i64, f32)| q.0 == p.0) {
baked.pop();
if env.last().is_some_and(|q: &(i64, f32)| q.0 == p.0) {
env.pop();
}
baked.push(p);
env.push(p);
}
c.gain_env = baked;
c.gain_env = env;
}
/// Clear the volume lane over `r` when the clips all sit inside it, else hold the fader there.
@@ -761,9 +726,8 @@ fn clip_gain_to_vol(e: &mut Engine, p: &Value) -> Result<Value> {
if let Some(l) = &old {
ats.extend(l.points.iter().map(|pt| pt.at).filter(|&at| at > c.start && at < c.end() - 1));
}
let env = EnvLookup::new(&c.gain_env);
for at in ats {
pts.push((at, level(at) + c.gain_db + env.db(at - c.start)));
pts.push((at, level(at) + c.gain_db + env_db(&c.gain_env, at - c.start)));
}
c.gain_db = 0.0;
c.gain_env.clear();
-33
View File
@@ -345,36 +345,3 @@ fn insert_and_send_reject_invalid_slot() {
let r = e.execute("mix.send", &json!({"track": "Gtr", "bus": "Verb"})).unwrap();
assert_eq!(r["slot"], 0);
}
#[test]
fn indexed_envelope_lookup_matches_the_walk() {
let envs: [Vec<(i64, f32)>; 5] = [
Vec::new(),
vec![(100, -3.0)],
vec![(0, 0.0), (100, -6.0), (100, 3.0), (250, -12.0), (900, 1.0)],
// Out of order, as a hand-edited session file may hold.
vec![(500, -1.0), (100, -6.0), (700, 4.0), (300, 2.0), (700, -9.0), (50, 0.5)],
(0..200).map(|i| ((i * 37) % 1000, (i as f32 * 0.7).sin() * 12.0)).collect(),
];
for env in &envs {
let lookup = EnvLookup::new(env);
for at in -20..1100 {
assert_eq!(lookup.db(at).to_bits(), env_db(env, at).to_bits(), "{env:?} at {at}");
}
}
}
#[test]
fn clip_gain_to_volume_with_a_large_envelope_stays_fast() {
let (mut e, t) = tone_session(&[]);
let clip = e.session().track(t).unwrap().clips()[0].id;
let n = 200_000i64;
let c = e.session_mut().find_clip_mut(clip).unwrap();
c.length = n * 5 + 10;
c.gain_env = (0..n).map(|i| (i * 5, if i % 2 == 0 { -6.0 } else { 0.0 })).collect();
let start = std::time::Instant::now();
e.execute("automation.clip_gain_to_volume", &json!({"track": t.0, "start": 0, "end": n * 5 + 10})).unwrap();
assert!(start.elapsed() < std::time::Duration::from_secs(10), "{:?}", start.elapsed());
let lane = e.session().track(t).unwrap().lane(&AutoParam::Volume).unwrap();
assert!(lane.points.len() as i64 >= n);
}
+1 -20
View File
@@ -96,22 +96,14 @@ pub fn specs() -> Vec<CommandSpec> {
const MIN_SPP: f64 = 0.25;
const MAX_SPP: f64 = 1_000_000.0;
/// Pixel column an off-screen edit cursor is brought to when zooming (the engine does not know the viewport width).
const OFFSCREEN_CURSOR_PX: f64 = 500.0;
fn zoom(e: &mut Engine, f: f64, x: Option<f64>) -> Result<Value> {
let insertion = e.session().edit.selection.start as f64;
let z = e.zoom_mut();
let (old, scroll) = (z.samples_per_px, z.scroll as f64);
let new = (old * f).clamp(MIN_SPP, MAX_SPP);
// Without a pointer, the edit cursor is the reference: held in place when visible, centred when off-screen.
let cursor_x = (insertion - scroll) / old;
if let Some(x) = x {
if let Some(x) = x.or_else(|| Some((insertion - scroll) / old).filter(|x| (0.0..4000.0).contains(x))) {
z.scroll = soundcraft_time::to_samples(scroll + x * (old - new)).clamp(0, MAX_POSITION);
} else if (0.0..4000.0).contains(&cursor_x) {
z.scroll = soundcraft_time::to_samples(scroll + cursor_x * (old - new)).clamp(0, MAX_POSITION);
} else {
z.scroll = soundcraft_time::to_samples(insertion - OFFSCREEN_CURSOR_PX * new).clamp(0, MAX_POSITION);
}
z.samples_per_px = new;
Ok(json!({"samples_per_px": new, "scroll": z.scroll}))
@@ -194,17 +186,6 @@ mod tests {
assert_eq!(view(&e), (100.0, 48_000));
}
#[test]
fn zoom_in_brings_an_offscreen_edit_cursor_into_view() {
let mut e = Engine::default();
e.execute("view.zoom_set", &json!({"samples_per_px": 100.0})).unwrap();
e.execute("edit.select", &json!({"start": 1_000_000, "end": 1_000_000})).unwrap();
e.execute("view.zoom_in", &json!({})).unwrap();
let (spp, scroll) = view(&e);
let x = (1_000_000.0 - scroll as f64) / spp;
assert!((x - OFFSCREEN_CURSOR_PX).abs() < 1.0, "cursor at {x}px");
}
#[test]
fn zoom_and_scroll_stay_in_range() {
let mut e = Engine::default();
-16
View File
@@ -178,13 +178,6 @@ pub struct Bounce {
enc: Option<soundcraft_audio_io::FileEncoder<Out>>,
}
/// The Broadcast Wave stamp of timeline position `at`: the session's timecode start plus `at`, in
/// samples since midnight (`bext` TimeReference; 0 before midnight).
fn time_stamp(s: &Session, at: Samples) -> soundcraft_audio_io::BwfInfo {
let time_reference = u64::try_from(s.timecode_start.saturating_add(at)).unwrap_or(0);
soundcraft_audio_io::BwfInfo { time_reference, ..Default::default() }
}
/// A finished bounce: the size of its file and its levels.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Bounced {
@@ -215,9 +208,6 @@ impl Bounce {
let part = path.with_file_name(format!("{name}.part"));
crate::file_access::check_write(path).map_err(EngineError::Io)?;
crate::file_access::check_write(&part).map_err(EngineError::Io)?;
// A WAV bounce says where it starts on the session's timecode, as other DAWs' bounces do,
// so post and broadcast tools can place it (only WAV has a `bext` chunk to say it in).
let opts = &EncodeOptions { bwf: opts.bwf.clone().or_else(|| Some(time_stamp(&s, r.start))), ..opts.clone() };
let enc = (|| -> std::result::Result<_, soundcraft_audio_io::AudioError> {
let enc = soundcraft_audio_io::FileEncoder::new(Out::create(&part)?, opts, channels, s.sample_rate.hz(), Some(fmt.channel_mask()))?;
enc.check_room(u64::try_from(r.len()).unwrap_or(0))?;
@@ -744,12 +734,6 @@ fn read_bundle(bytes: &[u8], limits: BundleLimits) -> Result<(String, std::colle
if z.len() > limits.entries {
return Err(EngineError::Io("bundle: too many files".into()));
}
// Entries are stored, so the files we keep can only outgrow the bundle when several entries
// point at the same bytes: a small bundle naming one payload thousands of times would be
// copied out once per name.
if z.has_overlapping_files().map_err(zerr)? {
return Err(EngineError::Io("bundle: files share data".into()));
}
let (mut text, mut files, mut total) = (None, std::collections::HashMap::new(), 0u64);
for i in 0..z.len() {
let mut f = z.by_index(i).map_err(zerr)?;
-17
View File
@@ -194,23 +194,6 @@ mod bundle_tests {
}
}
#[test]
fn entries_sharing_one_payload_are_refused() {
use std::io::Write as _;
let mut w = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
let opts = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
w.start_file("Song.scraft", opts).unwrap();
w.write_all(Session::default().to_json().unwrap().as_bytes()).unwrap();
w.start_file("Audio Files/a.wav", opts).unwrap();
w.write_all(&[7u8; 4096]).unwrap();
for i in 0..100 {
w.shallow_copy_file("Audio Files/a.wav", &format!("Audio Files/copy{i}.wav")).unwrap();
}
let bundle = w.finish().unwrap().into_inner();
let err = read_bundle(&bundle, BUNDLE_LIMITS).unwrap_err();
assert!(err.to_string().contains("share data"), "{err}");
}
#[test]
fn a_bundle_is_saved_only_if_it_opens_again() {
// #433: the writer keeps to the reader's limits.
-32
View File
@@ -123,38 +123,6 @@ fn a_bounce_too_big_for_its_format_fails_and_keeps_the_old_file() {
let _ = std::fs::remove_dir_all(&dir);
}
/// A WAV bounce is a Broadcast Wave stamped with where it starts on the session's timecode (`bext`
/// TimeReference, samples since midnight), as other DAWs write it, so post tools can place it.
#[test]
fn wav_bounces_carry_their_timecode_position_as_a_bwf_time_reference() {
let mut e = soundcraft_engine::demo::demo_engine();
let sr = u64::from(e.session().sample_rate.hz());
// The session starts at 01:00:00:00; the bounce at 2 s into it.
e.session_mut().timecode_start = (3600 * sr) as i64;
let (start, end) = (2 * sr, 3 * sr);
let dir = scratch("bounce-bext");
let stamp = |path: &std::path::Path| {
let ext = path.extension().and_then(|x| x.to_str());
soundcraft_audio_io::probe(&std::fs::read(path).unwrap(), ext).unwrap().bwf.map(|b| b.time_reference)
};
let mix = dir.join("Mix.wav");
e.execute("file.bounce_mix", &serde_json::json!({"path": mix, "start": start, "end": end})).unwrap();
assert_eq!(stamp(&mix), Some(3600 * sr + start), "mix");
let stems = dir.join("stems");
let files = e.execute("file.bounce_stems", &serde_json::json!({"dir": stems, "start": start, "end": end})).unwrap();
let files = files["files"].as_array().unwrap();
assert!(!files.is_empty());
for f in files {
let f = std::path::PathBuf::from(f.as_str().unwrap());
assert_eq!(stamp(&f), Some(3600 * sr + start), "{}", f.display());
}
// A session that starts at zero still says where the bounce starts.
e.session_mut().timecode_start = 0;
e.execute("file.bounce_mix", &serde_json::json!({"path": mix, "start": start, "end": end})).unwrap();
assert_eq!(stamp(&mix), Some(start));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn dropping_an_unfinished_bounce_cancels_it_and_keeps_the_old_file() {
let e = soundcraft_engine::demo::demo_engine();
-16
View File
@@ -68,23 +68,7 @@ pub struct NewInstance {
/// Creates a plugin (built-in or hosted), restores `state` (if any) and prepares it. Expensive
/// for third-party plugins: in realtime use, call it on the control thread.
pub fn create_instance(id: &str, state: Option<&[u8]>, sample_rate: f32, max_block: usize, ch: usize) -> Option<NewInstance> {
create_instance_for(id, state, sample_rate, max_block, ch, false)
}
/// [`create_instance`] for an engine that renders offline (`offline`) or in realtime: the plugin
/// is told which before it is prepared ([`Plugin::set_offline`]).
pub(crate) fn create_instance_for(
id: &str,
state: Option<&[u8]>,
sample_rate: f32,
max_block: usize,
ch: usize,
offline: bool,
) -> Option<NewInstance> {
let mut plugin = create_plugin(id)?;
if offline {
plugin.set_offline(true);
}
let refused_state = state.is_some_and(|st| !plugin.load_state(st));
if refused_state {
log::warn!("{id}: the plugin did not accept its stored state");
+1 -10
View File
@@ -144,8 +144,6 @@ pub struct MixEngine {
pub metronome: bool,
/// Third-party instances come from [`MixEngine::adopt`] instead of being created in `sync`.
external: bool,
/// An offline render: the plugins `sync` creates are told so ([`Plugin::set_offline`]).
offline: bool,
/// Instances handed over by the host, waiting for `sync` to place them.
adopt: HashMap<(TrackId, usize), PreparedInstance>,
/// Instances `sync` took out of service, for the host to drop (see [`MixEngine::take_retired`]).
@@ -179,7 +177,6 @@ impl MixEngine {
recording: false,
metronome: false,
external: false,
offline: false,
adopt: HashMap::new(),
retired: Vec::new(),
work: Vec::new(),
@@ -192,11 +189,6 @@ impl MixEngine {
self.external = on;
}
/// Offline render (a bounce): plugins created from now on are told they render offline.
pub fn set_offline(&mut self, on: bool) {
self.offline = on;
}
/// Takes instances created off the audio thread; the next render's `sync` places them.
/// An earlier instance waiting for the same slot is retired.
pub fn adopt(&mut self, instances: Vec<PreparedInstance>) {
@@ -279,8 +271,7 @@ impl MixEngine {
if self.main.len() != main_ch {
self.main = vec![vec![0.0; mb]; main_ch];
}
let mut cx =
SlotCtx { sr: self.sample_rate, mb, external: self.external, offline: self.offline, adopt: &mut self.adopt, retired: &mut self.retired };
let mut cx = SlotCtx { sr: self.sample_rate, mb, external: self.external, adopt: &mut self.adopt, retired: &mut self.retired };
let gone: Vec<TrackId> = self.strips.keys().filter(|id| s.track(**id).is_none()).copied().collect();
for id in gone {
self.meters.remove(&id);
+2 -8
View File
@@ -46,20 +46,12 @@ pub(crate) fn schedule_notes(s: &Session, t: &Track, pos: Samples, frames: usize
rtp("delay", 0.0).clamp(-1e7, 1e7) as i64,
)
};
// The block in clip ticks, one tick wider on each side so rounding never drops an event.
let near = s.tempo.samples_to_ticks(pos, sr).saturating_sub(base).saturating_sub(1)
..=s.tempo.samples_to_ticks(end, sr).saturating_sub(base).saturating_add(1);
for n0 in &sequence.notes {
let mut n = *n0;
n.velocity = (i64::from(n.velocity) + dv).clamp(1, 127) as u8;
n.pitch = (i64::from(n.pitch) + dp).clamp(0, 127) as u8;
n.length = ((n.length as f64 * dur) as i64).max(1);
n.start = n.start.saturating_add(delay).max(0);
// A note whose on and off both fall outside the block sends nothing: skip it before
// converting its positions, so a long sequence costs a comparison per note per block.
if n.start > *near.end() || n.start.saturating_add(n.length) < *near.start() {
continue;
}
let on = s.tempo.tick_to_samples(base.saturating_add(n.start), sr);
let off = s.tempo.tick_to_samples(base.saturating_add(n.start).saturating_add(n.length), sr).min(clip.end());
if on >= clip.end() {
@@ -76,6 +68,8 @@ pub(crate) fn schedule_notes(s: &Session, t: &Track, pos: Samples, frames: usize
held.retain(|p| *p != n.pitch);
}
}
let near = s.tempo.samples_to_ticks(pos, sr).saturating_sub(base).saturating_sub(1)
..=s.tempo.samples_to_ticks(end, sr).saturating_sub(base).saturating_add(1);
// Events before tick 0 are what a start trim hid: they don't play.
for c in sequence.ctrls.iter().filter(|c| c.tick >= 0) {
let tick = c.tick.saturating_add(delay).max(0);
+1 -2
View File
@@ -57,8 +57,7 @@ impl RangeRender {
/// Renders the main mix over `range`, or `bus` when one is named.
pub fn new(s: &Session, bus: Option<BusId>, range: Range, block: usize) -> Self {
let nch = source_channels(s, bus);
let mut eng = MixEngine::new(s.sample_rate.as_f64() as f32, block);
eng.set_offline(true);
let eng = MixEngine::new(s.sample_rate.as_f64() as f32, block);
let tmp = vec![vec![0.0f32; eng.max_block()]; nch];
let len = if nch == 0 { 0 } else { usize::try_from(range.len().max(0)).unwrap_or(0) };
RangeRender { eng, bus, tmp, start: range.start, done: 0, total: len, latency: 0 }
+24 -108
View File
@@ -46,8 +46,7 @@ pub(crate) fn processing_order(s: &Session) -> Vec<usize> {
} else {
// A cycle keeps the existing bounded ordering for the entire graph, including any
// independent acyclic branches. Feedback does not have a finite longest-path depth.
let depths = bounded_aux_depths(s);
auxes.sort_by_key(|&i| depths.get(i).copied().unwrap_or(0));
auxes.sort_by_key(|&i| bounded_aux_depth(s, i));
}
order.extend(auxes);
order
@@ -116,38 +115,32 @@ fn aux_output_busses(t: &Track) -> impl Iterator<Item = BusId> + '_ {
.filter_map(|route| if let Route::Bus(bus) = route { Some(*bus) } else { None })
}
/// Hop cap for the bounded depth used when the aux graph has a cycle.
const MAX_BOUNDED_DEPTH: usize = 8;
/// For each track, the bounded depth used for a cyclic graph: the length of the longest chain of
/// Aux/Folder feeders into it (through a bus its input reads and the feeder's output or a send
/// writes), capped at [`MAX_BOUNDED_DEPTH`] hops. One pass per hop over the tracks and their bus
/// writes, so the cost is linear in the routing, not a fresh feeder search per track per
/// sort comparison.
fn bounded_aux_depths(s: &Session) -> Vec<usize> {
let is_aux = |t: &Track| matches!(t.kind, TrackKind::Aux | TrackKind::Folder);
// Tracks with a feeder chain of the current length ending at them (length 0: every aux).
let mut reached: Vec<bool> = s.tracks.iter().map(is_aux).collect();
let mut depths = vec![0usize; s.tracks.len()];
let mut fed = std::collections::HashSet::<BusId>::new();
for hop in 1..=MAX_BOUNDED_DEPTH {
fed.clear();
for (t, _) in s.tracks.iter().zip(&reached).filter(|(t, r)| **r && is_aux(t)) {
fed.extend(aux_output_busses(t));
}
let mut any = false;
for ((t, r), d) in s.tracks.iter().zip(reached.iter_mut()).zip(depths.iter_mut()) {
*r = matches!(&t.mixer.input, Route::Bus(b) if fed.contains(b));
if *r {
*d = hop;
any = true;
}
}
if !any {
/// The previous bounded depth calculation, retained unchanged for any cyclic graph.
fn bounded_aux_depth(s: &Session, i: usize) -> usize {
let mut d = 0;
let mut cur = vec![i];
for _ in 0..8 {
let inputs: Vec<&Route> = cur.iter().filter_map(|&k| s.tracks.get(k)).map(|t| &t.mixer.input).collect();
let feeders: Vec<usize> = auxes_feeding(s, &inputs);
if feeders.is_empty() {
break;
}
d += 1;
cur = feeders;
}
depths
d
}
fn auxes_feeding(s: &Session, inputs: &[&Route]) -> Vec<usize> {
s.tracks
.iter()
.enumerate()
.filter(|(_, t)| matches!(t.kind, TrackKind::Aux | TrackKind::Folder))
.filter(|(_, t)| {
inputs.iter().any(|r| matches!(r, Route::Bus(_)) && (&&t.mixer.output == r || t.mixer.sends.iter().flatten().any(|sn| &&sn.target == r)))
})
.map(|(i, _)| i)
.collect()
}
/// Implicit solo: an aux is not muted when a soloed track feeds its input bus.
@@ -211,81 +204,4 @@ mod processing_order_tests {
// Both deepest independent returns tie with the cycle at the legacy eight-hop cap.
assert_eq!(processing_order(&s), vec![9, 8, 7, 6, 5, 4, 3, 2, 0, 1, 10]);
}
/// The per-track feeder search `bounded_aux_depths` replaced, kept as its reference.
fn reference_depth(s: &Session, i: usize) -> usize {
let mut d = 0;
let mut cur = vec![i];
for _ in 0..MAX_BOUNDED_DEPTH {
let inputs: Vec<&Route> = cur.iter().filter_map(|&k| s.tracks.get(k)).map(|t| &t.mixer.input).collect();
let feeders: Vec<usize> = s
.tracks
.iter()
.enumerate()
.filter(|(_, t)| matches!(t.kind, TrackKind::Aux | TrackKind::Folder))
.filter(|(_, t)| {
inputs
.iter()
.any(|r| matches!(r, Route::Bus(_)) && (&&t.mixer.output == r || t.mixer.sends.iter().flatten().any(|sn| &&sn.target == r)))
})
.map(|(i, _)| i)
.collect();
if feeders.is_empty() {
break;
}
d += 1;
cur = feeders;
}
d
}
#[test]
fn bounded_depths_match_the_per_track_feeder_search() {
// A small pseudo-random graph of auxes, folders and audio tracks with cycles, joins,
// sends and unrouted inputs.
let mut s = Session::default();
let busses: Vec<_> = (0..6).map(|i| s.add_bus(&format!("B{i}"), ChannelFormat::Stereo)).collect();
let mut x = 12345u32;
let mut next = |m: usize| {
x = x.wrapping_mul(1_103_515_245).wrapping_add(12_345);
(x >> 16) as usize % m
};
for i in 0..30 {
let kind = [TrackKind::Aux, TrackKind::Folder, TrackKind::Audio][next(3)];
let id = s.add_track(kind, ChannelFormat::Stereo, None);
let pick = |n: usize| if n < busses.len() { Route::Bus(busses[n]) } else { Route::Main };
let (input, output, send) = (pick(next(8)), pick(next(8)), pick(next(8)));
let t = s.track_mut(id).unwrap();
t.mixer.input = input;
t.mixer.output = output;
if i % 2 == 0 {
t.mixer.sends[0] = Some(SendSlot::new(send));
}
}
let depths = bounded_aux_depths(&s);
for (i, (t, depth)) in s.tracks.iter().zip(&depths).enumerate() {
if matches!(t.kind, TrackKind::Aux | TrackKind::Folder) {
assert_eq!(*depth, reference_depth(&s, i), "track {i}");
}
}
assert!(depths.contains(&MAX_BOUNDED_DEPTH));
}
#[test]
fn a_large_cyclic_graph_orders_quickly() {
// Thousands of auxes all reading and writing one bus: every aux feeds every other.
let mut s = Session::default();
let bus = s.add_bus("Loop", ChannelFormat::Stereo);
for _ in 0..3000 {
let id = s.add_track(TrackKind::Aux, ChannelFormat::Stereo, None);
let t = s.track_mut(id).unwrap();
t.mixer.input = Route::Bus(bus);
t.mixer.output = Route::Bus(bus);
t.mixer.sends[0] = Some(SendSlot::new(Route::Bus(bus)));
}
let start = std::time::Instant::now();
let order = processing_order(&s);
assert!(start.elapsed() < std::time::Duration::from_secs(5), "{:?}", start.elapsed());
assert_eq!(order, (0..3000).collect::<Vec<_>>());
}
}
+8 -128
View File
@@ -1,6 +1,6 @@
use super::automation::{GainRamp, transport_at, volume_db_at};
use super::clips::{ClipFx, clip_fx_params, render_clip};
use super::instances::{INSTRUMENT_SLOT, PluginFailures, PreparedInstance, create_instance_for, is_third_party};
use super::instances::{INSTRUMENT_SLOT, PluginFailures, PreparedInstance, create_instance, is_third_party};
use super::midi::{chase_controllers, schedule_notes};
use super::pan::{PanSpec, apply_matrix, build_matrix, identity_matrix};
use super::routing::receives_solo;
@@ -24,9 +24,6 @@ pub(crate) struct PluginSlot {
pub(crate) plugin: Box<dyn Plugin>,
/// Last values pushed to the plugin, in the insert's parameter-map order.
applied: Vec<f32>,
/// Scratch for `apply_params`: indexes of the track's automation lanes for this slot, kept
/// so the audio thread reuses its allocation.
lanes: Vec<usize>,
pub(crate) failed: bool,
}
@@ -160,8 +157,6 @@ pub(crate) struct SlotCtx<'a> {
pub(crate) sr: f32,
pub(crate) mb: usize,
pub(crate) external: bool,
/// The engine renders offline: plugins it creates are told so.
pub(crate) offline: bool,
pub(crate) adopt: &'a mut HashMap<(TrackId, usize), PreparedInstance>,
pub(crate) retired: &'a mut Vec<Box<dyn Plugin>>,
}
@@ -194,9 +189,9 @@ impl SlotCtx<'_> {
}
} else {
let state = if third { ins.state_bytes() } else { None };
create_instance_for(&ins.plugin, state.as_deref(), self.sr, self.mb, ch, self.offline).map(|n| n.plugin)
create_instance(&ins.plugin, state.as_deref(), self.sr, self.mb, ch).map(|n| n.plugin)
}?;
Some(PluginSlot { id: ins.plugin.clone(), ch, plugin, applied: Vec::new(), lanes: Vec::new(), failed: false })
Some(PluginSlot { id: ins.plugin.clone(), ch, plugin, applied: Vec::new(), failed: false })
}
}
@@ -496,40 +491,21 @@ pub(crate) fn apply_params(slot: &mut PluginSlot, ins: &soundcraft_model::Insert
let Ok(slot_u8) = u8::try_from(i) else {
return apply_values(slot, ins, |_, v| v);
};
let lanes = &t.automation;
let name = |j: usize| match lanes.get(j).map(|l| &l.param) {
Some(AutoParam::Plugin { param, .. }) => param.as_str(),
_ => "",
};
// This slot's lanes, ordered by parameter name (the parameter map's order) and keeping only
// the first lane per name (the one a lookup by name finds). Matching them to the parameters
// in one merge costs O(lanes log lanes + params) per block, not O(lanes x params).
let mut order = std::mem::take(&mut slot.lanes);
order.clear();
if t.mixer.automation_mode.reads() {
order
.extend(lanes.iter().enumerate().filter(|(_, l)| matches!(&l.param, AutoParam::Plugin { slot, .. } if *slot == slot_u8)).map(|(j, _)| j));
if !order.iter().any(|&j| lanes.get(j).is_some_and(|l| !l.points.is_empty())) {
order.clear();
}
order.sort_unstable_by(|&a, &b| name(a).cmp(name(b)).then(a.cmp(&b)));
order.dedup_by(|later, kept| name(*later) == name(*kept));
}
let mut next = order.iter().copied().peekable();
let automated = t.mixer.automation_mode.reads()
&& t.automation.iter().any(|l| matches!(&l.param, AutoParam::Plugin { slot, .. } if *slot == slot_u8) && !l.points.is_empty());
apply_values(slot, ins, |k, val| {
while next.next_if(|&j| name(j) < k).is_some() {}
if let Some(l) = next.peek().filter(|&&j| name(j) == k).and_then(|&j| lanes.get(j))
if automated
&& let Some(l) = t.automation.iter().find(|l| matches!(&l.param, AutoParam::Plugin { slot, param } if *slot == slot_u8 && param == k))
&& !l.points.is_empty()
{
return l.value_at(pos, val);
}
val
});
slot.lanes = order;
}
/// Sends each stored param (as mapped by `value`) to the plugin when it changed since last time.
fn apply_values(slot: &mut PluginSlot, ins: &soundcraft_model::Insert, mut value: impl FnMut(&str, f32) -> f32) {
fn apply_values(slot: &mut PluginSlot, ins: &soundcraft_model::Insert, value: impl Fn(&str, f32) -> f32) {
if slot.applied.len() != ins.params.len() {
slot.applied = vec![f32::NAN; ins.params.len()];
}
@@ -541,99 +517,3 @@ fn apply_values(slot: &mut PluginSlot, ins: &soundcraft_model::Insert, mut value
}
}
}
#[cfg(test)]
mod apply_params_tests {
use super::*;
use soundcraft_model::{AutomationLane, Insert};
use std::sync::Mutex;
/// A built-in gain that logs every parameter set.
struct Recorder {
inner: Box<dyn Plugin>,
sets: Arc<Mutex<Vec<(String, f32)>>>,
}
impl Plugin for Recorder {
fn info(&self) -> &'static soundcraft_dsp::PluginInfo {
self.inner.info()
}
fn prepare(&mut self, sr: f32, mb: usize, ch: usize) {
self.inner.prepare(sr, mb, ch);
}
fn reset(&mut self) {
self.inner.reset();
}
fn set_param(&mut self, id: &str, v: f32) -> bool {
self.sets.lock().unwrap().push((id.to_string(), v));
true
}
fn param(&self, id: &str) -> Option<f32> {
self.inner.param(id)
}
fn process(&mut self, io: &mut [Vec<f32>], frames: usize) {
self.inner.process(io, frames);
}
}
fn recorder() -> (PluginSlot, Arc<Mutex<Vec<(String, f32)>>>) {
let sets = Arc::new(Mutex::new(Vec::new()));
let plugin = Box::new(Recorder { inner: soundcraft_dsp::create("gain").unwrap(), sets: Arc::clone(&sets) });
(PluginSlot { id: "gain".into(), ch: 2, plugin, applied: Vec::new(), lanes: Vec::new(), failed: false }, sets)
}
fn lane(slot: u8, param: &str, point: Option<f32>) -> AutomationLane {
let mut l = AutomationLane::new(AutoParam::Plugin { slot, param: param.into() });
if let Some(v) = point {
l.set_point(0, v);
}
l
}
#[test]
fn first_lane_per_param_of_this_slot_drives_it() {
let mut s = Session::default();
let t = s.add_track(TrackKind::Audio, ChannelFormat::Stereo, None);
let tr = s.track_mut(t).unwrap();
tr.automation = vec![
lane(0, "c", None),
lane(1, "a", Some(5.0)),
lane(0, "b", Some(9.0)),
lane(0, "c", Some(7.0)),
lane(0, "zz", Some(1.0)),
lane(0, "b", Some(8.0)),
];
let mut ins = Insert::new("gain");
for (k, v) in [("a", 1.0), ("b", 2.0), ("c", 3.0), ("d", 4.0)] {
ins.params.insert(k.into(), v);
}
let (mut slot, sets) = recorder();
apply_params(&mut slot, &ins, tr, 0, 0);
let got = sets.lock().unwrap().clone();
let want: Vec<(String, f32)> = [("a", 1.0), ("b", 9.0), ("c", 3.0), ("d", 4.0)].iter().map(|(k, v)| (k.to_string(), *v)).collect();
assert_eq!(got, want);
// Unchanged values are not sent again.
apply_params(&mut slot, &ins, tr, 0, 0);
assert_eq!(sets.lock().unwrap().len(), 4);
}
#[test]
fn many_params_and_lanes_apply_without_a_quadratic_search() {
let n = 40_000;
let mut s = Session::default();
let t = s.add_track(TrackKind::Audio, ChannelFormat::Stereo, None);
let tr = s.track_mut(t).unwrap();
tr.automation = (0..n).rev().map(|i| lane(0, &format!("p{i:06}"), Some(i as f32))).collect();
let mut ins = Insert::new("gain");
for i in 0..n {
ins.params.insert(format!("p{i:06}"), -1.0);
}
let (mut slot, sets) = recorder();
let start = std::time::Instant::now();
apply_params(&mut slot, &ins, tr, 0, 0);
assert!(start.elapsed() < std::time::Duration::from_secs(5), "{:?}", start.elapsed());
let got = sets.lock().unwrap();
assert_eq!(got.len(), n);
assert!(got.iter().all(|(k, v)| k == &format!("p{:06}", *v as usize)));
}
}
+35 -144
View File
@@ -40,34 +40,14 @@ pub struct BarBeat {
/// Tempo and meter changes for a session. Always has at least one of each, at tick 0.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(from = "TempoMapData")]
pub struct TempoMap {
tempos: Vec<TempoEvent>,
meters: Vec<MeterEvent>,
/// For each tempo event, the elapsed time at its tick in quarter-note minutes (ticks / bpm,
/// summed over the earlier segments). Lets tick/sample conversions binary-search the segment
/// instead of walking every earlier tempo change, so converting many positions against a large
/// tempo map stays O(log n) each. Rebuilt whenever `tempos` changes.
#[serde(skip)]
elapsed: Vec<f64>,
}
/// The serialized form of [`TempoMap`], without the derived lookup table.
#[derive(serde::Deserialize)]
struct TempoMapData {
tempos: Vec<TempoEvent>,
meters: Vec<MeterEvent>,
}
impl From<TempoMapData> for TempoMap {
fn from(d: TempoMapData) -> Self {
TempoMap::from_parts(d.tempos, d.meters)
}
}
impl Default for TempoMap {
fn default() -> Self {
TempoMap::from_parts(vec![TempoEvent { tick: 0, bpm: 120.0 }], vec![MeterEvent { tick: 0, numerator: 4, denominator: 4 }])
TempoMap { tempos: vec![TempoEvent { tick: 0, bpm: 120.0 }], meters: vec![MeterEvent { tick: 0, numerator: 4, denominator: 4 }] }
}
}
@@ -80,43 +60,6 @@ fn valid_meter(numerator: u32, denominator: u32) -> bool {
}
impl TempoMap {
fn from_parts(tempos: Vec<TempoEvent>, meters: Vec<MeterEvent>) -> Self {
let mut m = TempoMap { tempos, meters, elapsed: Vec::new() };
m.reindex();
m
}
/// Rebuild `elapsed` from `tempos`. The first segment runs from tick 0 at the first tempo.
fn reindex(&mut self) {
let mut elapsed = Vec::with_capacity(self.tempos.len());
let mut acc = 0.0;
let mut seg_tick = 0i64;
let mut bpm = self.tempos.first().map_or(120.0, |e| e.bpm);
for (i, e) in self.tempos.iter().enumerate() {
if i > 0 {
// Saturating: a file's ticks are indexed before `sanitize` orders them.
acc += e.tick.saturating_sub(seg_tick) as f64 / bpm;
seg_tick = e.tick;
bpm = e.bpm;
}
elapsed.push(acc);
}
self.elapsed = elapsed;
}
/// Samples per quarter-note minute (the unit of `elapsed`) at `sr`.
fn samples_per_elapsed(sr: SampleRate) -> f64 {
sr.as_f64() * 60.0 / TICKS_PER_QUARTER as f64
}
/// The tempo segment `i` (an index into `tempos`, 0 when it is empty): its start tick (0 for
/// the first), bpm and elapsed time at its start.
fn segment(&self, i: usize) -> (i64, f64, f64) {
let bpm = self.tempos.get(i).or(self.tempos.first()).map_or(120.0, |e| e.bpm);
let tick = if i == 0 { 0 } else { self.tempos.get(i).map_or(0, |e| e.tick) };
(tick, bpm, self.elapsed.get(i).copied().unwrap_or(0.0))
}
pub fn new(bpm: f64, numerator: u32, denominator: u32) -> Result<Self, TimeError> {
let mut m = TempoMap::default();
m.set_tempo(0, bpm)?;
@@ -148,7 +91,6 @@ impl TempoMap {
let (numerator, denominator) = self.meters.first().map_or((4, 4), |m| (m.numerator, m.denominator));
self.meters.insert(0, MeterEvent { tick: 0, numerator, denominator });
}
self.reindex();
}
/// Insert or replace the tempo at `tick`.
@@ -163,7 +105,6 @@ impl TempoMap {
}
Err(i) => self.tempos.insert(i, TempoEvent { tick, bpm }),
}
self.reindex();
Ok(())
}
@@ -174,7 +115,6 @@ impl TempoMap {
}
let before = self.tempos.len();
self.tempos.retain(|e| e.tick != tick);
self.reindex();
before != self.tempos.len()
}
@@ -264,20 +204,36 @@ impl TempoMap {
}
fn tick_to_samples_f(&self, tick: i64, sr: SampleRate) -> f64 {
// Tempo events after the first that start before `tick` (the segments it has passed).
let passed = self.tempos.get(1..).map_or(0, |rest| rest.partition_point(|e| e.tick < tick));
let (seg_tick, bpm, elapsed) = self.segment(passed);
elapsed * Self::samples_per_elapsed(sr) + (tick - seg_tick) as f64 * Self::samples_per_tick(bpm, sr)
let mut acc = 0.0;
let mut seg_tick = 0i64;
let mut bpm = self.tempos.first().map_or(120.0, |e| e.bpm);
for e in self.tempos.iter().skip(1) {
if e.tick >= tick {
break;
}
acc += (e.tick - seg_tick) as f64 * Self::samples_per_tick(bpm, sr);
seg_tick = e.tick;
bpm = e.bpm;
}
acc + (tick - seg_tick) as f64 * Self::samples_per_tick(bpm, sr)
}
/// Samples → ticks (fractional).
pub fn samples_to_ticks_f(&self, s: Samples, sr: SampleRate) -> f64 {
let s = s as f64;
let scale = Self::samples_per_elapsed(sr);
// Tempo events after the first that start at or before `s`.
let passed = self.elapsed.get(1..).map_or(0, |rest| rest.partition_point(|e| e * scale <= s));
let (seg_tick, bpm, elapsed) = self.segment(passed);
seg_tick as f64 + (s - elapsed * scale) / Self::samples_per_tick(bpm, sr)
let mut acc = 0.0;
let mut seg_tick = 0i64;
let mut bpm = self.tempos.first().map_or(120.0, |e| e.bpm);
for e in self.tempos.iter().skip(1) {
let seg = (e.tick - seg_tick) as f64 * Self::samples_per_tick(bpm, sr);
if acc + seg > s {
break;
}
acc += seg;
seg_tick = e.tick;
bpm = e.bpm;
}
seg_tick as f64 + (s - acc) / Self::samples_per_tick(bpm, sr)
}
pub fn samples_to_ticks(&self, s: Samples, sr: SampleRate) -> i64 {
@@ -363,7 +319,6 @@ impl TempoMap {
e.bpm = valid_bpm(e.bpm * factor)?;
}
self.tempos = out;
self.reindex();
Ok(())
}
}
@@ -452,24 +407,24 @@ mod tests {
fn sanitize_keeps_what_the_setters_accept() {
let tempo = |tick, bpm| TempoEvent { tick, bpm };
let meter = |tick, numerator, denominator| MeterEvent { tick, numerator, denominator };
let mut m = TempoMap::from_parts(
vec![tempo(960, 4e9), tempo(3840, 90.0), tempo(-5, 100.0), tempo(1920, 60.0), tempo(1920, 70.0)],
vec![meter(0, 0, 4), meter(3840, 3, 4)],
);
let mut m = TempoMap {
tempos: vec![tempo(960, 4e9), tempo(3840, 90.0), tempo(-5, 100.0), tempo(1920, 60.0), tempo(1920, 70.0)],
meters: vec![meter(0, 0, 4), meter(3840, 3, 4)],
};
m.sanitize();
assert_eq!(m.tempos(), [tempo(0, 60.0), tempo(1920, 60.0), tempo(3840, 90.0)]);
assert_eq!(m.meters(), [meter(0, 3, 4), meter(3840, 3, 4)]);
let mut empty = TempoMap::from_parts(Vec::new(), Vec::new());
let mut empty = TempoMap { tempos: Vec::new(), meters: Vec::new() };
empty.sanitize();
assert_eq!(empty, TempoMap::default());
}
#[test]
fn sanitize_drops_events_beyond_the_position_range() {
let mut m = TempoMap::from_parts(
vec![TempoEvent { tick: 0, bpm: 120.0 }, TempoEvent { tick: i64::MAX, bpm: 90.0 }],
vec![MeterEvent { tick: 0, numerator: 4, denominator: 4 }, MeterEvent { tick: i64::MAX, numerator: 3, denominator: 4 }],
);
let mut m = TempoMap {
tempos: vec![TempoEvent { tick: 0, bpm: 120.0 }, TempoEvent { tick: i64::MAX, bpm: 90.0 }],
meters: vec![MeterEvent { tick: 0, numerator: 4, denominator: 4 }, MeterEvent { tick: i64::MAX, numerator: 3, denominator: 4 }],
};
m.sanitize();
assert_eq!(m, TempoMap::default());
assert_eq!(m.bar_start_tick(2), TICKS_PER_QUARTER * 4);
@@ -491,68 +446,4 @@ mod tests {
assert_eq!(m.bar_beat_at_tick(-TICKS_PER_QUARTER * 4), BarBeat { bar: 0, beat: 1, tick: 0 });
assert_eq!(m.bar_beat_at_tick(-TICKS_PER_QUARTER), BarBeat { bar: 0, beat: 4, tick: 0 });
}
/// The segment walk the indexed conversions replaced, kept as the reference they must match.
fn linear_tick_to_samples(m: &TempoMap, tick: i64, sr: SampleRate) -> i64 {
let mut acc = 0.0;
let mut seg_tick = 0i64;
let mut bpm = m.tempos[0].bpm;
for e in m.tempos.iter().skip(1) {
if e.tick >= tick {
break;
}
acc += (e.tick - seg_tick) as f64 * TempoMap::samples_per_tick(bpm, sr);
seg_tick = e.tick;
bpm = e.bpm;
}
to_samples(acc + (tick - seg_tick) as f64 * TempoMap::samples_per_tick(bpm, sr))
}
#[test]
fn indexed_conversions_match_the_segment_walk() {
let sr = SampleRate::HZ_48000;
let mut m = TempoMap::default();
for i in 1..200i64 {
m.set_tempo(i * 487, 40.0 + ((i * 37) % 300) as f64).unwrap();
}
assert!(m.remove_tempo(487 * 50));
for tick in (-1000..200 * 487 + 5000).step_by(113) {
let s = m.tick_to_samples(tick, sr);
assert_eq!(s, linear_tick_to_samples(&m, tick, sr), "tick {tick}");
assert!((m.samples_to_ticks(s, sr) - tick).abs() <= 1, "tick {tick}");
}
let json = serde_json::to_string(&m).unwrap();
assert!(!json.contains("elapsed"));
let back: TempoMap = serde_json::from_str(&json).unwrap();
assert_eq!(back, m);
assert_eq!(back.tick_to_samples(150 * 487 + 3, sr), m.tick_to_samples(150 * 487 + 3, sr));
}
#[test]
fn a_file_with_wild_ticks_is_indexed_without_overflow() {
let json = r#"{"tempos":[{"tick":0,"bpm":120.0},{"tick":-9223372036854775808,"bpm":100.0},{"tick":9223372036854775807,"bpm":90.0}],"meters":[{"tick":0,"numerator":4,"denominator":4}]}"#;
let mut m: TempoMap = serde_json::from_str(json).unwrap();
m.sanitize();
assert_eq!(m.tempos().first().map(|e| e.tick), Some(0));
assert_eq!(m.elapsed.len(), m.tempos().len());
}
#[test]
fn conversions_against_a_large_tempo_map_stay_fast() {
// 200k tempo changes, then 200k conversions each way: quadratic if every conversion walks
// the earlier segments, well under a second when it binary-searches them.
let sr = SampleRate::HZ_48000;
let n = 200_000i64;
let tempos = (0..n).map(|i| TempoEvent { tick: i * 10, bpm: 60.0 + (i % 7) as f64 }).collect();
let mut m = TempoMap::from_parts(tempos, vec![MeterEvent { tick: 0, numerator: 4, denominator: 4 }]);
m.sanitize();
let start = std::time::Instant::now();
let mut sum = 0i64;
for i in 0..n {
let s = m.tick_to_samples(i * 10 + 5, sr);
sum = sum.wrapping_add(s).wrapping_add(m.samples_to_ticks(s, sr));
}
assert!(sum != 0);
assert!(start.elapsed() < std::time::Duration::from_secs(5), "{:?}", start.elapsed());
}
}
+6 -42
View File
@@ -115,18 +115,8 @@ fn name_plate(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect, select
track.name.clone()
};
let name_color = if selected { t.text_dark } else { t.text.lerp_to_gamma(t.text_dim, 0.4) };
let font = bold(12.5);
let galley = ui.painter().layout_no_wrap(shown_name.clone(), font.clone(), name_color);
let overflows = galley.size().x > name_r.width() - 3.0;
let (text_pos, align) =
if overflows { (pos2(name_r.min.x + 1.5, name_r.center().y), Align2::LEFT_CENTER) } else { (name_r.center(), Align2::CENTER_CENTER) };
ui.painter().with_clip_rect(name_r).text(text_pos, align, &shown_name, font, name_color);
let mut nresp = ui.interact(name_r, ui.scope_id().with(("name", id.0)), Sense::click_and_drag());
if overflows {
nresp = nresp.on_hover_ui(|ui| {
ui.label(&shown_name);
});
}
ui.painter().with_clip_rect(name_r).text(pos2(name_r.min.x + 1.5, name_r.center().y), Align2::LEFT_CENTER, &shown_name, bold(12.5), name_color);
let nresp = ui.interact(name_r, ui.scope_id().with(("name", id.0)), Sense::click_and_drag());
if nresp.double_clicked() {
app.dialogs.open_rename_track(id, &track.name);
} else if nresp.clicked() {
@@ -200,9 +190,8 @@ fn toggle_keys(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect) {
let h = head.height();
let at = |x: f32, y: f32, w: f32, h: f32| Rect::from_min_size(pos2(head.min.x + x, head.min.y + y), vec2(w, h));
let m = &track.mixer;
let is_master = track.kind == TrackKind::Master;
if h >= 37.0 && !is_master {
let (records, mixes) = (track.kind.has_playlist(), true);
if h >= 37.0 {
let (records, mixes) = (track.kind.has_playlist(), track.kind != TrackKind::Master);
let implied = !m.solo && app.engine.session().tracks.iter().any(|t| t.mixer.solo);
let keys = [
(HeaderKey::Rec, m.record_arm, false, "mix.record_arm", records),
@@ -224,13 +213,10 @@ fn view_selector(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect) {
let id = track.id;
let h = head.height();
let at = |x: f32, y: f32, w: f32, h: f32| Rect::from_min_size(pos2(head.min.x + x, head.min.y + y), vec2(w, h));
let is_master = track.kind == TrackKind::Master;
let view_y = if is_master { 20.0 } else { 39.0 };
let view_min_h = if is_master { 37.0 } else { 58.0 };
if h >= view_min_h {
if h >= 58.0 {
let vresp = header_selector(
ui,
at(17.0, view_y, 105.0, 17.0),
at(17.0, 39.0, 105.0, 17.0),
ui.scope_id().with(("view", id.0)),
&view_label(&track.view),
t.text,
@@ -239,32 +225,10 @@ fn view_selector(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect) {
);
egui::Popup::menu(&vresp).show(|ui| view_menu(app, ui, track));
}
if is_master && h >= 58.0 {
let mode = track.mixer.automation_mode;
let col = match mode {
soundcraft_model::AutomationMode::Read => t.auto_read,
soundcraft_model::AutomationMode::Write => t.auto_write,
soundcraft_model::AutomationMode::Touch | soundcraft_model::AutomationMode::TouchLatch => t.auto_touch,
soundcraft_model::AutomationMode::Latch => t.auto_latch,
_ => t.text_dim,
};
let mode_name = format!("Automation mode for {}", track.name);
let aresp = header_selector(ui, at(17.0, 39.0, 105.0, 17.0), ui.scope_id().with(("auto_mode", id.0)), mode.label(), col, true, &mode_name);
egui::Popup::menu(&aresp).show(|ui| {
for m in soundcraft_model::AutomationMode::ALL {
if ui.selectable_label(m == mode, m.label()).clicked() {
let _ = app.run("mix.automation_mode", json!({"track": id.0, "mode": m.label()}));
}
}
});
}
}
/// Freeze, voice and automation mode.
fn voice_row(app: &mut SoundApp, ui: &mut Ui, track: &Track, head: Rect) {
if track.kind == TrackKind::Master {
return;
}
let t = Tokens::current();
let id = track.id;
let h = head.height();
+1 -2
View File
@@ -222,8 +222,7 @@ pub(super) fn draw_rulers(app: &mut SoundApp, ui: &mut Ui, area: Rect, tl: Rect,
} else if resp.clicked()
&& let Some(m) = hit
{
let command = if ui.input(|i| i.modifiers.alt) { "markers.delete" } else { "markers.recall" };
let _ = app.run(command, json!({"number": m.number}));
let _ = app.run("markers.recall", json!({"number": m.number}));
}
}
}
-47
View File
@@ -1508,50 +1508,3 @@ fn alt_click_on_a_breakpoint_deletes_it() {
assert_eq!(h.state().engine.session().track(track).unwrap().clips(), clips.as_slice(), "{tool}");
}
}
#[test]
fn alt_click_on_a_marker_deletes_it() {
let mut engine = soundcraft_engine::demo::demo_engine();
engine.session_mut().edit.rulers = vec!["markers".into()];
engine.execute("markers.add", &json!({"at": 48_000, "name": "Alt-delete me"})).unwrap();
let mut h = harness_with(engine);
let s = h.state().engine.session_arc();
let marker = s.markers.iter().find(|m| m.name == "Alt-delete me").unwrap();
let at = pos2(x_of(&s, timeline(&h), marker.start), timeline(&h).min.y - 11.5);
let before = s.markers.len();
click_with(&mut h, at, egui::Modifiers::ALT);
assert_eq!(h.state().engine.session().markers.len(), before - 1);
assert!(!h.state().engine.session().markers.iter().any(|m| m.name == "Alt-delete me"));
}
#[test]
fn short_track_name_is_centered_and_long_name_is_left_aligned() {
let mut h = harness(2);
let s = h.state().engine.session_arc();
let first = s.tracks[0].id.0;
let second = s.tracks[1].id.0;
h.state_mut().run("track.rename", json!({"track": first, "name": "Vocal"})).unwrap();
h.state_mut().run("track.rename", json!({"track": second, "name": "A Very Long Track Name That Exceeds Header Width"})).unwrap();
h.step();
let r0 = row(&h, 0);
let r1 = row(&h, 1);
let first_text = h.output().shapes.iter().find_map(|s| match &s.shape {
Shape::Text(t) if t.galley.text() == "Vocal" && s.clip_rect.intersects(r0) => Some((t.pos, t.galley.size())),
_ => None,
});
let long_text = h.output().shapes.iter().find_map(|s| match &s.shape {
Shape::Text(t) if t.galley.text() == "A Very Long Track Name That Exceeds Header Width" && s.clip_rect.intersects(r1) => Some(t.pos),
_ => None,
});
let (first_pos, first_size) = first_text.expect("short track name should be rendered in edit header");
let long_pos = long_text.expect("long track name should be rendered in edit header");
assert_eq!(long_pos.x, r1.min.x + 17.5);
let first_center_x = first_pos.x + first_size.x * 0.5;
assert!((first_center_x - (r0.min.x + 64.5)).abs() <= 0.1, "short name should be centered (center was at {first_center_x})");
}
-13
View File
@@ -176,19 +176,6 @@ pub fn draw(painter: &Painter, r: Rect, name: &str, c: Color32) {
painter.line_segment([p(r, x, 0.5 - h), p(r, x, 0.5 + h)], s);
}
}
"link_track" => {
// A link chain below two track rows, so the track link reads apart from the timeline link's plain chain.
painter.line_segment([p(r, 0.12, 0.18), p(r, 0.88, 0.18)], s);
painter.line_segment([p(r, 0.12, 0.32), p(r, 0.88, 0.32)], s);
painter.circle_stroke(p(r, 0.38, 0.72), r.width() * 0.16, s);
painter.circle_stroke(p(r, 0.62, 0.72), r.width() * 0.16, s);
}
"insertion_follows" => {
// The insertion line, a lead, and the playhead arrow it follows.
painter.line_segment([p(r, 0.3, 0.14), p(r, 0.3, 0.86)], s);
painter.line_segment([p(r, 0.42, 0.5), p(r, 0.62, 0.5)], s);
painter.add(Shape::convex_polygon(vec![p(r, 0.84, 0.5), p(r, 0.64, 0.34), p(r, 0.64, 0.66)], c, Stroke::NONE));
}
_ => {
painter.rect_stroke(r.shrink(r.width() * 0.25), 1.0, s, egui::StrokeKind::Middle);
}
+1 -22
View File
@@ -11,12 +11,7 @@ pub(crate) fn route_name(app: &SoundApp, r: &Route) -> String {
}
pub(crate) fn input_name(app: &SoundApp, track: &Track) -> String {
if track.kind.has_playlist() && track.mixer.input == Route::None {
let channels = track.format.channels();
if channels == 1 { "In 1".to_string() } else { format!("In 1-{channels}") }
} else {
route_name(app, &track.mixer.input)
}
if track.kind.has_playlist() && track.mixer.input == Route::None { "In 1".to_string() } else { route_name(app, &track.mixer.input) }
}
/// The input or output menu under `resp`. It closes only on a click outside, so its bus search
@@ -64,19 +59,3 @@ fn route_menu(app: &mut SoundApp, ui: &mut Ui, id: TrackId, input: bool) {
ui.close();
}
}
#[cfg(test)]
mod tests {
use super::input_name;
use crate::{Services, SoundApp};
use serde_json::json;
#[test]
fn stereo_track_defaults_to_a_stereo_input_pair() {
let mut app = SoundApp::new(soundcraft_engine::Engine::default(), None, Services::default());
app.run("track.new", json!({"format": "stereo"})).unwrap();
let track = &app.engine.session().tracks[0];
assert_eq!(input_name(&app, track), "In 1-2");
}
}
+4 -90
View File
@@ -388,17 +388,8 @@ fn send_route_menu(app: &mut SoundApp, ui: &Ui, field: &egui::Response, id: Trac
},
);
let Some((picked, footer)) = shown else { return };
// As in Pro Tools, a new send opens its Send window (moving it from any other send), so its
// fader is on screen right away. A send that only changes destination leaves the window alone.
let show_new_send = |app: &mut SoundApp| {
if !has_send {
app.ui.send_window = Some((id, slot));
}
};
if let Some(bus) = picked
&& app.run("mix.send", json!({"track": id.0, "slot": slot, "bus": bus})).is_ok()
{
show_new_send(app);
if let Some(bus) = picked {
let _ = app.run("mix.send", json!({"track": id.0, "slot": slot, "bus": bus}));
}
match footer {
Some(SendFooter::NewBus) => {
@@ -408,10 +399,8 @@ fn send_route_menu(app: &mut SoundApp, ui: &Ui, field: &egui::Response, id: Trac
{
n += 1;
}
if app.run("mix.new_bus", json!({"name": format!("Bus {n}")})).is_ok()
&& app.run("mix.send", json!({"track": id.0, "slot": slot, "bus": format!("Bus {n}")})).is_ok()
{
show_new_send(app);
if app.run("mix.new_bus", json!({"name": format!("Bus {n}")})).is_ok() {
let _ = app.run("mix.send", json!({"track": id.0, "slot": slot, "bus": format!("Bus {n}")}));
}
}
Some(SendFooter::RemoveSend) => {
@@ -475,79 +464,4 @@ mod tests {
assert_eq!(field(&h), search, "the search field stays put");
assert!((footer(&h).top() - full.top()).abs() < 1.0, "cleared, the footer is back under the full list: {:?} vs {full:?}", footer(&h));
}
/// Kick's send A slot in a test harness, with buses "Aux" and "Other"; with `existing_send`, the
/// slot already sends to "Other".
fn kick_send_harness(existing_send: bool) -> (Harness<'static, (SoundApp, TrackId)>, TrackId) {
let mut app = SoundApp::new(soundcraft_engine::demo::demo_engine(), None, Services::default());
app.run("mix.new_bus", json!({"name": "Aux"})).unwrap();
app.run("mix.new_bus", json!({"name": "Other"})).unwrap();
let kick = app.engine.session().track_by_name("Kick").unwrap().id;
if existing_send {
app.run("mix.send", json!({"track": kick.0, "slot": 0, "bus": "Other"})).unwrap();
}
let mut ready = false;
let h = Harness::builder().with_size(vec2(600.0, 800.0)).build_ui_state(
move |ui, (app, kick): &mut (SoundApp, TrackId)| {
if !ready {
crate::fonts::install(ui.ctx(), true);
crate::theme::apply(ui.ctx(), false);
ready = true;
return;
}
let track = app.engine.session().track(*kick).cloned().unwrap();
let r = Rect::from_min_size(ui.max_rect().min + vec2(20.0, 20.0), vec2(120.0, 24.0));
send_slot(app, ui, &track, 0, r);
},
(app, kick),
);
(h, kick)
}
/// Creating a send opens its Send window, so the new send's fader is on screen right away (#1269).
#[test]
fn creating_a_send_opens_its_send_window() {
let _palette = crate::theme::lock_test_palette();
let (mut h, kick) = kick_send_harness(false);
h.run();
h.get_by_label("Send A for Kick").click();
h.run();
h.get_by_label("Aux").click();
h.run();
let (app, _) = h.state();
let send = app.engine.session().track(kick).and_then(|t| t.mixer.sends.first().cloned().flatten());
assert!(send.is_some(), "the send was created");
assert_eq!(app.ui.send_window, Some((kick, 0)), "the new send's window is open");
}
/// So does a send made with "new bus...".
#[test]
fn a_send_to_a_new_bus_opens_its_send_window() {
let _palette = crate::theme::lock_test_palette();
let (mut h, kick) = kick_send_harness(false);
h.run();
h.get_by_label("Send A for Kick").click();
h.run();
h.get_by_label("new bus...").click();
h.run();
let (app, _) = h.state();
assert!(app.engine.session().track(kick).and_then(|t| t.mixer.sends.first().cloned().flatten()).is_some(), "the send was created");
assert_eq!(app.ui.send_window, Some((kick, 0)), "the new send's window is open");
}
/// Moving an existing send to another bus keeps the Send window as it was.
#[test]
fn changing_a_sends_destination_leaves_the_send_window_alone() {
let _palette = crate::theme::lock_test_palette();
let (mut h, kick) = kick_send_harness(true);
h.run();
h.get_by_label("Send A for Kick").click();
h.run();
h.get_by_label("Aux").click();
h.run();
let (app, _) = h.state();
let target = app.engine.session().track(kick).and_then(|t| t.mixer.sends.first().cloned().flatten()).map(|s| route_name(app, &s.target));
assert_eq!(target.as_deref(), Some("Aux"), "the send moved");
assert_eq!(app.ui.send_window, None, "no window opened");
}
}
+4 -24
View File
@@ -46,28 +46,9 @@ pub fn tracks_and_groups(app: &mut SoundApp, ui: &mut Ui) {
if crate::widgets::icon_button(&mut ui.new_child(egui::UiBuilder::new().max_rect(plus)), plus.size(), "plus", false, &tip).clicked() {
let _ = app.dialogs.open_for_command(&app.engine, "track.new");
}
let tracks: Vec<(u64, String, bool, [u8; 3], bool, usize)> = {
let tracks: Vec<(u64, String, bool, [u8; 3], bool)> = {
let s = app.engine.session();
s.tracks
.iter()
.map(|x| {
let mut current = x.folder;
let mut depth = 0;
let mut seen = Vec::new();
while let Some(folder_id) = current {
if seen.contains(&folder_id) {
break;
}
seen.push(folder_id);
let Some(folder) = s.track(folder_id) else {
break;
};
depth += 1;
current = folder.folder;
}
(x.id.0, x.name.clone(), !x.hidden, x.color, s.edit.selected_tracks.contains(&x.id), depth)
})
.collect()
s.tracks.iter().map(|x| (x.id.0, x.name.clone(), !x.hidden, x.color, s.edit.selected_tracks.contains(&x.id))).collect()
};
let hidden = egui::scroll_area::ScrollBarVisibility::AlwaysHidden;
let lists = with_pending_offset(egui::ScrollArea::vertical().id_salt("tracks_scroll"), ui, "tracks_scroll")
@@ -75,7 +56,7 @@ pub fn tracks_and_groups(app: &mut SoundApp, ui: &mut Ui) {
.auto_shrink([false, false])
.scroll_bar_visibility(hidden);
let out = lists.show(ui, |ui| {
for (id, name, shown, color, sel, depth) in tracks {
for (id, name, shown, color, sel) in tracks {
let (r, resp) = ui.allocate_exact_size(vec2(ui.available_width(), 17.0), Sense::click_and_drag());
resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::Button, true, sel, &name));
if sel {
@@ -89,8 +70,7 @@ pub fn tracks_and_groups(app: &mut SoundApp, ui: &mut Ui) {
}
ui.painter().rect_filled(Rect::from_min_size(pos2(r.min.x + 20.0, r.min.y + 3.0), vec2(4.0, 11.0)), 0.0, rgb(color));
ui.painter().with_clip_rect(r).text(
// Indent stops growing at eight levels so a deep name keeps room in the column.
pos2(r.min.x + 30.0 + depth.min(8) as f32 * 10.0, r.center().y),
pos2(r.min.x + 30.0, r.center().y),
Align2::LEFT_CENTER,
&name,
regular(11.5),
+9 -32
View File
@@ -6,7 +6,7 @@ use crate::{SoundApp, icons};
use egui::{Align2, Color32, CornerRadius, Rect, Sense, Stroke, StrokeKind, Ui, pos2, vec2};
use serde_json::json;
use soundcraft_engine::edit::{SHUFFLE_LOCK, SNAP_TO_GRID};
use soundcraft_model::{EditMode, EditState, Tool};
use soundcraft_model::{EditMode, Tool};
use soundcraft_time::{TimeFormat, format_length};
pub const HEIGHT: f32 = 78.0;
@@ -205,7 +205,14 @@ fn tools(app: &mut SoundApp, ui: &mut Ui) {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 2.0;
let e = app.engine.session().edit.clone();
for (id, icon, on, tip) in option_toggles(&e) {
let toggles: [(&str, &str, bool, &str); 5] = [
("options.tab_to_transient", "tab_transient", e.tab_to_transient, "Tab to Transient"),
("options.link_timeline_edit", "link", e.link_timeline_edit, "Link Timeline and Edit Selection"),
("options.link_track_edit", "link", e.link_track_edit, "Link Track and Edit Selection"),
("options.mirrored_midi", "note", e.mirrored_midi, "Mirrored MIDI Editing"),
("options.automation_follows_edit", "wave", e.automation_follows_edit, "Automation Follows Edit"),
];
for (id, icon, on, tip) in toggles {
if icon_button(ui, vec2(36.0, 18.0), icon, on, tip).clicked() {
let _ = app.run(id, json!({}));
}
@@ -220,18 +227,6 @@ fn tools(app: &mut SoundApp, ui: &mut Ui) {
});
}
/// The Edit-window toolbar's option toggles: (command id, icon, on, tooltip).
fn option_toggles(e: &EditState) -> [(&'static str, &'static str, bool, &'static str); 6] {
[
("options.tab_to_transient", "tab_transient", e.tab_to_transient, "Tab to Transient"),
("options.link_timeline_edit", "link", e.link_timeline_edit, "Link Timeline and Edit Selection"),
("options.link_track_edit", "link_track", e.link_track_edit, "Link Track and Edit Selection"),
("options.insertion_follows_playback", "insertion_follows", e.insertion_follows_playback, "Insertion Follows Playback"),
("options.mirrored_midi", "note", e.mirrored_midi, "Mirrored MIDI Editing"),
("options.automation_follows_edit", "wave", e.automation_follows_edit, "Automation Follows Edit"),
]
}
fn fmt_pos(app: &SoundApp, at: i64, f: TimeFormat) -> String {
let s = app.engine.session();
s.format_position(at, f)
@@ -462,21 +457,3 @@ fn output_meter(app: &mut SoundApp, ui: &mut Ui) {
crate::widgets::meter(ui, rr, m.level[c], m.hold[c], m.clip);
}
}
#[cfg(test)]
mod tests {
use super::*;
/// #1262: the two link toggles differ only by their icon, so each option toggle needs its own.
#[test]
fn option_toggles_have_distinct_icons() {
let toggles = option_toggles(&EditState::default());
let timeline = toggles.iter().find(|t| t.0 == "options.link_timeline_edit").unwrap();
let track = toggles.iter().find(|t| t.0 == "options.link_track_edit").unwrap();
assert_ne!(timeline.1, track.1, "the timeline and track links need different icons");
let mut icons: Vec<&str> = toggles.iter().map(|t| t.1).collect();
icons.sort_unstable();
icons.dedup();
assert_eq!(icons.len(), toggles.len(), "every option toggle needs its own icon");
}
}
+1 -29
View File
@@ -1,6 +1,5 @@
//! A send's level opens its Send window, as in Pro Tools: a fader with Alt-click for 0 dB, moved to
//! another send by clicking that send's level, closed by clicking the same one again. Creating a send
//! opens it too.
//! another send by clicking that send's level, closed by clicking the same one again.
use egui::{Event, Modifiers, PointerButton, Pos2, vec2};
use egui_kittest::Harness;
@@ -76,33 +75,6 @@ fn a_sends_level_opens_its_send_window() {
assert!(h.query_by_label_contains("Vox · Send").is_none());
}
/// Creating a send opens its Send window, so the new send's fader is on screen right away (#1269). The
/// window moves there from the send it was showing, and undoing the new send closes it.
#[test]
fn a_new_send_shows_its_fader_at_once() {
let (mut h, vox) = harness();
h.get_by_label("Send A level for Vox").click();
h.run_steps(3);
assert!(h.query_by_label_contains("Vox · Send A").is_some());
h.get_by_label("Send C for Vox").click();
h.run_steps(3);
// Send A's own "Verb" label is on screen too, so pick the menu's button.
h.get_by_role_and_label(egui::accesskit::Role::Button, "Verb").click();
h.run_steps(3);
assert!(h.state().engine.session().tracks[0].mixer.sends[2].is_some(), "send C was created");
assert_eq!(h.state().ui.send_window, Some((vox, 2)));
assert!(h.query_by_label_contains("Vox · Send C").is_some(), "the window shows the new send");
assert!(h.query_by_label_contains("Vox · Send A").is_none(), "and no longer send A");
assert!(h.query_by_label("Send level").is_some(), "the new send's fader is on screen");
h.state_mut().engine.execute("edit.undo", &json!({})).unwrap();
h.run_steps(3);
assert!(h.state().engine.session().tracks[0].mixer.sends[2].is_none(), "the send was undone");
assert_eq!(h.state().ui.send_window, None, "undoing the send closes its window");
assert!(h.query_by_label_contains("Vox · Send").is_none());
}
/// Escape first closes a send menu or cancels level typing, then a further Escape closes the send window.
#[test]
fn escape_closes_the_send_window_after_menus_and_typing() {
-42
View File
@@ -50,16 +50,6 @@ impl Rig {
.expect("row not in the Tracks list")
}
fn text_x(&self, name: &str) -> f32 {
self.shapes
.iter()
.find_map(|c| match &c.shape {
Shape::Text(t) if t.galley.text() == name && t.pos.x < 150.0 => Some(t.pos.x),
_ => None,
})
.expect("text not in the Tracks list")
}
fn press(&mut self, at: Pos2, pressed: bool) {
self.frame(vec![
Event::PointerMoved(at),
@@ -100,35 +90,3 @@ fn clicking_a_track_list_row_still_selects_the_track() {
r.press(at, false);
assert_eq!(r.app.engine.session().edit.selected_tracks, vec![r.order()[2]]);
}
#[test]
fn a_track_inside_a_folder_is_indented_in_the_track_list() {
let mut r = Rig::new();
let child = r.order()[1];
let child_name = r.app.engine.session().track(child).unwrap().name.clone();
r.app.run("track.move_to_new_folder", json!({"tracks": [child.0], "name": "Folder"})).unwrap();
r.frame(vec![]);
let folder = r.app.engine.session().tracks.iter().find(|track| track.is_folder()).unwrap();
assert!(r.text_x(&child_name) > r.text_x(&folder.name), "child is not indented under the folder");
}
#[test]
fn deeply_nested_tracks_keep_indenting_past_six_folders() {
let mut r = Rig::new();
let child = r.order()[1];
let child_name = r.app.engine.session().track(child).unwrap().name.clone();
r.app.run("track.move_to_new_folder", json!({"tracks": [child.0], "name": "Folder 1"})).unwrap();
let mut parent = r.app.engine.session().tracks.iter().find(|track| track.name == "Folder 1").unwrap().id;
for depth in 2..=7 {
r.app.run("track.move_to_new_folder", json!({"tracks": [parent.0], "name": format!("Folder {depth}")})).unwrap();
parent = r.app.engine.session().tracks.iter().find(|track| track.name == format!("Folder {depth}")).unwrap().id;
}
r.frame(vec![]);
// "Folder 1" is the child's own folder and the seventh level; "Folder 7" is the outermost.
assert_eq!(r.app.engine.session().track(parent).unwrap().name, "Folder 7");
assert!(r.text_x(&child_name) > r.text_x("Folder 1"), "deeply nested child is not indented under its own folder");
assert!(r.text_x("Folder 1") > r.text_x("Folder 2"), "each level indents further");
}
+7 -11
View File
@@ -75,8 +75,6 @@ pub(crate) struct Instance {
pub(super) active: bool,
pub(super) processing: bool,
pub(super) max_frames: usize,
/// `kRealtime` or `kOffline`, for `setupProcessing` and every `process` call.
pub(super) process_mode: i32,
pub(super) in_params: ComWrapper<ParamChanges>,
pub(super) out_params: ComWrapper<ParamChanges>,
pub(super) in_events: ComWrapper<EventList>,
@@ -253,12 +251,6 @@ impl Instance {
self.active
}
/// Offline (`kOffline`: no realtime pressure, e.g. a bounce) or realtime processing, from the
/// next [`Instance::configure`] on.
pub fn set_offline(&mut self, offline: bool) {
self.process_mode = if offline { sv::ProcessModes_::kOffline } else { sv::ProcessModes_::kRealtime } as i32;
}
fn bus_count(&self, media: sv::MediaTypes, dir: sv::BusDirections) -> i32 {
// SAFETY: valid component.
unsafe { self.component.getBusCount(media as sv::MediaType, dir as sv::BusDirection) }.max(0)
@@ -367,8 +359,12 @@ impl Instance {
return Err(Vst3Error::Process("plugin cannot process 32-bit float".into()));
}
let max = i32::try_from(max_frames).unwrap_or(i32::MAX);
let mut setup =
sv::ProcessSetup { processMode: self.process_mode, symbolicSampleSize: sample32, maxSamplesPerBlock: max, sampleRate: sample_rate };
let mut setup = sv::ProcessSetup {
processMode: sv::ProcessModes_::kRealtime as i32,
symbolicSampleSize: sample32,
maxSamplesPerBlock: max,
sampleRate: sample_rate,
};
// SAFETY: valid processor, inactive; `setup` is a valid struct for the call.
if !ok(unsafe { p.setupProcessing(&mut setup) }) {
return Err(Vst3Error::Process("setupProcessing failed".into()));
@@ -454,7 +450,7 @@ impl Instance {
None => self.context.projectTimeSamples = project_time,
}
let mut data = sv::ProcessData {
processMode: self.process_mode,
processMode: sv::ProcessModes_::kRealtime as i32,
symbolicSampleSize: sv::SymbolicSampleSizes_::kSample32 as i32,
numSamples: i32::try_from(frames).unwrap_or(0),
numInputs: i32::try_from(inputs.bufs.len()).unwrap_or(0),
+3 -14
View File
@@ -137,16 +137,6 @@ mod cf {
}
}
/// A libloading error with the system's reason. libloading's own text is only "dlopen failed"
/// (or "dlsym failed", "LoadLibraryExW failed"); the reason (`dlerror`, the Windows error) is its
/// source.
fn with_reason(e: &libloading::Error) -> String {
match std::error::Error::source(e) {
Some(why) => format!("{e}: {why}"),
None => e.to_string(),
}
}
/// Opens the shared library and runs the platform module entry (`bundleEntry` with a
/// `CFBundleRef` on macOS, `InitDll` on Windows, `ModuleEntry` with the `dlopen` handle on Linux).
/// Also returns the matching module exit, when the entry ran and the module exports one.
@@ -156,7 +146,7 @@ fn open_library(bundle: &Path, exe: &Path) -> Result<(libloading::Library, Optio
use libloading::os::unix::{Library as UnixLib, RTLD_LOCAL, RTLD_NOW};
// SAFETY: loading a shared library runs its static initialisers. That is inherent to
// hosting plugins; we only load files found in VST3 plugin folders or named explicitly.
let lib = unsafe { UnixLib::open(Some(exe), RTLD_NOW | RTLD_LOCAL) }.map_err(|e| with_reason(&e))?;
let lib = unsafe { UnixLib::open(Some(exe), RTLD_NOW | RTLD_LOCAL) }.map_err(|e| e.to_string())?;
type ModuleEntry = unsafe extern "C" fn(*mut c_void) -> bool;
// SAFETY: `ModuleEntry` is the VST3-mandated Linux entry with this signature.
let entry = unsafe { lib.get::<ModuleEntry>(b"ModuleEntry\0") }.ok().map(|s| *s);
@@ -180,7 +170,7 @@ fn open_library(bundle: &Path, exe: &Path) -> Result<(libloading::Library, Optio
{
// SAFETY: loading a shared library runs its static initialisers. That is inherent to
// hosting plugins; we only load files found in VST3 plugin folders or named explicitly.
let lib = unsafe { libloading::Library::new(exe) }.map_err(|e| with_reason(&e))?;
let lib = unsafe { libloading::Library::new(exe) }.map_err(|e| e.to_string())?;
let mut exit: Option<ModuleExit> = None;
#[cfg(windows)]
{
@@ -345,7 +335,7 @@ impl Bundle {
// SAFETY: `GetPluginFactory` is the VST3-mandated export with this signature.
let get = match unsafe { lib.get::<GetFactory>(b"GetPluginFactory\0") } {
Ok(sym) => *sym,
Err(e) => return Err(fail(format!("not a VST3 plugin (no GetPluginFactory): {}", with_reason(&e)))),
Err(e) => return Err(fail(format!("not a VST3 plugin: {e}"))),
};
// SAFETY: the module is loaded and its entry function (if any) has run.
let raw = unsafe { get() };
@@ -459,7 +449,6 @@ impl Bundle {
active: false,
processing: false,
max_frames: 0,
process_mode: sv::ProcessModes_::kRealtime as i32,
in_params: ComWrapper::new(ParamChanges::new(PARAM_QUEUE_CAP)),
out_params: ComWrapper::new(ParamChanges::new(PARAM_QUEUE_CAP)),
in_events: ComWrapper::new(EventList::new(EVENT_CAP)),
-16
View File
@@ -307,21 +307,6 @@ mod tests {
let _ = std::fs::remove_dir_all(&d);
}
/// A binary the system refuses to load reports the system's reason (missing library, wrong
/// glibc, not an ELF file), not just "dlopen failed".
#[test]
fn a_binary_that_does_not_load_says_why() {
let d = std::env::temp_dir().join(format!("soundcraft-vst3-reason-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(&d).unwrap();
let f = d.join("garbage.vst3");
std::fs::write(&f, b"\x7fELF this is not a plugin \x00\x01\x02").unwrap();
let msg = load_bundle(&f).err().map(|e| e.to_string()).unwrap_or_default();
let reason = msg.split_once("failed: ").map_or("", |(_, r)| r.trim());
assert!(!reason.is_empty(), "no reason in: {msg}");
let _ = std::fs::remove_dir_all(&d);
}
// Unix only: the test points a ".vst3" at a system library through a symlink.
#[cfg(unix)]
#[test]
@@ -341,7 +326,6 @@ mod tests {
std::os::unix::fs::symlink(&lib, d.join("z.vst3")).unwrap();
let e = load_bundle(&d.join("z.vst3")).unwrap_err();
assert!(e.to_string().contains("not a VST3 plugin"), "{e}");
assert!(e.to_string().contains("GetPluginFactory"), "the system's reason names the missing export: {e}");
let _ = std::fs::remove_dir_all(&d);
}
}
-5
View File
@@ -259,11 +259,6 @@ impl Plugin for Vst3Plugin {
}
}
fn set_offline(&mut self, offline: bool) {
// `processMode`, set up by the next `prepare`.
self.inst.set_offline(offline);
}
fn reset(&mut self) {
// Keep pending releases and release whatever is still held, so the processor's voices stop
// on the next block instead of sounding on while the host forgets them.
+7 -12
View File
@@ -14,9 +14,8 @@
//! - "Fixture Wide 16x64", "Fixture Wide 32 buses", "Fixture Wide 65 buses", "Fixture Wide 65
//! channels" (`Fx`): effects advertising wide layouts (16 buses of 64 channels, 32 and 65 buses
//! of 2 channels, one bus of 65 channels per direction), through `getBusInfo` only. `process` writes 1.0 into every channel
//! of output bus 0 when the host's buffers match the advertised layout (2.0 when the host set it
//! up and runs it with `kOffline`), -1.0 when they do not or when `process` gets another
//! `processMode` than `setupProcessing` did. Report 3 samples of latency.
//! of output bus 0 when the host's buffers match the advertised layout, -1.0 when they do not.
//! Report 3 samples of latency.
//!
//! The module checks the host's module lifecycle the way real plugins depend on it (see "module
//! lifecycle" below) and aborts the process when it is broken.
@@ -26,7 +25,7 @@
#![allow(non_snake_case, clippy::missing_safety_doc)]
use std::ffi::{c_char, c_int, c_void};
use std::sync::atomic::{AtomicBool, AtomicI32, AtomicI64, AtomicU32, AtomicU64, AtomicUsize, Ordering};
use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU32, AtomicU64, AtomicUsize, Ordering};
use vst3::Steinberg::Vst::*;
use vst3::Steinberg::*;
use vst3::{Class, ComRef, ComWrapper, uid};
@@ -641,8 +640,6 @@ impl IEditControllerTrait for Synth {
struct Wide {
buses: i32,
channels: i32,
/// The `processMode` of the last `setupProcessing`.
mode: AtomicI32,
_live: Live,
}
@@ -707,8 +704,7 @@ impl IAudioProcessorTrait for Wide {
unsafe fn getLatencySamples(&self) -> u32 {
3
}
unsafe fn setupProcessing(&self, setup: *mut ProcessSetup) -> tresult {
self.mode.store(unsafe { &*setup }.processMode, Ordering::Relaxed);
unsafe fn setupProcessing(&self, _setup: *mut ProcessSetup) -> tresult {
kResultOk
}
unsafe fn setProcessing(&self, _state: TBool) -> tresult {
@@ -718,11 +714,10 @@ impl IAudioProcessorTrait for Wide {
let d = unsafe { &*data };
let fits =
|bufs: *mut AudioBusBuffers, n: i32| n == self.buses && (0..n as usize).all(|i| unsafe { &*bufs.add(i) }.numChannels == self.channels);
let ok = fits(d.inputs, d.numInputs) && fits(d.outputs, d.numOutputs) && d.processMode == self.mode.load(Ordering::Relaxed);
let fit = if d.processMode == ProcessModes_::kOffline as i32 { 2.0 } else { 1.0 };
let ok = fits(d.inputs, d.numInputs) && fits(d.outputs, d.numOutputs);
if d.numOutputs > 0 {
for ch in unsafe { channels(&*d.outputs, d.numSamples.max(0) as usize) } {
ch.fill(if ok { fit } else { -1.0 });
ch.fill(if ok { 1.0 } else { -1.0 });
}
}
kResultOk
@@ -796,7 +791,7 @@ impl IPluginFactoryTrait for Factory {
.into_iter()
.find_map(|(c, layout)| (c == cid).then_some(layout))
{
ComWrapper::new(Wide { buses, channels, mode: AtomicI32::new(-1), _live: Live::new(&OBJECTS) }).to_com_ptr::<FUnknown>()
ComWrapper::new(Wide { buses, channels, _live: Live::new(&OBJECTS) }).to_com_ptr::<FUnknown>()
} else {
None
};
-12
View File
@@ -331,18 +331,6 @@ fn layouts_beyond_the_host_limits_are_rejected_not_truncated() {
}
}
#[test]
fn offline_renders_set_up_and_process_in_offline_mode() {
for (offline, want) in [(false, 1.0), (true, 2.0)] {
let mut p = soundcraft_vst3_host::create(&id_of("Fixture Wide 16x64")).unwrap();
p.set_offline(offline);
p.prepare(44_100.0, 64, 2);
let mut io = vec![vec![0.5f32; 64]; 2];
p.process(&mut io, 64);
assert!(io.iter().flatten().all(|&x| x == want), "offline {offline}: {:?}", &io[0][..4]);
}
}
#[test]
fn many_buses_like_multi_out_instruments_are_activated() {
// Multi-out instruments declare dozens of buses (SSD Sampler 5: 32); only the main one is
+3 -1
View File
@@ -38,7 +38,7 @@ Results go to standard output, as JSON where a program is meant to read them. `i
file), `describe`, `app` and the `--inspect` output of `run` and `script` pretty-print it; `--compact`
prints the same JSON on one line. The result lines of `run` and `script` (`ID: JSON`) and the
`--bounce` and `--save` results are always one line each. `commands`, `plugins`, `parity` and the
`.scraft` summary of `info` are text. Errors go to standard error as `error: <message>`, and so do the warnings and errors SoundCraft and the plugin hosts log (`warning: <message>`, `error: <message>`), such as a plugin that fails to load. `mcp` writes only the protocol
`.scraft` summary of `info` are text. Errors go to standard error as `error: <message>`. `mcp` writes only the protocol
to standard output.
| Exit code | Meaning |
@@ -77,6 +77,8 @@ link stays. An existing file that is not an input is replaced without asking.
|---|---|---|
| `SOUNDCRAFT_CONTROL_PORT` | `app --port` (CLI), `--control` (desktop app) | A port or `host:port`, an IPv6 host in brackets (`[::1]:7979`). The desktop app takes the port of a loopback `host:port` (`127.0.0.1`, `localhost`). Its control server listens on `127.0.0.1` only, so it ignores another host from the variable, with a log warning, and starts no control server; given to `--control`, another host is a usage error |
| `SOUNDCRAFT_CONTROL_TOKEN` | none | The secret every control request carries as `token`. The desktop app refuses requests without it; unset, it makes one per launch and prints it after the port line (`SOUNDCRAFT_CONTROL_TOKEN=…`). `app` and `mcp --connect` send the variable's value |
- A flag wins over its variable.
- An empty variable counts as unset.
- A bad value is a usage error that names the variable.
+8 -2
View File
@@ -6,7 +6,7 @@ input), and every command is reachable from:
| Surface | How |
|---|---|
| Desktop app control channel | `soundcraft --control <port>` (`0` = pick a free port; printed as `SOUNDCRAFT_CONTROL_PORT=…`) |
| Desktop app control channel | `soundcraft --control <port>` (`0` = pick a free port; printed as `SOUNDCRAFT_CONTROL_PORT=…`, then the launch's `SOUNDCRAFT_CONTROL_TOKEN=…` unless the variable was set) |
| CLI against a running app | `soundcraft-cli app --port <port> <method or command id> [JSON]` (see [cli.md](cli.md)) |
| Headless CLI | `soundcraft-cli run --demo --cmd 'mix.volume={"track":"Kick","db":-6}' --bounce out.wav` |
| MCP (headless) | `soundcraft-cli mcp [--demo]` |
@@ -29,12 +29,17 @@ input delivery or screen-reader acceptance. Native screenshots use the presented
Newline-delimited JSON over TCP on `127.0.0.1`. One request per line, one reply per line:
```json
{"id": 1, "method": "engine.execute", "params": {"command": "track.new", "params": {"count": 2, "format": "Stereo"}}}
{"id": 1, "method": "engine.execute", "params": {"command": "track.new", "params": {"count": 2, "format": "Stereo"}}, "token": "…"}
{"id": 1, "ok": true, "result": {"tracks": [12, 13]}}
```
Errors: `{"id": 1, "ok": false, "error": "bad parameters for `mix.volume`: no track named `Nope`"}`.
`token` is the launch's secret: `SOUNDCRAFT_CONTROL_TOKEN` when the app was started with it, otherwise
the one the app made and printed after the port line. Any local account can reach the loopback port,
so a request without the right token is refused (`"error": "unauthorized: …"`) and its connection
closed. `soundcraft-cli app` and `mcp --connect` read the variable.
## Methods
| Method | Params | What it does |
@@ -71,6 +76,7 @@ Scripted input (`ui.click`, `ui.drag`, `ui.scroll`, `ui.zoom`, `ui.key`, `ui.tex
## Examples
```sh
export SOUNDCRAFT_CONTROL_TOKEN=$(head -c 32 /dev/urandom | od -An -tx1 | tr -d ' \n')
P=$(soundcraft --demo --control 0 | sed -n 's/SOUNDCRAFT_CONTROL_PORT=//p') # or read stderr
soundcraft-cli app --port $P mix.solo '{"track":"Bass"}'
soundcraft-cli app --port $P transport.play
+2 -3
View File
@@ -1,6 +1,6 @@
# File-format parity
> **Last reviewed:** 2026-10-19 · **Last updated:** 2026-11-02 · **Change:** trivial (bounces carry a BWF time reference, #1296; previously trivial: Float32 file headroom, #791) · **Target:** Avid Pro Tools Ultimate 2026.4.1
> **Last reviewed:** 2026-10-19 · **Last updated:** 2026-10-19 · **Change:** trivial (Float32 file headroom, #791) · **Target:** Avid Pro Tools Ultimate 2026.4.1
Every format Pro Tools reads or writes, and ours. The Pro Tools list comes from the document types
its bundle declares (`CFBundleDocumentTypes` in Info.plist: names only, nothing else read) and its
@@ -33,7 +33,7 @@ Legend: ✅ supported and tested · 🟡 partial · ❌ missing · — not appli
| Format | Pro Tools | Read | Write | Tests / notes |
|---|---|---|---|---|
| WAV / BWF (PCM 16/24/32, float 32), `bext` | native | ✅ | ✅ | Native reader/writer, TPDF dither option; Float32 retains finite headroom (#791); also 8-bit and float 64 read. An empty data chunk followed by metadata reads as no audio; an unfinalised recording's audio is still read (#466). WAV bounces (mix and stems) carry a `bext` TimeReference: the session's timecode start plus the bounce start (#1296). |
| WAV / BWF (PCM 16/24/32, float 32), `bext` | native | ✅ | ✅ | Native reader/writer, TPDF dither option; Float32 retains finite headroom (#791); also 8-bit and float 64 read. An empty data chunk followed by metadata reads as no audio; an unfinalised recording's audio is still read (#466). |
| RF64 / BW64 (> 4 GiB) | read/write | ✅ | ✅ | `encode_wav_rf64` |
| WAV `WAVE_FORMAT_EXTENSIBLE` multichannel with channel mask | read/write | ✅ | ✅ | `encode_with_channel_mask`; default 5.1 import creates a stereo track (#89, PR #128) |
| AIFF / AIFF-C | native | ✅ | ✅ (`fl32`) | Native; `fl32` retains finite headroom (#791) |
@@ -80,7 +80,6 @@ Legend: ✅ supported and tested · 🟡 partial · ❌ missing · — not appli
| Date | Change | Summary |
|---|---|---|
| 2026-11-02 | trivial | WAV bounces (mix and stems) write a `bext` TimeReference: session timecode start plus bounce start (#1296) |
| 2026-10-19 | trivial | WAV and AIFF-C Float32 encoders retain finite headroom (#791); whole-file, streaming and bounce regression tests |
| 2026-10-13 | trivial | WAV: empty data chunk with trailing metadata (#466) |
| 2026-10-13 | minor | Fade curvature round-trips in sessions with a zero default for older files. Format version and readiness estimate unchanged |
+1
View File
@@ -8,6 +8,7 @@ Claude and other agents can make music with it. The server is the `mcp` subcomma
soundcraft-cli mcp # headless engine (no window, no audio device)
soundcraft-cli mcp --demo # headless, starting from the demo session
soundcraft-cli mcp --automation-read-root DIR --automation-write-root DIR # headless, confined to folders
export SOUNDCRAFT_CONTROL_TOKEN=… # the secret both sides share (see control-protocol.md)
soundcraft --control 7801 & # the desktop app…
soundcraft-cli mcp --connect 7801 # …driven over MCP: same tools plus screenshots and clicks
```