From f5440c235720ca2d6e2a016a21b1dd9067351ea7 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 1 Jul 2014 23:01:30 -0700 Subject: [PATCH 1/2] Fire a callback when cpu mhz changes. So these can adjust efficiently. It's not like it changes often. --- Core/CoreTiming.cpp | 11 +++++++++++ Core/CoreTiming.h | 2 ++ 2 files changed, 13 insertions(+) diff --git a/Core/CoreTiming.cpp b/Core/CoreTiming.cpp index 661aaabef4..5ca41f217e 100644 --- a/Core/CoreTiming.cpp +++ b/Core/CoreTiming.cpp @@ -87,6 +87,7 @@ static std::recursive_mutex externalEventSection; // Warning: not included in save state. void (*advanceCallback)(int cyclesExecuted) = NULL; +std::vector mhzChangeCallbacks; void SetClockFrequencyMHz(int cpuMhz) { @@ -97,6 +98,11 @@ void SetClockFrequencyMHz(int cpuMhz) CPU_HZ = cpuMhz * 1000000; // TODO: Rescale times of scheduled events? + + for (auto it = mhzChangeCallbacks.begin(), end = mhzChangeCallbacks.end(); it != end; ++it) { + MHzChangeCallback cb = *it; + cb(); + } } int GetClockFrequencyMHz() @@ -198,6 +204,7 @@ void Init() lastGlobalTimeTicks = 0; lastGlobalTimeUs = 0; hasTsEvents = 0; + mhzChangeCallbacks.clear(); } void Shutdown() @@ -407,6 +414,10 @@ void RegisterAdvanceCallback(void (*callback)(int cyclesExecuted)) advanceCallback = callback; } +void RegisterMHzChangeCallback(MHzChangeCallback callback) { + mhzChangeCallbacks.push_back(callback); +} + bool IsScheduled(int event_type) { if (!first) diff --git a/Core/CoreTiming.h b/Core/CoreTiming.h index 1bf5fd2efb..6dcb2650a0 100644 --- a/Core/CoreTiming.h +++ b/Core/CoreTiming.h @@ -76,6 +76,7 @@ namespace CoreTiming void Init(); void Shutdown(); + typedef void (*MHzChangeCallback)(); typedef void (*TimedCallback)(u64 userdata, int cyclesLate); u64 GetTicks(); @@ -116,6 +117,7 @@ namespace CoreTiming // Warning: not included in save states. void RegisterAdvanceCallback(void (*callback)(int cyclesExecuted)); + void RegisterMHzChangeCallback(MHzChangeCallback callback); std::string GetScheduledEventsSummary(); From b73c1ba29b13a96f00c25eb169bf36ceab8266ee Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 1 Jul 2014 23:08:38 -0700 Subject: [PATCH 2/2] Reduce audio drift from 44.1kHz. Although, not sure what the PSP's actual exact timing is... --- Core/HLE/__sceAudio.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/Core/HLE/__sceAudio.cpp b/Core/HLE/__sceAudio.cpp index 99daed620f..2d392a56fd 100644 --- a/Core/HLE/__sceAudio.cpp +++ b/Core/HLE/__sceAudio.cpp @@ -52,8 +52,8 @@ const int hwSampleRate = 44100; int hwBlockSize = 64; int hostAttemptBlockSize = 512; -static int audioIntervalUs; -static int audioHostIntervalUs; +static int audioIntervalCycles; +static int audioHostIntervalCycles; static s32 *mixBuffer; @@ -84,19 +84,25 @@ static inline s16 adjustvolume(s16 sample, int vol) { void hleAudioUpdate(u64 userdata, int cyclesLate) { // Schedule the next cycle first. __AudioUpdate() may consume cycles. - CoreTiming::ScheduleEvent(usToCycles(audioIntervalUs) - cyclesLate, eventAudioUpdate, 0); + CoreTiming::ScheduleEvent(audioIntervalCycles - cyclesLate, eventAudioUpdate, 0); __AudioUpdate(); } void hleHostAudioUpdate(u64 userdata, int cyclesLate) { - CoreTiming::ScheduleEvent(usToCycles(audioHostIntervalUs) - cyclesLate, eventHostAudioUpdate, 0); + CoreTiming::ScheduleEvent(audioHostIntervalCycles - cyclesLate, eventHostAudioUpdate, 0); // Not all hosts need this call to poke their audio system once in a while, but those that don't // can just ignore it. host->UpdateSound(); } +void __AudioCPUMHzChange() { + audioIntervalCycles = (int)(usToCycles(1000000ULL) * hwBlockSize / hwSampleRate); + audioHostIntervalCycles = (int)(usToCycles(1000000ULL) * hostAttemptBlockSize / hwSampleRate); +} + + void __AudioInit() { mixFrequency = 44100; @@ -122,14 +128,13 @@ void __AudioInit() { } - audioIntervalUs = (int)(1000000ULL * hwBlockSize / hwSampleRate); - audioHostIntervalUs = (int)(1000000ULL * hostAttemptBlockSize / hwSampleRate); + __AudioCPUMHzChange(); eventAudioUpdate = CoreTiming::RegisterEvent("AudioUpdate", &hleAudioUpdate); eventHostAudioUpdate = CoreTiming::RegisterEvent("AudioUpdateHost", &hleHostAudioUpdate); - CoreTiming::ScheduleEvent(usToCycles(audioIntervalUs), eventAudioUpdate, 0); - CoreTiming::ScheduleEvent(usToCycles(audioHostIntervalUs), eventHostAudioUpdate, 0); + CoreTiming::ScheduleEvent(audioIntervalCycles, eventAudioUpdate, 0); + CoreTiming::ScheduleEvent(audioHostIntervalCycles, eventHostAudioUpdate, 0); for (u32 i = 0; i < PSP_AUDIO_CHANNEL_MAX + 1; i++) chans[i].clear(); @@ -139,8 +144,8 @@ void __AudioInit() { __blockForAudioQueueLock(); outAudioQueue.clear(); __releaseAcquiredLock(); + CoreTiming::RegisterMHzChangeCallback(&__AudioCPUMHzChange); } - void __AudioDoState(PointerWrap &p) { auto s = p.Section("sceAudio", 1); if (!s)