Alchemy
Loading...
Searching...
No Matches

audio.c File Reference

Go to the source code of this file.

Macros

#define SOUND_PLAYER_COUNT   ((u16)(u32)Sound_PlayerCount)
#define SOUND_MAX_LINES   ((u32)Sound_MaxLines)
#define SOUND_MIXER_CODE_SIZE   0x400
 The bytes of mixer code Audio_Initialize copies to IWRAM.

Functions

u32 Audio_UmulHigh32 (u32 left, u32 right)
s32 Pcm_KeyToFrequency (void *wave, u8 key, u8 pitch)
 Interpolates adjacent key frequencies and scales them by the wave sample frequency.
void Audio_NoopStub (void)
 Provides the empty callback retained by the sound driver.
void Sound_Mixer (void)
void AudioEngine_Initialize (struct SoundWork *work)
 Resets the sound hardware, clears the whole sound work block and installs the default PCM driver: stores work in SOUND_WORK, points the FIFO DMAs at its two buffers, fills in the driver callbacks and command table, sets the default rate and finally sets the work's lock.
void AudioEngine_SetMode (u32 mode)
 Applies the reverb, channel-count, volume, bias and sample-rate fields of mode to the sound work, under its lock.
void AudioEngine_RunMixerTick (void)
void MusicPlayer_Initialize (struct SoundPlayer *player, struct SoundTrack *tracks, u8 count)
 Deactivates up to sixteen tracks and links the player into the mixer tick chain.
void MusicPlayer_StartSong (struct SoundPlayer *player, const struct SequenceHeader *header)
 Starts a song when its priority permits, resetting its tracks and tempo.
void MusicPlayer_Stop (struct SoundPlayer *player)
 Pauses this player and stops all of its tracks, under the player's lock.
void MusicPlayer_BeginFadeOut (struct SoundPlayer *player, u16 speed)
 Begins a fade that stops and clears tracks when its volume reaches zero.
void Audio_ResumePlayer (struct SoundPlayer *player)
 Clears the player's paused flag while holding the player lock.
void MusicTrack_OperateWorkByte (struct SoundPlayer *player, struct SoundTrack *track)
 Executes a work-byte operation and consumes a branch address for comparisons.
void MusicTrack_SetLfoSpeedFromCommand (struct SoundPlayer *player, struct SoundTrack *track)
void MusicTrack_SetModulationFromCommand (struct SoundPlayer *player, struct SoundTrack *track)
void MusicTrack_ReleaseKey (struct SoundPlayer *player, struct SoundTrack *track)
void AudioEngine_SetPcmRate (u32 mode)
 Sets the PCM frame length (mode bits 16 to 19 pick a gSound_FrameLengths entry) and the rates derived from it, and Timer 0's reload, starting playback at scanline 159.
void MusicTrack_Stop (struct SoundPlayer *player, struct SoundTrack *track)
void MusicPlayer_StepFade (struct SoundPlayer *player)
 Advances a due fade step, updating active track volumes, or at the end of a fade out stops (or pauses) the player.
void MusicTrack_CalcOutput (struct SoundPlayer *player, struct SoundTrack *track)
 Recalculates dirty stereo volumes and pitch from track settings and modulation.
void Cgb_UpdateChannels (void)
 Steps CGB note envelopes and writes changed pitch, volume and trigger registers, once a frame.
void CgbChannel_Mute (u8 channel)
 Disables one CGB channel using its envelope or wave-control register.
s32 Cgb_KeyToFrequency (u8 kind, u8 key, u8 fine)
 Converts a key and fractional pitch to a CGB tone or noise-register value.
static __inline__ void MusicPlayer_SetFadeParameters (struct SoundPlayer *player, u16 period, u16 volume)
 Starts a fade under the player lock, resuming the player for fade-ins.
void Audio_Initialize (void)
 Copies the RAM mixer to IWRAM, initializes the PCM and CGB paths and configures every player from gSound_PlayerSlots.
void AudioEngine_RunMixer (void)
void Audio_PlaySound (u16 songId)
 Starts the song on the player selected by its song-table entry.
UNUSED void Audio_PlaySoundIfInactive (u16 songId)
 Starts the song unless its selected player is already playing it.
UNUSED void Audio_PlayOrResumeSound (u16 songId)
 Starts a different or finished song, or resumes the same paused song.
