Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,9 @@ sets the configuration cancels the drain. `reset()` also cancels the drain.
### Reset

`reset()` keeps the configuration, the random stream and some controller and
filter state. If you need a fully new state, construct a new processor.
filter state. Configuration changes keep the audio histories, also when
`resynthesis.stereo_output` changes the voice pan. If you need a fully new
state, construct a new processor.

## Feedback and telemetry

Expand Down Expand Up @@ -507,7 +509,7 @@ page that opens the JavaScript.
| `Int16Array` finite tail | `beginDrain()`, `drain(left, right)`, `drainMono(audio)` |
| `Float32Array` finite tail | `beginDrain()`, `drainF32(left, right)`, `drainMonoF32(audio)` |
| Zero-copy `Float32` tail | `beginDrain()`, `drainF32InPlace(frames)`, `drainMonoF32InPlace(frames)` |
| Configured tail budget | `tailFrames()` |
| Tail budget | `tailFrames()` |
| Reset or release memory | `reset()`, `free()`, `views.dispose()` |

### Audio buffers
Expand Down
5 changes: 3 additions & 2 deletions src/effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
//! Punch and Warm operate on mono PCM before resynthesis. The processor does
//! not call their sample methods while they are disabled. Alien and reverb
//! accept stereo Q14 frames after resynthesis. They have their own bypass
//! behavior. Alien continues to record during bypass. Reverb keeps its
//! current delay state during bypass.
//! transitions. Alien captures a fresh loop when you enable it, and ramps its
//! wet mix on each toggle. Reverb lets an active tail decay with zero input
//! during bypass, then clears its histories.
//!
//! The configuration types validate the public controls. The prepared stage
//! types assume that these checks were successful.
Expand Down
249 changes: 183 additions & 66 deletions src/effects/alien.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
//! Reverse-loop playback with a continuous input history and boundary fades.
//! Reverse-loop playback with a captured input history and boundary fades.

use crate::error::check_count;
use crate::numeric::fixed::{Fixed, Q14};
use crate::Error;

/// The number of history frames that the loop reads from.
const HISTORY_FRAMES: usize = 192_000;
/// The number of frames for the boundary fade at each loop boundary.
/// The maximum number of frames for the boundary fade and the wet ramp.
const FADE_FRAMES: usize = 200;

/// Returns the boundary fade length for a validated loop length.
#[inline]
fn boundary_fade_frames(loop_frames: usize) -> usize {
if loop_frames == 1 {
0
} else {
FADE_FRAMES.min(loop_frames / 2)
}
}

