Correct usage of $4011 with DPCM?

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

Post Reply
User avatar
GradualGames
Posts: 1106
Joined: Sun Nov 09, 2008 9:18 pm
Location: Pennsylvania, USA
Contact:

Correct usage of $4011 with DPCM?

Post by GradualGames »

If I'm just playing samples and never manually feeding PCM samples through $4011, I should just leave the register totally alone, right? I just found a bug in ggsound where I had been writing this register every frame alongside sample playback. It only sounds correct vs. what I hear in FamiTracker when I totally leave $4011 alone. (interestingly, the only problem that it introduced when I HADN'T left it alone was very subtle differences in the pitch of the dpcm sample being played. Didn't notice it at all until now.)
tepples
Posts: 22705
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Correct usage of $4011 with DPCM?

Post by tepples »

In FamiTracker:
  • The Zxx effect in the DPCM channel writes its argument to $4011.
  • Each DPCM instrument specifies whether to write $4011 first. Down the middle of the DPCM instrument editor are three controls Octave, Pitch, and D-. If D- is set to "Off", no $4011 write is made before playback. If it's instead set to 0-127, that value is written to $4011.
  • There appears to be a difference between "note release", which only stops playback, and "note cut", which stops playback and then writes to $4011.
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: Correct usage of $4011 with DPCM?

Post by thefox »

It's not a good idea to write $4011 while you're playing samples, because it's not well defined whether the writes will take effect or not. (I guess you can force it to take effect by writing a couple of times in row, though.) You might want to write it right before you start a sample though, to make the sample playback always start at the same level (e.g., centered at $40). This makes sure that the samples can't go haywire and saturate to the upper/lower limit of the 7-bit range. However, it might cause some popping to be heard when the sample starts.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
tepples
Posts: 22705
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Correct usage of $4011 with DPCM?

Post by tepples »

Another technique is to convert the wave in an unsigned manner, adding DC to the start and end of the wave so that it comes out to 0 at the start and end. I think FamiTracker calls this "tilt".
User avatar
GradualGames
Posts: 1106
Joined: Sun Nov 09, 2008 9:18 pm
Location: Pennsylvania, USA
Contact:

Re: Correct usage of $4011 with DPCM?

Post by GradualGames »

thefox wrote:It's not a good idea to write $4011 while you're playing samples, because it's not well defined whether the writes will take effect or not. (I guess you can force it to take effect by writing a couple of times in row, though.) You might want to write it right before you start a sample though, to make the sample playback always start at the same level (e.g., centered at $40). This makes sure that the samples can't go haywire and saturate to the upper/lower limit of the 7-bit range. However, it might cause some popping to be heard when the sample starts.
What sort of going haywire can occur? Also what do you mean by level and 7 bit range (what sort of range?) Right at the moment, it appears the best behavior comes from never writing $4011. I did actually hear some popping sometimes when I HAD been writing $4011, which is unpleasant for music playback.
tepples
Posts: 22705
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Correct usage of $4011 with DPCM?

Post by tepples »

Not writing $4011 means no pops. But it also means your balance between triangle and noise on one hand and the pulse on the other hand depends on exactly where the sample was stopped.
User avatar
GradualGames
Posts: 1106
Joined: Sun Nov 09, 2008 9:18 pm
Location: Pennsylvania, USA
Contact:

Re: Correct usage of $4011 with DPCM?

Post by GradualGames »

tepples wrote:Not writing $4011 means no pops. But it also means your balance between triangle and noise on one hand and the pulse on the other hand depends on exactly where the sample was stopped.
I wonder what situations would reveal this behavior most clearly? I haven't noticed anything odd or like volume changed or what not when I leave $4011 alone.
tepples wrote:Another technique is to convert the wave in an unsigned manner, adding DC to the start and end of the wave so that it comes out to 0 at the start and end. I think FamiTracker calls this "tilt".
If FamiTracker somehow takes care of this during the sample conversion process to begin with (as maybe you were saying with regards to the "tilt" feature?) that may explain why I am not noticing anything, all my samples were generated by importing wavs in famitracker.

Adding in a write to $4011 when I restart the sample playback really sounds bad. Thankfully I don't use DPCM myself; just wondering on behalf of what users of ggsound may run into if I leave $4011 alone.
lidnariq
Posts: 11429
Joined: Sun Apr 13, 2008 11:12 am

Re: Correct usage of $4011 with DPCM?

Post by lidnariq »

GradualGames wrote:I haven't noticed anything odd or like volume changed or what not when I leave $4011 alone.
The difference in triangle and noise channel volume over the range of the DPCM DAC is only about 4 dB—it might be subtle enough to not be noticeable except by contrast within a song.
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: Correct usage of $4011 with DPCM?

Post by thefox »

GradualGames wrote:What sort of going haywire can occur? Also what do you mean by level and 7 bit range (what sort of range?) Right at the moment, it appears the best behavior comes from never writing $4011. I did actually hear some popping sometimes when I HAD been writing $4011, which is unpleasant for music playback.
What tepples said (although it's minor). There's another somewhat minor case where if your sample playback is not balanced (that is, the sum of the played +2/-2 samples is not 0), you will accumulate some bias in the DAC counter. Eventually it could hit the upper or lower boundary of the 7-bit range (0 or 127), and the sample would start getting distorted. Thus, I think a good safeguard is to reset the DAC counter at least sometimes (e.g., when starting a new song).

By level I simply meant the value of the DAC counter. The DAC counter has a 7-bit range, incremented/decremented by 2 based on each bit of the DPCM sample.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
User avatar
rainwarrior
Posts: 8731
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Correct usage of $4011 with DPCM?

Post by rainwarrior »

When it hits the edge, that particular peak gets cut off, but when the waveform returns it kind of makes its own headroom again, so DPCM samples tend to have an auto--centering kind of effect. The actual distortion from the clamp is fairly localized, it's like a single very soft pop, and not at all like e.g. clipping distortion. IMO it's not even very noticeable in the mix with DPCM's other inherent distortions.

Like, if your waveform is centred, you've got room for 32 steps up, 32 steps down without hitting the edge (128 / +/-2 per sample = 64).

Eg. if your sample has a peak of +/- 15, if started in the middle it won't hit either side, and the central/rest value should be the same before and after the sample. However, if it was started near one edge or the other, the clamping will automatically push it away from the edge when that peak is hit, so it would end up centred around 15 or 64-15 by the end.

The exception to this is if they waveform is continually having a peak to trough greater than 64, which would produce continual distortion of the waveform. That's something to be avoided.


There is also the possibility of making unbalanced waveforms that don't oscillate around a centre line. This is not something that's easy to do with standard audio software, unfortunately. Famitracker's "tilt" feature in its sample editor is sort of a manifestation of this. It inserts random 0 samples into the selected range, with the purpose that when the end of the sample is dying down in volume it also starts drifting back to 0, so that your samples may consistently leave the DMC's counter at 0 rather than some floating centre point.
Post Reply