2011-06-23 01:25:08 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nyancatanalyzer.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
|
2011-06-23 02:22:56 +02:00
|
|
|
#include <cmath>
|
2011-06-23 01:25:08 +02:00
|
|
|
|
|
|
|
#include <QTimerEvent>
|
|
|
|
|
|
|
|
const char* NyanCatAnalyzer::kName = "Nyan nyan nyan";
|
2011-06-23 14:34:30 +02:00
|
|
|
const float NyanCatAnalyzer::kPixelScale = 0.03f;
|
2011-06-23 01:25:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
NyanCatAnalyzer::NyanCatAnalyzer(QWidget* parent)
|
|
|
|
: Analyzer::Base(parent, 9),
|
|
|
|
cat_(":/nyancat.png"),
|
|
|
|
timer_id_(startTimer(kFrameIntervalMs)),
|
|
|
|
frame_(0)
|
|
|
|
{
|
|
|
|
memset(history_, 0, sizeof(history_));
|
|
|
|
|
|
|
|
for (int i=0 ; i<kRainbowBands ; ++i) {
|
|
|
|
colors_[i] = QPen(QColor::fromHsv(i * 255 / kRainbowBands, 255, 255), 4.5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NyanCatAnalyzer::transform(Scope& s) {
|
|
|
|
m_fht->spectrum(&s.front());
|
|
|
|
}
|
|
|
|
|
|
|
|
void NyanCatAnalyzer::timerEvent(QTimerEvent* e) {
|
|
|
|
if (e->timerId() == timer_id_) {
|
|
|
|
frame_ = (frame_ + 1) % kCatFrameCount;
|
|
|
|
} else {
|
|
|
|
Analyzer::Base::timerEvent(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NyanCatAnalyzer::analyze(QPainter& p, const Analyzer::Scope& s) {
|
|
|
|
// Discard the second half of the transform
|
|
|
|
const int scope_size = s.size() / 2;
|
|
|
|
|
|
|
|
// Transform the music into rainbows!
|
|
|
|
for (int band=0 ; band<kRainbowBands ; ++band) {
|
|
|
|
float* band_start = history_ + band * kHistorySize;
|
|
|
|
|
|
|
|
// Move the history of each band across by 1 frame.
|
|
|
|
memmove(band_start, band_start + 1, (kHistorySize - 1) * sizeof(float));
|
|
|
|
|
|
|
|
// And set the new frame to 0.
|
|
|
|
band_start[kHistorySize-1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now accumulate the scope data into each band. Should maybe use a series
|
|
|
|
// of band pass filters for this, so bands can leak into neighbouring bands,
|
|
|
|
// but for now it's a series of separate square filters.
|
|
|
|
const int samples_per_band = scope_size / kRainbowBands;
|
|
|
|
int sample = 0;
|
|
|
|
for (int band=0 ; band<kRainbowBands ; ++band) {
|
|
|
|
float* accumulator = &history_[(band+1) * kHistorySize - 1];
|
|
|
|
for (int i=0 ; i<samples_per_band ; ++i) {
|
2011-06-23 02:22:56 +02:00
|
|
|
*accumulator += s[sample++];
|
2011-06-23 01:25:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create polylines for the rainbows.
|
|
|
|
const float px_per_frame = float(width()) / kHistorySize;
|
|
|
|
QPointF polyline[kRainbowBands * kHistorySize];
|
|
|
|
QPointF* dest = polyline;
|
|
|
|
float* source = history_;
|
|
|
|
|
|
|
|
for (int band=0 ; band<kRainbowBands ; ++band) {
|
|
|
|
// Calculate the Y position of this band.
|
2011-06-23 02:22:56 +02:00
|
|
|
const float y = float(height()) / (kRainbowBands + 2) * (band + 0.5);
|
Fix compile error on Mac.
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMX,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,XMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMKdo,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,odKMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMWkkl';KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK;'lkkWMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMK ,KKKKKKK0000000000000000000000000000000000KKKKKKK, KMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMK ,KKKKK00000000000000Olld00000olk000000000000KKKKK, KMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMK ,KK00000xddO00000000Oxxk00000kxO000000000000000KK, KMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMK ,KK00000xooO00000000000000000dooooO000OkkO00000KK, KMNkkkkKMMMMMMMMMM
MMMMMMMMMMMMMMMMMK ,KK00000000000000000000000kxx.....odk0Oood00000KK, kKd....;KKNMMMMMMM
MMMMMMMMMMMMMMMMMK ,KK00000000000000000000000, ldddd '0000000000KK, .dddd: dMMMMMMM
MMk.........,MMMMK ,KK000000000000000kod00000, lddddll;.'00000000KK, :lldddd: dMMMMMMM
MMd ,,,,, llNMK ,KK000000000000000Oxx00000, lddddddl,,:;;;;;;:::;,,odddddd: dMMMMMMM
MMd ;;cdd;.. ldl ,KK00000000kxO000000000000, lddddddddo..........cddddddddd: dMMMMMMM
MMx .ooddl ,KK00000000olk0000000OOOkk, lddddddddddddddddddddddddddddd: oNNMMMMM
MMMMK :dddd. ,KK00000000000000000Oll, ;ddddddddddddddddddddddddddddddddddd. KMMMM
MMMMWkk: ..'ddc:; ,KK00kook00000000000Okk: ;dddddddKXx.'dddddddddddddKXx..odddd. KMMMM
MMMMMMM0::. ;;;;, ,KK00kddO00000000000000c ;ddddddddd: dddddddd:;ldddd: ldddd. KMMMM
MMMMMMMMMMo,,,, ,KK000000000000OkkO0000c ;ddxxxxx'...'dddddddd'.cdd'....oxxxx' KMMMM
MMMMMMMMMMMMMMM. ,KK000000000000Olld0000c ;ddkOOOOxddddddddddddddddddddddkOOOO' KMMMM
MMMMMMMMMMMMMMMNN0 ,KKKKK00dllO0000OOO0000c ;ddkOOOOxd: .ddddd' ldddd. .ddkOOOO' KMMMM
MMMMMMMMMMMMMMMMMK ,KKKKKKKkxxO00000000000xcc,..dxxxxdd: ..... ..... .ddxxo''cddNMMMM
MMMMMMMMMMMMMMMdl: .:lKKKKKKKKKKKKKKKKKKKKKKKd::,,lddddl,,,,,,,,,,,,,,,,,:dd;,;llKMMMMMMM
MMMMMMMMMMMMWKK'.......kkkkkkkkkkkkkkkkkkkkkkkkkk. ,lllllllllllllllllllllllll..cMMMMMMMMMM
MMMMMMMMMMMMK lddddddl KMMMMMMMMMMMM
MMMMMMMMMMMMK ldddd:. ,KK' :lll: 'KKKKKKKKKKKK' :lll: 'KK, ;llll 'KKWMMMMMMMMMMMM
MMMMMMMMMMMMK .,,,,. llkMM, ',,,,llkMMMMMMMMMMMMkll,,,,' ,MMkll;,,,,clkMMMMMMMMMMMMMMM
MMMMMMMMMMMMN:::::::::lMMMMMd::::::lMMMMMMMMMMMMMMMMMMl::::::dMMMMMl::::NMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
2011-06-23 13:28:57 +02:00
|
|
|
const float band_scale = std::pow(2.0, band);
|
2011-06-23 01:25:08 +02:00
|
|
|
|
|
|
|
// Add each point in the line.
|
|
|
|
for (int x=0 ; x<kHistorySize ; ++x) {
|
2011-06-23 02:22:56 +02:00
|
|
|
*dest = QPointF(px_per_frame * x, y + *source * kPixelScale * band_scale);
|
2011-06-23 01:25:08 +02:00
|
|
|
++ dest;
|
|
|
|
++ source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the rainbows
|
|
|
|
p.setRenderHint(QPainter::Antialiasing);
|
|
|
|
for (int band=kRainbowBands-1 ; band>=0 ; --band) {
|
|
|
|
p.setPen(colors_[band]);
|
|
|
|
p.drawPolyline(&polyline[band*kHistorySize], kHistorySize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw nyan cat (he's been waiting for this for 50 lines).
|
|
|
|
// Nyan nyan nyan nyan.
|
|
|
|
QRect cat_dest(width() - kCatWidth, (height() - kCatHeight) / 2,
|
|
|
|
kCatWidth, kCatHeight);
|
|
|
|
p.drawPixmap(cat_dest, cat_, CatSourceRect());
|
|
|
|
}
|