LevelNormalizer

class LevelNormalizer(floor: Float = DEFAULT_FLOOR, attack: Duration = 400.milliseconds, release: Duration = 20.seconds)(source)

Turns AudioLevel.peak into a 0..1 value for visuals that looks the same on every device.

Dividing by full scale leaves a meter nearly flat, because ordinary speech peaks far below it. Dividing by a fixed reference measured on one phone pins the meter at 1.0 on another whose input runs hotter (input gain varies with the platform, the audio source or mode, and whether gain control is on; the iOS record path often runs hotter than Android's). So the reference adapts: it rises within attack when the microphone turns out louder than assumed, and falls back over release so a pause does not make the next quiet word read as a shout.

The reference never drops below floor. Without that floor this would be an automatic gain that turns room noise into full-scale readings.

Not thread-safe; use one instance per session.

Constructors

Link copied to clipboard
constructor(floor: Float = DEFAULT_FLOOR, attack: Duration = 400.milliseconds, release: Duration = 20.seconds)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun normalize(level: AudioLevel, elapsed: Duration): Float
Link copied to clipboard
fun reset()

Forget what the previous session learned.