@@ -5,11 +5,15 @@ const PRECISION: u64 = 1_000_000;
5
5
6
6
fn get_max_value (bit_depth : u16 ) -> u64 {
7
7
if bit_depth != 4 && bit_depth != 8 && bit_depth != 16 && bit_depth != 24 && bit_depth != 32 {
8
- panic! (" Unsupported bit depth" );
8
+ panic! (" Unsupported bit depth" );
9
+ } else if bit_depth == 4 {
10
+ return 15 ;
11
+ } else if bit_depth == 8 {
12
+ return 255 ;
9
13
}
10
14
let mut n = bit_depth ;
11
15
let mut result : u64 = 1 ;
12
- while n > 0 {
16
+ while n > 1 {
13
17
result *= 2 ;
14
18
n -= 1 ;
15
19
};
@@ -18,20 +22,23 @@ fn get_max_value(bit_depth: u16) -> u64 {
18
22
// TODO: Test append to array version for efficiency
19
23
20
24
// TODO
21
- fn generate_sine_wave (frequency_hz : u32 , duration_ms : u32 , sample_rate_hz : u32 , bit_depth : u16 ) -> Array <u32 > {
25
+ fn generate_sine_wave (
26
+ frequency_hz : u32 , duration_ms : u32 , sample_rate_hz : u32 , bit_depth : u16
27
+ ) -> Array <u32 > {
22
28
let samples = array! [];
23
29
samples
24
30
}
25
31
26
- fn generate_square_wave (frequency_hz : u32 , duration_ms : u32 , sample_rate_hz : u32 , bit_depth : u16 ) -> Array <u32 > {
32
+ fn generate_square_wave (
33
+ frequency_hz : u32 , duration_ms : u32 , sample_rate_hz : u32 , bit_depth : u16
34
+ ) -> Array <u32 > {
27
35
let mut samples : Array <u32 > = array! [];
28
36
let mut num_samples_left : u64 = ((duration_ms * sample_rate_hz ) / 1000 ). into ();
29
37
30
38
let mega_full : u64 = (PRECISION * sample_rate_hz . into ()) / frequency_hz . into ();
31
39
let mega_half : u64 = mega_full / 2 ;
32
40
33
41
let max_value : u64 = get_max_value (bit_depth );
34
-
35
42
while num_samples_left > 0 {
36
43
if (num_samples_left * PRECISION ) % mega_full < mega_half {
37
44
samples . append (0 );
@@ -44,13 +51,14 @@ fn generate_square_wave(frequency_hz: u32, duration_ms: u32, sample_rate_hz: u32
44
51
}
45
52
46
53
// TODO
47
- fn generate_sawtooth_wave (frequency_hz : u32 , duration_ms : u32 , sample_rate_hz : u32 , bit_depth : u16 ) -> Array <u32 > {
54
+ fn generate_sawtooth_wave (
55
+ frequency_hz : u32 , duration_ms : u32 , sample_rate_hz : u32 , bit_depth : u16
56
+ ) -> Array <u32 > {
48
57
let mut samples : Array <u32 > = array! [];
49
58
let mut num_samples_left : u64 = ((duration_ms * sample_rate_hz ) / 1000 ). into ();
50
59
51
60
let mega_full : u64 = (PRECISION * sample_rate_hz . into ()) / frequency_hz . into ();
52
61
53
-
54
62
let max_value : u64 = get_max_value (bit_depth );
55
63
56
64
while num_samples_left > 0 {
@@ -63,15 +71,16 @@ fn generate_sawtooth_wave(frequency_hz: u32, duration_ms: u32, sample_rate_hz: u
63
71
samples
64
72
}
65
73
66
- fn generate_triangle_wave (frequency_hz : u32 , duration_ms : u32 , sample_rate_hz : u32 , bit_depth : u16 ) -> Array <u32 > {
74
+ fn generate_triangle_wave (
75
+ frequency_hz : u32 , duration_ms : u32 , sample_rate_hz : u32 , bit_depth : u16
76
+ ) -> Array <u32 > {
67
77
let mut samples : Array <u32 > = array! [];
68
78
let mut num_samples_left : u64 = ((duration_ms * sample_rate_hz ) / 1000 ). into ();
69
79
70
80
let mega_full : u64 = (PRECISION * sample_rate_hz . into ()) / frequency_hz . into ();
71
81
let mega_half : u64 = mega_full / 2 ;
72
82
73
83
let max_value : u64 = get_max_value (bit_depth );
74
-
75
84
while num_samples_left > 0 {
76
85
let pos = (num_samples_left * PRECISION ) % mega_full ;
77
86
let value = if pos < mega_half {
@@ -83,4 +92,5 @@ fn generate_triangle_wave(frequency_hz: u32, duration_ms: u32, sample_rate_hz: u
83
92
num_samples_left -= 1 ;
84
93
};
85
94
samples
86
- }
95
+ }
96
+
0 commit comments