janac

Custom waveform duration

Nov 12th, 2025 (edited)
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.82 KB | None | 0 0
  1.  
  2. -- trying to write a sine wave to slot 0 and play it through SFX 8.
  3.  
  4. sfx_memory = 0x3200
  5. pi = 3.1416
  6. sfx_id = 8 -- triggered by z key
  7. pitch = 24            
  8. custom_slot = 0 -- custom waveform location        
  9. volume = 3          
  10. effect = 0          
  11. custom_bit = 0x8000 -- indicates there is a custom waveform
  12.  
  13. -- write one step
  14. function poke_step(sfx_id, step, pitch, custom_slot, volume, effect, custom_bit)
  15.    
  16.     -- 0-5 pitch, 6-8 custom_slot, 9-11 volume, 12-14 effect, 15 custom_bit
  17.     pitch = pitch & 0x3f
  18.     custom_slot = custom_slot & 0x07
  19.     volume = volume & 0x07
  20.     effect = effect & 0x07
  21.  
  22.     local value = 0
  23.     value = value | pitch
  24.     value = value | (custom_slot << 6)
  25.     value = value | (volume << 9)
  26.     value = value | (effect << 12)
  27.     value = value | custom_bit
  28.  
  29.     -- address of the step
  30.     local addr = 0x3200 + sfx_id * 68 + step * 2
  31.  
  32.     -- little-endian
  33.     poke(addr, value & 0xff)
  34.     poke(addr+1, (value >> 8) & 0xff)
  35. end
  36.  
  37. function _init()
  38.     -- write samples to custom waveform slot 0
  39.     for i = 0, 63 do
  40.     local angle = (i / 64) * 2 * pi
  41.     local sample = flr(128 + 127.5 * sin(angle))
  42.     poke(sfx_memory + i, sample)
  43.     end
  44.  
  45.     -- populate step 0 of sfx 8
  46.     poke_step(sfx_id, 0, pitch, custom_slot, volume, effect, custom_bit)
  47.  
  48.     -- zero remaining 31 steps
  49.     for step=1,31 do
  50.         local addr = 0x3200 + sfx_id*68 + step*2
  51.         poke(addr, 0)
  52.         poke(addr+1, 0)
  53.     end
  54.  
  55.     -- Last 4 bytes of slot: filters, speed, loop start and stop
  56.     poke(0x3200 + sfx_id*68 + 64, 0)
  57.     poke(0x3200 + sfx_id*68 + 65, 0)
  58.     poke(0x3200 + sfx_id*68 + 66, 0)
  59.     poke(0x3200 + sfx_id*68 + 67, 0)
  60. end
  61.  
  62. function _update()
  63.     if btnp(4) then
  64.         sfx(sfx_id)
  65.     end
  66. end
  67.  
  68. function _draw()
  69.     cls()
  70.     print("press z", 10, 10, 7)
  71. end
Advertisement
Add Comment
Please, Sign In to add comment