Build with this node

Start with a prompt in Studio, or copy it into your own coding agent.

Ask Studio

@maruaudio/dsp/mixing/velocity holds the velocity laws sample players share, extracted from voices/sampler-zone with the arithmetic unchanged.

FunctionLawPrecision
velocitySquaredGain(v)v²f32
velocityResponseGain(v, floor, curve)floor + (1 − floor) × curve(v), curve VELOCITY_CURVE_SQUARED (v²) or VELOCITY_CURVE_LINEAR (v)f32
velocityToByte(v)floor(clamp(v × 127, 0, 127) + 0.5), the MIDI byte a layer is selected withinteger
velocityCrossfadeWeight(byte, low, high, fadeIn, fadeOut)1 without a fade; inside a fade span sin(w·π/2) of the linear positionf64

The crossfade spans are measured inward from the layer's own edges: fadeIn steps above low and fadeOut steps below high, with the linear position (steps + 0.5) / span. A layer never contributes outside the range it declares.

Parameters

There are no setters or state; every value is a function argument. floor is the gain at velocity 0: 0 follows the curve fully and 1 holds every strike at unity.

Lifecycle

Pure functions, evaluated at note-on and folded into the voice's constant gain. Nothing allocates or does I/O.

When to use it

Mapping note velocity to loudness in a sample player, choosing the velocity layer for a strike, and crossfading two adjacent layers at constant power across their seam.

Limits

  • The response shapes loudness only; layer selection is velocityToByte's.
  • velocityResponseGain(v, 0, VELOCITY_CURVE_SQUARED) equals velocitySquaredGain(v) to the bit.
  • The crossfade weight stays a double so the caller rounds once after folding it into its gain.
  • velocitySquaredGain and velocityResponseGain do not clamp v.

Algorithm properties and proofs

Lean 4 checks specific properties of this algorithm’s mathematical model. Each model result applies within its stated domain and assumptions.

3 Lean 4 results: 3 proofs in exact arithmetic.

Proved in exact arithmetic.The squared velocity curve stays between zero and one for a normalized strike.Exact rational normalized velocity in [0, 1]
Limits and assumptions
  • Hand-written source-reviewed model, not formal refinement of TypeScript or compiled code. Named functions and whole-file source digests are checked; finite canonical JavaScript cases are compared with executable Lean definitions. Exact rational arithmetic only: IEEE rounding, overflow, NaN, infinities and signed-zero behavior are not proved. Sampler mappings cover the named shared stage only. No optimization is enabled or claimed faster.
Theorem
MaruLean.LeafContracts.velocity_square_bounded
Recorded axioms
propext, Classical.choice, Quot.sound
Manifest reviewed
Proved in exact arithmetic.Both linear and squared velocity responses stay between the configured floor and unity.Exact rational velocity and floor in [0, 1]; linear or squared curve
Limits and assumptions
  • Hand-written source-reviewed model, not formal refinement of TypeScript or compiled code. Named functions and whole-file source digests are checked; finite canonical JavaScript cases are compared with executable Lean definitions. Exact rational arithmetic only: IEEE rounding, overflow, NaN, infinities and signed-zero behavior are not proved. Sampler mappings cover the named shared stage only. No optimization is enabled or claimed faster.
Theorem
MaruLean.LeafContracts.velocity_response_bounded
Recorded axioms
propext, Classical.choice, Quot.sound
Manifest reviewed
Proved in exact arithmetic.A velocity-response floor of one gives unity gain for either curve.Exact rational velocity; floor = 1; linear or squared curve
Limits and assumptions
  • Hand-written source-reviewed model, not formal refinement of TypeScript or compiled code. Named functions and whole-file source digests are checked; finite canonical JavaScript cases are compared with executable Lean definitions. Exact rational arithmetic only: IEEE rounding, overflow, NaN, infinities and signed-zero behavior are not proved. Sampler mappings cover the named shared stage only. No optimization is enabled or claimed faster.
Theorem
MaruLean.LeafContracts.velocity_response_neutral
Recorded axioms
propext, Classical.choice, Quot.sound
Manifest reviewed
Explore proofs alongside performance

Dependencies

None: this node imports no other node.

Dependents

Used by