Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <AL/al.h>
- #include <AL/alc.h>
- #include <SDL/SDL.h>
- #include <SDL/SDL_gfxPrimitives.h>
- #include "chuck_fft.h"
- #include <stdio.h>
- #include <math.h>
- #include <usb.h>
- using namespace std;
- static struct usb_device *find_device(uint16_t vendor, uint16_t product);
- static void update_keyboard();
- static char data_pkt_0[] =
- { 0x07, 0x27, 0x00, 0x00, 0xD8, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
- char red_lst[144];
- char grn_lst[144];
- char blu_lst[144];
- char data_pkt[4][64] = { 0 };
- int selected_switch = 1;
- bool switch_on[6] = {false};
- unsigned long switch_time[6] = {0};
- unsigned long sdl_ticks = 0;
- unsigned char buffer[256];
- ALint sample;
- float fft[256];
- unsigned char charfft[256];
- float win[256];
- struct usb_device *dev;
- struct usb_dev_handle *handle;
- float normalizeFFT(float fftin)
- {
- if(fftin > 0)
- {
- return 20.0f*log10(fftin);
- }
- else
- {
- return 0;
- }
- }
- int main(int argc, char *argv[])
- {
- float amplitude = 10.0f;
- float fftavg;
- int level;
- unsigned int red_val, grn_val, blu_val;
- unsigned char red, grn, blu;
- int bytes_written = 0;
- int status = 0;
- SDL_Surface* wavs = NULL;
- SDL_Surface* screen = NULL;
- SDL_Event event;
- printf("Searching for Corsair K70 RGB keyboard...\n");
- dev = find_device(0x1B1C, 0x1B13);
- if(dev == NULL)
- {
- printf("Corsair K70 RGB keyboard not detected :(\n");
- return 1;
- }
- else
- {
- printf("Corsair K70 RGB keyboard detected successfully :)\n");
- }
- handle = usb_open(dev);
- if(handle == NULL)
- {
- printf("USB device handle did not open :(\n");
- return 1;
- }
- else
- {
- printf("USB device handle opened successfully :)\n");
- }
- status = usb_claim_interface(handle, 3);
- usb_detach_kernel_driver_np(handle, 3);
- //if(status == 0)
- //{
- // printf("USB interface claimed successfully :)\n");
- //}
- //else
- //{
- // printf("USB interface claim failed with error %d :(\n", status);
- // return 1;
- //}
- SDL_Init( SDL_INIT_EVERYTHING );
- screen = SDL_SetVideoMode( 256, 256, 32, SDL_HWSURFACE );
- wavs = SDL_SetVideoMode( 256, 256, 32, SDL_SWSURFACE );
- SDL_WM_SetCaption("FanBus Audio Visualizer", NULL);
- ALCdevice *device = alcCaptureOpenDevice(NULL, 12000, AL_FORMAT_MONO8, 256);
- alcCaptureStart(device);
- hanning(win, 256);
- while (true)
- {
- for(int i = 0; i < 256; i++)
- {
- buffer[i] = 0;
- charfft[i] = 0;
- fft[i] = 0;
- }
- alcCaptureSamples(device, (ALCvoid *)buffer, 256);
- level = 0;
- for(int i = 0; i < 256; i++)
- {
- level += abs((int)buffer[i]-128);
- fft[i] = (buffer[i]-128)*amplitude;
- }
- rfft(fft, 256, 1);
- //apply_window(fft, win, 256);
- boxRGBA(wavs, 0, 0, 255, 255, 0, 0, 0, 255);
- for(int i = 0; i < 256; i+=2)
- {
- float fftmag = sqrt((fft[i]*fft[i])+(fft[i+1]*fft[i+1]));
- fftavg += fftmag;
- charfft[i] = (unsigned char)fftmag;
- charfft[i+1] = charfft[i];
- }
- fftavg /= 10;
- for(int i = 0; i < 256; i++)
- {
- lineRGBA(wavs, i, 255, i, 255-charfft[i]*4, 0, 255, 0, 255);
- pixelRGBA(wavs, i, 255- (unsigned char)buffer[i], 255, 0, 0, 255);
- }
- lineRGBA(wavs, 247, 255, 247, 255-(unsigned char)fftavg, 255, 255, 255, 128);
- lineRGBA(wavs, 248, 255, 248, 255-(unsigned char)fftavg, 255, 255, 255, 128);
- lineRGBA(wavs, 249, 255, 249, 255-(unsigned char)fftavg, 255, 255, 255, 128);
- lineRGBA(wavs, 250, 255, 250, 255-(unsigned char)fftavg, 255, 255, 255, 128);
- lineRGBA(wavs, 251, 255, 251, 255-(unsigned char)level/5, 255, 255, 255, 128);
- lineRGBA(wavs, 252, 255, 252, 255-(unsigned char)level/5, 255, 255, 255, 128);
- lineRGBA(wavs, 253, 255, 253, 255-(unsigned char)level/5, 255, 255, 255, 128);
- lineRGBA(wavs, 254, 255, 254, 255-(unsigned char)level/5, 255, 255, 255, 128);
- SDL_BlitSurface(wavs, NULL, screen, NULL);
- SDL_Flip(screen);
- SDL_PollEvent(&event);
- if(event.type == SDL_QUIT)
- {
- return 0;
- }
- SDL_Delay(15);
- //SDL_Delay(25);
- unsigned int blu_avg = 0;
- unsigned int grn_avg = 0;
- unsigned int red_avg = 0;
- for(int i = 10; i < 90; i++)
- {
- blu_avg += charfft[i];
- grn_avg += charfft[i+75];
- red_avg += charfft[i+155];
- }
- blu_avg = blu_avg / 50;
- grn_avg = grn_avg / 20;
- red_avg = red_avg / 15;
- red_val = (exp(red_avg)/2)-1;
- grn_val = (exp(grn_avg)/2)-1;
- blu_val = (exp(blu_avg)/2)-1;
- red = red_val;
- grn = grn_val;
- blu = blu_val;
- if(amplitude < 40)
- {
- amplitude += 0.1f;
- }
- if((red_val > 190) || (grn_val > 200) || (blu_val > 250))
- {
- amplitude -= 0.4f;
- }
- if(red_val > 255)
- {
- red = 255;
- }
- if(grn_val > 255)
- {
- grn = 255;
- }
- if(blu_val > 255)
- {
- blu = 255;
- }
- for(int i = 0; i < 144; i++)
- {
- red_lst[i] = 7 - (red/32);
- grn_lst[i] = 7 - (grn/32);
- blu_lst[i] = 7 - (blu/32);
- }
- update_keyboard();
- }
- alcCaptureStop(device);
- alcCaptureCloseDevice(device);
- return 0;
- }
- static void update_keyboard()
- {
- // Perform USB control message to keyboard
- //
- // Request Type: 0x21
- // Request: 0x09
- // Value 0x0300
- // Index: 0x03
- // Size: 64
- data_pkt[0][0] = 0x7F;
- data_pkt[0][1] = 0x01;
- data_pkt[0][2] = 0x3C;
- data_pkt[1][0] = 0x7F;
- data_pkt[1][1] = 0x02;
- data_pkt[1][2] = 0x3C;
- data_pkt[2][0] = 0x7F;
- data_pkt[2][1] = 0x03;
- data_pkt[2][2] = 0x3C;
- data_pkt[3][0] = 0x7F;
- data_pkt[3][1] = 0x04;
- data_pkt[3][2] = 0x24;
- for(int i = 0; i < 60; i++)
- {
- data_pkt[0][i+4] = red_lst[i*2] << 4 | red_lst[i*2+1];
- }
- for(int i = 0; i < 12; i++)
- {
- data_pkt[1][i+4] = red_lst[i*2+60] << 4 | red_lst[i*2+61];
- }
- for(int i = 0; i < 48; i++)
- {
- data_pkt[1][i+16] = grn_lst[i*2] << 4 | grn_lst[i*2+1];
- }
- for(int i = 0; i < 24; i++)
- {
- data_pkt[2][i+4] = grn_lst[i*2+48] << 4 | grn_lst[i*2+49];
- }
- for(int i = 0; i < 36; i++)
- {
- data_pkt[2][i+28] = blu_lst[i*2] << 4 | blu_lst[i*2+1];
- }
- for(int i = 0; i < 36; i++)
- {
- data_pkt[3][i+4] = blu_lst[i*2+72] << 4 | blu_lst[i*2+73];
- }
- usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[0], 64, 1000);
- usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[1], 64, 1000);
- usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[2], 64, 1000);
- usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt[3], 64, 1000);
- usb_control_msg(handle, 0x21, 0x09, 0x0300, 0x03, data_pkt_0, 64, 1000);
- }
- static struct usb_device *find_device(uint16_t vendor, uint16_t product)
- {
- struct usb_bus *bus;
- struct usb_device *dev;
- struct usb_bus *busses;
- usb_init();
- usb_find_busses();
- usb_find_devices();
- busses = usb_get_busses();
- for(bus = busses; bus; bus = bus->next)
- {
- for(dev = bus->devices; dev; dev = dev->next)
- {
- if((dev->descriptor.idVendor == vendor) && (dev->descriptor.idProduct == product))
- {
- return dev;
- }
- }
- }
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement