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.

ASM > Direct Sound in ASM

#178085 - chickendude - Sat Sep 21, 2013 9:12 pm

I'm trying to get direct sound set up but it never seems to play anything. I've read a bunch of different tutorials online but can't piece it together. Nothing comes out of the speakers.

Right now here's a bit of C code i'm trying to replicate:
Code:
REG_SOUNDCNT_H = SOUNDA_VOLUME_100 | SOUNDA_LOUT | SOUNDA_ROUT | SOUNDA_FIFORESET;   // = 0x0604
REG_SOUNDCNT_X = SOUND_ENABLE;   // = 0x80
REG_TM0D = 65536 - (16777216 / soundFreq);
REG_TM0CNT = TIMER_ENABLE;   // = 0x80
REG_DM1SAD = (u32) soundDataAddr;
REG_DM1DAD = (u32) &(REG_SGFIFOA);   // **** Maybe i'm getting this wrong? I don't know what this code actually means in C :/
REG_DM1CNT_H = DMA_DEST_FIXED | DMA_REPEAT | DMA_WORD | DMA_MODE_FIFO | DMA_ENABLE;   // = 0xB640

Here's what i've got so far:
Code:
main:
@ Turn off sound channels 1-4
   ldr r0,=REG_SOUNDCNT_L
   mov r1,#0
@   str r1,[r0]

@ Enable DSA, full volume, using Timer 0
   ldr r0,=REG_SOUNDCNT_H
   ldr r1,=0x0604 @ SND_OUTPUT_RATIO_100|DSA_OUTPUT_RATIO_100|DSA_OUTPUT_TO_BOTH|DSA_TIMER0|DSA_FIFO_RESET
   str r1,[r0]

   ldr r0,=REG_SOUNDCNT_X
   ldr r1,=0x80 @SND_ENABLED
   str r1,[r0]

   ldr r0,=REG_TM0D
   ldr r1,=0xFFFF - 761   @ CPU pulls one byte every 761 cycles
   str r1,[r0]

   ldr r0,=REG_TM0CNT
   ldr r1,=0x80 @TIMER_ENABLE
   str r1,[r0]

   ldr r0,=REG_DMA1SAD
   ldr r1,=music
   str r1,[r0]

   ldr r0,=REG_DMA1DAD
   ldr r1,=REG_FIFO_A
   str r1,[r0]

   ldr r0,=REG_DMA1CNT
   ldr r1,=0xB640 @DMA_ENABLE|START_ON_FIFO_EMPTY|DMA_16|DMA_REPEAT|DMA_DEST_FIXED
   str r1,[r0]

here:
   bl here
I put some junk data at "music:" but it doesn't play anything :/

#178094 - headspin - Sun Sep 22, 2013 2:04 pm

Check out the sound demo here
_________________
Warhawk DS | Manic Miner: The Lost Levels | The Detective Game

#178098 - DekuTree64 - Mon Sep 23, 2013 9:34 pm

You need to use strh for the 16-bit registers.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#178099 - chickendude - Tue Sep 24, 2013 2:07 pm

Thanks for both your suggestions. I've gotten sound effects to work with the sound channels, but i couldn''t get direct sound to work. Also thanks for the reminder on the half-words, it's been a while since i've touched ARM assembly...

I figured out what the issue was, i was writing to REG_DMA1CNT (REG_DMA1CNT_L) instead of the high byte(s).

Now to figure out how to write actual music to put in there... :D

EDIT: Btw, for background music in a game, how is it generally handled? I assumed it was using direct sound but i'm not really sure. And how do you compose music for the GBA?

#178100 - Dwedit - Tue Sep 24, 2013 7:19 pm

If you want a very nice GBA music and sound library, try Maxmod. It's a mod player engine for the GBA/DS, which also lets you play additional sampled sound effects as well. It comes included with devkitarm. It's also very low on CPU usage, playing the famous "aryx.s3m" file uses under 25% CPU usage.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#178101 - chickendude - Tue Sep 24, 2013 7:48 pm

Thanks, that looks and sounds great, but for now i'd like to try my hand at writing my own routines. I see some example files that use it (as well as mmutil), but i don't see anywhere to check out the actual maxmod source.

The songs in the sample rom on the Maxmod site sound amazing, are they played using direct sound? And how are they written? Online i've read things talking about "instrument sets" and other things which make it sound a little like midi files, but i really don't know. I'll try reading through the information on belogic, but a lot of it is a bit confusing to me.

#178102 - DekuTree64 - Tue Sep 24, 2013 9:32 pm

Lots of ways to go. Some games use the GBC channels for music and direct sound for SFX. My current NES-ish style game uses one direct sound channel for music (4 channel .mod format with a sound mixer), and GBC channels and the other direct sound for SFX. Most games, I'd do an 8-channel stereo mixer, and use it for both music and SFX. All depends on how much CPU time you want to devote to it, and what sort of quality you need for the sound effect/music style of your game.

As for learning to write your own routines, there's always my old tutorial series if you haven't run across it already :) http://deku.rydia.net/program/sound1.html

I need to write another chapter on my latest mixer, to show off some assembly optimizing. But for now, you can just peruse the source if you want. The music player is all the same as the tutorial, aside from how it sets up the mixer channels being a bit different.
http://deku.rydia.net/temp/SoundMix.s
http://deku.rydia.net/temp/Sound.h

.s3m/.xm/.it formats can make better sounding music than .mod, but the player code is a lot more complex, particularly in the case of .it. Here's my current PC game sound engine, which uses .it format... haven't worked on it in several years so I don't remember exactly what state it's in at the moment. I'm pretty sure the music player is complete, with all effects and fiddly timing issues as accurate to WinAmp 2.95 as I can get them. http://deku.rydia.net/temp/PC_Sound.zip

For music composition, I use ModPlug Tracker. But first you'll have to find/make some .wav files to use as instruments. Here's my current chiptune single-cycle wave set:
http://deku.rydia.net/temp/Waves.mod
Still working on sound effect generation techniques...

As always, my code and sounds are free for anyone to use, no credit needed.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#178103 - chickendude - Tue Sep 24, 2013 10:51 pm

Thanks! I've been going through your tutorial and this one here:
http://archive.gamedev.net/archive/reference/programming/features/gbasound1/index.html

I'll try to look through it all more thoroughly tomorrow, in particular your SoundMix code (assembly code is much easier for me to read than C code).

I can't wait to get some music i've written myself going on my GBA :)

#178104 - Dwedit - Wed Sep 25, 2013 2:16 am

Maxmod source code is there with the rest of devkitpro:

http://sourceforge.net/p/devkitpro/maxmod/ci/master/tree/

And it's almost all in assembly.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#178105 - chickendude - Wed Sep 25, 2013 9:06 am

Wow, thanks! I guess it makes sense that the source wouldn't be there after compiling devkitPro/deleting the sources ;)