CCCPaste

Custom waveform duration


-- trying to write a sine wave to slot 0 and play it through SFX 8.

sfx_memory = 0x3200 
pi = 3.1416
sfx_id = 8 -- triggered by z key
pitch = 24            
custom_slot = 0 -- custom waveform location        
volume = 3           
effect = 0           
custom_bit = 0x8000 -- indicates there is a custom waveform 

-- write one step
function poke_step(sfx_id, step, pitch, custom_slot, volume, effect, custom_bit)
    
    -- 0-5 pitch, 6-8 custom_slot, 9-11 volume, 12-14 effect, 15 custom_bit
    pitch = pitch & 0x3f
    custom_slot = custom_slot & 0x07
    volume = volume & 0x07
    effect = effect & 0x07

    local value = 0
    value = value | pitch
    value = value | (custom_slot << 6)
    value = value | (volume << 9)
    value = value | (effect << 12)
    value = value | custom_bit

    -- address of the step
    local addr = 0x3200 + sfx_id * 68 + step * 2

    -- little-endian
    poke(addr, value & 0xff)
    poke(addr+1, (value >> 8) & 0xff)
end

function _init()
    -- write samples to custom waveform slot 0
    for i = 0, 63 do
    local angle = (i / 64) * 2 * pi
    local sample = flr(128 + 127.5 * sin(angle)) 
    poke(sfx_memory + i, sample)
    end

    -- populate step 0 of sfx 8
    poke_step(sfx_id, 0, pitch, custom_slot, volume, effect, custom_bit)

    -- zero remaining 31 steps
    for step=1,31 do
        local addr = 0x3200 + sfx_id*68 + step*2
        poke(addr, 0)
        poke(addr+1, 0)
    end

    -- Last 4 bytes of slot: filters, speed, loop start and stop
    poke(0x3200 + sfx_id*68 + 64, 0)
    poke(0x3200 + sfx_id*68 + 65, 0)
    poke(0x3200 + sfx_id*68 + 66, 0)
    poke(0x3200 + sfx_id*68 + 67, 0)
end

function _update()
    if btnp(4) then
        sfx(sfx_id)
    end
end

function _draw()
    cls()
    print("press z", 10, 10, 7)
end