2021-08-23 16:42:39 +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.
|
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
use crate::dsp::helpers::{ChangeTrig, CtrlPitchQuantizer};
|
|
|
|
use crate::dsp::{DspNode, LedPhaseVals, NodeContext, NodeId, ProcBuf, SAtom};
|
2021-08-23 16:42:39 +00:00
|
|
|
use crate::nodes::{NodeAudioContext, NodeExecContext};
|
|
|
|
|
|
|
|
#[macro_export]
|
2022-07-17 09:58:28 +00:00
|
|
|
macro_rules! fa_cqnt {
|
|
|
|
($formatter: expr, $v: expr, $denorm_v: expr) => {{
|
|
|
|
write!($formatter, "?")
|
|
|
|
}};
|
|
|
|
}
|
2021-08-23 16:42:39 +00:00
|
|
|
|
2021-08-24 01:59:41 +00:00
|
|
|
#[macro_export]
|
2022-07-17 09:58:28 +00:00
|
|
|
macro_rules! fa_cqnt_omin {
|
|
|
|
($formatter: expr, $v: expr, $denorm_v: expr) => {{
|
|
|
|
let s = match ($v.round() as usize) {
|
|
|
|
0 => "-0",
|
|
|
|
1 => "-1",
|
|
|
|
2 => "-2",
|
|
|
|
3 => "-3",
|
|
|
|
4 => "-4",
|
|
|
|
_ => "?",
|
2021-08-24 01:59:41 +00:00
|
|
|
};
|
2022-07-17 09:58:28 +00:00
|
|
|
write!($formatter, "{}", s)
|
|
|
|
}};
|
|
|
|
}
|
2021-08-24 01:59:41 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
2022-07-17 09:58:28 +00:00
|
|
|
macro_rules! fa_cqnt_omax {
|
|
|
|
($formatter: expr, $v: expr, $denorm_v: expr) => {{
|
|
|
|
let s = match ($v.round() as usize) {
|
|
|
|
0 => "+0",
|
|
|
|
1 => "+1",
|
|
|
|
2 => "+2",
|
|
|
|
3 => "+3",
|
|
|
|
4 => "+4",
|
|
|
|
_ => "?",
|
2021-08-24 01:59:41 +00:00
|
|
|
};
|
2022-07-17 09:58:28 +00:00
|
|
|
write!($formatter, "{}", s)
|
|
|
|
}};
|
|
|
|
}
|
2021-08-24 01:59:41 +00:00
|
|
|
|
2021-08-28 17:56:08 +00:00
|
|
|
/// A control signal to pitch quantizer/converter
|
2021-08-23 16:42:39 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2021-08-25 19:14:41 +00:00
|
|
|
pub struct CQnt {
|
2022-07-17 09:58:28 +00:00
|
|
|
quant: Box<CtrlPitchQuantizer>,
|
2021-08-30 01:55:42 +00:00
|
|
|
change_trig: ChangeTrig,
|
2021-08-23 16:42:39 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 19:14:41 +00:00
|
|
|
impl CQnt {
|
2021-08-23 16:42:39 +00:00
|
|
|
pub fn new(_nid: &NodeId) -> Self {
|
2022-07-17 09:58:28 +00:00
|
|
|
Self { quant: Box::new(CtrlPitchQuantizer::new()), change_trig: ChangeTrig::new() }
|
2021-08-23 16:42:39 +00:00
|
|
|
}
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const inp: &'static str =
|
2021-09-06 01:35:57 +00:00
|
|
|
"CQnt inp\nThe unipolar input signal that is to be mapped to the \
|
|
|
|
selected pitch range.\nRange: (0..1)";
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const oct: &'static str = "CQnt oct\nThe octave offset from A4.\nRange: (-1..1)";
|
|
|
|
pub const omin: &'static str =
|
2021-09-06 01:35:57 +00:00
|
|
|
"CQnt omin\nThe minimum octave of the range. If 0 it will be 'oct'.\nRange: (-1..1)";
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const omax: &'static str =
|
2021-09-06 01:35:57 +00:00
|
|
|
"CQnt omax\nThe maximum octave of the range. If 0 it will be 'oct'.\nRange: (-1..1)";
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const sig: &'static str = "CQnt sig\nThe output pitch signal.\nRange: (-1..1)";
|
|
|
|
pub const t: &'static str = "CQnt t\nEverytime the quantizer snaps to a new pitch, it will \
|
2021-08-30 01:55:42 +00:00
|
|
|
emit a short trigger on this signal output. This is useful \
|
|
|
|
to trigger for example an envelope.";
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const keys: &'static str =
|
2021-09-06 01:35:57 +00:00
|
|
|
"CQnt keys\nHere you can select the individual notes of the range. \
|
|
|
|
If no note is selected, it's the same as if all notes were selected.";
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const DESC: &'static str = r#"Ctrl Pitch Quantizer
|
2021-08-23 16:42:39 +00:00
|
|
|
|
2021-09-06 01:35:57 +00:00
|
|
|
This special quantizer maps the unipolar 0..1 control signal input range on 'inp' evenly to the selected keys and octaves.
|
2021-08-23 16:42:39 +00:00
|
|
|
"#;
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const HELP: &'static str = r#"CQnt - A control signal to pitch quantizer
|
2021-08-23 16:42:39 +00:00
|
|
|
|
2021-09-06 01:35:57 +00:00
|
|
|
This is a specialized control signal quantizer to generate a pitch/frequency
|
|
|
|
from a signal within the 0..1 range. It does not quantize a typical -1..1
|
|
|
|
frequency signal like the 'Quant' node.
|
|
|
|
|
|
|
|
In contrast to 'Quant', this quantizer maps the incoming signal evenly
|
|
|
|
to the available note range. It will result in more evenly played notes
|
|
|
|
if you sweep accross the input signal range.
|
2021-08-23 16:42:39 +00:00
|
|
|
"#;
|
|
|
|
}
|
|
|
|
|
2021-08-25 19:14:41 +00:00
|
|
|
impl DspNode for CQnt {
|
2022-07-17 09:58:28 +00:00
|
|
|
fn outputs() -> usize {
|
|
|
|
1
|
|
|
|
}
|
2021-08-23 16:42:39 +00:00
|
|
|
|
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-23 16:42:39 +00:00
|
|
|
#[inline]
|
|
|
|
fn process<T: NodeAudioContext>(
|
2022-07-17 09:58:28 +00:00
|
|
|
&mut self,
|
|
|
|
ctx: &mut T,
|
|
|
|
_ectx: &mut NodeExecContext,
|
2021-10-30 17:40:11 +00:00
|
|
|
_nctx: &NodeContext,
|
2022-07-17 09:58:28 +00:00
|
|
|
atoms: &[SAtom],
|
|
|
|
inputs: &[ProcBuf],
|
|
|
|
outputs: &mut [ProcBuf],
|
|
|
|
ctx_vals: LedPhaseVals,
|
|
|
|
) {
|
|
|
|
use crate::dsp::{at, denorm, inp, out_buf};
|
2021-08-23 16:42:39 +00:00
|
|
|
|
2021-08-25 19:14:41 +00:00
|
|
|
let inp = inp::CQnt::inp(inputs);
|
|
|
|
let oct = inp::CQnt::oct(inputs);
|
2021-08-30 01:55:42 +00:00
|
|
|
let mut out = out_buf::CQnt::sig(outputs);
|
2022-07-17 09:58:28 +00:00
|
|
|
let mut t = out_buf::CQnt::t(outputs);
|
2021-08-25 19:14:41 +00:00
|
|
|
let keys = at::CQnt::keys(atoms);
|
|
|
|
let omin = at::CQnt::omin(atoms);
|
|
|
|
let omax = at::CQnt::omax(atoms);
|
2021-08-23 03:10:33 +00:00
|
|
|
|
2021-08-25 03:07:24 +00:00
|
|
|
self.quant.update_keys(keys.i(), omin.i(), omax.i());
|
2021-08-23 03:10:33 +00:00
|
|
|
|
2021-08-29 08:59:59 +00:00
|
|
|
for frame in 0..ctx.nframes() {
|
2022-07-17 09:58:28 +00:00
|
|
|
let pitch = self.quant.signal_to_pitch(denorm::CQnt::inp(inp, frame));
|
2021-08-30 01:55:42 +00:00
|
|
|
|
|
|
|
t.write(frame, self.change_trig.next(pitch));
|
2021-08-29 08:59:59 +00:00
|
|
|
out.write(frame, pitch + denorm::CQnt::oct(oct, frame));
|
2021-08-23 03:10:33 +00:00
|
|
|
}
|
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-23 16:42:39 +00:00
|
|
|
}
|
|
|
|
}
|