Sound 4 (Noise): Highest frequencies?

Discussion of programming and development for the original Game Boy and Game Boy Color.
Shonumi
Posts: 321
Joined: Sun Jan 26, 2014 9:31 am

Sound 4 (Noise): Highest frequencies?

Post by Shonumi » Sun Jan 26, 2014 10:13 am

For some reason, I'm finding the GB's Noise Channel (Sound 4) the most troublesome to fully understand. I'm trying to wrap my head around how it works, but I still have a couple of major questions.

1) Is the highest frequency for Sound 4's programmable frequency timer really 524288Hz? For example, assuming I set NR43 to 0x00 (using a 15 stage LSFR for simplicity), am I correct in saying that this will produce a frequency timer of 524288Hz?

The documents I've read say that the Sound 4's period is decided by the Dividing Ratio (Bits 0 - 2 of NR43) shifted some number of bits to left: (http://gbdev.gg8.se/wiki/articles/Gameb ... se_Channel). This should mean than when NR43 = 0x00, Sound 4's period is 8 clocks, meaning the frequency is 524288Hz.

Just to be sure, this formula decides how often the APU should generate a waveform, right?


2) Is the highest frequency of the LSFR counter also 524288Hz? If not what is the correct formula for determining this?


3) If the maximum frequency timer is 524288Hz, how do emulators reasonably recreate the noise effect using only 44100Hz? For example, I wrote a small test ROM to continuously output sound on Sound 4 with NR43 set to 0x00, and I tested it both on VBA-M as well as my old Game Boy Color. As far as my ears could tell, they were both very similar if not identical for all intents and purposes. This is the most frustrating part I'm trying to understand. I've included the test ROM as an attachment for reference (should play on any emulator and real hardware).


Sorry if this has been discussed before, but I've read through a couple of documents already (GBSOUND.txt, game_sound_emulation_by_blargg.txt, the GBDev Wiki, even the docs from belogic.com about GBA's sound) but I'm still not sure I'm getting the whole frequency part right. Any help is greatly appreciated.
Attachments
NOI_Normal.gb
(32 KiB) Downloaded 204 times

tepples
Posts: 22141
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Sound 4 (Noise): Highest frequencies?

Post by tepples » Sun Jan 26, 2014 10:16 am

Shonumi wrote:3) If the maximum frequency timer is 524288Hz, how do emulators reasonably recreate the noise effect using only 44100Hz?
The same way you display a 6000-pixel wide photo on a 1920-pixel-wide monitor: blur it and decimate it. Emulators use some sort of low-pass filter or band-limited synthesis method to average out samples and remove frequencies too high to hear.

Shonumi
Posts: 321
Joined: Sun Jan 26, 2014 9:31 am

Re: Sound 4 (Noise): Highest frequencies?

Post by Shonumi » Sun Jan 26, 2014 10:24 am

Thanks tepples. See, I've dealt with images and pixels for years, but I literally just started getting to grips with sound programming months ago (no joke, had no idea what frequencies and periods really meant until October :p). I'll definitely have a look at both methods.

User avatar
Jarhmander
Formerly ~J-@D!~
Posts: 510
Joined: Sun Mar 12, 2006 12:36 am
Location: Rive nord de Montréal

Re: Sound 4 (Noise): Highest frequencies?

Post by Jarhmander » Sun Jan 26, 2014 2:02 pm

Tip: look for Sample Rate Conversion on the Interwebz.
((λ (x) (x x)) (λ (x) (x x)))

Shonumi
Posts: 321
Joined: Sun Jan 26, 2014 9:31 am

Re: Sound 4 (Noise): Highest frequencies?

Post by Shonumi » Wed Feb 12, 2014 11:15 pm

Thanks for all of the replies everyone, I really appreciate your advice and taking the time to help me.

I'm still very confused about how the Game Boy Sound 4 actually determines the noise channel output frequency and the frequency at which it runs the LSFR as a Psuedo-Random Number Generator. I'm actually at my wits ends trying to figure this out. I'm primarily going off of http://www.devrs.com/gb/files/hosted/GBSOUND.txt, but honestly it's as clear as mud to me.

Basically, the Game Boy uses NR43 (0xFF22) to control how fast to run the noise channel and the LSFR. It uses these bits to determine something called the Dividing Ratio and a Shift Clock frequency. What I need to know is:

1) Using NR43 (0xFF22) what is the formula that determines Sound 4's output frequency?

2) Using NR43 (0xFF22) what is the formula that determines Sound 4's LSFR frequency (how many times per-second to run it's psuedo-random number generator)?

GBSOUND.txt claims that Sound 4 can output frequencies from 2Hz to 1048576Hz, but this isn't very clearly stated how one arrives at those numbers. I've noted the formulas present in that document, but every time I try to do the math myself, it never seems to work out. I just can't figure out any of it. Perhaps I'm just dense, in which case I'd be eternally grateful if someone could step through the math in a clear and exact manner (one example is fine).

Drag
Posts: 1327
Joined: Mon Sep 27, 2004 2:57 pm
Contact:

Re: Sound 4 (Noise): Highest frequencies?

Post by Drag » Thu Feb 13, 2014 11:13 am

According to pandocs,
FF22 - NR43 - Channel 4 Polynomial Counter (R/W)
[...]
Bit 7-4 - Shift Clock Frequency (s)
Bit 3 - Counter Step/Width (0=15 bits, 1=7 bits)
Bit 2-0 - Dividing Ratio of Frequencies (r)

Frequency = 524288 Hz / r / 2^(s+1) ;For r=0 assume r=0.5 instead
This one equation should answer both of your questions.

