X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=inline;f=sound.c;h=9bbda3f6416ccadf14cca7e66f64c2c8dad2968e;hb=ec4d13a31d4416f0f794f8530d03b820cfa55417;hp=8cdde0e6bb3ca065b85c3dcfce32110d5076e107;hpb=9115e7d2b5e5b8dc00774fe92db97f1d02b2fee1;p=fceu.git diff --git a/sound.c b/sound.c index 8cdde0e..9bbda3f 100644 --- a/sound.c +++ b/sound.c @@ -56,9 +56,11 @@ uint8 decvolume[3]; uint8 realvolume[3]; static int32 count[5]; -static int64 sqacc[2]={0,0}; +static int32 sqacc[2]={0,0}; uint8 sqnon=0; +uint32 soundtsoffs=0; + #undef printf uint16 nreg; @@ -80,7 +82,8 @@ static const uint32 SNoiseFreqTable[0x10]= }; static uint32 NoiseFreqTable[0x10]; -int64 nesincsizeLL; +static int32 nesincsize32; +int64 nesincsize; static const uint8 NTSCPCMTable[0x10]= { @@ -102,7 +105,7 @@ uint32 PSG_base; // $4013 - Size register: Size in bytes = (V+1)*64 -static int64 PCMacc=0; +static int32 PCMacc=0; static int PCMfreq; int32 PCMIRQCount; uint8 PCMBitIndex=0; @@ -152,7 +155,7 @@ static void PrepDPCM() PCMfreq=PALPCMTable[PSG[0x10]&0xF]; else PCMfreq=NTSCPCMTable[PSG[0x10]&0xF]; - PCMacc=(int64)PCMfreq<<50; + PCMacc=PCMfreq<<18; } uint8 sweepon[2]={0,0}; @@ -194,7 +197,7 @@ static uint8 DutyCount[2]={0,0}; static DECLFW(Write_PSG) { //if((A>=0x4004 && A<=0x4007) || A==0x4015) - //printf("$%04x:$%02x, %d\n",A,V,timestamp); + //printf("$%04x:$%02x, %d\n",A,V,SOUNDTS); A&=0x1f; switch(A) { @@ -224,7 +227,7 @@ static DECLFW(Write_PSG) DecCountTo1[0]=(PSG[0]&0xF)+1; SweepCount[0]=((PSG[0x1]>>4)&7)+1; DutyCount[0]=0; - sqacc[0]=((int64)curfreq[0]+1)<<50; + sqacc[0]=((int32)curfreq[0]+1)<<18; break; case 0x4: @@ -253,7 +256,7 @@ static DECLFW(Write_PSG) DecCountTo1[1]=(PSG[0x4]&0xF)+1; SweepCount[1]=((PSG[0x5]>>4)&7)+1; DutyCount[1]=0; - sqacc[1]=((int64)curfreq[1]+1)<<50; + sqacc[1]=((int32)curfreq[1]+1)<<18; break; case 0x8: DoTriangle(); @@ -328,15 +331,6 @@ static DECLFW(Write_PSG) X6502_IRQEnd(FCEU_IQDPCM); } break; - case 0x17: - V&=0xC0; - fcnt=0; - if(V&0x80) - FrameSoundUpdate(); - fhcnt=fhinc; - X6502_IRQEnd(FCEU_IQFCOUNT); - SIRQStat&=~0x40; - break; } PSG[A]=V; } @@ -565,22 +559,22 @@ static void RDoPCM(void) { int32 V; int32 start,end; - int64 freq; + int32 freq; uint32 out=PSG[0x11]<<3; start=ChannelBC[4]; - end=(timestamp<<16)/soundtsinc; + end=(SOUNDTS<<16)/soundtsinc; if(end<=start) return; ChannelBC[4]=end; if(PSG[0x15]&0x10) { freq=PCMfreq; - freq<<=50; + freq<<=18; for(V=start;V>4]+=out; - sqacc[0]-=nesincsizeLL; + sqacc[0]-=nesincsize32; if(sqacc[0]<=0) { rea: @@ -688,11 +682,11 @@ static void RDoSQ2(void) { int32 V; int32 start,end; - int64 freq; + int32 freq; CalcRectAmp(1); start=ChannelBC[1]; - end=(timestamp<<16)/soundtsinc; + end=(SOUNDTS<<16)/soundtsinc; if(end<=start) return; ChannelBC[1]=end; @@ -707,11 +701,11 @@ static void RDoSQ2(void) freq=curfreq[1]+1; { - freq<<=50; + freq<<=18; for(V=start;V>4]+=out; - sqacc[1]-=nesincsizeLL; + sqacc[1]-=nesincsize32; if(sqacc[1]<=0) { rea: @@ -722,7 +716,6 @@ static void RDoSQ2(void) DutyCount[1]&=7; out=RectAmp[1][DutyCount[1]]; } - } } } @@ -734,10 +727,10 @@ static void RDoTriangle(void) static uint32 tcout=0; int32 V; int32 start,end; //,freq; - int64 freq=(((PSG[0xa]|((PSG[0xb]&7)<<8))+1)); + int32 freq=(((PSG[0xa]|((PSG[0xb]&7)<<8))+1)); start=ChannelBC[2]; - end=(timestamp<<16)/soundtsinc; + end=(SOUNDTS<<16)/soundtsinc; if(end<=start) return; ChannelBC[2]=end; @@ -760,13 +753,13 @@ static void RDoTriangle(void) } else { - static int64 triacc=0; + static int32 triacc=0; static uint8 tc=0; - freq<<=49; + freq<<=17; for(V=start;V>6; + fcnt=0; + if(V&0x2) + FrameSoundUpdate(); + fcnt=1; + fhcnt=fhinc; + X6502_IRQEnd(FCEU_IQFCOUNT); + SIRQStat&=~0x40; + //IRQFrameMode=V; // IRQFrameMode is PSG[0x17] upper bits +} + void SetNESSoundMap(void) { SetWriteHandler(0x4000,0x4013,Write_PSG); SetWriteHandler(0x4011,0x4011,Write0x11); SetWriteHandler(0x4015,0x4015,Write_PSG); - SetWriteHandler(0x4017,0x4017,Write_PSG); + SetWriteHandler(0x4017,0x4017,Write_IRQFM); SetReadHandler(0x4015,0x4015,Read_PSG); } -static int32 WaveNSF[256]; +static int32 WaveNSF[2048]; int32 highp; // 0 through 65536, 0 = no high pass, 65536 = max high pass @@ -866,60 +873,29 @@ int32 lowp; // 0 through 65536, 65536 = max low pass(total at // 65536 = no low pass static void FilterSound(uint32 *in, int32 *out, int16 *outMono, int count) { - static int64 acc=0, acc2=0; - //int index=0; - //int16* tmp; - //int16* outorig=out; - //int32 prev=-99999; - for(;count;count--,in++)//,out++)//,index++) + static int32 acc=0, acc2=0; + + for(;count;count--,in++,outMono++) { - int64 diff; + int32 diff; - diff=((int64)*in<<24)-acc; + diff = *in - acc; - acc+=(diff*highp)>>16; - acc2+=((diff-acc2)*lowp)>>16; + acc += (diff*highp)>>16; + acc2+= (int32) (((int64)((diff-acc2)*lowp))>>16); *in=0; - // don't change the sound here -// *out=(acc2*(int64)FSettings.SoundVolume)>>(24+16); - // volume, 4 times louder by default?? -// *out = acc2 >> 24; - // just a bit louder. Hope it's okay - /* - *out = acc2 >> 22; - if(*out<-32767) *out=-32767; - if(*out>32767) *out=32767; - // go one back - - // do MONO - tmp=(int16 *)(out-1); - // don't do this the first time - if (prev == -99999) continue; - // the middle one should be interpolated - tmp[1]=(int16)((*out + prev) >> 1); - prev = *out; - */ - //outMono[index] = (int16)*out; - *outMono = (int16)(acc2 >> 24); - //if(*outMono<-16384) *outMono=-16384; - //if(*outMono>16384) *outMono=16384; - outMono++; - - // out=((int64)(acc2>>24)*(int64)FSettings.SoundVolume)>>16; //acc2>>24; - + *outMono = (int16)acc2; } - // do one more } - +static int32 inbuf=0; int FlushEmulateSound(void) { - uint32 end; int x; - + uint32 end; if(!timestamp) return(0); @@ -929,7 +905,7 @@ int FlushEmulateSound(void) goto nosoundo; } - end=(timestamp<<16)/soundtsinc; + end=(SOUNDTS<<16)/soundtsinc; DoSQ1(); DoSQ2(); DoTriangle(); @@ -944,12 +920,12 @@ int FlushEmulateSound(void) // printf("count %d, num ints %d\n", end, (end >> 4)); if(FCEUGameInfo.type==GIT_NSF) { - printf("IS NSF"); - int x,s=0,si=end/1024; // Only want 1/4 of the output buffer to be displayed - for(x=0;x<256;x++) + int x;//,s=0,si=end/1024; + for(x=0;x<1024;x++) { - WaveNSF[x]=WaveFinal[s>>4]; - s+=si; + //WaveNSF[x]=WaveFinal[s>>4]; + WaveNSF[x]=WaveFinalMono[x]; + //s+=si; } } @@ -960,15 +936,16 @@ int FlushEmulateSound(void) nosoundo: for(x=0;x<5;x++) ChannelBC[x]=end&0xF; - timestampbase+=timestamp; - timestamp=(soundtsinc*(end&0xF))>>16; - timestampbase-=timestamp; - return(end>>4); + soundtsoffs=(soundtsinc*(end&0xF))>>16; + end>>=4; + inbuf=end; + return(end); } -void GetSoundBuffer(int32 **W) +int GetSoundBuffer(int32 **W) { *W=WaveNSF; + return inbuf; } void PowerSound(void) @@ -984,6 +961,7 @@ void PowerSound(void) fhcnt=fhinc; fcnt=0; nreg=1; + soundtsoffs=0; } void ResetSound(void) @@ -1023,7 +1001,9 @@ void SetSoundVariables(void) if(GameExpSound.RChange) GameExpSound.RChange(); - nesincsizeLL=(int64)((int64)562949953421312*(long double)(PAL?PAL_CPU:NTSC_CPU)/(FSettings.SndRate OVERSAMPLE)); + // 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)); + nesincsize32=(int32)nesincsize; PSG_base=(uint32)(PAL?(long double)PAL_CPU/16:(long double)NTSC_CPU/16); for(x=0;x<0x10;x++) @@ -1039,7 +1019,7 @@ void SetSoundVariables(void) for(x=0;x<5;x++) ChannelBC[x]=0; highp=(250<<16)/FSettings.SndRate; // Arbitrary - lowp=((int64)25000<<16)/FSettings.SndRate; // Arbitrary + lowp=(25000<<16)/FSettings.SndRate; // Arbitrary if(highp>(1<<16)) highp=1<<16; if(lowp>(1<<16)) lowp=1<<16; @@ -1062,5 +1042,5 @@ void FCEUI_Sound(int Rate) void FCEUI_SetSoundVolume(uint32 volume) { - FSettings.SoundVolume=(volume<<16)/100; + FSettings.SoundVolume=volume; }