Skip to content

Commit 4b89016

Browse files
committed
audio-enhanced standard effects
backported from WLED-SR
1 parent faa62a9 commit 4b89016

File tree

3 files changed

+114
-12
lines changed

3 files changed

+114
-12
lines changed

wled00/FX.cpp

Lines changed: 104 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ static const char _data_FX_MODE_COMET[] PROGMEM = "Lighthouse@!,Fade rate;!,!;!"
12111211
/*
12121212
* Fireworks function.
12131213
*/
1214-
uint16_t mode_fireworks() {
1214+
static uint16_t mode_fireworks_core(bool useaudio) {
12151215
if (SEGLEN == 1) return mode_static();
12161216
const uint16_t width = SEGMENT.is2D() ? SEGMENT.virtualWidth() : SEGMENT.virtualLength();
12171217
const uint16_t height = SEGMENT.virtualHeight();
@@ -1227,17 +1227,44 @@ uint16_t mode_fireworks() {
12271227
bool valid1 = (SEGENV.aux0 < width*height);
12281228
bool valid2 = (SEGENV.aux1 < width*height);
12291229
uint32_t sv1 = 0, sv2 = 0;
1230+
1231+
// WLEDMM begin
1232+
um_data_t *um_data;
1233+
if (!usermods.getUMData(&um_data, USERMOD_ID_AUDIOREACTIVE)) {
1234+
useaudio = false; // no audio - fallback to standard behaviour (don't use soundSim)
1235+
}
1236+
bool addPixels = true; // false -> inhibit new pixels in silence
1237+
unsigned myIntensity = 129 - (SEGMENT.intensity >> 1); // make parameter explicit, so we can work with it
1238+
int soundColor = -1; // -1 = random color; 0..255 = use as palette index
1239+
1240+
if (useaudio) {
1241+
float volumeSmth = *(float*) um_data->u_data[0];
1242+
float FFT_MajorPeak = *(float*) um_data->u_data[4];
1243+
uint8_t samplePeak = *(uint8_t*)um_data->u_data[3];
1244+
if ((volumeSmth > 1.0f) && (FFT_MajorPeak > 60.0f)) { // we have sound - select color based on major frequency
1245+
float musicIndex = logf(FFT_MajorPeak); // log scaling of peak freq
1246+
soundColor = mapf(musicIndex, 4.6f, 9.06f, 0, 255); // pick color from frequency (4.6 = ln(100), 9.06 = ln(8600))
1247+
soundColor = constrain(soundColor, 0, 255); // remove over-shoot
1248+
if (samplePeak > 0) myIntensity -= myIntensity / 4; // increase effect intensity at peaks
1249+
} else { // silence -> fade away
1250+
valid1 = valid2 = false; // do not copy last pixels
1251+
addPixels = false; // don't add new pixels
1252+
}
1253+
}
1254+
// WLEDMM end
1255+
12301256
if (valid1) sv1 = SEGMENT.is2D() ? SEGMENT.getPixelColorXY(SEGENV.aux0%width, SEGENV.aux0/width) : SEGMENT.getPixelColor(SEGENV.aux0); // get spark color
12311257
if (valid2) sv2 = SEGMENT.is2D() ? SEGMENT.getPixelColorXY(SEGENV.aux1%width, SEGENV.aux1/width) : SEGMENT.getPixelColor(SEGENV.aux1);
12321258
if (!SEGENV.step) SEGMENT.blur(16);
12331259
if (valid1) { if (SEGMENT.is2D()) SEGMENT.setPixelColorXY(SEGENV.aux0%width, SEGENV.aux0/width, sv1); else SEGMENT.setPixelColor(SEGENV.aux0, sv1); } // restore spark color after blur
12341260
if (valid2) { if (SEGMENT.is2D()) SEGMENT.setPixelColorXY(SEGENV.aux1%width, SEGENV.aux1/width, sv2); else SEGMENT.setPixelColor(SEGENV.aux1, sv2); } // restore old spark color after blur
12351261

1262+
if (addPixels) // WLEDMM
12361263
for (int i=0; i<max(1, width/20); i++) {
1237-
if (random8(129 - (SEGMENT.intensity >> 1)) == 0) {
1264+
if (random8(myIntensity) == 0) { // WLEDMM
12381265
uint16_t index = random16(width*height);
12391266
uint16_t j = index % width, k = index / width;
1240-
uint32_t col = SEGMENT.color_from_palette(random8(), false, false, 0);
1267+
uint32_t col = SEGMENT.color_from_palette((soundColor > 0) ? soundColor + random8(24) : random8(), false, false, 0); // WLEDMM
12411268
if (SEGMENT.is2D()) SEGMENT.setPixelColorXY(j, k, col);
12421269
else SEGMENT.setPixelColor(index, col);
12431270
SEGENV.aux1 = SEGENV.aux0; // old spark
@@ -1246,8 +1273,12 @@ uint16_t mode_fireworks() {
12461273
}
12471274
return FRAMETIME;
12481275
}
1276+
1277+
uint16_t mode_fireworks(void) { return mode_fireworks_core(false); }
12491278
static const char _data_FX_MODE_FIREWORKS[] PROGMEM = "Fireworks@,Frequency;!,!;!;12;ix=192,pal=11";
12501279

1280+
uint16_t mode_fireworks_audio(void) { return mode_fireworks_core(true); }
1281+
static const char _data_FX_MODE_FIREWORKS_AR[] PROGMEM = "🎉 audio Fireworks@,Frequency;!,!;!;12;ix=192,pal=11";
12511282

12521283
//Twinkling LEDs running. Inspired by https://github.com/kitesurfer1404/WS2812FX/blob/master/src/custom/Rain.h
12531284
uint16_t mode_rain() {
@@ -3157,7 +3188,7 @@ typedef struct Spark {
31573188
* POPCORN
31583189
* modified from https://github.com/kitesurfer1404/WS2812FX/blob/master/src/custom/Popcorn.h
31593190
*/
3160-
uint16_t mode_popcorn(void) {
3191+
static uint16_t mode_popcorn_core(bool useaudio) {
31613192
if (SEGLEN == 1) return mode_static();
31623193
//allocate segment data
31633194
uint16_t strips = SEGMENT.nrOfVStrips();
@@ -3169,20 +3200,44 @@ uint16_t mode_popcorn(void) {
31693200
bool hasCol2 = SEGCOLOR(2);
31703201
if (!SEGMENT.check2) SEGMENT.fill(hasCol2 ? BLACK : SEGCOLOR(1));
31713202

3203+
// WLEDMM init um_data
3204+
um_data_t *um_data;
3205+
if (!usermods.getUMData(&um_data, USERMOD_ID_AUDIOREACTIVE)) {
3206+
// no audio - fallback to standard behaviour
3207+
useaudio = false;
3208+
um_data = simulateSound(SEGMENT.soundSim); // dummy
3209+
}
3210+
31723211
struct virtualStrip {
3173-
static void runStrip(uint16_t stripNr, Spark* popcorn) {
3212+
static void runStrip(uint16_t stripNr, Spark* popcorn, bool useaudio, um_data_t *um_data) { // WLEDMM added useaudio and um_data
31743213
float gravity = -0.0001 - (SEGMENT.speed/200000.0); // m/s/s
31753214
gravity *= SEGLEN;
31763215

31773216
uint8_t numPopcorn = SEGMENT.intensity*maxNumPopcorn/255;
31783217
if (numPopcorn == 0) numPopcorn = 1;
3218+
// WLEDMM audioreactive vars
3219+
float volumeSmth = *(float*) um_data->u_data[0];
3220+
int16_t volumeRaw = *(int16_t*) um_data->u_data[1];
3221+
uint8_t samplePeak = *(uint8_t*) um_data->u_data[3];
31793222

31803223
for(int i = 0; i < numPopcorn; i++) {
31813224
if (popcorn[i].pos >= 0.0f) { // if kernel is active, update its position
31823225
popcorn[i].pos += popcorn[i].vel;
31833226
popcorn[i].vel += gravity;
31843227
} else { // if kernel is inactive, randomly pop it
3185-
if (random8() < 2) { // POP!!!
3228+
bool doPopCorn = false; // WLEDMM allows to inhibit new pops
3229+
// WLEDMM begin
3230+
if (useaudio) {
3231+
if ( (volumeSmth > 1.0f) // no pops in silence
3232+
&&((samplePeak > 0) || (volumeRaw > 128)) // try to pop at onsets (our peek detector still sucks)
3233+
&&(random8() < 4) ) // stay somewhat random
3234+
doPopCorn = true;
3235+
} else {
3236+
if (random8() < 2) doPopCorn = true; // default POP!!!
3237+
}
3238+
// WLEDMM end
3239+
3240+
if (doPopCorn) { // POP!!!
31863241
popcorn[i].pos = 0.01f;
31873242

31883243
uint16_t peakHeight = 128 + random8(128); //0-255
@@ -3210,12 +3265,16 @@ uint16_t mode_popcorn(void) {
32103265
};
32113266

32123267
for (int stripNr=0; stripNr<strips; stripNr++)
3213-
virtualStrip::runStrip(stripNr, &popcorn[stripNr * maxNumPopcorn]);
3268+
virtualStrip::runStrip(stripNr, &popcorn[stripNr * maxNumPopcorn], useaudio, um_data); // WLEDMM added useaudio and um_data
32143269

32153270
return FRAMETIME;
32163271
}
3272+
3273+
uint16_t mode_popcorn(void) { return mode_popcorn_core(false); }
32173274
static const char _data_FX_MODE_POPCORN[] PROGMEM = "Popcorn@!,!,,,,,Overlay;!,!,!;!;1.5d;m12=1"; //bar WLEDMM 1.5d
32183275

3276+
uint16_t mode_popcorn_audio(void) { return mode_popcorn_core(true); }
3277+
static const char _data_FX_MODE_POPCORN_AR[] PROGMEM = "🎉 audio Popcorn@!,!,,,,,Overlay;!,!,!;!;1.5d;m12=1"; //bar WLEDMM 1.5d
32193278

32203279
//values close to 100 produce 5Hz flicker, which looks very candle-y
32213280
//Inspired by https://github.com/avanhanegem/ArduinoCandleEffectNeoPixel
@@ -3331,7 +3390,7 @@ typedef struct particle {
33313390
float fragment[STARBURST_MAX_FRAG];
33323391
} star;
33333392

3334-
uint16_t mode_starburst(void) {
3393+
static uint16_t mode_starburst_core(bool useaudio) {
33353394
if (SEGLEN == 1) return mode_static();
33363395
uint16_t maxData = FAIR_DATA_PER_SEG; //ESP8266: 256 ESP32: 640
33373396
uint8_t segs = strip.getActiveSegmentsNum();
@@ -3353,18 +3412,44 @@ uint16_t mode_starburst(void) {
33533412
float particleIgnition = 250.0f; // How long to "flash"
33543413
float particleFadeTime = 1500.0f; // Fade out time
33553414

3415+
// WLEDMM init um_data
3416+
um_data_t *um_data;
3417+
if (!usermods.getUMData(&um_data, USERMOD_ID_AUDIOREACTIVE)) {
3418+
// no audio - fallback to standard behaviour
3419+
useaudio = false;
3420+
um_data = simulateSound(SEGMENT.soundSim); // dummy
3421+
}
3422+
float volumeSmth = *(float*) um_data->u_data[0];
3423+
int16_t volumeRaw = *(int16_t*) um_data->u_data[1];
3424+
uint8_t samplePeak = *(uint8_t*) um_data->u_data[3];
3425+
33563426
for (int j = 0; j < numStars; j++)
33573427
{
33583428
// speed to adjust chance of a burst, max is nearly always.
3359-
if (random8((144-(SEGMENT.speed >> 1))) == 0 && stars[j].birth == 0)
3429+
bool doNewStar = random8((144-(SEGMENT.speed >> 1))) == 0; // WLEDMM original spawning trigger
3430+
// WLEDMM begin
3431+
if (useaudio) {
3432+
doNewStar = false;
3433+
int burstplus = (volumeSmth > 159)? 128:0; // high volume -> more stars
3434+
if (volumeRaw <= 56) burstplus = -64; // low volume -> fewer stars
3435+
int birthrate = (144-(SEGMENT.speed >> 1)) - burstplus;
3436+
birthrate = constrain(birthrate, 0, 144);
3437+
if ( (volumeSmth > 1.0f) // no bursts in silence
3438+
&& ((samplePeak > 0) || (volumeRaw > 48)) // try to burst with sound
3439+
&& (random8(birthrate) == 0) ) // original random rate
3440+
doNewStar = true;
3441+
}
3442+
// WLEDMM end
3443+
3444+
if (doNewStar && stars[j].birth == 0) // WLEDMM
33603445
{
33613446
// Pick a random color and location.
33623447
uint16_t startPos = (SEGLEN > 1) ? random16(SEGLEN-1) : 0;
33633448
float multiplier = (float)(random8())/255.0 * 1.0;
33643449

33653450
stars[j].color = CRGB(SEGMENT.color_wheel(random8()));
33663451
stars[j].pos = startPos;
3367-
stars[j].vel = maxSpeed * (float)(random8())/255.0 * multiplier;
3452+
stars[j].vel = maxSpeed * (float)(random8())/255.0f * multiplier;
33683453
stars[j].birth = it;
33693454
stars[j].last = it;
33703455
// more fragments means larger burst effect
@@ -3442,8 +3527,12 @@ uint16_t mode_starburst(void) {
34423527
return FRAMETIME;
34433528
}
34443529
#undef STARBURST_MAX_FRAG
3530+
3531+
uint16_t mode_starburst(void) { return mode_starburst_core(false); }
34453532
static const char _data_FX_MODE_STARBURST[] PROGMEM = "Fireworks Starburst@Chance,Fragments,,,,,Overlay;,!;!;;pal=11,m12=0";
34463533

3534+
uint16_t mode_starburst_audio(void) { return mode_starburst_core(true); }
3535+
static const char _data_FX_MODE_STARBURST_AR[] PROGMEM = "🔨 audio Fw Starburst@Chance,Fragments,,,,,Overlay;,!;!;;pal=11,m12=0";
34473536

34483537
/*
34493538
* Exploding fireworks effect
@@ -8336,6 +8425,11 @@ void WS2812FX::setupEffectData() {
83368425
addEffect(FX_MODE_WAVESINS, &mode_wavesins, _data_FX_MODE_WAVESINS);
83378426
addEffect(FX_MODE_ROCKTAVES, &mode_rocktaves, _data_FX_MODE_ROCKTAVES);
83388427

8428+
// --- WLEDSR experimental 1D audio enhanced
8429+
addEffect(FX_MODE_POPCORN_AR, &mode_popcorn_audio, _data_FX_MODE_POPCORN_AR);
8430+
addEffect(FX_MODE_STARBURST_AR, &mode_starburst_audio, _data_FX_MODE_STARBURST_AR);
8431+
addEffect(FX_MODE_FIREWORKS_AR, &mode_fireworks_audio, _data_FX_MODE_FIREWORKS_AR);
8432+
83398433
// --- 2D effects ---
83408434
#ifndef WLED_DISABLE_2D
83418435
addEffect(FX_MODE_2DSPACESHIPS, &mode_2Dspaceships, _data_FX_MODE_2DSPACESHIPS);

wled00/FX.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,15 @@ void strip_wait_until_idle(String whoCalledMe); // WLEDMM implemented in FX_fcn.
336336
#define FX_MODE_ARTIFX 187 //WLEDMM ARTIFX
337337
#define FX_MODE_PARTYJERK 188
338338

339-
#define MODE_COUNT 189
339+
// Experimental Audioresponsive modes from WLED-SR
340+
// #define FX_MODE_3DSphereMove 189 // experimental WLED-SR "cube" mode
341+
#define FX_MODE_POPCORN_AR 190 // WLED-SR audioreactive popcorn
342+
// #define FX_MODE_MULTI_COMET_AR 191 // WLED-SR audioreactive multi-comet
343+
#define FX_MODE_STARBURST_AR 192 // WLED-SR audioreactive fireworks starburst
344+
// #define FX_MODE_PALETTE_AR 193 // WLED-SR audioreactive palette
345+
#define FX_MODE_FIREWORKS_AR 194 // WLED-SR audioreactive fireworks 1D
346+
347+
#define MODE_COUNT 195
340348

341349
typedef enum mapping1D2D {
342350
M12_Pixels = 0,

wled00/wled.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
// version code in format yymmddb (b = daily build)
11-
#define VERSION 2312290
11+
#define VERSION 2312310
1212

1313
// WLEDMM - you can check for this define in usermods, to only enabled WLEDMM specific code in the "right" fork. Its not defined in AC WLED.
1414
#define _MoonModules_WLED_

0 commit comments

Comments
 (0)