Skip to content
Merged
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
14 changes: 14 additions & 0 deletions bbx_core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Foundational utilities and data structures for the bbx_audio workspace.
- **Lock-free SPSC**: Single-producer single-consumer ring buffer
- **Stack-allocated vector**: Fixed-capacity vector without heap allocation
- **RNG**: Fast XorShift random number generator
- **Lookup tables**: Compile-time `SineTable` and a shared interpolating table read

## Cargo Features

Expand Down Expand Up @@ -95,6 +96,19 @@ let mut rng = XorShiftRng::new(42);
let noise_sample = rng.next_noise_sample(); // Returns -1.0 to 1.0
```

### `table`

Lookup tables read by phase in cycles. `SineTable` is evaluated at compile time (`no_std` friendly), and `read_interpolated` is the shared linear-interpolating reader for any `[f32; LENGTH]` table laid out as `LENGTH - 1` cells plus a guard entry.

```rust
use bbx_core::{SINE_2048, SineTable};

static SMALL: SineTable<1025> = SineTable::new();

let sine = SINE_2048.read(0.25); // sin(2π · 0.25) ≈ 1.0
let cosine = SINE_2048.read_cosine(0.0); // same table, a quarter cycle ahead
```

### `error`

Unified error types for the workspace.
Expand Down
2 changes: 2 additions & 0 deletions bbx_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod simd;
#[cfg(feature = "alloc")]
pub mod spsc;
pub mod stack_vec;
pub mod table;

pub use buffer::Buffer;
pub use denormal::{flush_denormal_f32, flush_denormal_f64, flush_denormals_f32_batch, flush_denormals_f64_batch};
Expand All @@ -29,3 +30,4 @@ pub use sample::Sample;
#[cfg(feature = "alloc")]
pub use spsc::{Consumer, Producer, SpscRingBuffer};
pub use stack_vec::StackVec;
pub use table::{SINE_2048, SineTable, read_interpolated};
87 changes: 87 additions & 0 deletions bbx_core/src/table/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//! Lookup tables read by phase in cycles.
//!
//! Every table in this module follows one layout: `[f32; LENGTH]` holds
//! `LENGTH - 1` cells plus a guard entry at `table[LENGTH - 1]`. For a periodic
//! table the guard repeats `table[0]`; for an unwrapped curve it holds the final
//! height. Either way the last cell interpolates towards the guard, so the read
//! path never checks for wrap-around.
//!
//! The storage length is the const parameter (rather than the cell count) because
//! spelling `[f32; N + 1]` in a type needs the unstable `generic_const_exprs`
//! feature. `LENGTH - 1` is expected to be a power of two.

mod sine;

pub use sine::{SINE_2048, SineTable};

use crate::math;

/// Read `table` at `phase` (in cycles) with linear interpolation.
///
/// `phase` is wrapped into `[0, 1)` with `floor`, so any real value is valid and
/// `read_interpolated(table, p + 1.0)` reads the same cell as `p`.
///
/// Taking an array reference rather than a slice lets the compiler see the
/// length, so the two element loads compile without bounds checks.
#[inline]
pub fn read_interpolated<const LENGTH: usize>(table: &[f32; LENGTH], phase: f32) -> f32 {
debug_assert!(LENGTH >= 2, "a table needs at least one cell and a guard entry");
let cells = LENGTH - 1;

let wrapped = phase - math::floor(phase);
let position = wrapped * cells as f32;
// `wrapped < 1.0` but `wrapped * cells` can still round up to exactly `cells` in f32;
// clamping keeps `index + 1` inside the array and is what lets the bounds checks vanish.
let index = (position as usize).min(cells - 1);
let fraction = position - index as f32;

let start = table[index];
start + fraction * (table[index + 1] - start)
}

#[cfg(test)]
mod tests {
use super::*;

/// Four cells of a ramp `0, 1, 2, 3` with the guard holding the final height `4`.
const RAMP: [f32; 5] = [0.0, 1.0, 2.0, 3.0, 4.0];

#[test]
fn test_reads_cell_boundaries_exactly() {
for (index, expected) in RAMP[..4].iter().enumerate() {
assert_eq!(read_interpolated(&RAMP, index as f32 / 4.0), *expected);
}
}

#[test]
fn test_interpolates_midpoints_exactly() {
assert_eq!(read_interpolated(&RAMP, 0.125), 0.5);
assert_eq!(read_interpolated(&RAMP, 0.375), 1.5);
assert_eq!(read_interpolated(&RAMP, 0.875), 3.5);
}

#[test]
fn test_last_cell_interpolates_towards_guard() {
let just_below_one = 1.0 - f32::EPSILON;
let value = read_interpolated(&RAMP, just_below_one);
assert!(value > 3.9 && value <= 4.0, "got {value}");
}

#[test]
fn test_wraps_phase_with_floor() {
for phase in [0.125f32, 0.375, 0.875] {
let base = read_interpolated(&RAMP, phase);
assert_eq!(read_interpolated(&RAMP, phase + 1.0), base);
assert_eq!(read_interpolated(&RAMP, phase + 3.0), base);
assert_eq!(read_interpolated(&RAMP, phase - 1.0), base);
assert_eq!(read_interpolated(&RAMP, phase - 2.0), base);
}
}

#[test]
fn test_single_cell_table() {
let table = [1.0f32, 3.0];
assert_eq!(read_interpolated(&table, 0.0), 1.0);
assert_eq!(read_interpolated(&table, 0.5), 2.0);
}
}
228 changes: 228 additions & 0 deletions bbx_core/src/table/sine.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
//! A sine table evaluated at compile time.

use super::read_interpolated;

/// A sine wave sampled over one cycle, built at compile time.
///
/// `LENGTH - 1` cells plus a guard entry that repeats cell zero (see the
/// [`table`](crate::table) module for the layout). The cell count must be a power
/// of two, so the everyday instance is [`SINE_2048`] (`SineTable<2049>`, 8 KB).
///
/// # Accuracy
///
/// Linear interpolation with step `h = 2π / N` has error bounded by
/// `h² / 8 · max|sin''| = (2π / N)² / 8`:
///
/// | cells | bound | level |
/// |-------|----------|---------|
/// | 1024 | 4.7e-6 | −106 dB |
/// | 2048 | 1.2e-6 | −118 dB |
/// | 4096 | 3.0e-7 | −130 dB |
///
/// The entries themselves are rounded from `f64` and sit within `1e-6` of the
/// true sine. Tables beyond 4096 cells stop fitting in L1 cache and read slower,
/// not better.
///
/// # Building
///
/// `f64::sin` is not a `const fn`, so entries come from a Taylor series on a
/// quarter cycle (terms through `x¹¹ / 11!`, whose first dropped term at `π/2`
/// is about `5.7e-8`, below `f32` epsilon) and the remaining three quarters
/// from symmetry.
///
/// ```
/// use bbx_core::{SINE_2048, SineTable};
///
/// static QUIET: SineTable<1025> = SineTable::new();
///
/// assert!((SINE_2048.read(0.25) - 1.0).abs() < 1e-6);
/// assert!((QUIET.read_cosine(0.0) - 1.0).abs() < 1e-6);
/// ```
#[derive(Debug, Clone)]
pub struct SineTable<const LENGTH: usize> {
entries: [f32; LENGTH],
}

