rill_core_dsp/generators/
wavetable.rs1use crate::generators::{Generator, InterpolatedReader};
2use crate::vector::prelude::*;
3use rill_core::traits::algorithm::{Algorithm, AlgorithmCategory, AlgorithmMetadata};
4use rill_core::traits::ProcessResult;
5use rill_core::Transcendental;
6
7pub struct WavetableOscillator<T: Transcendental, const SIZE: usize> {
13 reader: InterpolatedReader<T>,
14 frequency: f32,
15 amplitude: ScalarVector1<T>,
16 sample_rate: f32,
17}
18
19impl<T: Transcendental, const SIZE: usize> WavetableOscillator<T, SIZE> {
20 pub fn new(table: [T; SIZE], frequency: f32) -> Self {
22 let mut reader = InterpolatedReader::new(table.to_vec());
23 reader.set_wrap(true);
24 let mut osc = Self {
25 reader,
26 frequency,
27 amplitude: ScalarVector1::splat(T::from_f32(1.0)),
28 sample_rate: 44100.0,
29 };
30 osc.update_rate();
31 osc
32 }
33
34 pub fn sine(frequency: f32) -> Self {
36 let mut table = [T::ZERO; SIZE];
37 for (i, tbl) in table.iter_mut().enumerate() {
38 let phase = (i as f32 / SIZE as f32) * 2.0 * core::f32::consts::PI;
39 *tbl = T::from_f32(phase.sin());
40 }
41 Self::new(table, frequency)
42 }
43
44 pub fn saw(frequency: f32) -> Self {
46 let mut table = [T::ZERO; SIZE];
47 for (i, tbl) in table.iter_mut().enumerate() {
48 *tbl = T::from_f32(2.0 * i as f32 / SIZE as f32 - 1.0);
49 }
50 Self::new(table, frequency)
51 }
52
53 pub fn set_table(&mut self, table: [T; SIZE]) {
55 self.reader.set_buffer(table.to_vec());
56 }
57
58 pub fn set_cubic(&mut self, cubic: bool) {
60 self.reader.set_cubic(cubic);
61 }
62
63 pub fn is_cubic(&self) -> bool {
65 self.reader.is_cubic()
66 }
67
68 fn update_rate(&mut self) {
69 let rate = self.frequency as f64 * SIZE as f64 / self.sample_rate as f64;
70 self.reader.set_rate(rate);
71 }
72}
73
74impl<T: Transcendental, const SIZE: usize> Algorithm<T> for WavetableOscillator<T, SIZE> {
75 fn init(&mut self, sample_rate: f32) {
76 self.sample_rate = sample_rate;
77 self.update_rate();
78 self.reader.set_position(0.0);
79 }
80
81 fn reset(&mut self) {
82 self.reader.set_position(0.0);
83 }
84
85 fn process(&mut self, _input: Option<&[T]>, output: &mut [T]) -> ProcessResult<()> {
86 let amp = self.amplitude.extract(0);
87 self.reader.render_block(output);
88 if amp != T::from_f32(1.0) {
89 for s in output.iter_mut() {
90 *s *= amp;
91 }
92 }
93 Ok(())
94 }
95
96 fn metadata(&self) -> AlgorithmMetadata {
97 AlgorithmMetadata {
98 name: "Wavetable Oscillator",
99 category: AlgorithmCategory::Generator,
100 description: "Wavetable oscillator with linear / cubic interpolation",
101 author: "Rill",
102 version: env!("CARGO_PKG_VERSION"),
103 }
104 }
105}
106
107impl<T: Transcendental, const SIZE: usize> Generator<T> for WavetableOscillator<T, SIZE> {
108 fn phase(&self) -> T {
109 let pos = self.reader.position();
110 let len = SIZE as f64;
111 T::from_f64((pos % len) / len)
112 }
113
114 fn set_phase(&mut self, phase: T) {
115 let p = phase.to_f64().clamp(0.0, 1.0);
116 self.reader.set_position(p * SIZE as f64);
117 }
118
119 fn reset_phase(&mut self) {
120 self.reader.set_position(0.0);
121 }
122
123 fn frequency(&self) -> f32 {
124 self.frequency
125 }
126
127 fn set_frequency(&mut self, freq: f32) {
128 self.frequency = freq;
129 self.update_rate();
130 }
131
132 fn amplitude(&self) -> T {
133 self.amplitude.extract(0)
134 }
135
136 fn set_amplitude(&mut self, amp: T) {
137 self.amplitude = ScalarVector1::splat(amp.clamp(T::ZERO, T::from_f32(1.0)));
138 }
139}