UNUSED void Audio_StopSound (u16 songId)
 Stops the selected player only when it holds this song.
UNUSED void Audio_ResumeSound (u16 songId)
 Resumes the selected player only when it holds this song.
UNUSED void Audio_StopAllPlayers (void)
 Stops every player. Nothing in ☀️ calls it.
UNUSED void MusicPlayer_Resume (struct SoundPlayer *player)
 Resumes player. Nothing in ☀️ calls it.
UNUSED void Audio_ResumeAllPlayers (void)
 Resumes every player. Nothing in ☀️ calls it.
void MusicPlayer_FadeOut (struct SoundPlayer *player, u16 speed)
 Begins the standard stop-and-clear fade.
UNUSED void MusicPlayer_FadeOutPause (struct SoundPlayer *player, u16 speed)
 Begins a fade that pauses the player at zero volume, keeping its tracks' state for a later resume.
UNUSED void MusicPlayer_FadeIn (struct SoundPlayer *player, u16 speed)
 Starts a fade from zero volume and resumes the player.
UNUSED void MusicPlayer_ResetActiveTracks (struct SoundPlayer *player)
 Initializes active tracks still carrying their reset flag: clears their settings and gives them the default bend range, volume and LFO speed.
void CgbAudio_Initialize (struct CgbNote *notes)
 Resets the CGB sound hardware, installs the CGB-aware command handlers and callbacks in the sound work, and clears the four channel records notes.

Variables

u8 gSound_PcmPitchCodes []
 PCM pitches: the high four bits hold the octave, the low four the note.
u32 gSound_PcmFrequencySteps []
 Frequencies by note at octave zero, in 32-bit fixed point.
char Sound_PlayerCount []
char Sound_MaxLines []
struct SoundWork gSound_Work
struct CgbNote gSound_CgbNotes [4]
u8 gSound_WorkBytes []
const struct PlayerSlot gSound_PlayerSlots []
const struct SongEntry gSound_SongTable []
SoundCommand gSound_CommandTable [36]

Macro Definition Documentation

◆ SOUND_PLAYER_COUNT

#define SOUND_PLAYER_COUNT   ((u16)(u32)Sound_PlayerCount)

Definition at line 44 of file audio.c.

Referenced by Audio_Initialize(), Audio_ResumeAllPlayers(), and Audio_StopAllPlayers().

◆ SOUND_MAX_LINES

#define SOUND_MAX_LINES   ((u32)Sound_MaxLines)

Definition at line 45 of file audio.c.

Referenced by CgbAudio_Initialize().

◆ SOUND_MIXER_CODE_SIZE

#define SOUND_MIXER_CODE_SIZE   0x400

The bytes of mixer code Audio_Initialize copies to IWRAM.

Definition at line 113 of file audio.c.

Referenced by Audio_Initialize().

Function Documentation

◆ Audio_UmulHigh32()

u32 Audio_UmulHigh32 ( u32 left,
u32 right )

◆ Pcm_KeyToFrequency()

s32 Pcm_KeyToFrequency ( void * wave,
u8 key,
u8 pitch )

Interpolates adjacent key frequencies and scales them by the wave sample frequency.

Called by the song player for each PCM note.

Definition at line 12 of file audio.c.

Referenced by Pcm_KeyToFrequency().

◆ Audio_NoopStub()

void Audio_NoopStub ( void )

Provides the empty callback retained by the sound driver.

Definition at line 39 of file audio.c.

Referenced by Audio_NoopStub().

◆ Sound_Mixer()

void Sound_Mixer ( void )

Referenced by Audio_Initialize(), and Sound_Mixer().

◆ AudioEngine_Initialize()

void AudioEngine_Initialize ( struct SoundWork * work)

Resets the sound hardware, clears the whole sound work block and installs the default PCM driver: stores work in SOUND_WORK, points the FIFO DMAs at its two buffers, fills in the driver callbacks and command table, sets the default rate and finally sets the work's lock.

Called by Audio_Initialize.

Definition at line 64 of file engine.c.

Referenced by Audio_Initialize(), Audio_UmulHigh32(), AudioEngine_Initialize(), and Sound_Mixer().

◆ AudioEngine_SetMode()

void AudioEngine_SetMode ( u32 mode)

Applies the reverb, channel-count, volume, bias and sample-rate fields of mode to the sound work, under its lock.

