#include namespace VIZ { Loudness::Loudness(AudioGrabber *pGrabber, Vulcan121 *pVulcan121) : VIZ(pGrabber, pVulcan121) {} void Loudness::onSetup() { if (isSetup) { double i = 1; bool wasFull = false; for (;;) { if (frameWidth >= 1.0) frameWidth = 1; i += wasFull ? -.7 : .5; if (i >= 100) wasFull = true; drawSetup(i / 100.0f); if (wasFull && i >= 100) { for (int j = 0; j < 5; ++j) { usleep(100000); drawSetup(0); usleep(100000); drawSetup(1); } } if (wasFull && i <= 0) { frameWidth = 1; break; } } usleep(200000); isSetup = false; } grabber->requestMode = AudioGrabber::ReqMode::RMS; } void Loudness::onTick(float delta) { if (isSetup) { return; } stereoSample val = grabber->getLoudness(); val.l = val.l > 1.0f ? 1.0f : val.l; val.r = val.r > 1.0f ? 1.0f : val.r; setForChannel(val.l, 0); setForChannel(val.r, 1); drawFrame(0); drawFrame(5); keyboard->sendLedMap(data); } const char *Loudness::name() { return m_name.c_str(); } void Loudness::setForChannel(float value, int channel) { // because we have stereo we need to move on the rows int offset = channel == 0 ? 1 : 3; int until = channel == 0 ? 3 : 5; auto color = colours[channel]; color.a = value * 255.0; for (int i = offset; i < until; ++i) { auto row = keyboard->getRow(i); int to = row->count * value; for (int j = 0; j < row->count; ++j) { auto index = row->index[j]; if (j < to) data.key[index] = color; else data.key[index].a = 0; } } } void Loudness::drawFrame(int toRow) { auto row = keyboard->getRow(toRow); for (int j = 0; j < row->count * frameWidth; ++j) { auto index = row->index[j]; data.key[index] = colours[2]; } } void Loudness::drawSetup(float val) { setForChannel(val, 0); setForChannel(val, 1); frameWidth = val; drawFrame(0); drawFrame(5); keyboard->sendLedMap(data); } }