Skip to content

Commit

Permalink
Cleanup audio sync code to use single set of variables for audio data…
Browse files Browse the repository at this point in the history
…, both real and simulated
  • Loading branch information
netmindz committed Mar 5, 2024
1 parent 40170ef commit 7906602
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 42 deletions.
41 changes: 23 additions & 18 deletions src/App/LedEffects.h
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ class BouncingBalls1D: public Effect {
}
}; // BouncingBalls2D

class RingEffect:public Effect {
class RingEffect {
protected:

void setRing(Leds &leds, int ring, CRGB colour) { //so britisch ;-)
Expand All @@ -631,7 +631,7 @@ class RingEffect:public Effect {

};

class RingRandomFlow:public RingEffect {
class RingRandomFlow:public RingEffect, public Effect {
public:
const char * name() {
return "RingRandomFlow 1D";
Expand Down Expand Up @@ -823,7 +823,14 @@ class PopCorn1D: public Effect {

#ifdef STARMOD_USERMOD_WLEDAUDIO

class GEQEffect:public Effect {
class AudioEffect:public Effect {
protected:
uint8_t *fftResult = wledAudioMod->sync.fftResult;
float *volumeSmth = &wledAudioMod->sync.volumeSmth;
};


class GEQEffect:public AudioEffect {
public:
const char * name() {
return "GEQ 2D";
Expand Down Expand Up @@ -877,15 +884,15 @@ class GEQEffect:public Effect {
unsigned8 frBand = ((NUM_BANDS < 16) && (NUM_BANDS > 1)) ? map(band, 0, NUM_BANDS - 1, 0, 15):band; // always use full range. comment out this line to get the previous behaviour.
// frBand = constrain(frBand, 0, 15); //WLEDMM can never be out of bounds (I think...)
unsigned16 colorIndex = frBand * 17; //WLEDMM 0.255
unsigned16 bandHeight = wledAudioMod->fftResults[frBand]; // WLEDMM we use the original ffResult, to preserve accuracy
unsigned16 bandHeight = fftResult[frBand]; // WLEDMM we use the original ffResult, to preserve accuracy

// WLEDMM begin - smooth out bars
if ((pos.x > 0) && (pos.x < (leds.size.x-1)) && (smoothBars)) {
// get height of next (right side) bar
unsigned8 nextband = (remaining < 1)? band +1: band;
nextband = constrain(nextband, 0, 15); // just to be sure
frBand = ((NUM_BANDS < 16) && (NUM_BANDS > 1)) ? map(nextband, 0, NUM_BANDS - 1, 0, 15):nextband; // always use full range. comment out this line to get the previous behaviour.
unsigned16 nextBandHeight = wledAudioMod->fftResults[frBand];
unsigned16 nextBandHeight = fftResult[frBand];
// smooth Band height
bandHeight = (7*bandHeight + 3*lastBandHeight + 3*nextBandHeight) / 12; // yeees, its 12 not 13 (10% amplification)
bandHeight = constrain(bandHeight, 0, 255); // remove potential over/underflows
Expand Down Expand Up @@ -940,7 +947,7 @@ class GEQEffect:public Effect {
}
};

class AudioRings:public RingEffect {
class AudioRings:public RingEffect, public AudioEffect {
public:
const char * name() {
return "AudioRings 1D";
Expand All @@ -952,11 +959,11 @@ class AudioRings:public RingEffect {

byte val;
if(mdl->getValue("inWards").as<bool>()) {
val = wledAudioMod->fftResults[(i*2)];
val = fftResult[(i*2)];
}
else {
int b = 14 -(i*2);
val = wledAudioMod->fftResults[b];
val = fftResult[b];
}

// Visualize leds to the beat
Expand All @@ -972,7 +979,7 @@ class AudioRings:public RingEffect {

}
void setRingFromFtt(Leds &leds, CRGBPalette16 pal, int index, int ring) {
byte val = wledAudioMod->fftResults[index];
byte val = fftResult[index];
// Visualize leds to the beat
CRGB color = ColorFromPalette(pal, val, 255);
color.nscale8_video(val);
Expand All @@ -985,7 +992,7 @@ class AudioRings:public RingEffect {
}
};

class FreqMatrix:public Effect {
class FreqMatrix:public AudioEffect {
public:
char tesst[77];
const char * name() {
Expand Down Expand Up @@ -1049,7 +1056,7 @@ class FreqMatrix:public Effect {
};


class DJLight:public Effect {
class DJLight:public AudioEffect {
public:

const char * name() {
Expand All @@ -1066,8 +1073,6 @@ class DJLight:public Effect {

unsigned8 *aux0 = leds.sharedData.bind(aux0);

uint8_t *fftResult = wledAudioMod->fftResults;
float volumeSmth = wledAudioMod->volumeSmth;

unsigned8 speed = mdl->getValue("speed");
bool candyFactory = mdl->getValue("candyFactory").as<bool>();
Expand All @@ -1087,15 +1092,15 @@ class DJLight:public Effect {
color = CRGB(fftResult[11]/2 + fftResult[12]/4 + fftResult[14]/4, // red : 2412-3704 + 4479-7106
fftResult[4]/2 + fftResult[3]/4, // green: 216-430
fftResult[0]/4 + fftResult[1]/4 + fftResult[2]/4); // blue: 46-216
if ((color.getLuma() < 96) && (volumeSmth >= 1.5f)) { // enhance "almost dark" pixels with yellow, based on not-yet-used channels
if ((color.getLuma() < 96) && (*volumeSmth >= 1.5f)) { // enhance "almost dark" pixels with yellow, based on not-yet-used channels
unsigned yello_g = (fftResult[5] + fftResult[6] + fftResult[7]) / 3;
unsigned yello_r = (fftResult[7] + fftResult[8] + fftResult[9] + fftResult[10]) / 4;
color.green += (uint8_t) yello_g / 2;
color.red += (uint8_t) yello_r / 2;
}
}

if (volumeSmth < 1.0f) color = CRGB(0,0,0); // silence = black
if (*volumeSmth < 1.0f) color = CRGB(0,0,0); // silence = black

// make colors less "pastel", by turning up color saturation in HSV space
if (color.getLuma() > 32) { // don't change "dark" pixels
Expand Down Expand Up @@ -1202,9 +1207,9 @@ class Effects {
#ifdef STARMOD_USERMOD_WLEDAUDIO

if (mdl->getValue("mHead") ) {
leds.fixture->head.x = wledAudioMod->fftResults[3];
leds.fixture->head.y = wledAudioMod->fftResults[8];
leds.fixture->head.z = wledAudioMod->fftResults[13];
leds.fixture->head.x = wledAudioMod->sync.fftResult[3];
leds.fixture->head.y = wledAudioMod->sync.fftResult[8];
leds.fixture->head.z = wledAudioMod->sync.fftResult[13];
}

#endif
Expand Down
34 changes: 10 additions & 24 deletions src/User/UserModWLEDAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class UserModWLEDAudio:public SysModule {
public:

WLEDSync sync;
byte fftResults[NUM_GEQ_CHANNELS]= {0};
float volumeSmth;

UserModWLEDAudio() :SysModule("WLED Audio Sync Receiver") {
};
Expand Down Expand Up @@ -62,14 +60,6 @@ class UserModWLEDAudio:public SysModule {
// SysModule::loop();
if (SysModules::isConnected && sync.read()) {
lastData = millis();
if(debug) USER_PRINTF("WLED-Sync: ");
for (int b = 0; b < NUM_GEQ_CHANNELS; b++) {
byte val = sync.fftResult[b];
fftResults[b] = val;
if(debug) USER_PRINTF("%u ", val);
}
volumeSmth = sync.volumeSmth;
if(debug) USER_PRINTF("\n");
}
else if((lastData == 0) || isTimeout()) { // Could also check for non-silent
simulateSound(UMS_BeatSin);
Expand Down Expand Up @@ -103,15 +93,10 @@ class UserModWLEDAudio:public SysModule {

void simulateSound(uint8_t simulationId)
{
static uint8_t samplePeak;
static float FFT_MajorPeak;
static uint8_t maxVol;
static uint8_t binNum;

static uint16_t volumeRaw;
static float my_magnitude;

uint32_t ms = millis();
uint8_t *fftResults = sync.fftResult;
float volumeSmth = 0;

switch (simulationId) {
default:
Expand Down Expand Up @@ -155,13 +140,14 @@ class UserModWLEDAudio:public SysModule {
break;
}

// samplePeak = random8() > 250;
// FFT_MajorPeak = 21 + (volumeSmth*volumeSmth) / 8.0f; // WLEDMM 21hz...8200hz
// maxVol = 31; // this gets feedback fro UI
// binNum = 8; // this gets feedback fro UI
// volumeRaw = volumeSmth;
// my_magnitude = 10000.0f / 8.0f; //no idea if 10000 is a good value for FFT_Magnitude ???
// if (volumeSmth < 1 ) my_magnitude = 0.001f; // noise gate closed - mute
// sync.samplePeak = random8() > 250;
sync.FFT_MajorPeak = 21 + (volumeSmth*volumeSmth) / 8.0f; // WLEDMM 21hz...8200hz
// sync.maxVol = 31; // this gets feedback fro UI
// sync.binNum = 8; // this gets feedback fro UI
sync.volumeRaw = volumeSmth;
sync.volumeSmth = volumeSmth;
sync.my_magnitude = 10000.0f / 8.0f; //no idea if 10000 is a good value for FFT_Magnitude ???
if (sync.volumeSmth < 1 ) sync.my_magnitude = 0.001f; // noise gate closed - mute

}

Expand Down

0 comments on commit 7906602

Please sign in to comment.