2020-10-03 15:22:44 +00:00
|
|
|
#include "ultra64.h"
|
|
|
|
#include "global.h"
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
void Audio_SequenceChannelProcessSound(SequenceChannel* channel, s32 recalculateVolume, s32 applyBend) {
|
2020-08-15 18:06:26 +00:00
|
|
|
f32 channelVolume;
|
|
|
|
f32 chanFreqScale;
|
|
|
|
s32 i;
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (channel->changes.s.volume || recalculateVolume) {
|
|
|
|
channelVolume = channel->volume * channel->volumeScale * channel->seqPlayer->appliedFadeVolume;
|
2022-07-04 16:00:29 +00:00
|
|
|
if (channel->seqPlayer->muted && (channel->muteBehavior & MUTE_BEHAVIOR_SOFTEN)) {
|
2021-11-07 16:58:50 +00:00
|
|
|
channelVolume = channel->seqPlayer->muteVolumeScale * channelVolume;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
channel->appliedVolume = channelVolume * channelVolume;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if (channel->changes.s.pan) {
|
|
|
|
channel->pan = channel->newPan * channel->panChannelWeight;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
chanFreqScale = channel->freqScale;
|
2022-06-03 19:59:02 +00:00
|
|
|
if (applyBend) {
|
|
|
|
chanFreqScale *= channel->seqPlayer->bend;
|
2021-11-07 16:58:50 +00:00
|
|
|
channel->changes.s.freqScale = true;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++) {
|
2021-11-07 16:58:50 +00:00
|
|
|
SequenceLayer* layer = channel->layers[i];
|
2022-06-03 19:59:02 +00:00
|
|
|
|
2020-08-15 18:06:26 +00:00
|
|
|
if (layer != NULL && layer->enabled && layer->note != NULL) {
|
|
|
|
if (layer->notePropertiesNeedInit) {
|
|
|
|
layer->noteFreqScale = layer->freqScale * chanFreqScale;
|
2021-11-07 16:58:50 +00:00
|
|
|
layer->noteVelocity = layer->velocitySquare2 * channel->appliedVolume;
|
|
|
|
layer->notePan = (channel->pan + layer->pan * (0x80 - channel->panChannelWeight)) >> 7;
|
2020-09-20 17:22:09 +00:00
|
|
|
layer->notePropertiesNeedInit = false;
|
2020-08-15 18:06:26 +00:00
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
if (channel->changes.s.freqScale) {
|
2020-08-15 18:06:26 +00:00
|
|
|
layer->noteFreqScale = layer->freqScale * chanFreqScale;
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (channel->changes.s.volume || recalculateVolume) {
|
|
|
|
layer->noteVelocity = layer->velocitySquare2 * channel->appliedVolume;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
if (channel->changes.s.pan) {
|
|
|
|
layer->notePan = (channel->pan + layer->pan * (0x80 - channel->panChannelWeight)) >> 7;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
channel->changes.asByte = 0;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Audio_SequencePlayerProcessSound(SequencePlayer* seqPlayer) {
|
|
|
|
s32 i;
|
|
|
|
|
|
|
|
if (seqPlayer->fadeTimer != 0) {
|
|
|
|
seqPlayer->fadeVolume += seqPlayer->fadeVelocity;
|
2020-09-20 17:22:09 +00:00
|
|
|
seqPlayer->recalculateVolume = true;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
|
|
|
if (seqPlayer->fadeVolume > 1.0f) {
|
|
|
|
seqPlayer->fadeVolume = 1.0f;
|
|
|
|
}
|
2022-06-03 19:59:02 +00:00
|
|
|
if (seqPlayer->fadeVolume < 0.0f) {
|
|
|
|
seqPlayer->fadeVolume = 0.0f;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
seqPlayer->fadeTimer--;
|
|
|
|
if (seqPlayer->fadeTimer == 0 && seqPlayer->state == 2) {
|
2021-11-07 16:58:50 +00:00
|
|
|
AudioSeq_SequencePlayerDisable(seqPlayer);
|
2020-08-15 18:06:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (seqPlayer->recalculateVolume) {
|
|
|
|
seqPlayer->appliedFadeVolume = seqPlayer->fadeVolume * seqPlayer->fadeVolumeScale;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
if (seqPlayer->channels[i]->enabled == 1) {
|
2022-06-03 19:59:02 +00:00
|
|
|
Audio_SequenceChannelProcessSound(seqPlayer->channels[i], seqPlayer->recalculateVolume,
|
|
|
|
seqPlayer->applyBend);
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 17:22:09 +00:00
|
|
|
seqPlayer->recalculateVolume = false;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
f32 Audio_GetPortamentoFreqScale(Portamento* portamento) {
|
2020-08-15 18:06:26 +00:00
|
|
|
u32 loResCur;
|
2022-06-03 19:59:02 +00:00
|
|
|
f32 portamentoFreq;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
portamento->cur += portamento->speed;
|
|
|
|
loResCur = (portamento->cur >> 8) & 0xFF;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
|
|
|
if (loResCur >= 127) {
|
|
|
|
loResCur = 127;
|
2022-06-03 19:59:02 +00:00
|
|
|
portamento->mode = 0;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
portamentoFreq = 1.0f + portamento->extent * (gBendPitchOneOctaveFrequencies[loResCur + 128] - 1.0f);
|
|
|
|
|
|
|
|
return portamentoFreq;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s16 Audio_GetVibratoPitchChange(VibratoState* vib) {
|
|
|
|
s32 index;
|
2022-06-03 19:59:02 +00:00
|
|
|
|
2020-08-15 18:06:26 +00:00
|
|
|
vib->time += (s32)vib->rate;
|
|
|
|
index = (vib->time >> 10) & 0x3F;
|
|
|
|
return vib->curve[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
f32 Audio_GetVibratoFreqScale(VibratoState* vib) {
|
2021-11-24 02:07:48 +00:00
|
|
|
static f32 D_80130510 = 0.0f;
|
|
|
|
static s32 D_80130514 = 0;
|
2020-08-15 18:06:26 +00:00
|
|
|
f32 pitchChange;
|
|
|
|
f32 extent;
|
|
|
|
f32 invExtent;
|
|
|
|
f32 result;
|
|
|
|
f32 temp;
|
2021-11-07 16:58:50 +00:00
|
|
|
SequenceChannel* channel = vib->channel;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
|
|
|
if (vib->delay != 0) {
|
|
|
|
vib->delay--;
|
2022-06-03 19:59:02 +00:00
|
|
|
return 1.0f;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
//! @bug this probably meant to compare with gAudioContext.sequenceChannelNone.
|
|
|
|
//! -1 isn't used as a channel pointer anywhere else.
|
|
|
|
if (channel != ((SequenceChannel*)(-1))) {
|
2020-08-15 18:06:26 +00:00
|
|
|
if (vib->extentChangeTimer) {
|
|
|
|
if (vib->extentChangeTimer == 1) {
|
|
|
|
vib->extent = (s32)channel->vibratoExtentTarget;
|
|
|
|
} else {
|
|
|
|
vib->extent += ((s32)channel->vibratoExtentTarget - vib->extent) / (s32)vib->extentChangeTimer;
|
|
|
|
}
|
|
|
|
|
|
|
|
vib->extentChangeTimer--;
|
|
|
|
} else if (channel->vibratoExtentTarget != (s32)vib->extent) {
|
|
|
|
if ((vib->extentChangeTimer = channel->vibratoExtentChangeDelay) == 0) {
|
|
|
|
vib->extent = (s32)channel->vibratoExtentTarget;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vib->rateChangeTimer) {
|
|
|
|
if (vib->rateChangeTimer == 1) {
|
|
|
|
vib->rate = (s32)channel->vibratoRateTarget;
|
|
|
|
} else {
|
|
|
|
vib->rate += ((s32)channel->vibratoRateTarget - vib->rate) / (s32)vib->rateChangeTimer;
|
|
|
|
}
|
|
|
|
|
|
|
|
vib->rateChangeTimer--;
|
|
|
|
} else if (channel->vibratoRateTarget != (s32)vib->rate) {
|
|
|
|
if ((vib->rateChangeTimer = channel->vibratoRateChangeDelay) == 0) {
|
|
|
|
vib->rate = (s32)channel->vibratoRateTarget;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:59:02 +00:00
|
|
|
if (vib->extent == 0.0f) {
|
2020-08-15 18:06:26 +00:00
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
pitchChange = Audio_GetVibratoPitchChange(vib) + 32768.0f;
|
|
|
|
temp = vib->extent / 4096.0f;
|
|
|
|
extent = temp + 1.0f;
|
|
|
|
invExtent = 1.0f / extent;
|
|
|
|
|
2021-11-24 02:07:48 +00:00
|
|
|
result = 1.0f / ((extent - invExtent) * pitchChange / 65536.0f + invExtent);
|
|
|
|
|
2020-08-15 18:06:26 +00:00
|
|
|
D_80130510 += result;
|
2021-11-24 02:07:48 +00:00
|
|
|
D_80130514++;
|
|
|
|
|
2020-08-15 18:06:26 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio_NoteVibratoUpdate(Note* note) {
|
2022-06-04 19:29:01 +00:00
|
|
|
if (note->playbackState.portamento.mode != 0) {
|
|
|
|
note->playbackState.portamentoFreqScale = Audio_GetPortamentoFreqScale(¬e->playbackState.portamento);
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
2022-06-04 19:29:01 +00:00
|
|
|
if (note->playbackState.vibratoState.active) {
|
|
|
|
note->playbackState.vibratoFreqScale = Audio_GetVibratoFreqScale(¬e->playbackState.vibratoState);
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio_NoteVibratoInit(Note* note) {
|
|
|
|
VibratoState* vib;
|
2021-11-07 16:58:50 +00:00
|
|
|
SequenceChannel* channel;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
|
|
|
note->playbackState.vibratoFreqScale = 1.0f;
|
|
|
|
|
2022-06-04 19:29:01 +00:00
|
|
|
vib = ¬e->playbackState.vibratoState;
|
2020-08-15 18:06:26 +00:00
|
|
|
|
|
|
|
vib->active = 1;
|
|
|
|
vib->time = 0;
|
|
|
|
|
|
|
|
vib->curve = gWaveSamples[2];
|
2021-11-07 16:58:50 +00:00
|
|
|
vib->channel = note->playbackState.parentLayer->channel;
|
|
|
|
channel = vib->channel;
|
|
|
|
if ((vib->extentChangeTimer = channel->vibratoExtentChangeDelay) == 0) {
|
|
|
|
vib->extent = (s32)channel->vibratoExtentTarget;
|
2020-08-15 18:06:26 +00:00
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
vib->extent = (s32)channel->vibratoExtentStart;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:58:50 +00:00
|
|
|
if ((vib->rateChangeTimer = channel->vibratoRateChangeDelay) == 0) {
|
|
|
|
vib->rate = (s32)channel->vibratoRateTarget;
|
2020-08-15 18:06:26 +00:00
|
|
|
} else {
|
2021-11-07 16:58:50 +00:00
|
|
|
vib->rate = (s32)channel->vibratoRateStart;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
2021-11-07 16:58:50 +00:00
|
|
|
vib->delay = channel->vibratoDelay;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Audio_NotePortamentoInit(Note* note) {
|
|
|
|
note->playbackState.portamentoFreqScale = 1.0f;
|
2022-06-04 19:29:01 +00:00
|
|
|
note->playbackState.portamento = note->playbackState.parentLayer->portamento;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 14:31:08 +00:00
|
|
|
void Audio_AdsrInit(AdsrState* adsr, EnvelopePoint* envelope, s16* volOut) {
|
2020-08-15 18:06:26 +00:00
|
|
|
adsr->action.asByte = 0;
|
|
|
|
adsr->delay = 0;
|
|
|
|
adsr->envelope = envelope;
|
|
|
|
adsr->sustain = 0.0f;
|
|
|
|
adsr->current = 0.0f;
|
|
|
|
// (An older versions of the audio engine used in Super Mario 64 did
|
|
|
|
// adsr->volOut = volOut. That line and associated struct member were
|
|
|
|
// removed, but the function parameter was forgotten and remains.)
|
|
|
|
}
|
|
|
|
|
|
|
|
f32 Audio_AdsrUpdate(AdsrState* adsr) {
|
|
|
|
u8 state = adsr->action.s.state;
|
2022-05-29 18:31:43 +00:00
|
|
|
|
2020-08-15 18:06:26 +00:00
|
|
|
switch (state) {
|
|
|
|
case ADSR_STATE_DISABLED:
|
|
|
|
return 0.0f;
|
|
|
|
|
2022-05-29 18:31:43 +00:00
|
|
|
case ADSR_STATE_INITIAL:
|
2020-08-15 18:06:26 +00:00
|
|
|
if (adsr->action.s.hang) {
|
|
|
|
adsr->action.s.state = ADSR_STATE_HANG;
|
|
|
|
break;
|
|
|
|
}
|
2022-06-03 16:51:23 +00:00
|
|
|
FALLTHROUGH;
|
2020-08-15 18:06:26 +00:00
|
|
|
case ADSR_STATE_START_LOOP:
|
|
|
|
adsr->envIndex = 0;
|
|
|
|
adsr->action.s.state = ADSR_STATE_LOOP;
|
2022-06-11 14:19:34 +00:00
|
|
|
retry:;
|
2022-06-12 22:58:13 +00:00
|
|
|
FALLTHROUGH;
|
2020-08-15 18:06:26 +00:00
|
|
|
case ADSR_STATE_LOOP:
|
|
|
|
adsr->delay = adsr->envelope[adsr->envIndex].delay;
|
|
|
|
switch (adsr->delay) {
|
|
|
|
case ADSR_DISABLE:
|
|
|
|
adsr->action.s.state = ADSR_STATE_DISABLED;
|
|
|
|
break;
|
2022-05-29 18:31:43 +00:00
|
|
|
|
2020-08-15 18:06:26 +00:00
|
|
|
case ADSR_HANG:
|
|
|
|
adsr->action.s.state = ADSR_STATE_HANG;
|
|
|
|
break;
|
2022-05-29 18:31:43 +00:00
|
|
|
|
2020-08-15 18:06:26 +00:00
|
|
|
case ADSR_GOTO:
|
|
|
|
adsr->envIndex = adsr->envelope[adsr->envIndex].arg;
|
|
|
|
goto retry;
|
2022-05-29 18:31:43 +00:00
|
|
|
|
2020-08-15 18:06:26 +00:00
|
|
|
case ADSR_RESTART:
|
|
|
|
adsr->action.s.state = ADSR_STATE_INITIAL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-05-29 18:31:43 +00:00
|
|
|
adsr->delay *= gAudioContext.audioBufferParameters.updatesPerFrameScaled;
|
2020-08-15 18:06:26 +00:00
|
|
|
if (adsr->delay == 0) {
|
|
|
|
adsr->delay = 1;
|
|
|
|
}
|
|
|
|
adsr->target = adsr->envelope[adsr->envIndex].arg / 32767.0f;
|
|
|
|
adsr->target = adsr->target * adsr->target;
|
|
|
|
adsr->velocity = (adsr->target - adsr->current) / adsr->delay;
|
|
|
|
adsr->action.s.state = ADSR_STATE_FADE;
|
|
|
|
adsr->envIndex++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (adsr->action.s.state != ADSR_STATE_FADE) {
|
|
|
|
break;
|
|
|
|
}
|
2022-06-03 16:51:23 +00:00
|
|
|
FALLTHROUGH;
|
2020-08-15 18:06:26 +00:00
|
|
|
case ADSR_STATE_FADE:
|
|
|
|
adsr->current += adsr->velocity;
|
2022-05-29 18:31:43 +00:00
|
|
|
adsr->delay--;
|
|
|
|
if (adsr->delay <= 0) {
|
2020-08-15 18:06:26 +00:00
|
|
|
adsr->action.s.state = ADSR_STATE_LOOP;
|
|
|
|
}
|
2022-06-03 16:51:23 +00:00
|
|
|
FALLTHROUGH;
|
2020-08-15 18:06:26 +00:00
|
|
|
case ADSR_STATE_HANG:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ADSR_STATE_DECAY:
|
2022-05-29 18:31:43 +00:00
|
|
|
case ADSR_STATE_RELEASE:
|
2020-08-15 18:06:26 +00:00
|
|
|
adsr->current -= adsr->fadeOutVel;
|
|
|
|
if (adsr->sustain != 0.0f && state == ADSR_STATE_DECAY) {
|
|
|
|
if (adsr->current < adsr->sustain) {
|
|
|
|
adsr->current = adsr->sustain;
|
|
|
|
adsr->delay = 128;
|
|
|
|
adsr->action.s.state = ADSR_STATE_SUSTAIN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (adsr->current < 0.00001f) {
|
|
|
|
adsr->current = 0.0f;
|
|
|
|
adsr->action.s.state = ADSR_STATE_DISABLED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ADSR_STATE_SUSTAIN:
|
2022-05-29 18:31:43 +00:00
|
|
|
adsr->delay--;
|
2020-08-15 18:06:26 +00:00
|
|
|
if (adsr->delay == 0) {
|
|
|
|
adsr->action.s.state = ADSR_STATE_RELEASE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (adsr->action.s.decay) {
|
|
|
|
adsr->action.s.state = ADSR_STATE_DECAY;
|
2020-09-20 17:22:09 +00:00
|
|
|
adsr->action.s.decay = false;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (adsr->action.s.release) {
|
|
|
|
adsr->action.s.state = ADSR_STATE_RELEASE;
|
2020-09-20 17:22:09 +00:00
|
|
|
adsr->action.s.release = false;
|
2020-08-15 18:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (adsr->current < 0.0f) {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
2022-05-29 18:31:43 +00:00
|
|
|
|
2020-08-15 18:06:26 +00:00
|
|
|
if (adsr->current > 1.0f) {
|
|
|
|
return 1.0f;
|
|
|
|
}
|
2022-05-29 18:31:43 +00:00
|
|
|
|
2020-08-15 18:06:26 +00:00
|
|
|
return adsr->current;
|
|
|
|
}
|