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
use std::f64::consts::PI;
use anyhow::Result;
use autd3_core::modulation::{ModProps, Modulation};
use autd3_traits::Modulation;
#[derive(Modulation)]
pub struct SineLegacy {
props: ModProps,
freq: f64,
amp: f64,
offset: f64,
}
impl SineLegacy {
pub fn new(freq: f64) -> Self {
Self::with_params(freq, 1.0, 0.5)
}
pub fn with_params(freq: f64, amp: f64, offset: f64) -> Self {
Self {
props: ModProps::new(),
freq,
amp,
offset,
}
}
#[allow(clippy::unnecessary_wraps)]
fn calc(&mut self) -> Result<()> {
let sf = self.sampling_freq();
let freq = self
.freq
.clamp(autd3_core::FPGA_CLK_FREQ as f64 / u32::MAX as f64, sf / 2.0);
let n = (1.0 / freq * sf).round() as usize;
self.props.buffer.resize(n, 0);
self.props.buffer.iter_mut().enumerate().for_each(|(i, m)| {
let amp = self.amp / 2.0 * (2.0 * PI * i as f64 / n as f64).sin() + self.offset;
let amp = amp.clamp(0.0, 1.0);
let duty = amp.asin() * 2.0 / PI * 255.0;
*m = duty as u8
});
Ok(())
}
}