gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Very very simple sound demo

#106868 - HyperHacker - Tue Oct 24, 2006 8:15 am

Just figured I'd post this because sound documentation seems to be quite scarce. This explains how to play a tone of a specific frequency. The idea here is rather than generating a sample or loading it from a file, you do it like you would on old consoles and just choose the frequency and manually start and stop playback. This is good for things like emulation of said consoles.

First you turn sound on:
Code:
SOUND_CR = SOUND_VOL(127) | SOUND_ENABLE;

For the most part you can keep the volume at max (127) as the DS provides a hardware volume control. Software volume control might be useful for environment effects or something. I'm not sure if the DS can play loud enough sounds to damage its speakers, but be sensible.

You're supposed to wait 15 milliseconds before playing sound after turning it on. DS runs at 60FPS so one frame will take about 16ms; call swiWaitForVBlank() twice just to be safe (lest you call right before VBlank and end up only delaying 1ms). You have to have the VBlank interrupt enabled for this, which you should know how to do already.

Once you've done that you just choose your frequency, channel volume, pan, and wave duty and start the sound:

Code:
SCHANNEL_TIMER(8) = SOUND_FREQ(DesiredFrequency << 3);
SCHANNEL_CR(8) = SOUND_VOL(127) | SOUND_PAN(64) | SCHANNEL_WAVEDUTY(6) | SOUND_FORMAT_PSG | SCHANNEL_ENABLE;


Simple breakdown of these values:
SOUND_FREQ(DesiredFrequency << 3) should be obvious. Your frequency needs to be multiplied by 8; you can left-shift by 3 as I've done here to accomplish the same but faster.

SOUND_VOL(127) sets the individual channel volume; again, no real reason to reduce it in a simple demo app.

SOUND_PAN(64) centers the sound. 0 would make it entirely on the left speaker, 127 entirely on the right.

SOUND_FORMAT_PSG enables the Programmable Sound Generator, which tells the DS we're doing choose-a-frequency-and-go sounds rather than playing actual sound samples in memory. There's probably a term for that but I can't think of one.

SCHANNEL_ENABLE, of course, turns the channel on.

libnds doesn't appear to have a wave duty macro, but you can add one:
#define SCHANNEL_WAVEDUTY(n) (n << 24)
Various documents list completely different information for the wave duty values. The most reasonable I've seen is GBATek's: 0=12.5%..6=87.5%, 7=silence.

Finally, note that you can only do this on channels 8 through 13. You can also generate white noise using PSG on channels 14 and 15.
_________________
I'm a PSP hacker now, but I still <3 DS.

#106879 - Lick - Tue Oct 24, 2006 11:14 am

Thanks man, quite easy to follow! Good job!

- Lick
_________________
http://licklick.wordpress.com

#106885 - 0xtob - Tue Oct 24, 2006 1:06 pm

Yeah, thanks for taking the time to write that up! This should be easier to understand for newbies than the PSG description in GBAtek.

In fact, I would be happy to have some more documentation on the PSG. As the DS inherited the programmable sound generator from the GBA, which in turn inherited it from the Game Boy, the DS should be capable of much more oldskool sound goodness than described in GBAtek (which only covers pulse waves and noise). So, are there some hardware gurus around who could take a look at that?

Bye,
Tob

#106902 - tepples - Tue Oct 24, 2006 4:29 pm

AXE uses the PSG.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#106912 - josath - Tue Oct 24, 2006 7:00 pm

I got this demo from mic a while ago, which he apparently wrote to test PSG emulation in his emulator, dualis. Includes a .NDS and the main source file, but not all of the supporting things like makefiles, etc. Should be easy enough to get to compile though. (may be using older version of libnds)

http://davr.org/ds2/PSG_Play.zip

#107195 - HyperHacker - Fri Oct 27, 2006 3:52 pm

I was doing some more testing and GBATek's wave duty information actually seems to be wrong. 0 and 6 sound almost identical (very coarse) while 3 sounds a lot different. This matches another document I read, but I don't recall where.
_________________
I'm a PSP hacker now, but I still <3 DS.

#114415 - clrFog - Sun Jan 07, 2007 7:57 pm

Hi, I want to start playing around with the PSG. I use the simple sound example project as a startingpoint. My problem is that when i paste

SOUND_CR = SOUND_ENABLE | SOUND_VOL(0x7F);

