From 359ef875def99a7840c18d67e611debf7fc5edee Mon Sep 17 00:00:00 2001 From: Will Tatam Date: Fri, 18 Nov 2022 12:10:20 +0000 Subject: [PATCH 1/4] Add second audio responsive palette --- wled00/FX.h | 2 +- wled00/FX_fcn.cpp | 13 +++++++------ wled00/const.h | 2 +- wled00/fcn_declare.h | 2 +- wled00/palettes.h | 4 +++- wled00/util.cpp | 25 ++++++++++++++++--------- 6 files changed, 29 insertions(+), 19 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index 49016e975a..ff8fc22f44 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -633,7 +633,7 @@ typedef struct Segment { void drawCharacter(unsigned char chr, int16_t x, int16_t y, uint8_t w, uint8_t h, CRGB color) {} void wu_pixel(uint32_t x, uint32_t y, CRGB c) {} #endif - CRGBPalette16 getAudioPalette(); //WLEDMM netmindz ar palette + CRGBPalette16 getAudioPalette(int pal); //WLEDMM netmindz ar palette } segment; //static int segSize = sizeof(Segment); diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index ace5eaf079..2e9a08d8ec 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -279,7 +279,8 @@ CRGBPalette16 &Segment::loadPalette(CRGBPalette16 &targetPalette, uint8_t pal) { case 12: //Rainbow stripe colors targetPalette = RainbowStripeColors_p; break; case 71: //WLEDMM netmindz ar palette +1 - targetPalette = getAudioPalette(); break; + case 72: //WLEDMM netmindz ar palette +1 + targetPalette = getAudioPalette(pal); break; default: //progmem palettes if (pal>245) { targetPalette = strip.customPalettes[255-pal]; // we checked bounds above @@ -1140,7 +1141,7 @@ uint32_t Segment::color_from_palette(uint16_t i, bool mapping, bool wrap, uint8_ } //WLEDMM netmindz ar palette -CRGBPalette16 Segment::getAudioPalette() { +CRGBPalette16 Segment::getAudioPalette(int pal) { um_data_t *um_data; if (!usermods.getUMData(&um_data, USERMOD_ID_AUDIOREACTIVE)) { um_data = simulateSound(SEGMENT.soundSim); @@ -1149,20 +1150,20 @@ CRGBPalette16 Segment::getAudioPalette() { uint8_t xyz[12]; // Needs to be 4 times however many colors are being used. // 3 colors = 12, 4 colors = 16, etc. - CRGB rgb = getCRGBForBand(0, fftResult); + CRGB rgb = getCRGBForBand(0, fftResult, pal); xyz[0] = 0; // anchor of first color - must be zero xyz[1] = rgb.r; xyz[2] = rgb.g; xyz[3] = rgb.b; - rgb = getCRGBForBand(4, fftResult); + rgb = getCRGBForBand(4, fftResult, pal); xyz[4] = 128; xyz[5] = rgb.r; xyz[6] = rgb.g; xyz[7] = rgb.b; - rgb = getCRGBForBand(8, fftResult); + rgb = getCRGBForBand(8, fftResult, pal); xyz[8] = 255; // anchor of last color - must be 255 xyz[9] = rgb.r; xyz[10] = rgb.g; @@ -1883,5 +1884,5 @@ const char JSON_palette_names[] PROGMEM = R"=====([ "Magenta","Magred","Yelmag","Yelblu","Orange & Teal","Tiamat","April Night","Orangery","C9","Sakura", "Aurora","Atlantica","C9 2","C9 New","Temperature","Aurora 2","Retro Clown","Candy","Toxy Reaf","Fairy Reaf", "Semi Blue","Pink Candy","Red Reaf","Aqua Flash","Yelblu Hot","Lite Light","Red Flash","Blink Red","Red Shift","Red Tide", -"Candy2","Audio Responsive" +"Candy2","Audio Responsive Hue","Audio Responsive Ratio" ])====="; diff --git a/wled00/const.h b/wled00/const.h index a4b951062c..9fd78543f0 100644 --- a/wled00/const.h +++ b/wled00/const.h @@ -5,7 +5,7 @@ * Readability defines and their associated numerical values + compile-time constants */ -#define GRADIENT_PALETTE_COUNT 59 //WLEDMM netmindz ar palette +1 +#define GRADIENT_PALETTE_COUNT 60 //WLEDMM netmindz ar palette +2 //Defaults #define DEFAULT_CLIENT_SSID "Your_Network" diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index 13d2ccc87c..ab97571176 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -334,7 +334,7 @@ int16_t extractModeDefaults(uint8_t mode, const char *segVar); uint16_t crc16(const unsigned char* data_p, size_t length); um_data_t* simulateSound(uint8_t simulationId); void enumerateLedmaps(); -CRGB getCRGBForBand(int x, uint8_t *fftResult); //WLEDMM netmindz ar palette +CRGB getCRGBForBand(int x, uint8_t *fftResult, int pal); //WLEDMM netmindz ar palette #ifdef WLED_ADD_EEPROM_SUPPORT //wled_eeprom.cpp diff --git a/wled00/palettes.h b/wled00/palettes.h index af8cc327d4..4038b747f4 100644 --- a/wled00/palettes.h +++ b/wled00/palettes.h @@ -915,7 +915,9 @@ const byte* const gGradientPalettes[] PROGMEM = { red_shift_gp, //68-55 Red Shift red_tide_gp, //69-56 Red Tide candy2_gp, //70-57 Candy2 - audio_responsive_gp, //71-58 AudioResponsive WLEDMM netmindz ar palette + // Palette contents not actually used as built on the fly, just here to create menu option + audio_responsive_gp, //71-58 AudioResponsive WLEDMM netmindz ar palette - placeholder1 + audio_responsive_gp, //72-59 AudioResponsive WLEDMM netmindz ar palette - placeholder2 }; #endif diff --git a/wled00/util.cpp b/wled00/util.cpp index 17371e7460..2547e3b7ad 100644 --- a/wled00/util.cpp +++ b/wled00/util.cpp @@ -516,17 +516,24 @@ void enumerateLedmaps() { } //WLEDMM netmindz ar palette -CRGB getCRGBForBand(int x, uint8_t *fftResult) { +CRGB getCRGBForBand(int x, uint8_t *fftResult, int pal) { CRGB value; CHSV hsv; - if(x == 0) { - value = CRGB(fftResult[10]/2, fftResult[4]/2, fftResult[0]/2); + if(pal == 71) { // bit hacky to use palette id here, but don't want to litter the code with lots of different methods. TODO: add enum for palette creation type + if(x == 0) { + value = CRGB(fftResult[10]/2, fftResult[4]/2, fftResult[0]/2); + } + else if(x == 255) { + value = CRGB(fftResult[10]/2, fftResult[0]/2, fftResult[4]/2); + } + else { + value = CRGB(fftResult[0]/2, fftResult[4]/2, fftResult[10]/2); + } + } + else if(pal == 72) { + int b = map(x, 0, 255, 0, 16); // convert palette position to freq band + hsv = CHSV(fftResult[b], 255, map(fftResult[b], 0, 255, 50, 255)); // pick hue + hsv2rgb_rainbow(hsv, value); // convert to R,G,B } - else if(x == 255) { - value = CRGB(fftResult[10]/2, fftResult[0]/2, fftResult[4]/2); - } - else { - value = CRGB(fftResult[0]/2, fftResult[4]/2, fftResult[10]/2); - } return value; } \ No newline at end of file From 557532265df3300aaf9a0200b217f473a336d8b2 Mon Sep 17 00:00:00 2001 From: Will Tatam Date: Fri, 18 Nov 2022 12:26:21 +0000 Subject: [PATCH 2/4] Add second audio responsive palette --- wled00/util.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wled00/util.cpp b/wled00/util.cpp index 2547e3b7ad..54436d33cd 100644 --- a/wled00/util.cpp +++ b/wled00/util.cpp @@ -531,8 +531,8 @@ CRGB getCRGBForBand(int x, uint8_t *fftResult, int pal) { } } else if(pal == 72) { - int b = map(x, 0, 255, 0, 16); // convert palette position to freq band - hsv = CHSV(fftResult[b], 255, map(fftResult[b], 0, 255, 50, 255)); // pick hue + int b = map(x, 0, 255, 0, 8); // convert palette position to lower half of freq band + hsv = CHSV(fftResult[b], 255, map(fftResult[b], 0, 255, 30, 255)); // pick hue hsv2rgb_rainbow(hsv, value); // convert to R,G,B } return value; From 850dd89d0604bfdbf68b611722bbf62c99cb1f44 Mon Sep 17 00:00:00 2001 From: Will Tatam Date: Fri, 18 Nov 2022 16:14:06 +0000 Subject: [PATCH 3/4] Fix palette ordering --- wled00/FX_fcn.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 2e9a08d8ec..8a4b320909 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1884,5 +1884,5 @@ const char JSON_palette_names[] PROGMEM = R"=====([ "Magenta","Magred","Yelmag","Yelblu","Orange & Teal","Tiamat","April Night","Orangery","C9","Sakura", "Aurora","Atlantica","C9 2","C9 New","Temperature","Aurora 2","Retro Clown","Candy","Toxy Reaf","Fairy Reaf", "Semi Blue","Pink Candy","Red Reaf","Aqua Flash","Yelblu Hot","Lite Light","Red Flash","Blink Red","Red Shift","Red Tide", -"Candy2","Audio Responsive Hue","Audio Responsive Ratio" +"Candy2","Audio Responsive Ratio","Audio Responsive Hue" ])====="; From 389d0047bc527f1fc9a6460d5436eaaca6389d94 Mon Sep 17 00:00:00 2001 From: Will Tatam Date: Fri, 18 Nov 2022 21:42:44 +0000 Subject: [PATCH 4/4] Use targetPalette.loadDynamicGradientPalette --- wled00/FX.h | 2 +- wled00/FX_fcn.cpp | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/wled00/FX.h b/wled00/FX.h index ff8fc22f44..bd9ae4388e 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -633,7 +633,7 @@ typedef struct Segment { void drawCharacter(unsigned char chr, int16_t x, int16_t y, uint8_t w, uint8_t h, CRGB color) {} void wu_pixel(uint32_t x, uint32_t y, CRGB c) {} #endif - CRGBPalette16 getAudioPalette(int pal); //WLEDMM netmindz ar palette + uint8_t * getAudioPalette(int pal); //WLEDMM netmindz ar palette } segment; //static int segSize = sizeof(Segment); diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 8a4b320909..c7ad5967d2 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -280,7 +280,7 @@ CRGBPalette16 &Segment::loadPalette(CRGBPalette16 &targetPalette, uint8_t pal) { targetPalette = RainbowStripeColors_p; break; case 71: //WLEDMM netmindz ar palette +1 case 72: //WLEDMM netmindz ar palette +1 - targetPalette = getAudioPalette(pal); break; + targetPalette.loadDynamicGradientPalette(getAudioPalette(pal)); break; default: //progmem palettes if (pal>245) { targetPalette = strip.customPalettes[255-pal]; // we checked bounds above @@ -1141,14 +1141,17 @@ uint32_t Segment::color_from_palette(uint16_t i, bool mapping, bool wrap, uint8_ } //WLEDMM netmindz ar palette -CRGBPalette16 Segment::getAudioPalette(int pal) { +uint8_t * Segment::getAudioPalette(int pal) { + // https://forum.makerforums.info/t/hi-is-it-possible-to-define-a-gradient-palette-at-runtime-the-define-gradient-palette-uses-the/63339 + um_data_t *um_data; if (!usermods.getUMData(&um_data, USERMOD_ID_AUDIOREACTIVE)) { um_data = simulateSound(SEGMENT.soundSim); } uint8_t *fftResult = (uint8_t*)um_data->u_data[2]; - uint8_t xyz[12]; // Needs to be 4 times however many colors are being used. - // 3 colors = 12, 4 colors = 16, etc. + + static uint8_t xyz[12]; // Needs to be 4 times however many colors are being used. + // 3 colors = 12, 4 colors = 16, etc. CRGB rgb = getCRGBForBand(0, fftResult, pal); @@ -1169,7 +1172,7 @@ CRGBPalette16 Segment::getAudioPalette(int pal) { xyz[10] = rgb.g; xyz[11] = rgb.b; - return CRGBPalette16(xyz); + return xyz; }