Files
soundcraft/crates/engine/src/cmd/instrument.rs
T
ebbicottandClaude Opus 5.5 ed47ec0fbe Instrument picker: choose an instrument track's instrument and open it
There was no way in the app to choose a CLAP, VST3 or Audio Units instrument,
or to open an instrument's editor:
- the insert menus leave instruments out;
- an instrument was only set by track.new;
- the Mix window's INSTRUMENT row looked for instrument inserts rather than the
  track's instrument;
- plugin windows opened only for insert slots.

Changes:
- mix.instrument {track?, plugin} sets an instrument track's instrument, with
  the plugin's default parameters, and is undoable. It refuses effects and
  other track kinds.
- The INST label on an instrument track's header opens an instrument menu: the
  current instrument and its editor, then the built-in, CLAP, VST3 and Audio
  Units instruments, by vendor.
- The Mix window's INSTRUMENT row shows the track's instrument. A click opens a
  hosted instrument's plugin window, and a right-click changes it.
- Plugin windows open for the instrument slot, with only the editor button,
  because the bypass, preset and parameter commands address insert slots.
  They no longer derive a slot letter for the instrument slot.

Closes #22

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 15:40:37 -04:00

84 lines
3.7 KiB
Rust

//! An instrument track's instrument (`mix.instrument`): the plugin that turns the track's MIDI
//! into audio, built in or hosted (CLAP, VST3, Audio Units). The mixer plays `Track::instrument`;
//! the instrument is otherwise only set when the track is created (`track.new`).
use super::*;
use crate::cmd;
use serde_json::json;
use soundcraft_model::{Insert, TrackKind};
pub fn specs() -> Vec<CommandSpec> {
vec![cmd!(
"mix.instrument",
"Set Instrument",
[],
None,
"{track?, plugin: id} — an instrument track's instrument: built-in, `clap:`, `vst3:` or `au:` id",
has_selection,
set_instrument
)]
}
fn set_instrument(e: &mut Engine, p: &Value) -> Result<Value> {
let plugin = str_param(p, "plugin").ok_or_else(|| bad("mix.instrument", "`plugin` required"))?;
let info = super::clap::plugin_info(plugin).ok_or_else(|| bad("mix.instrument", format!("unknown plugin `{plugin}`")))?;
if !info.is_instrument {
return Err(bad("mix.instrument", format!("`{plugin}` is not an instrument")));
}
let t = tracks_required(e, "mix.instrument", p)?.first().copied().ok_or_else(|| bad("mix.instrument", "no track"))?;
if e.session().track(t).map(|tr| tr.kind) != Some(TrackKind::Instrument) {
return Err(bad("mix.instrument", "only instrument tracks have an instrument"));
}
let mut ins = Insert::new(info.id);
for pi in info.params {
ins.params.insert(pi.id.to_string(), pi.default);
}
if let Some(tr) = e.session_mut().track_mut(t) {
tr.instrument = Some(ins);
}
Ok(json!({"track": t, "plugin": info.id}))
}
#[cfg(test)]
mod tests {
use crate::Engine;
use serde_json::json;
use soundcraft_model::TrackId;
fn track(e: &mut Engine, kind: &str) -> TrackId {
e.execute("track.new", &json!({"kind": kind})).unwrap();
e.session().tracks.last().unwrap().id
}
fn instrument_of(e: &Engine, t: TrackId) -> Option<String> {
e.session().track(t).and_then(|tr| tr.instrument.as_ref()).map(|i| i.plugin.clone())
}
#[test]
fn sets_an_instrument_tracks_instrument_with_defaults_and_undoes() {
let mut e = Engine::default();
let t = track(&mut e, "instrument");
assert_eq!(instrument_of(&e, t).as_deref(), Some("subtractive_synth"));
let r = e.execute("mix.instrument", &json!({"track": t.0, "plugin": "drum_synth"})).unwrap();
assert_eq!(r["plugin"], "drum_synth");
assert_eq!(instrument_of(&e, t).as_deref(), Some("drum_synth"));
let params = e.session().track(t).and_then(|tr| tr.instrument.as_ref()).map(|i| i.params.len());
assert_eq!(params, soundcraft_dsp::plugin_info("drum_synth").map(|p| p.params.len()), "every parameter at its default");
e.execute("edit.undo", &json!({})).unwrap();
assert_eq!(instrument_of(&e, t).as_deref(), Some("subtractive_synth"));
}
#[test]
fn refuses_effects_unknown_plugins_and_other_track_kinds() {
let mut e = Engine::default();
let inst = track(&mut e, "instrument");
let audio = track(&mut e, "audio");
assert!(e.execute("mix.instrument", &json!({"track": inst.0, "plugin": "eq_7band"})).is_err(), "an effect");
assert!(e.execute("mix.instrument", &json!({"track": inst.0, "plugin": "vst3:00000000000000000000000000000000"})).is_err());
assert!(e.execute("mix.instrument", &json!({"track": inst.0})).is_err(), "no plugin");
assert!(e.execute("mix.instrument", &json!({"track": audio.0, "plugin": "drum_synth"})).is_err(), "an audio track");
assert_eq!(instrument_of(&e, inst).as_deref(), Some("subtractive_synth"));
assert_eq!(instrument_of(&e, audio), None);
}
}