Alchemy
Loading...
Searching...
No Matches

sound.c

Go to the documentation of this file.
1#include "unit/sound/sound.h"
3#include "battle.h"
4#include "keystate.h"
5#include "debug.h"
6#include "field.h"
7#include "game.h"
8#include "graphics.h"
9#include "menu.h"
10#include "minigame.h"
11#include "sound.h"
12#include "system.h"
13#include "audio_engine.h"
14#include "audio_engine_types.h"
16#include "types.h"
17#include "io_reg.h"
18#include "songs.h"
19
20enum {
27};
28
29/// Dispatches a packed cue: fade controls, allocated sound effects, fanfares,
30/// or background music with an optional silent start. Reached through the
31/// sound far-call table as AudioCommand_PlayFar, which the whole game calls to
32/// play music and sound effects; it updates the music layer's cue records.
34 s32 flags;
35 s32 player;
36
37 flags = id & AUDIO_CUE_FLAGS_MASK;
39
40 if (id == AUDIO_CUE_FADE_BGM) {
41 if (gMusicFadeRequested != 0)
42 return;
46 } else if (id == AUDIO_CUE_FADE_SLOT3) {
49 } else if (id >= AUDIO_SONG_FIRST) {
50 // Slot 7 borrows a free effect player, searching backwards through 4..7.
51 player = gSound_SongTable[id].slot;
52 if (player == 7) {
53 next:
54 if (((u8 *)&gSound_PlayerSlots[player].player->status)[0] != 0) {
55 player--;
56 if (player > 3)
57 goto next;
58 player = 7;
59 }
60 }
62 gSound_SongTable[id].header);
63 gMusicPlayerVolumes[player] = id;
64 } else if (id >= AUDIO_JINGLE_FIRST) {
67 gMusicVolume = 0;
70 } else {
71 if (id == AUDIO_CUE_KEEP_BGM)
72 return;
73 if (id == gAudioSecondaryState)
74 return;
77 (id == MUS_070 || id == MUS_075 || id == MUS_067) ? 3 : 2);
79 if (flags & AUDIO_CUE_START_SILENT)
80 gMusicVolume = 0;
81 else
86 }
87}
88
89/// Restores BGM after a fanfare and steps its volume and combined tempo/pitch
90/// ramps once per VBlank, clamping each ramp when it crosses its target.
91/// Called by System_VBlankHandler through the sound far-call table.
93 s32 delta;
94
95 if (gMusicRestoreDelay != 0) {
96 if (gMusicRestoreDelay == 1) {
97 if ((u8)gMusicPlayerFanfare.status == 0) {
100 }
101 } else {
103 }
104 }
107 if (delta > 0) {
109 } else {
111 }
112 if ((((s16)gMusicVolumeTarget - (s16)gMusicVolume) ^ delta) < 0) {
114 }
116 }
119 if (delta > 0) {
121 } else {
123 }
124 if ((((s16)gMusicPitchTarget - (s16)gMusicPitch) ^ delta) < 0) {
126 }
129 (s16)gMusicPitch * 12 - 12 * MUSIC_UNITY_SCALE);
130 }
132}
133
134/// Runs the audio test's three cue selectors and five mixer presets forever.
135/// Buttons play, fade or adjust the selected cue while frames keep advancing.
136/// Reached only through the sound far-call table's veneer; nothing in ☀️
137/// names it.
139 s32 cues[3] = {0, 80, 100};
140 s32 preset;
141 s32 row;
142 s32 timer;
143
144 row = 0;
145 preset = 2;
146 /* FAKEMATCH(one-pass): the block keeps the state-address load after preset
147 * setup and the state store before timer setup. Plain store: differ 8 or more
148 * in nine statement orders. */
149 // XXX: this overwrites the first four digits of the build stamp copy; why
150 // the audio test clears them is not known.
151 do {
152 gBuildStamp[0] = 0;
153 } while (0);
154 timer = 20;
155
156 for (;;) {
157 if (timer != 0)
158 timer--;
159
160 if (gSoundClipFlag != 0) {
161 gSoundClipFlag = 0;
162 timer = 20;
163 }
164
165 if (gKeysRepeat & KEY_SELECT) {
166 preset = (preset + 1) % 5;
168 }
169 if (gKeysRepeat & KEY_R)
170 cues[row] += 10;
171 if (gKeysRepeat & KEY_L)
172 cues[row] -= 10;
174 cues[row] += 1;
175 if (gKeysRepeat & KEY_LEFT)
176 cues[row] -= 1;
177 if (gKeysRepeat & KEY_UP) {
178 if (row > 0)
179 row--;
180 }
181 if (gKeysRepeat & KEY_DOWN) {
182 if (row <= 1)
183 row++;
184 }
185 if (gKeysRepeat & KEY_A)
186 AudioCommand_Play(cues[row]);
187 if (gKeysRepeat & KEY_B)
193
194 WaitFrames(1);
195 }
196}
197
198/// Starts the sound driver, resets the BGM ramps (gMusicVolume and its kin)
199/// and clears all player cue records. Called by System_Initialize through the
200/// sound far-call table.
202 s16 *playerVolume;
203 s32 remaining;
204
216 playerVolume = gMusicPlayerVolumes;
217 gEwram_Data1 = 0;
218 remaining = MUSIC_PLAYER_COUNT - 1;
219 do {
220 remaining--;
221 *playerVolume = 0;
222 playerVolume += 1;
223 } while (remaining >= 0);
224}
225
226/// Sets the BGM tempo scale directly; 0x100 preserves its native tempo. Called
227/// through the far-call table, by the Lemurian ship's sailing among others.
230}
231
232/// Applies a pitch offset to every active BGM track. Reached only through the
233/// sound far-call table's veneer.
236}
237
238/// Sets the target and per-frame step of the combined BGM tempo/pitch ramp.
239/// Reached only through the sound far-call table's veneer.
240void Audio_SetWorkPairB(u16 targetScale, u16 step) {
241 gMusicPitchTarget = targetScale;
242 gMusicPitchStep = step;
243}
244
245/// Sets BGM volume immediately and cancels any outstanding volume ramp.
246/// Reached only through the sound far-call table's veneer.
249 gMusicVolumeTarget = volume;
250 gMusicVolume = volume;
251}
252
253/// Sets the BGM volume target and its per-frame adjustment. Reached only
254/// through the sound far-call table's veneer.
255void Audio_SetMusicVolumeTargetAndStep(u16 targetVolume, u16 step) {
256 gMusicVolumeTarget = targetVolume;
257 gMusicVolumeStep = step;
258}
259
260/// Returns the remaining delay before BGM can be restored after a fanfare.
262
263/// Stops every sound player while preserving its state for a later resume.
265
267
268/// Replaces the audio command mask, or toggles its requested bits when
269/// flagged. Reached only through the sound far-call table's veneer.
271 u32 toggle = command & AUDIO_COMMAND_TOGGLE;
272
273 command &= AUDIO_COMMAND_MASK;
274 if (toggle != 0)
275 gAudio_CommandMask ^= command;
276 else
277 gAudio_CommandMask = command;
278}
279
280/// Returns the last BGM cue or fade state selected by AudioCommand_Play.
282
283/// Waits a frame at a time for fanfare restoration, giving up after 300
284/// frames. Called by scenes before they go on after a fanfare (Sol Sanctum's
285/// quest acceptance among them).
287 s32 framesWaited = 0;
288
289 do {
290 if (gMusicRestoreDelay == 0)
291 break;
292 WaitFrames(1);
293 framesWaited++;
294 } while (framesWaited < MUSIC_WAIT_TIMEOUT_FRAMES);
295}
296
297/// Returns the mixer preset chosen by the cue dispatcher. Nothing in ☀️ calls
298/// it.
300 if (command == MUS_070 || command == MUS_075 || command == MUS_067)
301 return 3;
302 return 2;
303}
UNUSED void Audio_ResumeAllPlayers(void)
Resumes every player. Nothing in ☀️ calls it.
Definition audio.c:219
void Audio_PlaySound(u16 songId)
Starts the song on the player selected by its song-table entry.
Definition audio.c:142
void Audio_Initialize(void)
Copies the RAM mixer to IWRAM, initializes the PCM and CGB paths and configures every player from gSo...
Definition audio.c:118
const struct PlayerSlot gSound_PlayerSlots[]
void MusicPlayer_StartSong(struct SoundPlayer *player, const struct SequenceHeader *header)
Starts a song when its priority permits, resetting its tracks and tempo.
Definition engine.c:282
UNUSED void Audio_StopAllPlayers(void)
Stops every player. Nothing in ☀️ calls it.
Definition audio.c:206
void MusicPlayer_FadeOut(struct SoundPlayer *player, u16 speed)
Begins the standard stop-and-clear fade.
Definition audio.c:228
const struct SongEntry gSound_SongTable[]
Declares nothing of its own: it only includes audio_engine_symbols.h and sound/audio_engine_types....
The sound driver's command masks, cue controls and the routines and globals it shares with the engine...
u8 gAudio_CommandMask
u16 gMusicPitch
u8 gAudioSecondaryState
u16 gMusicPitchTarget
u16 gMusicVolume
u16 gMusicVolumeStep
struct SoundPlayer gSound_Player3
u8 gMusicRestoreDelay
@ AUDIO_COMMAND_TOGGLE
@ AUDIO_COMMAND_MASK
struct SoundPlayer gMusicPlayerFanfare
struct SoundPlayer gMusicPlayerBgm
u16 gMusicVolumeTarget
u16 gMusicPitchStep
u16 gMusicPlayerVolumes[8]
@ AUDIO_SONG_FIRST
@ AUDIO_CUE_FLAGS_MASK
@ AUDIO_CUE_ID_MASK
@ AUDIO_CUE_FADE_SLOT3
@ AUDIO_CUE_KEEP_BGM
@ AUDIO_CUE_FADE_BGM
@ AUDIO_JINGLE_FIRST
@ AUDIO_CUE_START_SILENT
The sound driver's types, registers and work.
The engine calls the debug menus import, and their palette swatch tiles.
void MusicPlayer_SetPitch(struct SoundPlayer *player, u16 mask, s16 pitch)
Sets the semitone and fractional pitch offsets of selected active tracks and marks their pitch for re...
Definition engine.c:828
void MusicPlayer_SetPitchAndUpdateFrequency(struct SoundPlayer *player, u16 scale)
Sets the tempo multiplier and updates the number of ticks advanced per frame.
Definition engine.c:787
void MusicPlayer_SetVolume(struct SoundPlayer *player, u16 mask, u16 volume)
Sets the volume scale of active tracks selected by the bit mask and marks their output for recalculat...
Definition engine.c:800
s32 Sound_LoadPresetParameters(s32 preset)
Copies the mixer code for sound preset preset (0 to 4; anything else is 0) from ROM into IwramSoundPr...
Definition frame.c:439
void WaitFrames(s32 frames)
The game's frame loop: runs frames frames and returns.
Definition frame.c:97
The screen blend's globals and the graphics routines.
The hardware's I/O registers and their bits, as pret names them.
@ KEY_UP
Definition io_reg.h:249
@ KEY_B
Definition io_reg.h:244
@ KEY_SELECT
Definition io_reg.h:245
@ KEY_LEFT
Definition io_reg.h:248
@ KEY_START
Definition io_reg.h:246
@ KEY_RIGHT
Definition io_reg.h:247
@ KEY_R
Definition io_reg.h:251
@ KEY_A
Definition io_reg.h:243
@ KEY_DOWN
Definition io_reg.h:250
@ KEY_L
Definition io_reg.h:252
The keypad state the VBlank interrupt keeps.
volatile u32 gKeysRepeat
Pressed keys, repeating while held: after 19 frames, then every 6.
The reel game's title lettering.
Song and sound effect ids, indices into gSound_SongTable.
#define MUS_075
Definition songs.h:77
#define MUS_019
Definition songs.h:29
#define MUS_067
Definition songs.h:70
#define MUS_070
Definition songs.h:72
u8 gMusicFadeRequested
s32 gSoundClipFlag
void Sound_TickDmaRestartTimer(void)
s32 gBuildStamp[]
The build stamp System_CopyBuildStamp copies to IWRAM at power-on: twelve digits and a zero word,...
u8 gEwram_Data1
@ MUSIC_DEFAULT_RAMP_STEP
Definition sound.c:22
@ MUSIC_WAIT_TIMEOUT_FRAMES
Definition sound.c:26
@ MUSIC_UNITY_SCALE
Definition sound.c:21
@ MUSIC_PLAYER_COUNT
Definition sound.c:24
@ MUSIC_ALL_TRACKS
Definition sound.c:23
@ MUSIC_RESTORE_DELAY_FRAMES
Definition sound.c:25
UNUSED s32 AudioCommand_GetWidth(s32 command)
Returns the mixer preset chosen by the cue dispatcher.
Definition sound.c:299
void MusicCommand_SetPitchAndUpdateFrequency(u16 tempoScale)
Sets the BGM tempo scale directly; 0x100 preserves its native tempo.
Definition sound.c:228
void MusicCommand_SetPitch(u16 pitch)
Applies a pitch offset to every active BGM track.
Definition sound.c:234
s32 AudioCommand_GetStateByte(void)
Returns the remaining delay before BGM can be restored after a fanfare.
Definition sound.c:261
void AudioCommand_Play(s32 id)
Dispatches a packed cue: fade controls, allocated sound effects, fanfares, or background music with a...
Definition sound.c:33
void Audio_InitializeRuntimeDefaults(void)
Starts the sound driver, resets the BGM ramps (gMusicVolume and its kin) and clears all player cue re...
Definition sound.c:201
s32 AudioCommand_GetSecondaryStateByte(void)
Returns the last BGM cue or fade state selected by AudioCommand_Play.
Definition sound.c:281
void AudioCommand_WaitForCompletion(void)
Waits a frame at a time for fanfare restoration, giving up after 300 frames.
Definition sound.c:286
void AudioCommand_ResumeAllPlayers(void)
Definition sound.c:266
void Audio_SetMusicVolumeTargetAndStep(u16 targetVolume, u16 step)
Sets the BGM volume target and its per-frame adjustment.
Definition sound.c:255
void MusicCommand_SetVolume(s16 volume)
Sets BGM volume immediately and cancels any outstanding volume ramp.
Definition sound.c:247
void AudioTest_RunParameterController(void)
Runs the audio test's three cue selectors and five mixer presets forever.
Definition sound.c:138
void Audio_SetWorkPairB(u16 targetScale, u16 step)
Sets the target and per-frame step of the combined BGM tempo/pitch ramp.
Definition sound.c:240
void AudioCommand_StopAllPlayers(void)
Stops every sound player while preserving its state for a later resume.
Definition sound.c:264
void AudioCommand_UpdateToggleMask(u32 command)
Replaces the audio command mask, or toggles its requested bits when flagged.
Definition sound.c:270
void MusicPlayer_StepVolumeAndPitchTowardTargets(void)
Restores BGM after a fanfare and steps its volume and combined tempo/pitch ramps once per VBlank,...
Definition sound.c:92
u16 slot
Index into gSound_PlayerSlots; AudioCommand_Play gives slot 7's songs the first player from 7 down to...
u32 status
bits: 31 paused | 15..0 tracks active after the last tick.
The system's routines and globals: interrupts, BIOS sound bias, random numbers and sleep.
The integer types, TRUE, UNUSED and the fixed-address macros.
signed int s32
Definition types.h:14
unsigned int u32
Definition types.h:15
signed short s16
Definition types.h:12
unsigned short u16
Definition types.h:13
#define UNUSED
Definition types.h:22
unsigned char u8
Definition types.h:11