This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createChain(words) { | |
chain = {}; | |
for (const word of words) { | |
addWordToChain(chain, word); | |
} | |
return chain; | |
} | |
/* | |
* Example: Adding "beat" to the chain will |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
'' : ..., | |
'he': ..., | |
'th': { | |
occurances: ['e', 'i', 'u', ...], | |
e: 582, | |
i: 226, | |
u: 56, | |
... | |
total: 1692 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MV_AVG_H | |
#define MV_AVG_H | |
typedef struct { | |
double average; | |
size_t oldest_index; | |
double *samples; | |
size_t num_samples; | |
} movingAverage_t; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |