Skip to content

Commit 1a6ca71

Browse files
ISSOtmavivace
andauthored
Overhaul audio section (#350)
Co-authored-by: Antonio Vivace <[email protected]>
1 parent 571dfb5 commit 1a6ca71

14 files changed

+1182
-431
lines changed

custom/style.css

+8
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

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Audio Overview
2+
3+
Game Boy audio is sometimes called "8-bit".
4+
This does not refer to the bit depth of the sound generated, but rather that it has sound capabilities typical of 8-bit consoles.
5+
Like much of its contemporary hardware, the Game Boy produces sound generated by simple digital circuits.
6+
7+
## Architecture
8+
9+
{{#include imgs/apu_overview.svg:2:}}
10+
11+
The Game Boy has four sound generation units, called **channels** 1 through 4, notated "CH1", "CH2", etc.
12+
Unlike some other sound chips, such as the C64's SID or the Atari 5200's POKEY, each sound channel is specialized in a way largely different from the other channels.
13+
14+
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].
15+
16+
Channels 1 and 2, the "pulse channels", produce [pulse width modulated waves](https://en.wikipedia.org/wiki/Pulse-width_modulation) with 4 fixed pulse width settings.
17+
Channel 3, the "wave" channel, produces arbitrary user-supplied waves.
18+
Channel 4 is the "[noise](https://en.wikipedia.org/wiki/Noise_in_music)" channel, producing a pseudo-random wave.
19+
20+
The VIN channel is an analog signal received directly from the cartridge, allowing external hardware to supply a fifth sound channel.
21+
No licensed games used this feature, and it was omitted from the Game Boy Advance.
22+
23+
::: tip POCKET MUSIC
24+
25+
Despite some online claims, *Pocket Music* does not use VIN.
26+
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.
27+
28+
:::
29+
30+
[^speaker_mono]:
31+
The speaker merges back the two channels, losing the stereo aspect entirely.
32+
33+
## Common concepts
34+
35+
### APU
36+
37+
The Game Boy's sound chip is called the <abbr title="Audio Processing Unit">APU</abbr>.
38+
39+
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>).
40+
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.
41+
The SGB2 rectifies this issue.
42+
43+
All interfaces to the APU use **durations** instead of frequencies, which may be confusing as signal theory and music are more typically based on the latter.
44+
Thus, durations will be expressed from their frequencies: for example, a "256 Hz tick" means "1 ∕ 256th of a second".
45+
46+
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.
47+
48+
::: warning
49+
50+
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.
51+
Readers wishing to learn more should read the [APU details](<#Audio Details>) chapter.
52+
53+
:::
54+
55+
### Triggering
56+
57+
**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].
58+
Most changes to a channel's parameters take effect immediately, but some require re-triggering the channel.
59+
60+
### Volume & envelope
61+
62+
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).
63+
64+
Additionally, an [**envelope**](https://en.wikipedia.org/wiki/Envelope_(music)) can be configured for CH1, CH2 and CH4, which allows automatically adjusting the volume over time.
65+
The parameters that can be controlled are the initial volume, the envelope's direction (but not its slope), and its duration.
66+
Internally, all envelopes are ticked at 64 Hz, and every 1–7 of those ticks, the volume will be increased or decreased.
67+
68+
### Length timer
69+
70+
All channels can be individually set to automatically shut themselves down after a certain amount of time.
71+
72+
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.
73+
When the length timer reaches 64, the channel is turned off.
74+
75+
### Frequency
76+
77+
Music notes and audio waves are typically manipulated in terms of **frequency**[^pitch], i.e. how often the signal repeats per second.
78+
However, as explained above, the Game Boy APU primarily works with durations; thus, **wavelengths** will be used instead of frequency.[^len_raw]
79+
80+
::: warning
81+
82+
The term "wavelength" throughout this document does *not* designate the inverse of the frequency, but instead a quantity akin to it.
83+
See the description of each NRx3 register for more information.
84+
85+
:::
86+
87+
---
88+
89+
[^trig_dac_off]:
90+
If [the channel's DAC](#DACs) is off, the channel will not turn on.
91+
92+
[^pulse_restart]:
93+
Except for pulse channels, whose phase position is only ever reset by turning the APU off.
94+
This is usually not noticeable unless changing the duty cycle mid-note.
95+
96+
[^vol_knob]:
97+
This is separate from the physical volume knob located on the side of the console.
98+
99+
[^len_cnt_dir]:
100+
Internally, the length timer is inverted when written, and *that* ticks down until it reaches 0.
101+
But the effect is as if the counter ticked up.
102+
103+
[^pitch]:
104+
There is also **pitch**, which is merely a measure of how we perceive frequency.
105+
The higher the frequency, the higher the pitch; therefore, pitch will be omitted from the rest of the document.
106+
107+
[^len_raw]:
108+
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)