Strategies for sound management.

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
User avatar
clueless
Posts: 496
Joined: Sun Sep 07, 2008 7:27 am
Location: Seatlle, WA, USA

Strategies for sound management.

Post by clueless »

How do games like Zelda, Metroid, Final Fantasy and Crystalis play "background" music while your character wanders around?

Do they do so with the square, triangle and noise channels or with DMC?

Is DMC typically used for one-off sounds, like getting hit by an enemy, or discharging an energy bolt from your sword?

Or is it better to avoid DMC entirely?

How much CPU time is typically required to keep a back-ground music track running (ie, keep the "buffer" if you will, saturated)?
User avatar
Bregalad
Posts: 8036
Joined: Fri Nov 12, 2004 2:49 pm
Location: Caen, France

Post by Bregalad »

Typically the sound engine is called once per frame, and will play the music that is supposed to play by writing to $4000-$400F APU registers.
Most games use all channels for sound FX and music (when there is a SFX the music is temporaly silented on that channel), but some games doesn't use all chanels to simplify code (typically Dragon Quest/Warrior only uses one square and triangle for music and one square and noise for SFX, it also uses the second square for title screen and ending. The same applies to Gradius).
Useless, lumbering half-wits don't scare us.
User avatar
blargg
Posts: 3717
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Re: Strategies for sound management.

Post by blargg »

clueless wrote:How much CPU time is typically required to keep a back-ground music track running (ie, keep the "buffer" if you will, saturated)?
There is no buffer. The APU plays waveforms without any CPU help; the CPU just needs to tell it to change parameters (frequency, volume, etc.). Most (all?) music drivers run at a regular rate, usually every frame. During that time, the playback code advance in the music sequence data as necessary and updates waveform parameters.
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Strategies for sound management.

Post by tokumaru »

clueless wrote:How do games like Zelda, Metroid, Final Fantasy and Crystalis play "background" music while your character wanders around?
For you, the player, it may seem like it's all happening at the same time, but the program actually alternates between updating the game's state (read input, move characters, etc) and updating the music channels. The NES keeps outputting even when you are not doing anything related to sound, based on the last settings written to the sound registers.

As Bregalad said, sound updates are usually performed once per frame, since there are 60 frames per second (NTSC) the sound channels are updated that many times. Between sound updates (when all other updates are performed), the NES just keeps outputting sound based on the last parameters you sent it.

Also, a sound effect isn't initialized at the exact moment the event that generated it happened (e.g. a monster starts blowing up). At that moment, the code will probably tell the music engine which sound effect must be played, but that won't happen until the music engine is executed again. A speed as high as 60 frames per second results in very short intervals between updates, short enough for us humans not to notice these small delays, and think it's all happening at once.
Is DMC typically used for one-off sounds, like getting hit by an enemy, or discharging an energy bolt from your sword?
That is up to the programmer. Some will reserve channels exclusively for sound effects, but that might make the music sound too poor with just standard NES audio (no mappers). Because of that, sound effects usually steal some sound channels temporally from the music, and gives them back when they end. Pay attention and you will notice that in many games some instruments of the music simply stop when there are sound effects. We usually don't notice that because the sound effect seems more important, as it is related to something that's happening on the screen, while the music is just background.
Or is it better to avoid DMC entirely?
Well, there are some problems with it, such as the corrupted controller reads discovered recently. It also seems to affect the timing of other operations, such as sprite DMA. I don't remember many games that used it during the main game. It's mostly used for drums, but so is the noise channel.
How much CPU time is typically required to keep a back-ground music track running (ie, keep the "buffer" if you will, saturated)?
Like blargg said, there is no buffer. It will keep generating sound based on the last parameters written to it. Music only exists because the sound engines change those parameters in a timely fashion.

How much CPU time a sound engine uses varies a lot from one to another. It's the same as asking "how much time does the logic of a platformer take?", that's too vague. It greatly depends on how many features there are and how well optimized the code is.
User avatar
clueless
Posts: 496
Joined: Sun Sep 07, 2008 7:27 am
Location: Seatlle, WA, USA

Post by clueless »

Thanks for the 50,000 foot view, guys. :)

One question: Tokumaru and Blargg both mentioned using DMC to play "drums". Do you guys literally mean the sound of the percussion instrument, or is "drums" an entire category of sounds?

In either case, why is DMC required? Is it that the regular sound channels can't modulate as fast as some drum beats (I'm guessing here)?
User avatar
Dwedit
Posts: 4470
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Post by Dwedit »

DMC will play any digitized sound you throw at it. Give it a recording of a drum and it will play it. It's how those baseball games on the NES yell out "Strike!" and "Ball!".
Of course, first the sound gets converted to 1-bit, and gets sampled at a much lower rate.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Post by tokumaru »

clueless wrote:Do you guys literally mean the sound of the percussion instrument
Percussion, yes.
In either case, why is DMC required? Is it that the regular sound channels can't modulate as fast as some drum beats (I'm guessing here)?
It's not a matter of speed, it's just that using the noise channel may not sound as realistic as actual drums. The noise channel produces basically a hissing sound, but if it starts suddenly with a high volume and then slowly fades away, it sounds close to a drum beat.
User avatar
clueless
Posts: 496
Joined: Sun Sep 07, 2008 7:27 am
Location: Seatlle, WA, USA

Post by clueless »

Dwedit wrote:DMC will play any digitized sound you throw at it. Give it a recording of a drum and it will play it. It's how those baseball games on the NES yell out "Strike!" and "Ball!".
Of course, first the sound gets converted to 1-bit, and gets sampled at a much lower rate.
Thanks.

Did you write this? (Who is David de Regt. ?)

http://nesdev.com/wav2nes.zip

or this one? http://nesdev.com/81v2.zip
User avatar
BMF54123
Posts: 410
Joined: Mon Aug 28, 2006 2:52 am
Contact:

Post by BMF54123 »

tokumaru wrote:It's not a matter of speed, it's just that using the noise channel may not sound as realistic as actual drums. The noise channel produces basically a hissing sound, but if it starts suddenly with a high volume and then slowly fades away, it sounds close to a drum beat.
Or you can use a combination of the triangle and noise channels to create a fairly realistic drum sound. I'm not sure exactly how it's done, but the Kiwi Kraze soundtrack provides pretty good examples of the technique.
Post Reply