Jump to content
Corsair Community

Spectrogram in .Net C#


Billism

Recommended Posts

Complete noob here. How would I go about using this?

 

You will need a program to compile the code from his Github, i suggest using Visual studio, easy installation and free.

 

It is noted that OpenTK is needed to make the program work, so you will need to install that as well.

 

all that is remaining is acctually getting the code from Github, run it in visual studio, and that should be it i belive ?, you may need to exit the corsair software thing, that might get in the way of making it run properly.

Link to comment
Share on other sites

You will need a program to compile the code from his Github, i suggest using Visual studio, easy installation and free.

 

It is noted that OpenTK is needed to make the program work, so you will need to install that as well.

 

all that is remaining is acctually getting the code from Github, run it in visual studio, and that should be it i belive ?, you may need to exit the corsair software thing, that might get in the way of making it run properly.

 

Basically, yea. However, using Visual Studio 2012/2013 and NuGet (on by default), the OpenTK library should download automatically when you build.

 

The pre-built Windows binaries can be downloaded from here:

https://drive.google.com/open?id=0B8K-r-Qxbbn0QlMtNzR5S2xoZUE&authuser=0

 

Though, this has not been tested on any other machine than my own.

Link to comment
Share on other sites

Basically, yea. However, using Visual Studio 2012/2013 and NuGet (on by default), the OpenTK library should download automatically when you build.

 

The pre-built Windows binaries can be downloaded from here:

https://drive.google.com/open?id=0B8K-r-Qxbbn0QlMtNzR5S2xoZUE&authuser=0

 

Though, this has not been tested on any other machine than my own.

 

Just gave it a go and it seems to work nicely,

 

however i had to get the code and change the amplifier down to like 1 to make you being able to see it following the music, other than that it looks very nice good job

 

May i ask where you found the FFT algorithm ?

 

NVM it seems like i cant read, i found it

Link to comment
Share on other sites

I also experience the LEDs feezing up. It seems to lag out after about a second of input. I tracked it down to the HidD_SetFeature method blocking after a couple of write calls. Could it be because I'm connected via 1 USB3.0 cable instead of 2 USB2.0 connections?
Link to comment
Share on other sites

I'm getting an error when I try to run the program on Visual Studio

An unhandled exception of type 'OpenTK.Audio.AudioDeviceException' occurred in OpenTK.dll

 

Additional information: All attempts to open capture devices returned IntPtr.Zero. See debug log for verbose list.

Link to comment
Share on other sites

I'm getting an error when I try to run the program on Visual Studio

An unhandled exception of type 'OpenTK.Audio.AudioDeviceException' occurred in OpenTK.dll

 

Additional information: All attempts to open capture devices returned IntPtr.Zero. See debug log for verbose list.

 

Can you provide the stack trace? Also, can you check to make sure you do indeed have a default audio input device? For the purpose of this program, we want a loopback device, such as Windows' "Stereo Mix" as the default audio input device.

Link to comment
Share on other sites

Ok, it appears to indeed be a synchronization issue. If I put in a delay after each HidD_SetFeature call by inserting System.Threading.Thread.Sleep(5), it works!

Many thanks Billism and CalcProgrammer1!

 

It would appear mine also freezes after a second of input. I, however, am fairly illiterate as to solving these problems myself. Would anyone happen to know where Corski applied this proposed solution?

 

Many thanks in advance!

Link to comment
Share on other sites

In the file, KeyboardWriter.cs, add the following line of code:

 

System.Threading.Thread.Sleep(1);

 

Just after the call to write to the usb, near the of the file. i.e. just after this line:

 

bool c = HidD_SetFeature(this.keyboardUsbDevice, ref usb_pkt[0], 65);

 

If it doesn't work for 1 millisecond in the Sleep call, try a longer delay.

 

In addition to this, you might also need to make sure that CUE is not running.

Link to comment
Share on other sites

i wonder if possible to get more colors option to run the visualization such as orange, violet, brown as the coding only limited to few basic colors such as pure red, green, white, black, skyblue, blue....

 

