This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Some NES audio questions

Some NES audio questions
by on (#36556)
The length counter table on nesdevwiki is conflicting to Blargg's audio document. Which is correct?

by on (#36557)
I think they both match. The one on the wiki is in a more logical order:

Code:
int table [32] = {
    10,254, 20,  2, 40,  4, 80,  6, 160,  8, 60, 10, 14, 12, 26, 14,
    12, 16, 24, 18, 48, 20, 96, 22, 192, 24, 72, 26, 16, 28, 32, 30
};

int length( int r4003 )
{
    return table [r4003 >> 3 & 0x1F];
}

by on (#36558)
Hah! That's what it was. I had misinterpreted the tables from the doc. I'm surprised that the music actually played correctly. Thank you :D

by on (#36563)
tomaitheous wrote:
I'm surprised that the music actually played correctly. Thank you :D

As I understand it, the length counter only determines when notes turn off. A lot of music engines don't even use the length counters, opting instead for pure software envelopes.

by on (#36572)
tepples wrote:
tomaitheous wrote:
I'm surprised that the music actually played correctly. Thank you :D

As I understand it, the length counter only determines when notes turn off. A lot of music engines don't even use the length counters, opting instead for pure software envelopes.


That would make sense why some of the songs played fine but another parts would sound weird.

I changed the topic title instead of creating a new thread. It's been over a year since I did any code for the APU (or more specifically 'emulated' it) and I've seem to forgotten quite a bit. So I'll just post more audio specific questions in this thread.


Concerning the length counter halt flag, does enabling it mid count disable the channel sound(output) or just the counter and the sound keeps playing(infinitely)? I assume it's the latter, so does the volume envelope immediately kick in?

Also, if the counter is at 00 or the halt disable flag is set, and the game code clears the flag(or updates LLLLL) - when does the channel go active? On the next tick of the frame sequencer or immediately?

by on (#36575)
tomaitheous wrote:
Concerning the length counter halt flag, does enabling it mid count disable the channel sound(output) or just the counter and the sound keeps playing(infinitely)? I assume it's the latter


All the halt flag does is stop the length counter from decrementing when it's clocked. Whether or not the channel is silenced depends on the value of the counter itself (zero=silence, nonzero=active), the halt flag is irrelevent.

Clearing the halt flag (enabling length counting) just means the length counter will start counting down and silence the channel once it reaches zero.

Quote:
, so does the volume envelope immediately kick in?


The current output volume is always applied unless the length counter is zero (in which case output is forced to zero/silence).

Quote:
Also, if the counter is at 00 or the halt disable flag is set, and the game code clears the flag(or updates LLLLL) - when does the channel go active? On the next tick of the frame sequencer or immediately?


The halt flag doesn't impact the counter's contents like this. It just prevents it from counting down. Whether or not the halt flag is set or clear... the channel becomes active and audible immediately after the next $4003/7 write (since that updates LLLLL, which reloads the length counter, making it nonzero)

The only time the channel would remain silenced after a write to $4003/7 is if the channel is disabled via $4015 (or if the channel's output volume is 0)

by on (#36576)
That clears up quite a bit. Many thanks :D

In the document it mentioned that the length counter would be set to 00 if the halt flag was set. That was the part I was getting confused with. So it doesn't actually set the counter to 00 (and disabling sound), it just prevents the counter from decrementing. Got it :)


Quote:
the channel becomes active and audible immediately after the next $4003/7 write (since that updates LLLLL, which reloads the length counter, making it nonzero)

Awesome. That simplifies some of the code then.

by on (#36580)
tomaitheous wrote:
In the document it mentioned that the length counter would be set to 00 if the halt flag was set.


You were probably confusing the length halt flag ($4000) with the status channel enable bit ($4015).

by on (#36581)
tomaitheous wrote:
In the document it mentioned that the length counter would be set to 00 if the halt flag was set.

What is "the document"? I'm guessing it's one of the older sound docs, which had many errors or ambiguous descriptions.