Called by Audio_Initialize.

Definition at line 136 of file engine.c.

Referenced by Audio_Initialize(), AudioEngine_RunMixerTick(), AudioEngine_SetMode(), MusicPlayer_StartSong(), MusicTrack_Stop(), and Sound_Mixer().

◆ AudioEngine_RunMixerTick()

void AudioEngine_RunMixerTick ( void )

◆ MusicPlayer_Initialize()

void MusicPlayer_Initialize ( struct SoundPlayer * player,
struct SoundTrack * tracks,
u8 count )

Deactivates up to sixteen tracks and links the player into the mixer tick chain.

Definition at line 246 of file engine.c.

Referenced by Audio_Initialize(), AudioEngine_RunMixerTick(), and MusicPlayer_Initialize().

◆ MusicPlayer_StartSong()

void MusicPlayer_StartSong ( struct SoundPlayer * player,
const struct SequenceHeader * header )

Starts a song when its priority permits, resetting its tracks and tempo.

Definition at line 282 of file engine.c.

Referenced by Audio_PlayOrResumeSound(), Audio_PlaySound(), Audio_PlaySoundIfInactive(), AudioCommand_Play(), AudioEngine_RunMixerTick(), MusicPlayer_StartSong(), and STATIC_ASSERT().

◆ MusicPlayer_Stop()

void MusicPlayer_Stop ( struct SoundPlayer * player)

Pauses this player and stops all of its tracks, under the player's lock.

Called by Audio_StopSound and Audio_StopAllPlayers.

Definition at line 339 of file engine.c.

Referenced by Audio_StopAllPlayers(), Audio_StopSound(), AudioEngine_RunMixerTick(), and MusicPlayer_Stop().

◆ MusicPlayer_BeginFadeOut()

void MusicPlayer_BeginFadeOut ( struct SoundPlayer * player,
u16 speed )

Begins a fade that stops and clears tracks when its volume reaches zero.

Called by MusicPlayer_FadeOut.

Definition at line 108 of file audio.c.

Referenced by AudioEngine_RunMixerTick(), MusicPlayer_BeginFadeOut(), and MusicPlayer_FadeOut().

◆ Audio_ResumePlayer()

void Audio_ResumePlayer ( struct SoundPlayer * player)

Clears the player's paused flag while holding the player lock.

Called by the resume and play-or-resume commands.

Definition at line 84 of file audio.c.

Referenced by Audio_PlayOrResumeSound(), Audio_ResumeAllPlayers(), Audio_ResumePlayer(), Audio_ResumeSound(), AudioEngine_RunMixerTick(), and MusicPlayer_Resume().

◆ MusicTrack_OperateWorkByte()

void MusicTrack_OperateWorkByte ( struct SoundPlayer * player,
struct SoundTrack * track )

Executes a work-byte operation and consumes a branch address for comparisons.

Definition at line 957 of file engine.c.

Referenced by AudioEngine_RunMixerTick(), CgbAudio_Initialize(), MusicTrack_HandleNote(), and MusicTrack_OperateWorkByte().

◆ MusicTrack_SetLfoSpeedFromCommand()

void MusicTrack_SetLfoSpeedFromCommand ( struct SoundPlayer * player,
struct SoundTrack * track )

◆ MusicTrack_SetModulationFromCommand()

void MusicTrack_SetModulationFromCommand ( struct SoundPlayer * player,
struct SoundTrack * track )

◆ MusicTrack_ReleaseKey()

void MusicTrack_ReleaseKey ( struct SoundPlayer * player,
struct SoundTrack * track )

◆ AudioEngine_SetPcmRate()

void AudioEngine_SetPcmRate ( u32 mode)

Sets the PCM frame length (mode bits 16 to 19 pick a gSound_FrameLengths entry) and the rates derived from it, and Timer 0's reload, starting playback at scanline 159.

Called by AudioEngine_Initialize and AudioEngine_SetMode, and by songs through the command table.

Definition at line 105 of file engine.c.

Referenced by AudioEngine_Initialize(), AudioEngine_RunMixerTick(), AudioEngine_SetMode(), AudioEngine_SetPcmRate(), AudioEngine_SetPcmRate(), CgbAudio_Initialize(), MusicTrack_HandleNote(), and MusicTrack_ReleaseKey().

◆ MusicTrack_Stop()