Shonumi
Posts: 321
Joined: Sun Jan 26, 2014 9:31 am

Re: Sound 4 (Noise): Highest frequencies?

Post by Shonumi » Thu Feb 13, 2014 7:30 pm

Drag wrote:According to pandocs,
FF22 - NR43 - Channel 4 Polynomial Counter (R/W)
[...]
Bit 7-4 - Shift Clock Frequency (s)
Bit 3 - Counter Step/Width (0=15 bits, 1=7 bits)
Bit 2-0 - Dividing Ratio of Frequencies (r)

Frequency = 524288 Hz / r / 2^(s+1) ;For r=0 assume r=0.5 instead
This one equation should answer both of your questions.
Thanks man, I'm a complete idiot. For some reason I can't count, and I kept taking only the top three bits of NR43 instead of the top nibble. A week's worth of frustration because I kept shifting and masking by 0x7 not 0xF :(

Anyway, many thanks to everyone, you've all been great :)

Wolfattackx
Posts: 6
Joined: Mon Sep 07, 2020 12:05 pm

Re: Sound 4 (Noise): Highest frequencies?

Post by Wolfattackx » Mon Sep 07, 2020 12:08 pm

Hello I am also recently making a Gameboy emulator and also stuck at the same position the pandocs links seems to be broken. Can someone help me

Shonumi
Posts: 321
Joined: Sun Jan 26, 2014 9:31 am

Re: Sound 4 (Noise): Highest frequencies?

Post by Shonumi » Mon Sep 07, 2020 1:12 pm

A newer version of PanDocs is being constructed at the moment. You can find it here: https://gbdev.io/pandocs/

For the sound controller part specifically, you can see it here: https://gbdev.io/pandocs/#sound-controller

There is still some older documentation left on the GBdev wiki page as well, but it should have the same information as the above link. I'll post it here just in case you need it: https://gbdev.gg8.se/wiki/articles/Sound_Controller

As for helping you emulate Game Boy sound, do you have any specific questions or specific problems we can help you with?

Wolfattackx
Posts: 6
Joined: Mon Sep 07, 2020 12:05 pm

Re: Sound 4 (Noise): Highest frequencies?

Post by Wolfattackx » Mon Sep 07, 2020 1:36 pm

I am calculating the Freq By the given formula 524288 Hz / r / 2^(s+1) this one and use it like this

Code: Select all

if (InterTimer % NoiseF) == 0 then TickLFSR
But the Noise Sound is not coming right.

Wolfattackx
Posts: 6
Joined: Mon Sep 07, 2020 12:05 pm

Re: Sound 4 (Noise): Highest frequencies?

Post by Wolfattackx » Mon Sep 07, 2020 1:45 pm

Btw InternalTimer is a uint16_t So In my audio tick I am incrementing it by 1 so 256hz should be (InterTimer % (4194304 / 256) == 0) and 64hz is when the InternalTimer becomes zero

lidnariq
Posts: 9839
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: Sound 4 (Noise): Highest frequencies?

Post by lidnariq » Mon Sep 07, 2020 5:21 pm

What pitch noise are you testing with? How are you generating sound to output?

Wolfattackx
Posts: 6
Joined: Mon Sep 07, 2020 12:05 pm

Re: Sound 4 (Noise): Highest frequencies?

Post by Wolfattackx » Tue Sep 08, 2020 4:12 am

TBH this is my first time making a apu. I don't know much about sound. All the channels expect noise is more or less fine

Code: Select all

void Apu::AudioCallBack(void * UserData, uint8_t * Buffer8Bit, int bytes)
{
	int &SampleIndex = *(int*) UserData;
	uint16_t* Buffer16Bit = (uint16_t*) Buffer8Bit; // Making it a 16 bit buffer

	for (int i = 0; i < bytes / 2; i++, SampleIndex++)
	{
		uint16_t pulse1 = 0, pulse2 = 0, wave = 0, noise = 0;
		double time = fmod((double)SampleIndex / SampleRate, 1); // Sample rate is 44100hz. so in 1 sec which index I am at is the time.
 
		if (Pulse1V > 0)
		{
			pulse1 = PulseWave(time, Pulse1F, Pulse1d) * 4000 * Pulse1V;
		}

		if (Pulse2V > 0)
			pulse2 = PulseWave(time, Pulse2F, Pulse2d) * 4000 * Pulse2V;

		if (WaveFreq > 0)
			wave = (WaveRam[int(fmod(time , 1.0 / WaveFreq) / (1.0 / WaveFreq) * 32)] >> WaveShift) << 7; 

		if (NoiseV > 0)
			noise = (NoiseLevel & 0x1) * 4000 * NoiseV;

		Buffer16Bit[i] = pulse1 + pulse2 + wave + noise;
	}
}


lidnariq
Posts: 9839
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: Sound 4 (Noise): Highest frequencies?

Post by lidnariq » Tue Sep 08, 2020 11:02 am

Many of the sounds that the game boy's noise channel contain frequency components that are substantially higher than your chosen sample rate, and the way you seem to be generating audio will produce "aliases" of these higher frequency components. The highest three octaves (s = 0 to 2) will sound wrong ... so hence my asking. See if you can test with other noise sounds.

Wolfattackx
Posts: 6
Joined: Mon Sep 07, 2020 12:05 pm

Re: Sound 4 (Noise): Highest frequencies?

Post by Wolfattackx » Tue Sep 08, 2020 11:46 am

I have tested it on many games but the one with the most problematic noise sound is the pokemon specially in the title screen. Other than that other games sound kinda ok

Post Reply