ckb-next  v0.2.8 at branch master
ckb-next driver for corsair devices
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
main.c File Reference
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <pulse/simple.h>
#include "../ckb/ckb-anim.h"
#include "kiss_fftr.h"
+ Include dependency graph for main.c:

Go to the source code of this file.

Functions

void ckb_info ()
 
void ckb_init (ckb_runctx *context)
 
void ckb_parameter (ckb_runctx *context, const char *name, const char *value)
 
void anim_add (ckb_key *press, float x, float y)
 
void anim_remove (float x, float y)
 
void ckb_keypress (ckb_runctx *context, ckb_key *key, int x, int y, int state)
 
void ckb_start (ckb_runctx *context, int state)
 
void ckb_time (ckb_runctx *context, double delta)
 
int max (int a, int b)
 
int min (int a, int b)
 
void getFreqDec ()
 
int ckb_frame (ckb_runctx *context)
 

Variables

double powers [2048] = { 0.f }
 
kiss_fft_cpxinbuf
 
kiss_fft_cpxoutbuf
 
ckb_gradient animcolor = { 0 }
 
pa_simple * pas = NULL
 
int power = 0
 
int gcounter = 0
 

Function Documentation

void anim_add ( ckb_key press,
float  x,
float  y 
)

Definition at line 77 of file main.c.

77  {
78  return;
79 }
void anim_remove ( float  x,
float  y 
)

Definition at line 81 of file main.c.

81  {
82  return;
83 }
int ckb_frame ( ckb_runctx context)

Definition at line 126 of file main.c.

References ckb_alpha_blend(), ckb_grad_color(), CKB_KEYCLEAR, getFreqDec(), ckb_runctx::height, ckb_runctx::keycount, ckb_runctx::keys, max(), min(), power, powers, and ckb_runctx::width.

126  {
127  CKB_KEYCLEAR(context);
128  ckb_key* keys = context->keys;
129  ckb_key* maxkey = keys+context->keycount-1;
130  getFreqDec();
131  unsigned int frames = context->width*context->height - 1;
132  int height = context->height;
133  for(ckb_key* key = keys; key < maxkey; key++){
134  int posl = height*key->x + key->y - 1;
135  posl = max(posl, 0);
136  int posr = height*key->x + key->y + 1;
137  posr = max(posr, 0);
138  int lowi = floorf(pow(2,posl*11.f/frames));
139  int highi = ceilf(pow(2,posr*11.f/frames));
140  highi= min(highi, (int)sizeof(powers)/sizeof(double)-1);
141  lowi = max(lowi, 0);
142  double total = 0;
143  unsigned int height = context->height;
144  for(unsigned int i = lowi; i <= highi; i++)
145  total += powers[i];
146  total /= highi - lowi + 1;
147  float a, r, g, b;
148  ckb_grad_color(&a, &r, &g, &b, &animcolor, total/(power ? 150994944.f : 12288.f));
149  ckb_alpha_blend(key, a, r, g, b);
150  }
151  return 0;
152 }
int power
Definition: main.c:59
ckb_gradient animcolor
Definition: main.c:52
Definition: keymap.h:49
unsigned height
Definition: ckb-anim.h:143
unsigned keycount
Definition: ckb-anim.h:141
void getFreqDec()
Definition: main.c:107
int max(int a, int b)
Definition: main.c:97
double powers[2048]
Definition: main.c:54
int min(int a, int b)
Definition: main.c:101
#define CKB_KEYCLEAR(context)
Definition: ckb-anim.h:148
ckb_key * keys
Definition: ckb-anim.h:140
void ckb_alpha_blend(ckb_key *key, float a, float r, float g, float b)
Definition: ckb-anim.h:283
unsigned width
Definition: ckb-anim.h:143
void ckb_grad_color(float *a, float *r, float *g, float *b, const ckb_gradient *grad, float pos)
Definition: ckb-anim.h:254

+ Here is the call graph for this function:

void ckb_info ( )

Definition at line 27 of file main.c.

