Skip to content

Instantly share code, notes, and snippets.

View J3698's full-sized avatar
๐Ÿ›
Inch Worm

Anti J3698

๐Ÿ›
Inch Worm
  • Working
  • Working
View GitHub Profile
const fs = require('fs');
const alph = 'abcdefghijklmnopqrstuvwxyz'
demo();
function demo() {
try {
var fileContents = fs.readFileSync('./dictionary.txt', 'utf8');
} catch(err) {
throw "Dictionary file does not exist";
function randomWord(chain, length) {
// get initial prefix
word = randomOccurance(chain, '');
for (var i = 0; i < length - 2; i++) {
lastTwoLetters = word.substr(-2);
// restart if chain hits dead end
if (chain[lastTwoLetters] == undefined) {
return randomWord(chain, length);
function createChain(words) {
chain = {};
for (const word of words) {
addWordToChain(chain, word);
}
return chain;
}
/*
* Example: Adding "beat" to the chain will
{
'' : ...,
'he': ...,
'th': {
occurances: ['e', 'i', 'u', ...],
e: 582,
i: 226,
u: 56,
...
total: 1692
# use gcc as the compiler
CC=gcc
visualizer: simple_visualizer.c libapa102.a moving_average.c
$(CC) -I/usr/local/include -L/usr/local/lib -L./libapa102 -I./libapa102/lib -o visualizer simple_visualizer.c moving_average.c -lapa102 -lwiringPi -lpthread -ljack -lm
libapa102.a: ~/visualizer/libapa102/lib/build/apa102.o ~/visualizer/libapa102/lib/build/apa102_anim.o
ar rcs $@ $^
~/visualizer/libapa102/lib/build/apa102.o:
#ifndef MV_AVG_H
#define MV_AVG_H
typedef struct {
double average;
size_t oldest_index;
double *samples;
size_t num_samples;
} movingAverage_t;
#include <stdlib.h>
#include "moving_average.h"
movingAverage_t* movingAverage_new(size_t num_samples) {
movingAverage_t *ma = malloc(sizeof(movingAverage_t));
ma->samples = malloc(num_samples * sizeof(double));
ma->oldest_index = 0;
ma->num_samples = num_samples;
}
typedef struct {
double average;
size_t oldest_index;
double *samples;
size_t num_samples;
} movingAverage_t;
void movingAverage_update(movingAverage_t *ma, double new_sample) {
ma->average -= ma->samples[ma->oldest_index];
ma->samples[ma->oldest_index] = new_sample;
bool has_beat(sample_t *audio_left, sample_t *audio_right, jack_nframes_t num_samples) {
static movingAverage_t *moving_avg = NULL;
if (moving_avg == NULL) {
moving_avg = movingAverage_new(FILTER_SAMPLES);
}
// beat detected if volume much higher than moving avg
double volume = calc_volume(audio_left, audio_right, num_samples);
bool beat_detected =
volume * FILTER_SAMPLES > BEAT_FACTOR * moving_avg->average;
void update_lights(bool beat_detected) {
static uint8_t blueness = 0;
if (beat_detected) {
blueness = MAX_BRIGHTNESS;
} else if (blueness >= BRIGHTNESS_DECAY) {
blueness = blueness - BRIGHTNESS_DECAY;
} else {
blueness = 0;
}