Arcade.Synth
Types
Every Float field holds a value a Swift Float can hold. glide_floor is Swift's max(glide, 0.0001) and life its decay * 6, both computed once.
The numbers a synth is built from, as written (not yet rounded to Float). rate is samples per second; max_voices the cap past which add drops the oldest voices; seed the noise generator's start. The rumble bed is low-passed noise: its loudness follows set_rumble by rumble_follow a sample, the noise is low-passed by lowpass_follow, and the product is scaled by rumble_gain. The drum bed is one noise hit of drum_amp and drum_decay every drum_step seconds. min_glide floors every glide time, attack_time is every voice's linear attack, square_level the square wave's height, and default_glide the glide time of the short voice forms.
The Float constants of the synth, rounded to Float once. drum_amp and drum_decay stay as written: voice rounds them, as it rounds every argument.
Functions
fn upto(n : Int, f : Int -> SynthVoice) : List(SynthVoice) needs () do upto_from(0, n, f, []) end
pfn upto_from(i : Int, n : Int, f : Int -> SynthVoice, acc : List(SynthVoice)) : List(SynthVoice) needs () do if i < n do upto_from(i + 1, n, f, Cons(f(i), acc)) else List.reverse(acc) end end
fn times(c : SynthConsts, i : Int, x : Float) : Float needs () do f32(c, int_to_float(i) *. f32(c, x)) end
fn arpeggio(c : SynthConsts, notes : List(Float), step : Float, amp : Float, decay : Float, w : SynthWave) : List(SynthVoice) needs () do arpeggio_from(c, notes, 0, step, amp, decay, w, []) end
fn max_voices() : Int needs () do let t = tuning() t.max_voices end
fn sinf(c : SynthConsts, x : Float) : Float needs () do Arcade.Float32.sinf(c.round, x) end
fn consts() : SynthConsts needs () do consts_of(tuning()) end
fn bend(c : SynthConsts, w : SynthWave, f : Float, to : Float, amp : Float, decay : Float) : SynthVoice needs () do voice(c, w, f, to, c.default_glide, amp, decay, 0.0) end
fn powf(c : SynthConsts, base : Float, e : Float) : Float needs () do Arcade.Float32.powf(c.round, base, e) end
fn tuning() : SynthTuning needs () do { rate: 44100.0, max_voices: 48, seed: 22222, rumble_follow: 0.0008, lowpass_follow: 0.035, rumble_gain: 0.9, drum_step: 0.055, drum_amp: 0.13, drum_decay: 0.03, min_glide: 0.0001, attack_time: 0.003, square_level: 0.6, default_glide: 0.1 } end
fn new() : Synth needs () do new_tuned(tuning()) end
fn tone(c : SynthConsts, w : SynthWave, f : Float, amp : Float, decay : Float) : SynthVoice needs () do voice(c, w, f, f, c.default_glide, amp, decay, 0.0) end
fn set_drum(s : Synth, drum_on : Bool) : Synth needs () do { s with drum: drum_on } end
fn voice_count(s : Synth) : Int needs () do List.length(s.voices) end
-- ---- Voices ----