1
0
Fork 0
mirror of https://github.com/zeldaret/oot.git synced 2024-11-11 03:39:59 +00:00

Audio Docs: Adsr Decay Rate (#1238)

* Init adsr decay docs

* cleanup

* cleanup

* Revert some docs

* Cleaner docs

* count hex to dec

* scaled updates per frame

* Consistency

* Oops, fix meaning

* Avoid `decayRate` conflict with reverb `decayRate`

* PR suggestion
This commit is contained in:
engineer124 2022-05-30 04:31:43 +10:00 committed by GitHub
parent d39ce02458
commit 8f1fd58f22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 47 deletions

View file

@ -193,7 +193,7 @@ typedef struct {
/* 0x00 */ u8 loaded; /* 0x00 */ u8 loaded;
/* 0x01 */ u8 normalRangeLo; /* 0x01 */ u8 normalRangeLo;
/* 0x02 */ u8 normalRangeHi; /* 0x02 */ u8 normalRangeHi;
/* 0x03 */ u8 releaseRate; /* 0x03 */ u8 adsrDecayIndex; // index used to obtain adsr decay rate from adsrDecayTable
/* 0x04 */ AdsrEnvelope* envelope; /* 0x04 */ AdsrEnvelope* envelope;
/* 0x08 */ SoundFontSound lowNotesSound; /* 0x08 */ SoundFontSound lowNotesSound;
/* 0x10 */ SoundFontSound normalNotesSound; /* 0x10 */ SoundFontSound normalNotesSound;
@ -201,7 +201,7 @@ typedef struct {
} Instrument; // size = 0x20 } Instrument; // size = 0x20
typedef struct { typedef struct {
/* 0x00 */ u8 releaseRate; /* 0x00 */ u8 adsrDecayIndex; // index used to obtain adsr decay rate from adsrDecayTable
/* 0x01 */ u8 pan; /* 0x01 */ u8 pan;
/* 0x02 */ u8 loaded; /* 0x02 */ u8 loaded;
/* 0x04 */ SoundFontSound sound; /* 0x04 */ SoundFontSound sound;
@ -271,7 +271,7 @@ typedef struct {
} SequencePlayer; // size = 0x160 } SequencePlayer; // size = 0x160
typedef struct { typedef struct {
/* 0x0 */ u8 releaseRate; /* 0x0 */ u8 decayIndex; // index used to obtain adsr decay rate from adsrDecayTable
/* 0x1 */ u8 sustain; /* 0x1 */ u8 sustain;
/* 0x4 */ AdsrEnvelope* envelope; /* 0x4 */ AdsrEnvelope* envelope;
} AdsrSettings; // size = 0x8 } AdsrSettings; // size = 0x8
@ -581,16 +581,16 @@ typedef struct {
/* 0x06 */ s16 samplesPerFrameTarget; /* 0x06 */ s16 samplesPerFrameTarget;
/* 0x08 */ s16 maxAiBufferLength; /* 0x08 */ s16 maxAiBufferLength;
/* 0x0A */ s16 minAiBufferLength; /* 0x0A */ s16 minAiBufferLength;
/* 0x0C */ s16 updatesPerFrame; /* 0x0C */ s16 updatesPerFrame; // for each frame of the audio thread (default 60 fps), number of updates to process audio
/* 0x0E */ s16 samplesPerUpdate; /* 0x0E */ s16 samplesPerUpdate;
/* 0x10 */ s16 samplesPerUpdateMax; /* 0x10 */ s16 samplesPerUpdateMax;
/* 0x12 */ s16 samplesPerUpdateMin; /* 0x12 */ s16 samplesPerUpdateMin;
/* 0x14 */ s16 numSequencePlayers; /* 0x14 */ s16 numSequencePlayers;
/* 0x18 */ f32 resampleRate; /* 0x18 */ f32 resampleRate;
/* 0x1C */ f32 updatesPerFrameInv; /* 0x1C */ f32 updatesPerFrameInv; // inverse (reciprocal) of updatesPerFrame
/* 0x20 */ f32 unkUpdatesPerFrameScaled; /* 0x20 */ f32 updatesPerFrameInvScaled; // updatesPerFrameInv scaled down by a factor of 256
/* 0x24 */ f32 unk_24; /* 0x24 */ f32 updatesPerFrameScaled; // updatesPerFrame scaled down by a factor of 4
} AudioBufferParameters; } AudioBufferParameters; // size = 0x28
typedef struct { typedef struct {
/* 0x0 */ u8* start; /* 0x0 */ u8* start;
@ -856,7 +856,7 @@ typedef struct {
/* 0x3518 */ volatile u8 resetStatus; /* 0x3518 */ volatile u8 resetStatus;
/* 0x3519 */ u8 audioResetSpecIdToLoad; /* 0x3519 */ u8 audioResetSpecIdToLoad;
/* 0x351C */ s32 audioResetFadeOutFramesLeft; /* 0x351C */ s32 audioResetFadeOutFramesLeft;
/* 0x3520 */ f32* unk_3520; /* 0x3520 */ f32* adsrDecayTable; // A table on the audio heap that stores decay rates used for adsr
/* 0x3524 */ u8* audioHeap; /* 0x3524 */ u8* audioHeap;
/* 0x3528 */ u32 audioHeapSize; /* 0x3528 */ u32 audioHeapSize;
/* 0x352C */ Note* notes; /* 0x352C */ Note* notes;

View file

@ -223,23 +223,21 @@ void Audio_AdsrInit(AdsrState* adsr, AdsrEnvelope* envelope, s16* volOut) {
f32 Audio_AdsrUpdate(AdsrState* adsr) { f32 Audio_AdsrUpdate(AdsrState* adsr) {
u8 state = adsr->action.s.state; u8 state = adsr->action.s.state;
switch (state) { switch (state) {
case ADSR_STATE_DISABLED: case ADSR_STATE_DISABLED:
return 0.0f; return 0.0f;
case ADSR_STATE_INITIAL: { case ADSR_STATE_INITIAL:
if (adsr->action.s.hang) { if (adsr->action.s.hang) {
adsr->action.s.state = ADSR_STATE_HANG; adsr->action.s.state = ADSR_STATE_HANG;
break; break;
} }
// fallthrough // fallthrough
}
case ADSR_STATE_START_LOOP: case ADSR_STATE_START_LOOP:
adsr->envIndex = 0; adsr->envIndex = 0;
adsr->action.s.state = ADSR_STATE_LOOP; adsr->action.s.state = ADSR_STATE_LOOP;
// fallthrough // fallthrough
retry: retry:
case ADSR_STATE_LOOP: case ADSR_STATE_LOOP:
adsr->delay = adsr->envelope[adsr->envIndex].delay; adsr->delay = adsr->envelope[adsr->envIndex].delay;
@ -247,18 +245,21 @@ f32 Audio_AdsrUpdate(AdsrState* adsr) {
case ADSR_DISABLE: case ADSR_DISABLE:
adsr->action.s.state = ADSR_STATE_DISABLED; adsr->action.s.state = ADSR_STATE_DISABLED;
break; break;
case ADSR_HANG: case ADSR_HANG:
adsr->action.s.state = ADSR_STATE_HANG; adsr->action.s.state = ADSR_STATE_HANG;
break; break;
case ADSR_GOTO: case ADSR_GOTO:
adsr->envIndex = adsr->envelope[adsr->envIndex].arg; adsr->envIndex = adsr->envelope[adsr->envIndex].arg;
goto retry; goto retry;
case ADSR_RESTART: case ADSR_RESTART:
adsr->action.s.state = ADSR_STATE_INITIAL; adsr->action.s.state = ADSR_STATE_INITIAL;
break; break;
default: default:
adsr->delay *= gAudioContext.audioBufferParameters.unk_24; adsr->delay *= gAudioContext.audioBufferParameters.updatesPerFrameScaled;
if (adsr->delay == 0) { if (adsr->delay == 0) {
adsr->delay = 1; adsr->delay = 1;
} }
@ -273,19 +274,18 @@ f32 Audio_AdsrUpdate(AdsrState* adsr) {
break; break;
} }
// fallthrough // fallthrough
case ADSR_STATE_FADE: case ADSR_STATE_FADE:
adsr->current += adsr->velocity; adsr->current += adsr->velocity;
if (--adsr->delay <= 0) { adsr->delay--;
if (adsr->delay <= 0) {
adsr->action.s.state = ADSR_STATE_LOOP; adsr->action.s.state = ADSR_STATE_LOOP;
} }
// fallthrough // fallthrough
case ADSR_STATE_HANG: case ADSR_STATE_HANG:
break; break;
case ADSR_STATE_DECAY: case ADSR_STATE_DECAY:
case ADSR_STATE_RELEASE: { case ADSR_STATE_RELEASE:
adsr->current -= adsr->fadeOutVel; adsr->current -= adsr->fadeOutVel;
if (adsr->sustain != 0.0f && state == ADSR_STATE_DECAY) { if (adsr->sustain != 0.0f && state == ADSR_STATE_DECAY) {
if (adsr->current < adsr->sustain) { if (adsr->current < adsr->sustain) {
@ -301,10 +301,9 @@ f32 Audio_AdsrUpdate(AdsrState* adsr) {
adsr->action.s.state = ADSR_STATE_DISABLED; adsr->action.s.state = ADSR_STATE_DISABLED;
} }
break; break;
}
case ADSR_STATE_SUSTAIN: case ADSR_STATE_SUSTAIN:
adsr->delay -= 1; adsr->delay--;
if (adsr->delay == 0) { if (adsr->delay == 0) {
adsr->action.s.state = ADSR_STATE_RELEASE; adsr->action.s.state = ADSR_STATE_RELEASE;
} }
@ -324,8 +323,10 @@ f32 Audio_AdsrUpdate(AdsrState* adsr) {
if (adsr->current < 0.0f) { if (adsr->current < 0.0f) {
return 0.0f; return 0.0f;
} }
if (adsr->current > 1.0f) { if (adsr->current > 1.0f) {
return 1.0f; return 1.0f;
} }
return adsr->current; return adsr->current;
} }

View file

@ -10,32 +10,40 @@ void AudioHeap_DiscardSampleCaches(void);
void AudioHeap_DiscardSampleBank(s32 sampleBankId); void AudioHeap_DiscardSampleBank(s32 sampleBankId);
void AudioHeap_DiscardSampleBanks(void); void AudioHeap_DiscardSampleBanks(void);
f32 func_800DDE20(f32 arg0) { /**
return 256.0f * gAudioContext.audioBufferParameters.unkUpdatesPerFrameScaled / arg0; * Effectively scales `updatesPerFrameInv` by the reciprocal of `scaleInv`
* `updatesPerFrameInvScaled` is just `updatesPerFrameInv` scaled down by a factor of 256.0f
* i.e. (256.0f * `updatesPerFrameInvScaled`) is just `updatesPerFrameInv`
*/
f32 AudioHeap_CalculateAdsrDecay(f32 scaleInv) {
return (256.0f * gAudioContext.audioBufferParameters.updatesPerFrameInvScaled) / scaleInv;
} }
void func_800DDE3C(void) { /**
* Initialize the decay rate table used for decaying notes as part of adsr
*/
void AudioHeap_InitAdsrDecayTable(void) {
s32 i; s32 i;
gAudioContext.unk_3520[255] = func_800DDE20(0.25f); gAudioContext.adsrDecayTable[255] = AudioHeap_CalculateAdsrDecay(0.25f);
gAudioContext.unk_3520[254] = func_800DDE20(0.33f); gAudioContext.adsrDecayTable[254] = AudioHeap_CalculateAdsrDecay(0.33f);
gAudioContext.unk_3520[253] = func_800DDE20(0.5f); gAudioContext.adsrDecayTable[253] = AudioHeap_CalculateAdsrDecay(0.5f);
gAudioContext.unk_3520[252] = func_800DDE20(0.66f); gAudioContext.adsrDecayTable[252] = AudioHeap_CalculateAdsrDecay(0.66f);
gAudioContext.unk_3520[251] = func_800DDE20(0.75f); gAudioContext.adsrDecayTable[251] = AudioHeap_CalculateAdsrDecay(0.75f);
for (i = 128; i < 251; i++) { for (i = 128; i < 251; i++) {
gAudioContext.unk_3520[i] = func_800DDE20(251 - i); gAudioContext.adsrDecayTable[i] = AudioHeap_CalculateAdsrDecay(251 - i);
} }
for (i = 16; i < 128; i++) { for (i = 16; i < 128; i++) {
gAudioContext.unk_3520[i] = func_800DDE20(4 * (143 - i)); gAudioContext.adsrDecayTable[i] = AudioHeap_CalculateAdsrDecay(4 * (143 - i));
} }
for (i = 1; i < 16; i++) { for (i = 1; i < 16; i++) {
gAudioContext.unk_3520[i] = func_800DDE20(60 * (23 - i)); gAudioContext.adsrDecayTable[i] = AudioHeap_CalculateAdsrDecay(60 * (23 - i));
} }
gAudioContext.unk_3520[0] = 0.0f; gAudioContext.adsrDecayTable[0] = 0.0f;
} }
void AudioHeap_ResetLoadStatus(void) { void AudioHeap_ResetLoadStatus(void) {
@ -826,9 +834,10 @@ void AudioHeap_Init(void) {
gAudioContext.audioBufferParameters.samplesPerUpdateMax = gAudioContext.audioBufferParameters.samplesPerUpdate + 8; gAudioContext.audioBufferParameters.samplesPerUpdateMax = gAudioContext.audioBufferParameters.samplesPerUpdate + 8;
gAudioContext.audioBufferParameters.samplesPerUpdateMin = gAudioContext.audioBufferParameters.samplesPerUpdate - 8; gAudioContext.audioBufferParameters.samplesPerUpdateMin = gAudioContext.audioBufferParameters.samplesPerUpdate - 8;
gAudioContext.audioBufferParameters.resampleRate = 32000.0f / (s32)gAudioContext.audioBufferParameters.frequency; gAudioContext.audioBufferParameters.resampleRate = 32000.0f / (s32)gAudioContext.audioBufferParameters.frequency;
gAudioContext.audioBufferParameters.unkUpdatesPerFrameScaled = gAudioContext.audioBufferParameters.updatesPerFrameInvScaled =
(1.0f / 256.0f) / gAudioContext.audioBufferParameters.updatesPerFrame; (1.0f / 256.0f) / gAudioContext.audioBufferParameters.updatesPerFrame;
gAudioContext.audioBufferParameters.unk_24 = gAudioContext.audioBufferParameters.updatesPerFrame * 0.25f; gAudioContext.audioBufferParameters.updatesPerFrameScaled =
gAudioContext.audioBufferParameters.updatesPerFrame / 4.0f;
gAudioContext.audioBufferParameters.updatesPerFrameInv = 1.0f / gAudioContext.audioBufferParameters.updatesPerFrame; gAudioContext.audioBufferParameters.updatesPerFrameInv = 1.0f / gAudioContext.audioBufferParameters.updatesPerFrame;
gAudioContext.sampleDmaBufSize1 = spec->sampleDmaBufSize1; gAudioContext.sampleDmaBufSize1 = spec->sampleDmaBufSize1;
gAudioContext.sampleDmaBufSize2 = spec->sampleDmaBufSize2; gAudioContext.sampleDmaBufSize2 = spec->sampleDmaBufSize2;
@ -898,8 +907,10 @@ void AudioHeap_Init(void) {
gAudioContext.maxAudioCmds * sizeof(u64)); gAudioContext.maxAudioCmds * sizeof(u64));
} }
gAudioContext.unk_3520 = AudioHeap_Alloc(&gAudioContext.notesAndBuffersPool, 0x100 * sizeof(f32)); // Initialize the decay rate table for adsr
func_800DDE3C(); gAudioContext.adsrDecayTable = AudioHeap_Alloc(&gAudioContext.notesAndBuffersPool, 256 * sizeof(f32));
AudioHeap_InitAdsrDecayTable();
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
gAudioContext.synthesisReverbs[i].useReverb = 0; gAudioContext.synthesisReverbs[i].useReverb = 0;
} }

View file

@ -125,7 +125,7 @@ void Audio_NoteSetResamplingRate(NoteSubEu* noteSubEu, f32 resamplingRateInput)
} }
void Audio_NoteInit(Note* note) { void Audio_NoteInit(Note* note) {
if (note->playbackState.parentLayer->adsr.releaseRate == 0) { if (note->playbackState.parentLayer->adsr.decayIndex == 0) {
Audio_AdsrInit(&note->playbackState.adsr, note->playbackState.parentLayer->channel->adsr.envelope, Audio_AdsrInit(&note->playbackState.adsr, note->playbackState.parentLayer->channel->adsr.envelope,
&note->playbackState.adsrVolScaleUnused); &note->playbackState.adsrVolScaleUnused);
} else { } else {
@ -506,10 +506,10 @@ void Audio_SeqLayerDecayRelease(SequenceLayer* layer, s32 target) {
} else { } else {
note->playbackState.unk_04 = 1; note->playbackState.unk_04 = 1;
note->playbackState.adsr.action.s.decay = true; note->playbackState.adsr.action.s.decay = true;
if (layer->adsr.releaseRate == 0) { if (layer->adsr.decayIndex == 0) {
note->playbackState.adsr.fadeOutVel = gAudioContext.unk_3520[layer->channel->adsr.releaseRate]; note->playbackState.adsr.fadeOutVel = gAudioContext.adsrDecayTable[layer->channel->adsr.decayIndex];
} else { } else {
note->playbackState.adsr.fadeOutVel = gAudioContext.unk_3520[layer->adsr.releaseRate]; note->playbackState.adsr.fadeOutVel = gAudioContext.adsrDecayTable[layer->adsr.decayIndex];
} }
note->playbackState.adsr.sustain = note->playbackState.adsr.sustain =
((f32)(s32)(layer->channel->adsr.sustain) * note->playbackState.adsr.current) / 256.0f; ((f32)(s32)(layer->channel->adsr.sustain) * note->playbackState.adsr.current) / 256.0f;

View file

@ -139,7 +139,7 @@ void AudioSeq_InitSequenceChannel(SequenceChannel* channel) {
channel->someOtherPriority = 1; channel->someOtherPriority = 1;
channel->delay = 0; channel->delay = 0;
channel->adsr.envelope = gDefaultEnvelope; channel->adsr.envelope = gDefaultEnvelope;
channel->adsr.releaseRate = 0xF0; channel->adsr.decayIndex = 0xF0;
channel->adsr.sustain = 0; channel->adsr.sustain = 0;
channel->vibratoRateTarget = 0x800; channel->vibratoRateTarget = 0x800;
channel->vibratoRateStart = 0x800; channel->vibratoRateStart = 0x800;
@ -181,7 +181,7 @@ s32 AudioSeq_SeqChannelSetLayer(SequenceChannel* channel, s32 layerIdx) {
layer = channel->layers[layerIdx]; layer = channel->layers[layerIdx];
layer->channel = channel; layer->channel = channel;
layer->adsr = channel->adsr; layer->adsr = channel->adsr;
layer->adsr.releaseRate = 0; layer->adsr.decayIndex = 0;
layer->enabled = true; layer->enabled = true;
layer->finished = false; layer->finished = false;
layer->stopSomething = false; layer->stopSomething = false;
@ -543,7 +543,7 @@ s32 AudioSeq_SeqLayerProcessScriptStep2(SequenceLayer* layer) {
} }
if (cmd == 0xFF) { if (cmd == 0xFF) {
layer->adsr.releaseRate = 0; layer->adsr.decayIndex = 0;
} }
break; break;
@ -588,7 +588,7 @@ s32 AudioSeq_SeqLayerProcessScriptStep2(SequenceLayer* layer) {
// fallthrough // fallthrough
case 0xCF: case 0xCF:
layer->adsr.releaseRate = AudioSeq_ScriptReadU8(state); layer->adsr.decayIndex = AudioSeq_ScriptReadU8(state);
break; break;
case 0xCC: case 0xCC:
@ -664,7 +664,7 @@ s32 AudioSeq_SeqLayerProcessScriptStep4(SequenceLayer* layer, s32 cmd) {
} }
sound = &drum->sound; sound = &drum->sound;
layer->adsr.envelope = (AdsrEnvelope*)drum->envelope; layer->adsr.envelope = (AdsrEnvelope*)drum->envelope;
layer->adsr.releaseRate = (u8)drum->releaseRate; layer->adsr.decayIndex = (u8)drum->adsrDecayIndex;
if (!layer->ignoreDrumPan) { if (!layer->ignoreDrumPan) {
layer->pan = drum->pan; layer->pan = drum->pan;
} }
@ -937,7 +937,7 @@ u8 AudioSeq_GetInstrument(SequenceChannel* channel, u8 instId, Instrument** inst
return 0; return 0;
} }
adsr->envelope = inst->envelope; adsr->envelope = inst->envelope;
adsr->releaseRate = inst->releaseRate; adsr->decayIndex = inst->adsrDecayIndex;
*instOut = inst; *instOut = inst;
instId += 2; instId += 2;
return instId; return instId;
@ -1118,7 +1118,7 @@ void AudioSeq_SequenceChannelProcessScript(SequenceChannel* channel) {
break; break;
case 0xD9: case 0xD9:
command = (u8)parameters[0]; command = (u8)parameters[0];
channel->adsr.releaseRate = command; channel->adsr.decayIndex = command;
break; break;
case 0xD8: case 0xD8:
command = (u8)parameters[0]; command = (u8)parameters[0];