Some NES audio questions

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

Post Reply
tomaitheous
Posts: 592
Joined: Thu Aug 28, 2008 1:17 am
Contact:

Some NES audio questions

Post by tomaitheous »

The length counter table on nesdevwiki is conflicting to Blargg's audio document. Which is correct?
Last edited by tomaitheous on Sat Aug 30, 2008 3:17 pm, edited 1 time in total.
User avatar
blargg
Posts: 3717
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Post by blargg »

I think they both match. The one on the wiki is in a more logical order:

Code: Select all

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];
}
tomaitheous
Posts: 592
Joined: Thu Aug 28, 2008 1:17 am
Contact:

Post by tomaitheous »

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
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Post by tepples »

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.
tomaitheous
Posts: 592
Joined: Thu Aug 28, 2008 1:17 am
Contact:

Post by tomaitheous »

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?
User avatar
Disch
Posts: 1848
Joined: Wed Nov 10, 2004 6:47 pm

Post by Disch »

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.
, 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).
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)
tomaitheous
Posts: 592
Joined: Thu Aug 28, 2008 1:17 am
Contact:

Post by tomaitheous »

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 :)

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.
User avatar
Disch
Posts: 1848
Joined: Wed Nov 10, 2004 6:47 pm

Post by Disch »

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).
User avatar
blargg
Posts: 3717
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Post by blargg »

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.
Post Reply