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.
|
|
|
|
|
|
|
|
use crate::nodes::{NodeAudioContext, NodeExecContext};
|
|
|
|
use crate::dsp::{NodeId, SAtom, ProcBuf, DspNode, LedPhaseVals, NodeContext};
|
2021-08-25 19:14:41 +00:00
|
|
|
use crate::dsp::helpers::CtrlPitchQuantizer;
|
2021-08-23 16:42:39 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
2021-08-25 19:14:41 +00:00
|
|
|
macro_rules! fa_cqnt { ($formatter: expr, $v: expr, $denorm_v: expr) => { {
|
2021-08-23 16:42:39 +00:00
|
|
|
write!($formatter, "?")
|
|
|
|
} } }
|
|
|
|
|
2021-08-24 01:59:41 +00:00
|
|
|
#[macro_export]
|
2021-08-25 19:14:41 +00:00
|
|
|
macro_rules! fa_cqnt_omin { ($formatter: expr, $v: expr, $denorm_v: expr) => { {
|
2021-08-24 01:59:41 +00:00
|
|
|
let s =
|
|
|
|
match ($v.round() as usize) {
|
|
|
|
0 => "-0",
|
|
|
|
1 => "-1",
|
|
|
|
2 => "-2",
|
|
|
|
3 => "-3",
|
|
|
|
4 => "-4",
|
|
|
|
_ => "?",
|
|
|
|
};
|
|
|
|
write!($formatter, "{}", s)
|
|
|
|
} } }
|
|
|
|
|
|
|
|
#[macro_export]
|
2021-08-25 19:14:41 +00:00
|
|
|
macro_rules! fa_cqnt_omax { ($formatter: expr, $v: expr, $denorm_v: expr) => { {
|
2021-08-24 01:59:41 +00:00
|
|
|
let s =
|
|
|
|
match ($v.round() as usize) {
|
|
|
|
0 => "+0",
|
|
|
|
1 => "+1",
|
|
|
|
2 => "+2",
|
|
|
|
3 => "+3",
|
|
|
|
4 => "+4",
|
|
|
|
_ => "?",
|
|
|
|
};
|
|
|
|
write!($formatter, "{}", s)
|
|
|
|
} } }
|
|
|
|
|
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 {
|
|
|
|
quant: Box<CtrlPitchQuantizer>,
|
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 {
|
|
|
|
Self {
|
2021-08-25 19:14:41 +00:00
|
|
|
quant: Box::new(CtrlPitchQuantizer::new()),
|
2021-08-23 16:42:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pub const inp : &'static str =
|
2021-08-25 19:14:41 +00:00
|
|
|
"CQnt inp\n\nRange: (0..1)";
|
2021-08-23 16:42:39 +00:00
|
|
|
pub const oct : &'static str =
|
2021-08-25 19:14:41 +00:00
|
|
|
"CQnt oct\n\nRange: (-1..1)";
|
2021-08-24 01:59:41 +00:00
|
|
|
pub const omin : &'static str =
|
2021-08-25 19:14:41 +00:00
|
|
|
"CQnt omin\n\nRange: (-1..1)";
|
2021-08-24 01:59:41 +00:00
|
|
|
pub const omax : &'static str =
|
2021-08-25 19:14:41 +00:00
|
|
|
"CQnt omax\n\nRange: (-1..1)";
|
2021-08-23 16:42:39 +00:00
|
|
|
pub const sig : &'static str =
|
2021-08-25 19:14:41 +00:00
|
|
|
"CQnt sig\n\nRange: (-1..1)";
|
2021-08-23 16:42:39 +00:00
|
|
|
pub const keys : &'static str =
|
2021-08-25 19:14:41 +00:00
|
|
|
"CQnt keys\n";
|
2021-08-23 16:42:39 +00:00
|
|
|
pub const DESC : &'static str =
|
2021-08-25 19:14:41 +00:00
|
|
|
r#"Control Pitch Quantizer
|
2021-08-23 16:42:39 +00:00
|
|
|
|
2021-08-25 19:14:41 +00:00
|
|
|
This special quantizer maps the 0..1 input range on 'inp' evenly to the selected keys and octaves.
|
2021-08-23 16:42:39 +00:00
|
|
|
"#;
|
|
|
|
pub const HELP : &'static str =
|
2021-08-25 19:14:41 +00:00
|
|
|
r#"CQnt - A control signal to pitch quantizer
|
2021-08-23 16:42:39 +00:00
|
|
|
|
2021-08-25 19:14:41 +00:00
|
|
|
This is a specialized 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.
|
2021-08-23 16:42:39 +00:00
|
|
|
"#;
|
|
|
|
}
|
|
|
|
|
2021-08-25 19:14:41 +00:00
|
|
|
impl DspNode for CQnt {
|
2021-08-23 16:42:39 +00:00
|
|
|
fn outputs() -> usize { 1 }
|
|
|
|
|
|
|
|
fn set_sample_rate(&mut self, _srate: f32) { }
|
|
|
|
fn reset(&mut self) { }
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn process<T: NodeAudioContext>(
|
|
|
|
&mut self, ctx: &mut T, _ectx: &mut NodeExecContext,
|
|
|
|
nctx: &NodeContext,
|
|
|
|
atoms: &[SAtom], inputs: &[ProcBuf],
|
|
|
|
outputs: &mut [ProcBuf], ctx_vals: LedPhaseVals)
|
|
|
|
{
|
|
|
|
use crate::dsp::{at, out, inp, denorm};
|
|
|
|
|
2021-08-25 19:14:41 +00:00
|
|
|
let inp = inp::CQnt::inp(inputs);
|
|
|
|
let oct = inp::CQnt::oct(inputs);
|
|
|
|
let out = out::CQnt::sig(outputs);
|
|
|
|
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-25 03:07:24 +00:00
|
|
|
if self.quant.has_no_keys() {
|
2021-08-23 03:10:33 +00:00
|
|
|
for frame in 0..ctx.nframes() {
|
|
|
|
out.write(
|
|
|
|
frame,
|
2021-08-25 19:14:41 +00:00
|
|
|
denorm::CQnt::inp(inp, frame)
|
|
|
|
+ denorm::CQnt::oct(oct, frame));
|
2021-08-23 03:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx_vals[1].set(100.0); // some unreachable value for Keys widget
|
|
|
|
ctx_vals[0].set(out.read(ctx.nframes() - 1));
|
2021-08-24 01:59:41 +00:00
|
|
|
|
2021-08-23 03:10:33 +00:00
|
|
|
} else {
|
2021-08-24 01:59:41 +00:00
|
|
|
let mut last_key = 0;
|
2021-08-23 03:10:33 +00:00
|
|
|
|
|
|
|
for frame in 0..ctx.nframes() {
|
2021-08-25 03:07:24 +00:00
|
|
|
let pitch =
|
|
|
|
self.quant.signal_to_pitch(
|
2021-08-25 19:14:41 +00:00
|
|
|
denorm::CQnt::inp(inp, frame));
|
|
|
|
out.write(frame, pitch + denorm::CQnt::oct(oct, frame));
|
2021-08-23 03:10:33 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 03:07:24 +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 03:10:33 +00:00
|
|
|
}
|
2021-08-23 16:42:39 +00:00
|
|
|
}
|
|
|
|
}
|