in main the compiler doesn't recognize it; " 'SOUND_CR' undeclared (first use in this function) ". And the documentation for libnds seems to be down at the moment. Can anywone explain whats wrong?
Yes I'm noob ;)
Would appreach any help.
_________________
DS lite white, M3 perfect lite micro SD, Sandisk 512MB

#114416 - Lick - Sun Jan 07, 2007 8:02 pm

SOUND_CR is only available on the ARM7.. You probably copied that code to the ARM9 sources.
_________________
http://licklick.wordpress.com

#114418 - clrFog - Sun Jan 07, 2007 8:11 pm

Oh, the simple sound is just one file though, simplesound.c. So that is a arm9 code?
_________________
DS lite white, M3 perfect lite micro SD, Sandisk 512MB

#114420 - Lick - Sun Jan 07, 2007 8:18 pm

Well, it's ARM9 alright. But you're not supposed to access SOUND_CR. The default ARM7 binary will handle it for you.
If you want your own initialization, you need to program the ARM7 as well. You can use the combined template to do this.
_________________
http://licklick.wordpress.com

#114421 - clrFog - Sun Jan 07, 2007 8:32 pm

Yes I tried the combined template and now my DS beeeeps!! =)
Thx for incredibly fast response, now im going to make my DIY syntheziser!
_________________
DS lite white, M3 perfect lite micro SD, Sandisk 512MB

#121430 - clrFog - Mon Mar 12, 2007 11:24 am

Mollusk has implemented acess to the PSG in PAlib.

http://palib.info/forum/modules/newbb/viewtopic.php?topic_id=1971&forum=30

Try it, its fun =)
_________________
DS lite white, M3 perfect lite micro SD, Sandisk 512MB

#139791 - lopazopy - Mon Sep 10, 2007 6:16 am

I've been playing with PSG sound and when I set the pan to 0 (all the way to the left) i still get sound out of the right speaker. I also checked it with headphones to make sure there was no sound contamination and I get the same thing. Anybody know why I can't get sound to play out of one speaker and one speaker only?

Also, how do I calculate perfect Hz frequencies? For example I want to play a sound at 300 Hz but the perfect frequency is something like 299.12 or 300.372 Hz. How do I find that out?

#139797 - Diddl - Mon Sep 10, 2007 8:44 am

is it possible to use more than one waveform (sinus, rectangle, sawtooth)?

is there only one oscillator or two per channel?

is it possible to make fm synthese?

#139811 - tepples - Mon Sep 10, 2007 12:25 pm

lopazopy wrote:
Anybody know why I can't get sound to play out of one speaker and one speaker only?

If you've tried it with good headphones, then it's probably leakage within the DS. I know of an experiment to confirm this, but it would require a GameCube, a Game Boy Player accessory, and a SLOT-2 card that fits in a GBA.

lopazopy wrote:
Also, how do I calculate perfect Hz frequencies? For example I want to play a sound at 300 Hz but the perfect frequency is something like 299.12 or 300.372 Hz. How do I find that out?

Differences in frequencies of less than 7 cents, where there are 1200 cents in an octave, should be inaudible to the human ear. What are you trying to do that needs a frequency better than +/- 0.4%?

Diddl wrote:
is it possible to use more than one waveform (sinus, rectangle, sawtooth)?

Rectangle is predefined for 1/8, 1/4, 3/8, and 1/2 duty cycle waveforms. For any other duty cycle, or for any other wave shape, you can always load a waveform into RAM and play it looped.

Diddl wrote:
is there only one oscillator or two per channel?

As far as I can tell, one.

Diddl wrote:
is it possible to make fm synthese?

Only in software, and the ARM7 hasn't been proven powerful enough to emulate the Yamaha FM chip in the Sega Genesis or AdLib chipset in real time.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#139828 - lopazopy - Mon Sep 10, 2007 4:59 pm

I fixed the sound leakage problem by decreasing the vol from 127 (max) to 10. It may still be there, but I can't hear it, so it isn't :)

tepples wrote:
What are you trying to do that needs a frequency better than +/- 0.4%?


I don't think I need frequencies that accurate, but when I was listening to a nonstop tone at 3000 Hz it sounded like it was clicking and didn't sound constant. I didn't know if it had to do with not picking a frequency that isn't a multiple of 32.768kHz or whatever of if that is just the way it is due to hardware. I guess I'd just rather have perfect frequencies and sound....why shoot for less?