Mercurial > vba-clojure
view src/gb/gbSound.cpp @ 524:7ef5c73ea8fa
working on recording sound. almost have it, but there is still unexplained popping sounds.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 23 Jun 2012 23:10:31 -0500 |
parents | f9f4f1b99eed |
children | fa7676dbf6f2 |
line wrap: on
line source
1 #ifdef WIN322 # include "../win32/stdafx.h"3 # include "../win32/VBA.h"4 #endif6 #include <cstring>7 #include <cassert>8 #include <stdio.h>10 #include "../common/System.h"11 #include "../common/Util.h"12 //#include "../Blip_Buffer.h"13 #include "gbGlobals.h"14 #include "gbSound.h"16 #ifndef countof17 #define countof(a) (sizeof(a) / sizeof(a[0]))18 #endif20 extern u8 soundBuffer[6][735];21 extern u16 soundFinalWave[1470];22 extern u16 soundFrameSound[735 * 30 * 2];23 extern int32 soundVolume;25 soundtick_t GB_USE_TICKS_AS = 24; // (1048576.0/44100.0); // FIXME: (4194304.0/70224.0)(fps) vs 60.0fps?27 #define SOUND_MAGIC 0x6000000028 #define SOUND_MAGIC_2 0x3000000029 #define NOISE_MAGIC (2097152.0 / 44100.0)31 extern int32 speed;33 extern u8 soundWavePattern[4][32];35 extern u32 soundBufferLen;36 extern u32 soundBufferTotalLen;37 extern int32 soundQuality;38 extern int32 soundPaused;39 extern int32 soundPlay;40 extern soundtick_t soundTicks;41 extern soundtick_t SOUND_CLOCK_TICKS;42 extern u32 soundNextPosition;44 extern int32 soundLevel1;45 extern int32 soundLevel2;46 extern int32 soundBalance;47 extern int32 soundMasterOn;48 extern u32 soundIndex;49 extern u32 soundBufferIndex;50 extern int32 soundFrameSoundWritten;51 int32 soundVIN = 0;52 extern int32 soundDebug;54 extern int32 sound1On;55 extern int32 sound1ATL;56 extern int32 sound1Skip;57 extern int32 sound1Index;58 extern int32 sound1Continue;59 extern int32 sound1EnvelopeVolume;60 extern int32 sound1EnvelopeATL;61 extern int32 sound1EnvelopeUpDown;62 extern int32 sound1EnvelopeATLReload;63 extern int32 sound1SweepATL;64 extern int32 sound1SweepATLReload;65 extern int32 sound1SweepSteps;66 extern int32 sound1SweepUpDown;67 extern int32 sound1SweepStep;68 extern u8 * sound1Wave;70 extern int32 sound2On;71 extern int32 sound2ATL;72 extern int32 sound2Skip;73 extern int32 sound2Index;74 extern int32 sound2Continue;75 extern int32 sound2EnvelopeVolume;76 extern int32 sound2EnvelopeATL;77 extern int32 sound2EnvelopeUpDown;78 extern int32 sound2EnvelopeATLReload;79 extern u8 * sound2Wave;81 extern int32 sound3On;82 extern int32 sound3ATL;83 extern int32 sound3Skip;84 extern int32 sound3Index;85 extern int32 sound3Continue;86 extern int32 sound3OutputLevel;87 extern int32 sound3Last;89 extern int32 sound4On;90 extern int32 sound4Clock;91 extern int32 sound4ATL;92 extern int32 sound4Skip;93 extern int32 sound4Index;94 extern int32 sound4ShiftRight;95 extern int32 sound4ShiftSkip;96 extern int32 sound4ShiftIndex;97 extern int32 sound4NSteps;98 extern int32 sound4CountDown;99 extern int32 sound4Continue;100 extern int32 sound4EnvelopeVolume;101 extern int32 sound4EnvelopeATL;102 extern int32 sound4EnvelopeUpDown;103 extern int32 sound4EnvelopeATLReload;105 extern int32 soundEnableFlag;106 extern int32 soundMutedFlag;108 extern int32 soundFreqRatio[8];109 extern int32 soundShiftClock[16];111 extern s16 soundFilter[4000];112 extern s16 soundLeft[5];113 extern s16 soundRight[5];114 extern int32 soundEchoIndex;115 extern bool8 soundEcho;116 extern bool8 soundLowPass;117 extern bool8 soundReverse;118 extern bool8 soundOffFlag;120 bool8 gbDigitalSound = false;122 void gbSoundEvent(register u16 address, register int data)123 {124 int freq = 0;126 gbMemory[address] = data;128 #ifndef FINAL_VERSION129 if (soundDebug)130 {131 // don't translate. debug only132 log("Sound event: %08lx %02x\n", address, data);133 }134 #endif135 switch (address)136 {137 case NR10:138 sound1SweepATL = sound1SweepATLReload = 344 * ((data >> 4) & 7);139 sound1SweepSteps = data & 7;140 sound1SweepUpDown = data & 0x08;141 sound1SweepStep = 0;142 break;143 case NR11:144 sound1Wave = soundWavePattern[data >> 6];145 sound1ATL = 172 * (64 - (data & 0x3f));146 break;147 case NR12:148 sound1EnvelopeVolume = data >> 4;149 sound1EnvelopeUpDown = data & 0x08;150 sound1EnvelopeATLReload = sound1EnvelopeATL = 689 * (data & 7);151 break;152 case NR13:153 freq = (((int)(gbMemory[NR14] & 7)) << 8) | data;154 sound1ATL = 172 * (64 - (gbMemory[NR11] & 0x3f));155 freq = 2048 - freq;156 if (freq)157 {158 sound1Skip = SOUND_MAGIC / freq;159 }160 else161 sound1Skip = 0;162 break;163 case NR14:164 freq = (((int)(data & 7) << 8) | gbMemory[NR13]);165 freq = 2048 - freq;166 sound1ATL = 172 * (64 - (gbMemory[NR11] & 0x3f));167 sound1Continue = data & 0x40;168 if (freq)169 {170 sound1Skip = SOUND_MAGIC / freq;171 }172 else173 sound1Skip = 0;174 if (data & 0x80)175 {176 gbMemory[NR52] |= 1;177 sound1EnvelopeVolume = gbMemory[NR12] >> 4;178 sound1EnvelopeUpDown = gbMemory[NR12] & 0x08;179 sound1ATL = 172 * (64 - (gbMemory[NR11] & 0x3f));180 sound1EnvelopeATLReload = sound1EnvelopeATL = 689 * (gbMemory[NR12] & 7);181 sound1SweepATL = sound1SweepATLReload = 344 * ((gbMemory[NR10] >> 4) & 7);182 sound1SweepSteps = gbMemory[NR10] & 7;183 sound1SweepUpDown = gbMemory[NR10] & 0x08;184 sound1SweepStep = 0;186 sound1Index = 0;187 sound1On = 1;188 }189 break;190 case NR21:191 sound2Wave = soundWavePattern[data >> 6];192 sound2ATL = 172 * (64 - (data & 0x3f));193 break;194 case NR22:195 sound2EnvelopeVolume = data >> 4;196 sound2EnvelopeUpDown = data & 0x08;197 sound2EnvelopeATLReload = sound2EnvelopeATL = 689 * (data & 7);198 break;199 case NR23:200 freq = (((int)(gbMemory[NR24] & 7)) << 8) | data;201 sound2ATL = 172 * (64 - (gbMemory[NR21] & 0x3f));202 freq = 2048 - freq;203 if (freq)204 {205 sound2Skip = SOUND_MAGIC / freq;206 }207 else208 sound2Skip = 0;209 break;210 case NR24:211 freq = (((int)(data & 7) << 8) | gbMemory[NR23]);212 freq = 2048 - freq;213 sound2ATL = 172 * (64 - (gbMemory[NR21] & 0x3f));214 sound2Continue = data & 0x40;215 if (freq)216 {217 sound2Skip = SOUND_MAGIC / freq;218 }219 else220 sound2Skip = 0;221 if (data & 0x80)222 {223 gbMemory[NR52] |= 2;224 sound2EnvelopeVolume = gbMemory[NR22] >> 4;225 sound2EnvelopeUpDown = gbMemory[NR22] & 0x08;226 sound2ATL = 172 * (64 - (gbMemory[NR21] & 0x3f));227 sound2EnvelopeATLReload = sound2EnvelopeATL = 689 * (gbMemory[NR22] & 7);229 sound2Index = 0;230 sound2On = 1;231 }232 break;233 case NR30:234 if (!(data & 0x80))235 {236 gbMemory[NR52] &= 0xfb;237 sound3On = 0;238 }239 break;240 case NR31:241 sound3ATL = 172 * (256 - data);242 break;243 case NR32:244 sound3OutputLevel = (data >> 5) & 3;245 break;246 case NR33:247 freq = 2048 - (((int)(gbMemory[NR34] & 7) << 8) | data);248 if (freq)249 {250 sound3Skip = SOUND_MAGIC_2 / freq;251 }252 else253 sound3Skip = 0;254 break;255 case NR34:256 freq = 2048 - (((data & 7) << 8) | (int)gbMemory[NR33]);257 if (freq)258 {259 sound3Skip = SOUND_MAGIC_2 / freq;260 }261 else262 {263 sound3Skip = 0;264 }265 sound3Continue = data & 0x40;266 if ((data & 0x80) && (gbMemory[NR30] & 0x80))267 {268 gbMemory[NR52] |= 4;269 sound3ATL = 172 * (256 - gbMemory[NR31]);270 sound3Index = 0;271 sound3On = 1;272 }273 break;274 case NR41:275 sound4ATL = 172 * (64 - (data & 0x3f));276 break;277 case NR42:278 sound4EnvelopeVolume = data >> 4;279 sound4EnvelopeUpDown = data & 0x08;280 sound4EnvelopeATLReload = sound4EnvelopeATL = 689 * (data & 7);281 break;282 case NR43:283 freq = soundFreqRatio[data & 7];284 sound4NSteps = data & 0x08;286 sound4Skip = freq * NOISE_MAGIC;288 sound4Clock = data >> 4;290 freq = freq / soundShiftClock[sound4Clock];292 sound4ShiftSkip = freq * NOISE_MAGIC;294 break;295 case NR44:296 sound4Continue = data & 0x40;297 if (data & 0x80)298 {299 gbMemory[NR52] |= 8;300 sound4EnvelopeVolume = gbMemory[NR42] >> 4;301 sound4EnvelopeUpDown = gbMemory[NR42] & 0x08;302 sound4ATL = 172 * (64 - (gbMemory[NR41] & 0x3f));303 sound4EnvelopeATLReload = sound4EnvelopeATL = 689 * (gbMemory[NR42] & 7);305 sound4On = 1;307 sound4Index = 0;308 sound4ShiftIndex = 0;310 freq = soundFreqRatio[gbMemory[NR43] & 7];312 sound4Skip = freq * NOISE_MAGIC;314 sound4NSteps = gbMemory[NR43] & 0x08;316 freq = freq / soundShiftClock[gbMemory[NR43] >> 4];318 sound4ShiftSkip = freq * NOISE_MAGIC;319 if (sound4NSteps)320 sound4ShiftRight = 0x7f;321 else322 sound4ShiftRight = 0x7fff;323 }324 break;325 case NR50:326 soundVIN = data & 0x88;327 soundLevel1 = data & 7;328 soundLevel2 = (data >> 4) & 7;329 break;330 case NR51:331 soundBalance = (data & soundEnableFlag);332 gbMemory[address] = data;333 break;334 case NR52:335 soundMasterOn = data & 0x80;336 if (!(data & 0x80))337 {338 sound1On = 0;339 sound2On = 0;340 sound3On = 0;341 sound4On = 0;342 }343 break;344 }346 gbDigitalSound = true;348 if (sound1On && sound1EnvelopeVolume != 0)349 gbDigitalSound = false;350 if (sound2On && sound2EnvelopeVolume != 0)351 gbDigitalSound = false;352 if (sound3On && sound3OutputLevel != 0)353 gbDigitalSound = false;354 if (sound4On && sound4EnvelopeVolume != 0)355 gbDigitalSound = false;356 }358 void gbSoundChannel1()359 {360 int vol = sound1EnvelopeVolume;362 int freq = 0;364 int value = 0;366 if (sound1On && (sound1ATL || !sound1Continue))367 {368 sound1Index += soundQuality * sound1Skip;369 sound1Index &= 0x1fffffff;371 value = ((s8)sound1Wave[sound1Index >> 24]) * vol;372 }374 soundBuffer[0][soundIndex] = value;376 if (sound1On)377 {378 if (sound1ATL)379 {380 sound1ATL -= soundQuality;382 if (sound1ATL <= 0 && sound1Continue)383 {384 gbMemory[NR52] &= 0xfe;385 sound1On = 0;386 }387 }389 if (sound1EnvelopeATL)390 {391 sound1EnvelopeATL -= soundQuality;393 if (sound1EnvelopeATL <= 0)394 {395 if (sound1EnvelopeUpDown)396 {397 if (sound1EnvelopeVolume < 15)398 sound1EnvelopeVolume++;399 }400 else401 {402 if (sound1EnvelopeVolume)403 sound1EnvelopeVolume--;404 }406 sound1EnvelopeATL += sound1EnvelopeATLReload;407 }408 }410 if (sound1SweepATL)411 {412 sound1SweepATL -= soundQuality;414 if (sound1SweepATL <= 0)415 {416 freq = (((int)(gbMemory[NR14] & 7) << 8) | gbMemory[NR13]);418 int updown = 1;420 if (sound1SweepUpDown)421 updown = -1;423 int newfreq = 0;424 if (sound1SweepSteps)425 {426 newfreq = freq + updown * freq / (1 << sound1SweepSteps);427 if (newfreq == freq)428 newfreq = 0;429 }430 else431 newfreq = freq;433 if (newfreq < 0)434 {435 sound1SweepATL += sound1SweepATLReload;436 }437 else if (newfreq > 2047)438 {439 sound1SweepATL = 0;440 sound1On = 0;441 gbMemory[NR52] &= 0xfe;442 }443 else444 {445 sound1SweepATL += sound1SweepATLReload;446 sound1Skip = SOUND_MAGIC / (2048 - newfreq);448 gbMemory[NR13] = newfreq & 0xff;449 gbMemory[NR14] = (gbMemory[NR14] & 0xf8) | ((newfreq >> 8) & 7);450 }451 }452 }453 }454 }456 void gbSoundChannel2()457 {458 // int freq = 0;459 int vol = sound2EnvelopeVolume;461 int value = 0;463 if (sound2On && (sound2ATL || !sound2Continue))464 {465 sound2Index += soundQuality * sound2Skip;466 sound2Index &= 0x1fffffff;468 value = ((s8)sound2Wave[sound2Index >> 24]) * vol;469 }471 soundBuffer[1][soundIndex] = value;473 if (sound2On)474 {475 if (sound2ATL)476 {477 sound2ATL -= soundQuality;479 if (sound2ATL <= 0 && sound2Continue)480 {481 gbMemory[NR52] &= 0xfd;482 sound2On = 0;483 }484 }486 if (sound2EnvelopeATL)487 {488 sound2EnvelopeATL -= soundQuality;490 if (sound2EnvelopeATL <= 0)491 {492 if (sound2EnvelopeUpDown)493 {494 if (sound2EnvelopeVolume < 15)495 sound2EnvelopeVolume++;496 }497 else498 {499 if (sound2EnvelopeVolume)500 sound2EnvelopeVolume--;501 }502 sound2EnvelopeATL += sound2EnvelopeATLReload;503 }504 }505 }506 }508 void gbSoundChannel3()509 {510 int value = sound3Last;512 if (sound3On && (sound3ATL || !sound3Continue))513 {514 sound3Index += soundQuality * sound3Skip;515 sound3Index &= 0x1fffffff;517 value = gbMemory[0xff30 + (sound3Index >> 25)];519 if ((sound3Index & 0x01000000))520 {521 value &= 0x0f;522 }523 else524 {525 value >>= 4;526 }528 value -= 8;529 value *= 2;531 switch (sound3OutputLevel)532 {533 case 0:534 value = 0;535 break;536 case 1:537 break;538 case 2:539 value = (value >> 1);540 break;541 case 3:542 value = (value >> 2);543 break;544 }545 //value += 1;546 sound3Last = value;547 }549 soundBuffer[2][soundIndex] = value;551 if (sound3On)552 {553 if (sound3ATL)554 {555 sound3ATL -= soundQuality;557 if (sound3ATL <= 0 && sound3Continue)558 {559 gbMemory[NR52] &= 0xfb;560 sound3On = 0;561 }562 }563 }564 }566 void gbSoundChannel4()567 {568 int vol = sound4EnvelopeVolume;570 int value = 0;572 if (sound4Clock <= 0x0c)573 {574 if (sound4On && (sound4ATL || !sound4Continue))575 {576 #define NOISE_ONE_SAMP_SCALE 0x200000578 sound4Index += soundQuality * sound4Skip;579 sound4ShiftIndex += soundQuality * sound4ShiftSkip;581 if (sound4NSteps)582 {583 while (sound4ShiftIndex >= NOISE_ONE_SAMP_SCALE)584 {585 sound4ShiftRight = (((sound4ShiftRight << 6) ^586 (sound4ShiftRight << 5)) & 0x40) |587 (sound4ShiftRight >> 1);588 sound4ShiftIndex -= NOISE_ONE_SAMP_SCALE;589 }590 }591 else592 {593 while (sound4ShiftIndex >= NOISE_ONE_SAMP_SCALE)594 {595 sound4ShiftRight = (((sound4ShiftRight << 14) ^596 (sound4ShiftRight << 13)) & 0x4000) |597 (sound4ShiftRight >> 1);599 sound4ShiftIndex -= NOISE_ONE_SAMP_SCALE;600 }601 }603 sound4Index %= NOISE_ONE_SAMP_SCALE;604 sound4ShiftIndex %= NOISE_ONE_SAMP_SCALE;606 value = ((sound4ShiftRight & 1) * 2 - 1) * vol;607 }608 else609 {610 value = 0;611 }612 }614 soundBuffer[3][soundIndex] = value;616 if (sound4On)617 {618 if (sound4ATL)619 {620 sound4ATL -= soundQuality;622 if (sound4ATL <= 0 && sound4Continue)623 {624 gbMemory[NR52] &= 0xfd;625 sound4On = 0;626 }627 }629 if (sound4EnvelopeATL)630 {631 sound4EnvelopeATL -= soundQuality;633 if (sound4EnvelopeATL <= 0)634 {635 if (sound4EnvelopeUpDown)636 {637 if (sound4EnvelopeVolume < 15)638 sound4EnvelopeVolume++;639 }640 else641 {642 if (sound4EnvelopeVolume)643 sound4EnvelopeVolume--;644 }645 sound4EnvelopeATL += sound4EnvelopeATLReload;646 }647 }648 }649 }651 void gbSoundMix()652 {653 int res = 0;655 if (gbMemory)656 soundBalance = (gbMemory[NR51] & soundEnableFlag & ~soundMutedFlag);658 if (soundBalance & 16)659 {660 res += ((s8)soundBuffer[0][soundIndex]);661 }662 if (soundBalance & 32)663 {664 res += ((s8)soundBuffer[1][soundIndex]);665 }666 if (soundBalance & 64)667 {668 res += ((s8)soundBuffer[2][soundIndex]);669 }670 if (soundBalance & 128)671 {672 res += ((s8)soundBuffer[3][soundIndex]);673 }675 if (gbDigitalSound)676 res = soundLevel1 * 256;677 else678 res *= soundLevel1 * 60;680 if (soundEcho)681 {682 res *= 2;683 res += soundFilter[soundEchoIndex];684 res /= 2;685 soundFilter[soundEchoIndex++] = res;686 }688 if (soundLowPass)689 {690 soundLeft[4] = soundLeft[3];691 soundLeft[3] = soundLeft[2];692 soundLeft[2] = soundLeft[1];693 soundLeft[1] = soundLeft[0];694 soundLeft[0] = res;695 res = (soundLeft[4] + 2 * soundLeft[3] + 8 * soundLeft[2] + 2 * soundLeft[1] +696 soundLeft[0]) / 14;697 }699 bool noSpecialEffects = false;700 #if (defined(WIN32) && !defined(SDL))701 if (theApp.soundRecording || theApp.aviRecording || theApp.nvAudioLog)702 noSpecialEffects = true;703 #endif705 if (!noSpecialEffects)706 {707 switch (soundVolume)708 {709 case 0:710 case 1:711 case 2:712 case 3:713 res *= (soundVolume + 1);714 break;715 case 4:716 res >>= 2;717 break;718 case 5:719 res >>= 1;720 break;721 }722 }724 if (res > 32767)725 res = 32767;726 if (res < -32768)727 res = -32768;729 if (soundReverse && !noSpecialEffects)730 {731 soundFinalWave[++soundBufferIndex] = res;732 if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound))733 printf("oh noes!\n");734 else735 soundFrameSound[++soundFrameSoundWritten] = res;736 }737 else738 {739 soundFinalWave[soundBufferIndex++] = res;740 if (soundFrameSoundWritten >= countof(soundFrameSound))741 printf("oh noes!\n");742 else743 soundFrameSound[soundFrameSoundWritten++] = res;744 }746 res = 0;748 if (soundBalance & 1)749 {750 res += ((s8)soundBuffer[0][soundIndex]);751 }752 if (soundBalance & 2)753 {754 res += ((s8)soundBuffer[1][soundIndex]);755 }756 if (soundBalance & 4)757 {758 res += ((s8)soundBuffer[2][soundIndex]);759 }760 if (soundBalance & 8)761 {762 res += ((s8)soundBuffer[3][soundIndex]);763 }765 if (gbDigitalSound)766 res = soundLevel2 * 256;767 else768 res *= soundLevel2 * 60;770 if (soundEcho)771 {772 res *= 2;773 res += soundFilter[soundEchoIndex];774 res /= 2;775 soundFilter[soundEchoIndex++] = res;777 if (soundEchoIndex >= 4000)778 soundEchoIndex = 0;779 }781 if (soundLowPass)782 {783 soundRight[4] = soundRight[3];784 soundRight[3] = soundRight[2];785 soundRight[2] = soundRight[1];786 soundRight[1] = soundRight[0];787 soundRight[0] = res;788 res = (soundRight[4] + 2 * soundRight[3] + 8 * soundRight[2] + 2 * soundRight[1] +789 soundRight[0]) / 14;790 }792 if (!noSpecialEffects)793 {794 switch (soundVolume)795 {796 case 0:797 case 1:798 case 2:799 case 3:800 res *= (soundVolume + 1);801 break;802 case 4:803 res >>= 2;804 break;805 case 5:806 res >>= 1;807 break;808 }809 }811 if (res > 32767)812 res = 32767;813 if (res < -32768)814 res = -32768;816 if (soundReverse && !noSpecialEffects)817 {818 soundFinalWave[-1 + soundBufferIndex++] = res;819 if ((soundFrameSoundWritten) >= countof(soundFrameSound))820 printf("oh noes!\n");821 else822 soundFrameSound[-1 + soundFrameSoundWritten++] = res;823 }824 else825 {826 soundFinalWave[soundBufferIndex++] = res;827 if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound))828 printf("oh noes!\n");829 else830 soundFrameSound[soundFrameSoundWritten++] = res;831 }832 }834 void gbSoundTick()835 {836 if (systemSoundOn)837 {838 if (soundMasterOn)839 {840 gbSoundChannel1();841 gbSoundChannel2();842 gbSoundChannel3();843 gbSoundChannel4();845 gbSoundMix();846 }847 else848 {849 soundFinalWave[soundBufferIndex++] = 0;850 soundFinalWave[soundBufferIndex++] = 0;851 if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound))853 printf("oh noes!\n");854 else855 {856 soundFrameSound[soundFrameSoundWritten++] = 0;857 soundFrameSound[soundFrameSoundWritten++] = 0;858 }859 }861 soundIndex++;863 if (2 * soundBufferIndex >= soundBufferLen)864 {865 if (systemSoundOn)866 {867 if (soundPaused)868 {869 extern void soundResume();870 soundResume();871 }873 systemSoundWriteToBuffer();874 }875 soundIndex = 0;876 soundBufferIndex = 0;877 }878 }879 }881 void gbSoundReset()882 {883 soundPaused = 1;884 soundPlay = 0;885 SOUND_CLOCK_TICKS = soundQuality * GB_USE_TICKS_AS;886 // soundTicks = SOUND_CLOCK_TICKS;887 soundTicks = 0;888 soundNextPosition = 0;889 soundMasterOn = 1;890 soundIndex = 0;891 soundBufferIndex = 0;892 soundLevel1 = 7;893 soundLevel2 = 7;894 soundVIN = 0;896 sound1On = 0;897 sound1ATL = 0;898 sound1Skip = 0;899 sound1Index = 0;900 sound1Continue = 0;901 sound1EnvelopeVolume = 0;902 sound1EnvelopeATL = 0;903 sound1EnvelopeUpDown = 0;904 sound1EnvelopeATLReload = 0;905 sound1SweepATL = 0;906 sound1SweepATLReload = 0;907 sound1SweepSteps = 0;908 sound1SweepUpDown = 0;909 sound1SweepStep = 0;910 sound1Wave = soundWavePattern[2];912 sound2On = 0;913 sound2ATL = 0;914 sound2Skip = 0;915 sound2Index = 0;916 sound2Continue = 0;917 sound2EnvelopeVolume = 0;918 sound2EnvelopeATL = 0;919 sound2EnvelopeUpDown = 0;920 sound2EnvelopeATLReload = 0;921 sound2Wave = soundWavePattern[2];923 sound3On = 0;924 sound3ATL = 0;925 sound3Skip = 0;926 sound3Index = 0;927 sound3Continue = 0;928 sound3OutputLevel = 0;930 sound4On = 0;931 sound4Clock = 0;932 sound4ATL = 0;933 sound4Skip = 0;934 sound4Index = 0;935 sound4ShiftRight = 0x7f;936 sound4NSteps = 0;937 sound4CountDown = 0;938 sound4Continue = 0;939 sound4EnvelopeVolume = 0;940 sound4EnvelopeATL = 0;941 sound4EnvelopeUpDown = 0;942 sound4EnvelopeATLReload = 0;944 // don't translate945 if (soundDebug)946 {947 log("*** Sound Init ***\n");948 }950 gbSoundEvent(0xff10, 0x80);951 gbSoundEvent(0xff11, 0xbf);952 gbSoundEvent(0xff12, 0xf3);953 gbSoundEvent(0xff14, 0xbf);954 gbSoundEvent(0xff16, 0x3f);955 gbSoundEvent(0xff17, 0x00);956 gbSoundEvent(0xff19, 0xbf);958 gbSoundEvent(0xff1a, 0x7f);959 gbSoundEvent(0xff1b, 0xff);960 gbSoundEvent(0xff1c, 0xbf);961 gbSoundEvent(0xff1e, 0xbf);963 gbSoundEvent(0xff20, 0xff);964 gbSoundEvent(0xff21, 0x00);965 gbSoundEvent(0xff22, 0x00);966 gbSoundEvent(0xff23, 0xbf);967 gbSoundEvent(0xff24, 0x77);968 gbSoundEvent(0xff25, 0xf3);970 gbSoundEvent(0xff26, 0xf0);972 // don't translate973 if (soundDebug)974 {975 log("*** Sound Init Complete ***\n");976 }978 sound1On = 0;979 sound2On = 0;980 sound3On = 0;981 sound4On = 0;983 int addr = 0xff30;985 while (addr < 0xff40)986 {987 gbMemory[addr++] = 0x00;988 gbMemory[addr++] = 0xff;989 }991 memset(soundFinalWave, 0x00, soundBufferLen);993 memset(soundFilter, 0, sizeof(soundFilter));994 soundEchoIndex = 0;995 }997 extern bool soundInit();998 extern void soundShutdown();1000 void gbSoundSetQuality(int quality)1001 {1002 if (soundQuality != quality && systemSoundCanChangeQuality())1003 {1004 if (!soundOffFlag)1005 soundShutdown();1006 soundQuality = quality;1007 soundNextPosition = 0;1008 if (!soundOffFlag)1009 soundInit();1010 SOUND_CLOCK_TICKS = (gbSpeed ? 2 : 1) * GB_USE_TICKS_AS * soundQuality;1011 soundIndex = 0;1012 soundBufferIndex = 0;1013 }1014 else1015 {1016 soundNextPosition = 0;1017 SOUND_CLOCK_TICKS = (gbSpeed ? 2 : 1) * GB_USE_TICKS_AS * soundQuality;1018 soundIndex = 0;1019 soundBufferIndex = 0;1020 }1021 }1023 static int32 soundTicks_int32;1024 static int32 SOUND_CLOCK_TICKS_int32;1025 variable_desc gbSoundSaveStruct[] = {1026 { &soundPaused, sizeof(int32) },1027 { &soundPlay, sizeof(int32) },1028 { &soundTicks_int32, sizeof(int32) },1029 { &SOUND_CLOCK_TICKS_int32, sizeof(int32) },1030 { &soundLevel1, sizeof(int32) },1031 { &soundLevel2, sizeof(int32) },1032 { &soundBalance, sizeof(int32) },1033 { &soundMasterOn, sizeof(int32) },1034 { &soundIndex, sizeof(int32) },1035 { &soundVIN, sizeof(int32) },1036 { &sound1On, sizeof(int32) },1037 { &sound1ATL, sizeof(int32) },1038 { &sound1Skip, sizeof(int32) },1039 { &sound1Index, sizeof(int32) },1040 { &sound1Continue, sizeof(int32) },1041 { &sound1EnvelopeVolume, sizeof(int32) },1042 { &sound1EnvelopeATL, sizeof(int32) },1043 { &sound1EnvelopeATLReload, sizeof(int32) },1044 { &sound1EnvelopeUpDown, sizeof(int32) },1045 { &sound1SweepATL, sizeof(int32) },1046 { &sound1SweepATLReload, sizeof(int32) },1047 { &sound1SweepSteps, sizeof(int32) },1048 { &sound1SweepUpDown, sizeof(int32) },1049 { &sound1SweepStep, sizeof(int32) },1050 { &sound2On, sizeof(int32) },1051 { &sound2ATL, sizeof(int32) },1052 { &sound2Skip, sizeof(int32) },1053 { &sound2Index, sizeof(int32) },1054 { &sound2Continue, sizeof(int32) },1055 { &sound2EnvelopeVolume, sizeof(int32) },1056 { &sound2EnvelopeATL, sizeof(int32) },1057 { &sound2EnvelopeATLReload, sizeof(int32) },1058 { &sound2EnvelopeUpDown, sizeof(int32) },1059 { &sound3On, sizeof(int32) },1060 { &sound3ATL, sizeof(int32) },1061 { &sound3Skip, sizeof(int32) },1062 { &sound3Index, sizeof(int32) },1063 { &sound3Continue, sizeof(int32) },1064 { &sound3OutputLevel, sizeof(int32) },1065 { &sound4On, sizeof(int32) },1066 { &sound4ATL, sizeof(int32) },1067 { &sound4Skip, sizeof(int32) },1068 { &sound4Index, sizeof(int32) },1069 { &sound4Clock, sizeof(int32) },1070 { &sound4ShiftRight, sizeof(int32) },1071 { &sound4ShiftSkip, sizeof(int32) },1072 { &sound4ShiftIndex, sizeof(int32) },1073 { &sound4NSteps, sizeof(int32) },1074 { &sound4CountDown, sizeof(int32) },1075 { &sound4Continue, sizeof(int32) },1076 { &sound4EnvelopeVolume, sizeof(int32) },1077 { &sound4EnvelopeATL, sizeof(int32) },1078 { &sound4EnvelopeATLReload, sizeof(int32) },1079 { &sound4EnvelopeUpDown, sizeof(int32) },1080 { &soundEnableFlag, sizeof(int32) },1081 { NULL, 0 }1082 };1084 //variable_desc gbSoundSaveStructV2[] = {1085 // { &soundTicks, sizeof(soundtick_t) },1086 // { &SOUND_CLOCK_TICKS, sizeof(soundtick_t) },1087 // { &GB_USE_TICKS_AS, sizeof(soundtick_t) },1088 // { NULL, 0 }1089 //};1091 void gbSoundSaveGame(gzFile gzFile)1092 {1093 soundTicks_int32 = (int32) soundTicks;1094 SOUND_CLOCK_TICKS_int32 = (int32) SOUND_CLOCK_TICKS;1096 utilWriteData(gzFile, gbSoundSaveStruct);1098 utilGzWrite(gzFile, soundBuffer, 4 * 735);1099 utilGzWrite(gzFile, soundFinalWave, 2 * 735);1100 utilGzWrite(gzFile, &soundQuality, sizeof(int32));1102 //utilWriteData(gzFile, gbSoundSaveStructV2);1103 }1105 void gbSoundReadGame(int version, gzFile gzFile)1106 {1107 int32 oldSoundPaused = soundPaused;1108 int32 oldSoundEnableFlag = soundEnableFlag;1109 utilReadData(gzFile, gbSoundSaveStruct);1110 soundPaused = oldSoundPaused;1111 soundEnableFlag = oldSoundEnableFlag;1113 soundBufferIndex = soundIndex * 2;1115 utilGzRead(gzFile, soundBuffer, 4 * 735);1116 utilGzRead(gzFile, soundFinalWave, 2 * 735);1118 if (version >= 7)1119 {1120 int quality = 1;1121 utilGzRead(gzFile, &quality, sizeof(int32));1122 gbSoundSetQuality(quality);1123 }1124 else1125 {1126 soundQuality = -1;1127 gbSoundSetQuality(1);1128 }1130 sound1Wave = soundWavePattern[gbMemory[NR11] >> 6];1131 sound2Wave = soundWavePattern[gbMemory[NR21] >> 6];1133 //if(version >= 14) {1134 // utilReadData(gzFile, gbSoundSaveStructV2);1135 //}1136 //else {1137 soundTicks = (soundtick_t) soundTicks_int32;1138 SOUND_CLOCK_TICKS = (soundtick_t) SOUND_CLOCK_TICKS_int32;1139 //}1140 }