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
use crate::building_blocks::{
Modulator, MonoSource, SampleBuffer, SynthParameterLabel, SynthParameterValue,
};
use std::f32::consts::PI;
/**
* A quasi-bandlimited sawtooth oscillator using fm synthesis, following:
*
* Peter Schoffhauzer - Synthesis of Quasi-Bandliminted Analog Waveforms
* Using Frequency Modulation
*
* http://scp.web.elte.hu/papers/synthesis1.pdf
*/
#[derive(Clone)]
pub struct FMSaw<const BUFSIZE: usize> {
// user parameters
freq: f32,
amp: f32,
// internal parameters
samplerate: f32,
osc: f32, // current output sample
phase: f32, // phase accumulator
w: f32, // normalized frequency
scaling: f32, // scaling amount
dc_comp: f32,
norm: f32,
del: f32, // one-pole filter delay
// modulator slots
freq_mod: Option<Modulator<BUFSIZE>>, // allows modulating frequency ..
amp_mod: Option<Modulator<BUFSIZE>>, // and level
}
impl<const BUFSIZE: usize> FMSaw<BUFSIZE> {
pub fn new(freq: f32, amp: f32, samplerate: f32) -> Self {
let w: f32 = freq / samplerate;
let n: f32 = 0.5 - w;
FMSaw {
freq,
amp,
samplerate,
osc: 0.0, // current output sample
phase: 0.0, // phase accumulator
w, // normalized frequency
scaling: 13.0 * n * n * n * n, // scaling amount
dc_comp: 0.1 + w * 0.2,
norm: 1.0 - 2.0 * w,
del: 0.0, // filter delay
freq_mod: None,
amp_mod: None,
}
}
#[inline(always)]
pub fn update_internals(&mut self, freq: f32) {
self.w = freq / self.samplerate;
let n: f32 = 0.5 - self.w;
self.scaling = 13.0 * n * n * n * n;
self.dc_comp = 0.1 + self.w * 0.2;
self.norm = 1.0 - 2.0 * self.w;
}
}
impl<const BUFSIZE: usize> MonoSource<BUFSIZE> for FMSaw<BUFSIZE> {
fn reset(&mut self) {}
fn set_modulator(
&mut self,
par: SynthParameterLabel,
init: f32,
modulator: Modulator<BUFSIZE>,
) {
match par {
SynthParameterLabel::PitchFrequency => {
self.freq = init;
self.freq_mod = Some(modulator);
}
SynthParameterLabel::OscillatorAmplitude => {
self.amp = init;
self.amp_mod = Some(modulator);
}
_ => {}
}
}
// some parameter limits might be nice ...
fn set_parameter(&mut self, par: SynthParameterLabel, value: &SynthParameterValue) {
match par {
SynthParameterLabel::PitchFrequency => {
if let SynthParameterValue::ScalarF32(f) = value {
self.freq = *f;
self.update_internals(*f);
}
}
SynthParameterLabel::OscillatorAmplitude => {
if let SynthParameterValue::ScalarF32(l) = value {
self.amp = *l;
}
}
_ => (),
};
}
fn finish(&mut self) {}
fn is_finished(&self) -> bool {
false
}
fn get_next_block(
&mut self,
start_sample: usize,
in_buffers: &[SampleBuffer],
) -> [f32; BUFSIZE] {
let mut out_buf: [f32; BUFSIZE] = [0.0; BUFSIZE];
if self.freq_mod.is_some() || self.amp_mod.is_some() {
let amp_buf = if let Some(m) = self.amp_mod.as_mut() {
m.process(self.amp, start_sample, in_buffers)
} else {
[self.amp; BUFSIZE]
};
let freq_buf = if let Some(m) = self.freq_mod.as_mut() {
m.process(self.freq, start_sample, in_buffers)
} else {
[self.freq; BUFSIZE]
};
for (i, current_sample) in out_buf
.iter_mut()
.enumerate()
.take(BUFSIZE)
.skip(start_sample)
{
self.update_internals(freq_buf[i]);
// phase accum
self.phase += 2.0 * self.w;
if self.phase >= 1.0 {
self.phase -= 2.0;
}
// next sample
// the paper says 2pi, but that doesn't make sense when you look at the plots ...
self.osc = (self.osc + (PI * (self.phase + self.scaling * self.osc)).sin()) * 0.5;
let o = 2.5 * self.osc - 1.5 * self.del;
self.del = self.osc;
// the normalization is different than in the paper, but it seems more symmetric
// to me this way ..
*current_sample = (o - self.dc_comp) * self.norm * amp_buf[i];
}
} else {
for current_sample in out_buf.iter_mut().take(BUFSIZE).skip(start_sample) {
// phase accum
self.phase += 2.0 * self.w;
if self.phase >= 1.0 {
self.phase -= 2.0;
}
// next sample
// the paper says 2pi, but that doesn't make sense when you look at the plots ...
self.osc = (self.osc + (PI * (self.phase + self.scaling * self.osc)).sin()) * 0.5;
// one-pole lowpass filter
let o = 2.5 * self.osc - 1.5 * self.del;
self.del = self.osc;
// the normalization is different than in the paper, but it seems more symmetric
// to me this way ..
*current_sample = (o - self.dc_comp) * self.norm * self.amp;
}
}
out_buf
}
}