Skip to content

Instantly share code, notes, and snippets.

@moritzuehling
Last active March 11, 2016 17:40
Show Gist options
  • Save moritzuehling/6b5c25c8da4b3741fc0f to your computer and use it in GitHub Desktop.
Save moritzuehling/6b5c25c8da4b3741fc0f to your computer and use it in GitHub Desktop.
#include "FastLED.h"
#define NUM_LEDS 120
#define DATA_PIN 6
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
for(int i = 0; i < NUM_LEDS; i++) {
leds[i].red = 5;
leds[i].green = 0;
leds[i].blue = 0;
}
pinMode(13, OUTPUT);
Serial.begin(115200);
}
void loop() {
if (Serial.available() > 0) {
for(int i = 0; i < NUM_LEDS; i++) {
int data = -1;
while(data < 0) {
data = Serial.read();
}
// read the incoming byte:
byte hue = (byte)(data * .375);
leds[i] = CHSV(96 - hue, 255 - hue, max(data, 5));
}
}
FastLED.show();
}
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NAudio.Wave.SampleProviders;
using NAudio.Wave;
using System.IO.Pipes;
namespace SerialThing
{
class Program
{
const byte NUM_LEDS = 120;
const int MAX_FREQ = 360;
const int factor = (int)((float)MAX_FREQ / NUM_LEDS);
static void Main(string[] args)
{
SampleAggregator sampleAggregator = new SampleAggregator(2048);
byte[] data = new byte[NUM_LEDS];
sampleAggregator.FftCalculated += (s, e) => {
var newData = new byte[NUM_LEDS];
for(var i = 0; i < NUM_LEDS; i++)
{
newData[i] = (byte)(data[i] * 0);
}
for (var i = 0; i < MAX_FREQ; i++)
{
var c = e.Result[i];
double intensityDB = 8 * Math.Log(Math.Sqrt(c.X * c.X + c.Y * c.Y));
double minDB = -96;
if (intensityDB < minDB) intensityDB = minDB;
double percent = Math.Min(intensityDB / minDB, 1);
newData[i / factor] = Math.Max((byte)((1 - percent) * (255)), newData[i / factor]);
}
data = newData;
};
sampleAggregator.PerformFFT = true;
// Here you decide what you want to use as the waveIn.
// There are many options in NAudio and you can use other streams/files.
// Note that the code varies for each different source.
var waveIn = new WasapiLoopbackCapture();
waveIn.DataAvailable += (s, e) => {
byte[] buffer = e.Buffer;
int bytesRecorded = e.BytesRecorded;
int bufferIncrement = waveIn.WaveFormat.BlockAlign;
for (int index = 0; index < bytesRecorded; index += bufferIncrement)
{
float sample32 = BitConverter.ToSingle(buffer, index);
sampleAggregator.Add(sample32);
}
};
waveIn.StartRecording();
SerialPort port = new SerialPort("COM6", 115200);
port.Open();
while (true)
{
port.Write(data, 0, NUM_LEDS);
System.Threading.Thread.Sleep(20);
}
}
private static void InputStream_DataAvailable(object sender, WaveInEventArgs e)
{
throw new NotImplementedException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment