More averaging in estimation code.

git-svn-id: https://gambatte.svn.sourceforge.net/svnroot/gambatte@170 9dfb2916-2d38-0410-aef4-c5fe6c9ffc24
This commit is contained in:
sinamas 2008-10-15 02:04:15 +00:00
parent 6c5cb46be6
commit 32232f58e1
6 changed files with 25 additions and 12 deletions

View File

@ -19,25 +19,35 @@
#include "rateest.h"
#include <cstdlib>
void RateEst::init(const long srate) {
this->srate.est = srate;
this->srate.var = srate >> 13;
static long limit(long est, const long reference) {
if (est > reference + (reference >> 4))
est = reference + (reference >> 4);
else if (est < reference - (reference >> 4))
est = reference - (reference >> 4);
return est;
}
void RateEst::init(long srate, const long reference) {
this->srate.est = limit(srate, reference);
this->srate.var = srate >> 12;
last = 0;
this->reference = reference;
samples = 0;
count = 16;
count = 32;
}
void RateEst::feed(const long samplesIn) {
samples += samplesIn;
if (--count == 0) {
count = 16;
count = 32;
const usec_t now = getusecs();
if (last) {
long est = samples * 1000000.0f / (now - last) + 0.5f;
est = (srate.est * 15 + est + 8) >> 4;
est = limit((srate.est * 31 + est + 16) >> 5, reference);
srate.var = (srate.var * 15 + std::abs(est - srate.est) + 8) >> 4;
srate.est = est;
}

View File

@ -31,12 +31,15 @@ public:
private:
Result srate;
usec_t last;
long reference;
long samples;
unsigned count;
public:
RateEst(const long srate=0) { init(srate); }
void init(long srate);
RateEst(long srate = 0) { init(srate); }
RateEst(long srate, long reference) { init(srate, reference); }
void init(long srate) { init(srate, srate); }
void init(long srate, long reference);
void feed(long samples);
const Result& result() const { return srate; }
};

View File

@ -150,7 +150,7 @@ int AlsaEngine::write(void *const buffer, const unsigned samples) {
}
if (underrun)
est.init(std::min(est.result().est + (est.result().est >> 10), (long) rate() + (rate() >> 4)));
est.init(est.result().est + (est.result().est >> 10), rate());
return 0;
}

View File

@ -124,7 +124,7 @@ int OssEngine::write(void *const buffer, const unsigned samples) {
if (bstate.fromUnderrun)
est.feed(prevfur - bstate.fromUnderrun);
else
est.init(std::min(est.result().est + (est.result().est >> 10), (long) rate() + (rate() >> 4)));
est.init(est.result().est + (est.result().est >> 10), rate());
}
prevfur = bstate.fromUnderrun + samples;

View File

@ -36,7 +36,7 @@ void FtEst::update(const usec_t t) {
if (--count == 0) {
count = COUNT;
long oldFtAvg = ftAvg;
ftAvg = (ftAvg * 15 + ft + 8) >> 4;
ftAvg = (ftAvg * 31 + ft + 16) >> 5;
if (ftAvg > ((frameTime + (frameTime >> 5)) << COUNT_LOG2))
ftAvg = (frameTime + (frameTime >> 5)) << COUNT_LOG2;

View File

@ -28,7 +28,7 @@
class QHBoxLayout;
class FtEst {
enum { COUNT_LOG2 = 4 };
enum { COUNT_LOG2 = 5 };
enum { COUNT = 1 << COUNT_LOG2 };
long frameTime;