◆ MusicPlayer_StepFade()

void MusicPlayer_StepFade ( struct SoundPlayer * player)

Advances a due fade step, updating active track volumes, or at the end of a fade out stops (or pauses) the player.

Called by the song player each tick through the command table.

Definition at line 360 of file engine.c.

Referenced by AudioEngine_RunMixerTick(), CgbAudio_Initialize(), MusicPlayer_StepFade(), MusicPlayer_StepFade(), and MusicTrack_Stop().

◆ MusicTrack_CalcOutput()

void MusicTrack_CalcOutput ( struct SoundPlayer * player,
struct SoundTrack * track )

Recalculates dirty stereo volumes and pitch from track settings and modulation.

Definition at line 411 of file engine.c.

Referenced by CgbAudio_Initialize(), MusicPlayer_Tick(), MusicTrack_CalcOutput(), MusicTrack_CalcOutput(), and MusicTrack_Stop().

◆ Cgb_UpdateChannels()

void Cgb_UpdateChannels ( void )

Steps CGB note envelopes and writes changed pitch, volume and trigger registers, once a frame.

Installed as the work's cgbUpdate callback by CgbAudio_Initialize; the mixer calls it.

Definition at line 537 of file engine.c.

Referenced by AudioEngine_RunMixerTick(), Cgb_UpdateChannels(), CgbAudio_Initialize(), and MusicTrack_Stop().

◆ CgbChannel_Mute()

void CgbChannel_Mute ( u8 channel)

Disables one CGB channel using its envelope or wave-control register.

Called by Cgb_UpdateChannels, and installed as the work's cgbMute callback.

Definition at line 485 of file engine.c.

Referenced by AudioEngine_RunMixerTick(), Cgb_UpdateChannels(), CgbAudio_Initialize(), CgbChannel_Mute(), and MusicTrack_Stop().

◆ Cgb_KeyToFrequency()

s32 Cgb_KeyToFrequency ( u8 kind,
u8 key,
u8 fine )

Converts a key and fractional pitch to a CGB tone or noise-register value.

Installed as the work's cgbFrequency callback by CgbAudio_Initialize.

Definition at line 448 of file engine.c.

Referenced by AudioEngine_RunMixerTick(), Cgb_KeyToFrequency(), CgbAudio_Initialize(), and MusicTrack_Stop().

◆ MusicPlayer_SetFadeParameters()

__inline__ void MusicPlayer_SetFadeParameters ( struct SoundPlayer * player,
u16 period,
u16 volume )
static

Starts a fade under the player lock, resuming the player for fade-ins.

Definition at line 93 of file audio.c.

Referenced by MusicPlayer_BeginFadeOut(), MusicPlayer_FadeIn(), MusicPlayer_FadeOutPause(), and MusicPlayer_SetFadeParameters().

◆ Audio_Initialize()

void Audio_Initialize ( void )

Copies the RAM mixer to IWRAM, initializes the PCM and CGB paths and configures every player from gSound_PlayerSlots.

Called by Audio_InitializeRuntimeDefaults at power-on.

Definition at line 118 of file audio.c.

Referenced by Audio_Initialize(), Audio_InitializeRuntimeDefaults(), and Sound_Mixer().

◆ AudioEngine_RunMixer()

void AudioEngine_RunMixer ( void )

Definition at line 138 of file audio.c.

Referenced by AudioEngine_RunMixer().

◆ Audio_PlaySound()

void Audio_PlaySound ( u16 songId)

Starts the song on the player selected by its song-table entry.

Called by AudioCommand_Play.

Definition at line 142 of file audio.c.

Referenced by Audio_PlaySound(), AudioCommand_Play(), and Sound_Mixer().

◆ Audio_PlaySoundIfInactive()

UNUSED void Audio_PlaySoundIfInactive ( u16 songId)

Starts the song unless its selected player is already playing it.

Nothing in ☀️ calls it.

Definition at line 152 of file audio.c.

Referenced by Audio_PlaySoundIfInactive().

◆ Audio_PlayOrResumeSound()

UNUSED void Audio_PlayOrResumeSound ( u16 songId)

Starts a different or finished song, or resumes the same paused song.

Nothing in ☀️ calls it.

Definition at line 167 of file audio.c.

Referenced by Audio_PlayOrResumeSound().

◆ Audio_StopSound()

