Skip to content

Commit 9227ea9

Browse files
committed
refactored code to improve readability
1 parent 1556032 commit 9227ea9

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

.DS_Store

0 Bytes
Binary file not shown.

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
target/
22
.vscode
3+
.DS_Store
4+
.idea/

src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn run() {
7373
.expect("Failed to get default output config")
7474
.config();
7575

76-
let osc2_clone = osc2.clone_osc();
76+
let osc2_clone = osc2.clone();
7777
let sample_rate = config.sample_rate.0 as f32;
7878
let mut samples_played = 0f32;
7979

@@ -114,4 +114,4 @@ pub fn run() {
114114

115115
fn main() {
116116
run();
117-
}
117+
}

src/osc.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
// use std::clone;
22

3-
#[derive(Clone)]
3+
use crate::osc::ShapeMath::{Sawwave, Sinewave, Squarewave, Trianglewave};
4+
5+
#[derive(Debug, Clone, Copy)]
46
pub enum ShapeMath {
57
Sinewave,
68
Squarewave,
79
Sawwave,
810
Trianglewave
911
}
1012

11-
#[derive(Clone)]
13+
#[derive(Debug, Clone)]
1214
pub struct Oscillator {
1315
amp: f32,
1416
freq: f32,
@@ -19,16 +21,16 @@ pub struct Oscillator {
1921
impl ShapeMath {
2022
pub fn compute(&self, freq: f32, time: f32) -> f32 {
2123
match self {
22-
ShapeMath::Sinewave => (2.0 * std::f32::consts::PI * freq * time).sin(),
23-
ShapeMath::Squarewave => (2.0 * std::f32::consts::PI * freq * time).sin().signum(),
24-
ShapeMath::Sawwave => 2.0 * (freq * time - freq * time.floor()) - 1.0,
25-
ShapeMath::Trianglewave => (2.0 * (freq * time - 0.5)).abs() - 1.0,
24+
Self::Sinewave => (2.0 * std::f32::consts::PI * freq * time).sin(),
25+
Self::Squarewave => (2.0 * std::f32::consts::PI * freq * time).sin().signum(),
26+
Self::Sawwave => 2.0 * (freq * time - freq * time.floor()) - 1.0,
27+
Self::Trianglewave => (2.0 * (freq * time - 0.5)).abs() - 1.0,
2628
}
2729
}
2830
}
2931

3032
impl Oscillator {
31-
pub fn new(amp: f32, freq: f32, shape: ShapeMath) -> Oscillator {
33+
pub fn new(amp: f32, freq: f32, shape: ShapeMath) -> Self {
3234
Oscillator {
3335
amp,
3436
freq,
@@ -37,7 +39,7 @@ impl Oscillator {
3739
}
3840
}
3941

40-
pub fn with_input(amp: f32, freq: f32, shape: ShapeMath, input: Oscillator) -> Oscillator {
42+
pub fn with_input(amp: f32, freq: f32, shape: ShapeMath, input: Oscillator) -> Self {
4143
Oscillator {
4244
amp,
4345
freq,
@@ -46,14 +48,14 @@ impl Oscillator {
4648
}
4749
}
4850

49-
pub fn clone_osc(&self) -> Oscillator {
50-
Oscillator {
51-
amp: self.amp,
52-
freq: self.freq,
53-
shape: self.shape.clone(),
54-
input: self.input.clone(),
55-
}
56-
}
51+
// pub fn clone_osc(&self) -> Self {
52+
// Oscillator {
53+
// amp: self.amp,
54+
// freq: self.freq,
55+
// shape: self.shape.clone(),
56+
// input: self.input.clone(),
57+
// }
58+
// }
5759

5860
pub fn frequency_modulation(&self, time: f32) -> f32 {
5961
let input_freq = match &self.input {

0 commit comments

Comments
 (0)