Let nyanalyzer cat nap inbetween songs

This commit is contained in:
David Sansome 2012-10-16 21:20:56 +11:00
parent aff1cc5339
commit 41d8c61e0d
5 changed files with 32 additions and 13 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1012 B

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -49,6 +49,7 @@ Analyzer::Base::Base( QWidget *parent, uint scopeSize )
, m_engine(NULL)
, m_lastScope(512)
, new_frame_(false)
, is_playing_(false)
{
}
@ -81,16 +82,14 @@ void Analyzer::Base::transform( Scope &scope ) //virtual
void Analyzer::Base::paintEvent(QPaintEvent * e)
{
EngineBase *engine = m_engine;
QPainter p(this);
p.fillRect(e->rect(), palette().color(QPalette::Window));
switch( engine->state() )
switch( m_engine->state() )
{
case Engine::Playing:
{
const Engine::Scope &thescope = engine->scope();
const Engine::Scope &thescope = m_engine->scope();
int i = 0;
// convert to mono here - our built in analyzers need mono, but we the engines provide interleaved pcm
@ -100,6 +99,7 @@ void Analyzer::Base::paintEvent(QPaintEvent * e)
i += 2;
}
is_playing_ = true;
transform( m_lastScope );
analyze( p, m_lastScope, new_frame_ );
@ -108,13 +108,16 @@ void Analyzer::Base::paintEvent(QPaintEvent * e)
break;
}
case Engine::Paused:
is_playing_ = false;
analyze(p, m_lastScope, new_frame_);
break;
default:
is_playing_ = false;
demo(p);
}
new_frame_ = false;
}
@ -152,9 +155,6 @@ int Analyzer::Base::resizeForBands( int bands )
return m_fht->size() / 2;
}
void Analyzer::Base::paused(QPainter&) //virtual
{}
void Analyzer::Base::demo(QPainter& p) //virtual
{
static int t = 201; //FIXME make static to namespace perhaps

View File

@ -68,7 +68,6 @@ protected:
virtual void init() {}
virtual void transform( Scope& );
virtual void analyze( QPainter& p, const Scope&, bool new_frame) = 0;
virtual void paused(QPainter& p);
virtual void demo(QPainter& p);
protected:
@ -79,6 +78,7 @@ protected:
Scope m_lastScope;
bool new_frame_;
bool is_playing_;
};

View File

@ -78,7 +78,8 @@ void NyanCatAnalyzer::analyze(QPainter& p, const Analyzer::Scope& s, bool new_fr
// Discard the second half of the transform
const int scope_size = s.size() / 2;
if (new_frame) {
if ((new_frame && is_playing_) ||
(buffer_[0].isNull() && buffer_[1].isNull())) {
// Transform the music into rainbows!
for (int band=0 ; band<kRainbowBands ; ++band) {
float* band_start = history_ + band * kHistorySize;
@ -161,7 +162,10 @@ void NyanCatAnalyzer::analyze(QPainter& p, const Analyzer::Scope& s, bool new_fr
// Draw nyan cat (he's been waiting for this for 75 lines).
// Nyan nyan nyan nyan.
QRect cat_dest(width() - kCatWidth, (height() - kCatHeight) / 2,
kCatWidth, kCatHeight);
p.drawPixmap(cat_dest, cat_, CatSourceRect());
if (!is_playing_) {
// Ssshhh!
p.drawPixmap(SleepingCatDestRect(), cat_, SleepingCatSourceRect());
} else {
p.drawPixmap(CatDestRect(), cat_, CatSourceRect());
}
}

View File

@ -40,8 +40,9 @@ protected:
private:
static const int kCatHeight = 21;
static const int kCatWidth = 34;
static const int kCatFrameCount = 5;
static const int kCatFrameCount = 6;
static const int kRainbowOverlap = 13;
static const int kSleepingCatHeight = 24;
static const int kHistorySize = 128;
static const int kRainbowBands = 6;
@ -54,6 +55,20 @@ private:
return QRect(0, kCatHeight * frame_, kCatWidth, kCatHeight);
}
inline QRect SleepingCatSourceRect() const {
return QRect(0, kCatHeight * kCatFrameCount, kCatWidth, kSleepingCatHeight);
}
inline QRect CatDestRect() const {
return QRect(width() - kCatWidth, (height() - kCatHeight) / 2,
kCatWidth, kCatHeight);
}
inline QRect SleepingCatDestRect() const {
return QRect(width() - kCatWidth, (height() - kSleepingCatHeight) / 2,
kCatWidth, kSleepingCatHeight);
}
private:
// "constants" that get initialised in the constructor
float band_scale_[kRainbowBands];