References CKB_COPYRIGHT, CKB_DESCRIPTION, CKB_GUID, CKB_KP_NONE, CKB_KPMODE, CKB_LICENSE, CKB_LIVEPARAMS, CKB_NAME, CKB_PARAM_AGRADIENT, CKB_PARAM_BOOL, CKB_PRESET_END, CKB_PRESET_PARAM, CKB_PRESET_START, CKB_REPEAT, CKB_TIME_ABSOLUTE, CKB_TIMEMODE, CKB_VERSION, FALSE, and TRUE.

27  {
28  // Plugin info
29  CKB_NAME("Music Visualization");
30  CKB_VERSION("0.2");
31  CKB_COPYRIGHT("2017", "RULER501");
32  CKB_LICENSE("GPLv2");
33  CKB_GUID("{097D69F0-70B2-48B8-AFE2-25CA1DB0D92C}");
34  CKB_DESCRIPTION("A collection of music visualization effects");
35 
36  // Effect parameters
37  CKB_PARAM_AGRADIENT("color", "Fade color:", "", "ffffffff");
38  CKB_PARAM_BOOL("power", "Use Power instead of Magnitude?", 0);
39 
40  // Timing/input parameters
45 
46  // Presets
47  CKB_PRESET_START("Default");
48  CKB_PRESET_PARAM("power", "0");
49  CKB_PRESET_PARAM("trigger", "0");
50  CKB_PRESET_PARAM("kptrigger", "1");
52 }
#define CKB_PRESET_END
Definition: ckb-anim.h:87
#define FALSE
Definition: ckb-anim.h:49
#define CKB_VERSION(version)
Definition: ckb-anim.h:64
#define CKB_TIMEMODE(mode)
Definition: ckb-anim.h:97
#define TRUE
Definition: ckb-anim.h:46
#define CKB_GUID(guid)
Definition: ckb-anim.h:60
#define CKB_NAME(name)
Definition: ckb-anim.h:62
#define CKB_KP_NONE
Definition: ckb-anim.h:90
#define CKB_TIME_ABSOLUTE
Definition: ckb-anim.h:96
#define CKB_LICENSE(license)
Definition: ckb-anim.h:68
#define CKB_COPYRIGHT(year, author)
Definition: ckb-anim.h:66
#define CKB_DESCRIPTION(description)
Definition: ckb-anim.h:70
#define CKB_PRESET_START(name)
Definition: ckb-anim.h:85
#define CKB_KPMODE(mode)
Definition: ckb-anim.h:93
#define CKB_PARAM_AGRADIENT(name, prefix, postfix, default)
Definition: ckb-anim.h:80
#define CKB_LIVEPARAMS(enable)
Definition: ckb-anim.h:104
#define CKB_REPEAT(enable)
Definition: ckb-anim.h:99
#define CKB_PRESET_PARAM(name, value)
Definition: ckb-anim.h:86
#define CKB_PARAM_BOOL(name, text, default)
Definition: ckb-anim.h:76
void ckb_init ( ckb_runctx context)

Definition at line 61 of file main.c.

References pas.

61  {
62  static const pa_sample_spec ss ={
63  .format = PA_SAMPLE_S16LE,
64  .rate = 44100,
65  .channels = 1
66  };
67  pas = pa_simple_new(NULL, "CKB Music Viz", PA_STREAM_RECORD, NULL, "CKB Music Viz", &ss, NULL, NULL, NULL);
68  inbuf = malloc(2048*sizeof(kiss_fft_cpx));
69  outbuf = malloc(2048*sizeof(kiss_fft_cpx));
70 }
pa_simple * pas
Definition: main.c:58
kiss_fft_cpx * outbuf
Definition: main.c:56
kiss_fft_cpx * inbuf
Definition: main.c:55
void ckb_keypress ( ckb_runctx context,
ckb_key key,
int  x,
int  y,
int  state 
)

Definition at line 85 of file main.c.