/// One cycle of sine over 2048 cells.
///
/// A `static` rather than a `const` so every reader shares this one copy; a
/// `const` would be re-materialised (an 8 KB copy) wherever it is used by value.
pub static SINE_2048: SineTable<2049> = SineTable::new();

// No `Default`: it would rebuild the table at runtime (thousands of f64 Taylor
// iterations, soft-float on Cortex-M7), which must be a deliberate `new()` call.
#[allow(clippy::new_without_default)]
impl<const LENGTH: usize> SineTable<LENGTH> {
/// Number of cells, one less than the storage length.
pub const CELLS: usize = LENGTH - 1;

/// Evaluate the table. Usable in `const` and `static` initialisers.
///
/// # Panics
///
/// At compile time (or at runtime if called there) when `LENGTH - 1` is not
/// a power of two.
pub const fn new() -> Self {
assert!(LENGTH >= 2, "SineTable needs at least one cell and a guard entry");
assert!(
(LENGTH - 1).is_power_of_two(),
"SineTable cell count (LENGTH - 1) must be a power of two"
);

let cells = LENGTH - 1;
let mut entries = [0.0f32; LENGTH];
let mut index = 0;
while index < cells {
entries[index] = sine_of_cycles(index as f64 / cells as f64) as f32;
index += 1;
}
entries[cells] = entries[0];

Self { entries }
}

/// `sin(2π · phase)` with `phase` in cycles, wrapped and interpolated.
#[inline]
pub fn read(&self, phase: f32) -> f32 {
read_interpolated(&self.entries, phase)
}

/// `cos(2π · phase)`, read from the same table a quarter cycle ahead.
#[inline]
pub fn read_cosine(&self, phase: f32) -> f32 {
self.read(phase + 0.25)
}

/// The raw entries, guard included.
#[inline]
pub const fn entries(&self) -> &[f32; LENGTH] {
&self.entries
}
}

/// `sin(2π · cycles)` for `cycles` in `[0, 1)`, reduced to a quarter cycle by symmetry.
const fn sine_of_cycles(cycles: f64) -> f64 {
let (sign, half) = if cycles >= 0.5 {
(-1.0, cycles - 0.5)
} else {
(1.0, cycles)
};
let quarter = if half > 0.25 { 0.5 - half } else { half };
sign * taylor_sin_quarter(core::f64::consts::TAU * quarter)
}

/// Taylor series for `sin(x)` on `[0, π/2]`.
const fn taylor_sin_quarter(x: f64) -> f64 {
let x_squared = x * x;
let mut term = x;
let mut sum = x;
let mut k = 1.0;
while k < 12.0 {
term *= -x_squared / ((k + 1.0) * (k + 2.0));
sum += term;
k += 2.0;
}
sum
}

