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
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (C) 2017 Devon Richards
4  * ckb-mviz is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * ckb-mviz is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with ckb-mviz. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include <math.h>
18 #include <stdint.h>
19 #include <stdlib.h>
20 
21 #include <pulse/simple.h>
22 
23 #include "../ckb/ckb-anim.h"
24 
25 #include "kiss_fftr.h"
26 
27 void ckb_info(){
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 }
53 
54 double powers[2048] = { 0.f };
58 pa_simple *pas = NULL;
59 int power = 0;
60 
61 void ckb_init(ckb_runctx* context){
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 }
71 
72 void ckb_parameter(ckb_runctx* context, const char* name, const char* value){
73  CKB_PARSE_AGRADIENT("color", &animcolor){}
74  CKB_PARSE_BOOL("power", &power);
75 }
76 
77 void anim_add(ckb_key* press, float x, float y){
78  return;
79 }
80 
81 void anim_remove(float x, float y){
82  return;
83 }
84 
85 void ckb_keypress(ckb_runctx* context, ckb_key* key, int x, int y, int state){
86  return;
87 }
88 
89 void ckb_start(ckb_runctx* context, int state){
90  return;
91 }
92 
93 void ckb_time(ckb_runctx* context, double delta){
94  return;
95 }
96 
97 int max(int a, int b){
98  return a > b ? a : b;
99 }
100 
101 int min(int a, int b){
102  return a < b ? a : b;
103 }
104 
105 int gcounter = 0;
106 
107 void getFreqDec(){
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 }
125 
126 int ckb_frame(ckb_runctx* context){
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
#define CKB_PRESET_END
Definition: ckb-anim.h:87
#define FALSE
Definition: ckb-anim.h:49
void ckb_keypress(ckb_runctx *context, ckb_key *key, int x, int y, int state)
Definition: main.c:75
void ckb_parameter(ckb_runctx *context, const char *name, const char *value)
Definition: main.c:68
#define CKB_VERSION(version)
Definition: ckb-anim.h:64
#define CKB_TIMEMODE(mode)
Definition: ckb-anim.h:97
float y
Definition: main.c:66
#define TRUE
Definition: ckb-anim.h:46
#define CKB_GUID(guid)
Definition: ckb-anim.h:60
pa_simple * pas
Definition: main.c:58
int gcounter
Definition: main.c:105
#define CKB_NAME(name)
Definition: ckb-anim.h:62
#define CKB_KP_NONE
Definition: ckb-anim.h:90
float x
Definition: main.c:66
ckb_gradient animcolor
Definition: main.c:52
#define CKB_PARSE_AGRADIENT(param_name, gradient_ptr)
Definition: ckb-anim.h:121
#define CKB_TIME_ABSOLUTE
Definition: ckb-anim.h:96
float i
Definition: kiss_fft.h:53
#define CKB_LICENSE(license)
Definition: ckb-anim.h:68
#define CKB_COPYRIGHT(year, author)
Definition: ckb-anim.h:66
Definition: keymap.h:49
unsigned height
Definition: ckb-anim.h:143
#define CKB_DESCRIPTION(description)
Definition: ckb-anim.h:70
int ckb_frame(ckb_runctx *context)
Definition: main.c:137
unsigned keycount
Definition: ckb-anim.h:141
#define CKB_PRESET_START(name)
Definition: ckb-anim.h:85
void ckb_time(ckb_runctx *context, double delta)
Definition: main.c:126
kiss_fft_cpx * outbuf
Definition: main.c:56
#define CKB_KPMODE(mode)
Definition: ckb-anim.h:93
void getFreqDec()
Definition: main.c:107
void kiss_fft_cleanup(void)
Definition: kiss_fft.c:391
#define CKB_PARAM_AGRADIENT(name, prefix, postfix, default)
Definition: ckb-anim.h:80
#define CKB_LIVEPARAMS(enable)
Definition: ckb-anim.h:104
kiss_fft_cfg kiss_fft_alloc(int nfft, int inverse_fft, void *mem, size_t *lenmem)
Definition: kiss_fft.c:339
void anim_remove(int index)
Definition: main.c:87
float r
Definition: kiss_fft.h:52
void ckb_info()
Definition: main.c:5
int max(int a, int b)
Definition: main.c:97
#define CKB_PARSE_BOOL(param_name, value_ptr)
Definition: ckb-anim.h:117
void kiss_fft(kiss_fft_cfg cfg, const kiss_fft_cpx *fin, kiss_fft_cpx *fout)
Definition: kiss_fft.c:385
void ckb_init(ckb_runctx *context)
Definition: main.c:57
double powers[2048]
Definition: main.c:54
#define CKB_REPEAT(enable)
Definition: ckb-anim.h:99
void anim_add(int index)
Definition: main.c:81
int min(int a, int b)
Definition: main.c:101
#define kiss_fft_free
Definition: kiss_fft.h:102
#define CKB_KEYCLEAR(context)
Definition: ckb-anim.h:148
void ckb_start(ckb_runctx *context, int state)
Definition: main.c:114
#define CKB_PRESET_PARAM(name, value)
Definition: ckb-anim.h:86
ckb_key * keys
Definition: ckb-anim.h:140
kiss_fft_cpx * inbuf
Definition: main.c:55
#define CKB_PARAM_BOOL(name, text, default)
Definition: ckb-anim.h:76
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