diff --git a/plugins/spu2ghz/dma.cpp b/plugins/spu2ghz/dma.cpp index 36c43fa2db..10d3460b84 100644 --- a/plugins/spu2ghz/dma.cpp +++ b/plugins/spu2ghz/dma.cpp @@ -228,8 +228,6 @@ void DoDMAWrite(int core,u16 *pMem,u32 size) void SPU2readDMA(int core, u16* pMem, u32 size) { - ENTER_CS(&threadSync); - if(hasPtr) TimeUpdate(*cPtr,1); u32 i; @@ -252,13 +250,10 @@ void SPU2readDMA(int core, u16* pMem, u32 size) //Cores[core].Regs.ATTR |= 0x30; Cores[core].TADR=Cores[core].MADR+(size<<1); - LEAVE_CS(&threadSync); } void SPU2writeDMA(int core, u16* pMem, u32 size) { - ENTER_CS(&threadSync); - if(hasPtr) TimeUpdate(*cPtr,1); Cores[core].DMAPtr=pMem; @@ -269,7 +264,6 @@ void SPU2writeDMA(int core, u16* pMem, u32 size) //Cores[core].Regs.ATTR |= 0x30; Cores[core].DMAICounter=1; - LEAVE_CS(&threadSync); return; } @@ -289,8 +283,6 @@ void SPU2writeDMA(int core, u16* pMem, u32 size) } Cores[core].Regs.STATX &= ~0x80; //Cores[core].Regs.ATTR |= 0x30; - - LEAVE_CS(&threadSync); } u32 CALLBACK SPU2ReadMemAddr(int core) diff --git a/plugins/spu2ghz/sndout.cpp b/plugins/spu2ghz/sndout.cpp index cd92e1025a..8a5c8f8cb1 100644 --- a/plugins/spu2ghz/sndout.cpp +++ b/plugins/spu2ghz/sndout.cpp @@ -311,10 +311,11 @@ void UpdateTempoChange() s32 bufferUsage = sndBuffer->GetBufferUsage(); s32 bufferSize = sndBuffer->GetBufferSize(); - + +//Emergency stretch to compensate for FPS fluctuations and keep the buffers happy bool a=(bufferUsage < CurBufferSize * 4); bool b=(bufferUsage >= (bufferSize - CurBufferSize * 4)); - + if(a!=b) { if (bufferUsage < CurBufferSize) { cTempo*=0.75f; } @@ -333,37 +334,40 @@ void UpdateTempoChange() { cTempo = cTempo * 0.9f + lastTempo * 0.1f; } +////////////////////////////////////////////////////////////////////////////// sndBuffer->GetStats(cWritten,cRead,false); - valAccum1 +=(cRead-oldRead); - valAccum2 +=(cInput-oldInput); - numAccum ++; + valAccum1 += (cRead-oldRead); + valAccum2 += (cInput-oldInput); + numAccum++; - oldRead=cRead; - oldInput=cInput; - - if(numAccum>=numUpdates) + oldRead = cRead; + oldInput = cInput; + +//normal stretch, scales sound to game speed + if(numAccum >= numUpdates) { float valAccum = 1.0; - if (valAccum1!=0) + if (valAccum1 != 0) valAccum=valAccum2 / valAccum1; - if((valAccum<1.05)&&(valAccum>0.95)&&(valAccum!=1)) + if((valAccum < 1.05) && (valAccum > 0.95) /*&& (valAccum != 1)*/) { - printf("Timestretch Debug > Playbackpeed: %f (difference disregarded, using 1.0).\n",valAccum); + // printf("Timestretch Debug > Playbackpeed: %f (difference disregarded, using 1.0).\n",valAccum); valAccum = 1; } - else + /*else { printf("Timestretch Debug > Playbackpeed: %f\n",valAccum); - } + }*/ + + if (valAccum != lastTempo) //only update soundtouch object when needed + pSoundTouch->setTempo(valAccum); - pSoundTouch->setTempo(valAccum); - - lastTempo =valAccum; - cTempo=lastTempo; + lastTempo = valAccum; + cTempo = lastTempo; valAccum1=0; valAccum2=0; diff --git a/plugins/spu2ghz/spu2.cpp b/plugins/spu2ghz/spu2.cpp index a4037b7eef..105ffc6e22 100644 --- a/plugins/spu2ghz/spu2.cpp +++ b/plugins/spu2ghz/spu2.cpp @@ -43,7 +43,6 @@ static char *libraryName = "GiGaHeRz's SPU2 (" DWORD CALLBACK TimeThread(PVOID /* unused param */); -bool EnableThread=false; const char *ParamNames[8]={"VOLL","VOLR","PITCH","ADSR1","ADSR2","ENVX","VOLXL","VOLXR"}; const char *AddressNames[6]={"SSAH","SSAL","LSAH","LSAL","NAXH","NAXL"}; @@ -343,16 +342,6 @@ s32 CALLBACK SPU2open(void *pDsp) { spu2open=1; if (!SndInit()) { - if(EnableThread) - { - InitializeCriticalSection(&threadSync); - if ((hThreadFunc=CreateThread(NULL,0,&TimeThread,0,0,(LPDWORD)&ThreadFuncID))==NULL) { - SysMessage("CreateThread Failed."); - SPU2close(); return -1; - } - SetThreadPriority(hThreadFunc,THREAD_PRIORITY_TIME_CRITICAL); - } - srate_pv=(double)SampleRate/48000.0; spdif_init(); @@ -377,15 +366,6 @@ void CALLBACK SPU2close() spdif_shutdown(); - if(EnableThread) - { - if(hasPtr) TimeUpdate(*cPtr,2); - - printf("Waiting for spu2 thread to finish...\n"); - WaitForSingleObject(hThreadFunc,INFINITE); - DeleteCriticalSection(&threadSync); - } - SndClose(); } @@ -571,10 +551,8 @@ DWORD CALLBACK TimeThread(PVOID /* unused param */) } else { - ENTER_CS(&threadSync); Mix(); TicksThread++; - LEAVE_CS(&threadSync); } } return 0; @@ -654,14 +632,7 @@ void CALLBACK TimeUpdate(u32 cClocks, u32 syncType) lClocks+=TickInterval; Cycles++; - if(EnableThread) - { - TicksCore++; - } - else - { - Mix(); - } + Mix(); } } } @@ -695,8 +666,6 @@ void CALLBACK SPU2async(u32 cycles) } numpad_minus_old = numpad_minus; } - - ENTER_CS(&threadSync); if(hasPtr) { @@ -707,8 +676,6 @@ void CALLBACK SPU2async(u32 cycles) pClocks+=cycles; TimeUpdate(pClocks,0); } - - LEAVE_CS(&threadSync); } void CALLBACK SPU2irqCallback(void (*SPU2callback)(),void (*DMA4callback)(),void (*DMA7callback)()) @@ -1183,8 +1150,6 @@ void CALLBACK SPU2writeLog(u32 rmem, u16 value) void CALLBACK SPU2write(u32 rmem, u16 value) { - ENTER_CS(&threadSync); - #ifdef S2R_ENABLE if(!replay_mode) s2r_writereg(Cycles,rmem,value); @@ -1201,7 +1166,6 @@ void CALLBACK SPU2write(u32 rmem, u16 value) spu2Mu16(Cores[0].TSA++)=value; Cores[0].TSA&=0xfffff; - LEAVE_CS(&threadSync); return; } else if(rmem==0x1f9005ac) @@ -1215,7 +1179,6 @@ void CALLBACK SPU2write(u32 rmem, u16 value) spu2Mu16(Cores[1].TSA++)=value; Cores[1].TSA&=0xfffff; - LEAVE_CS(&threadSync); return; } @@ -1507,13 +1470,10 @@ void CALLBACK SPU2write(u32 rmem, u16 value) { UpdateSpdifMode(); } - - LEAVE_CS(&threadSync); } u16 CALLBACK SPU2read(u32 rmem) { - ENTER_CS(&threadSync); // if(!replay_mode) // s2r_readreg(Cycles,rmem); @@ -1544,8 +1504,6 @@ u16 CALLBACK SPU2read(u32 rmem) FileLog("[%10d] SPU2 read mem %x (core %d, register %x): %x\n",Cycles, mem, core, (omem & 0x7ff), ret); } - LEAVE_CS(&threadSync); - return ret; } diff --git a/plugins/spu2ghz/spu2.h b/plugins/spu2ghz/spu2.h index b2fb8edeb5..c8239b20aa 100644 --- a/plugins/spu2ghz/spu2.h +++ b/plugins/spu2ghz/spu2.h @@ -101,13 +101,9 @@ extern CRITICAL_SECTION threadSync; extern u32 lClocks; -extern bool EnableThread; - -#define ENTER_CS(cs) do { if(EnableThread) EnterCriticalSection(cs); } while(0) -#define LEAVE_CS(cs) do { if(EnableThread) LeaveCriticalSection(cs); } while(0) - extern u32* cPtr; extern bool hasPtr; + void CALLBACK TimeUpdate(u32 cClocks, u32 syncType); void TimestretchUpdate(int bufferusage,int buffersize);