2021-05-18 03:11:19 +00:00
|
|
|
// Copyright (c) 2021 Weird Constructor <weirdconstructor@gmail.com>
|
2021-08-04 01:58:43 +00:00
|
|
|
// This file is a part of HexoDSP. Released under GPL-3.0-or-later.
|
2021-05-18 03:11:19 +00:00
|
|
|
// See README.md and COPYING for details.
|
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
use crate::dsp::helpers::fast_sin;
|
2021-06-06 07:24:41 +00:00
|
|
|
use crate::dsp::{
|
2022-07-17 09:58:28 +00:00
|
|
|
denorm_offs, inp, out, DspNode, LedPhaseVals, NodeContext, NodeId, ProcBuf, SAtom,
|
2021-06-06 07:24:41 +00:00
|
|
|
};
|
2022-07-17 09:58:28 +00:00
|
|
|
use crate::nodes::{NodeAudioContext, NodeExecContext};
|
2021-05-18 03:11:19 +00:00
|
|
|
|
|
|
|
/// A sine oscillator
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Sin {
|
|
|
|
/// Sample rate
|
|
|
|
srate: f32,
|
|
|
|
/// Oscillator phase
|
|
|
|
phase: f32,
|
2021-07-25 20:49:33 +00:00
|
|
|
/// Initial phase offset
|
|
|
|
init_phase: f32,
|
2021-05-18 03:11:19 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
const TWOPI: f32 = 2.0 * std::f32::consts::PI;
|
2021-05-18 03:11:19 +00:00
|
|
|
|
|
|
|
impl Sin {
|
2021-07-25 20:49:33 +00:00
|
|
|
pub fn new(nid: &NodeId) -> Self {
|
|
|
|
let init_phase = nid.init_phase();
|
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
Self { srate: 44100.0, phase: init_phase, init_phase }
|
2021-05-18 03:11:19 +00:00
|
|
|
}
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const freq: &'static str = "Sin freq\nFrequency of the oscillator.\n\nRange: (-1..1)\n";
|
|
|
|
pub const det: &'static str = "Sin det\nDetune the oscillator in semitones and cents. \
|
2021-06-05 07:13:42 +00:00
|
|
|
the input of this value is rounded to semitones on coarse input. \
|
|
|
|
Fine input lets you detune in cents (rounded). \
|
2021-06-06 07:24:41 +00:00
|
|
|
A signal sent to this port is not rounded.\n\
|
|
|
|
Note: The signal input allows detune +-10 octaves.\
|
|
|
|
\nRange: (Knob -0.2 .. 0.2) / (Signal -1.0 .. 1.0)\n";
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const sig: &'static str = "Sin sig\nOscillator signal output.\n\nRange: (-1..1)\n";
|
2021-06-07 03:00:10 +00:00
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const DESC: &'static str = r#"Sine Oscillator
|
2021-06-07 03:00:10 +00:00
|
|
|
|
2021-06-08 01:56:40 +00:00
|
|
|
This is a very simple oscillator that generates a sine wave.
|
|
|
|
"#;
|
2021-06-07 03:00:10 +00:00
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const HELP: &'static str = r#"Sin - A Sine Oscillator
|
2021-06-07 03:00:10 +00:00
|
|
|
|
2021-06-08 01:56:40 +00:00
|
|
|
This is a very simple oscillator that generates a sine wave.
|
|
|
|
The 'freq' paramter specifies the frequency, and the 'det' parameter
|
|
|
|
allows you to detune the oscillator easily.
|
2021-06-07 03:00:10 +00:00
|
|
|
|
2021-06-08 01:56:40 +00:00
|
|
|
You can send any signal to these input ports. The 'det' parameter takes
|
|
|
|
the same signal range as 'freq', which means, that a value of 0.1 detunes
|
|
|
|
by one octave. And a value 1.0 detunes by 10 octaves. This means that
|
|
|
|
for 'det' to be usefully modulated you need to attenuate the modulation input.
|
2021-06-07 03:00:10 +00:00
|
|
|
|
2021-06-08 01:56:40 +00:00
|
|
|
You can do FM with this node, but for easy FM synthesis there are other
|
|
|
|
nodes available.
|
|
|
|
"#;
|
2021-05-18 03:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DspNode for Sin {
|
2022-07-17 09:58:28 +00:00
|
|
|
fn outputs() -> usize {
|
|
|
|
1
|
|
|
|
}
|
2021-05-18 03:11:19 +00:00
|
|
|
|
|
|
|
fn set_sample_rate(&mut self, srate: f32) {
|
|
|
|
self.srate = srate;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reset(&mut self) {
|
2021-07-25 20:49:33 +00:00
|
|
|
self.phase = self.init_phase;
|
2021-05-18 03:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn process<T: NodeAudioContext>(
|
2022-07-17 09:58:28 +00:00
|
|
|
&mut self,
|
|
|
|
ctx: &mut T,
|
|
|
|
_ectx: &mut NodeExecContext,
|
2021-07-10 19:27:18 +00:00
|
|
|
_nctx: &NodeContext,
|
2022-07-17 09:58:28 +00:00
|
|
|
_atoms: &[SAtom],
|
|
|
|
inputs: &[ProcBuf],
|
|
|
|
outputs: &mut [ProcBuf],
|
|
|
|
ctx_vals: LedPhaseVals,
|
|
|
|
) {
|
|
|
|
let o = out::Sin::sig(outputs);
|
2021-05-18 03:11:19 +00:00
|
|
|
let freq = inp::Sin::freq(inputs);
|
2022-07-17 09:58:28 +00:00
|
|
|
let det = inp::Sin::det(inputs);
|
|
|
|
let isr = 1.0 / self.srate;
|
2021-05-18 03:11:19 +00:00
|
|
|
|
|
|
|
let mut last_val = 0.0;
|
|
|
|
for frame in 0..ctx.nframes() {
|
2021-06-06 07:24:41 +00:00
|
|
|
let freq = denorm_offs::Sampl::freq(freq, det.read(frame), frame);
|
2021-05-18 03:11:19 +00:00
|
|
|
|
|
|
|
last_val = fast_sin(self.phase * TWOPI);
|
|
|
|
o.write(frame, last_val);
|
|
|
|
|
|
|
|
self.phase += freq * isr;
|
|
|
|
self.phase = self.phase.fract();
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx_vals[0].set(last_val);
|
|
|
|
}
|
|
|
|
}
|