No luck with Decay & Length - what am I doing wrong?

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
No luck with Decay & Length - what am I doing wrong?
by on (#61148)
During reset, this is being done:
LDX #$40
STX $4017
LDX #$00
STX $4010

$4016 & $4017 are manipulated during controller reads.

And when I'm ready for sound code, I do the following, which I expected to give a length to a note in SQ1:
LDA #$0F
STA $4015
LDA #%00011111
STA $4000
LDA #$F8
STA $4002
LDA #$03
ORA #$C0
STA $4003

Maybe my expectation is wrong? I was expecting a note to play for a short time, and then stop.

And this was how I was trying to get decay working:
LDA #$0F
STA $4015
LDA #%00101111
STA $4000
LDA #$F8
STA $4002
LDA #$03
STA $4003

Also not working - I was expecting a note to start, and then get quieter an quieter until it was silent.

Perhaps it's something obvious to an experienced sound programmer. If it's not, maybe someone can tell me exactly how their code decays notes & gives them length, so I can give that a try.

Thanks
Re: No luck with Decay & Length - what am I doing wrong?
by on (#61150)
rizz1010 wrote:
Perhaps it's something obvious to an experienced sound programmer. If it's not, maybe someone can tell me exactly how their code decays notes & gives them length, so I can give that a try.

Most music engines in actual NES games do this by changing the volume registers ($4000, $4004, and $400C) every few frames. The value written to these registers disables the length counter, disables the built-in envelope feature, and changes the volume. This allows quadratic, exponential, echoed, or other more complex envelopes.