Skip to content

Commit 6180889

Browse files
committed
[WIP] Overhaul audio section
1 parent ae89342 commit 6180889

File tree

13 files changed

+1132
-431
lines changed

13 files changed

+1132
-431
lines changed

custom/style.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,11 @@ code {
142142
.rust {
143143
--c-warning-text: #ad8e00;
144144
}
145+
146+
/* Avoid text being squished too much in fractions */
147+
math[display="block"], mfrac {
148+
font-size: 2rem;
149+
}
150+
mfrac msup :not(:first-child) {
151+
font-size: 1.8rem;
152+
}

src/Audio.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Audio Overview
2+
3+
Game Boy audio is sometimes called "8-bit", but this doesn't mean much: the term is used to designate audio similar to that produced by 8-bit consoles.
4+
5+
A better description would be "chiptune": tunes produced by a chip.
6+
This section describes how audio is generated on the Game Boy.
7+
8+
## Architecture
9+
10+
{{#include imgs/apu_overview.svg:2:}}
11+
12+
The Game Boy has four sound generation units, called **channels** 1 through 4, notated "CH1", "CH2", etc.
13+
Unlike some other sound chips, such as the SNES' S-DSP or the Atari 5200's POKEY, each sound channel is specialized in a way largely different from the other channels.
14+
15+
Each channel generates an electronic signal; these signals are then mixed into two new channels (for stereo: one for the left ear, one for the right ear), which are then individually amplified, and then output either to the headphone jack, or the speaker[^speaker_mono].
16+
17+
Channels 1 and 2, the "pulse channels", produce [pulse (rectangular) waves](https://en.wikipedia.org/wiki/Pulse_wave).
18+
Channel 3, the "wave" channel, produces arbitrary user-supplied waves.
19+
Channel 4 is the "[noise](https://en.wikipedia.org/wiki/Noise_in_music)" channel, producing a pseudo-random wave.
20+
21+
The VIN channel is a signal received directly from the cartridge, allowing external hardware to supply a fifth sound channel.
22+
No licensed games used this feature, and it was omitted from the Game Boy Advance.
23+
24+
::: tip POCKET MUSIC
25+
26+
Despite some online claims, *Pocket Music* does not use VIN.
27+
It refuses to run on the GBA for a different reason: the developer couldn't figure out how to silence buzzing associated with sample playback on the wave channel.
28+
29+
:::
30+
31+
[^speaker_mono]:
32+
The speaker merges back the two channels, losing the stereo aspect entirely.
33+
34+
## Common concepts
35+
36+
### APU
37+
38+
The Game Boy's sound chip is called the <abbr title="Audio Processing Unit">APU</abbr>.
39+
40+
The APU runs off the same master clock as the rest of the Game Boy, which is to say, it is fully synced with the CPU and [PPU](<#Rendering Overview>).
41+
This also means that the APU runs about 2.4% faster on the SGB1, increasing frequencies by as much and thus sounding slightly higher-pitched.
42+
The SGB2 rectifies this issue.
43+
44+
The APU processes a lot of events at a fixed rate dependent on the event's type.
45+
Thus, the APU interfaces use **durations** instead of frequencies, which may be confusing as signal theory and music are more typically based on the latter.
46+
Thus, durations will be expressed from their frequencies: for example, a "256 Hz tick" means "1 ∕ 256th of a second".
47+
48+
The length of APU ticks is not affected by [CGB double speed](<#FF4D — KEY1 (CGB Mode only): Prepare speed switch>), so the APU works just the same regardless of CPU speed.
49+
50+
::: warning
51+
52+
The Game Boy's APU is actually full of tricky details; this chapter will mostly describe the intended / common behavior, and often paper over bugs & quirks.
53+
Readers wishing to learn more should read the [APU details](<#Audio details>) chapter.
54+
55+
:::
56+
57+
### Triggering
58+
59+
**Triggering** a channel causes it to turn on if it wasn't[^trig_dac_off], and to start playing its wave from the beginning[^pulse_restart].
60+
Most changes to a channel's parameters take effect immediately, but some require re-triggering the channel.
61+
62+
### Volume & envelope
63+
64+
The volume can be controlled in two ways: there is a "master volume" control[^vol_knob] (which has separate settings for the left and right outputs), and each channel's volume can be individually set as well (CH3's less precisely than the others).
65+
66+
Additionally, an [**envelope**](https://en.wikipedia.org/wiki/Envelope_(music)) can be configured for all channels except CH3, which allows automatically adjusting the volume over time.
67+
The parameters that can be controlled are how many times the envelope will be stepped, in which direction, and how long between steps.
68+
All envelopes are ticked at 64 Hz, and every 1–7 of those ticks, the volume will be increased or decreased.
69+
70+
### Length timer
71+
72+
All channels can be individually set to automatically shut themselves down after a certain amount of time.
73+
74+
If the functionality is enabled, a channel's **length timer** ticks up[^len_cnt_dir] at 256 Hz (tied to [DIV-APU](<#DIV-APU>)) from the value it's initially set at.
75+
When the length timer reaches 64, the channel is turned off.
76+
77+
### Frequency
78+
79+
Music notes and audio waves are typically manipulated in terms of **frequency**[^pitch], i.e. how often the signal repeats per second.
80+
However, as explained above, the Game Boy APU primarily works with durations; thus, **wavelengths** will be used instead of frequency.[^len_raw]
81+
82+
::: warning
83+
84+
The term "wavelength" throughout this document does *not* designate the inverse of the frequency, but instead a quantity akin to it.
85+
See the description of each NRx3 register for more information.
86+
87+
:::
88+
89+
---
90+
91+
[^trig_dac_off]:
92+
If the channel's DAC is off (TODO: link to explanation), the channel will not turn on.
93+
94+
[^pulse_restart]:
95+
Except for pulse channels, whose index into their waves is only ever reset by turning the APU off.
96+
This is not noticeable when playing sound, and only matters for "advanced" APU usage.
97+
98+
[^vol_knob]:
99+
This is separate from the physical volume knob located on the side of the console.
100+
101+
[^len_cnt_dir]:
102+
Internally, the length timer is inverted when written, and *that* ticks down until it reaches 0.
103+
But the effect is as if the counter ticked up.
104+
105+
[^pitch]:
106+
There is also **pitch**, which is merely a measure of how we perceive frequency.
107+
The higher the frequency, the higher the pitch; therefore, pitch will be omitted from the rest of the document.
108+
109+
[^len_raw]:
110+
Actually, the APU interfaces don't work with any wavelengths either, but with values that are more akin to wavelengths than frequencies.

0 commit comments

Comments
 (0)