85  {
86  return;
87 }
void ckb_parameter ( ckb_runctx context,
const char *  name,
const char *  value 
)

Definition at line 72 of file main.c.

References CKB_PARSE_AGRADIENT, CKB_PARSE_BOOL, and power.

72  {
73  CKB_PARSE_AGRADIENT("color", &animcolor){}
74  CKB_PARSE_BOOL("power", &power);
75 }
int power
Definition: main.c:59
ckb_gradient animcolor
Definition: main.c:52
#define CKB_PARSE_AGRADIENT(param_name, gradient_ptr)
Definition: ckb-anim.h:121
#define CKB_PARSE_BOOL(param_name, value_ptr)
Definition: ckb-anim.h:117
void ckb_start ( ckb_runctx context,
int  state 
)

Definition at line 89 of file main.c.

89  {
90  return;
91 }
void ckb_time ( ckb_runctx context,
double  delta 
)

Definition at line 93 of file main.c.

93  {
94  return;
95 }
void getFreqDec ( )

Definition at line 107 of file main.c.

References kiss_fft_cpx::i, kiss_fft(), kiss_fft_alloc(), kiss_fft_cleanup(), kiss_fft_free, pas, power, powers, and kiss_fft_cpx::r.

Referenced by ckb_frame().

107  {
108  int16_t data[2048];
109  pa_simple_read(pas, data, sizeof(data), NULL);
110  for(int j=0; j<2048; j++){
111  inbuf[j].r = data[j];
112  inbuf[j].i = 0;
113  }
114  kiss_fft_cfg config = kiss_fft_alloc(2048, 0, NULL, NULL);
115  kiss_fft(config, inbuf, outbuf);
116 
117  for(unsigned int j=0; j < 2048; j++)
118  if(power)
119  powers[j] = outbuf[j].r*outbuf[j].r + outbuf[j].i*outbuf[j].i;
120  else
121  powers[j] = sqrt(outbuf[j].r*outbuf[j].r + outbuf[j].i*outbuf[j].i);
122  kiss_fft_free(config);
124 }
int power
Definition: main.c:59
pa_simple * pas
Definition: main.c:58
float i
Definition: kiss_fft.h:53
kiss_fft_cpx * outbuf
Definition: main.c:56
void kiss_fft_cleanup(void)
Definition: kiss_fft.c:391
kiss_fft_cfg kiss_fft_alloc(int nfft, int inverse_fft, void *mem, size_t *lenmem)
Definition: kiss_fft.c:339
float r
Definition: kiss_fft.h:52
void kiss_fft(kiss_fft_cfg cfg, const kiss_fft_cpx *fin, kiss_fft_cpx *fout)
Definition: kiss_fft.c:385
double powers[2048]
Definition: main.c:54
#define kiss_fft_free
Definition: kiss_fft.h:102
kiss_fft_cpx * inbuf
Definition: main.c:55

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int max ( int  a,
int  b 
)

Definition at line 97 of file main.c.

Referenced by ckb_frame().

97  {
98  return a > b ? a : b;
99 }

+ Here is the caller graph for this function:

int min ( int  a,
int  b 
)

Definition at line 101 of file main.c.

Referenced by ckb_frame(), and dpiExp().

101  {
102  return a < b ? a : b;
103 }

+ Here is the caller graph for this function:

Variable Documentation

ckb_gradient animcolor = { 0 }

Definition at line 57 of file main.c.

int gcounter = 0

Definition at line 105 of file main.c.

kiss_fft_cpx* inbuf

Definition at line 55 of file main.c.

kiss_fft_cpx* outbuf

Definition at line 56 of file main.c.

pa_simple* pas = NULL

Definition at line 58 of file main.c.

Referenced by ckb_init(), and getFreqDec().

int power = 0

Definition at line 59 of file main.c.

Referenced by ckb_frame(), ckb_parameter(), and getFreqDec().

double powers[2048] = { 0.f }

Definition at line 54 of file main.c.

Referenced by ckb_frame(), and getFreqDec().