-
Notifications
You must be signed in to change notification settings - Fork 664
Expand file tree
/
Copy patheffects.c
More file actions
368 lines (313 loc) · 11 KB
/
effects.c
File metadata and controls
368 lines (313 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "ultra64.h"
#include "attributes.h"
#include "audio.h"
/**
* original name: __Nas_CallWaveProcess_Sub
*/
void Audio_SequenceChannelProcessSound(SequenceChannel* channel, s32 recalculateVolume, s32 applyBend) {
f32 channelVolume;
f32 chanFreqScale;
s32 i;
if (channel->changes.s.volume || recalculateVolume) {
channelVolume = channel->volume * channel->volumeScale * channel->seqPlayer->appliedFadeVolume;
if (channel->seqPlayer->muted && (channel->muteBehavior & MUTE_BEHAVIOR_SOFTEN)) {
channelVolume = channel->seqPlayer->muteVolumeScale * channelVolume;
}
channel->appliedVolume = channelVolume * channelVolume;
}
if (channel->changes.s.pan) {
channel->pan = channel->newPan * channel->panChannelWeight;
}
chanFreqScale = channel->freqScale;
if (applyBend) {
chanFreqScale *= channel->seqPlayer->bend;
channel->changes.s.freqScale = true;
}
for (i = 0; i < 4; i++) {
SequenceLayer* layer = channel->layers[i];
if (layer != NULL && layer->enabled && layer->note != NULL) {
if (layer->notePropertiesNeedInit) {
layer->noteFreqScale = layer->freqScale * chanFreqScale;
layer->noteVelocity = layer->velocitySquare2 * channel->appliedVolume;
layer->notePan = (channel->pan + layer->pan * (0x80 - channel->panChannelWeight)) >> 7;
layer->notePropertiesNeedInit = false;
} else {
if (channel->changes.s.freqScale) {
layer->noteFreqScale = layer->freqScale * chanFreqScale;
}
if (channel->changes.s.volume || recalculateVolume) {
layer->noteVelocity = layer->velocitySquare2 * channel->appliedVolume;
}
if (channel->changes.s.pan) {
layer->notePan = (channel->pan + layer->pan * (0x80 - channel->panChannelWeight)) >> 7;
}
}
}
}
channel->changes.asByte = 0;
}
/**
* original name: Nas_MainCtrl
*/
void Audio_SequencePlayerProcessSound(SequencePlayer* seqPlayer) {
s32 i;
if (seqPlayer->fadeTimer != 0) {
seqPlayer->fadeVolume += seqPlayer->fadeVelocity;
seqPlayer->recalculateVolume = true;
if (seqPlayer->fadeVolume > 1.0f) {
seqPlayer->fadeVolume = 1.0f;
}
if (seqPlayer->fadeVolume < 0.0f) {
seqPlayer->fadeVolume = 0.0f;
}
seqPlayer->fadeTimer--;
if (seqPlayer->fadeTimer == 0 && seqPlayer->state == 2) {
AudioSeq_SequencePlayerDisable(seqPlayer);
return;
}
}
if (seqPlayer->recalculateVolume) {
seqPlayer->appliedFadeVolume = seqPlayer->fadeVolume * seqPlayer->fadeVolumeScale;
}
for (i = 0; i < 16; i++) {
if (seqPlayer->channels[i]->enabled == 1) {
Audio_SequenceChannelProcessSound(seqPlayer->channels[i], seqPlayer->recalculateVolume,
seqPlayer->applyBend);
}
}
seqPlayer->recalculateVolume = false;
}
/**
* original name: Nas_SweepCalculator
*/
f32 Audio_GetPortamentoFreqScale(Portamento* portamento) {
u32 loResCur;
f32 portamentoFreq;
portamento->cur += portamento->speed;
loResCur = (portamento->cur >> 8) & 0xFF;
if (loResCur >= 127) {
loResCur = 127;
portamento->mode = 0;
}
portamentoFreq = 1.0f + portamento->extent * (gBendPitchOneOctaveFrequencies[loResCur + 128] - 1.0f);
return portamentoFreq;
}
/**
* original name: Nas_ModTableRead
*/
s16 Audio_GetVibratoPitchChange(VibratoState* vib) {
s32 index;
vib->time += (s32)vib->rate;
index = (vib->time >> 10) & 0x3F;
return vib->curve[index];
}
/**
* original name: Nas_Modulator
*/
f32 Audio_GetVibratoFreqScale(VibratoState* vib) {
static f32 D_80130510 = 0.0f;
static s32 D_80130514 = 0;
f32 pitchChange;
f32 depth;
f32 invDepth;
f32 result;
f32 temp;
SequenceChannel* channel = vib->channel;
if (vib->delay != 0) {
vib->delay--;
return 1.0f;
}
//! @bug this probably meant to compare with gAudioCtx.sequenceChannelNone.
//! -1 isn't used as a channel pointer anywhere else.
if (channel != ((SequenceChannel*)(-1))) {
if (vib->depthChangeTimer) {
if (vib->depthChangeTimer == 1) {
vib->depth = (s32)channel->vibratoDepthTarget;
} else {
vib->depth += ((s32)channel->vibratoDepthTarget - vib->depth) / (s32)vib->depthChangeTimer;
}
vib->depthChangeTimer--;
} else if (channel->vibratoDepthTarget != (s32)vib->depth) {
if ((vib->depthChangeTimer = channel->vibratoDepthChangeDelay) == 0) {
vib->depth = (s32)channel->vibratoDepthTarget;
}
}
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;
}
}
}
if (vib->depth == 0.0f) {
return 1.0f;
}
pitchChange = Audio_GetVibratoPitchChange(vib) + 32768.0f;
temp = vib->depth / 4096.0f;
depth = temp + 1.0f;
invDepth = 1.0f / depth;
result = 1.0f / ((depth - invDepth) * pitchChange / 65536.0f + invDepth);
D_80130510 += result;
D_80130514++;
return result;
}
/**
* original name: Nas_ChannelModulation
*/
void Audio_NoteVibratoUpdate(Note* note) {
if (note->playbackState.portamento.mode != 0) {
note->playbackState.portamentoFreqScale = Audio_GetPortamentoFreqScale(¬e->playbackState.portamento);
}
if (note->playbackState.vibratoState.active) {
note->playbackState.vibratoFreqScale = Audio_GetVibratoFreqScale(¬e->playbackState.vibratoState);
}
}
/**
* original name: Nas_ChannelModInit
*/
void Audio_NoteVibratoInit(Note* note) {
VibratoState* vib;
SequenceChannel* channel;
note->playbackState.vibratoFreqScale = 1.0f;
vib = ¬e->playbackState.vibratoState;
vib->active = true;
vib->time = 0;
vib->curve = gWaveSamples[2]; // gSineWaveSample[0..63]
vib->channel = note->playbackState.parentLayer->channel;
channel = vib->channel;
if ((vib->depthChangeTimer = channel->vibratoDepthChangeDelay) == 0) {
vib->depth = (s32)channel->vibratoDepthTarget;
} else {
vib->depth = (s32)channel->vibratoDepthStart;
}
if ((vib->rateChangeTimer = channel->vibratoRateChangeDelay) == 0) {
vib->rate = (s32)channel->vibratoRateTarget;
} else {
vib->rate = (s32)channel->vibratoRateStart;
}
vib->delay = channel->vibratoDelay;
}
/**
* original name: Nas_SweepInit
*/
void Audio_NotePortamentoInit(Note* note) {
note->playbackState.portamentoFreqScale = 1.0f;
note->playbackState.portamento = note->playbackState.parentLayer->portamento;
}
/**
* original name: Nas_EnvInit
*/
void Audio_AdsrInit(AdsrState* adsr, EnvelopePoint* envelope, s16* volOut) {
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.)
}
/**
* original name: Nas_EnvProcess
*/
f32 Audio_AdsrUpdate(AdsrState* adsr) {
u8 state = adsr->action.s.state;
switch (state) {
case ADSR_STATE_DISABLED:
return 0.0f;
case ADSR_STATE_INITIAL:
if (adsr->action.s.hang) {
adsr->action.s.state = ADSR_STATE_HANG;
break;
}
FALLTHROUGH;
case ADSR_STATE_START_LOOP:
adsr->envIndex = 0;
adsr->action.s.state = ADSR_STATE_LOOP;
retry:;
FALLTHROUGH;
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;
case ADSR_HANG:
adsr->action.s.state = ADSR_STATE_HANG;
break;
case ADSR_GOTO:
adsr->envIndex = adsr->envelope[adsr->envIndex].arg;
goto retry;
case ADSR_RESTART:
adsr->action.s.state = ADSR_STATE_INITIAL;
break;
default:
adsr->delay *= gAudioCtx.audioBufferParameters.ticksPerUpdateScaled;
if (adsr->delay == 0) {
adsr->delay = 1;
}
adsr->target = adsr->envelope[adsr->envIndex].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++;
break;
}
if (adsr->action.s.state != ADSR_STATE_FADE) {
break;
}
FALLTHROUGH;
case ADSR_STATE_FADE:
adsr->current += adsr->velocity;
adsr->delay--;
if (adsr->delay <= 0) {
adsr->action.s.state = ADSR_STATE_LOOP;
}
FALLTHROUGH;
case ADSR_STATE_HANG:
break;
case ADSR_STATE_DECAY:
case ADSR_STATE_RELEASE:
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:
adsr->delay--;
if (adsr->delay == 0) {
adsr->action.s.state = ADSR_STATE_RELEASE;
}
break;
}
if (adsr->action.s.decay) {
adsr->action.s.state = ADSR_STATE_DECAY;
adsr->action.s.decay = false;
}
if (adsr->action.s.release) {
adsr->action.s.state = ADSR_STATE_RELEASE;
adsr->action.s.release = false;
}
if (adsr->current < 0.0f) {
return 0.0f;
}
if (adsr->current > 1.0f) {
return 1.0f;
}
return adsr->current;
}