diff --git a/src/transform/slice.rs b/src/transform/slice.rs index eadbdfc..7c6a902 100644 --- a/src/transform/slice.rs +++ b/src/transform/slice.rs @@ -1,26 +1,36 @@ use crate::pattern::*; use crate::Vector; -#[derive(derive_more::Constructor)] pub struct SliceFactory { child: Box, - scale: u8, + width: f32, + rest: f32, } #[derive(derive_more::Constructor)] pub struct Slice { child: Box, - scale: u8, + width: f32, + rest: f32, +} + +impl SliceFactory { + pub fn new(child: Box, slices: u8) -> Self { + let width = 1.0 / slices as f32; + let rest = 1.0 - width; + + Self { child, width, rest } + } } impl PatternFactory for SliceFactory { fn create(&self, config: &Config) -> Box { - Box::new(Slice::new(self.child.create(config), self.scale)) + Box::new(Slice::new(self.child.create(config), self.width, self.rest)) } } impl Pattern for Slice { fn sample(&self, pos: Vector) -> f32 { - self.child.sample(pos) * self.scale as f32 + (self.child.sample(pos) - self.rest) / self.width } }