1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
pub mod brown_noise;
/// A collection of oscillators, some of which are modeled
/// after scsynth, csound, etc ...
pub mod fm_saw;
pub mod fm_square;
pub mod fm_tri;
pub mod lf_cub;
pub mod lf_impulse;
pub mod lf_rsaw;
pub mod lf_saw;
pub mod lf_square;
pub mod lf_tri;
pub mod naive_blit;
pub mod sine_osc;
pub mod wavematrix;
pub mod wavetable;
pub mod white_noise;
pub mod wt_saw;
pub use crate::building_blocks::oscillators::brown_noise::BrownNoise;
pub use crate::building_blocks::oscillators::fm_saw::FMSaw;
pub use crate::building_blocks::oscillators::fm_square::FMSquare;
pub use crate::building_blocks::oscillators::fm_tri::FMTri;
pub use crate::building_blocks::oscillators::lf_cub::LFCub;
pub use crate::building_blocks::oscillators::lf_impulse::LFImpulse;
pub use crate::building_blocks::oscillators::lf_rsaw::LFRSaw;
pub use crate::building_blocks::oscillators::lf_saw::LFSaw;
pub use crate::building_blocks::oscillators::lf_square::LFSquare;
pub use crate::building_blocks::oscillators::lf_tri::LFTri;
pub use crate::building_blocks::oscillators::sine_osc::SineOsc;
pub use crate::building_blocks::oscillators::wavematrix::Wavematrix;
pub use crate::building_blocks::oscillators::wavetable::Wavetable;
pub use crate::building_blocks::oscillators::white_noise::WhiteNoise;
pub use crate::building_blocks::oscillators::wt_saw::WTSaw;
// TEST TEST TEST
#[cfg(test)]
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
use crate::building_blocks::MonoSource;
use std::f32::consts::PI;
#[test]
fn sine_osc_test_at_block_start() {
let mut osc = SineOsc::<128>::new(440.0, 1.0, 44100.0);
let out_1 = osc.get_next_block(0, &Vec::new());
let mut comp_1 = [0.0; 128];
for i in 0..128 {
comp_1[i] = (2.0 * PI * 440.0 * (i as f32 * (1.0 / 44100.0))).sin()
}
// the new sine osc seems to be a bit less precise ....
for i in 0usize..128usize {
//let b = out_1[i];
//let c = comp_1[i];
//debug_plotter::plot!(b, c where caption = "BlockPlot");
assert_approx_eq::assert_approx_eq!(out_1[i], comp_1[i], 0.008);
}
}
/*
#[test]
fn sine_osc_rel_phase_offset() {
let mut osc = SineOsc::<128>::new(440.0, 1.0, 44100.0);
osc.set_parameter(SynthParameterLabel::OscillatorPhaseRelative,
&SynthParameterValue::ScalarF32(0.5));
let out_1 = osc.get_next_block(0, &Vec::new());
let mut comp_1 = [0.0; 128];
for i in 0..128 {
comp_1[i] = (2.0 * PI * 440.0 * (i as f32 * (1.0 / 44100.0))).cos()
}
// the new sine osc seems to be a bit less precise ....
for i in 0usize..128usize {
let b = out_1[i];
let c = comp_1[i];
debug_plotter::plot!(b, c where caption = "BlockPlotRelPhase");
//assert_approx_eq::assert_approx_eq!(out_1[i], comp_1[i], 0.008);
}
}
#[test]
fn sine_osc_abs_phase_offset() {
let mut osc = SineOsc::<128>::new(440.0, 200.0, 44100.0);
osc.set_parameter(SynthParameterLabel::OscillatorPhaseEffective,
&SynthParameterValue::ScalarF32(100.0));
let out_1 = osc.get_next_block(0, &Vec::new());
let mut comp_1 = [0.0; 128];
for i in 0..128 {
comp_1[i] = (2.0 * PI * 440.0 * (i as f32 * (1.0 / 44100.0))).sin() * 200.0
}
// the new sine osc seems to be a bit less precise ....
for i in 0usize..128usize {
let b = out_1[i];
let c = comp_1[i];
debug_plotter::plot!(b, c where caption = "BlockPlotAbsPhase");
//assert_approx_eq::assert_approx_eq!(out_1[i], comp_1[i], 0.008);
}
}
#[test]
fn plot_tri() {
let mut osc = FMSaw::<128>::new(10000.0, 1.0, 44100.0);
for _ in 0..50 {
let out_1 = osc.get_next_block(0, &Vec::new());
// the new sine osc seems to be a bit less precise ....
for i in 0usize..128usize {
let b = out_1[i];
debug_plotter::plot!(b where caption = "PlotFmTri2");
}
}
}*/
#[test]
fn sine_osc_test_start_in_block() {
let mut osc = SineOsc::<128>::new(440.0, 1.0, 44100.0);
let start_time: f32 = 0.001;
let sample_offset = (44100.0 * start_time).round() as usize;
let out_1 = osc.get_next_block(sample_offset, &Vec::new());
let mut comp_1 = [0.0; 128];
for i in sample_offset..128 {
comp_1[i] = (2.0 * PI * 440.0 * ((i - sample_offset) as f32 * (1.0 / 44100.0))).sin()
}
for i in 0..128 {
//println!("{} {} {}; ", i, out_1[i], comp_1[i]);
assert_approx_eq::assert_approx_eq!(out_1[i], comp_1[i], 0.008);
}
}
#[test]
fn sine_osc_test_multiple_blocks() {
let mut osc = SineOsc::<128>::new(440.0, 1.0, 44100.0);
let out_1 = osc.get_next_block(0, &Vec::new());
let out_2 = osc.get_next_block(0, &Vec::new());
let out_3 = osc.get_next_block(0, &Vec::new());
let out_4 = osc.get_next_block(0, &Vec::new());
let out_5 = osc.get_next_block(0, &Vec::new());
let out_6 = osc.get_next_block(0, &Vec::new());
let mut comp_1 = [0.0; 128];
let mut comp_2 = [0.0; 128];
let mut comp_3 = [0.0; 128];
let mut comp_4 = [0.0; 128];
let mut comp_5 = [0.0; 128];
let mut comp_6 = [0.0; 128];
for i in 0..128 {
comp_1[i] = (2.0 * PI * 440.0 * (i as f32 * (1.0 / 44100.0))).sin()
}
for i in 128..256 {
comp_2[i - 128] = (2.0 * PI * 440.0 * (i as f32 * (1.0 / 44100.0))).sin()
}
for i in 256..384 {
comp_3[i - 256] = (2.0 * PI * 440.0 * (i as f32 * (1.0 / 44100.0))).sin()
}
for i in 384..512 {
comp_4[i - 384] = (2.0 * PI * 440.0 * (i as f32 * (1.0 / 44100.0))).sin()
}
for i in 512..640 {
comp_5[i - 512] = (2.0 * PI * 440.0 * (i as f32 * (1.0 / 44100.0))).sin()
}
for i in 640..768 {
comp_6[i - 640] = (2.0 * PI * 440.0 * (i as f32 * (1.0 / 44100.0))).sin()
}
for i in 0..128 {
// this isn't very precise ???
//println!("{} {} {}; ", i, out_1[i], comp_1[i]);
assert_approx_eq::assert_approx_eq!(out_1[i], comp_1[i], 0.008);
}
for i in 0..128 {
// this isn't very precise ???
//println!("{} {} {}; ", i, out_2[i], comp_2[i]);
assert_approx_eq::assert_approx_eq!(out_2[i], comp_2[i], 0.008);
}
for i in 0..128 {
// this isn't very precise ???
//println!("{} {} {}; ", i, out_3[i], comp_3[i]);
assert_approx_eq::assert_approx_eq!(out_3[i], comp_3[i], 0.008);
}
for i in 0..128 {
// this isn't very precise ???
//println!("{} {} {}; ", i, out_1[i], comp_1[i]);
assert_approx_eq::assert_approx_eq!(out_4[i], comp_4[i], 0.008);
}
for i in 0..128 {
// this isn't very precise ???
//println!("{} {} {}; ", i, out_2[i], comp_2[i]);
assert_approx_eq::assert_approx_eq!(out_5[i], comp_5[i], 0.008);
}
for i in 0..128 {
// this isn't very precise ???
//println!("{} {} {}; ", i, out_3[i], comp_3[i]);
assert_approx_eq::assert_approx_eq!(out_6[i], comp_6[i], 0.008);
}
}
}