|
6 | 6 | #include "../common/movie.h" |
7 | 7 | #include "../common/vbalua.h" |
8 | 8 |
|
| 9 | +// evil macros |
| 10 | +#ifndef countof |
| 11 | +#define countof(a) (sizeof(a) / sizeof(a[0])) |
| 12 | +#endif |
| 13 | + |
9 | 14 | // evil static variables |
10 | | -static u32 lastFrameTime = 0; |
11 | | -static int32 frameSkipCount = 0; |
12 | | -static int32 frameCount = 0; |
13 | | -static bool pauseAfterFrameAdvance = false; |
| 15 | +static u32 lastFrameTime = 0; |
| 16 | +static int32 frameSkipCount = 0; |
| 17 | +static int32 frameCount = 0; |
| 18 | +static bool pauseAfterFrameAdvance = false; |
| 19 | + |
| 20 | +static s16 soundFilter[4000]; |
| 21 | +static s16 soundRight[5] = { 0, 0, 0, 0, 0 }; |
| 22 | +static s16 soundLeft[5] = { 0, 0, 0, 0, 0 }; |
| 23 | +static int32 soundEchoIndex = 0; |
14 | 24 |
|
15 | 25 | // systemABC stuff are core-related |
16 | 26 |
|
@@ -350,3 +360,183 @@ void systemGetLCDBaseOffset(int32 &xofs, int32 &yofs) |
350 | 360 | break; |
351 | 361 | } |
352 | 362 | } |
| 363 | + |
| 364 | +// sound |
| 365 | + |
| 366 | +bool systemSoundCleanInit() |
| 367 | +{ |
| 368 | + if (systemSoundInit()) |
| 369 | + { |
| 370 | + memset(soundBuffer[0], 0, 735); |
| 371 | + memset(soundBuffer[1], 0, 735); |
| 372 | + memset(soundBuffer[2], 0, 735); |
| 373 | + memset(soundBuffer[3], 0, 735); |
| 374 | + memset(soundFinalWave, 0, soundBufferLen); |
| 375 | + |
| 376 | + soundPaused = 1; |
| 377 | + return true; |
| 378 | + } |
| 379 | + return false; |
| 380 | +} |
| 381 | + |
| 382 | +void systemSoundEnableChannels(int channels) |
| 383 | +{ |
| 384 | + int c = (channels & 0x0f) << 4; |
| 385 | + soundEnableFlag |= ((channels & 0x30f) | c); |
| 386 | +} |
| 387 | + |
| 388 | +void systemSoundDisableChannels(int channels) |
| 389 | +{ |
| 390 | + int c = (channels & 0x0f) << 4; |
| 391 | + soundEnableFlag &= ~((channels & 0x30f) | c); |
| 392 | +} |
| 393 | + |
| 394 | +int systemSoundGetEnabledChannels() |
| 395 | +{ |
| 396 | + return (soundEnableFlag & 0x30f); |
| 397 | +} |
| 398 | + |
| 399 | +void systemSoundSetQuality(int quality) |
| 400 | +{ |
| 401 | + if (soundQuality != quality) |
| 402 | + { |
| 403 | + if (!soundOffFlag) |
| 404 | + systemSoundShutdown(); |
| 405 | + soundQuality = quality; |
| 406 | + if (!soundOffFlag) |
| 407 | + systemSoundCleanInit(); |
| 408 | + } |
| 409 | + else if (soundQuality != quality) |
| 410 | + { |
| 411 | + soundNextPosition = 0; |
| 412 | + } |
| 413 | + soundNextPosition = 0; |
| 414 | + soundTickStep = USE_TICKS_AS * soundQuality; |
| 415 | + soundIndex = 0; |
| 416 | + soundBufferIndex = 0; |
| 417 | +} |
| 418 | + |
| 419 | +void systemSoundMixReset() |
| 420 | +{ |
| 421 | + soundBufferIndex = 0; |
| 422 | + memset(soundFinalWave, 0, soundBufferLen); |
| 423 | + memset(soundFilter, 0, sizeof(soundFilter)); |
| 424 | + soundEchoIndex = 0; |
| 425 | +} |
| 426 | + |
| 427 | +void systemSoundMixSilence() |
| 428 | +{ |
| 429 | + soundFinalWave[soundBufferIndex++] = 0; |
| 430 | + soundFinalWave[soundBufferIndex++] = 0; |
| 431 | + if ((soundFrameSoundWritten + 1) < countof(soundFrameSound)) |
| 432 | + { |
| 433 | + soundFrameSound[soundFrameSoundWritten++] = 0; |
| 434 | + soundFrameSound[soundFrameSoundWritten++] = 0; |
| 435 | + } |
| 436 | +} |
| 437 | + |
| 438 | +void systemSoundMix(int resL, int resR) |
| 439 | +{ |
| 440 | + bool usesDSP = systemSoundAppliesDSP(); |
| 441 | + if (soundEcho) |
| 442 | + { |
| 443 | + if (soundEchoIndex >= countof(soundFilter)) |
| 444 | + soundEchoIndex = 0; |
| 445 | + |
| 446 | + resL += soundFilter[soundEchoIndex] / 2; |
| 447 | + soundFilter[soundEchoIndex++] = resL; |
| 448 | + |
| 449 | + resR += soundFilter[soundEchoIndex] / 2; |
| 450 | + soundFilter[soundEchoIndex++] = resR; |
| 451 | + } |
| 452 | + |
| 453 | + if (soundLowPass) |
| 454 | + { |
| 455 | + soundLeft[4] = soundLeft[3]; |
| 456 | + soundLeft[3] = soundLeft[2]; |
| 457 | + soundLeft[2] = soundLeft[1]; |
| 458 | + soundLeft[1] = soundLeft[0]; |
| 459 | + soundLeft[0] = resL; |
| 460 | + resL = (soundLeft[4] + 2 * soundLeft[3] + 8 * soundLeft[2] + 2 * soundLeft[1] + soundLeft[0]) / 14; |
| 461 | + |
| 462 | + soundRight[4] = soundRight[3]; |
| 463 | + soundRight[3] = soundRight[2]; |
| 464 | + soundRight[2] = soundRight[1]; |
| 465 | + soundRight[1] = soundRight[0]; |
| 466 | + soundRight[0] = resR; |
| 467 | + resR = (soundRight[4] + 2 * soundRight[3] + 8 * soundRight[2] + 2 * soundRight[1] + soundRight[0]) / 14; |
| 468 | + } |
| 469 | + |
| 470 | + if (usesDSP) |
| 471 | + { |
| 472 | + switch (soundVolume) |
| 473 | + { |
| 474 | + case 0: |
| 475 | + case 1: |
| 476 | + case 2: |
| 477 | + case 3: |
| 478 | + resL *= (soundVolume + 1); |
| 479 | + resR *= (soundVolume + 1); |
| 480 | + break; |
| 481 | + case 4: |
| 482 | + resL >>= 2; |
| 483 | + resR >>= 2; |
| 484 | + break; |
| 485 | + case 5: |
| 486 | + resL >>= 1; |
| 487 | + resR >>= 1; |
| 488 | + break; |
| 489 | + } |
| 490 | + } |
| 491 | + |
| 492 | + if (resL > 32767) |
| 493 | + resL = 32767; |
| 494 | + else if (resL < -32768) |
| 495 | + resL = -32768; |
| 496 | + |
| 497 | + if (resR > 32767) |
| 498 | + resR = 32767; |
| 499 | + else if (resR < -32768) |
| 500 | + resR = -32768; |
| 501 | + |
| 502 | + if (soundReverse && usesDSP) |
| 503 | + { |
| 504 | + soundFinalWave[soundBufferIndex++] = resR; |
| 505 | + soundFinalWave[soundBufferIndex++] = resL; |
| 506 | + if ((soundFrameSoundWritten + 1) < countof(soundFrameSound)) |
| 507 | + { |
| 508 | + soundFrameSound[soundFrameSoundWritten++] = resR; |
| 509 | + soundFrameSound[soundFrameSoundWritten++] = resL; |
| 510 | + } |
| 511 | + } |
| 512 | + else |
| 513 | + { |
| 514 | + soundFinalWave[soundBufferIndex++] = resL; |
| 515 | + soundFinalWave[soundBufferIndex++] = resR; |
| 516 | + if (soundFrameSoundWritten + 1 < countof(soundFrameSound)) |
| 517 | + { |
| 518 | + soundFrameSound[soundFrameSoundWritten++] = resL; |
| 519 | + soundFrameSound[soundFrameSoundWritten++] = resR; |
| 520 | + } |
| 521 | + } |
| 522 | +} |
| 523 | + |
| 524 | +void systemSoundNext() |
| 525 | +{ |
| 526 | + soundIndex++; |
| 527 | + |
| 528 | + if (2 * soundBufferIndex >= soundBufferLen) |
| 529 | + { |
| 530 | + if (systemSoundOn) |
| 531 | + { |
| 532 | + if (soundPaused && !systemIsPaused()) // this checking is for the old frame timing |
| 533 | + { |
| 534 | + systemSoundResume(); |
| 535 | + } |
| 536 | + |
| 537 | + systemSoundWriteToBuffer(); |
| 538 | + } |
| 539 | + soundIndex = 0; |
| 540 | + soundBufferIndex = 0; |
| 541 | + } |
| 542 | +} |
0 commit comments