2021-05-19 03:00:02 +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-19 03:00:02 +00:00
|
|
|
// See README.md and COPYING for details.
|
|
|
|
|
2022-07-20 03:25:09 +00:00
|
|
|
use super::helpers::{cubic_interpolate, Trigger};
|
2022-07-17 09:58:28 +00:00
|
|
|
use crate::dsp::{at, denorm, denorm_offs, inp, out}; //, inp, denorm, denorm_v, inp_dir, at};
|
|
|
|
use crate::dsp::{DspNode, LedPhaseVals, NodeContext, NodeId, ProcBuf, SAtom};
|
|
|
|
use crate::nodes::{NodeAudioContext, NodeExecContext};
|
2021-05-19 03:00:02 +00:00
|
|
|
|
2021-06-12 07:12:25 +00:00
|
|
|
#[macro_export]
|
2022-07-17 09:58:28 +00:00
|
|
|
macro_rules! fa_sampl_dir {
|
|
|
|
($formatter: expr, $v: expr, $denorm_v: expr) => {{
|
|
|
|
let s = match ($v.round() as usize) {
|
|
|
|
0 => "Forward",
|
|
|
|
1 => "Reverse",
|
|
|
|
_ => "?",
|
2021-06-12 07:12:25 +00:00
|
|
|
};
|
2022-07-17 09:58:28 +00:00
|
|
|
write!($formatter, "{}", s)
|
|
|
|
}};
|
|
|
|
}
|
2021-06-12 07:12:25 +00:00
|
|
|
|
2021-06-04 16:14:41 +00:00
|
|
|
#[macro_export]
|
2022-07-17 09:58:28 +00:00
|
|
|
macro_rules! fa_sampl_dclick {
|
|
|
|
($formatter: expr, $v: expr, $denorm_v: expr) => {{
|
|
|
|
let s = match ($v.round() as usize) {
|
|
|
|
0 => "Off",
|
|
|
|
1 => "On",
|
|
|
|
_ => "?",
|
2021-06-04 16:14:41 +00:00
|
|
|
};
|
2022-07-17 09:58:28 +00:00
|
|
|
write!($formatter, "{}", s)
|
|
|
|
}};
|
|
|
|
}
|
2021-06-04 16:14:41 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
2022-07-17 09:58:28 +00:00
|
|
|
macro_rules! fa_sampl_pmode {
|
|
|
|
($formatter: expr, $v: expr, $denorm_v: expr) => {{
|
|
|
|
let s = match ($v.round() as usize) {
|
|
|
|
0 => "Loop",
|
|
|
|
1 => "OneShot",
|
|
|
|
_ => "?",
|
2021-06-04 16:14:41 +00:00
|
|
|
};
|
2022-07-17 09:58:28 +00:00
|
|
|
write!($formatter, "{}", s)
|
|
|
|
}};
|
|
|
|
}
|
2021-06-04 16:14:41 +00:00
|
|
|
|
2021-05-19 03:00:02 +00:00
|
|
|
/// A simple amplifier
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Sampl {
|
2022-07-17 09:58:28 +00:00
|
|
|
phase: f64,
|
|
|
|
srate: f64,
|
|
|
|
trig: Trigger,
|
|
|
|
is_playing: bool,
|
|
|
|
last_sample: f32,
|
|
|
|
decaying: f32,
|
2021-05-19 03:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Sampl {
|
2021-06-03 03:10:29 +00:00
|
|
|
pub fn new(_nid: &NodeId) -> Self {
|
2021-05-19 03:00:02 +00:00
|
|
|
Self {
|
2022-07-17 09:58:28 +00:00
|
|
|
phase: 0.0,
|
|
|
|
srate: 44100.0,
|
|
|
|
trig: Trigger::new(),
|
|
|
|
is_playing: false,
|
|
|
|
last_sample: 0.0,
|
|
|
|
decaying: 0.0,
|
2021-05-19 03:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const freq: &'static str =
|
2021-05-28 03:14:56 +00:00
|
|
|
"Sampl freq\nPitch input for the sampler, giving the playback speed of the \
|
2021-05-19 03:00:02 +00:00
|
|
|
sample.\nRange: (-1..1)\n";
|
2021-05-22 10:13:38 +00:00
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const trig: &'static str =
|
2021-05-28 03:14:56 +00:00
|
|
|
"Sampl trig\nThe trigger input causes a resync of the playback phase \
|
2021-05-22 10:13:38 +00:00
|
|
|
and triggers the playback if the 'pmode' is 'OneShot'";
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const offs: &'static str = "Sampl offs\nStart position offset.\nRange: (0..1)\n";
|
|
|
|
pub const len: &'static str =
|
2021-06-11 03:05:42 +00:00
|
|
|
"Sampl len\nAdjusts the playback length of the sample in relation \
|
|
|
|
to the original length of the sample.\nRange: (0..1)\n";
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const dcms: &'static str =
|
2021-05-29 13:28:41 +00:00
|
|
|
"Sampl dcms\nDeclick fade time in milliseconds.\nNot audio rate!\nRange: (0..1)\n";
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const det: &'static str = "Sin det\nDetune the oscillator in semitones and cents. \
|
2021-06-06 07:24:41 +00:00
|
|
|
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\
|
2021-06-08 03:08:52 +00:00
|
|
|
Note: The signal input allows detune +-10 octaves.\
|
|
|
|
\nRange: (Knob -0.2 .. 0.2) / (Signal -1.0 .. 1.0)\n";
|
2021-05-22 10:13:38 +00:00
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const sample: &'static str =
|
2021-05-19 03:00:02 +00:00
|
|
|
"Sampl sample\nThe audio sample that is played back.\nRange: (-1..1)\n";
|
2021-05-22 10:13:38 +00:00
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const pmode: &'static str = "Sampl pmode\nThe playback mode of the sampler.\n\
|
2021-05-28 03:14:56 +00:00
|
|
|
- 'Loop' constantly plays back the sample. You can reset/sync the phase \
|
2021-05-22 10:13:38 +00:00
|
|
|
using the 'trig' input in this case.\n\
|
|
|
|
- 'OneShot' plays back the sample if a trigger is received on 'trig' input.\n";
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const dclick: &'static str =
|
2021-06-08 03:08:52 +00:00
|
|
|
"Sampl dclick\nIf this is enabled it will enable short fade in and out ramps.\n\
|
2021-05-28 03:14:56 +00:00
|
|
|
This if useful if you don't want to add an envelope just for \
|
2021-05-22 10:13:38 +00:00
|
|
|
getting rid of the clicks if spos and epos are modulated.";
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const dir: &'static str =
|
2021-06-12 07:20:44 +00:00
|
|
|
"Sampl dir\nSets the direction of the playhead, plays the sample \
|
|
|
|
forwards or backwards.";
|
2021-05-22 10:13:38 +00:00
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const sig: &'static str = "Sampl sig\nSampler audio output\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 = "Sample Player\n\n\
|
2021-06-08 03:08:52 +00:00
|
|
|
Provides a simple sample player that you can load a single audio \
|
|
|
|
sample from a WAV file into.";
|
2022-07-17 09:58:28 +00:00
|
|
|
pub const HELP: &'static str = r#"Sample Player
|
2021-06-08 03:08:52 +00:00
|
|
|
|
|
|
|
Provides a simple sample player for playing back one loaded audio sample.
|
|
|
|
It can be used for purposes like:
|
|
|
|
|
|
|
|
* Adding ambient samples to your patches.
|
|
|
|
* Using drum samples (set 'pmode' to 'OneShot').
|
|
|
|
* Having an oscillator with a custom waveform (set 'pmode' to 'Loop').
|
2021-08-31 03:00:27 +00:00
|
|
|
* As custom control signal source for very long or very custom envelopes.
|
2021-06-08 03:08:52 +00:00
|
|
|
|
|
|
|
Only a single audio sample can be loaded into this player. In HexoSynth
|
|
|
|
the sample selection can be done by the file browser in the right panel
|
|
|
|
in the 'Samples' tab.
|
|
|
|
|
|
|
|
You can adjust the playback speed of the sample either by the 'freq' parameter
|
|
|
|
or the 'det' parameter. You can offset into the sample using the 'offs'
|
2021-06-11 03:05:42 +00:00
|
|
|
parameter and modify the playback length relative to the original
|
|
|
|
sample length using the 'len' parameter.
|
2021-06-11 03:01:19 +00:00
|
|
|
|
2021-06-08 03:08:52 +00:00
|
|
|
Even though you are advised to use an envelope for controlling the playback
|
|
|
|
volume of the sample to prevent clicks a simple in and out ramp is provided
|
|
|
|
using by the 'dclick' setting. The length of these ramps can be controlled
|
|
|
|
using the 'dcms' parameter.
|
|
|
|
|
|
|
|
When 'pmode' is set to 'Loop' the sample will restart playing immediately
|
|
|
|
after it has finished. This is useful when you just want to load a waveform
|
|
|
|
into the sample player to use it as oscillator.
|
|
|
|
|
|
|
|
To start samples when 'pmode' is set to 'OneShot' a trigger input needs to
|
|
|
|
be provided on the 'trig' input port. The 'trig' input also works in
|
|
|
|
'Loop' mode to retrigger the sample.
|
|
|
|
"#;
|
2021-05-19 03:00:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 19:37:16 +00:00
|
|
|
impl Sampl {
|
2021-06-12 07:12:25 +00:00
|
|
|
#[allow(clippy::many_single_char_names)]
|
|
|
|
#[inline]
|
2022-07-20 03:25:09 +00:00
|
|
|
fn next_sample(
|
|
|
|
&mut self,
|
|
|
|
sr_factor: f64,
|
|
|
|
speed: f64,
|
|
|
|
sample_data: &[f32],
|
|
|
|
reverse: bool,
|
|
|
|
) -> f32 {
|
2021-05-28 19:37:16 +00:00
|
|
|
let sd_len = sample_data.len();
|
2022-07-17 09:58:28 +00:00
|
|
|
if sd_len < 1 {
|
|
|
|
return 0.0;
|
|
|
|
}
|
2021-05-19 03:00:02 +00:00
|
|
|
|
2022-07-20 03:25:09 +00:00
|
|
|
let i = self.phase.floor() as usize % sd_len;
|
2021-06-12 07:12:25 +00:00
|
|
|
let f = self.phase.fract();
|
2022-07-20 03:25:09 +00:00
|
|
|
self.phase = i as f64 + f + sr_factor * speed;
|
2021-05-28 19:37:16 +00:00
|
|
|
|
2022-07-20 03:25:09 +00:00
|
|
|
let (i, f) = if reverse { (((sd_len - 1) - i), 1.0 - f) } else { (i, f) };
|
2022-07-19 18:47:46 +00:00
|
|
|
cubic_interpolate(&sample_data[..], sd_len, i, f as f32)
|
2021-05-28 19:37:16 +00:00
|
|
|
}
|
2021-05-19 03:00:02 +00:00
|
|
|
|
2021-06-07 03:06:04 +00:00
|
|
|
#[allow(clippy::float_cmp)]
|
2021-05-19 03:00:02 +00:00
|
|
|
#[inline]
|
2022-07-17 09:58:28 +00:00
|
|
|
fn play(
|
|
|
|
&mut self,
|
|
|
|
inputs: &[ProcBuf],
|
|
|
|
nframes: usize,
|
|
|
|
sample_data: &[f32],
|
|
|
|
out: &mut ProcBuf,
|
|
|
|
do_loop: bool,
|
|
|
|
declick: bool,
|
|
|
|
reverse: bool,
|
|
|
|
) {
|
|
|
|
let freq = inp::Sampl::freq(inputs);
|
|
|
|
let trig = inp::Sampl::trig(inputs);
|
|
|
|
let offs = inp::Sampl::offs(inputs);
|
|
|
|
let len = inp::Sampl::len(inputs);
|
|
|
|
let dcms = inp::Sampl::dcms(inputs);
|
|
|
|
let det = inp::Sampl::det(inputs);
|
2021-05-28 19:37:16 +00:00
|
|
|
|
|
|
|
let sample_srate = sample_data[0] as f64;
|
2022-07-17 09:58:28 +00:00
|
|
|
let sample_data = &sample_data[1..];
|
|
|
|
let sr_factor = sample_srate / self.srate;
|
2021-05-28 19:37:16 +00:00
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
let ramp_time = denorm::Sampl::dcms(dcms, 0) as f64 * self.srate;
|
2021-05-29 13:22:32 +00:00
|
|
|
let ramp_sample_count = (ramp_time / 1000.0).ceil() as usize;
|
2022-07-17 09:58:28 +00:00
|
|
|
let ramp_inc = 1000.0 / ramp_time;
|
2021-05-29 11:45:26 +00:00
|
|
|
|
2021-05-29 09:41:46 +00:00
|
|
|
let mut is_playing = self.is_playing;
|
2021-05-28 19:37:16 +00:00
|
|
|
|
2021-05-29 09:41:46 +00:00
|
|
|
if do_loop {
|
|
|
|
is_playing = true;
|
2021-05-28 19:37:16 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
let mut prev_offs = -10.0;
|
|
|
|
let mut prev_len = -10.0;
|
2021-05-19 03:00:02 +00:00
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
let mut start_idx = 0;
|
2021-05-29 09:41:46 +00:00
|
|
|
let mut end_idx_plus1 = sample_data.len();
|
2021-05-22 09:11:03 +00:00
|
|
|
|
2021-05-28 19:37:16 +00:00
|
|
|
for frame in 0..nframes {
|
|
|
|
let trig_val = denorm::Sampl::trig(trig, frame);
|
|
|
|
let triggered = self.trig.check_trigger(trig_val);
|
2021-05-22 09:11:03 +00:00
|
|
|
|
2021-05-28 19:37:16 +00:00
|
|
|
if triggered {
|
|
|
|
self.phase = 0.0;
|
2021-05-29 19:15:01 +00:00
|
|
|
self.decaying = self.last_sample;
|
2021-05-28 19:37:16 +00:00
|
|
|
is_playing = true;
|
|
|
|
}
|
2021-05-22 07:19:11 +00:00
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
let s = if is_playing {
|
|
|
|
let freq = denorm_offs::Sampl::freq(freq, det.read(frame), frame);
|
|
|
|
let playback_speed = freq / 440.0;
|
|
|
|
|
|
|
|
let prev_phase = self.phase;
|
|
|
|
|
|
|
|
let sd_len = sample_data.len();
|
|
|
|
|
|
|
|
let cur_offs = denorm::Sampl::offs(offs, frame).abs().min(0.999999) as f64;
|
|
|
|
let recalc_end = if prev_offs != cur_offs {
|
|
|
|
start_idx = ((sd_len as f64 * cur_offs).floor() as usize).min(sd_len);
|
|
|
|
prev_offs = cur_offs;
|
|
|
|
true
|
2021-05-29 19:00:45 +00:00
|
|
|
} else {
|
2022-07-17 09:58:28 +00:00
|
|
|
false
|
2021-05-29 19:00:45 +00:00
|
|
|
};
|
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
let cur_len = denorm::Sampl::len(len, frame).abs().min(1.0) as f64;
|
|
|
|
if recalc_end || prev_len != cur_len {
|
|
|
|
let max_sd_len = (sd_len as f64 * cur_len as f64).round() as usize;
|
|
|
|
|
|
|
|
let remain_s_len =
|
|
|
|
if start_idx <= sd_len { (sd_len - start_idx).min(max_sd_len) } else { 0 };
|
|
|
|
|
|
|
|
end_idx_plus1 = remain_s_len;
|
|
|
|
|
|
|
|
prev_len = cur_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
let sample_slice = &sample_data[start_idx..(start_idx + end_idx_plus1)];
|
|
|
|
|
|
|
|
// next_sample mutates self.phase, so we need the current phase
|
|
|
|
// that is used for looking up the sample from the audio data.
|
|
|
|
let sample_idx = self.phase.floor() as usize;
|
|
|
|
|
2022-07-20 03:25:09 +00:00
|
|
|
let mut s =
|
|
|
|
self.next_sample(sr_factor, playback_speed as f64, sample_slice, reverse);
|
2021-05-29 19:15:01 +00:00
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
if declick {
|
|
|
|
let samples_to_end = sample_slice.len() - sample_idx;
|
|
|
|
|
|
|
|
let ramp_atten_factor = if sample_idx < ramp_sample_count {
|
|
|
|
sample_idx as f64 * ramp_inc
|
|
|
|
} else if samples_to_end < ramp_sample_count {
|
|
|
|
samples_to_end as f64 * ramp_inc
|
|
|
|
} else {
|
|
|
|
1.0
|
|
|
|
};
|
|
|
|
|
|
|
|
s *= ramp_atten_factor as f32;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.last_sample = s;
|
|
|
|
out.write(frame, s);
|
|
|
|
|
|
|
|
if !do_loop && prev_phase > self.phase {
|
|
|
|
// played past end => stop playing.
|
|
|
|
is_playing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
s
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
|
|
|
|
|
|
|
let s = if !declick || self.decaying.abs() < 0.00001 {
|
|
|
|
self.decaying = 0.0;
|
|
|
|
s
|
|
|
|
} else {
|
|
|
|
self.decaying *= 0.98;
|
|
|
|
(s + self.decaying).clamp(-1.0, 1.0)
|
|
|
|
};
|
|
|
|
|
2021-05-29 19:15:01 +00:00
|
|
|
self.last_sample = s;
|
2021-05-29 19:00:45 +00:00
|
|
|
out.write(frame, s);
|
2021-05-28 19:37:16 +00:00
|
|
|
}
|
2021-05-22 09:11:03 +00:00
|
|
|
|
2021-05-28 19:37:16 +00:00
|
|
|
self.is_playing = is_playing;
|
|
|
|
}
|
|
|
|
}
|
2021-05-22 09:11:03 +00:00
|
|
|
|
2021-05-28 19:37:16 +00:00
|
|
|
impl DspNode for Sampl {
|
2022-07-17 09:58:28 +00:00
|
|
|
fn outputs() -> usize {
|
|
|
|
1
|
|
|
|
}
|
2021-05-22 07:19:11 +00:00
|
|
|
|
2022-07-17 09:58:28 +00:00
|
|
|
fn set_sample_rate(&mut self, srate: f32) {
|
|
|
|
self.srate = srate.into();
|
|
|
|
}
|
2021-05-28 19:37:16 +00:00
|
|
|
fn reset(&mut self) {
|
|
|
|
self.trig.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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,
|
|
|
|
) {
|
2021-05-28 19:37:16 +00:00
|
|
|
let sample = at::Sampl::sample(atoms);
|
2022-07-17 09:58:28 +00:00
|
|
|
let pmode = at::Sampl::pmode(atoms);
|
2021-05-29 11:45:26 +00:00
|
|
|
let dclick = at::Sampl::dclick(atoms);
|
2022-07-17 09:58:28 +00:00
|
|
|
let dir = at::Sampl::dir(atoms);
|
|
|
|
let out = out::Sampl::sig(outputs);
|
2021-05-28 19:37:16 +00:00
|
|
|
|
|
|
|
if let SAtom::AudioSample((_, Some(sample_data))) = sample {
|
2021-05-29 09:41:46 +00:00
|
|
|
// 3 is for sample-sample-rate and at least 2 audio samples.
|
|
|
|
if sample_data.len() < 3 {
|
|
|
|
for frame in 0..ctx.nframes() {
|
|
|
|
out.write(frame, 0.0);
|
|
|
|
}
|
2021-05-29 19:15:01 +00:00
|
|
|
self.last_sample = 0.0;
|
2021-05-29 09:41:46 +00:00
|
|
|
return;
|
2021-05-22 07:19:11 +00:00
|
|
|
}
|
2021-05-29 09:41:46 +00:00
|
|
|
|
|
|
|
self.play(
|
|
|
|
inputs,
|
|
|
|
ctx.nframes(),
|
|
|
|
&sample_data[..],
|
|
|
|
out,
|
2021-05-29 11:45:26 +00:00
|
|
|
pmode.i() == 0,
|
2021-06-12 07:12:25 +00:00
|
|
|
dclick.i() == 1,
|
2022-07-17 09:58:28 +00:00
|
|
|
dir.i() == 1,
|
|
|
|
);
|
2021-05-22 07:19:11 +00:00
|
|
|
} else {
|
|
|
|
for frame in 0..ctx.nframes() {
|
|
|
|
out.write(frame, 0.0);
|
|
|
|
}
|
2021-05-29 19:15:01 +00:00
|
|
|
self.last_sample = 0.0;
|
2021-05-19 03:00:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 19:37:16 +00:00
|
|
|
let last_frame = ctx.nframes() - 1;
|
|
|
|
ctx_vals[0].set(out.read(last_frame));
|
2021-05-19 03:00:02 +00:00
|
|
|
}
|
|
|
|
}
|