From e1eaa51d26566c87f842bce3efbbded789c016e1 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Tue, 23 Jul 2024 18:54:46 +0300 Subject: [PATCH] fix sample preview crashes --- src/devices/sampler.rs | 2 +- src/devices/sampler/add_sample.rs | 16 +++++++++++----- src/devices/sampler/voice.rs | 3 +-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/devices/sampler.rs b/src/devices/sampler.rs index 387400b2..b79b7a53 100644 --- a/src/devices/sampler.rs +++ b/src/devices/sampler.rs @@ -75,7 +75,7 @@ pub const KEYMAP_SAMPLER: &'static [KeyBinding] = keymap!(Sampler { state.cursor.0 = (state.cursor.0 + 1) % (state.mapped.len() + state.unmapped.len()); Ok(true) }], - [Char('t'), NONE, "/sampler/play", "play current sample", |state: &mut Sampler| { + [Char('p'), NONE, "/sampler/play", "play current sample", |state: &mut Sampler| { if let Some(sample) = state.sample() { state.voices.write().unwrap().push(Sample::play(sample, 0, &100.into())); } diff --git a/src/devices/sampler/add_sample.rs b/src/devices/sampler/add_sample.rs index b05aef58..3ba84552 100644 --- a/src/devices/sampler/add_sample.rs +++ b/src/devices/sampler/add_sample.rs @@ -255,11 +255,17 @@ impl Sample { while sample.channels.len() < spec.channels.count() { sample.channels.push(vec![]); } - let mut samples = SampleBuffer::new(decoded.frames() as u64, spec); - samples.copy_interleaved_ref(decoded); - for frame in samples.samples().chunks(spec.channels.count()) { - for (chan, frame) in frame.iter().enumerate() { - sample.channels[chan].push(*frame) + // Load sample + let mut samples = SampleBuffer::new( + decoded.frames() as u64, + spec + ); + if samples.capacity() > 0 { + samples.copy_interleaved_ref(decoded); + for frame in samples.samples().chunks(spec.channels.count()) { + for (chan, frame) in frame.iter().enumerate() { + sample.channels[chan].push(*frame) + } } } }, diff --git a/src/devices/sampler/voice.rs b/src/devices/sampler/voice.rs index ac9e9dcf..baa3a1b5 100644 --- a/src/devices/sampler/voice.rs +++ b/src/devices/sampler/voice.rs @@ -20,7 +20,7 @@ impl Iterator for Voice { if self.position < sample.end { let position = self.position; self.position = self.position + 1; - return Some([ + return sample.channels[0].get(position).map(|amplitude|[ sample.channels[0][position] * self.velocity, sample.channels[0][position] * self.velocity, ]) @@ -28,4 +28,3 @@ impl Iterator for Voice { None } } -