From 21e720bb89e494cc89e99169e13fff9bf0dbdc6f Mon Sep 17 00:00:00 2001 From: Grant Shangreaux Date: Fri, 29 Oct 2021 19:44:06 -0500 Subject: Add: pack an Array> of samples and push_buffer - add a method to set the sample rate of the sine wave --- klangfarbrs/src/lib.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'klangfarbrs') diff --git a/klangfarbrs/src/lib.rs b/klangfarbrs/src/lib.rs index 1489667..0628e34 100644 --- a/klangfarbrs/src/lib.rs +++ b/klangfarbrs/src/lib.rs @@ -5,12 +5,14 @@ use std::f32::consts::TAU; #[derive(NativeClass)] #[inherit(Node)] -pub struct SineWave {} +pub struct SineWave { + sample_rate: f32 +} #[methods] impl SineWave { fn new(_owner: &Node) -> Self { - SineWave {} + SineWave { sample_rate: 44100.0 } } #[export] @@ -19,12 +21,17 @@ impl SineWave { } #[export] - pub fn frames(&self, _owner: &Node, frequency: f32, sample_rate: f32, duration: i32) -> TypedArray { + fn set_sample_rate(&mut self, _owner: &Node, sample_rate: f32) { + self.sample_rate = sample_rate; + } + + #[export] + pub fn frames(&self, _owner: &Node, frequency: f32, duration: i32) -> TypedArray { let mut frames = TypedArray::new(); - let calculated_duration = sample_rate * duration as f32; - for i in 0..calculated_duration as i32 { - frames.push((TAU * frequency * i as f32/sample_rate).sin()); + for i in 0..duration as i32 { + let sample = (TAU * frequency * i as f32/self.sample_rate).sin(); + frames.push(Vector2::new(sample, sample)); } return frames -- cgit v1.2.3