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
@J3698
J3698 / simple_visualizer.c
Last active April 29, 2019 01:03
A very simple visualizer
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <math.h>
#include <getopt.h>
#include <string.h>
#include <wchar.h>
#include <locale.h>
@J3698
J3698 / process.c
Last active April 29, 2019 01:03
The process method for the audio visualizer
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);
@J3698
J3698 / desktop.html
Last active March 12, 2019 17:39
A simple desktop website
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Site</title>
<meta charset="UTF-8">
<style>
#header {
position: absolute;
top: 0; left: 0;
width: 100%;
@J3698
J3698 / desktop-mobile.html
Last active March 12, 2019 17:39
A mobile-friendly website
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Site</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"</meta>
<style>
#header {
position: absolute;
top: 0; left: 0;
@J3698
J3698 / simple_visualizer.c
Last active May 7, 2019 22:10
Lights Simple Visualizer
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <math.h>
#include <string.h>
#include "moving_average.h"
// for audio
@J3698
J3698 / process_lights.c
Last active April 30, 2019 03:54
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);
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;
}
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;
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;
#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;
}