From cb4c321a697cc1f83679f13d615369521d2f8312 Mon Sep 17 00:00:00 2001 From: AlphaNeon <102443376+ItsAlphaNeon@users.noreply.github.com> Date: Thu, 3 Dec 2026 02:21:44 -0700 Subject: [PATCH] Lemon Drum Sampler: reopened kits play each pad its own sample (#467) The host gives the plugin one sample file per layer in state order, the empty pads' layers included, but rebuilding the kit counted only the layers that hold a sample. After a project was reopened (the only time the host fills that list) the k-th sounding layer opened the file of the k-th layer overall, so pads played another pad's sample or nothing. The kit now finds a layer's file from its pad's first layer plus its own place. Regression tests in the plugin (state round trip, every pad's sample and level) and in the session (the real reopen path). Co-Authored-By: Claude Opus 5.5 --- crates/os-session/src/drums.rs | 40 ++++++++++++++- plugins/lemon-drum-sampler/src/lib.rs | 74 +++++++++++++++++++++++++-- 2 files changed, 109 insertions(+), 5 deletions(-) diff --git a/crates/os-session/src/drums.rs b/crates/os-session/src/drums.rs index 0a55301cc..c7d9fe9a1 100644 --- a/crates/os-session/src/drums.rs +++ b/crates/os-session/src/drums.rs @@ -133,10 +133,15 @@ mod tests { /// A 0.1 s stereo 16-bit WAV at 44.1 kHz: a tone. fn wav(name: &str) -> std::path::PathBuf { + wav_at(name, 16000.0) + } + + /// [`wav`] with the tone's peak at `amp` (16-bit units). + fn wav_at(name: &str, amp: f32) -> std::path::PathBuf { let frames = 4410usize; let mut data = Vec::new(); for i in 0..frames { - let v = ((i as f32 * 0.05).sin() * 16000.0) as i16; + let v = ((i as f32 * 0.05).sin() * amp) as i16; data.extend(v.to_le_bytes()); data.extend(v.to_le_bytes()); } @@ -195,4 +200,37 @@ mod tests { let _ = std::fs::remove_file(path); } + /// Issue #467: a project's kit with samples on several pads, opened again, plays each pad's own sample (the files + /// the host gives cover every layer, the empty pads' too). + #[test] + fn a_reopened_kit_plays_each_pad_its_own_sample() { + let st = &crate::settings_file::SettingsStore::memory(); + let (a, b) = (wav_at("reopen-a", 4000.0), wav_at("reopen-b", 16000.0)); + let mut s = State::default(); + s.set_sample(1, &a.to_string_lossy()); + s.set_sample(4, &b.to_string_lossy()); + let fl = s.to_bytes(); + let mut pl = Plugins::default(); + pl.available = crate::plugins::builtin(); + pl.set_rate(48000); + let index = pl.index_of(LEMON_DRUMS).unwrap(); + pl.create_with(1, index, Some(&plugin_state(st, &fl))).unwrap(); + let inst = pl.hosted[&1].plugin.clone() as SharedInstrument; + let mut peaks = Vec::new(); + for pad in [1, 4] { + let key = s.pad(pad).unwrap().key as u8; + let mut i = inst.lock().unwrap(); + i.note_on(0, key, 100.0 / 128.0); + peaks.push(peak(&mut *i)); + // Let the one-shot finish before the next pad. + for _ in 0..4 { + let _ = peak(&mut *i); + } + } + // Pad 2 (index 1) holds the quiet sample, pad 5 the one four times louder. + let ratio = peaks[1] / peaks[0]; + assert!((ratio - 4.0).abs() < 0.4, "pad peaks {peaks:?}: each pad plays its own sample"); + let _ = std::fs::remove_file(a); + let _ = std::fs::remove_file(b); + } } diff --git a/plugins/lemon-drum-sampler/src/lib.rs b/plugins/lemon-drum-sampler/src/lib.rs index 7f4e1f2dd..e96e40528 100644 --- a/plugins/lemon-drum-sampler/src/lib.rs +++ b/plugins/lemon-drum-sampler/src/lib.rs @@ -70,10 +70,19 @@ impl Shared { /// Rebuilds what the voices play from the saved state, opening the samples it names. pub fn rebuild(&self, params: &DrumParams) { let st = params.state(); - let mut n = 0; - let kit = dsp::Kit::new(&st, &mut |_, _, stored| { - let file = Self::file_for(params, n, stored); - n += 1; + // The given files are one per layer in state order, empty layers included (only layers with a sample are + // opened): a layer's file is found by its pad's first layer plus its own place. + let first: Vec = st + .pads() + .iter() + .scan(0, |n, p| { + let at = *n; + *n += p.layers.len(); + Some(at) + }) + .collect(); + let kit = dsp::Kit::new(&st, &mut |i, j, stored| { + let file = Self::file_for(params, first.get(i).map_or(usize::MAX, |f| f + j), stored); self.sample(&file) }); if let Ok(mut k) = self.kit.lock() { @@ -313,6 +322,63 @@ pub(crate) mod tests { let _ = std::fs::remove_file(p); } + /// Issue #467: a kit saved and opened again plays on every pad the sample put on it. The host gives a file for + /// every layer, the empty pads' layers included, so a layer's file is found by its place among all layers. + #[test] + fn a_reopened_kit_plays_each_pad_its_own_sample() { + let files = [(4usize, wav("reopen-a", 0.2)), (1, wav("reopen-b", 0.5)), (9, wav("reopen-c", 0.8))]; + let params = DrumParams::default(); + let sh = Shared::default(); + for (pad, f) in &files { + sh.load(¶ms, *pad, &f.to_string_lossy()).unwrap(); + } + // Saved, then opened as the host opens a project: every layer's stored path given back resolved. + let fl = params.raw_copy(); + let given: Vec = params::stored_paths(&fl); + let blob = params::plugin_state_from_fl(&fl, &given); + let json = std::str::from_utf8(&blob[8..]).unwrap(); + let v: serde_json::Value = serde_json::from_str(json).unwrap(); + let fields: std::collections::BTreeMap = v["fields"] + .as_object() + .unwrap() + .iter() + .map(|(k, v)| (k.clone(), v.as_str().unwrap().to_string())) + .collect(); + let reopened = DrumParams::default(); + reopened.deserialize_fields(&fields); + let sh = Shared::default(); + sh.rebuild(&reopened); + let kit = sh.kit.lock().unwrap().clone().unwrap(); + let st = reopened.state(); + for (pad, f) in &files { + assert_eq!(st.pad(*pad).unwrap().layers[0].path, f.to_string_lossy()); + let want = sh.sample(&f.to_string_lossy()).unwrap(); + let got = sh.pad_sample(*pad).unwrap_or_else(|| panic!("pad {pad} has no sample")); + assert!(Arc::ptr_eq(&got, &want), "pad {pad} plays another pad's sample"); + } + // And by ear: each pad's key sounds at its own sample's level (pads alike but for their samples). + let amps = [0.2f32, 0.5, 0.8]; + let ratios: Vec = files + .iter() + .zip(amps) + .map(|((pad, _), amp)| { + let mut e = dsp::Engine::new(44100.0); + e.set_kit(Some(kit.clone())); + e.note_on(st.pad(*pad).unwrap().key as u8, 1.0, None); + let (mut l, mut r) = (vec![0.0; 2048], vec![0.0; 2048]); + e.render(&mut l, &mut r); + l.iter().fold(0.0f32, |m, v| m.max(v.abs())) / amp + }) + .collect(); + assert!( + ratios[0] > 0.0 && ratios.iter().all(|r| (r / ratios[0] - 1.0).abs() < 0.05), + "peak / sample level per pad: {ratios:?}" + ); + for (_, f) in &files { + let _ = std::fs::remove_file(f); + } + } + /// The host's files for the layers follow their layers through edits. #[test] fn given_files_follow_their_layers() {