Files
Lyrebird/Sources/LyrebirdUI/DbSliderConversion.swift
T
2026-06-01 18:27:40 -07:00

47 lines
1.9 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// DbSliderConversion.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
/// Shared dB ↔ linear-amplitude helpers for amplitude sliders.
///
/// Both `LyrebirdEventListView` and `LyrebirdProcessView` use a `-90 … +12` dB slider that
/// writes a linear amplitude back to the underlying engine value. The conversion logic is
/// identical between the two views; this enum centralises it so changes only need one edit.
internal enum DbSliderConversion {
/// Slider floor — at this value the source is treated as silenced.
static let minDb: Double = -90.0
/// Default slider ceiling — `dbToAmp(12) ≈ 3.98×`. Per-process views may
/// override via ``LyrebirdProcess/maxUIAmpDb`` when more headroom is needed.
static let maxDb: Double = 12.0
/// Convert the slider's dB position to the linear amplitude written to the engine value.
/// Floors at `minDb` (silence).
static func ampFromDb(_ db: Double) -> Double {
if db <= minDb { return 0.0 }
return dbToAmp(db)
}
/// Inverse — used to seed the slider position from a linear amplitude. Treats
/// `amp == 0` (or anything below `minDb`'s amplitude) as the slider floor so the
/// knob does not jump to `-inf`. Caller passes the active slider ceiling so
/// per-process maxima above `+12` dB are respected.
static func dbFromAmp(_ amp: Double, maxDb: Double = DbSliderConversion.maxDb) -> Double {
guard amp > 0 else { return minDb }
let db: Double = ampToDb(amp)
return max(minDb, min(maxDb, db))
}
/// Render the current dB value. Shows `-inf` at the slider floor so users see
/// immediately that the source is silenced rather than `−90.0 dB`.
static func dbDisplay(_ db: Double) -> String {
if db <= minDb { return "-inf dB" }
return String(format: "%+.1f dB", db)
}
}