From 17dd2a23a324468035f2334e5e590bacd38d4125 Mon Sep 17 00:00:00 2001 From: asrael Date: Thu, 8 Jan 2026 14:16:15 -0600 Subject: [PATCH] update demo/mod/music.fnl --- demo/mod/music.fnl | 127 ++++++++++++++++++++++++++++----------------- 1 file changed, 79 insertions(+), 48 deletions(-) diff --git a/demo/mod/music.fnl b/demo/mod/music.fnl index 564c4de..d8ff199 100644 --- a/demo/mod/music.fnl +++ b/demo/mod/music.fnl @@ -3,60 +3,78 @@ (var time 0) (var step 0) (var ctx nil) -(var params nil) +(var melody-params nil) +(var harmony-params nil) (var bass-params nil) (var playing false) -(local bpm 120) +(local bpm 95) (local beat (/ 60 bpm)) -(local whole (* 4 beat)) -(local half (* 2 beat)) -(local quarter beat) -(local eighth (/ beat 2)) (local sixteenth (/ beat 4)) +(local eighth (/ beat 2)) +(local quarter beat) +(local whole (* 4 beat)) -(local melody [[60 eighth] [64 eighth] [67 eighth] [72 eighth] - [67 eighth] [64 eighth] [60 eighth] [64 eighth] - [67 eighth] [72 eighth] [76 eighth] [72 eighth] - [67 eighth] [64 eighth] [62 eighth] [66 eighth] - [69 eighth] [74 eighth] [69 eighth] [66 eighth] - [62 eighth] [66 eighth] [69 eighth] [74 eighth] - [78 eighth] [74 eighth] [69 eighth] [66 eighth]]) +(local melody [[69 eighth] [0 sixteenth] [72 sixteenth] + [76 eighth] [74 eighth] + [72 eighth] [69 eighth] + [67 eighth] [69 eighth] -; (local bass [[36 half] [40 half] [36 half] [38 half] -; [38 half] [36 half] [40 half] [38 half]]) + [69 eighth] [0 sixteenth] [72 sixteenth] + [76 eighth] [79 eighth] + [77 eighth] [76 eighth] + [74 quarter] -(local step-duration eighth) + [74 eighth] [0 sixteenth] [76 sixteenth] + [77 eighth] [74 eighth] + [72 eighth] [69 eighth] + [67 eighth] [69 eighth] + + [72 eighth] [0 sixteenth] [74 sixteenth] + [76 eighth] [72 eighth] + [69 eighth] [67 eighth] + [69 quarter]]) + +(local harmony [[57 whole] ; A3 + [60 whole] ; C4 + [62 whole] ; D4 + [57 whole]]) ; A3 + +(local bass [[45 eighth] [45 eighth] [57 eighth] [45 eighth] + [45 eighth] [57 eighth] [45 eighth] [57 eighth] + [48 eighth] [48 eighth] [60 eighth] [48 eighth] + [48 eighth] [60 eighth] [48 eighth] [60 eighth] + [50 eighth] [50 eighth] [62 eighth] [50 eighth] + [50 eighth] [62 eighth] [50 eighth] [62 eighth] + [45 eighth] [45 eighth] [57 eighth] [45 eighth] + [45 eighth] [57 eighth] [52 eighth] [45 eighth]]) + +(local step-duration sixteenth) (fn init [] (set ctx (pxl8.sfx_context_create)) - - (local delay (pxl8.sfx_delay_create {:time_l 350 :time_r 500 :feedback 0.6 :mix 0.2})) - (local reverb (pxl8.sfx_reverb_create {:room 0.25 :damping 0.5 :mix 0.5})) - (local compressor (pxl8.sfx_compressor_create {:threshold -12 :ratio 4 :attack 10 :release 100})) - - (pxl8.sfx_context_append_node ctx delay) - (pxl8.sfx_context_append_node ctx reverb) - (pxl8.sfx_context_append_node ctx compressor) - (pxl8.sfx_mixer_attach ctx) - (set params (pxl8.sfx_voice_params + (set melody-params (pxl8.sfx_voice_params {:waveform pxl8.SFX_WAVE_TRIANGLE - :attack 0.01 :decay 0.25 :sustain 0.0 :release 0.01 + :attack 0.02 :decay 0.15 :sustain 0.5 :release 0.15 :filter_type pxl8.SFX_FILTER_LOWPASS - :filter_cutoff 2500 :filter_resonance 0.05 - :filter_attack 0.05 :filter_decay 0.1 :filter_sustain 0.2 :filter_release 0.05 - :filter_env_depth 1500 - :fx_send 0.7}))) + :filter_cutoff 4000 :filter_resonance 0.0 + :fx_send 0.0})) - ; (set bass-params (pxl8.sfx_voice_params - ; {:waveform pxl8.SFX_WAVE_SAW - ; :attack 0.02 :decay 0.3 :sustain 0.6 :release 0.4 - ; :filter_type pxl8.SFX_FILTER_LOWPASS - ; :filter_attack 0.02 :filter_decay 0.3 :filter_sustain 0.6 :filter_release 0.4 - ; :filter_cutoff 90 :filter_resonance 0.25 - ; :fx_send 0.2}))) + (set harmony-params (pxl8.sfx_voice_params + {:waveform pxl8.SFX_WAVE_TRIANGLE + :attack 0.05 :decay 0.2 :sustain 0.3 :release 0.2 + :filter_type pxl8.SFX_FILTER_LOWPASS + :filter_cutoff 2500 :filter_resonance 0.0 + :fx_send 0.0})) + + (set bass-params (pxl8.sfx_voice_params + {:waveform pxl8.SFX_WAVE_TRIANGLE + :attack 0.015 :decay 0.1 :sustain 0.7 :release 0.1 + :filter_type pxl8.SFX_FILTER_LOWPASS + :filter_cutoff 1200 :filter_resonance 0.0 + :fx_send 0.0}))) (fn start [] (set playing true) @@ -72,16 +90,29 @@ (set time (+ time dt)) (when (>= time step-duration) (set time (- time step-duration)) - (local melody-entry (. melody (+ 1 (% step (length melody))))) - (local note (. melody-entry 1)) - (local dur (. melody-entry 2)) - (pxl8.sfx_play_note ctx note params 0.4 dur) - ; (when (= (% step 4) 0) - ; (local bass-idx (+ 1 (% (math.floor (/ step 4)) (length bass)))) - ; (local bass-entry (. bass bass-idx)) - ; (local bass-note (. bass-entry 1)) - ; (local bass-dur (. bass-entry 2)) - ; (pxl8.sfx_play_note ctx bass-note bass-params 0.35 bass-dur)) + + (local melody-idx (+ 1 (% step (length melody)))) + (local melody-entry (. melody melody-idx)) + (local melody-note (. melody-entry 1)) + (local melody-dur (. melody-entry 2)) + (when (> melody-note 0) + (pxl8.sfx_play_note ctx melody-note melody-params 0.45 melody-dur)) + + (local bar (math.floor (/ step 32))) + (local harmony-idx (+ 1 (% bar (length harmony)))) + (local harmony-entry (. harmony harmony-idx)) + (local harmony-note (. harmony-entry 1)) + (when (= (% step 32) 0) + (pxl8.sfx_play_note ctx harmony-note harmony-params 0.25 whole)) + + (local bass-step (math.floor (/ step 2))) + (local bass-idx (+ 1 (% bass-step (length bass)))) + (local bass-entry (. bass bass-idx)) + (local bass-note (. bass-entry 1)) + (local bass-dur (. bass-entry 2)) + (when (= (% step 2) 0) + (pxl8.sfx_play_note ctx bass-note bass-params 0.55 bass-dur)) + (set step (+ step 1))))) {:init init