effects.c (17690B)
1 #include <ultra64.h> 2 3 #include "effects.h" 4 #include "load.h" 5 #include "data.h" 6 #include "seqplayer.h" 7 8 #ifdef VERSION_JP 9 #define US_FLOAT2(x) x##.0 10 #else 11 #define US_FLOAT2(x) x 12 #endif 13 14 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 15 void sequence_channel_process_sound(struct SequenceChannel *seqChannel, s32 recalculateVolume) { 16 f32 channelVolume; 17 s32 i; 18 19 if (seqChannel->changes.as_bitfields.volume || recalculateVolume) { 20 channelVolume = seqChannel->volume * seqChannel->volumeScale * seqChannel->seqPlayer->appliedFadeVolume; 21 if (seqChannel->seqPlayer->muted && (seqChannel->muteBehavior & MUTE_BEHAVIOR_SOFTEN) != 0) { 22 channelVolume = seqChannel->seqPlayer->muteVolumeScale * channelVolume; 23 } 24 #if defined(VERSION_SH) || defined(VERSION_CN) 25 seqChannel->appliedVolume = channelVolume * channelVolume; 26 #else 27 seqChannel->appliedVolume = channelVolume; 28 #endif 29 } 30 31 if (seqChannel->changes.as_bitfields.pan) { 32 seqChannel->pan = seqChannel->newPan * seqChannel->panChannelWeight; 33 } 34 35 for (i = 0; i < 4; i++) { 36 struct SequenceChannelLayer *layer = seqChannel->layers[i]; 37 if (layer != NULL && layer->enabled && layer->note != NULL) { 38 if (layer->notePropertiesNeedInit) { 39 layer->noteFreqScale = layer->freqScale * seqChannel->freqScale; 40 layer->noteVelocity = layer->velocitySquare * seqChannel->appliedVolume; 41 layer->notePan = (seqChannel->pan + layer->pan * (0x80 - seqChannel->panChannelWeight)) >> 7; 42 layer->notePropertiesNeedInit = FALSE; 43 } else { 44 if (seqChannel->changes.as_bitfields.freqScale) { 45 layer->noteFreqScale = layer->freqScale * seqChannel->freqScale; 46 } 47 if (seqChannel->changes.as_bitfields.volume || recalculateVolume) { 48 layer->noteVelocity = layer->velocitySquare * seqChannel->appliedVolume; 49 } 50 if (seqChannel->changes.as_bitfields.pan) { 51 layer->notePan = (seqChannel->pan + layer->pan * (0x80 - seqChannel->panChannelWeight)) >> 7; 52 } 53 } 54 } 55 } 56 seqChannel->changes.as_u8 = 0; 57 } 58 #else 59 static void sequence_channel_process_sound(struct SequenceChannel *seqChannel) { 60 f32 channelVolume; 61 f32 panLayerWeight; 62 f32 panFromChannel; 63 s32 i; 64 65 channelVolume = seqChannel->volume * seqChannel->volumeScale * seqChannel->seqPlayer->fadeVolume; 66 if (seqChannel->seqPlayer->muted && (seqChannel->muteBehavior & MUTE_BEHAVIOR_SOFTEN) != 0) { 67 channelVolume *= seqChannel->seqPlayer->muteVolumeScale; 68 } 69 70 panFromChannel = seqChannel->pan * seqChannel->panChannelWeight; 71 panLayerWeight = US_FLOAT(1.0) - seqChannel->panChannelWeight; 72 73 for (i = 0; i < 4; i++) { 74 struct SequenceChannelLayer *layer = seqChannel->layers[i]; 75 if (layer != NULL && layer->enabled && layer->note != NULL) { 76 layer->noteFreqScale = layer->freqScale * seqChannel->freqScale; 77 layer->noteVelocity = layer->velocitySquare * channelVolume; 78 layer->notePan = (layer->pan * panLayerWeight) + panFromChannel; 79 } 80 } 81 } 82 #endif 83 84 void sequence_player_process_sound(struct SequencePlayer *seqPlayer) { 85 s32 i; 86 87 if (seqPlayer->fadeRemainingFrames != 0) { 88 seqPlayer->fadeVolume += seqPlayer->fadeVelocity; 89 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 90 seqPlayer->recalculateVolume = TRUE; 91 #endif 92 93 if (seqPlayer->fadeVolume > US_FLOAT2(1)) { 94 seqPlayer->fadeVolume = US_FLOAT2(1); 95 } 96 if (seqPlayer->fadeVolume < 0) { 97 seqPlayer->fadeVolume = 0; 98 } 99 100 if (--seqPlayer->fadeRemainingFrames == 0) { 101 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 102 if (seqPlayer->state == 2) { 103 sequence_player_disable(seqPlayer); 104 return; 105 } 106 #else 107 switch (seqPlayer->state) { 108 case SEQUENCE_PLAYER_STATE_FADE_OUT: 109 sequence_player_disable(seqPlayer); 110 return; 111 112 case SEQUENCE_PLAYER_STATE_2: 113 case SEQUENCE_PLAYER_STATE_3: 114 seqPlayer->state = SEQUENCE_PLAYER_STATE_0; 115 break; 116 117 case SEQUENCE_PLAYER_STATE_4: 118 break; 119 } 120 #endif 121 } 122 } 123 124 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 125 if (seqPlayer->recalculateVolume) { 126 seqPlayer->appliedFadeVolume = seqPlayer->fadeVolume * seqPlayer->fadeVolumeScale; 127 } 128 #endif 129 130 // Process channels 131 for (i = 0; i < CHANNELS_MAX; i++) { 132 if (IS_SEQUENCE_CHANNEL_VALID(seqPlayer->channels[i]) == TRUE 133 && seqPlayer->channels[i]->enabled == TRUE) { 134 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 135 sequence_channel_process_sound(seqPlayer->channels[i], seqPlayer->recalculateVolume); 136 #else 137 sequence_channel_process_sound(seqPlayer->channels[i]); 138 #endif 139 } 140 } 141 142 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 143 seqPlayer->recalculateVolume = FALSE; 144 #endif 145 } 146 147 f32 get_portamento_freq_scale(struct Portamento *p) { 148 u32 v0; 149 f32 result; 150 #if defined(VERSION_JP) || defined(VERSION_US) 151 if (p->mode == 0) { 152 return 1.0f; 153 } 154 #endif 155 156 p->cur += p->speed; 157 v0 = (u32) p->cur; 158 159 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 160 if (v0 > 127) 161 #else 162 if (v0 >= 127) 163 #endif 164 { 165 v0 = 127; 166 } 167 168 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 169 result = US_FLOAT(1.0) + p->extent * (gPitchBendFrequencyScale[v0 + 128] - US_FLOAT(1.0)); 170 #else 171 result = US_FLOAT(1.0) + p->extent * (gPitchBendFrequencyScale[v0 + 127] - US_FLOAT(1.0)); 172 #endif 173 return result; 174 } 175 176 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 177 s16 get_vibrato_pitch_change(struct VibratoState *vib) { 178 s32 index; 179 vib->time += (s32) vib->rate; 180 index = (vib->time >> 10) & 0x3F; 181 return vib->curve[index] >> 8; 182 } 183 #else 184 s8 get_vibrato_pitch_change(struct VibratoState *vib) { 185 s32 index; 186 vib->time += vib->rate; 187 188 index = (vib->time >> 10) & 0x3F; 189 190 switch (index & 0x30) { 191 case 0x10: 192 index = 31 - index; 193 194 case 0x00: 195 return vib->curve[index]; 196 197 case 0x20: 198 index -= 0x20; 199 break; 200 201 case 0x30: 202 index = 63 - index; 203 break; 204 } 205 206 return -vib->curve[index]; 207 } 208 #endif 209 210 f32 get_vibrato_freq_scale(struct VibratoState *vib) { 211 s32 pitchChange; 212 f32 extent; 213 f32 result; 214 215 if (vib->delay != 0) { 216 vib->delay--; 217 return 1; 218 } 219 220 if (vib->extentChangeTimer) { 221 if (vib->extentChangeTimer == 1) { 222 vib->extent = (s32) vib->seqChannel->vibratoExtentTarget; 223 } else { 224 vib->extent += 225 ((s32) vib->seqChannel->vibratoExtentTarget - vib->extent) / (s32) vib->extentChangeTimer; 226 } 227 228 vib->extentChangeTimer--; 229 } else if (vib->seqChannel->vibratoExtentTarget != (s32) vib->extent) { 230 if ((vib->extentChangeTimer = vib->seqChannel->vibratoExtentChangeDelay) == 0) { 231 vib->extent = (s32) vib->seqChannel->vibratoExtentTarget; 232 } 233 } 234 235 if (vib->rateChangeTimer) { 236 if (vib->rateChangeTimer == 1) { 237 vib->rate = (s32) vib->seqChannel->vibratoRateTarget; 238 } else { 239 vib->rate += ((s32) vib->seqChannel->vibratoRateTarget - vib->rate) / (s32) vib->rateChangeTimer; 240 } 241 242 vib->rateChangeTimer--; 243 } else if (vib->seqChannel->vibratoRateTarget != (s32) vib->rate) { 244 if ((vib->rateChangeTimer = vib->seqChannel->vibratoRateChangeDelay) == 0) { 245 vib->rate = (s32) vib->seqChannel->vibratoRateTarget; 246 } 247 } 248 249 if (vib->extent == 0) { 250 return 1.0f; 251 } 252 253 pitchChange = get_vibrato_pitch_change(vib); 254 extent = (f32) vib->extent / US_FLOAT(4096.0); 255 256 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 257 result = US_FLOAT(1.0) + extent * (gPitchBendFrequencyScale[pitchChange + 128] - US_FLOAT(1.0)); 258 #else 259 result = US_FLOAT(1.0) + extent * (gPitchBendFrequencyScale[pitchChange + 127] - US_FLOAT(1.0)); 260 #endif 261 return result; 262 } 263 264 void note_vibrato_update(struct Note *note) { 265 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 266 if (note->portamento.mode != 0) { 267 note->portamentoFreqScale = get_portamento_freq_scale(¬e->portamento); 268 } 269 if (note->vibratoState.active && note->parentLayer != NO_LAYER) { 270 note->vibratoFreqScale = get_vibrato_freq_scale(¬e->vibratoState); 271 } 272 #else 273 if (note->vibratoState.active) { 274 note->portamentoFreqScale = get_portamento_freq_scale(¬e->portamento); 275 if (note->parentLayer != NO_LAYER) { 276 note->vibratoFreqScale = get_vibrato_freq_scale(¬e->vibratoState); 277 } 278 } 279 #endif 280 } 281 282 void note_vibrato_init(struct Note *note) { 283 struct VibratoState *vib; 284 UNUSED struct SequenceChannel *seqChannel; 285 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 286 struct NotePlaybackState *seqPlayerState = (struct NotePlaybackState *) ¬e->priority; 287 #endif 288 289 note->vibratoFreqScale = 1.0f; 290 note->portamentoFreqScale = 1.0f; 291 292 vib = ¬e->vibratoState; 293 294 #if defined(VERSION_JP) || defined(VERSION_US) 295 if (note->parentLayer->seqChannel->vibratoExtentStart == 0 296 && note->parentLayer->seqChannel->vibratoExtentTarget == 0 297 && note->parentLayer->portamento.mode == 0) { 298 vib->active = FALSE; 299 return; 300 } 301 #endif 302 303 vib->active = TRUE; 304 vib->time = 0; 305 306 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 307 vib->curve = gWaveSamples[2]; 308 vib->seqChannel = note->parentLayer->seqChannel; 309 if ((vib->extentChangeTimer = vib->seqChannel->vibratoExtentChangeDelay) == 0) { 310 vib->extent = FLOAT_CAST(vib->seqChannel->vibratoExtentTarget); 311 } else { 312 vib->extent = FLOAT_CAST(vib->seqChannel->vibratoExtentStart); 313 } 314 315 if ((vib->rateChangeTimer = vib->seqChannel->vibratoRateChangeDelay) == 0) { 316 vib->rate = FLOAT_CAST(vib->seqChannel->vibratoRateTarget); 317 } else { 318 vib->rate = FLOAT_CAST(vib->seqChannel->vibratoRateStart); 319 } 320 vib->delay = vib->seqChannel->vibratoDelay; 321 322 seqPlayerState->portamento = seqPlayerState->parentLayer->portamento; 323 #else 324 vib->curve = gVibratoCurve; 325 vib->seqChannel = note->parentLayer->seqChannel; 326 seqChannel = vib->seqChannel; 327 328 if ((vib->extentChangeTimer = seqChannel->vibratoExtentChangeDelay) == 0) { 329 vib->extent = seqChannel->vibratoExtentTarget; 330 } else { 331 vib->extent = seqChannel->vibratoExtentStart; 332 } 333 334 if ((vib->rateChangeTimer = seqChannel->vibratoRateChangeDelay) == 0) { 335 vib->rate = seqChannel->vibratoRateTarget; 336 } else { 337 vib->rate = seqChannel->vibratoRateStart; 338 } 339 vib->delay = seqChannel->vibratoDelay; 340 341 note->portamento = note->parentLayer->portamento; 342 #endif 343 } 344 345 void adsr_init(struct AdsrState *adsr, struct AdsrEnvelope *envelope, UNUSED s16 *volOut) { 346 adsr->action = 0; 347 adsr->state = ADSR_STATE_DISABLED; 348 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 349 adsr->delay = 0; 350 adsr->envelope = envelope; 351 #if defined(VERSION_SH) || defined(VERSION_CN) 352 adsr->sustain = 0.0f; 353 #endif 354 adsr->current = 0.0f; 355 #else 356 adsr->initial = 0; 357 adsr->delay = 0; 358 adsr->velocity = 0; 359 adsr->envelope = envelope; 360 adsr->volOut = volOut; 361 #endif 362 } 363 364 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 365 f32 adsr_update(struct AdsrState *adsr) { 366 #else 367 s32 adsr_update(struct AdsrState *adsr) { 368 #endif 369 u8 action = adsr->action; 370 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 371 u8 state = adsr->state; 372 switch (state) { 373 #else 374 switch (adsr->state) { 375 #endif 376 case ADSR_STATE_DISABLED: 377 return 0; 378 379 case ADSR_STATE_INITIAL: { 380 #if defined(VERSION_JP) || defined(VERSION_US) 381 adsr->current = adsr->initial; 382 adsr->target = adsr->initial; 383 #endif 384 if (action & ADSR_ACTION_HANG) { 385 adsr->state = ADSR_STATE_HANG; 386 break; 387 } 388 // fallthrough 389 } 390 391 case ADSR_STATE_START_LOOP: 392 adsr->envIndex = 0; 393 #if defined(VERSION_JP) || defined(VERSION_US) 394 adsr->currentHiRes = adsr->current << 0x10; 395 #endif 396 adsr->state = ADSR_STATE_LOOP; 397 // fallthrough 398 399 #if defined(VERSION_SH) || defined(VERSION_CN) 400 restart: 401 #endif 402 case ADSR_STATE_LOOP: 403 adsr->delay = BSWAP16(adsr->envelope[adsr->envIndex].delay); 404 switch (adsr->delay) { 405 case ADSR_DISABLE: 406 adsr->state = ADSR_STATE_DISABLED; 407 break; 408 case ADSR_HANG: 409 adsr->state = ADSR_STATE_HANG; 410 break; 411 case ADSR_GOTO: 412 adsr->envIndex = BSWAP16(adsr->envelope[adsr->envIndex].arg); 413 #if defined(VERSION_SH) || defined(VERSION_CN) 414 goto restart; 415 #else 416 break; 417 #endif 418 case ADSR_RESTART: 419 adsr->state = ADSR_STATE_INITIAL; 420 break; 421 422 default: 423 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 424 if (adsr->delay >= 4) { 425 adsr->delay = adsr->delay * gAudioBufferParameters.updatesPerFrame 426 #if defined(VERSION_SH) || defined(VERSION_CN) 427 / gAudioBufferParameters.presetUnk4 428 #endif 429 / 4; 430 } 431 #if defined(VERSION_SH) || defined(VERSION_CN) 432 if (adsr->delay == 0) { 433 adsr->delay = 1; 434 } 435 adsr->target = (f32) BSWAP16(adsr->envelope[adsr->envIndex].arg) / 32767.0f; 436 #elif defined(VERSION_EU) 437 adsr->target = (f32) BSWAP16(adsr->envelope[adsr->envIndex].arg) / 32767.0; 438 #endif 439 adsr->target = adsr->target * adsr->target; 440 adsr->velocity = (adsr->target - adsr->current) / adsr->delay; 441 #else 442 adsr->target = BSWAP16(adsr->envelope[adsr->envIndex].arg); 443 adsr->velocity = ((adsr->target - adsr->current) << 0x10) / adsr->delay; 444 #endif 445 adsr->state = ADSR_STATE_FADE; 446 adsr->envIndex++; 447 break; 448 } 449 if (adsr->state != ADSR_STATE_FADE) { 450 break; 451 } 452 // fallthrough 453 454 case ADSR_STATE_FADE: 455 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 456 adsr->current += adsr->velocity; 457 #else 458 adsr->currentHiRes += adsr->velocity; 459 adsr->current = adsr->currentHiRes >> 0x10; 460 #endif 461 if (--adsr->delay <= 0) { 462 adsr->state = ADSR_STATE_LOOP; 463 } 464 // fallthrough 465 466 case ADSR_STATE_HANG: 467 break; 468 469 case ADSR_STATE_DECAY: 470 case ADSR_STATE_RELEASE: { 471 adsr->current -= adsr->fadeOutVel; 472 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 473 if (adsr->sustain != 0.0f && state == ADSR_STATE_DECAY) { 474 #else 475 if (adsr->sustain != 0 && adsr->state == ADSR_STATE_DECAY) { 476 #endif 477 if (adsr->current < adsr->sustain) { 478 adsr->current = adsr->sustain; 479 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 480 adsr->delay = 128; 481 #else 482 adsr->delay = adsr->sustain / 16; 483 #endif 484 adsr->state = ADSR_STATE_SUSTAIN; 485 } 486 break; 487 } 488 489 #if defined(VERSION_SH) || defined(VERSION_CN) 490 if (adsr->current < 0.00001f) { 491 adsr->current = 0.0f; 492 adsr->state = ADSR_STATE_DISABLED; 493 } 494 #elif defined(VERSION_EU) 495 if (adsr->current < 0) { 496 adsr->current = 0.0f; 497 adsr->state = ADSR_STATE_DISABLED; 498 } 499 #else 500 if (adsr->current < 100) { 501 adsr->current = 0; 502 adsr->state = ADSR_STATE_DISABLED; 503 } 504 #endif 505 break; 506 } 507 508 case ADSR_STATE_SUSTAIN: 509 adsr->delay -= 1; 510 if (adsr->delay == 0) { 511 adsr->state = ADSR_STATE_RELEASE; 512 } 513 break; 514 } 515 516 if ((action & ADSR_ACTION_DECAY)) { 517 adsr->state = ADSR_STATE_DECAY; 518 adsr->action = action & ~ADSR_ACTION_DECAY; 519 } 520 521 if ((action & ADSR_ACTION_RELEASE)) { 522 adsr->state = ADSR_STATE_RELEASE; 523 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 524 adsr->action = action & ~ADSR_ACTION_RELEASE; 525 #else 526 adsr->action = action & ~(ADSR_ACTION_RELEASE | ADSR_ACTION_DECAY); 527 #endif 528 } 529 530 #if defined(VERSION_EU) || defined(VERSION_SH) || defined(VERSION_CN) 531 if (adsr->current < 0.0f) { 532 return 0.0f; 533 } 534 if (adsr->current > 1.0f) { 535 eu_stubbed_printf_1("Audio:Envp: overflow %f\n", adsr->current); 536 return 1.0f; 537 } 538 return adsr->current; 539 #else 540 *adsr->volOut = adsr->current; 541 return 0; 542 #endif 543 }