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.

Audio > audio double buffer via dma

#146397 - bpoint - Mon Dec 03, 2007 5:02 pm

Hello all,

I've been going over a lot of the posts in these forums to get an idea of the best way to pipe my mixer's output to the GBA. Deku's tutorial was actually quite helpful (thanks Deku!).

It seems the most common methods of swapping buffers is to either set up a separate timer (wasteful?) or to latch onto the VBlank, since it's there already and is most likely already in use. However, I haven't seen any methods at all anywhere which drive the buffer swapping by having the DMA generate interrupts upon completion.

Is there some underlying reason why no one uses this method? I'm currently testing a few things out myself right now, but I'd like to know if anyone's tried this before.

#146449 - bpoint - Tue Dec 04, 2007 11:28 am

Well, after a bit of playing around, it seems the word count in the DMAxCNT_L register is ignored when DMA'ing to the sound FIFOs (gbatek mentions this too, even though it's easy to miss).

If DMA interrupts are enabled, an interrupt is generated after each 16 bytes are DMA'ed to the sound FIFOs (at least using the emulators I tested [VBA, no$gba, and AdvDbg] -- don't know about real hardware). I guess it would be possible to use the DMA interrupt as a counter, then swap buffers at the right time, but that's still a lot of interrupt overhead for just incrementing a counter.

...which is, I guess why no one uses this method. :)

#146451 - Lick - Tue Dec 04, 2007 12:18 pm

Thanks for this investigation! :)
_________________
http://licklick.wordpress.com

#146466 - eKid - Tue Dec 04, 2007 4:50 pm

indeed, each method has its flaws

-----------------------------------------------------
counting dma transfers:

pros:
accurate

cons:
tons of interrupts, huge overhead

------------------------------------------------
resetting with timer (timer can count up from sampling timer):

pros:
accurate

cons:
uses a timer

-------------------------------------------------
resetting at vblank

pros:
in sync with frames (can be done without interrupts)

cons:
inaccurate, playback rate must be tweaked to a certain rate for click free playback.

#146470 - kusma - Tue Dec 04, 2007 5:14 pm

I'd like to add the con for the timer solution: Unstable timing for the client-application.

The accuracy of the resetting-at-vblank can be solved by varying the size of the buffer used at every frame. The variation will be small, so it's not really a problem for buffer-sizes. Just calculate how many samples the hardware has played already with a sub-sample precision, and reset accordingly. It's all very predictable, so buffers can be filled correctly with the same logic. But to be honest, I'm personally not too worried about limiting the sample rates to those who are "click free".