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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ Nothing has been released yet. The first release, 0.1.0, will cover what is on `
- Expanded windows for Distortion, Tape Saturation and Soft Clipper (the static transfer on a 48 dB square with the
gain or clipping it applies lit against unity, two cycles of a full-scale sine through the device, and every control
in one card) and Bit Crusher (one crushed cycle across the display under its held rate and number of levels).
- Expanded windows for Utility (its controls on three cards, the meters down the face) and Stereo Widener (the stereo
field over frequency above a correlation lane that plots the input and output correlation of the last six seconds).
The engine measures a built-in device's stereo correlation while its window is open (`se_builtin_correlation`).
- An expanded window for Tuner: the card's tuner at twice its size across the display, with its input meter.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ Limiter adds a short-term loudness lane against a target), Transient Shaper, who
gain it applied, and Multiband Compressor, whose window draws each band's level, gain and thresholds on a frequency
axis with draggable crossovers, and Delay, Echo and Reverb, whose window draws the card's echo train, tail picture or repeat
EQ at full size with every control in a row below, Distortion, Tape Saturation and Soft Clipper, whose window draws
the transfer curve beside a sine through the device, and Bit Crusher, whose window draws one crushed cycle; the other devices open their card in a window for now.
the transfer curve beside a sine through the device, Bit Crusher, whose window draws one crushed cycle, Utility, and Stereo Widener, whose window draws the stereo
field over a live correlation lane, and Tuner; the other devices open their card in a window for now.
[docs/devices.md](docs/devices.md) explains how they work and how to add one.

| Kind | Devices |
Expand Down
8 changes: 6 additions & 2 deletions crates/dstudio-app/src/analyzer_views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,12 @@ impl Controller {
}

fn tuner_canvas(&self, d: &Device, def: &'static DeviceDef, w_pt: f32, accent: Color) -> PanelCanvas {
let z = Z;
let (w, h) = (w_pt, TUNER_BOTTOM - FIELD_Y); // unscaled; scaled on output
self.tuner_canvas_at(d, def, w_pt, TUNER_BOTTOM - FIELD_Y, Z, accent)
}

/// The tuner picture `w` x `h` units drawn at `z` px per unit (the card's is Z; the
/// expanded window draws it larger).
pub(crate) fn tuner_canvas_at(&self, d: &Device, def: &'static DeviceDef, w: f32, h: f32, z: f32, accent: Color) -> PanelCanvas {
let empty = TunerView::default();
let tv = self.devices.panel.analyzers.tuners.get(&d.id).unwrap_or(&empty);
let tuning = def.param("tuning").map_or(0, |p| self.panel_value(d, def, p).round() as usize).min(TUNING_STRINGS.len() - 1);
Expand Down
231 changes: 225 additions & 6 deletions crates/dstudio-app/src/device_window.rs

Large diffs are not rendered by default.

224 changes: 224 additions & 0 deletions crates/dstudio-app/src/device_window_widener.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
//! Stereo Widener's expanded window display: the stereo field (effective width against
//! frequency, 20 Hz..2 kHz on a 0..400 % ladder, lit between the curve and 100 %), and
//! under it a correlation lane with the input and output correlation over the last
//! seconds (+1 in phase, 0 uncorrelated, −1 cancels in mono). A vertical drag on the
//! field sets Width.

use crate::PanelCanvas;
use crate::controller::Controller;
use crate::device_canvas::{fill, path, rgba, stroke, text, white};
use crate::device_panel::Z;
use crate::device_window::Kit;
use crate::{PanelPath, PanelText, PanelZone};
use dstudio_core::builtin::{self, DeviceDef};
use dstudio_core::{Device, DeviceId};
use slint::{Color, ModelRc, VecModel};
use std::collections::VecDeque;
use std::rc::Rc;

fn model<T: Clone + 'static>(v: Vec<T>) -> ModelRc<T> {
ModelRc::from(Rc::new(VecModel::from(v)))
}

/// The field's frequency axis (Hz) and width ladder (%).
const F_LO: f32 = 20.0;
const F_HI: f32 = 2000.0;
const PCT_TOP: f32 = 400.0;
const HZ_TICKS: [(f32, &str); 5] = [(50.0, "50"), (100.0, "100"), (200.0, "200"), (500.0, "500"), (1000.0, "1k")];
/// Field height and the lane under it (pt, from the plot top).
const FIELD_H: f32 = 300.0;
const LANE_Y: f32 = 359.0;
const LANE_H: f32 = 120.0;
/// Seconds the correlation lane shows, one grid division each.
pub(crate) const CORR_SPAN: f64 = 6.0;
const FILL: f32 = 0.22;
/// Readings further apart than this (s) are not joined.
const GAP_S: f64 = 0.25;
/// A correlation reading of a silent side.
const SILENT: f32 = 1.5;

/// One correlation reading: (time s, input, output); above 1 where that side was silent.
pub(crate) type CorrSample = (f64, f32, f32);

/// Appends a reading and drops those older than the lane shows.
pub(crate) fn push_corr(h: &mut VecDeque<CorrSample>, s: CorrSample) {
h.push_back(s);
while h.front().is_some_and(|f| f.0 < s.0 - CORR_SPAN - 0.5) {
h.pop_front();
}
}

/// Polyline commands of one series, broken where it was silent or readings stopped.
fn series(
h: &VecDeque<CorrSample>,
now: f64,
pick: fn(&CorrSample) -> f32,
x_of: impl Fn(f64) -> f32,
y_of: impl Fn(f32) -> f32,
) -> String {
let mut out = String::new();
let mut open = false;
let mut last = f64::NEG_INFINITY;
for s in h.iter().filter(|s| s.0 >= now - CORR_SPAN) {
let c = pick(s);
if c > SILENT {
open = false;
continue;
}
if s.0 - last > GAP_S {
open = false;
}
last = s.0;
let (x, y) = (x_of(s.0), y_of(c));
out.push_str(&format!("{}{x:.1} {y:.1} ", if open { "L" } else { "M" }));
open = true;
}
out
}

impl Controller {
/// The correlation history the Widener window draws.
pub(crate) fn win_corr(&self, id: DeviceId) -> Option<&VecDeque<CorrSample>> {
self.devices.wins.corr.get(&id)
}

pub(crate) fn widener_window_canvas(&self, d: &Device, def: &'static DeviceDef, k: &Kit, accent: Color) -> PanelCanvas {
let v = |id: &str| def.param(id).map_or(0.0, |p| self.panel_value(d, def, p));
let (_, _, dw, dh) = k.display;
let (pl, pt, pr, pb) = k.plot;
let (pw, ph) = ((dw - pl - pr) * Z, (dh - pt - pb) * Z);
let (width, mid_db, side_db, low, mix) = (v("width2"), v("midgain"), v("sidegain"), v("lowcut"), v("mix"));
let resp = |f: f32| builtin::widener_response(f, width, mid_db, side_db, low, mix, 48_000.0);
let fh = FIELD_H * Z;
let x_of = |f: f32| (f.clamp(F_LO, F_HI) / F_LO).ln() / (F_HI / F_LO).ln() * pw;
let y_of = |pct: f32| fh * (1.0 - (pct / PCT_TOP).clamp(0.0, 1.0));
let dim = white(0.32);
let mut paths: Vec<PanelPath> = Vec::new();
let mut texts: Vec<PanelText> = Vec::new();

// The field: grid, ladder, axis.
let mut grid = String::new();
for (f, label) in HZ_TICKS {
let x = x_of(f).round() + 0.5;
grid.push_str(&format!("M{x:.1} 0 V{fh:.1} "));
texts.push(text(x, fh + 10.0 * Z, 40.0 * Z, label, dim, 10.0 * Z, 1));
}
for pct in [100.0, 200.0, 300.0] {
let y = y_of(pct).round() + 0.5;
grid.push_str(&format!("M0 {y:.1} H{pw:.1} "));
texts.push(text(-8.0 * Z, y_of(pct), 36.0 * Z, format!("{pct:.0}"), dim, 10.0 * Z, 2));
}
grid.push_str(&format!("M0 {fh:.1} H{pw:.1} M{pw:.1} 0 V{fh:.1}"));
paths.push(stroke(grid, white(0.05), 1.0));
texts.push(text(-8.0 * Z, fh + 10.0 * Z, 36.0 * Z, "%", dim, 10.0 * Z, 2));
texts.push(text(pw, fh + 10.0 * Z, 40.0 * Z, "Hz", dim, 10.0 * Z, 2));

// The width curve, lit against 100 %.
let n = ((pw / Z) / 4.0).round().max(48.0) as usize;
let curve: Vec<(f32, f32)> = (0..=n)
.map(|i| {
let f = F_LO * (F_HI / F_LO).powf(i as f32 / n as f32);
(x_of(f), y_of(resp(f)))
})
.collect();
let line = path(&curve);
let unity = y_of(100.0);
paths.push(fill(format!("{line} L{pw:.1} {unity:.1} L0 {unity:.1} Z"), rgba(accent, FILL)));
paths.push(stroke(line, accent, 1.25 * Z));

// Caption above the field.
let cy = -(pt - 12.0) * Z;
let cx = -(pl - 14.0) * Z;
texts.push(text(cx, cy, 120.0 * Z, "Stereo Field", white(0.55), 12.0 * Z, 0));
let at = cx + (crate::text_measure::value_width("Stereo Field") + 14.0) * Z;
texts.push(text(at, cy, 120.0 * Z, format!("{:.0}%", resp(F_HI)), white(0.92), 13.0 * Z, 0));

// The correlation lane: caption and legend, grid, the two series.
let (ly, lh) = (LANE_Y * Z, LANE_H * Z);
let ky = ly - 13.0 * Z;
texts.push(text(cx, ky, 120.0 * Z, "Correlation", white(0.55), 12.0 * Z, 0));
let mut x = cx + (crate::text_measure::value_width("Correlation") + 14.0) * Z;
let glyph = 14.0 * Z;
for (label, ink) in [("In", white(0.5)), ("Out", accent)] {
paths.push(stroke(format!("M{x:.1} {ky:.1} h{glyph:.1}"), ink, 1.25 * Z));
texts.push(text(x + glyph + 6.0 * Z, ky, 60.0 * Z, label, white(0.7), 10.0 * Z, 0));
x += glyph + 6.0 * Z + (crate::text_measure::value_width(label) + 16.0) * Z;
}
x += 4.0 * Z;
for label in ["+1 in phase", "0 uncorrelated", "\u{2212}1 cancels in mono"] {
texts.push(text(x, ky, 160.0 * Z, label, white(0.3), 10.0 * Z, 0));
x += (crate::text_measure::value_width(label) + 14.0) * Z;
}
let mut lane = String::new();
for i in 0..=4 {
let y = (ly + lh * i as f32 / 4.0).round() + 0.5;
lane.push_str(&format!("M0 {y:.1} H{pw:.1} "));
}
for i in 1..6 {
let x = (pw * i as f32 / 6.0).round() + 0.5;
lane.push_str(&format!("M{x:.1} {ly:.1} V{:.1} ", ly + lh));
}
paths.push(stroke(lane, white(0.05), 1.0));
let mid = (ly + lh / 2.0).round() + 0.5;
paths.push(stroke(format!("M0 {mid:.1} H{pw:.1}"), white(0.12), 1.0));
if let Some(h) = self.win_corr(d.id) {
let now = h.back().map_or(0.0, |s| s.0);
let tx = |t: f64| pw - ((now - t) / CORR_SPAN) as f32 * pw;
let cy = |c: f32| ly + lh * (1.0 - c.clamp(-1.0, 1.0)) / 2.0;
let input = series(h, now, |s| s.1, tx, cy);
let output = series(h, now, |s| s.2, tx, cy);
if !input.is_empty() {
paths.push(stroke(input, white(0.5), 1.25 * Z));
}
if !output.is_empty() {
paths.push(stroke(output, accent, 1.5 * Z));
}
}

// A vertical drag on the field sets Width.
let zones = vec![PanelZone {
x: 0.0,
y: 0.0,
w: pw,
h: fh,
param_x: -1,
param_y: def.index_of("width2").map_or(-1, |i| i as i32),
rate_x: 0.0,
rate_y: 1.0 / fh,
}];
PanelCanvas {
visible: true,
w: pw,
h: ph,
paths: model(paths),
texts: model(texts),
zones: model(zones),
label: "Stereo Field".into(),
..Default::default()
}
}
}

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

#[test]
fn correlation_history_keeps_the_lane_span_and_breaks_at_silence() {
let mut h = VecDeque::new();
for i in 0..800 {
let t = i as f64 / 60.0;
push_corr(&mut h, (t, if (300..360).contains(&i) { 2.0 } else { 0.5 }, 1.0));
}
assert!(h.front().unwrap().0 >= h.back().unwrap().0 - CORR_SPAN - 0.5);
let now = h.back().unwrap().0;
let s = series(&h, now, |s| s.1, |t| t as f32, |c| c);
assert_eq!(s.matches('M').count(), 1, "no silence in view: one run");
let mut h2 = VecDeque::new();
for i in 0..300 {
push_corr(&mut h2, (i as f64 / 60.0, if (100..160).contains(&i) { 2.0 } else { 0.0 }, 0.0));
}
let s2 = series(&h2, h2.back().unwrap().0, |s| s.1, |t| t as f32, |c| c);
assert_eq!(s2.matches('M').count(), 2, "silence splits the run");
}
}
1 change: 1 addition & 0 deletions crates/dstudio-app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ mod device_window_fields;
mod device_window_multiband;
mod device_window_shaper;
mod device_window_wave;
mod device_window_widener;
mod devices;
mod devminimap;
mod dialogs;
Expand Down
1 change: 1 addition & 0 deletions crates/dstudio-engine/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,7 @@ unsafe extern "C" {
pub fn se_builtin_readouts(engine: *mut se_engine, device_id: u64, out: *mut f32, capacity: usize, out_count: *mut usize) -> i32;
pub fn se_builtin_set_metering(engine: *mut se_engine, device_id: u64, on: u8) -> i32;
pub fn se_builtin_levels(engine: *mut se_engine, device_id: u64, out: *mut f32) -> i32;
pub fn se_builtin_correlation(engine: *mut se_engine, device_id: u64, out: *mut f32) -> i32;
pub fn se_builtin_set_spectrum(engine: *mut se_engine, device_id: u64, mode: u8) -> i32;
pub fn se_builtin_spectrum(
engine: *mut se_engine,
Expand Down
6 changes: 6 additions & 0 deletions crates/dstudio-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,12 @@ impl Engine {
check(unsafe { ffi::se_builtin_levels(self.raw, device_id, out.as_mut_ptr()) })
}

/// Stereo correlation of a live built-in device's input and output since the previous
/// call (-1..1; 2 where that side was silent or metering is off).
pub fn builtin_correlation(&self, device_id: u64, out: &mut [f32; 2]) -> Result<(), EngineError> {
check(unsafe { ffi::se_builtin_correlation(self.raw, device_id, out.as_mut_ptr()) })
}

/// Number of bins in an analyser frame ([`Engine::builtin_spectrum`]).
pub const SPECTRUM_BINS: usize = 128;

Expand Down
9 changes: 7 additions & 2 deletions docs/devices.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ flowchart LR
Flanger, Phaser, Ring Mod and Character Mod; and the dynamics family:
Compressor, Limiter, Gate, Glue Compressor, Character Compressor and Mastering
Limiter; Transient Shaper; Multiband Compressor; Delay, Echo and Reverb; Distortion, Tape Saturation,
Soft Clipper and Bit Crusher), otherwise the card itself in a window. A
Soft Clipper and Bit Crusher; Utility, Stereo Widener and Tuner), otherwise the card itself in a window. A
window is a face 960 pt wide (Ring Mod 860): a top bar (power, presets, the device's header pills), a
large display (the card's canvas drawn at window size with its axis and
legend), In / Out meters, and rows of controls laid out with the same
Expand Down Expand Up @@ -123,7 +123,12 @@ flowchart LR
(src/device_window_wave.rs) draw the static transfer from the core's
`distortion_transfer`, `tape_transfer` and `clipper_transfer` as the sine
peak out against the sine peak in on a 48 dB square, and the Bit Crusher
draws `crush_picture` across its display. The
draws `crush_picture` across its display. Utility's window has no display,
only three cards. The Stereo Widener's correlation lane reads
`se_builtin_correlation` (`Engine::builtin_correlation`): while metering is
on the audio thread sums L·R, L² and R² of the input and the output of each
block, and each poll takes them as a correlation per side (2 where a side
was silent). The
meters read `se_builtin_set_metering` / `se_builtin_levels`
(`Engine::builtin_levels(id, &mut [f32; 4])`): while a window is open the
audio thread keeps the input and output peak of each block, and the app
Expand Down
8 changes: 8 additions & 0 deletions engine/include/dstudio_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,14 @@ int32_t se_builtin_set_metering(se_engine* engine, uint64_t device_id, uint8_t o
* -> SE_ERR_INVALID_ARG. */
int32_t se_builtin_levels(se_engine* engine, uint64_t device_id, float* out);

/* Stereo correlation of a live built-in device's input (out[0]) and output
* (out[1]) since the previous call: -1 cancels in mono, 0 uncorrelated, +1 in
* phase; 2 where that side was silent or metering is off. Reading resets it.
* Measured while metering is on (se_builtin_set_metering).
* MAIN THREAD. Unknown or non-built-in device -> SE_ERR_NOT_FOUND; out NULL
* -> SE_ERR_INVALID_ARG. */
int32_t se_builtin_correlation(se_engine* engine, uint64_t device_id, float* out);

/* The analyser of a built-in device's expanded window: mode 0 off, 1 the
* device's input (Pre), 2 its output (Post). While it is on the audio thread
* measures a 128-bin log-spaced level spectrum (20 Hz .. 20 kHz, dBFS, floor
Expand Down
9 changes: 9 additions & 0 deletions engine/src/c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,15 @@ int32_t se_builtin_levels(se_engine* engine, uint64_t device_id, float* out) {
});
}

int32_t se_builtin_correlation(se_engine* engine, uint64_t device_id, float* out) {
SE_REQUIRE_ENGINE(engine);
if (out == nullptr) return fail(SE_ERR_INVALID_ARG, "out is NULL");
return guarded([&]() -> int32_t {
const int32_t rc = engine->impl->builtin_correlation(device_id, out);
return rc == SE_OK ? rc : fail(rc, "no live built-in device with this id");
});
}

int32_t se_builtin_set_spectrum(se_engine* engine, uint64_t device_id, uint8_t mode) {
SE_REQUIRE_ENGINE(engine);
if (mode > 2) return fail(SE_ERR_INVALID_ARG, "mode must be 0 (off), 1 (pre) or 2 (post)");
Expand Down
22 changes: 22 additions & 0 deletions engine/src/devices/builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,26 @@ void accumulate_peak(std::atomic<float>& slot, const float* x, uint32_t frames)
while (p > cur && !slot.compare_exchange_weak(cur, p, std::memory_order_relaxed)) {
}
}
// A block's (sum LR, sum LL, sum RR) added to three running sums (RT; the main thread
// resets them to 0). Mono input (r == nullptr) counts as fully correlated.
void accumulate_correlation(std::atomic<float>* slot, const float* l, const float* r, uint32_t frames) noexcept {
if (l == nullptr) return;
const float* rr = r != nullptr ? r : l;
float s[3] = {0.0f, 0.0f, 0.0f};
for (uint32_t i = 0; i < frames; ++i) {
s[0] += l[i] * rr[i];
s[1] += l[i] * l[i];
s[2] += rr[i] * rr[i];
}
for (int k = 0; k < 3; ++k) {
if (!std::isfinite(s[k])) return;
}
for (int k = 0; k < 3; ++k) {
float cur = slot[k].load(std::memory_order_relaxed);
while (!slot[k].compare_exchange_weak(cur, cur + s[k], std::memory_order_relaxed)) {
}
}
}
} // namespace

void BuiltinInstance::process(float* l, float* r, uint32_t frames, const MidiEvent* events, uint32_t event_count,
Expand All @@ -459,6 +479,7 @@ void BuiltinInstance::process(float* l, float* r, uint32_t frames, const MidiEve
if (metering) {
accumulate_peak(peaks_[0], l, frames);
accumulate_peak(peaks_[1], r, frames);
accumulate_correlation(&corr_[0], l, r, frames);
}
const int spec_mode = spectrum_mode_.load(std::memory_order_relaxed);
WindowSpectrum* const spec = spec_mode != 0 ? spectrum_rt_.load(std::memory_order_acquire) : nullptr;
Expand Down Expand Up @@ -556,6 +577,7 @@ void BuiltinInstance::process(float* l, float* r, uint32_t frames, const MidiEve
if (metering) {
accumulate_peak(peaks_[2], l, frames);
accumulate_peak(peaks_[3], r, frames);
accumulate_correlation(&corr_[3], l, r, frames);
}
if (spec != nullptr && spec_mode == 2) spec->push(l, r, frames);
sc_l_ = sc_r_ = nullptr;
Expand Down
Loading
Loading