Skip to content

Instantly share code, notes, and snippets.

@J3698
Last active April 30, 2019 03:54
Show Gist options
  • Save J3698/8526f48e0bc81378ec64ef7b5236a6fb to your computer and use it in GitHub Desktop.
Save J3698/8526f48e0bc81378ec64ef7b5236a6fb to your computer and use it in GitHub Desktop.
Addings Lights to Process
int process(jack_nframes_t nframes, void *arg) {
sample_t *outBufferLeft = (sample_t *) jack_port_get_buffer(output_port_l, nframes);
sample_t *outBufferRight = (sample_t *) jack_port_get_buffer(output_port_r, nframes);
sample_t *inBufferLeft = (sample_t *) jack_port_get_buffer(input_port_l, nframes);
sample_t *inBufferRight = (sample_t *) jack_port_get_buffer(input_port_r, nframes);
// copy input audio to output
memcpy(outBufferLeft, inBufferLeft, sizeof(sample_t) * nframes);
memcpy(outBufferRight, inBufferRight, sizeof(sample_t) * nframes);
// initialize empty array of values
static double *totals = NULL;
if (totals == NULL) {
totals = malloc(sizeof(double) * 1024);
}
static double wholeTotal = 0;
static size_t oldestIndex = 0;
static size_t blueness = 0x66;
// calculate an estimate for the volume of the data
double total = 0;
for (int i = 0; i < nframes; i++) {
total += pow(inBufferLeft[i], 2);
total += pow(inBufferRight[i], 2);
}
total /= FILTER_SAMPLES;
bool beat = false;
if (total * FILTER_SAMPLES > 1.3 * wholeTotal) {
beat = true;
}
printf("w: %f", wholeTotal);
printf("t: %f\n", total);
wholeTotal -= totals[oldestIndex];
totals[oldestIndex] = total;
wholeTotal += totals[oldestIndex];
oldestIndex = (oldestIndex + 1) % FILTER_SAMPLES;
// print to the console based on the volume estimate
size_t decay = 10;
if (blueness >= decay) {
s = blueness - decay;
} else {
blueness = 0;
}
if (beat) {
blueness = 0x66;
}
APA102_Fill(strip, APA102_CreateFrame(31, 0x00, 0x00, blueness));
// uncomment the following line for debugging
//printf("\n%f", total);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment