2020-10-03 15:22:44 +00:00
|
|
|
#include "global.h"
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2021-08-31 22:53:35 +00:00
|
|
|
void Audio_InitNoteSub(Note* note, NoteSubEu* sub, NoteSubAttributes* attrs) {
|
2020-04-21 08:19:33 +00:00
|
|
|
f32 volRight, volLeft;
|
2020-05-14 19:25:54 +00:00
|
|
|
s32 smallPanIndex;
|
|
|
|
u64 pad;
|
|
|
|
u8 strongLeft;
|
|
|
|
u8 strongRight;
|
|
|
|
f32 vel;
|
|
|
|
u8 pan;
|
|
|
|
u8 reverbVol;
|
2021-08-31 22:53:35 +00:00
|
|
|
StereoData sp24;
|
2020-05-14 19:25:54 +00:00
|
|
|
s32 stereoHeadsetEffects = note->playbackState.stereoHeadsetEffects;
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2021-08-31 22:53:35 +00:00
|
|
|
vel = attrs->velocity;
|
|
|
|
pan = attrs->pan;
|
|
|
|
reverbVol = attrs->reverbVol;
|
|
|
|
sp24 = attrs->stereo.s;
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
sub->bitField0 = note->noteSubEu.bitField0;
|
|
|
|
sub->bitField1 = note->noteSubEu.bitField1;
|
2020-05-14 19:25:54 +00:00
|
|
|
sub->sound.samples = note->noteSubEu.sound.samples;
|
|
|
|
sub->unk_06 = note->noteSubEu.unk_06;
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2021-08-31 22:53:35 +00:00
|
|
|
Audio_NoteSetResamplingRate(sub, attrs->frequency);
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2020-05-14 19:25:54 +00:00
|
|
|
pan &= 0x7F;
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
sub->bitField0.stereoStrongRight = false;
|
|
|
|
sub->bitField0.stereoStrongLeft = false;
|
|
|
|
sub->bitField0.stereoHeadsetEffects = sp24.stereoHeadsetEffects;
|
|
|
|
sub->bitField0.usesHeadsetPanEffects = sp24.usesHeadsetPanEffects;
|
2021-07-27 23:44:58 +00:00
|
|
|
if (stereoHeadsetEffects && gAudioContext.soundMode == 1) {
|
2020-05-14 19:25:54 +00:00
|
|
|
smallPanIndex = pan >> 1;
|
2021-10-03 03:17:09 +00:00
|
|
|
if (smallPanIndex > 0x3F) {
|
|
|
|
smallPanIndex = 0x3F;
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub->headsetPanLeft = gHeadsetPanQuantization[smallPanIndex];
|
2021-10-03 03:17:09 +00:00
|
|
|
sub->headsetPanRight = gHeadsetPanQuantization[0x3F - smallPanIndex];
|
2021-11-07 16:58:50 +00:00
|
|
|
sub->bitField1.usesHeadsetPanEffects2 = true;
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2020-05-14 19:25:54 +00:00
|
|
|
volLeft = gHeadsetPanVolume[pan];
|
2021-10-03 03:17:09 +00:00
|
|
|
volRight = gHeadsetPanVolume[0x7F - pan];
|
2021-07-27 23:44:58 +00:00
|
|
|
} else if (stereoHeadsetEffects && gAudioContext.soundMode == 0) {
|
2020-05-14 19:25:54 +00:00
|
|
|
strongLeft = strongRight = 0;
|
2020-04-21 08:19:33 +00:00
|
|
|
sub->headsetPanRight = 0;
|
|
|
|
sub->headsetPanLeft = 0;
|
2021-11-07 16:58:50 +00:00
|
|
|
sub->bitField1.usesHeadsetPanEffects2 = false;
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2020-05-14 19:25:54 +00:00
|
|
|
volLeft = gStereoPanVolume[pan];
|
2021-10-03 03:17:09 +00:00
|
|
|
volRight = gStereoPanVolume[0x7F - pan];
|
2020-05-14 19:25:54 +00:00
|
|
|
if (pan < 0x20) {
|
2020-04-21 08:19:33 +00:00
|
|
|
strongLeft = 1;
|
2020-05-14 19:25:54 +00:00
|
|
|
} else if (pan > 0x60) {
|
2020-04-21 08:19:33 +00:00
|
|
|
strongRight = 1;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
sub->bitField0.stereoStrongRight = strongRight;
|
|
|
|
sub->bitField0.stereoStrongLeft = strongLeft;
|
2020-04-21 08:19:33 +00:00
|
|
|
|
|
|
|
switch (sp24.bit2) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
2021-11-07 16:58:50 +00:00
|
|
|
sub->bitField0.stereoStrongRight = sp24.strongRight;
|
|
|
|
sub->bitField0.stereoStrongLeft = sp24.strongLeft;
|
2020-04-21 08:19:33 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2021-11-07 16:58:50 +00:00
|
|
|
sub->bitField0.stereoStrongRight = sp24.strongRight | strongRight;
|
|
|
|
sub->bitField0.stereoStrongLeft = sp24.strongLeft | strongLeft;
|
2020-04-21 08:19:33 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2021-11-07 16:58:50 +00:00
|
|
|
sub->bitField0.stereoStrongRight = sp24.strongRight ^ strongRight;
|
|
|
|
sub->bitField0.stereoStrongLeft = sp24.strongLeft ^ strongLeft;
|
2020-04-21 08:19:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-07-27 23:44:58 +00:00
|
|
|
} else if (gAudioContext.soundMode == 3) {
|
2021-11-07 16:58:50 +00:00
|
|
|
sub->bitField0.stereoHeadsetEffects = false;
|
|
|
|
sub->bitField0.usesHeadsetPanEffects = false;
|
2021-11-01 23:29:47 +00:00
|
|
|
volLeft = 0.707f; // approx 1/sqrt(2)
|
2020-04-21 08:19:33 +00:00
|
|
|
volRight = 0.707f;
|
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
sub->bitField0.stereoStrongRight = sp24.strongRight;
|
|
|
|
sub->bitField0.stereoStrongLeft = sp24.strongLeft;
|
2020-05-14 19:25:54 +00:00
|
|
|
volLeft = gDefaultPanVolume[pan];
|
2021-10-03 03:17:09 +00:00
|
|
|
volRight = gDefaultPanVolume[0x7F - pan];
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vel = 0.0f > vel ? 0.0f : vel;
|
|
|
|
vel = 1.0f < vel ? 1.0f : vel;
|
|
|
|
|
2021-11-01 23:29:47 +00:00
|
|
|
sub->targetVolLeft = (s32)((vel * volLeft) * (0x1000 - 0.001f));
|
|
|
|
sub->targetVolRight = (s32)((vel * volRight) * (0x1000 - 0.001f));
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2021-08-31 22:53:35 +00:00
|
|
|
sub->unk_2 = attrs->unk_1;
|
|
|
|
sub->filter = attrs->filter;
|
|
|
|
sub->unk_07 = attrs->unk_14;
|
|
|
|
sub->unk_0E = attrs->unk_16;
|
2020-05-14 19:25:54 +00:00
|
|
|
sub->reverbVol = reverbVol;
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Audio_NoteSetResamplingRate(NoteSubEu* noteSubEu, f32 resamplingRateInput) {
|
|
|
|
f32 resamplingRate = 0.0f;
|
|
|
|
|
|
|
|
if (resamplingRateInput < 2.0f) {
|
2021-11-07 16:58:50 +00:00
|
|
|
noteSubEu->bitField1.hasTwoParts = false;
|
2020-04-21 08:19:33 +00:00
|
|
|
|
|
|
|
if (1.99998f < resamplingRateInput) {
|
|
|
|
resamplingRate = 1.99998f;
|
|
|
|
} else {
|
|
|
|
resamplingRate = resamplingRateInput;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
noteSubEu->bitField1.hasTwoParts = true;
|
2020-04-21 08:19:33 +00:00
|
|
|
if (3.99996f < resamplingRateInput) {
|
|
|
|
resamplingRate = 1.99998f;
|
|
|
|
} else {
|
|
|
|
resamplingRate = resamplingRateInput * 0.5f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
noteSubEu->resamplingRateFixedPoint = (s32)(resamplingRate * 32768.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio_NoteInit(Note* note) {
|
|
|
|
if (note->playbackState.parentLayer->adsr.releaseRate == 0) {
|
2021-11-07 16:58:50 +00:00
|
|
|
Audio_AdsrInit(¬e->playbackState.adsr, note->playbackState.parentLayer->channel->adsr.envelope,
|
|
|
|
¬e->playbackState.adsrVolScaleUnused);
|
2020-04-21 08:19:33 +00:00
|
|
|
} else {
|
|
|
|
Audio_AdsrInit(¬e->playbackState.adsr, note->playbackState.parentLayer->adsr.envelope,
|
2021-11-07 16:58:50 +00:00
|
|
|
¬e->playbackState.adsrVolScaleUnused);
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
note->playbackState.unk_04 = 0;
|
2020-08-15 18:06:26 +00:00
|
|
|
note->playbackState.adsr.action.s.state = ADSR_STATE_INITIAL;
|
2020-04-21 08:19:33 +00:00
|
|
|
note->noteSubEu = gDefaultNoteSub;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio_NoteDisable(Note* note) {
|
2021-11-07 16:58:50 +00:00
|
|
|
if (note->noteSubEu.bitField0.needsInit == true) {
|
|
|
|
note->noteSubEu.bitField0.needsInit = false;
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
note->playbackState.priority = 0;
|
2021-11-07 16:58:50 +00:00
|
|
|
note->noteSubEu.bitField0.enabled = false;
|
2020-04-21 08:19:33 +00:00
|
|
|
note->playbackState.unk_04 = 0;
|
2021-11-07 16:58:50 +00:00
|
|
|
note->noteSubEu.bitField0.finished = false;
|
2020-04-21 08:19:33 +00:00
|
|
|
note->playbackState.parentLayer = NO_LAYER;
|
|
|
|
note->playbackState.prevParentLayer = NO_LAYER;
|
2020-08-15 18:06:26 +00:00
|
|
|
note->playbackState.adsr.action.s.state = ADSR_STATE_DISABLED;
|
|
|
|
note->playbackState.adsr.current = 0;
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 18:06:26 +00:00
|
|
|
void Audio_ProcessNotes(void) {
|
|
|
|
s32 pad[2];
|
2021-08-31 22:53:35 +00:00
|
|
|
NoteAttributes* attrs;
|
2020-08-15 18:06:26 +00:00
|
|
|
NoteSubEu* noteSubEu2;
|
|
|
|
NoteSubEu* noteSubEu;
|
|
|
|
Note* note;
|
|
|
|
NotePlaybackState* playbackState;
|
2021-08-31 22:53:35 +00:00
|
|
|
NoteSubAttributes subAttrs;
|
2020-08-15 18:06:26 +00:00
|
|
|
u8 bookOffset;
|
|
|
|
f32 scale;
|
|
|
|
s32 i;
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
for (i = 0; i < gAudioContext.numNotes; i++) {
|
2021-07-27 23:44:58 +00:00
|
|
|
note = &gAudioContext.notes[i];
|
|
|
|
noteSubEu2 = &gAudioContext.noteSubsEu[gAudioContext.noteSubEuOffset + i];
|
2020-08-15 18:06:26 +00:00
|
|
|
playbackState = ¬e->playbackState;
|
|
|
|
if (playbackState->parentLayer != NO_LAYER) {
|
2021-10-03 03:17:09 +00:00
|
|
|
if ((u32)playbackState->parentLayer < 0x7FFFFFFF) {
|
2020-08-15 18:06:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (note != playbackState->parentLayer->note && playbackState->unk_04 == 0) {
|
2020-09-20 17:22:09 +00:00
|
|
|
playbackState->adsr.action.s.release = true;
|
2021-07-27 23:44:58 +00:00
|
|
|
playbackState->adsr.fadeOutVel = gAudioContext.audioBufferParameters.updatesPerFrameInv;
|
2020-08-15 18:06:26 +00:00
|
|
|
playbackState->priority = 1;
|
|
|
|
playbackState->unk_04 = 2;
|
|
|
|
goto out;
|
|
|
|
} else if (!playbackState->parentLayer->enabled && playbackState->unk_04 == 0 &&
|
|
|
|
playbackState->priority >= 1) {
|
|
|
|
// do nothing
|
2021-11-07 16:58:50 +00:00
|
|
|
} else if (playbackState->parentLayer->channel->seqPlayer == NULL) {
|
|
|
|
AudioSeq_SequenceChannelDisable(playbackState->parentLayer->channel);
|
2020-08-15 18:06:26 +00:00
|
|
|
playbackState->priority = 1;
|
|
|
|
playbackState->unk_04 = 1;
|
|
|
|
continue;
|
2021-11-07 16:58:50 +00:00
|
|
|
} else if (playbackState->parentLayer->channel->seqPlayer->muted &&
|
|
|
|
(playbackState->parentLayer->channel->muteBehavior & 0x40)) {
|
2020-08-15 18:06:26 +00:00
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
Audio_SeqLayerNoteRelease(playbackState->parentLayer);
|
2020-08-15 18:06:26 +00:00
|
|
|
Audio_AudioListRemove(¬e->listItem);
|
|
|
|
Audio_AudioListPushFront(¬e->listItem.pool->decaying, ¬e->listItem);
|
|
|
|
playbackState->priority = 1;
|
|
|
|
playbackState->unk_04 = 2;
|
|
|
|
} else if (playbackState->unk_04 == 0 && playbackState->priority >= 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (playbackState->priority != 0) {
|
|
|
|
if (1) {}
|
|
|
|
noteSubEu = ¬e->noteSubEu;
|
2021-11-07 16:58:50 +00:00
|
|
|
if (playbackState->unk_04 >= 1 || noteSubEu->bitField0.finished) {
|
|
|
|
if (playbackState->adsr.action.s.state == ADSR_STATE_DISABLED || noteSubEu->bitField0.finished) {
|
2020-08-15 18:06:26 +00:00
|
|
|
if (playbackState->wantedParentLayer != NO_LAYER) {
|
|
|
|
Audio_NoteDisable(note);
|
2021-11-07 16:58:50 +00:00
|
|
|
if (playbackState->wantedParentLayer->channel != NULL) {
|
2020-08-15 18:06:26 +00:00
|
|
|
Audio_NoteInitForLayer(note, playbackState->wantedParentLayer);
|
|
|
|
Audio_NoteVibratoInit(note);
|
|
|
|
Audio_NotePortamentoInit(note);
|
|
|
|
Audio_AudioListRemove(¬e->listItem);
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioSeq_AudioListPushBack(¬e->listItem.pool->active, ¬e->listItem);
|
2020-08-15 18:06:26 +00:00
|
|
|
playbackState->wantedParentLayer = NO_LAYER;
|
|
|
|
// don't skip
|
|
|
|
} else {
|
|
|
|
Audio_NoteDisable(note);
|
|
|
|
Audio_AudioListRemove(¬e->listItem);
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioSeq_AudioListPushBack(¬e->listItem.pool->disabled, ¬e->listItem);
|
2020-08-15 18:06:26 +00:00
|
|
|
playbackState->wantedParentLayer = NO_LAYER;
|
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (playbackState->parentLayer != NO_LAYER) {
|
2020-09-20 17:22:09 +00:00
|
|
|
playbackState->parentLayer->bit1 = true;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
Audio_NoteDisable(note);
|
|
|
|
Audio_AudioListRemove(¬e->listItem);
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioSeq_AudioListPushBack(¬e->listItem.pool->disabled, ¬e->listItem);
|
2020-08-15 18:06:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (playbackState->adsr.action.s.state == ADSR_STATE_DISABLED) {
|
|
|
|
if (playbackState->parentLayer != NO_LAYER) {
|
2020-09-20 17:22:09 +00:00
|
|
|
playbackState->parentLayer->bit1 = true;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
Audio_NoteDisable(note);
|
|
|
|
Audio_AudioListRemove(¬e->listItem);
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioSeq_AudioListPushBack(¬e->listItem.pool->disabled, ¬e->listItem);
|
2020-08-15 18:06:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
scale = Audio_AdsrUpdate(&playbackState->adsr);
|
|
|
|
Audio_NoteVibratoUpdate(note);
|
2021-08-31 22:53:35 +00:00
|
|
|
attrs = &playbackState->attributes;
|
2020-08-15 18:06:26 +00:00
|
|
|
if (playbackState->unk_04 == 1 || playbackState->unk_04 == 2) {
|
2021-08-31 22:53:35 +00:00
|
|
|
subAttrs.frequency = attrs->freqScale;
|
|
|
|
subAttrs.velocity = attrs->velocity;
|
|
|
|
subAttrs.pan = attrs->pan;
|
|
|
|
subAttrs.reverbVol = attrs->reverb;
|
|
|
|
subAttrs.stereo = attrs->stereo;
|
|
|
|
subAttrs.unk_1 = attrs->unk_1;
|
|
|
|
subAttrs.filter = attrs->filter;
|
|
|
|
subAttrs.unk_14 = attrs->unk_4;
|
|
|
|
subAttrs.unk_16 = attrs->unk_6;
|
2021-11-07 16:58:50 +00:00
|
|
|
bookOffset = noteSubEu->bitField1.bookOffset;
|
2020-08-15 18:06:26 +00:00
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
SequenceLayer* layer = playbackState->parentLayer;
|
|
|
|
SequenceChannel* channel = layer->channel;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-08-31 22:53:35 +00:00
|
|
|
subAttrs.frequency = layer->noteFreqScale;
|
|
|
|
subAttrs.velocity = layer->noteVelocity;
|
|
|
|
subAttrs.pan = layer->notePan;
|
|
|
|
if (layer->stereo.asByte == 0) {
|
|
|
|
subAttrs.stereo = channel->stereo;
|
2020-08-15 18:06:26 +00:00
|
|
|
} else {
|
2021-08-31 22:53:35 +00:00
|
|
|
subAttrs.stereo = layer->stereo;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
2021-08-31 22:53:35 +00:00
|
|
|
subAttrs.reverbVol = channel->reverb;
|
|
|
|
subAttrs.unk_1 = channel->unk_0C;
|
|
|
|
subAttrs.filter = channel->filter;
|
|
|
|
subAttrs.unk_14 = channel->unk_0F;
|
|
|
|
subAttrs.unk_16 = channel->unk_20;
|
2020-08-15 18:06:26 +00:00
|
|
|
bookOffset = channel->bookOffset & 0x7;
|
|
|
|
|
|
|
|
if (channel->seqPlayer->muted && (channel->muteBehavior & 8)) {
|
2021-08-31 22:53:35 +00:00
|
|
|
subAttrs.frequency = 0.0f;
|
|
|
|
subAttrs.velocity = 0.0f;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 22:53:35 +00:00
|
|
|
subAttrs.frequency *= playbackState->vibratoFreqScale * playbackState->portamentoFreqScale;
|
|
|
|
subAttrs.frequency *= gAudioContext.audioBufferParameters.resampleRate;
|
|
|
|
subAttrs.velocity *= scale;
|
|
|
|
Audio_InitNoteSub(note, noteSubEu2, &subAttrs);
|
2021-11-07 16:58:50 +00:00
|
|
|
noteSubEu->bitField1.bookOffset = bookOffset;
|
2020-08-15 18:06:26 +00:00
|
|
|
skip:;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
SoundFontSound* Audio_InstrumentGetSound(Instrument* instrument, s32 semitone) {
|
|
|
|
SoundFontSound* sound;
|
2020-04-21 08:19:33 +00:00
|
|
|
if (semitone < instrument->normalRangeLo) {
|
|
|
|
sound = &instrument->lowNotesSound;
|
|
|
|
} else if (semitone <= instrument->normalRangeHi) {
|
|
|
|
sound = &instrument->normalNotesSound;
|
|
|
|
} else {
|
|
|
|
sound = &instrument->highNotesSound;
|
|
|
|
}
|
|
|
|
return sound;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
Instrument* Audio_GetInstrumentInner(s32 fontId, s32 instId) {
|
2020-04-21 08:19:33 +00:00
|
|
|
Instrument* inst;
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (fontId == 0xFF) {
|
2020-04-21 08:19:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (!AudioLoad_IsFontLoadComplete(fontId)) {
|
|
|
|
gAudioContext.audioErrorFlags = fontId + 0x10000000;
|
2020-04-21 08:19:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (instId >= gAudioContext.soundFonts[fontId].numInstruments) {
|
|
|
|
gAudioContext.audioErrorFlags = ((fontId << 8) + instId) + 0x3000000;
|
2020-04-21 08:19:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
inst = gAudioContext.soundFonts[fontId].instruments[instId];
|
2020-04-21 08:19:33 +00:00
|
|
|
if (inst == NULL) {
|
2021-11-07 16:58:50 +00:00
|
|
|
gAudioContext.audioErrorFlags = ((fontId << 8) + instId) + 0x1000000;
|
2020-04-21 08:19:33 +00:00
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
Drum* Audio_GetDrum(s32 fontId, s32 drumId) {
|
2020-04-21 08:19:33 +00:00
|
|
|
Drum* drum;
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (fontId == 0xFF) {
|
2020-04-21 08:19:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (!AudioLoad_IsFontLoadComplete(fontId)) {
|
|
|
|
gAudioContext.audioErrorFlags = fontId + 0x10000000;
|
2020-04-21 08:19:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (drumId >= gAudioContext.soundFonts[fontId].numDrums) {
|
|
|
|
gAudioContext.audioErrorFlags = ((fontId << 8) + drumId) + 0x4000000;
|
2020-04-21 08:19:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if ((u32)gAudioContext.soundFonts[fontId].drums < 0x80000000) {
|
2020-04-21 08:19:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
drum = gAudioContext.soundFonts[fontId].drums[drumId];
|
2020-04-21 08:19:33 +00:00
|
|
|
|
|
|
|
if (drum == NULL) {
|
2021-11-07 16:58:50 +00:00
|
|
|
gAudioContext.audioErrorFlags = ((fontId << 8) + drumId) + 0x5000000;
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return drum;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
SoundFontSound* Audio_GetSfx(s32 fontId, s32 sfxId) {
|
|
|
|
SoundFontSound* sfx;
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (fontId == 0xFF) {
|
2020-04-21 08:19:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (!AudioLoad_IsFontLoadComplete(fontId)) {
|
|
|
|
gAudioContext.audioErrorFlags = fontId + 0x10000000;
|
2020-04-21 08:19:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (sfxId >= gAudioContext.soundFonts[fontId].numSfx) {
|
|
|
|
gAudioContext.audioErrorFlags = ((fontId << 8) + sfxId) + 0x4000000;
|
2020-04-21 08:19:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if ((u32)gAudioContext.soundFonts[fontId].soundEffects < 0x80000000) {
|
2020-04-21 08:19:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
sfx = &gAudioContext.soundFonts[fontId].soundEffects[sfxId];
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2020-09-20 17:22:09 +00:00
|
|
|
if (sfx == NULL) {
|
2021-11-07 16:58:50 +00:00
|
|
|
gAudioContext.audioErrorFlags = ((fontId << 8) + sfxId) + 0x5000000;
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
2020-09-20 17:22:09 +00:00
|
|
|
if (sfx->sample == NULL) {
|
2020-04-21 08:19:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-09-20 17:22:09 +00:00
|
|
|
return sfx;
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
s32 Audio_SetFontInstrument(s32 instrumentType, s32 fontId, s32 index, void* value) {
|
|
|
|
if (fontId == 0xFF) {
|
2020-04-21 08:19:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (!AudioLoad_IsFontLoadComplete(fontId)) {
|
2020-04-21 08:19:33 +00:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
switch (instrumentType) {
|
2020-04-21 08:19:33 +00:00
|
|
|
case 0:
|
2021-11-07 16:58:50 +00:00
|
|
|
if (index >= gAudioContext.soundFonts[fontId].numDrums) {
|
2020-04-21 08:19:33 +00:00
|
|
|
return -3;
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
gAudioContext.soundFonts[fontId].drums[index] = value;
|
2020-04-21 08:19:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2021-11-07 16:58:50 +00:00
|
|
|
if (index >= gAudioContext.soundFonts[fontId].numSfx) {
|
2020-04-21 08:19:33 +00:00
|
|
|
return -3;
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
gAudioContext.soundFonts[fontId].soundEffects[index] = *(SoundFontSound*)value;
|
2020-04-21 08:19:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-11-07 16:58:50 +00:00
|
|
|
if (index >= gAudioContext.soundFonts[fontId].numInstruments) {
|
2020-04-21 08:19:33 +00:00
|
|
|
return -3;
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
gAudioContext.soundFonts[fontId].instruments[index] = value;
|
2020-04-21 08:19:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_SeqLayerDecayRelease(SequenceLayer* layer, s32 target) {
|
2020-04-21 08:19:33 +00:00
|
|
|
Note* note;
|
2021-08-31 22:53:35 +00:00
|
|
|
NoteAttributes* attrs;
|
2020-08-15 18:06:26 +00:00
|
|
|
SequenceChannel* chan;
|
|
|
|
s32 i;
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (layer == NO_LAYER) {
|
2020-04-21 08:19:33 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-06-14 03:29:59 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
layer->bit3 = false;
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (layer->note == NULL) {
|
2020-04-21 08:19:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
note = layer->note;
|
2021-08-31 22:53:35 +00:00
|
|
|
attrs = ¬e->playbackState.attributes;
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (note->playbackState.wantedParentLayer == layer) {
|
2020-04-21 08:19:33 +00:00
|
|
|
note->playbackState.wantedParentLayer = NO_LAYER;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (note->playbackState.parentLayer != layer) {
|
2020-06-14 03:29:59 +00:00
|
|
|
if (note->playbackState.parentLayer == NO_LAYER && note->playbackState.wantedParentLayer == NO_LAYER &&
|
2021-11-07 16:58:50 +00:00
|
|
|
note->playbackState.prevParentLayer == layer && target != ADSR_STATE_DECAY) {
|
2021-07-27 23:44:58 +00:00
|
|
|
note->playbackState.adsr.fadeOutVel = gAudioContext.audioBufferParameters.updatesPerFrameInv;
|
2020-09-20 17:22:09 +00:00
|
|
|
note->playbackState.adsr.action.s.release = true;
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-15 18:06:26 +00:00
|
|
|
if (note->playbackState.adsr.action.s.state != ADSR_STATE_DECAY) {
|
2021-11-07 16:58:50 +00:00
|
|
|
attrs->freqScale = layer->noteFreqScale;
|
|
|
|
attrs->velocity = layer->noteVelocity;
|
|
|
|
attrs->pan = layer->notePan;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (layer->channel != NULL) {
|
|
|
|
chan = layer->channel;
|
2021-08-31 22:53:35 +00:00
|
|
|
attrs->reverb = chan->reverb;
|
|
|
|
attrs->unk_1 = chan->unk_0C;
|
|
|
|
attrs->filter = chan->filter;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2021-08-31 22:53:35 +00:00
|
|
|
if (attrs->filter != NULL) {
|
2020-08-15 18:06:26 +00:00
|
|
|
for (i = 0; i < 8; i++) {
|
2021-08-31 22:53:35 +00:00
|
|
|
attrs->filterBuf[i] = attrs->filter[i];
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
2021-08-31 22:53:35 +00:00
|
|
|
attrs->filter = attrs->filterBuf;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 22:53:35 +00:00
|
|
|
attrs->unk_6 = chan->unk_20;
|
|
|
|
attrs->unk_4 = chan->unk_0F;
|
2020-08-15 18:06:26 +00:00
|
|
|
if (chan->seqPlayer->muted && (chan->muteBehavior & 8)) {
|
2021-11-07 16:58:50 +00:00
|
|
|
note->noteSubEu.bitField0.finished = true;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (layer->stereo.asByte == 0) {
|
2021-08-31 22:53:35 +00:00
|
|
|
attrs->stereo = chan->stereo;
|
2020-08-15 18:06:26 +00:00
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
attrs->stereo = layer->stereo;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
note->playbackState.priority = chan->someOtherPriority;
|
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
attrs->stereo = layer->stereo;
|
2020-08-15 18:06:26 +00:00
|
|
|
note->playbackState.priority = 1;
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2020-04-21 08:19:33 +00:00
|
|
|
note->playbackState.prevParentLayer = note->playbackState.parentLayer;
|
|
|
|
note->playbackState.parentLayer = NO_LAYER;
|
2020-08-15 18:06:26 +00:00
|
|
|
if (target == ADSR_STATE_RELEASE) {
|
2021-07-27 23:44:58 +00:00
|
|
|
note->playbackState.adsr.fadeOutVel = gAudioContext.audioBufferParameters.updatesPerFrameInv;
|
2020-09-20 17:22:09 +00:00
|
|
|
note->playbackState.adsr.action.s.release = true;
|
2020-08-15 18:06:26 +00:00
|
|
|
note->playbackState.unk_04 = 2;
|
2020-04-21 08:19:33 +00:00
|
|
|
} else {
|
2020-08-15 18:06:26 +00:00
|
|
|
note->playbackState.unk_04 = 1;
|
2020-09-20 17:22:09 +00:00
|
|
|
note->playbackState.adsr.action.s.decay = true;
|
2021-11-07 16:58:50 +00:00
|
|
|
if (layer->adsr.releaseRate == 0) {
|
|
|
|
note->playbackState.adsr.fadeOutVel = gAudioContext.unk_3520[layer->channel->adsr.releaseRate];
|
2020-04-21 08:19:33 +00:00
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
note->playbackState.adsr.fadeOutVel = gAudioContext.unk_3520[layer->adsr.releaseRate];
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
2020-06-14 03:29:59 +00:00
|
|
|
note->playbackState.adsr.sustain =
|
2021-11-07 16:58:50 +00:00
|
|
|
((f32)(s32)(layer->channel->adsr.sustain) * note->playbackState.adsr.current) / 256.0f;
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 18:06:26 +00:00
|
|
|
if (target == ADSR_STATE_DECAY) {
|
2020-04-21 08:19:33 +00:00
|
|
|
Audio_AudioListRemove(¬e->listItem);
|
|
|
|
Audio_AudioListPushFront(¬e->listItem.pool->decaying, ¬e->listItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_SeqLayerNoteDecay(SequenceLayer* layer) {
|
|
|
|
Audio_SeqLayerDecayRelease(layer, ADSR_STATE_DECAY);
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_SeqLayerNoteRelease(SequenceLayer* layer) {
|
|
|
|
Audio_SeqLayerDecayRelease(layer, ADSR_STATE_RELEASE);
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
s32 Audio_BuildSyntheticWave(Note* note, SequenceLayer* layer, s32 waveId) {
|
2020-04-21 08:19:33 +00:00
|
|
|
f32 freqScale;
|
|
|
|
f32 ratio;
|
|
|
|
u8 sampleCountIndex;
|
|
|
|
|
|
|
|
if (waveId < 128) {
|
|
|
|
waveId = 128;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
freqScale = layer->freqScale;
|
|
|
|
if (layer->portamento.mode != 0 && 0.0f < layer->portamento.extent) {
|
|
|
|
freqScale *= (layer->portamento.extent + 1.0f);
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
if (freqScale < 0.99999f) {
|
|
|
|
sampleCountIndex = 0;
|
|
|
|
ratio = 1.0465f;
|
|
|
|
} else if (freqScale < 1.99999f) {
|
|
|
|
sampleCountIndex = 1;
|
|
|
|
ratio = 0.52325f;
|
|
|
|
} else if (freqScale < 3.99999f) {
|
|
|
|
sampleCountIndex = 2;
|
|
|
|
ratio = 0.26263f;
|
|
|
|
} else {
|
|
|
|
sampleCountIndex = 3;
|
|
|
|
ratio = 0.13081f;
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
layer->freqScale *= ratio;
|
2020-04-21 08:19:33 +00:00
|
|
|
note->playbackState.waveId = waveId;
|
|
|
|
note->playbackState.sampleCountIndex = sampleCountIndex;
|
|
|
|
|
|
|
|
note->noteSubEu.sound.samples = &gWaveSamples[waveId - 128][sampleCountIndex * 64];
|
|
|
|
|
|
|
|
return sampleCountIndex;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_InitSyntheticWave(Note* note, SequenceLayer* layer) {
|
2020-04-21 08:19:33 +00:00
|
|
|
s32 sampleCountIndex;
|
|
|
|
s32 waveSampleCountIndex;
|
2021-11-07 16:58:50 +00:00
|
|
|
s32 waveId = layer->instOrWave;
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2021-10-03 03:17:09 +00:00
|
|
|
if (waveId == 0xFF) {
|
2021-11-07 16:58:50 +00:00
|
|
|
waveId = layer->channel->instOrWave;
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sampleCountIndex = note->playbackState.sampleCountIndex;
|
2021-11-07 16:58:50 +00:00
|
|
|
waveSampleCountIndex = Audio_BuildSyntheticWave(note, layer, waveId);
|
2020-04-21 08:19:33 +00:00
|
|
|
|
|
|
|
if (waveSampleCountIndex != sampleCountIndex) {
|
|
|
|
note->noteSubEu.unk_06 = waveSampleCountIndex * 4 + sampleCountIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio_InitNoteList(AudioListItem* list) {
|
|
|
|
list->prev = list;
|
|
|
|
list->next = list;
|
|
|
|
list->u.count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio_InitNoteLists(NotePool* pool) {
|
|
|
|
Audio_InitNoteList(&pool->disabled);
|
|
|
|
Audio_InitNoteList(&pool->decaying);
|
|
|
|
Audio_InitNoteList(&pool->releasing);
|
|
|
|
Audio_InitNoteList(&pool->active);
|
|
|
|
pool->disabled.pool = pool;
|
|
|
|
pool->decaying.pool = pool;
|
|
|
|
pool->releasing.pool = pool;
|
|
|
|
pool->active.pool = pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio_InitNoteFreeList(void) {
|
|
|
|
s32 i;
|
|
|
|
|
2021-07-27 23:44:58 +00:00
|
|
|
Audio_InitNoteLists(&gAudioContext.noteFreeLists);
|
2021-11-07 16:58:50 +00:00
|
|
|
for (i = 0; i < gAudioContext.numNotes; i++) {
|
2021-07-27 23:44:58 +00:00
|
|
|
gAudioContext.notes[i].listItem.u.value = &gAudioContext.notes[i];
|
|
|
|
gAudioContext.notes[i].listItem.prev = NULL;
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioSeq_AudioListPushBack(&gAudioContext.noteFreeLists.disabled, &gAudioContext.notes[i].listItem);
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio_NotePoolClear(NotePool* pool) {
|
|
|
|
s32 i;
|
|
|
|
AudioListItem* source;
|
|
|
|
AudioListItem* cur;
|
|
|
|
AudioListItem* dest;
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
switch (i) {
|
|
|
|
case 0:
|
|
|
|
source = &pool->disabled;
|
2021-07-27 23:44:58 +00:00
|
|
|
dest = &gAudioContext.noteFreeLists.disabled;
|
2020-04-21 08:19:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
source = &pool->decaying;
|
2021-07-27 23:44:58 +00:00
|
|
|
dest = &gAudioContext.noteFreeLists.decaying;
|
2020-04-21 08:19:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
source = &pool->releasing;
|
2021-07-27 23:44:58 +00:00
|
|
|
dest = &gAudioContext.noteFreeLists.releasing;
|
2020-04-21 08:19:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
source = &pool->active;
|
2021-07-27 23:44:58 +00:00
|
|
|
dest = &gAudioContext.noteFreeLists.active;
|
2020-04-21 08:19:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
cur = source->next;
|
|
|
|
if (cur == source || cur == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Audio_AudioListRemove(cur);
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioSeq_AudioListPushBack(dest, cur);
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio_NotePoolFill(NotePool* pool, s32 count) {
|
|
|
|
s32 i;
|
|
|
|
s32 j;
|
|
|
|
Note* note;
|
|
|
|
AudioListItem* source;
|
|
|
|
AudioListItem* dest;
|
|
|
|
|
|
|
|
Audio_NotePoolClear(pool);
|
|
|
|
|
|
|
|
for (i = 0, j = 0; j < count; i++) {
|
|
|
|
if (i == 4) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (i) {
|
|
|
|
case 0:
|
2021-07-27 23:44:58 +00:00
|
|
|
source = &gAudioContext.noteFreeLists.disabled;
|
2020-04-21 08:19:33 +00:00
|
|
|
dest = &pool->disabled;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2021-07-27 23:44:58 +00:00
|
|
|
source = &gAudioContext.noteFreeLists.decaying;
|
2020-04-21 08:19:33 +00:00
|
|
|
dest = &pool->decaying;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2021-07-27 23:44:58 +00:00
|
|
|
source = &gAudioContext.noteFreeLists.releasing;
|
2020-04-21 08:19:33 +00:00
|
|
|
dest = &pool->releasing;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2021-07-27 23:44:58 +00:00
|
|
|
source = &gAudioContext.noteFreeLists.active;
|
2020-04-21 08:19:33 +00:00
|
|
|
dest = &pool->active;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (j < count) {
|
2021-11-07 16:58:50 +00:00
|
|
|
note = AudioSeq_AudioListPopBack(source);
|
2020-04-21 08:19:33 +00:00
|
|
|
if (note == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioSeq_AudioListPushBack(dest, ¬e->listItem);
|
2020-04-21 08:19:33 +00:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio_AudioListPushFront(AudioListItem* list, AudioListItem* item) {
|
|
|
|
// add 'item' to the front of the list given by 'list', if it's not in any list
|
|
|
|
if (item->prev == NULL) {
|
|
|
|
item->prev = list;
|
|
|
|
item->next = list->next;
|
|
|
|
list->next->prev = item;
|
|
|
|
list->next = item;
|
|
|
|
list->u.count++;
|
|
|
|
item->pool = list->pool;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio_AudioListRemove(AudioListItem* item) {
|
|
|
|
// remove 'item' from the list it's in, if any
|
|
|
|
if (item->prev != NULL) {
|
|
|
|
item->prev->next = item->next;
|
|
|
|
item->next->prev = item->prev;
|
|
|
|
item->prev = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
Note* Audio_FindNodeWithPrioLessThan(AudioListItem* list, s32 limit) {
|
2020-04-21 08:19:33 +00:00
|
|
|
AudioListItem* cur = list->next;
|
|
|
|
AudioListItem* best;
|
|
|
|
|
|
|
|
if (cur == list) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (best = cur; cur != list; cur = cur->next) {
|
2020-04-28 04:14:27 +00:00
|
|
|
if (((Note*)best->u.value)->playbackState.priority >= ((Note*)cur->u.value)->playbackState.priority) {
|
2020-04-21 08:19:33 +00:00
|
|
|
best = cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (best == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (limit <= ((Note*)best->u.value)->playbackState.priority) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return best->u.value;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_NoteInitForLayer(Note* note, SequenceLayer* layer) {
|
2020-04-21 08:19:33 +00:00
|
|
|
s32 pad[3];
|
|
|
|
s16 instId;
|
|
|
|
NotePlaybackState* playback = ¬e->playbackState;
|
|
|
|
NoteSubEu* sub = ¬e->noteSubEu;
|
|
|
|
|
|
|
|
note->playbackState.prevParentLayer = NO_LAYER;
|
2021-11-07 16:58:50 +00:00
|
|
|
note->playbackState.parentLayer = layer;
|
|
|
|
playback->priority = layer->channel->notePriority;
|
|
|
|
layer->notePropertiesNeedInit = true;
|
|
|
|
layer->bit3 = true;
|
|
|
|
layer->note = note;
|
|
|
|
layer->channel->noteUnused = note;
|
|
|
|
layer->channel->layerUnused = layer;
|
|
|
|
layer->noteVelocity = 0.0f;
|
2020-04-21 08:19:33 +00:00
|
|
|
Audio_NoteInit(note);
|
2021-11-07 16:58:50 +00:00
|
|
|
instId = layer->instOrWave;
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2021-10-03 03:17:09 +00:00
|
|
|
if (instId == 0xFF) {
|
2021-11-07 16:58:50 +00:00
|
|
|
instId = layer->channel->instOrWave;
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
sub->sound.soundFontSound = layer->sound;
|
2020-04-21 08:19:33 +00:00
|
|
|
|
|
|
|
if (instId >= 0x80 && instId < 0xC0) {
|
2021-11-07 16:58:50 +00:00
|
|
|
sub->bitField1.isSyntheticWave = true;
|
2020-04-21 08:19:33 +00:00
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
sub->bitField1.isSyntheticWave = false;
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (sub->bitField1.isSyntheticWave) {
|
|
|
|
Audio_BuildSyntheticWave(note, layer, instId);
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
playback->fontId = layer->channel->fontId;
|
|
|
|
playback->stereoHeadsetEffects = layer->channel->stereoHeadsetEffects;
|
|
|
|
sub->bitField1.reverbIndex = layer->channel->reverbIndex & 3;
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void func_800E82C0(Note* note, SequenceLayer* layer) {
|
|
|
|
// similar to Audio_NoteReleaseAndTakeOwnership, hard to say what the difference is
|
|
|
|
Audio_SeqLayerNoteRelease(note->playbackState.parentLayer);
|
|
|
|
note->playbackState.wantedParentLayer = layer;
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
void Audio_NoteReleaseAndTakeOwnership(Note* note, SequenceLayer* layer) {
|
|
|
|
note->playbackState.wantedParentLayer = layer;
|
|
|
|
note->playbackState.priority = layer->channel->notePriority;
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2021-07-27 23:44:58 +00:00
|
|
|
note->playbackState.adsr.fadeOutVel = gAudioContext.audioBufferParameters.updatesPerFrameInv;
|
2020-09-20 17:22:09 +00:00
|
|
|
note->playbackState.adsr.action.s.release = true;
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
Note* Audio_AllocNoteFromDisabled(NotePool* pool, SequenceLayer* layer) {
|
|
|
|
Note* note = AudioSeq_AudioListPopBack(&pool->disabled);
|
2020-04-21 08:19:33 +00:00
|
|
|
if (note != NULL) {
|
2021-11-07 16:58:50 +00:00
|
|
|
Audio_NoteInitForLayer(note, layer);
|
2020-04-21 08:19:33 +00:00
|
|
|
Audio_AudioListPushFront(&pool->active, ¬e->listItem);
|
|
|
|
}
|
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
Note* Audio_AllocNoteFromDecaying(NotePool* pool, SequenceLayer* layer) {
|
|
|
|
Note* note = AudioSeq_AudioListPopBack(&pool->decaying);
|
2020-04-21 08:19:33 +00:00
|
|
|
if (note != NULL) {
|
2021-11-07 16:58:50 +00:00
|
|
|
Audio_NoteReleaseAndTakeOwnership(note, layer);
|
|
|
|
AudioSeq_AudioListPushBack(&pool->releasing, ¬e->listItem);
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
Note* Audio_AllocNoteFromActive(NotePool* pool, SequenceLayer* layer) {
|
2020-04-21 08:19:33 +00:00
|
|
|
Note* rNote;
|
|
|
|
Note* aNote;
|
2021-02-14 00:49:40 +00:00
|
|
|
s32 rPriority;
|
|
|
|
s32 aPriority;
|
2020-04-21 08:19:33 +00:00
|
|
|
|
2021-02-14 00:49:40 +00:00
|
|
|
rPriority = aPriority = 0x10;
|
2021-11-07 16:58:50 +00:00
|
|
|
rNote = Audio_FindNodeWithPrioLessThan(&pool->releasing, layer->channel->notePriority);
|
2020-04-21 08:19:33 +00:00
|
|
|
|
|
|
|
if (rNote != NULL) {
|
|
|
|
rPriority = rNote->playbackState.priority;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
aNote = Audio_FindNodeWithPrioLessThan(&pool->active, layer->channel->notePriority);
|
2020-04-21 08:19:33 +00:00
|
|
|
|
|
|
|
if (aNote != NULL) {
|
|
|
|
aPriority = aNote->playbackState.priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rNote == NULL && aNote == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aPriority < rPriority) {
|
|
|
|
Audio_AudioListRemove(&aNote->listItem);
|
2021-11-07 16:58:50 +00:00
|
|
|
func_800E82C0(aNote, layer);
|
|
|
|
AudioSeq_AudioListPushBack(&pool->releasing, &aNote->listItem);
|
|
|
|
aNote->playbackState.priority = layer->channel->notePriority;
|
2020-04-21 08:19:33 +00:00
|
|
|
return aNote;
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
rNote->playbackState.wantedParentLayer = layer;
|
|
|
|
rNote->playbackState.priority = layer->channel->notePriority;
|
2020-04-21 08:19:33 +00:00
|
|
|
return rNote;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
Note* Audio_AllocNote(SequenceLayer* layer) {
|
2020-04-21 08:19:33 +00:00
|
|
|
Note* ret;
|
2021-11-07 16:58:50 +00:00
|
|
|
u32 policy = layer->channel->noteAllocPolicy;
|
2020-04-21 08:19:33 +00:00
|
|
|
|
|
|
|
if (policy & 1) {
|
2021-11-07 16:58:50 +00:00
|
|
|
ret = layer->note;
|
|
|
|
if (ret != NULL && ret->playbackState.prevParentLayer == layer &&
|
2020-04-21 08:19:33 +00:00
|
|
|
ret->playbackState.wantedParentLayer == NO_LAYER) {
|
2021-11-07 16:58:50 +00:00
|
|
|
Audio_NoteReleaseAndTakeOwnership(ret, layer);
|
2020-04-21 08:19:33 +00:00
|
|
|
Audio_AudioListRemove(&ret->listItem);
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioSeq_AudioListPushBack(&ret->listItem.pool->releasing, &ret->listItem);
|
2020-04-21 08:19:33 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (policy & 2) {
|
2021-11-07 16:58:50 +00:00
|
|
|
if (!(ret = Audio_AllocNoteFromDisabled(&layer->channel->notePool, layer)) &&
|
|
|
|
!(ret = Audio_AllocNoteFromDecaying(&layer->channel->notePool, layer)) &&
|
|
|
|
!(ret = Audio_AllocNoteFromActive(&layer->channel->notePool, layer))) {
|
2020-04-21 08:19:33 +00:00
|
|
|
goto null_return;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (policy & 4) {
|
2021-11-07 16:58:50 +00:00
|
|
|
if (!(ret = Audio_AllocNoteFromDisabled(&layer->channel->notePool, layer)) &&
|
|
|
|
!(ret = Audio_AllocNoteFromDisabled(&layer->channel->seqPlayer->notePool, layer)) &&
|
|
|
|
!(ret = Audio_AllocNoteFromDecaying(&layer->channel->notePool, layer)) &&
|
|
|
|
!(ret = Audio_AllocNoteFromDecaying(&layer->channel->seqPlayer->notePool, layer)) &&
|
|
|
|
!(ret = Audio_AllocNoteFromActive(&layer->channel->notePool, layer)) &&
|
|
|
|
!(ret = Audio_AllocNoteFromActive(&layer->channel->seqPlayer->notePool, layer))) {
|
2020-04-21 08:19:33 +00:00
|
|
|
goto null_return;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (policy & 8) {
|
2021-11-07 16:58:50 +00:00
|
|
|
if (!(ret = Audio_AllocNoteFromDisabled(&gAudioContext.noteFreeLists, layer)) &&
|
|
|
|
!(ret = Audio_AllocNoteFromDecaying(&gAudioContext.noteFreeLists, layer)) &&
|
|
|
|
!(ret = Audio_AllocNoteFromActive(&gAudioContext.noteFreeLists, layer))) {
|
2020-04-21 08:19:33 +00:00
|
|
|
goto null_return;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (!(ret = Audio_AllocNoteFromDisabled(&layer->channel->notePool, layer)) &&
|
|
|
|
!(ret = Audio_AllocNoteFromDisabled(&layer->channel->seqPlayer->notePool, layer)) &&
|
|
|
|
!(ret = Audio_AllocNoteFromDisabled(&gAudioContext.noteFreeLists, layer)) &&
|
|
|
|
!(ret = Audio_AllocNoteFromDecaying(&layer->channel->notePool, layer)) &&
|
|
|
|
!(ret = Audio_AllocNoteFromDecaying(&layer->channel->seqPlayer->notePool, layer)) &&
|
|
|
|
!(ret = Audio_AllocNoteFromDecaying(&gAudioContext.noteFreeLists, layer)) &&
|
|
|
|
!(ret = Audio_AllocNoteFromActive(&layer->channel->notePool, layer)) &&
|
|
|
|
!(ret = Audio_AllocNoteFromActive(&layer->channel->seqPlayer->notePool, layer)) &&
|
|
|
|
!(ret = Audio_AllocNoteFromActive(&gAudioContext.noteFreeLists, layer))) {
|
2020-04-21 08:19:33 +00:00
|
|
|
goto null_return;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
null_return:
|
2021-11-07 16:58:50 +00:00
|
|
|
layer->bit3 = true;
|
2020-04-21 08:19:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio_NoteInitAll(void) {
|
|
|
|
Note* note;
|
|
|
|
s32 i;
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
for (i = 0; i < gAudioContext.numNotes; i++) {
|
2021-07-27 23:44:58 +00:00
|
|
|
note = &gAudioContext.notes[i];
|
2020-04-21 08:19:33 +00:00
|
|
|
note->noteSubEu = gZeroNoteSub;
|
|
|
|
note->playbackState.priority = 0;
|
|
|
|
note->playbackState.unk_04 = 0;
|
|
|
|
note->playbackState.parentLayer = NO_LAYER;
|
|
|
|
note->playbackState.wantedParentLayer = NO_LAYER;
|
|
|
|
note->playbackState.prevParentLayer = NO_LAYER;
|
|
|
|
note->playbackState.waveId = 0;
|
|
|
|
note->playbackState.attributes.velocity = 0.0f;
|
2021-11-07 16:58:50 +00:00
|
|
|
note->playbackState.adsrVolScaleUnused = 0;
|
2020-08-15 18:06:26 +00:00
|
|
|
note->playbackState.adsr.action.asByte = 0;
|
|
|
|
note->vibratoState.active = 0;
|
|
|
|
note->portamento.cur = 0;
|
|
|
|
note->portamento.speed = 0;
|
2020-09-20 17:22:09 +00:00
|
|
|
note->playbackState.stereoHeadsetEffects = false;
|
2020-04-21 08:19:33 +00:00
|
|
|
note->unk_BC = 0;
|
2021-11-07 16:58:50 +00:00
|
|
|
note->synthesisState.synthesisBuffers = AudioHeap_AllocDmaMemory(&gAudioContext.notesAndBuffersPool, 0x1E0);
|
2020-04-21 08:19:33 +00:00
|
|
|
}
|
|
|
|
}
|