UNUSED void Audio_StopSound ( u16 songId)

Stops the selected player only when it holds this song.

Nothing in ☀️ calls it.

Definition at line 183 of file audio.c.

Referenced by Audio_StopSound().

◆ Audio_ResumeSound()

UNUSED void Audio_ResumeSound ( u16 songId)

Resumes the selected player only when it holds this song.

Nothing in ☀️ calls it.

Definition at line 195 of file audio.c.

Referenced by Audio_ResumeSound().

◆ Audio_StopAllPlayers()

UNUSED void Audio_StopAllPlayers ( void )

Stops every player. Nothing in ☀️ calls it.

Definition at line 206 of file audio.c.

Referenced by Audio_StopAllPlayers(), and AudioCommand_StopAllPlayers().

◆ MusicPlayer_Resume()

UNUSED void MusicPlayer_Resume ( struct SoundPlayer * player)

Resumes player. Nothing in ☀️ calls it.

Definition at line 214 of file audio.c.

Referenced by MusicPlayer_Resume().

◆ Audio_ResumeAllPlayers()

UNUSED void Audio_ResumeAllPlayers ( void )

Resumes every player. Nothing in ☀️ calls it.

Definition at line 219 of file audio.c.

Referenced by Audio_ResumeAllPlayers(), and AudioCommand_ResumeAllPlayers().

◆ MusicPlayer_FadeOut()

void MusicPlayer_FadeOut ( struct SoundPlayer * player,
u16 speed )

Begins the standard stop-and-clear fade.

Called by AudioCommand_Play's fade cues.

Definition at line 228 of file audio.c.

Referenced by AudioCommand_Play(), MusicPlayer_FadeOut(), and STATIC_ASSERT().

◆ MusicPlayer_FadeOutPause()

UNUSED void MusicPlayer_FadeOutPause ( struct SoundPlayer * player,
u16 speed )

Begins a fade that pauses the player at zero volume, keeping its tracks' state for a later resume.

Nothing in ☀️ calls it.

Definition at line 234 of file audio.c.

Referenced by MusicPlayer_FadeOutPause().

◆ MusicPlayer_FadeIn()

UNUSED void MusicPlayer_FadeIn ( struct SoundPlayer * player,
u16 speed )

Starts a fade from zero volume and resumes the player.

Nothing in ☀️ calls it.

Definition at line 241 of file audio.c.

Referenced by MusicPlayer_FadeIn().

◆ MusicPlayer_ResetActiveTracks()

UNUSED void MusicPlayer_ResetActiveTracks ( struct SoundPlayer * player)

Initializes active tracks still carrying their reset flag: clears their settings and gives them the default bend range, volume and LFO speed.

Nothing in ☀️ calls it.

Definition at line 248 of file audio.c.

Referenced by MusicPlayer_ResetActiveTracks().

◆ CgbAudio_Initialize()

void CgbAudio_Initialize ( struct CgbNote * notes)

Resets the CGB sound hardware, installs the CGB-aware command handlers and callbacks in the sound work, and clears the four channel records notes.

Called by Audio_Initialize.

Definition at line 272 of file audio.c.

Referenced by Audio_Initialize(), CgbAudio_Initialize(), and STATIC_ASSERT().

Variable Documentation

◆ gSound_PcmPitchCodes

u8 gSound_PcmPitchCodes[]
extern

PCM pitches: the high four bits hold the octave, the low four the note.

Definition at line 105 of file tables.c.

Referenced by Audio_UmulHigh32(), and Pcm_KeyToFrequency().

◆ gSound_PcmFrequencySteps

u32 gSound_PcmFrequencySteps[]
extern

Frequencies by note at octave zero, in 32-bit fixed point.

Definition at line 124 of file tables.c.

Referenced by Audio_UmulHigh32(), and Pcm_KeyToFrequency().

◆ Sound_PlayerCount

char Sound_PlayerCount[]
extern

◆ Sound_MaxLines

char Sound_MaxLines[]
extern

◆ gSound_Work

struct SoundWork gSound_Work
extern

◆ gSound_CgbNotes

struct CgbNote gSound_CgbNotes[4]
extern

◆ gSound_WorkBytes

u8 gSound_WorkBytes[]
extern

◆ gSound_PlayerSlots

◆ gSound_SongTable

◆ gSound_CommandTable