private void upDown_ValueChanged(object sender, RoutedPropertyChangedEventArgs <object> e) { if (e.OldValue == null) { return; } LEDSetup.pixelSize = (int)e.NewValue; LEDSetup.resetLEDs(); removeLEDs(); drawLEDs(); }
private void canvasColor_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs <Color?> e) { if (!canvasColor.SelectedColor.HasValue) { return; } Color col = canvasColor.SelectedColor.Value; byte[] packet = LEDSetup.getMagicHeader(); for (int i = 0; i < LEDSetup.LED_C; i++) { LEDSetup.processColorSimple(i, packet, col.R, col.G, col.B); } LEDSetup.sendSerialData(packet); }
protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { DrawTimer?.Stop(); System.Threading.Thread.Sleep(50); if (LEDSetup.ActivePort != null) { if (LEDSetup.ActivePort.IsOpen) { LEDSetup.drawOff(); } } TrayIcon.Dispose(); sColors.stopFluxListener(); Logger.QueueLine("Application closing"); base.OnClosing(e); }
void Heartbeat_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Dispatcher.BeginInvoke(new Action(() => { textBlockFPS.Text = "FPS: " + LEDSetup.frameCount; LEDSetup.frameCount = 0; if ((DateTime.Now - lastBeat).TotalSeconds > 5) { lastBeat = DateTime.Now; if (aColors != null) { textBlockBeats.Text = "BPM: " + 6 * aColors.beats; aColors.beats = aColors.beats / aColors.fftCount; } } })); if (LEDSetup.ActivePort == null) { Logger.QueueLine("No active port, checking for controllers..."); if (LEDSetup.Setup() == null) { Logger.QueueLine("No active Adalight controller detected"); } else { Logger.QueueLine("Starting LED draw timer"); DrawTimer = new System.Timers.Timer(1000.0 / 30.0); DrawTimer.Elapsed += DrawTimer_Elapsed; DrawTimer.Start(); } } else if (!LEDSetup.ActivePort.IsOpen) { Logger.QueueLine("Active port closed, checking for controllers..."); if (LEDSetup.Setup() == null) { Logger.QueueLine("No active Adalight controller detected"); } else { Logger.QueueLine("Starting LED draw timer"); DrawTimer = new System.Timers.Timer(1000.0 / 30.0); DrawTimer.Elapsed += DrawTimer_Elapsed; DrawTimer.Start(); } } }
private void initLEDs() { sColors = new ScreenColors(); //aColors = new AudioColors(); if (LEDSetup.Setup() == null) { Logger.QueueLine("No active Adalight controller detected"); Dispatcher.BeginInvoke(new Action(() => { MessageBox.Show("No active Adalight controller detected"); })); } else { Logger.QueueLine("Starting LED draw timer"); DrawTimer = new System.Timers.Timer(1000.0 / 30.0); DrawTimer.Elapsed += DrawTimer_Elapsed; DrawTimer.Start(); } }
void PowerManager_IsMonitorOnChanged(object sender, EventArgs e) { if (PowerManager.IsMonitorOn) { Logger.QueueLine("Monitor has turned on, resuming LEDs"); LEDSetup.LEDS_ON = true; if (LEDSetup.ActivePort != null) { if (sColors != null) { DrawTimer.Start(); } if (aColors != null) { aColors.wi.StartRecording(); } } } else { Logger.QueueLine("Monitor has turned off, disabling LEDs"); LEDSetup.LEDS_ON = false; if (LEDSetup.ActivePort != null) { if (sColors != null) { DrawTimer.Stop(); } if (aColors != null) { aColors.wi.StopRecording(); } System.Threading.Thread.Sleep(50); LEDSetup.drawOff(); } } }
void doFft(int channel, FftEventArgs e) { // Do something with e.result! float[] output = new float[e.Result.Length]; float val = 0; int val_i = 0; for (int i = 0; i < e.Result.Length; i++) { output[i] = (float)Math.Sqrt(e.Result[i].X * e.Result[i].X + e.Result[i].Y * e.Result[i].Y); if (output[i] > val) { val = output[i]; val_i = i; } } maxVals[maxVali++] = output.Max(); if (maxVali >= maxVals.Length) { maxVali = 0; } float scale = maxVals.Average(); if (scale < 50) { scale = 50; } #if DEBUG //Console.WriteLine("{3}: FFT value max of {0} at index {1} ({2} Hz), scale {4}", val, val_i, val_i * wi.WaveFormat.SampleRate / fftLength, DateTime.Now.Millisecond, scale); #endif int maxi = 12000 / (wi.WaveFormat.SampleRate / fftLength); int s = maxi / (LEDSetup.LED_C / 2); //byte[] serialData = LEDSetup.getMagicHeader(); /* * for (int i = 0; i < LEDSetup.LED_C/2; i++) * { * float sum = 0; * for(int j=i; j<i*s;j++){ * sum+=output[j]; * } * //sum/=(s); * sum /= 100; * LEDSetup.processColor(i, serialData, (int)sum, (int)sum, (int)sum); * LEDSetup.processColor(24-i, serialData, (int)sum, (int)sum, (int)sum); * } * */ int bass1_max = 80 / (wi.WaveFormat.SampleRate / fftLength); float sum = 0; float bassSum = 0; for (int i = 1; i <= bass1_max; i++) { sum += output[i]; } bassSum = sum; sum /= 100; sum *= 4000 / scale; if (sum > 256) { sum = 256; } if (channel == 1) { LEDSetup.processColor(0, serialData, (int)sum, (int)0, (int)sum / 4); } else { LEDSetup.processColor(24, serialData, (int)sum, (int)0, (int)sum / 4); } int bass2_max = 120 / (wi.WaveFormat.SampleRate / fftLength); sum = 0; for (int i = bass1_max - 2; i <= bass2_max; i++) { sum += output[i]; } bassSum += sum; beatVals[beatVali++] = bassSum * bassSum; if (beatVali >= beatVals.Length) { beatVali = 0; } #if DEBUG Console.WriteLine("{2}: Bass sum {0}, beat average {1}", bassSum, beatVals.Average(), DateTime.Now.Millisecond, scale); if (bassSum * bassSum > beatVals.Average() * 2 && bassSum > 200) { if (!beatHit) { beats++; Console.WriteLine("BEAT DETECTED!"); } beatHit = true; } else { beatHit = false; } #endif sum /= 100; sum *= 4000 / scale; if (sum > 256) { sum = 256; } if (channel == 1) { LEDSetup.processColor(1, serialData, (int)sum, (int)0, (int)sum / 4); } else { LEDSetup.processColor(23, serialData, (int)sum, (int)0, (int)sum / 4); } int bass3_max = 170 / (wi.WaveFormat.SampleRate / fftLength); sum = 0; for (int i = bass2_max - 2; i <= bass3_max; i++) { sum += output[i]; } sum /= 100; sum *= 4000 / scale; if (sum > 256) { sum = 256; } if (channel == 1) { LEDSetup.processColor(2, serialData, (int)sum, (int)0, (int)sum / 2); } else { LEDSetup.processColor(22, serialData, (int)sum, (int)0, (int)sum / 2); } int bass4_max = 220 / (wi.WaveFormat.SampleRate / fftLength); sum = 0; for (int i = bass3_max - 2; i <= bass4_max; i++) { sum += output[i]; } sum /= 100; sum *= 4000 / scale; if (sum > 256) { sum = 256; } if (channel == 1) { LEDSetup.processColor(3, serialData, (int)sum, (int)0, (int)sum / 2); } else { LEDSetup.processColor(21, serialData, (int)sum, (int)0, (int)sum / 2); } int mid1_max = 500 / (wi.WaveFormat.SampleRate / fftLength); //440 sum = 0; for (int i = bass4_max - 5; i <= mid1_max; i++) { sum += output[i]; } sum /= 100; sum *= 4000 / scale; if (sum > 256) { sum = 256; } if (channel == 1) { LEDSetup.processColor(4, serialData, (int)sum, (int)sum / 2, (int)sum / 2); } else { LEDSetup.processColor(20, serialData, (int)sum, (int)sum / 2, (int)sum / 2); } int mid2_max = 900 / (wi.WaveFormat.SampleRate / fftLength); sum = 0; for (int i = mid1_max - 5; i <= mid2_max; i++) { sum += output[i]; } sum /= 100; sum *= 4000 / scale; if (sum > 256) { sum = 256; } if (channel == 1) { LEDSetup.processColor(5, serialData, (int)sum, (int)sum / 2, (int)sum / 2); } else { LEDSetup.processColor(19, serialData, (int)sum, (int)sum / 2, (int)sum / 2); } int mid3_max = 2000 / (wi.WaveFormat.SampleRate / fftLength); //1200 sum = 0; for (int i = mid2_max - 5; i <= mid3_max; i++) { sum += output[i]; } sum /= 100; sum *= 4000 / scale; if (sum > 256) { sum = 256; } LEDSetup.processColor(6, serialData, (int)sum, (int)sum / 2, (int)0); LEDSetup.processColor(18, serialData, (int)sum, (int)sum / 2, (int)0); int mid4_max = 4000 / (wi.WaveFormat.SampleRate / fftLength); sum = 0; for (int i = mid3_max - 5; i <= mid4_max; i++) { sum += output[i]; } sum /= 100; sum *= 4000 / scale; if (sum > 256) { sum = 256; } if (channel == 1) { LEDSetup.processColor(7, serialData, (int)sum, (int)sum / 2, (int)0); } else { LEDSetup.processColor(17, serialData, (int)sum, (int)sum / 2, (int)0); } int mid5_max = 6000 / (wi.WaveFormat.SampleRate / fftLength); //5000 sum = 0; for (int i = mid4_max - 5; i <= mid5_max; i++) { sum += output[i]; } sum /= 100; sum *= 4000 / scale; if (sum > 256) { sum = 256; } if (channel == 1) { LEDSetup.processColor(8, serialData, (int)sum, (int)sum / 2, (int)sum / 4); } else { LEDSetup.processColor(16, serialData, (int)sum, (int)sum / 2, (int)sum / 4); } int mid6_max = 8000 / (wi.WaveFormat.SampleRate / fftLength); //5000 sum = 0; for (int i = mid5_max - 5; i <= mid6_max; i++) { sum += output[i]; } sum /= 100; sum *= 4000 / scale; if (sum > 256) { sum = 256; } if (channel == 1) { LEDSetup.processColor(9, serialData, (int)sum, (int)sum / 2, (int)sum / 4); } else { LEDSetup.processColor(15, serialData, (int)sum, (int)sum / 2, (int)sum / 4); } int treb1_max = 10000 / (wi.WaveFormat.SampleRate / fftLength); //8000 sum = 0; for (int i = mid6_max - 10; i <= treb1_max; i++) { sum += output[i]; } sum /= 100; sum *= 4000 / scale; if (sum > 256) { sum = 256; } if (channel == 1) { LEDSetup.processColor(10, serialData, (int)sum, (int)sum, (int)sum / 2); } else { LEDSetup.processColor(14, serialData, (int)sum, (int)sum, (int)sum / 2); } int treb2_max = 12000 / (wi.WaveFormat.SampleRate / fftLength); //8000 sum = 0; for (int i = treb1_max - 10; i <= treb2_max; i++) { sum += output[i]; } sum /= 100; sum *= 4000 / scale; if (sum > 256) { sum = 256; } if (channel == 1) { LEDSetup.processColor(11, serialData, (int)sum, (int)sum, (int)sum / 2); } else { LEDSetup.processColor(13, serialData, (int)sum, (int)sum, (int)sum / 2); } int treb3_max = 15000 / (wi.WaveFormat.SampleRate / fftLength); //8000 sum = 0; for (int i = treb2_max - 10; i <= treb3_max; i++) { sum += output[i]; } sum /= 100; sum *= 4000 / scale; if (sum > 256) { sum = 256; } LEDSetup.processColor(12, serialData, (int)(sum + serialData[6 + 12 * 3]) / 2, (int)(sum + serialData[6 + 12 * 3 + 1]) / 2, (int)((sum * 0.75) + serialData[6 + 12 * 3 + 2]) / 2); fftSent |= channel + 1; if (fftSent == 3) { LEDSetup.sendSerialData(serialData); fftSent = 0; } }
public void draw() { if (LEDSetup.OVERRIDE) { return; } if (device == null) { return; } byte[] serialData = LEDSetup.getMagicHeader(); //Bitmap screenBitmap = getScreenGDI(); Bitmap screenBitmap = getScreenDX(); if (screenBitmap == null) { return; } #if true for (int i = 0; i < LEDSetup.LED_C; i++) { int r = 0, g = 0, b = 0, c = 0; long re = 0, gr = 0, bl = 0; #if true var rect = new System.Drawing.Rectangle((int)LEDSetup.leds[i].Coords.X, (int)LEDSetup.leds[i].Coords.Y, (int)LEDSetup.leds[i].Coords.Width, (int)LEDSetup.leds[i].Coords.Height); byte[] dat; lock (screenBitmap) { var bmpData = screenBitmap.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb); dat = new byte[bmpData.Stride * bmpData.Height]; System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, dat, 0, dat.Length); screenBitmap.UnlockBits(bmpData); } for (int d = 0; d < dat.Length; d += 4 * 60) { // Format is actually Bgr despite the name above being Rgb bl += dat[d] * dat[d]; gr += dat[d + 1] * dat[d + 1]; re += dat[d + 2] * dat[d + 2]; c++; } #else for (int x = (int)LEDSetup.leds[i].Coords.Left; x < (int)LEDSetup.leds[i].Coords.Right; x += 10) { for (int y = (int)LEDSetup.leds[i].Coords.Top; y < (int)LEDSetup.leds[i].Coords.Bottom; y += 10) { System.Drawing.Color col; lock (screenBitmap) { col = screenBitmap.GetPixel(x, y); } re += (long)(col.R * col.R); gr += (long)(col.G * col.G); bl += (long)(col.B * col.B); c++; } } #endif r = (int)(Math.Sqrt(re / c) * rc); g = (int)(Math.Sqrt(gr / c) * gc); b = (int)(Math.Sqrt(bl / c) * bc); if (r > 255) { r = 255; } if (g > 255) { g = 255; } if (b > 255) { b = 255; } LEDSetup.processColor(i, serialData, r, g, b); } #else Parallel.For(0, LEDSetup.LED_C, (i) => { int r = 0, g = 0, b = 0, c = 0; long re = 0, gr = 0, bl = 0; for (int x = (int)LEDSetup.leds[i].Coords.Left; x < (int)LEDSetup.leds[i].Coords.Right; x += 10) { for (int y = (int)LEDSetup.leds[i].Coords.Top; y < (int)LEDSetup.leds[i].Coords.Bottom; y += 10) { System.Drawing.Color col; lock (screenBitmap) { col = screenBitmap.GetPixel(x, y); } if (false) { re += (long)col.R; gr += (long)col.G; bl += (long)col.B; } else { re += (long)(col.R * col.R); gr += (long)(col.G * col.G); bl += (long)(col.B * col.B); } c++; } } if (false) { r = (int)(re / c); g = (int)(gr / c); b = (int)(bl / c); } else { r = (int)Math.Sqrt(re / c); g = (int)Math.Sqrt(gr / c); b = (int)Math.Sqrt(bl / c); } if (r > 255) { r = 255; } if (g > 255) { g = 255; } if (b > 255) { b = 255; } LEDSetup.processColor(i, serialData, r, g, b); }); #endif screenBitmap.Dispose(); LEDSetup.sendSerialData(serialData); }