#[cfg(test)]
mod tests {
use core::f64::consts::TAU;

use super::*;
use crate::random::XorShiftRng;

const ENTRY_TOLERANCE: f64 = 1e-6;
const READ_TOLERANCE: f64 = 2e-6;

fn true_sine(cycles: f64) -> f64 {
libm::sin(TAU * cycles)
}

fn check_entries<const LENGTH: usize>() {
let table = SineTable::<LENGTH>::new();
let cells = LENGTH - 1;
for (index, entry) in table.entries().iter().enumerate() {
let expected = true_sine(index as f64 / cells as f64);
let error = (*entry as f64 - expected).abs();
assert!(
error < ENTRY_TOLERANCE,
"{LENGTH}: entry {index} = {entry}, expected {expected}, error {error}"
);
}
assert_eq!(table.entries()[cells], table.entries()[0]);
}

#[test]
fn test_entries_match_libm_sine() {
check_entries::<1025>();
check_entries::<2049>();
check_entries::<4097>();
}

#[test]
fn test_builds_in_const_context() {
const TABLE: SineTable<2049> = SineTable::new();
assert_eq!(TABLE.entries().len(), 2049);
assert_eq!(SineTable::<2049>::CELLS, 2048);
assert!((SINE_2048.read(0.25) - 1.0).abs() < 1e-6);
}

#[test]
fn test_read_at_random_phases() {
let mut rng = XorShiftRng::new(0x5EED);
for _ in 0..10_000 {
let phase = rng.next_noise_sample() * 4.0;
let actual = SINE_2048.read(phase as f32) as f64;
let expected = true_sine(phase);
let error = (actual - expected).abs();
assert!(
error < READ_TOLERANCE,
"phase {phase}: {actual} vs {expected}, error {error}"
);
}
}

#[test]
fn test_read_is_periodic_exactly_on_dyadic_phases() {
// `k / 1024 ± 1.0` and the floor subtraction are exact in f32, so the wrapped phase is
// bit-identical; for arbitrary phases `(p + 1.0) - 1.0 != p` and only a tolerance holds.
for k in 0..1024 {
let phase = k as f32 / 1024.0;
let base = SINE_2048.read(phase);
assert_eq!(SINE_2048.read(phase + 1.0), base, "phase {phase} + 1");
assert_eq!(SINE_2048.read(phase - 1.0), base, "phase {phase} - 1");
}
}

#[test]
fn test_landmark_phases() {
assert_eq!(SINE_2048.read(0.0), 0.0);
assert!((SINE_2048.read(0.25) - 1.0).abs() < 1e-6);
assert!(SINE_2048.read(0.5).abs() < 1e-6);
assert!((SINE_2048.read(0.75) + 1.0).abs() < 1e-6);
assert!((SINE_2048.read_cosine(0.0) - 1.0).abs() < 1e-6);
assert!(SINE_2048.read_cosine(0.25).abs() < 1e-6);
}

#[test]
fn test_last_cell_before_wrap() {
let value = SINE_2048.read(1.0 - f32::EPSILON);
assert!(value.is_finite());
assert!(value.abs() < 1e-5, "got {value}");
}

#[test]
fn test_read_stays_in_unit_range() {
for k in 0..100_000 {
let value = SINE_2048.read(k as f32 * 1e-5);
assert!((-1.0..=1.0).contains(&value), "phase {k}e-5 read {value}");
}
}

#[test]
#[should_panic(expected = "power of two")]
fn test_rejects_non_power_of_two_cell_count() {
let _ = SineTable::<1000>::new();
}
}
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
- [SPSC Ring Buffer](crates/core/spsc.md)
- [Stack Vector](crates/core/stack-vec.md)
- [Random Number Generation](crates/core/random.md)
- [Sine Table](crates/core/sine-table.md)
- [Error Types](crates/core/error.md)
- [bbx_daisy](crates/bbx-daisy.md)
- [bbx_draw](crates/bbx-draw.md)
Expand Down
2 changes: 2 additions & 0 deletions docs/src/crates/bbx-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ bbx_core provides low-level utilities designed for real-time audio applications:
- Lock-free data structures for inter-thread communication
- Stack-allocated containers to avoid heap allocations
- Fast random number generation
- Compile-time lookup tables read by phase in cycles

## Installation

Expand All @@ -28,6 +29,7 @@ bbx_core = "0.1"
| [SPSC Ring Buffer](core/spsc.md) | Lock-free producer-consumer queue |
| [Stack Vector](core/stack-vec.md) | Fixed-capacity heap-free vector |
| [Random](core/random.md) | Fast XorShift RNG |
| [Sine Table](core/sine-table.md) | Compile-time sine table and interpolating table read |
| [Error Types](core/error.md) | Unified error handling |

## Quick Example
Expand Down
Loading
Loading