tur/params

stdlib/params.tur
defn

warp-value

(warp-value [spec value])

Map a user-space value to OSC range using the spec's warp type.

specA ParamSpec map containing at least :warp.
valueInput value in user space.
Warped value suitable for sending to scsynth.

  (warp-value PARAM_FREQ 440.0)  ; => 440.0 (exp warp applies octave formula)
defn

unwarp-value

(unwarp-value [spec value])

Map an OSC value back to user space using the spec's warp type.

specA ParamSpec map containing at least :warp.
valueValue received from scsynth in OSC space.
Value in user space (inverse warp of warp-value).

  (unwarp-value PARAM_FREQ (warp-value PARAM_FREQ 440.0))  ; => 440.0
defn

clamp-value

(clamp-value [spec value])

Clamp a value to the min/max range specified in a ParamSpec.

specA ParamSpec map with :min and :max fields.
valueValue to clamp.
The value clamped to [spec.min, spec.max].

  (clamp-value PARAM_AMP 1.5)  ; => 1.0
defn

param->osc

(param->osc [spec value])

Clamp and warp a user-space value ready for scsynth.

specA ParamSpec map.
valueUser-space value.
OSC-ready float (clamped first, then warped).

  (param->osc PARAM_AMP 0.8)  ; => 0.8
defn

osc->param

(osc->param [spec value])

Unwarp and clamp an OSC value back to user space.

specA ParamSpec map.
valueOSC value from scsynth.
User-space float clamped to the spec's original range.

  (osc->param PARAM_AMP 0.8)  ; => 0.8
defn

param-spec

(param-spec [name default & [min 0.0 max 1.0 unit "" warp :linear])

Create a parameter specification with common defaults.

nameParameter name string (must match the SynthDef control name).
defaultDefault value.
minMinimum value (default: 0.0).
maxMaximum value (default: 1.0).
unitUnit label string, e.g. "Hz", "dB", "sec" (default: "").
warpWarp type keyword: :linear, :exp, :log, :db, :db2 (default: :linear).
A ParamSpec map.

  (param-spec "freq" 440.0 20.0 20000.0 "Hz" :exp)
defn

synthdef-spec

(synthdef-spec [synthdef_name param-specs])

Assemble a SynthDef specification from a list of ParamSpecs.

synthdef_nameSynthDef name (currently unused; reserved for future metadata).
param-specsVector of ParamSpec maps describing each control.
SynthDefSpec (currently just the param-specs vector).

  (synthdef-spec "mySynth" [PARAM_FREQ PARAM_AMP])
defn

find-param

(find-param [spec param_name])

Find a ParamSpec by name within a SynthDefSpec.

specSynthDefSpec (vector of ParamSpec maps).
param_nameParameter name to search for.
The matching ParamSpec map, or nil if not found.

  (find-param SIN_SYNTH_SPEC "freq")
defn

find-param-index

(find-param-index [spec param_name])

Get the zero-based index of a parameter by name.

specSynthDefSpec (vector of ParamSpec maps).
param_nameParameter name to search for.
Integer index of the parameter, or nil if not found.

  (find-param-index SIN_SYNTH_SPEC "amp")  ; => 1
defstruct

SynthController

(defstruct SynthController [])
defn

synth-controller-new

(synth-controller-new [session synthdef-name synthdef-spec & [params {}])

Create a synth and wrap it in a typed controller.

sessionParent ScscmSession.
synthdef-nameName of the SynthDef to instantiate.
synthdef-specSynthDefSpec listing this SynthDef's parameters.
paramsOptional initial values as a name -> value map.
A SynthController map. Panics if the synth cannot be created.

  (synth-controller-new sess "mySynth" SIN_SYNTH_SPEC {"freq" 880.0})
defn

synth-controller-free

(synth-controller-free [ctrl])

Free the synth node owned by a controller.

ctrlA SynthController map.
defn

ctrl-set

(ctrl-set [ctrl param_name value])

Set a named parameter on a synth controller (with warping).

ctrlA SynthController map.
param_nameName of the parameter to set.
valueUser-space value (will be clamped and warped before sending).
true if the parameter was found and set, false otherwise.

  (ctrl-set ctrl "freq" 880.0)
defn

ctrl-set-multi

(ctrl-set-multi [ctrl param-map])

Set multiple named parameters on a controller.

ctrlA SynthController map.
param-mapMap of parameter name -> user-space value.
Number of parameters successfully set.

  (ctrl-set-multi ctrl {"freq" 880.0 "amp" 0.6})  ; => 2
defn

ctrl-get

(ctrl-get [ctrl param_name])

Get the current value for a named parameter (returns spec default).

ctrlA SynthController map.
param_nameParameter name to look up.
Default value from the ParamSpec, or nil if the parameter is not found.

  (ctrl-get ctrl "freq")  ; => 440.0 (spec default)
defn

midi->hz

(midi->hz [note])

Convert a MIDI note number to frequency in Hz.

noteMIDI note number (69 = A4 = 440 Hz).
Frequency in Hz as a float.

  (midi->hz 69)  ; => 440.0
defn

vel->amp

(vel->amp [vel])

Convert a MIDI velocity to linear amplitude.

velMIDI velocity (0-127).
Amplitude in [0.0, 1.0].

  (vel->amp 64)  ; => ~0.504
defn

db->amp

(db->amp [db])

Convert a decibel level to linear amplitude.

dbLevel in decibels.
Linear amplitude as a float.

  (db->amp -6.0)  ; => ~0.501
defn

amp->db

(amp->db [amp])

Convert linear amplitude to decibels.

ampLinear amplitude (0.0 or below returns -90.0).
Level in decibels.

  (amp->db 0.5)  ; => ~-6.02