Advertisement
CalcProgrammer1

Corsair K70 RGB Music Visualizer Code

Oct 7th, 2014
1,128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.33 KB | None | 0 0
  1. #include <AL/al.h>
  2. #include <AL/alc.h>
  3. #include <SDL/SDL.h>
  4. #include <SDL/SDL_gfxPrimitives.h>
  5. #include "chuck_fft.h"
  6. #include <stdio.h>
  7. #include <math.h>
  8. #include <usb.h>
  9. using namespace std;
  10.  
  11. static struct usb_device *find_device(uint16_t vendor, uint16_t product);
  12.  
  13. static void update_keyboard();
  14.  
  15. static char data_pkt_0[] =
  16. { 0x07, 0x27, 0x00, 0x00, 0xD8, 0x00, 0x00, 0x00,
  17. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  20. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  21. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  22. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  24.  
  25. char red_lst[144];
  26. char grn_lst[144];
  27. char blu_lst[144];
  28.  
  29. char data_pkt[4][64] = { 0 };
  30.  
  31. int selected_switch = 1;
  32. bool switch_on[6] = {false};
  33. unsigned long switch_time[6] = {0};
  34. unsigned long sdl_ticks = 0;
  35. unsigned char buffer[256];
  36. ALint sample;
  37. float fft[256];
  38. unsigned char charfft[256];
  39. float win[256];
  40. struct usb_device *dev;
  41. struct usb_dev_handle *handle;
  42.  
  43. float normalizeFFT(float fftin)
  44. {
  45. if(fftin > 0)
  46. {
  47. return 20.0f*log10(fftin);
  48. }
  49. else
  50. {
  51. return 0;
  52. }
  53. }
  54.  
  55. int main(int argc, char *argv[])
  56. {
  57. float amplitude = 10.0f;
  58. float fftavg;
  59. int level;
  60. unsigned int red_val, grn_val, blu_val;
  61. unsigned char red, grn, blu;
  62.  
  63. int bytes_written = 0;
  64. int status = 0;
  65.  
  66. SDL_Surface* wavs = NULL;
  67. SDL_Surface* screen = NULL;
  68. SDL_Event event;
  69.  
  70.  
  71. printf("Searching for Corsair K70 RGB keyboard...\n");
  72.  
  73. dev = find_device(0x1B1C, 0x1B13);
  74.  
  75. if(dev == NULL)
  76. {
  77. printf("Corsair K70 RGB keyboard not detected :(\n");
  78. return 1;
  79. }
  80. else
  81. {
  82. printf("Corsair K70 RGB keyboard detected successfully :)\n");
  83. }
  84.  
  85. handle = usb_open(dev);
  86.  
  87. if(handle == NULL)
  88. {
  89. printf("USB device handle did not open :(\n");
  90. return 1;
  91. }
  92. else
  93. {
  94. printf("USB device handle opened successfully :)\n");
  95. }
  96.  
  97. status = usb_claim_interface(handle, 3);
  98.  
  99. usb_detach_kernel_driver_np(handle, 3);
  100. //if(status == 0)
  101. //{
  102. // printf("USB interface claimed successfully :)\n");
  103. //}
  104. //else
  105. //{
  106. // printf("USB interface claim failed with error %d :(\n", status);
  107. // return 1;
  108. //}
  109.  
  110. SDL_Init( SDL_INIT_EVERYTHING );
  111. screen = SDL_SetVideoMode( 256, 256, 32, SDL_HWSURFACE );
  112. wavs = SDL_SetVideoMode( 256, 256, 32, SDL_SWSURFACE );
  113. SDL_WM_SetCaption("FanBus Audio Visualizer", NULL);
  114.  
  115. ALCdevice *device = alcCaptureOpenDevice(NULL, 12000, AL_FORMAT_MONO8, 256);
  116. alcCaptureStart(device);
  117.  
  118. hanning(win, 256);
  119.  
  120. while (true)
  121. {
  122. for(int i = 0; i < 256; i++)
  123. {
  124. buffer[i] = 0;
  125. charfft[i] = 0;
  126. fft[i] = 0;
  127. }
  128.  
  129. alcCaptureSamples(device, (ALCvoid *)buffer, 256);
  130. level = 0;
  131. for(int i = 0; i < 256; i++)
  132. {
  133. level += abs((int)buffer[i]-128);
  134. fft[i] = (buffer[i]-128)*amplitude;
  135. }
  136.  
  137. rfft(fft, 256, 1);
  138. //apply_window(fft, win, 256);
  139. boxRGBA(wavs, 0, 0, 255, 255, 0, 0, 0, 255);
  140.  
  141. for(int i = 0; i < 256; i+=2)
  142. {
  143. float fftmag = sqrt((fft[i]*fft[i])+(fft[i+1]*fft[i+1]));
  144. fftavg += fftmag;
  145. charfft[i] = (unsigned char)fftmag;
  146. charfft[i+1] = charfft[i];
  147. }
  148. fftavg /= 10;
  149. for(int i = 0; i < 256; i++)
  150. {
  151. lineRGBA(wavs, i, 255, i, 255-charfft[i]*4, 0, 255, 0, 255);
  152. pixelRGBA(wavs, i, 255- (unsigned char)buffer[i], 255, 0, 0, 255);
  153. }
  154. lineRGBA(wavs, 247, 255, 247, 255-(unsigned char)fftavg, 255, 255, 255, 128);
  155. lineRGBA(wavs, 248, 255, 248, 255-(unsigned char)fftavg, 255, 255, 255, 128);
  156. lineRGBA(wavs, 249, 255, 249, 255-(unsigned char)fftavg, 255, 255, 255, 128);
  157. lineRGBA(wavs, 250, 255, 250, 255-(unsigned char)fftavg, 255, 255, 255, 128);
  158.  
  159. lineRGBA(wavs, 251, 255, 251, 255-(unsigned char)level/5, 255, 255, 255, 128);
  160. lineRGBA(wavs, 252, 255, 252, 255-(unsigned char)level/5, 255, 255, 255, 128);
  161. lineRGBA(wavs, 253, 255, 253, 255-(unsigned char)level/5, 255, 255, 255, 128);
  162. lineRGBA(wavs, 254, 255, 254, 255-(unsigned char)level/5, 255, 255, 255, 128);
  163. SDL_BlitSurface(wavs, NULL, screen, NULL);
  164. SDL_Flip(screen);
  165. SDL_PollEvent(&event);
  166. if(event.type == SDL_QUIT)
  167. {
  168. return 0;
  169. }
  170. SDL_Delay(15);
  171. //SDL_Delay(25);
  172.  
  173. unsigned int blu_avg = 0;
  174. unsigned int grn_avg = 0;
  175. unsigned int red_avg = 0;
  176. for(int i = 10; i < 90; i++)
  177. {
  178. blu_avg += charfft[i];
  179. grn_avg += charfft[i+75];
  180. red_avg += charfft[i+155];
  181. }
  182. blu_avg = blu_avg / 50;
  183. grn_avg = grn_avg / 20;
  184. red_avg = red_avg / 15;
  185.  
  186. red_val = (exp(red_avg)/2)-1;
  187. grn_val = (exp(grn_avg)/2)-1;
  188. blu_val = (exp(blu_avg)/2)-1;
  189.  
  190. red = red_val;
  191. grn = grn_val;
  192. blu = blu_val;
  193.  
  194. if(amplitude < 40)
  195. {
  196. amplitude += 0.1f;
  197. }
  198.  
  199. if((red_val > 190) || (grn_val > 200) || (blu_val > 250))
  200. {
  201. amplitude -= 0.4f;
  202. }
  203.  
  204. if(red_val > 255)
  205. {
  206. red = 255;
  207. }
  208.  
  209. if(grn_val > 255)
  210. {
  211. grn = 255;
  212. }
  213.  
  214. if(blu_val > 255)
  215. {
  216. blu = 255;
  217. }
  218.  
  219. for(int i = 0; i < 144; i++)
  220. {
  221. red_lst[i] = 7 - (red/32);
  222. grn_lst[i] = 7 - (grn/32);
  223. blu_lst[i] = 7 - (blu/32);
  224. }
  225.  
  226. update_keyboard();
  227. }
  228.  
  229. alcCaptureStop(device);
  230. alcCaptureCloseDevice(device);
  231.  
  232. return 0;
  233. }
  234.  
  235. static void update_keyboard()
  236. {
  237. // Perform USB control message to keyboard
  238. //
  239. // Request Type: 0x21
  240. // Request: 0x09
  241. // Value 0x0300
  242. // Index: 0x03
  243. // Size: 64
  244.  
  245. data_pkt[0][0] = 0x7F;
  246. data_pkt[0][1] = 0x01;
  247. data_pkt[0][2] = 0x3C;
  248.  
  249. data_pkt[1][0] = 0x7F;
  250. data_pkt[1][1] = 0x02;
  251. data_pkt[1][2] = 0x3C;
  252.  
  253. data_pkt[2][0] = 0x7F;
  254. data_pkt[2][1] = 0x03;
  255. data_pkt[2][2] = 0x3C;
  256.  
  257. data_pkt[3][0] = 0x7F;
  258. data_pkt[3][1] = 0x04;
  259. data_pkt[3][2] = 0x24;
  260.  
  261.  
  262. for(int i = 0; i < 60; i++)
  263. {
  264. data_pkt[0][i+4] = red_lst[i*2] << 4 | red_lst[i*2+1];
  265. }
  266.  
  267. for(int i = 0; i < 12; i++)
  268. {
  269. data_pkt[1][i+4] = red_lst[i*2+60] << 4 | red_lst[i*2+61];
  270. }
  271.  
  272. for(int i = 0; i < 48; i++)
  273. {
  274. data_pkt[1][i+16] = grn_lst[i*2] << 4 | grn_lst[i*2+1];
  275. }
  276.  
  277. for(int i = 0; i < 24; i++)
  278. {
  279. data_pkt[2][i+4] = grn_lst[i*2+48] << 4 | grn_lst[i*2+49];
  280. }
  281.  
  282. for(int i = 0; i < 36; i++)
  283. {
  284. data_pkt[2][i+28] = blu_lst[i*2] << 4 | blu_lst[i*2+1];
  285. }
  286.  
  287. for(int i = 0; i < 36; i++)
  288. {
  289. data_pkt[3][i+4] = blu_lst[i*2+72] << 4 | blu_lst[i*2+73];
  290. }
  291.  
  292. usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[0], 64, 1000);
  293. usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[1], 64, 1000);
  294. usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[2], 64, 1000);
  295. usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[3], 64, 1000);
  296. usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt_0, 64, 1000);
  297. }
  298.  
  299.  
  300. static struct usb_device *find_device(uint16_t vendor, uint16_t product)
  301. {
  302. struct usb_bus *bus;
  303. struct usb_device *dev;
  304. struct usb_bus *busses;
  305.  
  306. usb_init();
  307. usb_find_busses();
  308. usb_find_devices();
  309.  
  310. busses = usb_get_busses();
  311.  
  312. for(bus = busses; bus; bus = bus->next)
  313. {
  314. for(dev = bus->devices; dev; dev = dev->next)
  315. {
  316. if((dev->descriptor.idVendor == vendor) && (dev->descriptor.idProduct == product))
  317. {
  318. return dev;
  319. }
  320. }
  321. }
  322.  
  323. return NULL;
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement