diff --git a/include/audio/effects.h b/include/audio/effects.h new file mode 100644 index 0000000000..67741200f3 --- /dev/null +++ b/include/audio/effects.h @@ -0,0 +1,117 @@ +#ifndef AUDIO_EFFECTS_H +#define AUDIO_EFFECTS_H + +#include "PR/ultratypes.h" +#include "unk.h" + +struct Note; +struct SequencePlayer; + +/* Multi-Point ADSR Envelope (Attack, Decay, Sustain, Release) */ + +typedef enum AdsrStatus { + /* 0 */ ADSR_STATUS_DISABLED, + /* 1 */ ADSR_STATUS_INITIAL, + /* 2 */ ADSR_STATUS_START_LOOP, + /* 3 */ ADSR_STATUS_LOOP, + /* 4 */ ADSR_STATUS_FADE, + /* 5 */ ADSR_STATUS_HANG, + /* 6 */ ADSR_STATUS_DECAY, + /* 7 */ ADSR_STATUS_RELEASE, + /* 8 */ ADSR_STATUS_SUSTAIN +} AdsrStatus; + +typedef struct EnvelopePoint { + /* 0x0 */ s16 delay; + /* 0x2 */ s16 arg; +} EnvelopePoint; // size = 0x4 + +typedef struct AdsrSettings { + /* 0x0 */ u8 decayIndex; // index used to obtain adsr decay rate from adsrDecayTable + /* 0x1 */ u8 sustain; + /* 0x4 */ EnvelopePoint* envelope; +} AdsrSettings; // size = 0x8 + +typedef struct AdsrState { + union { + struct { + /* 0x00 */ u8 unused : 1; + /* 0x00 */ u8 hang : 1; + /* 0x00 */ u8 decay : 1; + /* 0x00 */ u8 release : 1; + /* 0x00 */ u8 status : 4; + } s; + /* 0x00 */ u8 asByte; + } action; + /* 0x01 */ u8 envelopeIndex; + /* 0x02 */ s16 delay; + /* 0x04 */ f32 sustain; + /* 0x08 */ f32 velocity; + /* 0x0C */ f32 fadeOutVel; + /* 0x10 */ f32 current; + /* 0x14 */ f32 target; + /* 0x18 */ UNK_TYPE1 pad18[4]; + /* 0x1C */ EnvelopePoint* envelope; +} AdsrState; // size = 0x20 + + +/* Vibrato */ + +typedef struct VibratoSubStruct { + /* 0x0 */ u16 vibratoRateStart; + /* 0x2 */ u16 vibratoDepthStart; + /* 0x4 */ u16 vibratoRateTarget; + /* 0x6 */ u16 vibratoDepthTarget; + /* 0x8 */ u16 vibratoRateChangeDelay; + /* 0xA */ u16 vibratoDepthChangeDelay; + /* 0xC */ u16 vibratoDelay; +} VibratoSubStruct; // size = 0xE + +typedef struct VibratoState { + /* 0x00 */ VibratoSubStruct* vibSubStruct; // Something else? + /* 0x04 */ u32 time; // 0x400 is 1 unit of time, 0x10000 is 1 period + /* 0x08 */ s16* curve; + /* 0x0C */ f32 depth; + /* 0x10 */ f32 rate; + /* 0x14 */ u8 active; + /* 0x16 */ u16 rateChangeTimer; + /* 0x18 */ u16 depthChangeTimer; + /* 0x1A */ u16 delay; +} VibratoState; // size = 0x1C + + +/* Portamento */ + +typedef enum PortamentoMode { + /* 0 */ PORTAMENTO_MODE_OFF, + /* 1 */ PORTAMENTO_MODE_1, + /* 2 */ PORTAMENTO_MODE_2, + /* 3 */ PORTAMENTO_MODE_3, + /* 4 */ PORTAMENTO_MODE_4, + /* 5 */ PORTAMENTO_MODE_5 +} PortamentoMode; + +#define PORTAMENTO_IS_SPECIAL(x) ((x).mode & 0x80) +#define PORTAMENTO_MODE(x) ((x).mode & ~0x80) + +// Pitch sliding by up to one octave in the positive direction. Negative +// direction is "supported" by setting extent to be negative. The code +// extrapolates exponentially in the wrong direction in that case, but that +// doesn't prevent seqplayer from doing it, AFAICT. +typedef struct Portamento { + /* 0x0 */ u8 mode; // bit 0x80 denotes something; the rest are an index 0-5 + /* 0x2 */ u16 cur; + /* 0x4 */ u16 speed; + /* 0x8 */ f32 extent; +} Portamento; // size = 0xC + +void AudioScript_SequencePlayerProcessSound(struct SequencePlayer* seqPlayer); + +void AudioEffects_InitAdsr(AdsrState* adsr, EnvelopePoint* envelope, s16* volOut); +void AudioEffects_InitVibrato(struct Note* note); +void AudioEffects_InitPortamento(struct Note* note); + +f32 AudioEffects_UpdateAdsr(AdsrState* adsr); +void AudioEffects_UpdatePortamentoAndVibrato(struct Note* note); + +#endif diff --git a/include/functions.h b/include/functions.h index 52ba8b111c..ac13a30f11 100644 --- a/include/functions.h +++ b/include/functions.h @@ -1825,13 +1825,6 @@ void AudioPlayback_AudioListRemove(AudioListItem* item); Note* AudioPlayback_AllocNote(SequenceLayer* layer); void AudioPlayback_NoteInitAll(void); -void AudioEffects_SequencePlayerProcessSound(SequencePlayer* seqPlayer); -void AudioEffects_NoteVibratoUpdate(Note* note); -void AudioEffects_NoteVibratoInit(Note* note); -void AudioEffects_NotePortamentoInit(Note* note); -void AudioEffects_AdsrInit(AdsrState* adsr, EnvelopePoint* envelope, s16* volOut); -f32 AudioEffects_AdsrUpdate(AdsrState* adsr); - void AudioScript_SequenceChannelDisable(SequenceChannel* channel); void AudioScript_SequencePlayerDisableAsFinished(SequencePlayer* seqPlayer); void AudioScript_SequencePlayerDisable(SequencePlayer* seqPlayer); diff --git a/include/z64audio.h b/include/z64audio.h index c4943c09ab..13e3165b7a 100644 --- a/include/z64audio.h +++ b/include/z64audio.h @@ -5,6 +5,7 @@ #include "ultra64/os_voice.h" #include "audiothread_cmd.h" #include "libc/stddef.h" +#include "audio/effects.h" #define NO_LAYER ((SequenceLayer*)(-1)) @@ -111,18 +112,6 @@ typedef enum { /* 4 */ SOUNDMODE_SURROUND } SoundMode; -typedef enum { - /* 0 */ ADSR_STATE_DISABLED, - /* 1 */ ADSR_STATE_INITIAL, - /* 2 */ ADSR_STATE_START_LOOP, - /* 3 */ ADSR_STATE_LOOP, - /* 4 */ ADSR_STATE_FADE, - /* 5 */ ADSR_STATE_HANG, - /* 6 */ ADSR_STATE_DECAY, - /* 7 */ ADSR_STATE_RELEASE, - /* 8 */ ADSR_STATE_SUSTAIN -} AdsrStatus; - typedef enum { /* 0 */ MEDIUM_RAM, /* 1 */ MEDIUM_UNK, @@ -203,22 +192,6 @@ typedef struct NotePool { /* 0x30 */ AudioListItem active; } NotePool; // size = 0x40 -// Pitch sliding by up to one octave in the positive direction. Negative -// direction is "supported" by setting extent to be negative. The code -// extrapolates exponentially in the wrong direction in that case, but that -// doesn't prevent seqplayer from doing it, AFAICT. -typedef struct { - /* 0x0 */ u8 mode; // bit 0x80 denotes something; the rest are an index 0-5 - /* 0x2 */ u16 cur; - /* 0x4 */ u16 speed; - /* 0x8 */ f32 extent; -} Portamento; // size = 0xC - -typedef struct { - /* 0x0 */ s16 delay; - /* 0x2 */ s16 arg; -} EnvelopePoint; // size = 0x4 - typedef struct { /* 0x00 */ u32 start; /* 0x04 */ u32 loopEnd; // numSamples into the sample where the loop ends @@ -326,16 +299,16 @@ typedef struct { } Instrument; // size = 0x20 typedef struct { - /* 0x00 */ u8 adsrDecayIndex; // index used to obtain adsr decay rate from adsrDecayTable - /* 0x01 */ u8 pan; - /* 0x02 */ u8 isRelocated; // have tunedSample.sample and envelope been relocated (offsets to pointers) - /* 0x04 */ TunedSample tunedSample; - /* 0x0C */ EnvelopePoint* envelope; + /* 0x0 */ u8 adsrDecayIndex; // index used to obtain adsr decay rate from adsrDecayTable + /* 0x1 */ u8 pan; + /* 0x2 */ u8 isRelocated; // have tunedSample.sample and envelope been relocated (offsets to pointers) + /* 0x4 */ TunedSample tunedSample; + /* 0xC */ EnvelopePoint* envelope; } Drum; // size = 0x10 typedef struct { - /* 0x00 */ TunedSample tunedSample; -} SoundEffect; // size = 0x08 + /* 0x0 */ TunedSample tunedSample; +} SoundEffect; // size = 0x8 typedef struct { /* 0x00 */ u8 numInstruments; @@ -357,7 +330,7 @@ typedef struct { } SeqScriptState; // size = 0x1C // Also known as a Group, according to debug strings. -typedef struct { +typedef struct SequencePlayer { /* 0x000 */ u8 enabled : 1; /* 0x000 */ u8 finished : 1; /* 0x000 */ u8 muted : 1; @@ -400,34 +373,6 @@ typedef struct { /* 0x158 */ s8 seqScriptIO[8]; } SequencePlayer; // size = 0x160 -typedef struct { - /* 0x0 */ u8 decayIndex; // index used to obtain adsr decay rate from adsrDecayTable - /* 0x1 */ u8 sustain; - /* 0x4 */ EnvelopePoint* envelope; -} AdsrSettings; // size = 0x8 - -typedef struct { - union { - struct { - /* 0x00 */ u8 unused : 1; - /* 0x00 */ u8 hang : 1; - /* 0x00 */ u8 decay : 1; - /* 0x00 */ u8 release : 1; - /* 0x00 */ u8 state : 4; - } s; - /* 0x00 */ u8 asByte; - } action; - /* 0x01 */ u8 envIndex; - /* 0x02 */ s16 delay; - /* 0x04 */ f32 sustain; - /* 0x08 */ f32 velocity; - /* 0x0C */ f32 fadeOutVel; - /* 0x10 */ f32 current; - /* 0x14 */ f32 target; - /* 0x18 */ UNK_TYPE1 unk_18[4]; - /* 0x1C */ EnvelopePoint* envelope; -} AdsrState; // size = 0x20 - typedef union { struct { /* 0x0 */ u8 unused : 2; @@ -454,16 +399,6 @@ typedef struct { /* 0x14 */ s16* filterBuf; } NoteAttributes; // size = 0x18 -typedef struct VibratoSubStruct { - /* 0x0 */ u16 vibratoRateStart; - /* 0x2 */ u16 vibratoDepthStart; - /* 0x4 */ u16 vibratoRateTarget; - /* 0x6 */ u16 vibratoDepthTarget; - /* 0x8 */ u16 vibratoRateChangeDelay; - /* 0xA */ u16 vibratoDepthChangeDelay; - /* 0xC */ u16 vibratoDelay; -} VibratoSubStruct; // size = 0xE - // Also known as a SubTrack, according to sm64 debug strings. typedef struct SequenceChannel { /* 0x00 */ u8 enabled : 1; @@ -551,7 +486,7 @@ typedef struct SequenceLayer { /* 0x0A */ u16 bit_0 : 1; /* 0x0A */ u16 bit_1 : 1; /* 0x0A */ u16 bit_2 : 1; - /* 0x0A */ u16 bit_3 : 1; + /* 0x0A */ u16 useVibrato : 1; /* 0x0A */ u16 bit_4 : 1; /* 0x0A */ u16 bit_5 : 1; /* 0x0A */ u16 bit_6 : 1; @@ -623,18 +558,6 @@ typedef struct { /* 0x20 */ UNK_TYPE1 unk_20[0x4]; } NoteSynthesisState; // size = 0x24 -typedef struct { - /* 0x00 */ struct VibratoSubStruct* vibSubStruct; // Something else? - /* 0x04 */ u32 time; - /* 0x08 */ s16* curve; - /* 0x0C */ f32 depth; - /* 0x10 */ f32 rate; - /* 0x14 */ u8 active; - /* 0x16 */ u16 rateChangeTimer; - /* 0x18 */ u16 depthChangeTimer; - /* 0x1A */ u16 delay; -} VibratoState; // size = 0x1C - typedef enum { /* 0 */ PLAYBACK_STATUS_0, /* 1 */ PLAYBACK_STATUS_1, diff --git a/src/audio/lib/effects.c b/src/audio/lib/effects.c index 580b7fd224..93d8955140 100644 --- a/src/audio/lib/effects.c +++ b/src/audio/lib/effects.c @@ -1,6 +1,18 @@ +/** + * @file effects.c + * + * The first half of this file processes sound on the seqPlayer, channel, and layer level + * once the .seq file is finished for this update. + * + * The second half of this file implements three types of audio effects over long periods of times: + * - Vibrato: regular, pulsating change of pitch + * - Portamento: pitch sliding from one note to another + * - Multi-Point ADSR Envelope: volume changing over time through Attack, Decay, Sustain, Release + */ #include "global.h" +#include "audio/effects.h" -void AudioEffects_SequenceChannelProcessSound(SequenceChannel* channel, s32 recalculateVolume, s32 applyBend) { +void AudioScript_SequenceChannelProcessSound(SequenceChannel* channel, s32 recalculateVolume, s32 applyBend) { f32 channelVolume; f32 chanFreqScale; s32 i; @@ -8,7 +20,7 @@ void AudioEffects_SequenceChannelProcessSound(SequenceChannel* channel, s32 reca if (channel->changes.s.volume || recalculateVolume) { channelVolume = channel->volume * channel->volumeScale * channel->seqPlayer->appliedFadeVolume; if (channel->seqPlayer->muted && (channel->muteFlags & MUTE_FLAGS_SOFTEN)) { - channelVolume = channel->seqPlayer->muteVolumeScale * channelVolume; + channelVolume = channelVolume * channel->seqPlayer->muteVolumeScale; } channel->appliedVolume = SQ(channelVolume); } @@ -48,7 +60,7 @@ void AudioEffects_SequenceChannelProcessSound(SequenceChannel* channel, s32 reca channel->changes.asByte = 0; } -void AudioEffects_SequencePlayerProcessSound(SequencePlayer* seqPlayer) { +void AudioScript_SequencePlayerProcessSound(SequencePlayer* seqPlayer) { s32 i; if ((seqPlayer->fadeTimer != 0) && (seqPlayer->skipTicks == 0)) { @@ -73,29 +85,32 @@ void AudioEffects_SequencePlayerProcessSound(SequencePlayer* seqPlayer) { seqPlayer->appliedFadeVolume = seqPlayer->fadeVolume * seqPlayer->fadeVolumeScale; } - for (i = 0; i < ARRAY_COUNT(seqPlayer->channels); i++) { + for (i = 0; i < SEQ_NUM_CHANNELS; i++) { if (seqPlayer->channels[i]->enabled == true) { - AudioEffects_SequenceChannelProcessSound(seqPlayer->channels[i], seqPlayer->recalculateVolume, - seqPlayer->applyBend); + AudioScript_SequenceChannelProcessSound(seqPlayer->channels[i], seqPlayer->recalculateVolume, + seqPlayer->applyBend); } } seqPlayer->recalculateVolume = false; } -f32 AudioEffects_GetPortamentoFreqScale(Portamento* portamento) { - u32 loResCur; +/** + * @return freqScale + */ +f32 AudioEffects_UpdatePortamento(Portamento* portamento) { + u32 bendIndex; f32 portamentoFreq; portamento->cur += portamento->speed; - loResCur = (portamento->cur >> 8) & 0xFF; + bendIndex = (portamento->cur >> 8) & 0xFF; - if (loResCur >= 127) { - loResCur = 127; - portamento->mode = 0; + if (bendIndex >= 127) { + bendIndex = 127; + portamento->mode = PORTAMENTO_MODE_OFF; } - portamentoFreq = AUDIO_LERPIMP(1.0f, gBendPitchOneOctaveFrequencies[loResCur + 128], portamento->extent); + portamentoFreq = AUDIO_LERPIMP(1.0f, gBendPitchOneOctaveFrequencies[bendIndex + 128], portamento->extent); return portamentoFreq; } @@ -104,13 +119,17 @@ s16 AudioEffects_GetVibratoPitchChange(VibratoState* vib) { s32 index; vib->time += (s32)vib->rate; - index = (vib->time >> 10) & 0x3F; + // 0x400 is 1 unit of time, 0x10000 is 1 period + index = (vib->time / 0x400) % WAVE_SAMPLE_COUNT; return vib->curve[index]; } -f32 AudioEffects_GetVibratoFreqScale(VibratoState* vib) { - static f32 activeVibratoFreqScaleSum = 0.0f; - static s32 activeVibratoCount = 0; +/** + * @return freqScale + */ +f32 AudioEffects_UpdateVibrato(VibratoState* vib) { + static f32 sActiveVibratoFreqScaleSum = 0.0f; + static s32 sActiveVibratoCount = 0; f32 pitchChange; f32 depth; f32 invDepth; @@ -124,7 +143,7 @@ f32 AudioEffects_GetVibratoFreqScale(VibratoState* vib) { } if (subVib != NULL) { - if (vib->depthChangeTimer) { + if ((u32)vib->depthChangeTimer != 0) { if (vib->depthChangeTimer == 1) { vib->depth = (s32)subVib->vibratoDepthTarget; } else { @@ -138,7 +157,7 @@ f32 AudioEffects_GetVibratoFreqScale(VibratoState* vib) { } } - if (vib->rateChangeTimer) { + if ((u32)vib->rateChangeTimer != 0) { if (vib->rateChangeTimer == 1) { vib->rate = (s32)subVib->vibratoRateTarget; } else { @@ -157,38 +176,40 @@ f32 AudioEffects_GetVibratoFreqScale(VibratoState* vib) { return 1.0f; } - pitchChange = AudioEffects_GetVibratoPitchChange(vib) + 32768.0f; + pitchChange = (f32)AudioEffects_GetVibratoPitchChange(vib) + 0x8000; scaledDepth = vib->depth / 4096.0f; depth = scaledDepth + 1.0f; invDepth = 1.0f / depth; - // inverse linear interpolation - result = 1.0f / ((depth - invDepth) * pitchChange / 65536.0f + invDepth); + // Inverse linear interpolation + result = 1.0f / ((depth - invDepth) * pitchChange / 0x10000 + invDepth); - activeVibratoFreqScaleSum += result; - activeVibratoCount++; + sActiveVibratoFreqScaleSum += result; + sActiveVibratoCount++; return result; } -void AudioEffects_NoteVibratoUpdate(Note* note) { - if (note->playbackState.portamento.mode != 0) { - note->playbackState.portamentoFreqScale = AudioEffects_GetPortamentoFreqScale(¬e->playbackState.portamento); +void AudioEffects_UpdatePortamentoAndVibrato(Note* note) { + // Update Portamento + if (note->playbackState.portamento.mode != PORTAMENTO_MODE_OFF) { + note->playbackState.portamentoFreqScale = AudioEffects_UpdatePortamento(¬e->playbackState.portamento); } + // Update Vibrato if (note->playbackState.vibratoState.active) { - note->playbackState.vibratoFreqScale = AudioEffects_GetVibratoFreqScale(¬e->playbackState.vibratoState); + note->playbackState.vibratoFreqScale = AudioEffects_UpdateVibrato(¬e->playbackState.vibratoState); } } -void AudioEffects_NoteVibratoInit(Note* note) { +void AudioEffects_InitVibrato(Note* note) { NotePlaybackState* playbackState = ¬e->playbackState; VibratoState* vib = &playbackState->vibratoState; VibratoSubStruct* subVib; vib->active = true; - vib->curve = gWaveSamples[2]; + vib->curve = gWaveSamples[2]; // gSineWaveSample - if (playbackState->parentLayer->unk_0A.s.bit_3 == 1) { + if (playbackState->parentLayer->unk_0A.s.useVibrato == true) { vib->vibSubStruct = &playbackState->parentLayer->channel->vibrato; } else { vib->vibSubStruct = &playbackState->parentLayer->vibrato; @@ -213,12 +234,12 @@ void AudioEffects_NoteVibratoInit(Note* note) { vib->delay = subVib->vibratoDelay; } -void AudioEffects_NotePortamentoInit(Note* note) { +void AudioEffects_InitPortamento(Note* note) { note->playbackState.portamentoFreqScale = 1.0f; note->playbackState.portamento = note->playbackState.parentLayer->portamento; } -void AudioEffects_AdsrInit(AdsrState* adsr, EnvelopePoint* envelope, s16* volOut) { +void AudioEffects_InitAdsr(AdsrState* adsr, EnvelopePoint* envelope, s16* volOut) { adsr->action.asByte = 0; adsr->delay = 0; adsr->envelope = envelope; @@ -230,41 +251,44 @@ void AudioEffects_AdsrInit(AdsrState* adsr, EnvelopePoint* envelope, s16* volOut // removed, but the function parameter was forgotten and remains.) } -f32 AudioEffects_AdsrUpdate(AdsrState* adsr) { - u8 state = adsr->action.s.state; +/** + * @return volumeScale + */ +f32 AudioEffects_UpdateAdsr(AdsrState* adsr) { + u8 status = adsr->action.s.status; - switch (state) { - case ADSR_STATE_DISABLED: + switch (status) { + case ADSR_STATUS_DISABLED: return 0.0f; - case ADSR_STATE_INITIAL: + case ADSR_STATUS_INITIAL: if (adsr->action.s.hang) { - adsr->action.s.state = ADSR_STATE_HANG; + adsr->action.s.status = ADSR_STATUS_HANG; break; } // fallthrough - case ADSR_STATE_START_LOOP: - adsr->envIndex = 0; - adsr->action.s.state = ADSR_STATE_LOOP; + case ADSR_STATUS_START_LOOP: + adsr->envelopeIndex = 0; + adsr->action.s.status = ADSR_STATUS_LOOP; // fallthrough retry: - case ADSR_STATE_LOOP: - adsr->delay = adsr->envelope[adsr->envIndex].delay; + case ADSR_STATUS_LOOP: + adsr->delay = adsr->envelope[adsr->envelopeIndex].delay; switch (adsr->delay) { case ADSR_DISABLE: - adsr->action.s.state = ADSR_STATE_DISABLED; + adsr->action.s.status = ADSR_STATUS_DISABLED; break; case ADSR_HANG: - adsr->action.s.state = ADSR_STATE_HANG; + adsr->action.s.status = ADSR_STATUS_HANG; break; case ADSR_GOTO: - adsr->envIndex = adsr->envelope[adsr->envIndex].arg; + adsr->envelopeIndex = adsr->envelope[adsr->envelopeIndex].arg; goto retry; case ADSR_RESTART: - adsr->action.s.state = ADSR_STATE_INITIAL; + adsr->action.s.status = ADSR_STATUS_INITIAL; break; default: @@ -272,60 +296,60 @@ f32 AudioEffects_AdsrUpdate(AdsrState* adsr) { if (adsr->delay == 0) { adsr->delay = 1; } - adsr->target = adsr->envelope[adsr->envIndex].arg / 32767.0f; - adsr->target = adsr->target * adsr->target; + adsr->target = adsr->envelope[adsr->envelopeIndex].arg / 32767.0f; + adsr->target = SQ(adsr->target); adsr->velocity = (adsr->target - adsr->current) / adsr->delay; - adsr->action.s.state = ADSR_STATE_FADE; - adsr->envIndex++; + adsr->action.s.status = ADSR_STATUS_FADE; + adsr->envelopeIndex++; break; } - if (adsr->action.s.state != ADSR_STATE_FADE) { + if (adsr->action.s.status != ADSR_STATUS_FADE) { break; } // fallthrough - case ADSR_STATE_FADE: + case ADSR_STATUS_FADE: adsr->current += adsr->velocity; adsr->delay--; if (adsr->delay <= 0) { - adsr->action.s.state = ADSR_STATE_LOOP; + adsr->action.s.status = ADSR_STATUS_LOOP; } // fallthrough - case ADSR_STATE_HANG: + case ADSR_STATUS_HANG: break; - case ADSR_STATE_DECAY: - case ADSR_STATE_RELEASE: + case ADSR_STATUS_DECAY: + case ADSR_STATUS_RELEASE: adsr->current -= adsr->fadeOutVel; - if (adsr->sustain != 0.0f && state == ADSR_STATE_DECAY) { + if ((adsr->sustain != 0.0f) && (status == ADSR_STATUS_DECAY)) { if (adsr->current < adsr->sustain) { adsr->current = adsr->sustain; adsr->delay = 128; - adsr->action.s.state = ADSR_STATE_SUSTAIN; + adsr->action.s.status = ADSR_STATUS_SUSTAIN; } break; } if (adsr->current < 0.00001f) { adsr->current = 0.0f; - adsr->action.s.state = ADSR_STATE_DISABLED; + adsr->action.s.status = ADSR_STATUS_DISABLED; } break; - case ADSR_STATE_SUSTAIN: + case ADSR_STATUS_SUSTAIN: adsr->delay--; if (adsr->delay == 0) { - adsr->action.s.state = ADSR_STATE_RELEASE; + adsr->action.s.status = ADSR_STATUS_RELEASE; } break; } if (adsr->action.s.decay) { - adsr->action.s.state = ADSR_STATE_DECAY; + adsr->action.s.status = ADSR_STATUS_DECAY; adsr->action.s.decay = false; } if (adsr->action.s.release) { - adsr->action.s.state = ADSR_STATE_RELEASE; + adsr->action.s.status = ADSR_STATUS_RELEASE; adsr->action.s.release = false; } diff --git a/src/audio/lib/heap.c b/src/audio/lib/heap.c index 5d8dfee894..24bb7043d0 100644 --- a/src/audio/lib/heap.c +++ b/src/audio/lib/heap.c @@ -1,4 +1,5 @@ #include "global.h" +#include "audio/effects.h" void* AudioHeap_SearchRegularCaches(s32 tableType, s32 cache, s32 id); void AudioHeap_InitSampleCaches(size_t persistentSampleCacheSize, size_t temporarySampleCacheSize); @@ -870,7 +871,7 @@ s32 AudioHeap_ResetStep(void) { } else { for (i = 0; i < gAudioCtx.numNotes; i++) { if (gAudioCtx.notes[i].sampleState.bitField0.enabled && - gAudioCtx.notes[i].playbackState.adsr.action.s.state != ADSR_STATE_DISABLED) { + gAudioCtx.notes[i].playbackState.adsr.action.s.status != ADSR_STATUS_DISABLED) { gAudioCtx.notes[i].playbackState.adsr.fadeOutVel = gAudioCtx.audioBufferParameters.updatesPerFrameInv; gAudioCtx.notes[i].playbackState.adsr.action.s.release = true; diff --git a/src/audio/lib/playback.c b/src/audio/lib/playback.c index a626645c52..6bc5e3943c 100644 --- a/src/audio/lib/playback.c +++ b/src/audio/lib/playback.c @@ -1,4 +1,5 @@ #include "global.h" +#include "audio/effects.h" void AudioPlayback_NoteSetResamplingRate(NoteSampleState* sampleState, f32 resamplingRateInput); void AudioPlayback_AudioListPushFront(AudioListItem* list, AudioListItem* item); @@ -134,15 +135,15 @@ void AudioPlayback_NoteSetResamplingRate(NoteSampleState* sampleState, f32 resam void AudioPlayback_NoteInit(Note* note) { if (note->playbackState.parentLayer->adsr.decayIndex == 0) { - AudioEffects_AdsrInit(¬e->playbackState.adsr, note->playbackState.parentLayer->channel->adsr.envelope, + AudioEffects_InitAdsr(¬e->playbackState.adsr, note->playbackState.parentLayer->channel->adsr.envelope, ¬e->playbackState.adsrVolScaleUnused); } else { - AudioEffects_AdsrInit(¬e->playbackState.adsr, note->playbackState.parentLayer->adsr.envelope, + AudioEffects_InitAdsr(¬e->playbackState.adsr, note->playbackState.parentLayer->adsr.envelope, ¬e->playbackState.adsrVolScaleUnused); } note->playbackState.status = PLAYBACK_STATUS_0; - note->playbackState.adsr.action.s.state = ADSR_STATE_INITIAL; + note->playbackState.adsr.action.s.status = ADSR_STATUS_INITIAL; note->sampleState = gDefaultSampleState; } @@ -156,7 +157,7 @@ void AudioPlayback_NoteDisable(Note* note) { note->sampleState.bitField0.finished = false; note->playbackState.parentLayer = NO_LAYER; note->playbackState.prevParentLayer = NO_LAYER; - note->playbackState.adsr.action.s.state = ADSR_STATE_DISABLED; + note->playbackState.adsr.action.s.status = ADSR_STATUS_DISABLED; note->playbackState.adsr.current = 0; } @@ -170,7 +171,7 @@ void AudioPlayback_ProcessNotes(void) { NotePlaybackState* playbackState; NoteSubAttributes subAttrs; u8 bookOffset; - f32 scale; + f32 adsrVolumeScale; s32 i; for (i = 0; i < gAudioCtx.numNotes; i++) { @@ -218,14 +219,14 @@ void AudioPlayback_ProcessNotes(void) { if (1) {} noteSampleState = ¬e->sampleState; if ((playbackState->status >= 1) || noteSampleState->bitField0.finished) { - if ((playbackState->adsr.action.s.state == ADSR_STATE_DISABLED) || + if ((playbackState->adsr.action.s.status == ADSR_STATUS_DISABLED) || noteSampleState->bitField0.finished) { if (playbackState->wantedParentLayer != NO_LAYER) { AudioPlayback_NoteDisable(note); if (playbackState->wantedParentLayer->channel != NULL) { AudioPlayback_NoteInitForLayer(note, playbackState->wantedParentLayer); - AudioEffects_NoteVibratoInit(note); - AudioEffects_NotePortamentoInit(note); + AudioEffects_InitVibrato(note); + AudioEffects_InitPortamento(note); AudioPlayback_AudioListRemove(¬e->listItem); AudioScript_AudioListPushBack(¬e->listItem.pool->active, ¬e->listItem); playbackState->wantedParentLayer = NO_LAYER; @@ -247,7 +248,7 @@ void AudioPlayback_ProcessNotes(void) { continue; } } - } else if (playbackState->adsr.action.s.state == ADSR_STATE_DISABLED) { + } else if (playbackState->adsr.action.s.status == ADSR_STATUS_DISABLED) { if (playbackState->parentLayer != NO_LAYER) { playbackState->parentLayer->bit1 = true; } @@ -257,8 +258,8 @@ void AudioPlayback_ProcessNotes(void) { continue; } - scale = AudioEffects_AdsrUpdate(&playbackState->adsr); - AudioEffects_NoteVibratoUpdate(note); + adsrVolumeScale = AudioEffects_UpdateAdsr(&playbackState->adsr); + AudioEffects_UpdatePortamentoAndVibrato(note); playbackStatus = playbackState->status; attrs = &playbackState->attributes; if ((playbackStatus == PLAYBACK_STATUS_1) || (playbackStatus == PLAYBACK_STATUS_2)) { @@ -320,7 +321,7 @@ void AudioPlayback_ProcessNotes(void) { subAttrs.frequency *= playbackState->vibratoFreqScale * playbackState->portamentoFreqScale; subAttrs.frequency *= gAudioCtx.audioBufferParameters.resampleRate; - subAttrs.velocity *= scale; + subAttrs.velocity *= adsrVolumeScale; AudioPlayback_InitSampleState(note, sampleState, &subAttrs); noteSampleState->bitField1.bookOffset = bookOffset; skip:; @@ -490,14 +491,14 @@ void AudioPlayback_SeqLayerDecayRelease(SequenceLayer* layer, s32 target) { if (note->playbackState.parentLayer != layer) { if (note->playbackState.parentLayer == NO_LAYER && note->playbackState.wantedParentLayer == NO_LAYER && - note->playbackState.prevParentLayer == layer && target != ADSR_STATE_DECAY) { + note->playbackState.prevParentLayer == layer && target != ADSR_STATUS_DECAY) { note->playbackState.adsr.fadeOutVel = gAudioCtx.audioBufferParameters.updatesPerFrameInv; note->playbackState.adsr.action.s.release = true; } return; } - if (note->playbackState.adsr.action.s.state != ADSR_STATE_DECAY) { + if (note->playbackState.adsr.action.s.status != ADSR_STATUS_DECAY) { attrs->freqScale = layer->noteFreqScale; attrs->velocity = layer->noteVelocity; attrs->pan = layer->notePan; @@ -551,7 +552,7 @@ void AudioPlayback_SeqLayerDecayRelease(SequenceLayer* layer, s32 target) { note->playbackState.prevParentLayer = note->playbackState.parentLayer; note->playbackState.parentLayer = NO_LAYER; - if (target == ADSR_STATE_RELEASE) { + if (target == ADSR_STATUS_RELEASE) { note->playbackState.adsr.fadeOutVel = gAudioCtx.audioBufferParameters.updatesPerFrameInv; note->playbackState.adsr.action.s.release = true; note->playbackState.status = PLAYBACK_STATUS_2; @@ -568,18 +569,18 @@ void AudioPlayback_SeqLayerDecayRelease(SequenceLayer* layer, s32 target) { } } - if (target == ADSR_STATE_DECAY) { + if (target == ADSR_STATUS_DECAY) { AudioPlayback_AudioListRemove(¬e->listItem); AudioPlayback_AudioListPushFront(¬e->listItem.pool->decaying, ¬e->listItem); } } void AudioPlayback_SeqLayerNoteDecay(SequenceLayer* layer) { - AudioPlayback_SeqLayerDecayRelease(layer, ADSR_STATE_DECAY); + AudioPlayback_SeqLayerDecayRelease(layer, ADSR_STATUS_DECAY); } void AudioPlayback_SeqLayerNoteRelease(SequenceLayer* layer) { - AudioPlayback_SeqLayerDecayRelease(layer, ADSR_STATE_RELEASE); + AudioPlayback_SeqLayerDecayRelease(layer, ADSR_STATUS_RELEASE); } /** @@ -600,7 +601,7 @@ s32 AudioPlayback_BuildSyntheticWave(Note* note, SequenceLayer* layer, s32 waveI } freqScale = layer->freqScale; - if ((layer->portamento.mode != 0) && (0.0f < layer->portamento.extent)) { + if ((layer->portamento.mode != PORTAMENTO_MODE_OFF) && (layer->portamento.extent > 0.0f)) { freqScale *= (layer->portamento.extent + 1.0f); } diff --git a/src/audio/lib/seqplayer.c b/src/audio/lib/seqplayer.c index 270164972e..34ec93a7b4 100644 --- a/src/audio/lib/seqplayer.c +++ b/src/audio/lib/seqplayer.c @@ -15,20 +15,8 @@ */ #include "global.h" -#define PORTAMENTO_IS_SPECIAL(x) ((x).mode & 0x80) -#define PORTAMENTO_MODE(x) ((x).mode & ~0x80) - #define PROCESS_SCRIPT_END -1 -typedef enum { - /* 0 */ PORTAMENTO_MODE_OFF, - /* 1 */ PORTAMENTO_MODE_1, - /* 2 */ PORTAMENTO_MODE_2, - /* 3 */ PORTAMENTO_MODE_3, - /* 4 */ PORTAMENTO_MODE_4, - /* 5 */ PORTAMENTO_MODE_5 -} PortamentoMode; - u8 AudioScript_ScriptReadU8(SeqScriptState* state); s16 AudioScript_ScriptReadS16(SeqScriptState* state); u16 AudioScript_ScriptReadCompressedU16(SeqScriptState* state); @@ -634,7 +622,7 @@ s32 AudioScript_SeqLayerProcessScriptStep5(SequenceLayer* layer, s32 sameTunedSa note = layer->note; if (note->playbackState.parentLayer == layer) { - AudioEffects_NoteVibratoInit(note); + AudioEffects_InitVibrato(note); } } } @@ -642,7 +630,7 @@ s32 AudioScript_SeqLayerProcessScriptStep5(SequenceLayer* layer, s32 sameTunedSa if ((layer->note != NULL) && (layer->note->playbackState.parentLayer == layer)) { note = layer->note; - AudioEffects_NotePortamentoInit(note); + AudioEffects_InitPortamento(note); } return 0; @@ -2204,7 +2192,7 @@ void AudioScript_ProcessSequences(s32 arg0) { seqPlayer = &gAudioCtx.seqPlayers[i]; if (seqPlayer->enabled == true) { AudioScript_SequencePlayerProcessSequence(seqPlayer); - AudioEffects_SequencePlayerProcessSound(seqPlayer); + AudioScript_SequencePlayerProcessSound(seqPlayer); } } @@ -2214,7 +2202,7 @@ void AudioScript_ProcessSequences(s32 arg0) { void AudioScript_SkipForwardSequence(SequencePlayer* seqPlayer) { while (seqPlayer->skipTicks > 0) { AudioScript_SequencePlayerProcessSequence(seqPlayer); - AudioEffects_SequencePlayerProcessSound(seqPlayer); + AudioScript_SequencePlayerProcessSound(seqPlayer); seqPlayer->skipTicks--; } } diff --git a/src/audio/lib/thread.c b/src/audio/lib/thread.c index 082e77a67d..fe4804693d 100644 --- a/src/audio/lib/thread.c +++ b/src/audio/lib/thread.c @@ -4,6 +4,7 @@ * Top-level file that coordinates all audio code on the audio thread. */ #include "global.h" +#include "audio/effects.h" AudioTask* AudioThread_UpdateImpl(void); void AudioThread_SetFadeOutTimer(s32 seqPlayerIndex, s32 fadeTimer); @@ -938,7 +939,7 @@ s32 AudioThread_CountAndReleaseNotes(s32 flags) { playbackState = ¬e->playbackState; if (note->sampleState.bitField0.enabled) { noteSampleState = ¬e->sampleState; - if (playbackState->adsr.action.s.state != ADSR_STATE_DISABLED) { + if (playbackState->adsr.action.s.status != ADSR_STATUS_DISABLED) { if (flags >= AUDIO_NOTE_SAMPLE_NOTES) { tunedSample = noteSampleState->tunedSample; if ((tunedSample == NULL) || noteSampleState->bitField1.isSyntheticWave) { diff --git a/src/code/z_sram_NES.c b/src/code/z_sram_NES.c index 4da2f69636..5efb438960 100644 --- a/src/code/z_sram_NES.c +++ b/src/code/z_sram_NES.c @@ -1,4 +1,3 @@ -#include "prevent_bss_reordering.h" #include "global.h" #include "z64horse.h" #include "overlays/gamestates/ovl_file_choose/z_file_select.h" diff --git a/src/overlays/actors/ovl_Boss_03/z_boss_03.c b/src/overlays/actors/ovl_Boss_03/z_boss_03.c index af79315db4..5db259be18 100644 --- a/src/overlays/actors/ovl_Boss_03/z_boss_03.c +++ b/src/overlays/actors/ovl_Boss_03/z_boss_03.c @@ -48,6 +48,8 @@ * - Effect Update/Draw * - Seaweed */ + +#include "prevent_bss_reordering.h" #include "z_boss_03.h" #include "overlays/actors/ovl_Door_Warp1/z_door_warp1.h" #include "overlays/actors/ovl_En_Water_Effect/z_en_water_effect.h" diff --git a/src/overlays/actors/ovl_En_Wood02/z_en_wood02.c b/src/overlays/actors/ovl_En_Wood02/z_en_wood02.c index cf4331ffa0..8611c1c301 100644 --- a/src/overlays/actors/ovl_En_Wood02/z_en_wood02.c +++ b/src/overlays/actors/ovl_En_Wood02/z_en_wood02.c @@ -4,7 +4,6 @@ * Description: Trees, shrubs */ -#include "prevent_bss_reordering.h" #include "z_en_wood02.h" #include "objects/object_wood02/object_wood02.h" diff --git a/tools/disasm/functions.txt b/tools/disasm/functions.txt index 214e1ef4e9..decceda754 100644 --- a/tools/disasm/functions.txt +++ b/tools/disasm/functions.txt @@ -3757,16 +3757,16 @@ 0x801964F8:("AudioPlayback_AllocNoteFromActive",), 0x801965F0:("AudioPlayback_AllocNote",), 0x801968C4:("AudioPlayback_NoteInitAll",), - 0x80196A00:("AudioEffects_SequenceChannelProcessSound",), - 0x80196BC8:("AudioEffects_SequencePlayerProcessSound",), - 0x80196D20:("AudioEffects_GetPortamentoFreqScale",), + 0x80196A00:("AudioScript_SequenceChannelProcessSound",), + 0x80196BC8:("AudioScript_SequencePlayerProcessSound",), + 0x80196D20:("AudioEffects_UpdatePortamento",), 0x80196D7C:("AudioEffects_GetVibratoPitchChange",), - 0x80196DB4:("AudioEffects_GetVibratoFreqScale",), - 0x80196FEC:("AudioEffects_NoteVibratoUpdate",), - 0x80197048:("AudioEffects_NoteVibratoInit",), - 0x80197138:("AudioEffects_NotePortamentoInit",), - 0x80197164:("AudioEffects_AdsrInit",), - 0x80197188:("AudioEffects_AdsrUpdate",), + 0x80196DB4:("AudioEffects_UpdateVibrato",), + 0x80196FEC:("AudioEffects_UpdatePortamentoAndVibrato",), + 0x80197048:("AudioEffects_InitVibrato",), + 0x80197138:("AudioEffects_InitPortamento",), + 0x80197164:("AudioEffects_InitAdsr",), + 0x80197188:("AudioEffects_UpdateAdsr",), 0x801974D0:("AudioScript_GetScriptControlFlowArgument",), 0x80197538:("AudioScript_HandleScriptFlowControl",), 0x80197714:("AudioScript_InitSequenceChannel",), diff --git a/tools/sizes/code_functions.csv b/tools/sizes/code_functions.csv index 074eb35ec7..85c087b373 100644 --- a/tools/sizes/code_functions.csv +++ b/tools/sizes/code_functions.csv @@ -3274,16 +3274,16 @@ asm/non_matchings/code/audio_playback/AudioPlayback_AllocNoteFromDecaying.s,Audi asm/non_matchings/code/audio_playback/AudioPlayback_AllocNoteFromActive.s,AudioPlayback_AllocNoteFromActive,0x801964F8,0x3E asm/non_matchings/code/audio_playback/AudioPlayback_AllocNote.s,AudioPlayback_AllocNote,0x801965F0,0xB5 asm/non_matchings/code/audio_playback/AudioPlayback_NoteInitAll.s,AudioPlayback_NoteInitAll,0x801968C4,0x4F -asm/non_matchings/code/audio_effects/AudioEffects_SequenceChannelProcessSound.s,AudioEffects_SequenceChannelProcessSound,0x80196A00,0x72 -asm/non_matchings/code/audio_effects/AudioEffects_SequencePlayerProcessSound.s,AudioEffects_SequencePlayerProcessSound,0x80196BC8,0x56 -asm/non_matchings/code/audio_effects/AudioEffects_GetPortamentoFreqScale.s,AudioEffects_GetPortamentoFreqScale,0x80196D20,0x17 +asm/non_matchings/code/audio_effects/AudioScript_SequenceChannelProcessSound.s,AudioScript_SequenceChannelProcessSound,0x80196A00,0x72 +asm/non_matchings/code/audio_effects/AudioScript_SequencePlayerProcessSound.s,AudioScript_SequencePlayerProcessSound,0x80196BC8,0x56 +asm/non_matchings/code/audio_effects/AudioEffects_UpdatePortamento.s,AudioEffects_UpdatePortamento,0x80196D20,0x17 asm/non_matchings/code/audio_effects/AudioEffects_GetVibratoPitchChange.s,AudioEffects_GetVibratoPitchChange,0x80196D7C,0xE -asm/non_matchings/code/audio_effects/AudioEffects_GetVibratoFreqScale.s,AudioEffects_GetVibratoFreqScale,0x80196DB4,0x8E -asm/non_matchings/code/audio_effects/AudioEffects_NoteVibratoUpdate.s,AudioEffects_NoteVibratoUpdate,0x80196FEC,0x17 -asm/non_matchings/code/audio_effects/AudioEffects_NoteVibratoInit.s,AudioEffects_NoteVibratoInit,0x80197048,0x3C -asm/non_matchings/code/audio_effects/AudioEffects_NotePortamentoInit.s,AudioEffects_NotePortamentoInit,0x80197138,0xB -asm/non_matchings/code/audio_effects/AudioEffects_AdsrInit.s,AudioEffects_AdsrInit,0x80197164,0x9 -asm/non_matchings/code/audio_effects/AudioEffects_AdsrUpdate.s,AudioEffects_AdsrUpdate,0x80197188,0xD2 +asm/non_matchings/code/audio_effects/AudioEffects_UpdateVibrato.s,AudioEffects_UpdateVibrato,0x80196DB4,0x8E +asm/non_matchings/code/audio_effects/AudioEffects_UpdatePortamentoAndVibrato.s,AudioEffects_UpdatePortamentoAndVibrato,0x80196FEC,0x17 +asm/non_matchings/code/audio_effects/AudioEffects_InitVibrato.s,AudioEffects_InitVibrato,0x80197048,0x3C +asm/non_matchings/code/audio_effects/AudioEffects_InitPortamento.s,AudioEffects_InitPortamento,0x80197138,0xB +asm/non_matchings/code/audio_effects/AudioEffects_InitAdsr.s,AudioEffects_InitAdsr,0x80197164,0x9 +asm/non_matchings/code/audio_effects/AudioEffects_UpdateAdsr.s,AudioEffects_UpdateAdsr,0x80197188,0xD2 asm/non_matchings/code/audio_seqplayer/AudioScript_GetScriptControlFlowArgument.s,AudioScript_GetScriptControlFlowArgument,0x801974D0,0x1A asm/non_matchings/code/audio_seqplayer/AudioScript_HandleScriptFlowControl.s,AudioScript_HandleScriptFlowControl,0x80197538,0x77 asm/non_matchings/code/audio_seqplayer/AudioScript_InitSequenceChannel.s,AudioScript_InitSequenceChannel,0x80197714,0x5B