update demo/mod/music.fnl

This commit is contained in:
asrael 2026-01-08 14:16:15 -06:00
parent b659523d69
commit 17dd2a23a3

View file

@ -3,60 +3,78 @@
(var time 0) (var time 0)
(var step 0) (var step 0)
(var ctx nil) (var ctx nil)
(var params nil) (var melody-params nil)
(var harmony-params nil)
(var bass-params nil) (var bass-params nil)
(var playing false) (var playing false)
(local bpm 120) (local bpm 95)
(local beat (/ 60 bpm)) (local beat (/ 60 bpm))
(local whole (* 4 beat))
(local half (* 2 beat))
(local quarter beat)
(local eighth (/ beat 2))
(local sixteenth (/ beat 4)) (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] (local melody [[69 eighth] [0 sixteenth] [72 sixteenth]
[67 eighth] [64 eighth] [60 eighth] [64 eighth] [76 eighth] [74 eighth]
[67 eighth] [72 eighth] [76 eighth] [72 eighth] [72 eighth] [69 eighth]
[67 eighth] [64 eighth] [62 eighth] [66 eighth] [67 eighth] [69 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 bass [[36 half] [40 half] [36 half] [38 half] [69 eighth] [0 sixteenth] [72 sixteenth]
; [38 half] [36 half] [40 half] [38 half]]) [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 [] (fn init []
(set ctx (pxl8.sfx_context_create)) (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) (pxl8.sfx_mixer_attach ctx)
(set params (pxl8.sfx_voice_params (set melody-params (pxl8.sfx_voice_params
{:waveform pxl8.SFX_WAVE_TRIANGLE {: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_type pxl8.SFX_FILTER_LOWPASS
:filter_cutoff 2500 :filter_resonance 0.05 :filter_cutoff 4000 :filter_resonance 0.0
:filter_attack 0.05 :filter_decay 0.1 :filter_sustain 0.2 :filter_release 0.05 :fx_send 0.0}))
:filter_env_depth 1500
:fx_send 0.7})))
; (set bass-params (pxl8.sfx_voice_params (set harmony-params (pxl8.sfx_voice_params
; {:waveform pxl8.SFX_WAVE_SAW {:waveform pxl8.SFX_WAVE_TRIANGLE
; :attack 0.02 :decay 0.3 :sustain 0.6 :release 0.4 :attack 0.05 :decay 0.2 :sustain 0.3 :release 0.2
; :filter_type pxl8.SFX_FILTER_LOWPASS :filter_type pxl8.SFX_FILTER_LOWPASS
; :filter_attack 0.02 :filter_decay 0.3 :filter_sustain 0.6 :filter_release 0.4 :filter_cutoff 2500 :filter_resonance 0.0
; :filter_cutoff 90 :filter_resonance 0.25 :fx_send 0.0}))
; :fx_send 0.2})))
(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 [] (fn start []
(set playing true) (set playing true)
@ -72,16 +90,29 @@
(set time (+ time dt)) (set time (+ time dt))
(when (>= time step-duration) (when (>= time step-duration)
(set time (- time step-duration)) (set time (- time step-duration))
(local melody-entry (. melody (+ 1 (% step (length melody)))))
(local note (. melody-entry 1)) (local melody-idx (+ 1 (% step (length melody))))
(local dur (. melody-entry 2)) (local melody-entry (. melody melody-idx))
(pxl8.sfx_play_note ctx note params 0.4 dur) (local melody-note (. melody-entry 1))
; (when (= (% step 4) 0) (local melody-dur (. melody-entry 2))
; (local bass-idx (+ 1 (% (math.floor (/ step 4)) (length bass)))) (when (> melody-note 0)
; (local bass-entry (. bass bass-idx)) (pxl8.sfx_play_note ctx melody-note melody-params 0.45 melody-dur))
; (local bass-note (. bass-entry 1))
; (local bass-dur (. bass-entry 2)) (local bar (math.floor (/ step 32)))
; (pxl8.sfx_play_note ctx bass-note bass-params 0.35 bass-dur)) (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))))) (set step (+ step 1)))))
{:init init {:init init