Topic: Noise generator code

Just in case anyone will need it, came up with a kind of universal noise routine. It is strongly based on utz's noise generator with rlc h. Allows to control pitch, volume, and duration of a noise burst.

    ld hl,0                ;noise accumulator
    ld de,#2174            ;utz's rand seed
    ld bc,#0101            ;noise pitch, #01 highest, #ff (#00 actually) lowest
    exx
    ld bc,1000            ;noise duration

noise_loop

    exx                    ;4
    dec c                ;4
    jr nz,noise_skip    ;7/12
    ld c,b                ;4
    add hl,de            ;11
    rlc h                ;8        utz's noise generator idea
    inc d                ;4        improves randomness
    jp noise_next        ;10
    
noise_skip

    jr $+2                ;12
    jr $+2                ;12
    nop                    ;4
    nop                    ;4
    
noise_next

    ld a,h                ;4
    
noise_volume=$+1
    cp #80                ;7    noise 'volume' here, #80 max, less is lower volume
    
    sbc a,a                ;4
    out (#fe),a            ;11
    exx                    ;4

    dec bc                ;6
    ld a,b                ;4
    or c                ;4
    jp nz, noise_loop    ;10=106t
website - 1bit music - other music - youtube - bandcamp - patreon - twitter (latest news there)

Re: Noise generator code

Excellent. I think noise is another underexplored area in beeper music, we definately can do more with it.
Btw I have an algo that produces similar quality as the above, but doesn't need DE. I'm using this for the click drums in my recent engines.

     add hl,hl    ;11
     sbc a,a      ;4
     xor l        ;4
     ld l,a       ;4

Then afterwards, the usual cp with duty threshold. Some suitable seeds are 1, #1011, and #1237.

I also tried to do it with just 8-bit operations.

     ld a,d
     add a,e
     rlca
     ld d,a
     rr e

Seed DE with #21. Results are ok-ish, but not great. So I abandoned this idea for now.