Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Simplify how to get the debug stats from StereoResampler
  • Loading branch information
hrydgard committed May 16, 2020
1 parent b6a051d commit fce09f1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 49 deletions.
8 changes: 3 additions & 5 deletions Core/HLE/__sceAudio.cpp
Expand Up @@ -47,7 +47,6 @@
#include "Core/Util/AudioFormat.h"

StereoResampler resampler;
AudioDebugStats g_AudioDebugStats;

// Should be used to lock anything related to the outAudioQueue.
// atomic locks are used on the lock. TODO: make this lock-free
Expand Down Expand Up @@ -100,7 +99,7 @@ static void __AudioCPUMHzChange() {


void __AudioInit() {
memset(&g_AudioDebugStats, 0, sizeof(g_AudioDebugStats));
resampler.ResetStatCounters();
mixFrequency = 44100;
srcFrequency = 0;

Expand Down Expand Up @@ -459,9 +458,8 @@ int __AudioMix(short *outstereo, int numFrames, int sampleRate) {
return resampler.Mix(outstereo, numFrames, false, sampleRate);
}

const AudioDebugStats *__AudioGetDebugStats() {
resampler.GetAudioDebugStats(&g_AudioDebugStats);
return &g_AudioDebugStats;
void __AudioGetDebugStats(char *buf, size_t bufSize) {
resampler.GetAudioDebugStats(buf, bufSize);
}

void __PushExternalAudio(const s32 *audio, int numSamples) {
Expand Down
3 changes: 2 additions & 1 deletion Core/HLE/__sceAudio.h
Expand Up @@ -26,6 +26,7 @@ struct AudioDebugStats {
int underrunCount;
int overrunCount;
int instantSampleRate;
int targetSampleRate;
int lastPushSize;
};

Expand All @@ -44,7 +45,7 @@ void __AudioWakeThreads(AudioChannel &chan, int result, int step);
void __AudioWakeThreads(AudioChannel &chan, int result);

int __AudioMix(short *outstereo, int numSamples, int sampleRate);
const AudioDebugStats *__AudioGetDebugStats();
void __AudioGetDebugStats(char *buf, size_t bufSize);
void __PushExternalAudio(const s32 *audio, int numSamples); // Should not be used in-game, only at the menu!

// Audio Dumping stuff
Expand Down
47 changes: 28 additions & 19 deletions Core/HW/StereoResampler.cpp
Expand Up @@ -56,16 +56,7 @@

StereoResampler::StereoResampler()
: m_bufsize(MAX_SAMPLES_DEFAULT)
, m_lowwatermark(LOW_WATERMARK_DEFAULT)
, m_input_sample_rate(44100)
, m_indexW(0)
, m_indexR(0)
, m_numLeftI(0.0f)
, m_frac(0)
, underrunCount_(0)
, overrunCount_(0)
, sample_rate_(0.0f)
, lastBufSize_(0) {
, m_lowwatermark(LOW_WATERMARK_DEFAULT) {
// Need to have space for the worst case in case it changes.
m_buffer = new int16_t[MAX_SAMPLES_EXTRA * 2]();

Expand All @@ -75,7 +66,9 @@ StereoResampler::StereoResampler()

// If framerate is "close"...
if (refresh != 60.0f && refresh > 50.0f && refresh < 70.0f) {
SetInputSampleRate((int)(44100 * (refresh / 60.0f)));
int input_sample_rate = (int)(44100 * (refresh / 60.0f));
ILOG("StereoResampler: Adjusting target sample rate to %dHz", input_sample_rate);
SetInputSampleRate(input_sample_rate);
}

UpdateBufferSize();
Expand Down Expand Up @@ -273,16 +266,32 @@ void StereoResampler::PushSamples(const s32 *samples, unsigned int num_samples)
lastPushSize_ = num_samples;
}

void StereoResampler::GetAudioDebugStats(AudioDebugStats *stats) {
stats->buffered = lastBufSize_;
stats->underrunCount += underrunCount_;
void StereoResampler::GetAudioDebugStats(char *buf, size_t bufSize) {
snprintf(buf, bufSize,
"Audio buffer: %d/%d (low watermark: %d)\n"
"Underruns: %d\n"
"Overruns: %d\n"
"Sample rate: %d (input: %d)\n"
"Push size: %d\n",
lastBufSize_,
m_bufsize * 2,
m_lowwatermark,
underrunCountTotal_,
overrunCountTotal_,
(int)sample_rate_,
m_input_sample_rate,
lastPushSize_);
underrunCountTotal_ += underrunCount_;
overrunCountTotal_ += overrunCount_;
underrunCount_ = 0;
stats->overrunCount += overrunCount_;
overrunCount_ = 0;
stats->watermark = m_lowwatermark;
stats->bufsize = m_bufsize * 2;
stats->instantSampleRate = (int)sample_rate_;
stats->lastPushSize = lastPushSize_;
}

void StereoResampler::ResetStatCounters() {
underrunCount_ = 0;
overrunCount_ = 0;
underrunCountTotal_ = 0;
overrunCountTotal_ = 0;
}

void StereoResampler::SetInputSampleRate(unsigned int rate) {
Expand Down
25 changes: 14 additions & 11 deletions Core/HW/StereoResampler.h
Expand Up @@ -42,23 +42,26 @@ class StereoResampler {

void DoState(PointerWrap &p);

void GetAudioDebugStats(AudioDebugStats *stats);
void GetAudioDebugStats(char *buf, size_t bufSize);
void ResetStatCounters();

protected:
void UpdateBufferSize();
void SetInputSampleRate(unsigned int rate);

int m_bufsize;
int m_lowwatermark;
unsigned int m_input_sample_rate;
unsigned int m_input_sample_rate = 44100;
int16_t *m_buffer;
volatile u32 m_indexW;
volatile u32 m_indexR;
float m_numLeftI;
u32 m_frac;
int underrunCount_;
int overrunCount_;
float sample_rate_;
int lastBufSize_;
int lastPushSize_;
volatile u32 m_indexW = 0;
volatile u32 m_indexR = 0;
float m_numLeftI = 0.0f;
u32 m_frac = 0;
int underrunCount_ = 0;
int overrunCount_ = 0;
int underrunCountTotal_ = 0;
int overrunCountTotal_ = 0;
float sample_rate_ = 0.0;
int lastBufSize_ = 0;
int lastPushSize_ = 0;
};
15 changes: 2 additions & 13 deletions UI/EmuScreen.cpp
Expand Up @@ -1265,19 +1265,8 @@ static void DrawDebugStats(DrawBuffer *draw2d, const Bounds &bounds) {

static void DrawAudioDebugStats(DrawBuffer *draw2d, const Bounds &bounds) {
FontID ubuntu24("UBUNTU24");
char statbuf[1024] = { 0 };
const AudioDebugStats *stats = __AudioGetDebugStats();
snprintf(statbuf, sizeof(statbuf),
"Audio buffer: %d/%d (low watermark: %d)\n"
"Underruns: %d\n"
"Overruns: %d\n"
"Sample rate: %d\n"
"Push size: %d\n",
stats->buffered, stats->bufsize, stats->watermark,
stats->underrunCount,
stats->overrunCount,
stats->instantSampleRate,
stats->lastPushSize);
char statbuf[4096] = { 0 };
__AudioGetDebugStats(statbuf, sizeof(statbuf));
draw2d->SetFontScale(0.7f, 0.7f);
draw2d->DrawTextRect(ubuntu24, statbuf, bounds.x + 11, bounds.y + 31, bounds.w - 20, bounds.h - 30, 0xc0000000, FLAG_DYNAMIC_ASCII | FLAG_WRAP_TEXT);
draw2d->DrawTextRect(ubuntu24, statbuf, bounds.x + 10, bounds.y + 30, bounds.w - 20, bounds.h - 30, 0xFFFFFFFF, FLAG_DYNAMIC_ASCII | FLAG_WRAP_TEXT);
Expand Down

0 comments on commit fce09f1

Please sign in to comment.