merge x6502 code FCEUX
[fceu.git] / sound.c
diff --git a/sound.c b/sound.c
index bb3c89d..f0eeaf8 100644 (file)
--- a/sound.c
+++ b/sound.c
 #include "svga.h"
 #include "sound.h"
 
-uint32 soundtsinc;
-uint32 soundtsi;
-
-uint32 Wave[2048];
-int16 WaveFinalMono[2048];
+uint32 Wave[2048+512];
+int32 WaveHi[40000]; // unused
+int16 WaveFinalMono[2048+512];
 
-EXPSOUND GameExpSound={0,0,0};
+EXPSOUND GameExpSound={0,0,0,0,0,0};
 
 uint8 trimode=0;
 uint8 tricoop=0;
@@ -63,7 +61,7 @@ uint32 soundtsoffs=0;
 #undef printf
 uint16 nreg;
 
-int32 lengthcount[4];
+static int32 lengthcount[4];
 
 extern int soundvol;
 
@@ -81,8 +79,10 @@ static const uint32 SNoiseFreqTable[0x10]=
 };
 static uint32 NoiseFreqTable[0x10];
 
-static int32 nesincsize32;
-int64 nesincsize;
+int32 nesincsize;
+uint32 soundtsinc;
+uint32 soundtsi;
+
 
 static const uint8 NTSCPCMTable[0x10]=
 {
@@ -555,6 +555,31 @@ static void FASTAPASS(1) CalcRectAmp(int P)
    *b=V;
 }
 
+void FCEU_SoundCPUHook(int cycles48)
+{
+ fhcnt-=cycles48;
+ if(fhcnt<=0)
+ {
+  FrameSoundUpdate();
+  fhcnt+=fhinc;
+ }
+
+ if(PCMIRQCount>0)
+ {
+  PCMIRQCount-=cycles48;
+  if(PCMIRQCount<=0)
+  {
+   vdis=1;
+   if((PSG[0x10]&0x80) && !(PSG[0x10]&0x40))
+   {
+    extern uint8 SIRQStat;
+    SIRQStat|=0x80;
+    X6502_IRQBegin(FCEU_IQDPCM);
+   }
+  }
+ }
+}
+
 static void RDoPCM(int32 end)
 {
    int32 V;
@@ -574,7 +599,7 @@ static void RDoPCM(int32 end)
 
       for(V=start;V<end;V++)
       {
-       PCMacc-=nesincsize32;
+       PCMacc-=nesincsize;
        if(PCMacc<=0)
        {
        if(!PCMBitIndex)
@@ -664,7 +689,7 @@ static void RDoSQ1(int32 end)
       for(V=start;V<end;V++)
       {
        Wave[V>>4]+=out;
-       sqacc[0]-=nesincsize32;
+       sqacc[0]-=nesincsize;
        if(sqacc[0]<=0)
        {
         rea:
@@ -710,7 +735,7 @@ static void RDoSQ2(int32 end)
       for(V=start;V<end;V++)
       {
        Wave[V>>4]+=out;
-       sqacc[1]-=nesincsize32;
+       sqacc[1]-=nesincsize;
        if(sqacc[1]<=0)
        {
         rea:
@@ -764,7 +789,7 @@ static void RDoTriangle(int32 end)
       freq<<=17;
       for(V=start;V<end;V++)
       {
-       triacc-=nesincsize32;
+       triacc-=nesincsize;
        if(triacc<=0)
        {
         rea:
@@ -878,6 +903,8 @@ static int32 flt_acc=0, flt_acc2=0;
 static void FilterSound(uint32 *in, int16 *outMono, int count)
 {
 // static int min=0, max=0;
+ int sh=2;
+ if (soundvol < 5) sh += 5 - soundvol;
 
  for(;count;count--,in++,outMono++)
  {
@@ -889,7 +916,7 @@ static void FilterSound(uint32 *in, int16 *outMono, int count)
   flt_acc2+= (int32) (((int64)((diff-flt_acc2)*lowp))>>16);
   *in=0;
 
-  *outMono = flt_acc2*7 >> 2; // * 1.75
+  *outMono = flt_acc2*7 >> sh; // * 7 >> 2 = * 1.75
 //  if (acc2 < min) { printf("min: %i %04x\n", acc2, acc2); min = acc2; }
 //  if (acc2 > max) { printf("max: %i %04x\n", acc2, acc2); max = acc2; }
  }
@@ -996,8 +1023,7 @@ void SetSoundVariables(void)
    GameExpSound.RChange();
 
   // nesincsizeLL=(int64)((int64)562949953421312*(double)(PAL?PAL_CPU:NTSC_CPU)/(FSettings.SndRate OVERSAMPLE));
-  nesincsize=(int64)(((int64)1<<17)*(double)(PAL?PAL_CPU:NTSC_CPU)/(FSettings.SndRate * 16)); // 308845 - 1832727
-  nesincsize32=(int32)nesincsize;
+  nesincsize=(int32)(((int64)1<<17)*(double)(PAL?PAL_CPU:NTSC_CPU)/(FSettings.SndRate * 16)); // 308845 - 1832727
   PSG_base=(uint32)(PAL?(long double)PAL_CPU/16:(long double)NTSC_CPU/16);
 
   for(x=0;x<0x10;x++)