Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Visual Effects #127

Open
wants to merge 8 commits into
base: testing
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ckb.pro
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ SUBDIRS = \
src/ckb-gradient \
src/ckb-pinwheel \
src/ckb-random \
src/ckb-rain
src/ckb-rain \
src/ckb-heat \
src/ckb-mviz

QMAKE_MAC_SDK = macosx10.10
18 changes: 18 additions & 0 deletions src/ckb-heat/ckb-heat.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
TEMPLATE = app
TARGET = ckb-heat

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
125 changes: 125 additions & 0 deletions src/ckb-heat/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include "../ckb/ckb-anim.h"
#include <math.h>

void ckb_info(){
// Plugin info
CKB_NAME("Heat Map");
CKB_VERSION("0.2");
CKB_COPYRIGHT("2015", "RULER501");
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 Brightness", 0);
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);
CKB_TIMEMODE(CKB_TIME_ABSOLUTE);
CKB_REPEAT(FALSE);
CKB_LIVEPARAMS(TRUE);

// Presets
CKB_PRESET_START("Single Spot");
CKB_PRESET_PARAM("random", "0");
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 };
int randomBright = 0;
long ffade = 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){
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_BOOL("random", &randomBright){}
CKB_PARSE_LONG("ffade", &ffade){}
CKB_PARSE_DOUBLE("pressestofull", &pressestofull){}
}

void anim_add(int index){
anims[index].usages += ffade;
anims[index].pressed = 1;
anims[index].timing = 0.f;
}

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 - context->keys);
else
anim_remove(key - context->keys);
}

void ckb_start(ckb_runctx* context, int state){
return;
}

void ckb_time(ckb_runctx* context, double delta){
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;
anims[i].usages--;
}
}
}
}

inline int max(int a, int b){
return a < b ? b : a;
}

inline int min(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;
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() % 1024))/10.f);
else
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;
}
11 changes: 11 additions & 0 deletions src/ckb-mviz/COPYING
Original file line number Diff line number Diff line change
@@ -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.
164 changes: 164 additions & 0 deletions src/ckb-mviz/_kiss_fft_guts.h
Original file line number Diff line number Diff line change
@@ -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 <limits.h>

#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 <alloca.h>
#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
21 changes: 21 additions & 0 deletions src/ckb-mviz/ckb-mviz.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
TEMPLATE = app
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
} else {
DESTDIR = $$PWD/../../bin/ckb-animations
}

CONFIG = debug
QT =
LIBS =

SOURCES += \
main.c \
kiss_fft.c \
kiss_fftr.c
Loading