You can change the colors of the background and the bars by modifying the code in KeyboardWriter.cs, then compiling.

 

public void Write(int iter, byte[] fftData)
       {
           // Rainbow to key lights
           for (int x = 0; x < 91; x++)
           {
               for (int y = 0; y < 7; y++)
               {
                   this.red = (byte)(1.5f * (Math.Sin((x / 92.0f) * 2 * 3.14f) + 1));
                   this.grn = (byte)(1.5f * (Math.Sin(((x / 92.0f) * 2 * 3.14f) - (6.28f / 3)) + 1));
                   this.blu = (byte)(1.5f * (Math.Sin(((x / 92.0f) * 2 * 3.14f) + (6.28f / 3)) + 1));

                   this.SetLed((x + iter) % 92, y, red, grn, blu);
               }
           }

           // FFT Data to key lights
           for (int i = 0; i < 91; i++)
           {
               for (int k = 0; k < 7; k++)
               {
                   if (fftData[(int)(i * 2.1 + 1)] > (255 / (15 + (i * 0.8))) * (7 - k))
                   {
                       this.SetLed(i, k, 0x07, 0x07, 0x07);
                   }
               }
           }

 

The first nest of for loops produces the background lighting, while the second nest of for loops produces the lighting for the bars.

 

You can replace "this.SetLed((x + iter) % 92, y, red, grn, blu);" with something like "this.SetLed((x + iter) % 92, y, 0x00, 0x00, 0x00);" if you want a static color, or you can program your own lighting effect if you have the ability. The last 3 arguments of SetLed are the brightness level of red, green, and blue channels respectively. You can put 0x00, 0x01, ..., all the way to 0x07. (8 levels of brightness each, including black/off)

 

The second for loop nest has the line "this.SetLed(i, k, 0x07, 0x07, 0x07);" which can be changed to something else if you want. It is white by default.

 

I managed to make a rainbowish lighting arrangement with a bit of tweaking, looks pretty cool imo

 

[ame]https://www.youtube.com/watch?v=i9TFnZIe53I[/ame]

Link to comment
Share on other sites

You can change the colors of the background and the bars by modifying the code in KeyboardWriter.cs, then compiling.

 

public void Write(int iter, byte[] fftData)
       {
           // Rainbow to key lights
           for (int x = 0; x < 91; x++)
           {
               for (int y = 0; y < 7; y++)
               {
                   this.red = (byte)(1.5f * (Math.Sin((x / 92.0f) * 2 * 3.14f) + 1));
                   this.grn = (byte)(1.5f * (Math.Sin(((x / 92.0f) * 2 * 3.14f) - (6.28f / 3)) + 1));
                   this.blu = (byte)(1.5f * (Math.Sin(((x / 92.0f) * 2 * 3.14f) + (6.28f / 3)) + 1));

                   this.SetLed((x + iter) % 92, y, red, grn, blu);
               }
           }

           // FFT Data to key lights
           for (int i = 0; i < 91; i++)
           {
               for (int k = 0; k < 7; k++)
               {
                   if (fftData[(int)(i * 2.1 + 1)] > (255 / (15 + (i * 0.8))) * (7 - k))
                   {
                       this.SetLed(i, k, 0x07, 0x07, 0x07);
                   }
               }
           }

 

The first nest of for loops produces the background lighting, while the second nest of for loops produces the lighting for the bars.

 

You can replace "this.SetLed((x + iter) % 92, y, red, grn, blu);" with something like "this.SetLed((x + iter) % 92, y, 0x00, 0x00, 0x00);" if you want a static color, or you can program your own lighting effect if you have the ability. The last 3 arguments of SetLed are the brightness level of red, green, and blue channels respectively. You can put 0x00, 0x01, ..., all the way to 0x07. (8 levels of brightness each, including black/off)

 

The second for loop nest has the line "this.SetLed(i, k, 0x07, 0x07, 0x07);" which can be changed to something else if you want. It is white by default.

 

I managed to make a rainbowish lighting arrangement with a bit of tweaking, looks pretty cool imo

 

https://www.youtube.com/watch?v=i9TFnZIe53I

 

thanks for the advice, and i also manage to build the same effect like you own in the video...=)

Link to comment
Share on other sites

Just wondering, do you have any problem with the program crashing after a while? Mine tends to crash after a few minutes, randomly.

 

i lower down my frequency to 0.7 ~ 1.0f and put System.Threading.Thread.Sleep(5); in the private void SendUsbMessage(byte[] data_pkt). Not sure if works with you.

 

My situation is the led only crash with those specific softwares such as CUE and steam, And the FF 13, 13-2 pc version also.

Link to comment
Share on other sites

i lower down my frequency to 0.7 ~ 1.0f and put System.Threading.Thread.Sleep(5); in the private void SendUsbMessage(byte[] data_pkt). Not sure if works with you.

 

My situation is the led only crash with those specific softwares such as CUE and steam, And the FF 13, 13-2 pc version also.

 

Thanks a lot, that seems to have solved the freezing problem. Runs nice and smooth now!

 

Also, here's some code for a more "true" looking rainbow effect (red, orange, yellow, green, blue, violet, back to red). I don't really know C#, but I figured out enough of it to make it work the way I want it to.

 

Any optimization tips? :p:

 

http://pastebin.com/JEzPsHaG

Link to comment
Share on other sites

Thanks a lot, that seems to have solved the freezing problem. Runs nice and smooth now!

 

Also, here's some code for a more "true" looking rainbow effect (red, orange, yellow, green, blue, violet, back to red). I don't really know C#, but I figured out enough of it to make it work the way I want it to.

 

Any optimization tips? :p:

 

http://pastebin.com/JEzPsHaG

 

actually you can just apply the rainbow motion effect to the spectrograph with this

 

for (int i = 0; i < 91; i++)
           {
               for (int k = 0; k < 7; k++)
               {
                   if (fftData[(int)(i * 2.1 + 1)] > (255 / (15 + (i * 0.8))) * (7 - k))
                   {
                       this.red = (byte)(4.5f * (Math.Sin((i / 92.0f) * 2 * 3.14f) + 1));
                       this.grn = (byte)(4.5f * (Math.Sin(((i / 92.0f) * 2 * 3.14f) - (6.28f / 3)) + 1));
                       this.blu = (byte)(4.5f * (Math.Sin(((i / 92.0f) * 2 * 3.14f) + (6.28f / 3)) + 1));

                       this.SetLed((i + iter) % 92, k, red, grn, blu);
                   }

               }
           }

           UpdateKeyboard();

 

or this one ---- invert rainbow----

 

for (int i = 0; i < 91; i++)
           {
               for (int k = 0; k < 7; k++)
               {
                   if (fftData[(int)(i * 2.1 + 1)] > (255 / (15 + (i * 0.8))) * (7 - k))
                   {
                       this.red = (byte)(4f * (Math.Sin((i % 15.0f) * 2 * 5.14f) + 1));
                       this.grn = (byte)(4f * (Math.Sin(((i % 15.0f) * 2 * 5.14f) - (6.28f / 3)) + 1));
                       this.blu = (byte)(4f * (Math.Sin(((i % 15.0f) * 2 * 5.14f) + (6.28f / 3)) + 1));

                       this.SetLed((i + iter) % 92, k, red, grn, blu);
                   }

               }
           }

           UpdateKeyboard();

Link to comment
Share on other sites

actually you can just apply the rainbow motion effect to the spectrograph with this

 

That was a crude rainbow-ish thing I came up with using the sin functions. It isn't a true rainbow in that it never actually displays pure single color. To get a proper rainbow look into HSV (hue, saturation, value - aka HSB - hue, saturation, brightness) to RGB conversion. I just used HSV to RGB to make a much better rainbow effect on an Arduino with a WS2812b LED strip but the same technique works here.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...