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 > Inner working of the DAC/sound bias ?

#174407 - Bregalad - Tue Jun 08, 2010 11:21 am

The DAC on the GBA can work for a various # of bits, and this at various sampling rates. According to belogic.com :

Quote:
00=9bit at 32768 Hz
01= 8bit at 65536 Hz
10=7bit at 131072 Hz
11= 6bit at 262144 Hz


Now, let's consider the following :
* Output for each GB channel can vary between 0 and 15 -> output for all of them vary between 0 and 60
* Output for each Direct Sound channel can very between -128 and 127

--> Total output can vary between -256 and 314, or if it is "offseted" into an unsigned range, form 0 and 570.
This implies the DAC should be at least 10 bit for no loss of quality. (because 9 bits can only handle 0-511).

Also I didn't take in account register 0x82, which allows to change global volume on Direct Sound and Gameboy channels. I'll assume it shifts the output of the corresponding channels right 1 or 2 times if the volume is reduced, dropping the low bit(s) which implies a loss of quality.

What am I missing ?

#174433 - Exophase - Thu Jun 10, 2010 10:31 pm

GBA outputs over PWM, not DAC, so the quality degradation is even worse than what you get from just removing the low bits. But you're right, some data bits have to be thrown out of the bottom before converting to PWM pulses, and some range is clipped. I would imagine that each channel individually is full range, so it's clipped at the same magnitude of a single direct sound channel or GB PSG channel.

The GB value is also probably output at a signed range like the direct sound value, scaled to the same range and mixed.

So somewhat roughly speaking, you should be able to take all audio inputs and normalize them to +/- 1.0. Then multiply them by volume values normalized to 0 to 1.0. Then add them together and clamp to +/- 1.0. Then multiply by the midpoint of the range (so for 8bit, 127.5) and add the bias value.

This is assuming what I said about range and clipping is correct, but this part should be fairly easily testable. You could also still be off by a little bit due to the non-centered nature of two's complement integer ranges. There may also be some roundoff if volume correct is applied multiple times and the internal precision of the mixing and affects everything.

#174441 - Bregalad - Fri Jun 11, 2010 7:40 pm

Mmmh this sounds obscure to me. The hardware can't do fractional multiplication - it just takes digital output of channels, shift/add them and output that to the PWM. Also I guess there is some kind of lowpass filter past the PWM (or does the speaker itself act as the filter ?).

If so, then I don't see the point to reduce the # of bits to increase the frequency of the PWM since it'll be filtered anyway (unless the filter cutoff is adaptive to the sound bias value, which I doubt).

So why does most games go for the 8-bit 65535 Hz bias ? I guess the best would be 9 bit 32768 Hz, and that 6 bit for GB(C) backwards compatibility.

In all cases I guess they suppose you to only use one direct sound channel per speaker. In that case - it'd be possible to have no quality loss with 9 bits.

Another thing I guess could affect quality is the frequency of the Direct Sound channel updates. For example, if the frequency is a direct divisor to the frequency of the PWM, could this result in better quality ?

I mean for example, if I have a sampling rate of 16384 Hz, and the PWM is set to 32768 Hz, it will take each direct sound input exactly 2 times, and it will sound as supposed.
But if I increase the sampling rate to, say 20 kHz, this would supposedly increase the quality (because higher sampling rate), but each sample will be used by the PWM either 1 or 2 times, and this could eventually increase the noise/distortion of the signal ?

#176959 - Bregalad - Mon Nov 07, 2011 11:17 pm

I decided to seriously test the behavior of this register.
I recorded (from a real DS in GBA mode) 4 samples from Final Fantasy V using all 4 different possible PWM configurations.

The samples are available in FLAC here.

'8' is for 32kHz 9-bit
'9' is for 65kHz 8-bit
'A' is for 131kHz 7-bit
'B' is for 262kHz 6-bit

Sample "direct" and "direct2" tests some sequenced music using direct sound channels.
Sample "direct3" test a streamed music using direct sound channels.
Sample "direct4" test a song using PSG channels.

My conclusions are that '9' and 'A' sounds better than '8' and 'B'.
In '8' there is some serious distortion going on and it sounds really nasty. In '9' there is less distortion, and in 'A' there is no distortion but it starts to sound quite noisy especially in quiet parts. In 'B' it definitely sounds very noisy.

This is for a sample rate of 13379Hz however. Maybe if it was something like exactly 16384Hz, it would sound better ?