0,]']±>Sï}WÿÍu§ðp8·d%qy)6o;]ÉA¦ösï÷ôôô”°ÂVl†30 and ref>0,"distance must be > 0") return source_db-20*log10(d/ref) end -- distance where the level falls to target_db (inverse of spl_at) function dist_for(source_db,target_db,ref) ref=ref or REF_FT return ref*10^((source_db-target_db)/20) end -- zone for a level: 1 <85, 2 85-120, 3 120-140, 4 >=140 ZONE_LIMITS={85,120,140} function zone_of(db) local z=1 for i=1,#ZONE_LIMITS do if db>=ZONE_LIMITS[i] then z=i+1 end end return z end function clamp(v,a,b) if vb then return b end return v end ---------------------------------------------------------------- -- display constants (sweetie-16 palette) ---------------------------------------------------------------- ZONE_COL={6,4,3,2} -- green, yellow, orange, red ZONE_TXT={ "below 85 dB: quieter zone", "85+ dB: risky with long exposure", "120+ dB: around the pain threshold", "140+ dB: instant hearing damage risk"} AX0,AX1=32,232 -- distance axis, 100 px per decade MX0,MX1=32,232 -- meter: 60..160 dB, 2 px per dB M_LO,M_HI=60,160 GROUND=64 function x_of_dist(d) return AX0+(log10(d)-1)*(AX1-AX0)/2 end function x_of_db(db) return MX0+(clamp(db,M_LO,M_HI)-M_LO)*(MX1-MX0)/(M_HI-M_LO) end ---------------------------------------------------------------- -- state ---------------------------------------------------------------- src=150 -- dB at 10 ft ld=2 -- log10(distance ft), 1..3; starts at 100 ft t=0 inited=false last_zone=nil function dist() return 10^ld end ---------------------------------------------------------------- -- sound: one soft triangle-ish wave, short decaying envelope, -- played on three channels as a chord. Volume capped at 7/15. ---------------------------------------------------------------- function init_sound() local waddr=0x0FFE4+16 -- waveform 1 for i=0,15 do local a=i<8 and i*2 or (15-i)*2 local b=a+1 poke(waddr+i,(clamp(a,0,15))|(clamp(b,0,15)<<4)) end local base=0x100E4 -- SFX 0 for f=0,29 do local vol=f*2 -- stored inverted: 0 = loudest if vol>15 then vol=15 end poke(base+f*2,vol|(1<<4)) -- volume, wave 1 poke(base+f*2+1,0) end poke(base+60,0) -- octave/speed defaults poke(base+61,0) for i=62,65 do poke(base+i,0) end end function chord(db) local v=clamp(math.floor((db-60)/15),2,7) sfx(0,"C#4",14,0,v) sfx(0,"E-4",14,1,v) sfx(0,"A#4",14,2,v) end ---------------------------------------------------------------- -- drawing helpers ---------------------------------------------------------------- function draw_horn(x,y) -- bell + body of a horn icon tri(x+4,y+6,x+18,y-2,x+18,y+14,13) rect(x,y+3,6,6,14) rect(x+17,y-3,3,18,12) rect(x-2,y+9,4,7,15) end function draw_listener(x,y,c) circ(x,y-15,3,c) line(x,y-12,x,y-5,c) line(x-4,y-10,x+4,y-10,c) line(x,y-5,x-3,y,c) line(x,y-5,x+3,y,c) end function fmt_ft(d) if d<100 then return string.format("%.1f ft",d) end return string.format("%d ft",math.floor(d+0.5)) end ---------------------------------------------------------------- -- main loop ---------------------------------------------------------------- function TIC() if not inited then init_sound() inited=true end t=t+1 -- input if btn(2) then ld=ld-0.006 end if btn(3) then ld=ld+0.006 end local jumped=false if btnp(0,20,10) then ld=ld+log10(2) jumped=true end if btnp(1,20,10) then ld=ld-log10(2) jumped=true end ld=clamp(ld,1,3) local changed=false if btnp(4,15,4) then src=src-1 changed=true end if btnp(5,15,4) then src=src+1 changed=true end src=clamp(src,100,180) local d=dist() local db=spl_at(src,d) local z=zone_of(db) if jumped or changed or (last_zone and z~=last_zone) then chord(db) end last_zone=z cls(0) -- header print("DECIBEL DISTANCE EXPLORER",45,2,12) print("SOURCE "..src.." dB at 10 ft",4,12,13,false,1,true) print("Z- X+",206,12,14,false,1,true) print("<> walk ^ x2 dist v /2 dist",4,20,14,false,1,true) -- sound rings from the horn, colored by the level they reach clip(24,26,216,GROUND-26) local hx,hy=12,GROUND-12 for k=0,9 do local r=((t//2)+k*22)%220 local rd=10^(1+math.max(0,hx+r-AX0)*2/(AX1-AX0)) circb(hx,hy,r,ZONE_COL[zone_of(spl_at(src,rd))]) end clip() -- ground, horn, listener line(0,GROUND,239,GROUND,15) draw_horn(2,GROUND-18) local px=x_of_dist(d) draw_listener(px,GROUND-1,ZONE_COL[z]) -- distance axis (log) local marks={10,20,50,100,200,500,1000} line(AX0,GROUND+3,AX1,GROUND+3,13) for _,m in ipairs(marks) do local x=x_of_dist(m) line(x,GROUND+2,x,GROUND+5,13) local s=tostring(m) print(s,x-#s*2+1,GROUND+7,13,false,1,true) end print("ft",AX1-4,GROUND+13,14,false,1,true) -- meter: zone strip + fill bar local my=90 for zi=1,4 do local lo=zi==1 and M_LO or ZONE_LIMITS[zi-1] local hi=zi==4 and M_HI or ZONE_LIMITS[zi] local x0,x1=x_of_db(lo),x_of_db(hi) rect(x0,my+9,x1-x0,2,ZONE_COL[zi]) end rectb(MX0-1,my-1,MX1-MX0+2,10,13) rect(MX0,my,x_of_db(db)-MX0,8,ZONE_COL[z]) for _,lim in ipairs(ZONE_LIMITS) do local x=x_of_db(lim) line(x,my-3,x,my+8,12) print(lim,x-4,my-9,12,false,1,true) end print("dB",4,my+1,13) -- readout local txt=string.format("%.1f dB at %s",db,fmt_ft(d)) print(txt,120-print(txt,0,-20,0,false,1)//2,my+15,ZONE_COL[z]) print(ZONE_TXT[z],120-print(ZONE_TXT[z],0,-20,0,false,1,true)//2,my+24,12,false,1,true) print("estimates, free field - train-horn.com",4,130,14,false,1,true) end