fix sample preview crashes

This commit is contained in:
🪞👃🪞 2024-07-23 18:54:46 +03:00
parent bdd3e31403
commit e1eaa51d26
3 changed files with 13 additions and 8 deletions

View file

@ -75,7 +75,7 @@ pub const KEYMAP_SAMPLER: &'static [KeyBinding<Sampler>] = 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()));
}

View file

@ -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)
}
}
}
},

View file

@ -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
}
}