From 760f43c93423e6434890a15c86044916bdc9fc39 Mon Sep 17 00:00:00 2001 From: Devon Richards Date: Thu, 17 Sep 2015 13:15:28 -0500 Subject: [PATCH 1/8] Basic functionality added to hist. Still a little buggy --- ckb.pro | 3 +- src/ckb-hist/ckb-hist.pro | 18 +++++ src/ckb-hist/main.c | 135 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 src/ckb-hist/ckb-hist.pro create mode 100644 src/ckb-hist/main.c diff --git a/ckb.pro b/ckb.pro index e439a68..e8d4278 100644 --- a/ckb.pro +++ b/ckb.pro @@ -8,6 +8,7 @@ SUBDIRS = \ src/ckb-gradient \ src/ckb-pinwheel \ src/ckb-random \ - src/ckb-rain + src/ckb-rain \ + src/ckb-hist QMAKE_MAC_SDK = macosx10.10 diff --git a/src/ckb-hist/ckb-hist.pro b/src/ckb-hist/ckb-hist.pro new file mode 100644 index 0000000..6a5a7c1 --- /dev/null +++ b/src/ckb-hist/ckb-hist.pro @@ -0,0 +1,18 @@ +TEMPLATE = app +TARGET = ckb-hist + +QMAKE_CFLAGS += -std=c99 +QMAKE_MAC_SDK = macosx10.10 + +macx { + DESTDIR = $$PWD/../../ckb.app/Contents/Resources/ckb-animations +} else { + DESTDIR = $$PWD/../../bin/ckb-animations +} + +CONFIG = +QT = +LIBS = + +SOURCES += \ + main.c diff --git a/src/ckb-hist/main.c b/src/ckb-hist/main.c new file mode 100644 index 0000000..5b62184 --- /dev/null +++ b/src/ckb-hist/main.c @@ -0,0 +1,135 @@ +#include "../ckb/ckb-anim.h" +#include + +void ckb_info(){ + // Plugin info + CKB_NAME("Histogram"); + CKB_VERSION("0.0"); + CKB_COPYRIGHT("2015", "RULER501"); + CKB_LICENSE("GPLv2"); + CKB_GUID("{097D69F0-70B2-48B8-AFE2-25A1CDB0D92C}"); + CKB_DESCRIPTION("A spot effect on pressed keys that shows usage"); + + // Effect parameters + CKB_PARAM_AGRADIENT("color", "Fade color:", "", "ffffffff"); + //CKB_PARAM_LONG("frequency", "Spot Radius:", "", "1", "0", "150"); + + // Timing/input parameters + CKB_KPMODE(CKB_KP_POSITION); + CKB_TIMEMODE(CKB_TIME_DURATION); + CKB_REPEAT(FALSE); + CKB_LIVEPARAMS(TRUE); + + // Presets + CKB_PRESET_START("Single Spot"); + CKB_PRESET_PARAM("duration", "1.0"); + //CKB_PRESET_PARAM("radius2", "0"); + CKB_PRESET_PARAM("trigger", "0"); + CKB_PRESET_PARAM("kptrigger", "1"); + CKB_PRESET_END; +} + +ckb_gradient animcolor = { 0 }; +long radius = 1; +int* keyUsages = NULL; +int* keyPressed = NULL; +double* keyTiming = NULL; +double duration = 1.f; + +void ckb_init(ckb_runctx* context){ + keyUsages = calloc(context->keycount, sizeof(int)); + keyPressed = calloc(context->keycount, sizeof(int)); + keyTiming = calloc(context->keycount, sizeof(double)); +} + +void ckb_parameter(ckb_runctx* context, const char* name, const char* value){ + CKB_PARSE_AGRADIENT("color", &animcolor){} + //CKB_PARSE_LONG("frequency", &radius){} + CKB_PARSE_DOUBLE("duration", &duration){} +} + +#define ANIM_MAX (144 * 2) +struct { + int active; + float x, y; + ckb_key* press; +} anim[ANIM_MAX] = { }; + +void anim_add(ckb_key* press, float x, float y){ + for(int i = 0; i < ANIM_MAX; i++){ + if(anim[i].press == press && anim[i].active) + return; + } + for(int i = 0; i < ANIM_MAX; i++){ + if(anim[i].active) + continue; + anim[i].active = 1; + anim[i].x = x; + anim[i].y = y; + anim[i].press = press; + return; + } +} + +void anim_remove(float x, float y){ + for(int i = 0; i < ANIM_MAX; i++){ + if(anim[i].active && anim[i].x == x && anim[i].y == y) + anim[i].active = 0; + } +} + +void ckb_keypress(ckb_runctx* context, ckb_key* key, int x, int y, int state){ + // Add or remove a spot on this key + if(state){ + anim_add(key, x, y); + keyUsages[key - context->keys] += 60*duration; + keyPressed[key - context->keys] = 1; + keyTiming[key - context->keys] = 0; + } + else{ + //anim_remove(x, y); + keyPressed[key - context->keys] = 0; + } +} + +void ckb_start(ckb_runctx* context, int state){ + return; +} + +void ckb_time(ckb_runctx* context, double delta){ + int i = context->keycount; + for(;i --> 0;){ + if(keyUsages[i] && !keyPressed[i]){ + keyTiming[i] -= delta; + while(keyTiming[i] < 0){ + keyUsages[i]--; + keyTiming[i] += 0.03333333333; + } + } + } +} + +inline int max(int a, int b){ + return a > b ? a : b; +} + +int ckb_frame(ckb_runctx* context){ + CKB_KEYCLEAR(context); + // Draw spots + unsigned count = context->keycount; + ckb_key* keys = context->keys; + for(unsigned i = 0; i < ANIM_MAX; i++){ + //if(anim[i].active){ + for(ckb_key* key = keys; key < keys + count; key++){ + if(!anim[i].press || keyUsages[anim[i].press - keys] == 0) + continue; + if(max(abs(anim[i].x - key->x), abs(anim[i].y - key->y)) <= radius){ + float a, r, g, b; + ckb_grad_color(&a, &r, &g, &b, &animcolor, max(keyUsages[anim[i].press - keys],600)/600.f); + ckb_alpha_blend(key, a, r, g, b); + } + } + //} + } + return 0; +} From c542ade477535148508ca7c9c8851bf1a3dad19b Mon Sep 17 00:00:00 2001 From: Devon Richards Date: Wed, 23 Sep 2015 12:37:54 -0500 Subject: [PATCH 2/8] Touch up heat and bump version --- ckb.pro | 2 +- .../ckb-hist.pro => ckb-heat/ckb-heat.pro} | 2 +- src/{ckb-hist => ckb-heat}/main.c | 41 ++++++++++++++----- 3 files changed, 33 insertions(+), 12 deletions(-) rename src/{ckb-hist/ckb-hist.pro => ckb-heat/ckb-heat.pro} (93%) rename src/{ckb-hist => ckb-heat}/main.c (78%) diff --git a/ckb.pro b/ckb.pro index e8d4278..bcf63fb 100644 --- a/ckb.pro +++ b/ckb.pro @@ -9,6 +9,6 @@ SUBDIRS = \ src/ckb-pinwheel \ src/ckb-random \ src/ckb-rain \ - src/ckb-hist + src/ckb-heat QMAKE_MAC_SDK = macosx10.10 diff --git a/src/ckb-hist/ckb-hist.pro b/src/ckb-heat/ckb-heat.pro similarity index 93% rename from src/ckb-hist/ckb-hist.pro rename to src/ckb-heat/ckb-heat.pro index 6a5a7c1..e95fe70 100644 --- a/src/ckb-hist/ckb-hist.pro +++ b/src/ckb-heat/ckb-heat.pro @@ -1,5 +1,5 @@ TEMPLATE = app -TARGET = ckb-hist +TARGET = ckb-heat QMAKE_CFLAGS += -std=c99 QMAKE_MAC_SDK = macosx10.10 diff --git a/src/ckb-hist/main.c b/src/ckb-heat/main.c similarity index 78% rename from src/ckb-hist/main.c rename to src/ckb-heat/main.c index 5b62184..dfe72f5 100644 --- a/src/ckb-hist/main.c +++ b/src/ckb-heat/main.c @@ -3,15 +3,16 @@ void ckb_info(){ // Plugin info - CKB_NAME("Histogram"); - CKB_VERSION("0.0"); + CKB_NAME("HeatMap"); + CKB_VERSION("0.5"); CKB_COPYRIGHT("2015", "RULER501"); - CKB_LICENSE("GPLv2"); + CKB_LICENSE("LGPLv3"); CKB_GUID("{097D69F0-70B2-48B8-AFE2-25A1CDB0D92C}"); CKB_DESCRIPTION("A spot effect on pressed keys that shows usage"); // Effect parameters CKB_PARAM_AGRADIENT("color", "Fade color:", "", "ffffffff"); + CKB_PARAM_BOOL("random", "Random Coloring", 0); //CKB_PARAM_LONG("frequency", "Spot Radius:", "", "1", "0", "150"); // Timing/input parameters @@ -35,6 +36,7 @@ int* keyUsages = NULL; int* keyPressed = NULL; double* keyTiming = NULL; double duration = 1.f; +int randomBright = 0; void ckb_init(ckb_runctx* context){ keyUsages = calloc(context->keycount, sizeof(int)); @@ -46,6 +48,7 @@ void ckb_parameter(ckb_runctx* context, const char* name, const char* value){ CKB_PARSE_AGRADIENT("color", &animcolor){} //CKB_PARSE_LONG("frequency", &radius){} CKB_PARSE_DOUBLE("duration", &duration){} + CKB_PARSE_BOOL("random", &randomBright){} } #define ANIM_MAX (144 * 2) @@ -82,7 +85,7 @@ void ckb_keypress(ckb_runctx* context, ckb_key* key, int x, int y, int state){ // Add or remove a spot on this key if(state){ anim_add(key, x, y); - keyUsages[key - context->keys] += 60*duration; + keyUsages[key - context->keys] += 30*duration; keyPressed[key - context->keys] = 1; keyTiming[key - context->keys] = 0; } @@ -97,7 +100,7 @@ void ckb_start(ckb_runctx* context, int state){ } void ckb_time(ckb_runctx* context, double delta){ - int i = context->keycount; +/* int i = context->keycount; for(;i --> 0;){ if(keyUsages[i] && !keyPressed[i]){ keyTiming[i] -= delta; @@ -106,13 +109,23 @@ void ckb_time(ckb_runctx* context, double delta){ keyTiming[i] += 0.03333333333; } } - } + }*/ } inline int max(int a, int b){ return a > b ? a : b; } - + +inline int min(int a, int b){ + return a < b ? a : b; +} + +inline int clamp(int a, int b, int t){ + return max(min(a,b), t); +} + +int gcounter = 0; + int ckb_frame(ckb_runctx* context){ CKB_KEYCLEAR(context); // Draw spots @@ -121,15 +134,23 @@ int ckb_frame(ckb_runctx* context){ for(unsigned i = 0; i < ANIM_MAX; i++){ //if(anim[i].active){ for(ckb_key* key = keys; key < keys + count; key++){ - if(!anim[i].press || keyUsages[anim[i].press - keys] == 0) + if(!(anim[i].press && keyUsages[anim[i].press - keys])) continue; if(max(abs(anim[i].x - key->x), abs(anim[i].y - key->y)) <= radius){ float a, r, g, b; - ckb_grad_color(&a, &r, &g, &b, &animcolor, max(keyUsages[anim[i].press - keys],600)/600.f); - ckb_alpha_blend(key, a, r, g, b); + if(random) + ckb_grad_color(&a, &r, &g, &b, &animcolor, ((float)(rand() % 1000))/10.f); + else + ckb_grad_color(&a, &r, &g, &b, &animcolor, ((float)min(keyUsages[anim[i].press-keys],300))/3.f); + ckb_alpha_blend(key, a, r, g, b); } } //} } + gcounter++; + if(!(gcounter %= 3)) + for(;count --> 0;) + if(keyUsages[count] && !keyPressed[count]) + keyUsages[count]--; return 0; } From 2320878d49118486aadd244b34c05be0ad3c7913 Mon Sep 17 00:00:00 2001 From: Devon Richards Date: Wed, 23 Sep 2015 12:53:30 -0500 Subject: [PATCH 3/8] Start on visualization(No actual code yet) --- src/ckb-mviz/ckb-mviz.pro | 18 +++++ src/ckb-mviz/main.c | 160 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 src/ckb-mviz/ckb-mviz.pro create mode 100644 src/ckb-mviz/main.c diff --git a/src/ckb-mviz/ckb-mviz.pro b/src/ckb-mviz/ckb-mviz.pro new file mode 100644 index 0000000..f9ee3a9 --- /dev/null +++ b/src/ckb-mviz/ckb-mviz.pro @@ -0,0 +1,18 @@ +TEMPLATE = app +TARGET = ckb-mviz + +QMAKE_CFLAGS += -std=c99 +QMAKE_MAC_SDK = macosx10.10 + +macx { + DESTDIR = $$PWD/../../ckb.app/Contents/Resources/ckb-animations +} else { + DESTDIR = $$PWD/../../bin/ckb-animations +} + +CONFIG = +QT = +LIBS = pulse + +SOURCES += \ + main.c diff --git a/src/ckb-mviz/main.c b/src/ckb-mviz/main.c new file mode 100644 index 0000000..b181f52 --- /dev/null +++ b/src/ckb-mviz/main.c @@ -0,0 +1,160 @@ +#include + +#include + +#include "../ckb/ckb-anim.h" +#include "kiss_fftr.h" + +void ckb_info(){ + // Plugin info + CKB_NAME("Music Visualization"); + CKB_VERSION("0.5"); + CKB_COPYRIGHT("2015", "RULER501"); + CKB_LICENSE("LGPLv3"); + CKB_GUID("{097D69F0-70B2-48B8-AFE2-25CA1DB0D92C}"); + CKB_DESCRIPTION("A collection of music visualization effects"); + + // Effect parameters + CKB_PARAM_AGRADIENT("color", "Fade color:", "", "ffffffff"); + CKB_PARAM_BOOL("random", "Random Coloring", 0); + //CKB_PARAM_LONG("frequency", "Spot Radius:", "", "1", "0", "150"); + + // Timing/input parameters + CKB_KPMODE(CKB_KP_POSITION); + CKB_TIMEMODE(CKB_TIME_DURATION); + CKB_REPEAT(FALSE); + CKB_LIVEPARAMS(TRUE); + + // Presets + CKB_PRESET_START("Single Spot"); + CKB_PRESET_PARAM("duration", "1.0"); + //CKB_PRESET_PARAM("radius2", "0"); + CKB_PRESET_PARAM("trigger", "0"); + CKB_PRESET_PARAM("kptrigger", "1"); + CKB_PRESET_END; +} + +ckb_gradient animcolor = { 0 }; +long radius = 1; +int* keyUsages = NULL; +int* keyPressed = NULL; +double* keyTiming = NULL; +double duration = 1.f; +int randomBright = 0; + +void ckb_init(ckb_runctx* context){ + keyUsages = calloc(context->keycount, sizeof(int)); + keyPressed = calloc(context->keycount, sizeof(int)); + keyTiming = calloc(context->keycount, sizeof(double)); +} + +void ckb_parameter(ckb_runctx* context, const char* name, const char* value){ + CKB_PARSE_AGRADIENT("color", &animcolor){} + //CKB_PARSE_LONG("frequency", &radius){} + CKB_PARSE_DOUBLE("duration", &duration){} + CKB_PARSE_BOOL("random", &randomBright){} +} + +#define ANIM_MAX (144 * 2) +struct { + int active; + float x, y; + ckb_key* press; +} anim[ANIM_MAX] = { }; + +void anim_add(ckb_key* press, float x, float y){ + for(int i = 0; i < ANIM_MAX; i++){ + if(anim[i].press == press && anim[i].active) + return; + } + for(int i = 0; i < ANIM_MAX; i++){ + if(anim[i].active) + continue; + anim[i].active = 1; + anim[i].x = x; + anim[i].y = y; + anim[i].press = press; + return; + } +} + +void anim_remove(float x, float y){ + for(int i = 0; i < ANIM_MAX; i++){ + if(anim[i].active && anim[i].x == x && anim[i].y == y) + anim[i].active = 0; + } +} + +void ckb_keypress(ckb_runctx* context, ckb_key* key, int x, int y, int state){ + // Add or remove a spot on this key + if(state){ + anim_add(key, x, y); + keyUsages[key - context->keys] += 30*duration; + keyPressed[key - context->keys] = 1; + keyTiming[key - context->keys] = 0; + } + else{ + //anim_remove(x, y); + keyPressed[key - context->keys] = 0; + } +} + +void ckb_start(ckb_runctx* context, int state){ + return; +} + +void ckb_time(ckb_runctx* context, double delta){ +/* int i = context->keycount; + for(;i --> 0;){ + if(keyUsages[i] && !keyPressed[i]){ + keyTiming[i] -= delta; + while(keyTiming[i] < 0){ + keyUsages[i]--; + keyTiming[i] += 0.03333333333; + } + } + }*/ +} + +inline int max(int a, int b){ + return a > b ? a : b; +} + +inline int min(int a, int b){ + return a < b ? a : b; +} + +inline int clamp(int a, int b, int t){ + return max(min(a,b), t); +} + +int gcounter = 0; + +int ckb_frame(ckb_runctx* context){ + CKB_KEYCLEAR(context); + // Draw spots + unsigned count = context->keycount; + ckb_key* keys = context->keys; + for(unsigned i = 0; i < ANIM_MAX; i++){ + //if(anim[i].active){ + for(ckb_key* key = keys; key < keys + count; key++){ + if(!(anim[i].press && keyUsages[anim[i].press - keys])) + continue; + if(max(abs(anim[i].x - key->x), abs(anim[i].y - key->y)) <= radius){ + float a, r, g, b; + if(random) + ckb_grad_color(&a, &r, &g, &b, &animcolor, ((float)(rand() % 1000))/10.f); + else + ckb_grad_color(&a, &r, &g, &b, &animcolor, ((float)min(keyUsages[anim[i].press-keys],300))/3.f); + ckb_alpha_blend(key, a, r, g, b); + } + } + //} + } + gcounter++; + if(!(gcounter %= 3)) + for(;count --> 0;) + if(keyUsages[count] && !keyPressed[count]) + keyUsages[count]--; + return 0; +} From 51e887d96550d5d3e36edd4ef73839a87f5be3f3 Mon Sep 17 00:00:00 2001 From: Devon Richards Date: Thu, 24 Sep 2015 09:31:00 -0500 Subject: [PATCH 4/8] Got Music Viz working. Now touchups needed --- ckb.pro | 6 +- quickinstall | 2 +- src/ckb-heat/main.c | 2 +- src/ckb-mviz/COPYING | 11 + src/ckb-mviz/_kiss_fft_guts.h | 164 ++++++++++++++ src/ckb-mviz/ckb-mviz.pro | 9 +- src/ckb-mviz/kiss_fft.c | 408 ++++++++++++++++++++++++++++++++++ src/ckb-mviz/kiss_fft.h | 124 +++++++++++ src/ckb-mviz/kiss_fftr.c | 159 +++++++++++++ src/ckb-mviz/kiss_fftr.h | 46 ++++ src/ckb-mviz/main.c | 141 +++++++++--- 11 files changed, 1036 insertions(+), 36 deletions(-) create mode 100644 src/ckb-mviz/COPYING create mode 100644 src/ckb-mviz/_kiss_fft_guts.h create mode 100644 src/ckb-mviz/kiss_fft.c create mode 100644 src/ckb-mviz/kiss_fft.h create mode 100644 src/ckb-mviz/kiss_fftr.c create mode 100644 src/ckb-mviz/kiss_fftr.h diff --git a/ckb.pro b/ckb.pro index bcf63fb..0323d3d 100644 --- a/ckb.pro +++ b/ckb.pro @@ -1,5 +1,6 @@ TEMPLATE = subdirs -CONFIG += debug_and_release +#CONFIG += debug_and_release +CONFIG = debug SUBDIRS = \ src/ckb-daemon \ src/ckb \ @@ -9,6 +10,7 @@ SUBDIRS = \ src/ckb-pinwheel \ src/ckb-random \ src/ckb-rain \ - src/ckb-heat + src/ckb-heat \ + src/ckb-mviz QMAKE_MAC_SDK = macosx10.10 diff --git a/quickinstall b/quickinstall index ac3e57b..12e4044 100755 --- a/quickinstall +++ b/quickinstall @@ -53,7 +53,7 @@ if [[ -d $SRCDIR ]]; then echo "Compiling binaries..." echo "(This can take a while, please be patient)" newtmp - make -j 24 "$@" 2>$TMPFILE + make -j 8 "$@" 2>$TMPFILE checkfail $? echo "" echo "Finished!" diff --git a/src/ckb-heat/main.c b/src/ckb-heat/main.c index dfe72f5..0235c5e 100644 --- a/src/ckb-heat/main.c +++ b/src/ckb-heat/main.c @@ -138,7 +138,7 @@ int ckb_frame(ckb_runctx* context){ continue; if(max(abs(anim[i].x - key->x), abs(anim[i].y - key->y)) <= radius){ float a, r, g, b; - if(random) + if(randomBright) ckb_grad_color(&a, &r, &g, &b, &animcolor, ((float)(rand() % 1000))/10.f); else ckb_grad_color(&a, &r, &g, &b, &animcolor, ((float)min(keyUsages[anim[i].press-keys],300))/3.f); diff --git a/src/ckb-mviz/COPYING b/src/ckb-mviz/COPYING new file mode 100644 index 0000000..2fc6685 --- /dev/null +++ b/src/ckb-mviz/COPYING @@ -0,0 +1,11 @@ +Copyright (c) 2003-2010 Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/src/ckb-mviz/_kiss_fft_guts.h b/src/ckb-mviz/_kiss_fft_guts.h new file mode 100644 index 0000000..ba66144 --- /dev/null +++ b/src/ckb-mviz/_kiss_fft_guts.h @@ -0,0 +1,164 @@ +/* +Copyright (c) 2003-2010, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/* kiss_fft.h + defines kiss_fft_scalar as either short or a float type + and defines + typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */ +#include "kiss_fft.h" +#include + +#define MAXFACTORS 32 +/* e.g. an fft of length 128 has 4 factors + as far as kissfft is concerned + 4*4*4*2 + */ + +struct kiss_fft_state{ + int nfft; + int inverse; + int factors[2*MAXFACTORS]; + kiss_fft_cpx twiddles[1]; +}; + +/* + Explanation of macros dealing with complex math: + + C_MUL(m,a,b) : m = a*b + C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise + C_SUB( res, a,b) : res = a - b + C_SUBFROM( res , a) : res -= a + C_ADDTO( res , a) : res += a + * */ +#ifdef FIXED_POINT +#if (FIXED_POINT==32) +# define FRACBITS 31 +# define SAMPPROD int64_t +#define SAMP_MAX 2147483647 +#else +# define FRACBITS 15 +# define SAMPPROD int32_t +#define SAMP_MAX 32767 +#endif + +#define SAMP_MIN -SAMP_MAX + +#if defined(CHECK_OVERFLOW) +# define CHECK_OVERFLOW_OP(a,op,b) \ + if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \ + fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); } +#endif + + +# define smul(a,b) ( (SAMPPROD)(a)*(b) ) +# define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS ) + +# define S_MUL(a,b) sround( smul(a,b) ) + +# define C_MUL(m,a,b) \ + do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \ + (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0) + +# define DIVSCALAR(x,k) \ + (x) = sround( smul( x, SAMP_MAX/k ) ) + +# define C_FIXDIV(c,div) \ + do { DIVSCALAR( (c).r , div); \ + DIVSCALAR( (c).i , div); }while (0) + +# define C_MULBYSCALAR( c, s ) \ + do{ (c).r = sround( smul( (c).r , s ) ) ;\ + (c).i = sround( smul( (c).i , s ) ) ; }while(0) + +#else /* not FIXED_POINT*/ + +# define S_MUL(a,b) ( (a)*(b) ) +#define C_MUL(m,a,b) \ + do{ (m).r = (a).r*(b).r - (a).i*(b).i;\ + (m).i = (a).r*(b).i + (a).i*(b).r; }while(0) +# define C_FIXDIV(c,div) /* NOOP */ +# define C_MULBYSCALAR( c, s ) \ + do{ (c).r *= (s);\ + (c).i *= (s); }while(0) +#endif + +#ifndef CHECK_OVERFLOW_OP +# define CHECK_OVERFLOW_OP(a,op,b) /* noop */ +#endif + +#define C_ADD( res, a,b)\ + do { \ + CHECK_OVERFLOW_OP((a).r,+,(b).r)\ + CHECK_OVERFLOW_OP((a).i,+,(b).i)\ + (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \ + }while(0) +#define C_SUB( res, a,b)\ + do { \ + CHECK_OVERFLOW_OP((a).r,-,(b).r)\ + CHECK_OVERFLOW_OP((a).i,-,(b).i)\ + (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \ + }while(0) +#define C_ADDTO( res , a)\ + do { \ + CHECK_OVERFLOW_OP((res).r,+,(a).r)\ + CHECK_OVERFLOW_OP((res).i,+,(a).i)\ + (res).r += (a).r; (res).i += (a).i;\ + }while(0) + +#define C_SUBFROM( res , a)\ + do {\ + CHECK_OVERFLOW_OP((res).r,-,(a).r)\ + CHECK_OVERFLOW_OP((res).i,-,(a).i)\ + (res).r -= (a).r; (res).i -= (a).i; \ + }while(0) + + +#ifdef FIXED_POINT +# define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase)) +# define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase)) +# define HALF_OF(x) ((x)>>1) +#elif defined(USE_SIMD) +# define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) ) +# define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) ) +# define HALF_OF(x) ((x)*_mm_set1_ps(.5)) +#else +# define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase) +# define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase) +# define HALF_OF(x) ((x)*.5) +#endif + +#define kf_cexp(x,phase) \ + do{ \ + (x)->r = KISS_FFT_COS(phase);\ + (x)->i = KISS_FFT_SIN(phase);\ + }while(0) + + +/* a debugging function */ +#define pcpx(c)\ + fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) ) + + +#ifdef KISS_FFT_USE_ALLOCA +// define this to allow use of alloca instead of malloc for temporary buffers +// Temporary buffers are used in two case: +// 1. FFT sizes that have "bad" factors. i.e. not 2,3 and 5 +// 2. "in-place" FFTs. Notice the quotes, since kissfft does not really do an in-place transform. +#include +#define KISS_FFT_TMP_ALLOC(nbytes) alloca(nbytes) +#define KISS_FFT_TMP_FREE(ptr) +#else +#define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes) +#define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr) +#endif diff --git a/src/ckb-mviz/ckb-mviz.pro b/src/ckb-mviz/ckb-mviz.pro index f9ee3a9..cb26fc5 100644 --- a/src/ckb-mviz/ckb-mviz.pro +++ b/src/ckb-mviz/ckb-mviz.pro @@ -3,6 +3,7 @@ TARGET = ckb-mviz QMAKE_CFLAGS += -std=c99 QMAKE_MAC_SDK = macosx10.10 +QMAKE_LFLAGS += -lpulse-simple macx { DESTDIR = $$PWD/../../ckb.app/Contents/Resources/ckb-animations @@ -10,9 +11,11 @@ macx { DESTDIR = $$PWD/../../bin/ckb-animations } -CONFIG = +CONFIG = debug QT = -LIBS = pulse +LIBS = SOURCES += \ - main.c + main.c \ + kiss_fft.c \ + kiss_fftr.c diff --git a/src/ckb-mviz/kiss_fft.c b/src/ckb-mviz/kiss_fft.c new file mode 100644 index 0000000..465d6c9 --- /dev/null +++ b/src/ckb-mviz/kiss_fft.c @@ -0,0 +1,408 @@ +/* +Copyright (c) 2003-2010, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#include "_kiss_fft_guts.h" +/* The guts header contains all the multiplication and addition macros that are defined for + fixed or floating point complex numbers. It also delares the kf_ internal functions. + */ + +static void kf_bfly2( + kiss_fft_cpx * Fout, + const size_t fstride, + const kiss_fft_cfg st, + int m + ) +{ + kiss_fft_cpx * Fout2; + kiss_fft_cpx * tw1 = st->twiddles; + kiss_fft_cpx t; + Fout2 = Fout + m; + do{ + C_FIXDIV(*Fout,2); C_FIXDIV(*Fout2,2); + + C_MUL (t, *Fout2 , *tw1); + tw1 += fstride; + C_SUB( *Fout2 , *Fout , t ); + C_ADDTO( *Fout , t ); + ++Fout2; + ++Fout; + }while (--m); +} + +static void kf_bfly4( + kiss_fft_cpx * Fout, + const size_t fstride, + const kiss_fft_cfg st, + const size_t m + ) +{ + kiss_fft_cpx *tw1,*tw2,*tw3; + kiss_fft_cpx scratch[6]; + size_t k=m; + const size_t m2=2*m; + const size_t m3=3*m; + + + tw3 = tw2 = tw1 = st->twiddles; + + do { + C_FIXDIV(*Fout,4); C_FIXDIV(Fout[m],4); C_FIXDIV(Fout[m2],4); C_FIXDIV(Fout[m3],4); + + C_MUL(scratch[0],Fout[m] , *tw1 ); + C_MUL(scratch[1],Fout[m2] , *tw2 ); + C_MUL(scratch[2],Fout[m3] , *tw3 ); + + C_SUB( scratch[5] , *Fout, scratch[1] ); + C_ADDTO(*Fout, scratch[1]); + C_ADD( scratch[3] , scratch[0] , scratch[2] ); + C_SUB( scratch[4] , scratch[0] , scratch[2] ); + C_SUB( Fout[m2], *Fout, scratch[3] ); + tw1 += fstride; + tw2 += fstride*2; + tw3 += fstride*3; + C_ADDTO( *Fout , scratch[3] ); + + if(st->inverse) { + Fout[m].r = scratch[5].r - scratch[4].i; + Fout[m].i = scratch[5].i + scratch[4].r; + Fout[m3].r = scratch[5].r + scratch[4].i; + Fout[m3].i = scratch[5].i - scratch[4].r; + }else{ + Fout[m].r = scratch[5].r + scratch[4].i; + Fout[m].i = scratch[5].i - scratch[4].r; + Fout[m3].r = scratch[5].r - scratch[4].i; + Fout[m3].i = scratch[5].i + scratch[4].r; + } + ++Fout; + }while(--k); +} + +static void kf_bfly3( + kiss_fft_cpx * Fout, + const size_t fstride, + const kiss_fft_cfg st, + size_t m + ) +{ + size_t k=m; + const size_t m2 = 2*m; + kiss_fft_cpx *tw1,*tw2; + kiss_fft_cpx scratch[5]; + kiss_fft_cpx epi3; + epi3 = st->twiddles[fstride*m]; + + tw1=tw2=st->twiddles; + + do{ + C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3); + + C_MUL(scratch[1],Fout[m] , *tw1); + C_MUL(scratch[2],Fout[m2] , *tw2); + + C_ADD(scratch[3],scratch[1],scratch[2]); + C_SUB(scratch[0],scratch[1],scratch[2]); + tw1 += fstride; + tw2 += fstride*2; + + Fout[m].r = Fout->r - HALF_OF(scratch[3].r); + Fout[m].i = Fout->i - HALF_OF(scratch[3].i); + + C_MULBYSCALAR( scratch[0] , epi3.i ); + + C_ADDTO(*Fout,scratch[3]); + + Fout[m2].r = Fout[m].r + scratch[0].i; + Fout[m2].i = Fout[m].i - scratch[0].r; + + Fout[m].r -= scratch[0].i; + Fout[m].i += scratch[0].r; + + ++Fout; + }while(--k); +} + +static void kf_bfly5( + kiss_fft_cpx * Fout, + const size_t fstride, + const kiss_fft_cfg st, + int m + ) +{ + kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4; + int u; + kiss_fft_cpx scratch[13]; + kiss_fft_cpx * twiddles = st->twiddles; + kiss_fft_cpx *tw; + kiss_fft_cpx ya,yb; + ya = twiddles[fstride*m]; + yb = twiddles[fstride*2*m]; + + Fout0=Fout; + Fout1=Fout0+m; + Fout2=Fout0+2*m; + Fout3=Fout0+3*m; + Fout4=Fout0+4*m; + + tw=st->twiddles; + for ( u=0; ur += scratch[7].r + scratch[8].r; + Fout0->i += scratch[7].i + scratch[8].i; + + scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r); + scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r); + + scratch[6].r = S_MUL(scratch[10].i,ya.i) + S_MUL(scratch[9].i,yb.i); + scratch[6].i = -S_MUL(scratch[10].r,ya.i) - S_MUL(scratch[9].r,yb.i); + + C_SUB(*Fout1,scratch[5],scratch[6]); + C_ADD(*Fout4,scratch[5],scratch[6]); + + scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r); + scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r); + scratch[12].r = - S_MUL(scratch[10].i,yb.i) + S_MUL(scratch[9].i,ya.i); + scratch[12].i = S_MUL(scratch[10].r,yb.i) - S_MUL(scratch[9].r,ya.i); + + C_ADD(*Fout2,scratch[11],scratch[12]); + C_SUB(*Fout3,scratch[11],scratch[12]); + + ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4; + } +} + +/* perform the butterfly for one stage of a mixed radix FFT */ +static void kf_bfly_generic( + kiss_fft_cpx * Fout, + const size_t fstride, + const kiss_fft_cfg st, + int m, + int p + ) +{ + int u,k,q1,q; + kiss_fft_cpx * twiddles = st->twiddles; + kiss_fft_cpx t; + int Norig = st->nfft; + + kiss_fft_cpx * scratch = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC(sizeof(kiss_fft_cpx)*p); + + for ( u=0; u=Norig) twidx-=Norig; + C_MUL(t,scratch[q] , twiddles[twidx] ); + C_ADDTO( Fout[ k ] ,t); + } + k += m; + } + } + KISS_FFT_TMP_FREE(scratch); +} + +static +void kf_work( + kiss_fft_cpx * Fout, + const kiss_fft_cpx * f, + const size_t fstride, + int in_stride, + int * factors, + const kiss_fft_cfg st + ) +{ + kiss_fft_cpx * Fout_beg=Fout; + const int p=*factors++; /* the radix */ + const int m=*factors++; /* stage's fft length/p */ + const kiss_fft_cpx * Fout_end = Fout + p*m; + +#ifdef _OPENMP + // use openmp extensions at the + // top-level (not recursive) + if (fstride==1 && p<=5) + { + int k; + + // execute the p different work units in different threads +# pragma omp parallel for + for (k=0;k floor_sqrt) + p = n; /* no more factors, skip to end */ + } + n /= p; + *facbuf++ = p; + *facbuf++ = n; + } while (n > 1); +} + +/* + * + * User-callable function to allocate all necessary storage space for the fft. + * + * The return value is a contiguous block of memory, allocated with malloc. As such, + * It can be freed with free(), rather than a kiss_fft-specific function. + * */ +kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem ) +{ + kiss_fft_cfg st=NULL; + size_t memneeded = sizeof(struct kiss_fft_state) + + sizeof(kiss_fft_cpx)*(nfft-1); /* twiddle factors*/ + + if ( lenmem==NULL ) { + st = ( kiss_fft_cfg)KISS_FFT_MALLOC( memneeded ); + }else{ + if (mem != NULL && *lenmem >= memneeded) + st = (kiss_fft_cfg)mem; + *lenmem = memneeded; + } + if (st) { + int i; + st->nfft=nfft; + st->inverse = inverse_fft; + + for (i=0;iinverse) + phase *= -1; + kf_cexp(st->twiddles+i, phase ); + } + + kf_factor(nfft,st->factors); + } + return st; +} + + +void kiss_fft_stride(kiss_fft_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int in_stride) +{ + if (fin == fout) { + //NOTE: this is not really an in-place FFT algorithm. + //It just performs an out-of-place FFT into a temp buffer + kiss_fft_cpx * tmpbuf = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC( sizeof(kiss_fft_cpx)*st->nfft); + kf_work(tmpbuf,fin,1,in_stride, st->factors,st); + memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft); + KISS_FFT_TMP_FREE(tmpbuf); + }else{ + kf_work( fout, fin, 1,in_stride, st->factors,st ); + } +} + +void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout) +{ + kiss_fft_stride(cfg,fin,fout,1); +} + + +void kiss_fft_cleanup(void) +{ + // nothing needed any more +} + +int kiss_fft_next_fast_size(int n) +{ + while(1) { + int m=n; + while ( (m%2) == 0 ) m/=2; + while ( (m%3) == 0 ) m/=3; + while ( (m%5) == 0 ) m/=5; + if (m<=1) + break; /* n is completely factorable by twos, threes, and fives */ + n++; + } + return n; +} diff --git a/src/ckb-mviz/kiss_fft.h b/src/ckb-mviz/kiss_fft.h new file mode 100644 index 0000000..64c50f4 --- /dev/null +++ b/src/ckb-mviz/kiss_fft.h @@ -0,0 +1,124 @@ +#ifndef KISS_FFT_H +#define KISS_FFT_H + +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + ATTENTION! + If you would like a : + -- a utility that will handle the caching of fft objects + -- real-only (no imaginary time component ) FFT + -- a multi-dimensional FFT + -- a command-line utility to perform ffts + -- a command-line utility to perform fast-convolution filtering + + Then see kfc.h kiss_fftr.h kiss_fftnd.h fftutil.c kiss_fastfir.c + in the tools/ directory. +*/ + +#ifdef USE_SIMD +# include +# define kiss_fft_scalar __m128 +#define KISS_FFT_MALLOC(nbytes) _mm_malloc(nbytes,16) +#define KISS_FFT_FREE _mm_free +#else +#define KISS_FFT_MALLOC malloc +#define KISS_FFT_FREE free +#endif + + +#ifdef FIXED_POINT +#include +# if (FIXED_POINT == 32) +# define kiss_fft_scalar int32_t +# else +# define kiss_fft_scalar int16_t +# endif +#else +# ifndef kiss_fft_scalar +/* default is float */ +# define kiss_fft_scalar float +# endif +#endif + +typedef struct { + kiss_fft_scalar r; + kiss_fft_scalar i; +}kiss_fft_cpx; + +typedef struct kiss_fft_state* kiss_fft_cfg; + +/* + * kiss_fft_alloc + * + * Initialize a FFT (or IFFT) algorithm's cfg/state buffer. + * + * typical usage: kiss_fft_cfg mycfg=kiss_fft_alloc(1024,0,NULL,NULL); + * + * The return value from fft_alloc is a cfg buffer used internally + * by the fft routine or NULL. + * + * If lenmem is NULL, then kiss_fft_alloc will allocate a cfg buffer using malloc. + * The returned value should be free()d when done to avoid memory leaks. + * + * The state can be placed in a user supplied buffer 'mem': + * If lenmem is not NULL and mem is not NULL and *lenmem is large enough, + * then the function places the cfg in mem and the size used in *lenmem + * and returns mem. + * + * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough), + * then the function returns NULL and places the minimum cfg + * buffer size in *lenmem. + * */ + +kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem); + +/* + * kiss_fft(cfg,in_out_buf) + * + * Perform an FFT on a complex input buffer. + * for a forward FFT, + * fin should be f[0] , f[1] , ... ,f[nfft-1] + * fout will be F[0] , F[1] , ... ,F[nfft-1] + * Note that each element is complex and can be accessed like + f[k].r and f[k].i + * */ +void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout); + +/* + A more generic version of the above function. It reads its input from every Nth sample. + * */ +void kiss_fft_stride(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int fin_stride); + +/* If kiss_fft_alloc allocated a buffer, it is one contiguous + buffer and can be simply free()d when no longer needed*/ +#define kiss_fft_free free + +/* + Cleans up some memory that gets managed internally. Not necessary to call, but it might clean up + your compiler output to call this before you exit. +*/ +void kiss_fft_cleanup(void); + + +/* + * Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5) + */ +int kiss_fft_next_fast_size(int n); + +/* for real ffts, we need an even size */ +#define kiss_fftr_next_fast_size_real(n) \ + (kiss_fft_next_fast_size( ((n)+1)>>1)<<1) + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/ckb-mviz/kiss_fftr.c b/src/ckb-mviz/kiss_fftr.c new file mode 100644 index 0000000..b8e238b --- /dev/null +++ b/src/ckb-mviz/kiss_fftr.c @@ -0,0 +1,159 @@ +/* +Copyright (c) 2003-2004, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "kiss_fftr.h" +#include "_kiss_fft_guts.h" + +struct kiss_fftr_state{ + kiss_fft_cfg substate; + kiss_fft_cpx * tmpbuf; + kiss_fft_cpx * super_twiddles; +#ifdef USE_SIMD + void * pad; +#endif +}; + +kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem) +{ + int i; + kiss_fftr_cfg st = NULL; + size_t subsize, memneeded; + + if (nfft & 1) { + fprintf(stderr,"Real FFT optimization must be even.\n"); + return NULL; + } + nfft >>= 1; + + kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize); + memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * ( nfft * 3 / 2); + + if (lenmem == NULL) { + st = (kiss_fftr_cfg) KISS_FFT_MALLOC (memneeded); + } else { + if (*lenmem >= memneeded) + st = (kiss_fftr_cfg) mem; + *lenmem = memneeded; + } + if (!st) + return NULL; + + st->substate = (kiss_fft_cfg) (st + 1); /*just beyond kiss_fftr_state struct */ + st->tmpbuf = (kiss_fft_cpx *) (((char *) st->substate) + subsize); + st->super_twiddles = st->tmpbuf + nfft; + kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize); + + for (i = 0; i < nfft/2; ++i) { + double phase = + -3.14159265358979323846264338327 * ((double) (i+1) / nfft + .5); + if (inverse_fft) + phase *= -1; + kf_cexp (st->super_twiddles+i,phase); + } + return st; +} + +void kiss_fftr(kiss_fftr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata) +{ + /* input buffer timedata is stored row-wise */ + int k,ncfft; + kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc; + + if ( st->substate->inverse) { + fprintf(stderr,"kiss fft usage error: improper alloc\n"); + exit(1); + } + + ncfft = st->substate->nfft; + + /*perform the parallel fft of two real signals packed in real,imag*/ + kiss_fft( st->substate , (const kiss_fft_cpx*)timedata, st->tmpbuf ); + /* The real part of the DC element of the frequency spectrum in st->tmpbuf + * contains the sum of the even-numbered elements of the input time sequence + * The imag part is the sum of the odd-numbered elements + * + * The sum of tdc.r and tdc.i is the sum of the input time sequence. + * yielding DC of input time sequence + * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1... + * yielding Nyquist bin of input time sequence + */ + + tdc.r = st->tmpbuf[0].r; + tdc.i = st->tmpbuf[0].i; + C_FIXDIV(tdc,2); + CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i); + CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i); + freqdata[0].r = tdc.r + tdc.i; + freqdata[ncfft].r = tdc.r - tdc.i; +#ifdef USE_SIMD + freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0); +#else + freqdata[ncfft].i = freqdata[0].i = 0; +#endif + + for ( k=1;k <= ncfft/2 ; ++k ) { + fpk = st->tmpbuf[k]; + fpnk.r = st->tmpbuf[ncfft-k].r; + fpnk.i = - st->tmpbuf[ncfft-k].i; + C_FIXDIV(fpk,2); + C_FIXDIV(fpnk,2); + + C_ADD( f1k, fpk , fpnk ); + C_SUB( f2k, fpk , fpnk ); + C_MUL( tw , f2k , st->super_twiddles[k-1]); + + freqdata[k].r = HALF_OF(f1k.r + tw.r); + freqdata[k].i = HALF_OF(f1k.i + tw.i); + freqdata[ncfft-k].r = HALF_OF(f1k.r - tw.r); + freqdata[ncfft-k].i = HALF_OF(tw.i - f1k.i); + } +} + +void kiss_fftri(kiss_fftr_cfg st,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata) +{ + /* input buffer timedata is stored row-wise */ + int k, ncfft; + + if (st->substate->inverse == 0) { + fprintf (stderr, "kiss fft usage error: improper alloc\n"); + exit (1); + } + + ncfft = st->substate->nfft; + + st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r; + st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r; + C_FIXDIV(st->tmpbuf[0],2); + + for (k = 1; k <= ncfft / 2; ++k) { + kiss_fft_cpx fk, fnkc, fek, fok, tmp; + fk = freqdata[k]; + fnkc.r = freqdata[ncfft - k].r; + fnkc.i = -freqdata[ncfft - k].i; + C_FIXDIV( fk , 2 ); + C_FIXDIV( fnkc , 2 ); + + C_ADD (fek, fk, fnkc); + C_SUB (tmp, fk, fnkc); + C_MUL (fok, tmp, st->super_twiddles[k-1]); + C_ADD (st->tmpbuf[k], fek, fok); + C_SUB (st->tmpbuf[ncfft - k], fek, fok); +#ifdef USE_SIMD + st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0); +#else + st->tmpbuf[ncfft - k].i *= -1; +#endif + } + kiss_fft (st->substate, st->tmpbuf, (kiss_fft_cpx *) timedata); +} diff --git a/src/ckb-mviz/kiss_fftr.h b/src/ckb-mviz/kiss_fftr.h new file mode 100644 index 0000000..72e5a57 --- /dev/null +++ b/src/ckb-mviz/kiss_fftr.h @@ -0,0 +1,46 @@ +#ifndef KISS_FTR_H +#define KISS_FTR_H + +#include "kiss_fft.h" +#ifdef __cplusplus +extern "C" { +#endif + + +/* + + Real optimized version can save about 45% cpu time vs. complex fft of a real seq. + + + + */ + +typedef struct kiss_fftr_state *kiss_fftr_cfg; + + +kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem, size_t * lenmem); +/* + nfft must be even + + If you don't care to allocate space, use mem = lenmem = NULL +*/ + + +void kiss_fftr(kiss_fftr_cfg cfg,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata); +/* + input timedata has nfft scalar points + output freqdata has nfft/2+1 complex points +*/ + +void kiss_fftri(kiss_fftr_cfg cfg,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata); +/* + input freqdata has nfft/2+1 complex points + output timedata has nfft scalar points +*/ + +#define kiss_fftr_free free + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/ckb-mviz/main.c b/src/ckb-mviz/main.c index b181f52..8481205 100644 --- a/src/ckb-mviz/main.c +++ b/src/ckb-mviz/main.c @@ -1,8 +1,12 @@ #include +#include +#include #include #include "../ckb/ckb-anim.h" + +//#define FIXED_POINT 16 #include "kiss_fftr.h" void ckb_info(){ @@ -16,18 +20,18 @@ void ckb_info(){ // Effect parameters CKB_PARAM_AGRADIENT("color", "Fade color:", "", "ffffffff"); - CKB_PARAM_BOOL("random", "Random Coloring", 0); + //CKB_PARAM_BOOL("random", "Random Coloring", 0); //CKB_PARAM_LONG("frequency", "Spot Radius:", "", "1", "0", "150"); // Timing/input parameters - CKB_KPMODE(CKB_KP_POSITION); - CKB_TIMEMODE(CKB_TIME_DURATION); + CKB_KPMODE(CKB_KP_NONE); + CKB_TIMEMODE(CKB_TIME_ABSOLUTE); CKB_REPEAT(FALSE); CKB_LIVEPARAMS(TRUE); // Presets - CKB_PRESET_START("Single Spot"); - CKB_PRESET_PARAM("duration", "1.0"); + CKB_PRESET_START("Default"); + //CKB_PRESET_PARAM("duration", "1.0"); //CKB_PRESET_PARAM("radius2", "0"); CKB_PRESET_PARAM("trigger", "0"); CKB_PRESET_PARAM("kptrigger", "1"); @@ -35,35 +39,47 @@ void ckb_info(){ } ckb_gradient animcolor = { 0 }; -long radius = 1; -int* keyUsages = NULL; -int* keyPressed = NULL; -double* keyTiming = NULL; -double duration = 1.f; -int randomBright = 0; +pa_simple *pas = NULL; +FILE* t; + +typedef struct{ + int freq; + unsigned int power; +} freqdec; + +freqdec buf[2049]; +kiss_fft_cpx* inbuf; +kiss_fft_cpx* outbuf; void ckb_init(ckb_runctx* context){ - keyUsages = calloc(context->keycount, sizeof(int)); - keyPressed = calloc(context->keycount, sizeof(int)); - keyTiming = calloc(context->keycount, sizeof(double)); + static const pa_sample_spec ss ={ + .format = PA_SAMPLE_S16LE, + .rate = 44100, + .channels = 1 + }; + int error; + t = fopen("/run/media/devon/Share/Projects/ckb/stderr", "w"); + pas = pa_simple_new(NULL, "CKB Music Viz", PA_STREAM_RECORD, NULL, "CKB Music Viz", &ss, NULL, NULL, &error); + fprintf(t, "Initialized Pulse\n"); + fflush(t); + if(!pas){ + fprintf(t, "Very serious problem with initialization of pulse\n"); fflush(t); + } + for(int i=0; i<2048; i++) + buf[i].freq = 22050*i/2048; + inbuf = malloc(2048*sizeof(kiss_fft_cpx)); + outbuf = malloc(2048*sizeof(kiss_fft_cpx)); } void ckb_parameter(ckb_runctx* context, const char* name, const char* value){ CKB_PARSE_AGRADIENT("color", &animcolor){} //CKB_PARSE_LONG("frequency", &radius){} - CKB_PARSE_DOUBLE("duration", &duration){} - CKB_PARSE_BOOL("random", &randomBright){} + //CKB_PARSE_DOUBLE("duration", &duration){} + //CKB_PARSE_BOOL("random", &randomBright){} } -#define ANIM_MAX (144 * 2) -struct { - int active; - float x, y; - ckb_key* press; -} anim[ANIM_MAX] = { }; - void anim_add(ckb_key* press, float x, float y){ - for(int i = 0; i < ANIM_MAX; i++){ + /*for(int i = 0; i < ANIM_MAX; i++){ if(anim[i].press == press && anim[i].active) return; } @@ -76,18 +92,20 @@ void anim_add(ckb_key* press, float x, float y){ anim[i].press = press; return; } + */ } void anim_remove(float x, float y){ - for(int i = 0; i < ANIM_MAX; i++){ + /*for(int i = 0; i < ANIM_MAX; i++){ if(anim[i].active && anim[i].x == x && anim[i].y == y) anim[i].active = 0; } + */ } void ckb_keypress(ckb_runctx* context, ckb_key* key, int x, int y, int state){ // Add or remove a spot on this key - if(state){ + /*if(state){ anim_add(key, x, y); keyUsages[key - context->keys] += 30*duration; keyPressed[key - context->keys] = 1; @@ -97,6 +115,7 @@ void ckb_keypress(ckb_runctx* context, ckb_key* key, int x, int y, int state){ //anim_remove(x, y); keyPressed[key - context->keys] = 0; } + */ } void ckb_start(ckb_runctx* context, int state){ @@ -130,12 +149,75 @@ inline int clamp(int a, int b, int t){ int gcounter = 0; +void getFreqDec(){ + int16_t data[2048]; + int error; + fprintf(t, "Trying to read from pulse\n"); fflush(t); + pa_simple_read(pas, data, sizeof(data), &error); + fprintf(t, "Made it past reading, and kiss_fftr expects %d sized scalars\n", (int)sizeof(kiss_fft_scalar)); fflush(t); + //kiss_fftr_cfg config = kiss_fftr_alloc(4096, 0, NULL, NULL); + //kiss_fft_cpx in[2048]; + for(int j=0; j<2048; j++){ + inbuf[j].r = data[j]; + inbuf[j].i = 0; + } + kiss_fft_cfg config = kiss_fft_alloc(2048, 0, NULL, NULL); + //kiss_fft_cpx out[2048]; + kiss_fft(config, inbuf, outbuf); + //kiss_fftr(config, data, out); + fprintf(t, "made it past calculating the fft\n"); fflush(t); + + for(unsigned int j=0; j < 2048; j++) + buf[j].power = sqrt(outbuf[j].r*outbuf[j].r + outbuf[j].i*outbuf[j].i); + + kiss_fft_free(config); + kiss_fft_cleanup(); + //fprintf(t, "Somehow made it through getFreqDec\n");fflush(t); + return; +} + int ckb_frame(ckb_runctx* context){ CKB_KEYCLEAR(context); - // Draw spots - unsigned count = context->keycount; ckb_key* keys = context->keys; - for(unsigned i = 0; i < ANIM_MAX; i++){ + ckb_key* maxkey = keys+context->keycount-1; + fprintf(t, "Calling getFreqDec\n"); fflush(t); + getFreqDec(); + fprintf(t, "ckb_frame resumed control\n"); fflush(t); + unsigned int frames = context->width*context->height - 1; + int height = context->height; + if(!frames) + fprintf(t, "Frames is empty\n"); fflush(t); + for(ckb_key* key = keys; key < maxkey; key++){ + int posl = height*key->x + key->y - 1; + posl = posl > 0 ? posl : 0; + int posr = height*key->x + key->y + 1; + posr = posr > 0 ? posr : 0; + int lowfreq = floorf(pow(2,posl*11.f/frames)); + int highfreq = ceilf(pow(2,posr*11.f/frames)); + highfreq = highfreq < sizeof(buf)/sizeof(freqdec) ? highfreq : sizeof(buf)/sizeof(freqdec) - 1; + lowfreq = lowfreq > 0 ? lowfreq : 0; + int count = 0; + long long total = 0; + unsigned int height = context->height; + fprintf(t, "too the for loop\n"); fflush(t); + for(unsigned int i = lowfreq; i <= highfreq; i++){ + //fprintf(t, "Entering the for loop\n"); fflush(t); + //float lfreq = log(buf[i].freq > 0 ? buf[i].freq : 1); + //if(lowfreq <= lfreq && lfreq <= highfreq){ + total += buf[i].power; + count++; + //} + } + if(count) + total /= count; + else + total = 0; + float a, r, g, b; + ckb_grad_color(&a, &r, &g, &b, &animcolor, total/4096.f); + ckb_alpha_blend(key, a, r, g, b); + } + fprintf(t, "Made it through the frame\n"); fflush(t); +/* for(unsigned i = 0; i < ANIM_MAX; i++){ //if(anim[i].active){ for(ckb_key* key = keys; key < keys + count; key++){ if(!(anim[i].press && keyUsages[anim[i].press - keys])) @@ -156,5 +238,6 @@ int ckb_frame(ckb_runctx* context){ for(;count --> 0;) if(keyUsages[count] && !keyPressed[count]) keyUsages[count]--; +*/ return 0; } From 3bcd042e019e403835375517835633a0d2601c5e Mon Sep 17 00:00:00 2001 From: Devon Richards Date: Thu, 24 Sep 2015 23:30:30 -0500 Subject: [PATCH 5/8] Extreme touchup to ckb-heat --- src/ckb-heat/main.c | 151 ++++++++++++++++++-------------------------- 1 file changed, 63 insertions(+), 88 deletions(-) diff --git a/src/ckb-heat/main.c b/src/ckb-heat/main.c index 0235c5e..953c222 100644 --- a/src/ckb-heat/main.c +++ b/src/ckb-heat/main.c @@ -3,8 +3,8 @@ void ckb_info(){ // Plugin info - CKB_NAME("HeatMap"); - CKB_VERSION("0.5"); + CKB_NAME("Heat Map"); + CKB_VERSION("0.2"); CKB_COPYRIGHT("2015", "RULER501"); CKB_LICENSE("LGPLv3"); CKB_GUID("{097D69F0-70B2-48B8-AFE2-25A1CDB0D92C}"); @@ -12,87 +12,76 @@ void ckb_info(){ // Effect parameters CKB_PARAM_AGRADIENT("color", "Fade color:", "", "ffffffff"); - CKB_PARAM_BOOL("random", "Random Coloring", 0); - //CKB_PARAM_LONG("frequency", "Spot Radius:", "", "1", "0", "150"); + CKB_PARAM_BOOL("random", "Random Brightness", 0); + CKB_PARAM_LONG("ifade", "Inverse Frame Speed", "frames", 3, 1, 100); + CKB_PARAM_LONG("duration", "Frames to fade", "frames", 30, 10, 1000); + CKB_PARAM_DOUBLE("clicktofull", "Presses to full", "keypresses", 10, 1, 100); // Timing/input parameters CKB_KPMODE(CKB_KP_POSITION); - CKB_TIMEMODE(CKB_TIME_DURATION); + CKB_TIMEMODE(CKB_TIME_ABSOLUTE); CKB_REPEAT(FALSE); CKB_LIVEPARAMS(TRUE); // Presets CKB_PRESET_START("Single Spot"); CKB_PRESET_PARAM("duration", "1.0"); - //CKB_PRESET_PARAM("radius2", "0"); - CKB_PRESET_PARAM("trigger", "0"); + CKB_PRESET_PARAM("radius", "1"); + CKB_PRESET_PARAM("ifade", "3"); + CKB_PRESET_PARAM("trigger", "0"); CKB_PRESET_PARAM("kptrigger", "1"); CKB_PRESET_END; } ckb_gradient animcolor = { 0 }; -long radius = 1; -int* keyUsages = NULL; -int* keyPressed = NULL; -double* keyTiming = NULL; double duration = 1.f; int randomBright = 0; +long ifade = 3; +int gcounter = 0; +long duration = 30; +double pressestofull = 10.f; + +typedef struct{ + int usages; + int pressed; + double timing; + int x; + int y; +} keyanim; + +keyanim* anims = NULL; void ckb_init(ckb_runctx* context){ - keyUsages = calloc(context->keycount, sizeof(int)); - keyPressed = calloc(context->keycount, sizeof(int)); - keyTiming = calloc(context->keycount, sizeof(double)); + anims = calloc(context->keycount, sizeof(keyanim)); + for(int i=0; i < context->keycount; i++){ + anims[i].x = context->keys[i].x; + anims[i].y = context->keys[i].y; + } } void ckb_parameter(ckb_runctx* context, const char* name, const char* value){ CKB_PARSE_AGRADIENT("color", &animcolor){} - //CKB_PARSE_LONG("frequency", &radius){} - CKB_PARSE_DOUBLE("duration", &duration){} + CKB_PARSE_LONG("ifade", &ifade){} + //CKB_PARSE_DOUBLE("duration", &duration){} CKB_PARSE_BOOL("random", &randomBright){} } -#define ANIM_MAX (144 * 2) -struct { - int active; - float x, y; - ckb_key* press; -} anim[ANIM_MAX] = { }; - -void anim_add(ckb_key* press, float x, float y){ - for(int i = 0; i < ANIM_MAX; i++){ - if(anim[i].press == press && anim[i].active) - return; - } - for(int i = 0; i < ANIM_MAX; i++){ - if(anim[i].active) - continue; - anim[i].active = 1; - anim[i].x = x; - anim[i].y = y; - anim[i].press = press; - return; - } +void anim_add(int index){ + anims[index].usages += 45; + anims[index].pressed = 1; + anims[index].timing = 0.f; } -void anim_remove(float x, float y){ - for(int i = 0; i < ANIM_MAX; i++){ - if(anim[i].active && anim[i].x == x && anim[i].y == y) - anim[i].active = 0; - } +void anim_remove(int index){ + anims[index].pressed = 0; } void ckb_keypress(ckb_runctx* context, ckb_key* key, int x, int y, int state){ // Add or remove a spot on this key - if(state){ - anim_add(key, x, y); - keyUsages[key - context->keys] += 30*duration; - keyPressed[key - context->keys] = 1; - keyTiming[key - context->keys] = 0; - } - else{ - //anim_remove(x, y); - keyPressed[key - context->keys] = 0; - } + if(state) + anim_add(key - context->keys); + else + anim_remove(key - context->keys); } void ckb_start(ckb_runctx* context, int state){ @@ -100,57 +89,43 @@ void ckb_start(ckb_runctx* context, int state){ } void ckb_time(ckb_runctx* context, double delta){ -/* int i = context->keycount; - for(;i --> 0;){ - if(keyUsages[i] && !keyPressed[i]){ - keyTiming[i] -= delta; - while(keyTiming[i] < 0){ - keyUsages[i]--; - keyTiming[i] += 0.03333333333; - } + unsigned int count = context->keycount; + for(unsigned int i=0; i < count; i++){ + if(anims[i].usages && !anims[i].pressed){ + anims[i].timing -= delta; + while(anims[i].timing < 0){ + anims[i].timing += 1.f/30.f; + gcounter++; + if(!(gcounter %= ifade)){ + anims[i].usages--; + } + } } - }*/ + } } inline int max(int a, int b){ - return a > b ? a : b; + return a < b ? b : a; } inline int min(int a, int b){ return a < b ? a : b; } -inline int clamp(int a, int b, int t){ - return max(min(a,b), t); -} - -int gcounter = 0; - int ckb_frame(ckb_runctx* context){ CKB_KEYCLEAR(context); // Draw spots unsigned count = context->keycount; ckb_key* keys = context->keys; - for(unsigned i = 0; i < ANIM_MAX; i++){ - //if(anim[i].active){ - for(ckb_key* key = keys; key < keys + count; key++){ - if(!(anim[i].press && keyUsages[anim[i].press - keys])) - continue; - if(max(abs(anim[i].x - key->x), abs(anim[i].y - key->y)) <= radius){ - float a, r, g, b; - if(randomBright) - ckb_grad_color(&a, &r, &g, &b, &animcolor, ((float)(rand() % 1000))/10.f); - else - ckb_grad_color(&a, &r, &g, &b, &animcolor, ((float)min(keyUsages[anim[i].press-keys],300))/3.f); - ckb_alpha_blend(key, a, r, g, b); - } - } - //} + for(int i =0; i < count; i++){ + if(!anims[i].usages) + continue; + float a, r, g, b; + if(randomBright) + ckb_grad_color(&a, &r, &g, &b, &animcolor, ((float)(rand() % 1000))/10.f); + else + ckb_grad_color(&a, &r, &g, &b, &animcolor, ((float)min(anims[i].usages,300))/3.f); + ckb_alpha_blend(keys+i, a, r, g, b); } - gcounter++; - if(!(gcounter %= 3)) - for(;count --> 0;) - if(keyUsages[count] && !keyPressed[count]) - keyUsages[count]--; return 0; } From 26b58588101d71ce0e15120c464409d578b1c145 Mon Sep 17 00:00:00 2001 From: Devon Richards Date: Fri, 25 Sep 2015 17:39:16 -0500 Subject: [PATCH 6/8] Lots of cleanup and small improvements --- src/ckb-heat/main.c | 26 ++++---- src/ckb-mviz/main.c | 148 +++++++------------------------------------- 2 files changed, 35 insertions(+), 139 deletions(-) diff --git a/src/ckb-heat/main.c b/src/ckb-heat/main.c index 953c222..8f2aede 100644 --- a/src/ckb-heat/main.c +++ b/src/ckb-heat/main.c @@ -14,8 +14,8 @@ void ckb_info(){ CKB_PARAM_AGRADIENT("color", "Fade color:", "", "ffffffff"); CKB_PARAM_BOOL("random", "Random Brightness", 0); CKB_PARAM_LONG("ifade", "Inverse Frame Speed", "frames", 3, 1, 100); - CKB_PARAM_LONG("duration", "Frames to fade", "frames", 30, 10, 1000); - CKB_PARAM_DOUBLE("clicktofull", "Presses to full", "keypresses", 10, 1, 100); + CKB_PARAM_LONG("ffade", "Frames to fade", "frames", 30, 10, 1000); + CKB_PARAM_DOUBLE("pressestofull", "Presses to full", "keypresses", 10.f, 1.f, 100.f); // Timing/input parameters CKB_KPMODE(CKB_KP_POSITION); @@ -25,20 +25,20 @@ void ckb_info(){ // Presets CKB_PRESET_START("Single Spot"); - CKB_PRESET_PARAM("duration", "1.0"); - CKB_PRESET_PARAM("radius", "1"); + CKB_PRESET_PARAM("random", "0"); CKB_PRESET_PARAM("ifade", "3"); + CKB_PRESET_PARAM("ffade", "30"); + CKB_PRESET_PARAM("pressestofull", "10"); CKB_PRESET_PARAM("trigger", "0"); CKB_PRESET_PARAM("kptrigger", "1"); CKB_PRESET_END; } ckb_gradient animcolor = { 0 }; -double duration = 1.f; int randomBright = 0; long ifade = 3; int gcounter = 0; -long duration = 30; +long ffade = 30; double pressestofull = 10.f; typedef struct{ @@ -61,13 +61,14 @@ void ckb_init(ckb_runctx* context){ void ckb_parameter(ckb_runctx* context, const char* name, const char* value){ CKB_PARSE_AGRADIENT("color", &animcolor){} + CKB_PARSE_BOOL("random", &randomBright){} CKB_PARSE_LONG("ifade", &ifade){} - //CKB_PARSE_DOUBLE("duration", &duration){} - CKB_PARSE_BOOL("random", &randomBright){} + CKB_PARSE_LONG("ffade", &ffade){} + CKB_PARSE_DOUBLE("pressestofull", &pressestofull){} } void anim_add(int index){ - anims[index].usages += 45; + anims[index].usages += ffade; anims[index].pressed = 1; anims[index].timing = 0.f; } @@ -117,14 +118,15 @@ int ckb_frame(ckb_runctx* context){ // Draw spots unsigned count = context->keycount; ckb_key* keys = context->keys; - for(int i =0; i < count; i++){ + unsigned int full = pressestofull*ffade; + for(int i =0; i < count; i++){ if(!anims[i].usages) continue; float a, r, g, b; if(randomBright) - ckb_grad_color(&a, &r, &g, &b, &animcolor, ((float)(rand() % 1000))/10.f); + ckb_grad_color(&a, &r, &g, &b, &animcolor, ((float)(rand() % 1024))/10.f); else - ckb_grad_color(&a, &r, &g, &b, &animcolor, ((float)min(anims[i].usages,300))/3.f); + ckb_grad_color(&a, &r, &g, &b, &animcolor, (float)min(anims[i].usages,full)*100.f/full); ckb_alpha_blend(keys+i, a, r, g, b); } return 0; diff --git a/src/ckb-mviz/main.c b/src/ckb-mviz/main.c index 8481205..d798e28 100644 --- a/src/ckb-mviz/main.c +++ b/src/ckb-mviz/main.c @@ -6,13 +6,12 @@ #include "../ckb/ckb-anim.h" -//#define FIXED_POINT 16 #include "kiss_fftr.h" void ckb_info(){ // Plugin info CKB_NAME("Music Visualization"); - CKB_VERSION("0.5"); + CKB_VERSION("0.2"); CKB_COPYRIGHT("2015", "RULER501"); CKB_LICENSE("LGPLv3"); CKB_GUID("{097D69F0-70B2-48B8-AFE2-25CA1DB0D92C}"); @@ -20,8 +19,6 @@ void ckb_info(){ // Effect parameters CKB_PARAM_AGRADIENT("color", "Fade color:", "", "ffffffff"); - //CKB_PARAM_BOOL("random", "Random Coloring", 0); - //CKB_PARAM_LONG("frequency", "Spot Radius:", "", "1", "0", "150"); // Timing/input parameters CKB_KPMODE(CKB_KP_NONE); @@ -31,25 +28,21 @@ void ckb_info(){ // Presets CKB_PRESET_START("Default"); - //CKB_PRESET_PARAM("duration", "1.0"); - //CKB_PRESET_PARAM("radius2", "0"); CKB_PRESET_PARAM("trigger", "0"); CKB_PRESET_PARAM("kptrigger", "1"); CKB_PRESET_END; } -ckb_gradient animcolor = { 0 }; -pa_simple *pas = NULL; -FILE* t; - typedef struct{ int freq; unsigned int power; } freqdec; -freqdec buf[2049]; +freqdec buf[2048]; kiss_fft_cpx* inbuf; kiss_fft_cpx* outbuf; +ckb_gradient animcolor = { 0 }; +pa_simple *pas = NULL; void ckb_init(ckb_runctx* context){ static const pa_sample_spec ss ={ @@ -57,14 +50,7 @@ void ckb_init(ckb_runctx* context){ .rate = 44100, .channels = 1 }; - int error; - t = fopen("/run/media/devon/Share/Projects/ckb/stderr", "w"); - pas = pa_simple_new(NULL, "CKB Music Viz", PA_STREAM_RECORD, NULL, "CKB Music Viz", &ss, NULL, NULL, &error); - fprintf(t, "Initialized Pulse\n"); - fflush(t); - if(!pas){ - fprintf(t, "Very serious problem with initialization of pulse\n"); fflush(t); - } + pas = pa_simple_new(NULL, "CKB Music Viz", PA_STREAM_RECORD, NULL, "CKB Music Viz", &ss, NULL, NULL, NULL); for(int i=0; i<2048; i++) buf[i].freq = 22050*i/2048; inbuf = malloc(2048*sizeof(kiss_fft_cpx)); @@ -73,49 +59,18 @@ void ckb_init(ckb_runctx* context){ void ckb_parameter(ckb_runctx* context, const char* name, const char* value){ CKB_PARSE_AGRADIENT("color", &animcolor){} - //CKB_PARSE_LONG("frequency", &radius){} - //CKB_PARSE_DOUBLE("duration", &duration){} - //CKB_PARSE_BOOL("random", &randomBright){} } void anim_add(ckb_key* press, float x, float y){ - /*for(int i = 0; i < ANIM_MAX; i++){ - if(anim[i].press == press && anim[i].active) - return; - } - for(int i = 0; i < ANIM_MAX; i++){ - if(anim[i].active) - continue; - anim[i].active = 1; - anim[i].x = x; - anim[i].y = y; - anim[i].press = press; - return; - } - */ + return; } void anim_remove(float x, float y){ - /*for(int i = 0; i < ANIM_MAX; i++){ - if(anim[i].active && anim[i].x == x && anim[i].y == y) - anim[i].active = 0; - } - */ + return; } void ckb_keypress(ckb_runctx* context, ckb_key* key, int x, int y, int state){ - // Add or remove a spot on this key - /*if(state){ - anim_add(key, x, y); - keyUsages[key - context->keys] += 30*duration; - keyPressed[key - context->keys] = 1; - keyTiming[key - context->keys] = 0; - } - else{ - //anim_remove(x, y); - keyPressed[key - context->keys] = 0; - } - */ + return; } void ckb_start(ckb_runctx* context, int state){ @@ -123,121 +78,60 @@ void ckb_start(ckb_runctx* context, int state){ } void ckb_time(ckb_runctx* context, double delta){ -/* int i = context->keycount; - for(;i --> 0;){ - if(keyUsages[i] && !keyPressed[i]){ - keyTiming[i] -= delta; - while(keyTiming[i] < 0){ - keyUsages[i]--; - keyTiming[i] += 0.03333333333; - } - } - }*/ + return; } -inline int max(int a, int b){ +int max(int a, int b){ return a > b ? a : b; } -inline int min(int a, int b){ +int min(int a, int b){ return a < b ? a : b; } -inline int clamp(int a, int b, int t){ - return max(min(a,b), t); -} - int gcounter = 0; void getFreqDec(){ int16_t data[2048]; - int error; - fprintf(t, "Trying to read from pulse\n"); fflush(t); - pa_simple_read(pas, data, sizeof(data), &error); - fprintf(t, "Made it past reading, and kiss_fftr expects %d sized scalars\n", (int)sizeof(kiss_fft_scalar)); fflush(t); - //kiss_fftr_cfg config = kiss_fftr_alloc(4096, 0, NULL, NULL); - //kiss_fft_cpx in[2048]; + pa_simple_read(pas, data, sizeof(data), NULL); for(int j=0; j<2048; j++){ inbuf[j].r = data[j]; inbuf[j].i = 0; } kiss_fft_cfg config = kiss_fft_alloc(2048, 0, NULL, NULL); - //kiss_fft_cpx out[2048]; kiss_fft(config, inbuf, outbuf); - //kiss_fftr(config, data, out); - fprintf(t, "made it past calculating the fft\n"); fflush(t); for(unsigned int j=0; j < 2048; j++) buf[j].power = sqrt(outbuf[j].r*outbuf[j].r + outbuf[j].i*outbuf[j].i); kiss_fft_free(config); kiss_fft_cleanup(); - //fprintf(t, "Somehow made it through getFreqDec\n");fflush(t); - return; } int ckb_frame(ckb_runctx* context){ CKB_KEYCLEAR(context); ckb_key* keys = context->keys; ckb_key* maxkey = keys+context->keycount-1; - fprintf(t, "Calling getFreqDec\n"); fflush(t); getFreqDec(); - fprintf(t, "ckb_frame resumed control\n"); fflush(t); unsigned int frames = context->width*context->height - 1; int height = context->height; - if(!frames) - fprintf(t, "Frames is empty\n"); fflush(t); for(ckb_key* key = keys; key < maxkey; key++){ int posl = height*key->x + key->y - 1; - posl = posl > 0 ? posl : 0; + posl = max(posl, 0); int posr = height*key->x + key->y + 1; - posr = posr > 0 ? posr : 0; - int lowfreq = floorf(pow(2,posl*11.f/frames)); - int highfreq = ceilf(pow(2,posr*11.f/frames)); - highfreq = highfreq < sizeof(buf)/sizeof(freqdec) ? highfreq : sizeof(buf)/sizeof(freqdec) - 1; - lowfreq = lowfreq > 0 ? lowfreq : 0; - int count = 0; + posr = max(posr, 0); + int lowi = floorf(pow(2,posl*11.f/frames)); + int highi = ceilf(pow(2,posr*11.f/frames)); + highi= min(highi, (int)sizeof(buf)/sizeof(freqdec)-1); + lowi = max(lowi, 0); long long total = 0; unsigned int height = context->height; - fprintf(t, "too the for loop\n"); fflush(t); - for(unsigned int i = lowfreq; i <= highfreq; i++){ - //fprintf(t, "Entering the for loop\n"); fflush(t); - //float lfreq = log(buf[i].freq > 0 ? buf[i].freq : 1); - //if(lowfreq <= lfreq && lfreq <= highfreq){ + for(unsigned int i = lowi; i <= highi; i++) total += buf[i].power; - count++; - //} - } - if(count) - total /= count; - else - total = 0; + total /= highi - lowi + 1; float a, r, g, b; - ckb_grad_color(&a, &r, &g, &b, &animcolor, total/4096.f); + ckb_grad_color(&a, &r, &g, &b, &animcolor, total/12288.f); ckb_alpha_blend(key, a, r, g, b); } - fprintf(t, "Made it through the frame\n"); fflush(t); -/* for(unsigned i = 0; i < ANIM_MAX; i++){ - //if(anim[i].active){ - for(ckb_key* key = keys; key < keys + count; key++){ - if(!(anim[i].press && keyUsages[anim[i].press - keys])) - continue; - if(max(abs(anim[i].x - key->x), abs(anim[i].y - key->y)) <= radius){ - float a, r, g, b; - if(random) - ckb_grad_color(&a, &r, &g, &b, &animcolor, ((float)(rand() % 1000))/10.f); - else - ckb_grad_color(&a, &r, &g, &b, &animcolor, ((float)min(keyUsages[anim[i].press-keys],300))/3.f); - ckb_alpha_blend(key, a, r, g, b); - } - } - //} - } - gcounter++; - if(!(gcounter %= 3)) - for(;count --> 0;) - if(keyUsages[count] && !keyPressed[count]) - keyUsages[count]--; -*/ return 0; } From af36cca86fdb12885c191e989868e58582b3fd00 Mon Sep 17 00:00:00 2001 From: Devon Richards Date: Fri, 25 Sep 2015 17:43:18 -0500 Subject: [PATCH 7/8] Moved back to be more compliant with ccMSC --- ckb.pro | 3 +-- quickinstall | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/ckb.pro b/ckb.pro index 0323d3d..224288c 100644 --- a/ckb.pro +++ b/ckb.pro @@ -1,6 +1,5 @@ TEMPLATE = subdirs -#CONFIG += debug_and_release -CONFIG = debug +CONFIG += debug_and_release SUBDIRS = \ src/ckb-daemon \ src/ckb \ diff --git a/quickinstall b/quickinstall index 12e4044..ac3e57b 100755 --- a/quickinstall +++ b/quickinstall @@ -53,7 +53,7 @@ if [[ -d $SRCDIR ]]; then echo "Compiling binaries..." echo "(This can take a while, please be patient)" newtmp - make -j 8 "$@" 2>$TMPFILE + make -j 24 "$@" 2>$TMPFILE checkfail $? echo "" echo "Finished!" From e400e141b346ddd1c4d28cabffe7fd14a9240307 Mon Sep 17 00:00:00 2001 From: Devon Richards Date: Fri, 25 Sep 2015 18:14:35 -0500 Subject: [PATCH 8/8] Add more customization --- src/ckb-heat/main.c | 10 +--------- src/ckb-mviz/main.c | 29 ++++++++++++++--------------- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/ckb-heat/main.c b/src/ckb-heat/main.c index 8f2aede..408d828 100644 --- a/src/ckb-heat/main.c +++ b/src/ckb-heat/main.c @@ -13,7 +13,6 @@ void ckb_info(){ // Effect parameters CKB_PARAM_AGRADIENT("color", "Fade color:", "", "ffffffff"); CKB_PARAM_BOOL("random", "Random Brightness", 0); - CKB_PARAM_LONG("ifade", "Inverse Frame Speed", "frames", 3, 1, 100); CKB_PARAM_LONG("ffade", "Frames to fade", "frames", 30, 10, 1000); CKB_PARAM_DOUBLE("pressestofull", "Presses to full", "keypresses", 10.f, 1.f, 100.f); @@ -26,7 +25,6 @@ void ckb_info(){ // Presets CKB_PRESET_START("Single Spot"); CKB_PRESET_PARAM("random", "0"); - CKB_PRESET_PARAM("ifade", "3"); CKB_PRESET_PARAM("ffade", "30"); CKB_PRESET_PARAM("pressestofull", "10"); CKB_PRESET_PARAM("trigger", "0"); @@ -36,8 +34,6 @@ void ckb_info(){ ckb_gradient animcolor = { 0 }; int randomBright = 0; -long ifade = 3; -int gcounter = 0; long ffade = 30; double pressestofull = 10.f; @@ -62,7 +58,6 @@ void ckb_init(ckb_runctx* context){ void ckb_parameter(ckb_runctx* context, const char* name, const char* value){ CKB_PARSE_AGRADIENT("color", &animcolor){} CKB_PARSE_BOOL("random", &randomBright){} - CKB_PARSE_LONG("ifade", &ifade){} CKB_PARSE_LONG("ffade", &ffade){} CKB_PARSE_DOUBLE("pressestofull", &pressestofull){} } @@ -96,10 +91,7 @@ void ckb_time(ckb_runctx* context, double delta){ anims[i].timing -= delta; while(anims[i].timing < 0){ anims[i].timing += 1.f/30.f; - gcounter++; - if(!(gcounter %= ifade)){ - anims[i].usages--; - } + anims[i].usages--; } } } diff --git a/src/ckb-mviz/main.c b/src/ckb-mviz/main.c index d798e28..0bd4d6c 100644 --- a/src/ckb-mviz/main.c +++ b/src/ckb-mviz/main.c @@ -19,6 +19,7 @@ void ckb_info(){ // Effect parameters CKB_PARAM_AGRADIENT("color", "Fade color:", "", "ffffffff"); + CKB_PARAM_BOOL("power", "Use Power instead of Magnitude?", 0); // Timing/input parameters CKB_KPMODE(CKB_KP_NONE); @@ -28,21 +29,18 @@ void ckb_info(){ // Presets CKB_PRESET_START("Default"); - CKB_PRESET_PARAM("trigger", "0"); + CKB_PRESET_PARAM("power", "0"); + CKB_PRESET_PARAM("trigger", "0"); CKB_PRESET_PARAM("kptrigger", "1"); CKB_PRESET_END; } -typedef struct{ - int freq; - unsigned int power; -} freqdec; - -freqdec buf[2048]; +double powers[2048] = { 0.f }; kiss_fft_cpx* inbuf; kiss_fft_cpx* outbuf; ckb_gradient animcolor = { 0 }; pa_simple *pas = NULL; +int power = 0; void ckb_init(ckb_runctx* context){ static const pa_sample_spec ss ={ @@ -51,14 +49,13 @@ void ckb_init(ckb_runctx* context){ .channels = 1 }; pas = pa_simple_new(NULL, "CKB Music Viz", PA_STREAM_RECORD, NULL, "CKB Music Viz", &ss, NULL, NULL, NULL); - for(int i=0; i<2048; i++) - buf[i].freq = 22050*i/2048; inbuf = malloc(2048*sizeof(kiss_fft_cpx)); outbuf = malloc(2048*sizeof(kiss_fft_cpx)); } void ckb_parameter(ckb_runctx* context, const char* name, const char* value){ CKB_PARSE_AGRADIENT("color", &animcolor){} + CKB_PARSE_BOOL("power", &power); } void anim_add(ckb_key* press, float x, float y){ @@ -102,8 +99,10 @@ void getFreqDec(){ kiss_fft(config, inbuf, outbuf); for(unsigned int j=0; j < 2048; j++) - buf[j].power = sqrt(outbuf[j].r*outbuf[j].r + outbuf[j].i*outbuf[j].i); - + if(power) + powers[j] = outbuf[j].r*outbuf[j].r + outbuf[j].i*outbuf[j].i; + else + powers[j] = sqrt(outbuf[j].r*outbuf[j].r + outbuf[j].i*outbuf[j].i); kiss_fft_free(config); kiss_fft_cleanup(); } @@ -122,15 +121,15 @@ int ckb_frame(ckb_runctx* context){ posr = max(posr, 0); int lowi = floorf(pow(2,posl*11.f/frames)); int highi = ceilf(pow(2,posr*11.f/frames)); - highi= min(highi, (int)sizeof(buf)/sizeof(freqdec)-1); + highi= min(highi, (int)sizeof(powers)/sizeof(double)-1); lowi = max(lowi, 0); - long long total = 0; + double total = 0; unsigned int height = context->height; for(unsigned int i = lowi; i <= highi; i++) - total += buf[i].power; + total += powers[i]; total /= highi - lowi + 1; float a, r, g, b; - ckb_grad_color(&a, &r, &g, &b, &animcolor, total/12288.f); + ckb_grad_color(&a, &r, &g, &b, &animcolor, total/(power ? 150994944.f : 12288.f)); ckb_alpha_blend(key, a, r, g, b); } return 0;