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
}
}