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
use super::super::Modulation;
use crate::core::configuration::Configuration;
use crate::Float;
use num::clamp;
use num::integer::gcd;
pub struct SineModulation {
freq: i32,
amp: Float,
offset: Float,
buffer: Vec<u8>,
sent: usize,
}
impl SineModulation {
pub fn create_with_amp(freq: i32, amp: Float, offset: Float) -> Self {
Self {
freq,
amp,
offset,
buffer: vec![],
sent: 0,
}
}
pub fn create(freq: i32) -> Self {
SineModulation::create_with_amp(freq, 1.0, 0.5)
}
}
impl Modulation for SineModulation {
fn build(&mut self, config: Configuration) {
let sf = config.sampling_frequency() as usize;
let mod_buf_size = config.buf_size() as usize;
let freq = clamp(self.freq as usize, 1, sf / 2);
let d = gcd(sf, freq);
let n = mod_buf_size / d / (mod_buf_size / sf);
let rep = freq / d;
self.buffer = Vec::with_capacity(n);
let offset = self.offset;
let amp = self.amp;
for i in 0..n {
let tamp = ((2 * rep * i) as Float / n as Float) % 2.0;
let tamp = if tamp > 1. { 2. - tamp } else { tamp };
let tamp = clamp(offset + (tamp - 0.5) * amp, 0.0, 1.0);
self.buffer.push((tamp * 255.0) as u8);
}
}
fn buffer(&self) -> &[u8] {
&self.buffer
}
fn sent(&mut self) -> &mut usize {
&mut self.sent
}
}