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
179
180
181
182
183
184
185
186
187
use crate::dsp::{
DspNode, GraphAtomData, GraphFun, LedPhaseVals, NodeContext, NodeGlobalRef, NodeId, ProcBuf,
SAtom,
};
use crate::nodes::{NodeAudioContext, NodeExecContext};
use synfx_dsp::PolyBlepOscillator;
#[macro_export]
macro_rules! fa_bosc_wtype {
($formatter: expr, $v: expr, $denorm_v: expr) => {{
let s = match ($v.round() as usize) {
0 => "Sin",
1 => "Tri",
2 => "Saw",
3 => "Pulse",
4 => "Pulse-DC",
_ => "?",
};
write!($formatter, "{}", s)
}};
}
#[derive(Debug, Clone)]
pub struct BOsc {
osc: PolyBlepOscillator,
israte: f32,
}
impl BOsc {
pub fn new(nid: &NodeId, _node_global: &NodeGlobalRef) -> Self {
let init_phase = nid.init_phase();
Self { osc: PolyBlepOscillator::new(init_phase), israte: 1.0 / 44100.0 }
}
pub const freq: &'static str = "Base frequency of the oscillator.\n";
pub const det: &'static str = "Detune the oscillator in semitones and cents. \
the input of this value is rounded to semitones on coarse input. \
Fine input lets you detune in cents (rounded). \
A signal sent to this port is not rounded.\n\
Note: The signal input allows detune +-10 octaves.";
pub const pw: &'static str = "";
pub const wtype: &'static str = "Waveform type. Available waveforms:\n\
- **Sin** - Sine Waveform\n\
- **Tri** - Triangle Waveform\n\
- **Saw** - Sawtooth Waveform\n\
- **Pulse** - Pulse Waveform\n\
- **Pulse-DC** - Pulse Waveform (DC corrected)";
pub const sig: &'static str = "Oscillator output";
pub const DESC: &'static str = r#"Basic Oscillator
A very basic band limited oscillator with a sine, triangle, pulse and sawtooth waveform.
"#;
pub const HELP: &'static str = r#"Basic Waveform Oscillator
A very basic band limited oscillator with a sine, triangle, pulse and sawtooth
waveform. The pulse width ~~pw~~ parameter only has an effect for the
**Pulse** waveform.
There are two pulse waveforms: **Pulse** and **Pulse-DC**. Depending on the pulse width
setting of the oscillator the output of the pulse might introduce DC (direct current) into
the signal. The **Pulse-DC** variant compensates that DC component by shifting the signal,
just like a high pass filter would do.
"#;
pub fn graph_fun() -> Option<GraphFun> {
let mut osc = Box::new(PolyBlepOscillator::new(0.0));
let israte = 1.0 / 128.0;
Some(Box::new(move |gd: &dyn GraphAtomData, init: bool, _x: f32, _xn: f32| -> f32 {
let wtype = NodeId::BOsc(0).inp_param("wtype").unwrap().inp();
let pw = NodeId::BOsc(0).inp_param("pw").unwrap().inp();
let wtype = gd.get(wtype as u32).map(|a| a.i()).unwrap_or(0);
let pw = gd.get_denorm(pw as u32);
let freq = 2.0;
if init {
osc.reset();
if wtype == 1 {
for _ in 0..256 {
osc.next_tri(freq, israte);
}
}
}
let s = match wtype {
0 => (osc.next_sin(freq, israte) + 1.0) * 0.5,
1 => (osc.next_tri(freq, israte) + 1.0) * 0.5,
2 => (osc.next_saw(freq, israte) + 1.0) * 0.5,
3 => (osc.next_pulse_no_dc(freq, israte, pw) + 1.0) * 0.5,
_ => (osc.next_pulse(freq, israte, pw) + 1.0) * 0.5,
};
s * 0.9 + 0.05
}))
}
}
impl DspNode for BOsc {
fn set_sample_rate(&mut self, srate: f32) {
self.israte = 1.0 / srate;
}
fn reset(&mut self) {
self.osc.reset();
}
#[inline]
fn process(
&mut self,
ctx: &mut dyn NodeAudioContext,
_ectx: &mut NodeExecContext,
_nctx: &NodeContext,
atoms: &[SAtom],
inputs: &[ProcBuf],
outputs: &mut [ProcBuf],
ctx_vals: LedPhaseVals,
) {
use crate::dsp::{at, denorm, denorm_offs, inp, out};
let freq = inp::BOsc::freq(inputs);
let det = inp::BOsc::det(inputs);
let pw = inp::BOsc::pw(inputs);
let out = out::BOsc::sig(outputs);
let wtype = at::BOsc::wtype(atoms);
let israte = self.israte;
match wtype.i() {
0 => {
for frame in 0..ctx.nframes() {
let freq = denorm_offs::BOsc::freq(freq, det.read(frame), frame);
out.write(frame, self.osc.next_sin(freq, israte));
}
}
1 => {
for frame in 0..ctx.nframes() {
let freq = denorm_offs::BOsc::freq(freq, det.read(frame), frame);
out.write(frame, self.osc.next_tri(freq, israte));
}
}
2 => {
for frame in 0..ctx.nframes() {
let freq = denorm_offs::BOsc::freq(freq, det.read(frame), frame);
out.write(frame, self.osc.next_saw(freq, israte));
}
}
3 => {
for frame in 0..ctx.nframes() {
let freq = denorm_offs::BOsc::freq(freq, det.read(frame), frame);
let pw = denorm::BOsc::pw(pw, frame);
out.write(frame, self.osc.next_pulse_no_dc(freq, israte, pw));
}
}
_ => {
for frame in 0..ctx.nframes() {
let freq = denorm_offs::BOsc::freq(freq, det.read(frame), frame);
let pw = denorm::BOsc::pw(pw, frame);
out.write(frame, self.osc.next_pulse(freq, israte, pw));
}
}
}
ctx_vals[0].set(out.read(ctx.nframes() - 1));
}
}