/// Reverse playback of recent stereo audio in repeated loops with fades.
///
/// Alien comes after resynthesis. Its input history and loop phase advance
/// also when Alien is disabled. Thus, when you enable Alien, it can play
/// audio that it recorded during bypass. A change of the loop length keeps
/// the history and clamps the current phase into the new loop. The boundary
/// fades are 200 frames long at all sample rates.
/// Alien comes after resynthesis. When you enable Alien, it captures one loop
/// of fresh input while it holds the dry signal. Then it ramps the wet mix to
/// full level over 200 frames. When you disable Alien, it ramps the wet mix to
/// zero over 200 frames.
///
/// If the loop length changes during wet playback, Alien ramps the active loop
/// to zero over 200 frames. Then it resets the phase and captures any extra
/// frames that the new loop needs. The boundary fade length is zero at a loop
/// length of one frame, and `min(200, loop_frames / 2)` at longer loop lengths.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
#[cfg_attr(
Expand All @@ -25,13 +39,12 @@ const FADE_FRAMES: usize = 200;
)]
pub struct AlienConfig {
/// Selects reversed playback instead of the direct input. The default is
/// `false`. The recording and the phase continue to advance in both
/// states.
/// `false`. Each toggle ramps the wet mix over 200 frames.
pub enabled: bool,
/// The loop length in frames, 1..=96000. The default is 95,999.
/// This value is a frame count at the two rates. Thus, its duration in
/// seconds changes with the sample rate. A short loop can be fully inside
/// the fades.
/// seconds changes with the sample rate. The boundary fade is shorter for
/// short loops and is zero when `loop_frames` is 1.
pub loop_frames: u32,
}
impl Default for AlienConfig {
Expand Down Expand Up @@ -66,100 +79,204 @@ pub(crate) struct Alien {
/// 192,000 stereo Q14 frames, which are allocated one time at
/// construction.
buffer: Vec<[Q14; 2]>,
/// The current recording position. The position of the reverse reader
/// comes from this cursor.
/// The current write position. The position of the reverse reader comes
/// from this cursor.
read: usize,
/// The current frame in the loop. It is always less than `loop_len`.
/// The current frame in the loop. It is always less than
/// `active_loop_len`.
phase: usize,
/// The validated loop length. It is not more than half of the history
/// capacity.
loop_len: usize,
/// The active loop length. It stays constant until `wet_frames` is zero.
active_loop_len: usize,
/// The number of frames in `buffer` since construction or the last reset.
written: usize,
/// The number of frames that Alien captured since it became active.
captured: usize,
/// The current frame in the 200-frame wet mix ramp, in 0..=200.
wet_frames: usize,
/// If `true`, the wet mix ramps to zero before the next loop state starts.
fading_out: bool,
/// If `true`, `captured` resets to zero when `wet_frames` reaches zero.
reset_capture: bool,
}
impl Alien {
/// Allocates a silent stereo ring from validated loop settings.
pub(crate) fn new(config: AlienConfig) -> Self {
let (captured, wet_frames) = if config.enabled {
(HISTORY_FRAMES, FADE_FRAMES)
} else {
(0, 0)
};
Self {
config,
buffer: vec![[Q14::default(); 2]; HISTORY_FRAMES],
read: 0,
phase: 0,
loop_len: config.loop_frames as usize,
active_loop_len: config.loop_frames as usize,
written: 0,
captured,
wet_frames,
fading_out: false,
reset_capture: false,
}
}
/// Applies a validated configuration, keeps the recorded audio and clamps
/// the phase into the new loop, which is not empty.
/// Applies a validated configuration and does not clear the buffer.
///
/// While `wet_frames` is above zero, `active_loop_len` stays unchanged
/// until the wet ramp reaches zero.
pub(crate) fn set_config(&mut self, config: AlienConfig) {
let was_enabled = self.config.enabled;
self.config = config;
self.loop_len = config.loop_frames as usize;
self.phase = self.phase.min(self.loop_len - 1);
if !config.enabled || !was_enabled {
if self.wet_frames == 0 {
self.active_loop_len = config.loop_frames as usize;
self.phase = 0;
self.captured = 0;
self.fading_out = false;
self.reset_capture = false;
} else {
self.fading_out = true;
self.reset_capture = true;
}
} else if self.wet_frames == 0 {
self.active_loop_len = config.loop_frames as usize;
self.phase = 0;
self.fading_out = false;
} else {
self.fading_out =
self.reset_capture || config.loop_frames as usize != self.active_loop_len;
}
}
/// Returns the loop length and the current frame in the loop.
/// Returns the active loop length and the current frame in the loop.
///
/// The frame is always less than the length. The stage state does not
/// change.
pub(crate) fn loop_state(&self) -> (usize, usize) {
(self.loop_len, self.phase)
(self.active_loop_len, self.phase)
}
/// Clears the recorded audio and sets both cursors to zero, but keeps the
/// configured loop.
/// Returns the finite tail budget for the active state or an active ramp.
pub(crate) fn tail_frames(&self) -> usize {
if self.config.enabled {
let base = 2 * self.config.loop_frames as usize + FADE_FRAMES;
if self.fading_out {
base.saturating_add(self.wet_frames)
} else {
base
}
} else {
self.wet_frames
}
}
/// Clears the recorded audio and resets the cursors and ramps, while it
/// keeps the configured loop.
pub(crate) fn reset(&mut self) {
self.buffer[..self.written].fill([Q14::default(); 2]);
self.written = 0;
self.read = 0;
self.phase = 0;
}
/// Records a frame, selects the direct or reverse output, then advances
/// both cursors.
/// The recording cursor moves forward by one and the reverse offset
/// increases by two. Thus, the playback moves backward by one frame. The
/// validated loop limit keeps that subtraction in a single ring wrap.
pub(crate) fn process_sample(&mut self, input: [Q14; 2]) -> [Q14; 2] {
self.buffer[self.read] = input;
if self.written < HISTORY_FRAMES {
self.written += 1;
self.active_loop_len = self.config.loop_frames as usize;
if self.config.enabled {
self.captured = HISTORY_FRAMES;
self.wet_frames = FADE_FRAMES;
} else {
self.captured = 0;
self.wet_frames = 0;
}
let result = if self.config.enabled {
let read = self.read + HISTORY_FRAMES - self.phase * 2;
let read = if read >= HISTORY_FRAMES {
read - HISTORY_FRAMES
} else {
read
};
let distance = self.phase.min(self.loop_len - self.phase);
let mut result = self.buffer[read];
if distance < FADE_FRAMES {
let gain: Q14 = Fixed::<0>::from_bits(distance as i32)
.div(Fixed::<0>::from_bits(FADE_FRAMES as i32));
result = result.map(|sample| sample.mul::<14, 14>(gain));
}
result
self.fading_out = false;
self.reset_capture = false;
}
/// Reads the current reverse frame and applies the loop boundary fade.
#[inline]
fn reverse_sample(&self) -> [Q14; 2] {
let read = self.read + HISTORY_FRAMES - self.phase * 2;
let read = if read >= HISTORY_FRAMES {
read - HISTORY_FRAMES
} else {
input
read
};
let distance = self.phase.min(self.active_loop_len - self.phase);
let fade_frames = boundary_fade_frames(self.active_loop_len);
let mut result = self.buffer[read];
if distance < fade_frames {
let gain: Q14 = Fixed::<0>::from_bits(distance as i32)
.div(Fixed::<0>::from_bits(fade_frames as i32));
result = result.map(|sample| sample.mul::<14, 14>(gain));
}
result
}
/// Blends the direct input and the reverse sample with the Q14 wet ramp.
#[inline]
fn mix_wet(&self, input: [Q14; 2], reverse: [Q14; 2]) -> [Q14; 2] {
if self.wet_frames == 0 {
input
} else if self.wet_frames >= FADE_FRAMES {
reverse
} else {
let wet: Q14 = Fixed::<0>::from_bits(self.wet_frames as i32)
.div(Fixed::<0>::from_bits(FADE_FRAMES as i32));
let dry = Q14::ONE - wet;
[
input[0].mul::<14, 14>(dry) + reverse[0].mul::<14, 14>(wet),
input[1].mul::<14, 14>(dry) + reverse[1].mul::<14, 14>(wet),
]
}
}
/// Advances the write cursor by one frame in the history ring.
#[inline]
fn advance_read(&mut self) {
self.read += 1;
if self.read == HISTORY_FRAMES {
self.read = 0;
}
}
/// Advances the loop phase by one frame and wraps at `active_loop_len`.
#[inline]
fn advance_phase(&mut self) {
self.phase += 1;
if self.phase == self.loop_len {
if self.phase == self.active_loop_len {
self.phase = 0;
}
result
}
}

/// Returns the finite tail budget: two loop lengths plus the boundary
/// fade.
///
/// The value is zero when Alien is disabled. It is a configured budget,
/// not a measured tail.
pub(crate) fn tail_frames(config: AlienConfig) -> usize {
if config.enabled {
2 * config.loop_frames as usize + FADE_FRAMES
} else {
0
/// Captures input, holds the dry signal until the loop fills, and ramps
/// the Q14 wet mix over 200 frames on each transition.
pub(crate) fn process_sample(&mut self, input: [Q14; 2]) -> [Q14; 2] {
if !self.config.enabled && self.wet_frames == 0 {
return input;
}
self.buffer[self.read] = input;
if self.written < HISTORY_FRAMES {
self.written += 1;
}
if self.config.enabled && !self.reset_capture {
self.captured = (self.captured + 1).min(HISTORY_FRAMES);
}
if self.fading_out || !self.config.enabled {
let reverse = self.reverse_sample();
self.wet_frames -= 1;
let result = self.mix_wet(input, reverse);
self.advance_read();
self.advance_phase();
if self.wet_frames == 0 {
self.active_loop_len = self.config.loop_frames as usize;
self.phase = 0;
self.fading_out = false;
if self.reset_capture || !self.config.enabled {
self.captured = 0;
self.reset_capture = false;
}
}
return result;
}
if self.captured < self.active_loop_len {
self.advance_read();
return input;
}
let reverse = self.reverse_sample();
if self.wet_frames < FADE_FRAMES {
self.wet_frames += 1;
}
let result = self.mix_wet(input, reverse);
self.advance_read();
self.advance_phase();
result
}
}
Loading
Loading