2021-08-28 17:56:08 +00:00
|
|
|
// Copyright (c) 2021 Weird Constructor <weirdconstructor@gmail.com>
|
|
|
|
// This file is a part of HexoDSP. Released under GPL-3.0-or-later.
|
|
|
|
// See README.md and COPYING for details.
|
|
|
|
|
|
|
|
use crate::nodes::{NodeAudioContext, NodeExecContext};
|
|
|
|
use crate::dsp::{NodeId, SAtom, ProcBuf, DspNode, LedPhaseVals, NodeContext};
|
2021-08-30 01:55:42 +00:00
|
|
|
use crate::dsp::helpers::{Quantizer, ChangeTrig};
|
2021-08-28 17:56:08 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! fa_quant { ($formatter: expr, $v: expr, $denorm_v: expr) => { {
|
|
|
|
write!($formatter, "?")
|
|
|
|
} } }
|
|
|
|
|
|
|
|
/// A pitch quantizer
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Quant {
|
2021-08-30 01:55:42 +00:00
|
|
|
quant: Box<Quantizer>,
|
|
|
|
change_trig: ChangeTrig,
|
2021-08-28 17:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Quant {
|
|
|
|
pub fn new(_nid: &NodeId) -> Self {
|
|
|
|
Self {
|
2021-08-30 01:55:42 +00:00
|
|
|
quant: Box::new(Quantizer::new()),
|
|
|
|
change_trig: ChangeTrig::new(),
|
2021-08-28 17:56:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pub const freq : &'static str =
|
2021-08-30 01:55:42 +00:00
|
|
|
"Quant freq\nAny signal that is to be pitch quantized.\nRange: (-1..1)";
|
2021-08-28 17:56:08 +00:00
|
|
|
pub const oct : &'static str =
|
2021-08-30 01:55:42 +00:00
|
|
|
"Quant oct\nPitch offset, the knob is snapping to octave offsets. \
|
|
|
|
Feed signal values snapped to 0.1 multiples for exact octave offsets.\
|
|
|
|
\nRange: (-1..1)";
|
2021-08-28 17:56:08 +00:00
|
|
|
pub const sig : &'static str =
|
2021-08-30 01:55:42 +00:00
|
|
|
"Quant sig\nThe quantized output signal that is rounded to \
|
|
|
|
the next selected note pitch within the octave of the \
|
|
|
|
original input to 'freq'.\nRange: (-1..1)";
|
2021-08-28 17:56:08 +00:00
|
|
|
pub const keys : &'static str =
|
2021-08-30 01:55:42 +00:00
|
|
|
"Quant keys\nSelect the notes you want to snap to here. \
|
|
|
|
If no notes are selected, the quantizer will snap the \
|
|
|
|
incoming signal to any closest note.";
|
|
|
|
pub const t : &'static str =
|
|
|
|
"Quant t\nEverytime the quantizer snaps to a new pitch, it will \
|
|
|
|
emit a short trigger on this signal output. This is useful \
|
|
|
|
to trigger for example an envelope.";
|
2021-08-28 17:56:08 +00:00
|
|
|
pub const DESC : &'static str =
|
|
|
|
r#"Pitch Quantizer
|
|
|
|
|
|
|
|
This is a simple quantizer, that snaps a pitch signal on 'freq' to the closest selected notes within their octave.
|
|
|
|
"#;
|
|
|
|
pub const HELP : &'static str =
|
|
|
|
r#"Quant - A pitch quantizer
|
|
|
|
|
|
|
|
This is a simple quantizer, that snaps a pitch signal on 'freq' to the
|
|
|
|
closest selected notes within their octave.
|
|
|
|
|
|
|
|
If you sweep along pitches you will notice that notes that are closer together
|
|
|
|
are travelled across faster. That means the notes are not evenly distributed
|
|
|
|
across the pitch input. If you want a more evenly distributed pitch selection
|
|
|
|
please see also the 'CQnt' node.
|
|
|
|
"#;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DspNode for Quant {
|
|
|
|
fn outputs() -> usize { 1 }
|
|
|
|
|
2021-08-30 01:55:42 +00:00
|
|
|
fn set_sample_rate(&mut self, srate: f32) {
|
|
|
|
self.change_trig.set_sample_rate(srate);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reset(&mut self) {
|
|
|
|
self.change_trig.reset();
|
|
|
|
}
|
2021-08-28 17:56:08 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn process<T: NodeAudioContext>(
|
|
|
|
&mut self, ctx: &mut T, _ectx: &mut NodeExecContext,
|
2022-06-25 06:37:05 +00:00
|
|
|
_nctx: &NodeContext,
|
2021-08-28 17:56:08 +00:00
|
|
|
atoms: &[SAtom], inputs: &[ProcBuf],
|
|
|
|
outputs: &mut [ProcBuf], ctx_vals: LedPhaseVals)
|
|
|
|
{
|
2021-08-30 01:55:42 +00:00
|
|
|
use crate::dsp::{at, out_buf, inp, denorm};
|
2021-08-28 17:56:08 +00:00
|
|
|
|
|
|
|
let freq = inp::Quant::freq(inputs);
|
|
|
|
let oct = inp::Quant::oct(inputs);
|
|
|
|
let keys = at::Quant::keys(atoms);
|
2021-08-30 01:55:42 +00:00
|
|
|
let mut out = out_buf::CQnt::sig(outputs);
|
|
|
|
let mut t = out_buf::CQnt::t(outputs);
|
2021-08-28 17:56:08 +00:00
|
|
|
|
|
|
|
self.quant.set_keys(keys.i());
|
|
|
|
|
|
|
|
for frame in 0..ctx.nframes() {
|
|
|
|
let pitch = self.quant.process(freq.read(frame));
|
2021-08-30 01:55:42 +00:00
|
|
|
|
|
|
|
t.write(frame, self.change_trig.next(pitch));
|
2021-08-28 17:56:08 +00:00
|
|
|
out.write(frame, pitch + denorm::Quant::oct(oct, frame));
|
|
|
|
}
|
|
|
|
|
2021-08-29 08:59:59 +00:00
|
|
|
let last_pitch = self.quant.last_key_pitch();
|
|
|
|
ctx_vals[1].set(last_pitch * 10.0 + 0.0001);
|
|
|
|
ctx_vals[0].set((last_pitch * 10.0 - 0.5) * 2.0);
|
2021-08-28 17:56:08 +00:00
|
|
|
}
|
|
|
|
}
|