HexoDSP/src/dsp/node_sampl.rs

249 lines
8 KiB
Rust
Raw Normal View History

2021-05-19 03:00:02 +00:00
// Copyright (c) 2021 Weird Constructor <weirdconstructor@gmail.com>
// This is a part of HexoDSP. Released under (A)GPLv3 or any later.
// See README.md and COPYING for details.
use crate::nodes::NodeAudioContext;
use crate::dsp::{SAtom, ProcBuf, DspNode, LedPhaseVals};
use crate::dsp::{out, at, inp, denorm}; //, inp, denorm, denorm_v, inp_dir, at};
use super::helpers::Trigger;
2021-05-19 03:00:02 +00:00
/// A simple amplifier
#[derive(Debug, Clone)]
pub struct Sampl {
phase: f64,
srate: f64,
trig: Trigger,
is_playing: bool,
2021-05-19 03:00:02 +00:00
}
impl Sampl {
pub fn new() -> Self {
Self {
phase: 0.0,
srate: 44100.0,
trig: Trigger::new(),
is_playing: false,
2021-05-19 03:00:02 +00:00
}
}
pub const freq : &'static str =
"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
pub const trig : &'static str =
"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'";
pub const offs : &'static str =
"Sampl offs\nStart position offset.\nRange: (0..1)\n";
pub const len : &'static str =
"Sampl len\nLength of the sample, after the offset has been applied.\nRange: (0..1)\n";
2021-05-22 10:13:38 +00:00
2021-05-19 03:00:02 +00:00
pub const sample : &'static str =
"Sampl sample\nThe audio sample that is played back.\nRange: (-1..1)\n";
2021-05-22 10:13:38 +00:00
pub const pmode : &'static str =
"Sampl pmode\nThe playback mode of the sampler.\n\
- '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";
pub const dclick : &'static str =
"Sampl dclick\nIf this is enabled and the 'pmode' is 'OneShot' \
2021-05-22 10:13:38 +00:00
this will enable short fade in and out ramps.\n\
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.";
2021-05-19 03:00:02 +00:00
pub const sig : &'static str =
"Sampl sig\nSampler audio output\nRange: (-1..1)\n";
}
impl Sampl {
#[inline]
fn next_sample(&mut self, sr_factor: f64, speed: f64, sample_data: &[f32]) -> f32 {
let sd_len = sample_data.len();
if sd_len < 1 { return 0.0; }
2021-05-19 03:00:02 +00:00
let i = self.phase.floor() as usize + sd_len;
// Hermite interpolation, take from
// https://github.com/eric-wood/delay/blob/main/src/delay.rs#L52
//
// Thanks go to Eric Wood!
//
// For the interpolation code:
// MIT License, Copyright (c) 2021 Eric Wood
let xm1 = sample_data[(i - 1) % sd_len];
let x0 = sample_data[i % sd_len];
let x1 = sample_data[(i + 1) % sd_len];
let x2 = sample_data[(i + 2) % sd_len];
let c = (x1 - xm1) * 0.5;
let v = x0 - x1;
let w = c + v;
let a = w + v + (x2 - x0) * 0.5;
let b_neg = w + a;
let f = self.phase.fract();
self.phase = (i % sd_len) as f64 + f + sr_factor * speed;
let f = f as f32;
(((a * f) - b_neg) * f + c) * f + x0
}
2021-05-19 03:00:02 +00:00
// #[inline]
// fn play_loop(&mut self, inputs: &[ProcBuf], nframes: usize, sample_data: &[f32], out: &mut ProcBuf) {
// let freq = inp::Sampl::freq(inputs);
// let offs = inp::Sampl::offs(inputs);
// let len = inp::Sampl::len(inputs);
//
// let sample_srate = sample_data[0] as f64;
// let sample_data = &sample_data[1..];
// let sr_factor = sample_srate / self.srate;
//
// for frame in 0..nframes {
// let playback_speed =
// denorm::Sampl::freq(freq, frame) / 440.0;
//
// let start_idx =
// (sample_data.len() as f32
// * denorm::Sampl::offs(offs, frame).abs().min(0.999999))
// .floor() as usize;
//
// let end_idx_plus1 =
// ((sample_data.len() - start_idx) as f32
// * denorm::Sampl::len(offs, frame).abs().min(0.999999))
// .ceil() as usize;
//
// out.write(frame,
// self.next_sample(
// sr_factor,
// playback_speed as f64,
// sample_data[start_idx..end_idx_plus1]));
// }
// }
//
2021-05-19 03:00:02 +00:00
#[inline]
fn play(&mut self, inputs: &[ProcBuf], nframes: usize,
sample_data: &[f32], out: &mut ProcBuf, do_loop: 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 sample_srate = sample_data[0] as f64;
let sample_data = &sample_data[1..];
let sr_factor = sample_srate / self.srate;
let mut is_playing = self.is_playing;
if do_loop {
is_playing = true;
}
let mut prev_offs = -10.0;
let mut prev_len = -10.0;
2021-05-19 03:00:02 +00:00
let mut start_idx = 0;
let mut end_idx_plus1 = sample_data.len();
for frame in 0..nframes {
let trig_val = denorm::Sampl::trig(trig, frame);
let triggered = self.trig.check_trigger(trig_val);
if triggered {
self.phase = 0.0;
is_playing = true;
}
2021-05-22 07:19:11 +00:00
if is_playing {
let playback_speed =
denorm::Sampl::freq(freq, frame) / 440.0;
let prev_phase = self.phase;
let cur_offs =
denorm::Sampl::offs(offs, frame)
.abs().min(0.999999);
if prev_offs != cur_offs {
start_idx =
(sample_data.len() as f32 * cur_offs)
.floor() as usize;
prev_offs = cur_offs;
}
let cur_len =
denorm::Sampl::len(len, frame)
.abs().min(0.999999);
if prev_len != cur_len {
end_idx_plus1 =
((sample_data.len() - start_idx) as f32
* denorm::Sampl::len(len, frame).abs().min(0.999999))
.ceil() as usize;
prev_len = cur_len;
}
let s = self.next_sample(
sr_factor,
playback_speed as f64,
&sample_data[start_idx..(start_idx + end_idx_plus1)]);
out.write(frame, s);
if !do_loop && prev_phase > self.phase {
// played past end => stop playing.
is_playing = false;
}
} else {
out.write(frame, 0.0);
}
}
self.is_playing = is_playing;
}
}
impl DspNode for Sampl {
fn outputs() -> usize { 1 }
2021-05-22 07:19:11 +00:00
fn set_sample_rate(&mut self, srate: f32) { self.srate = srate.into(); }
fn reset(&mut self) {
self.trig.reset();
}
#[inline]
fn process<T: NodeAudioContext>(
&mut self, ctx: &mut T, atoms: &[SAtom], _params: &[ProcBuf],
inputs: &[ProcBuf], outputs: &mut [ProcBuf], ctx_vals: LedPhaseVals)
{
let sample = at::Sampl::sample(atoms);
let pmode = at::Sampl::pmode(atoms);
let out = out::Sampl::sig(outputs);
if let SAtom::AudioSample((_, Some(sample_data))) = sample {
// 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);
}
return;
2021-05-22 07:19:11 +00:00
}
self.play(
inputs,
ctx.nframes(),
&sample_data[..],
out,
pmode.i() == 0);
2021-05-22 07:19:11 +00:00
} else {
for frame in 0..ctx.nframes() {
out.write(frame, 0.0);
}
2021-05-19 03:00:02 +00:00
}
let last_frame = ctx.nframes() - 1;
ctx_vals[0].set(out.read(last_frame));
2021-05-19 03:00:02 +00:00
}
}