Skip to content

Instantly share code, notes, and snippets.

View geraintluff's full-sized avatar

Geraint geraintluff

View GitHub Profile
@geraintluff
geraintluff / main.cpp
Created February 1, 2025 19:15
Test code and output plots comparing two very-close BLAMPs vs a single BLEP, with single-precision float
#include "elliptic-blep.h"
#include "plot.h"
#include "simple-fft.h"
#include <vector>
void generateWaveform(float freq, float shape, std::vector<float> &result, bool sawtooth) {
float phase = 0;
float gradUp = freq*2/shape;
@geraintluff
geraintluff / apple-clang-16.0.0-warning.h
Last active January 20, 2025 17:57
Code to break a build when using Apple Clang 16.0.0 with `--fast-math`
#if defined(__FAST_MATH__) && (__apple_build_version__ >= 16000000) && (__apple_build_version__ <= 16000099)
# error Apple Clang 16.0.0 is broken, and generates completely incorrect code for some SIMD operations. -ffast-math makes it worse, so if you HAVE to use this version of Clang, you can't enable -ffast-math.
#endif
#include <complex>
#include <vector>
#include <cmath>
#include "./linear.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
@geraintluff
geraintluff / json-simplified.js
Created February 20, 2024 10:26
Simple RegEx for slightly prettier JSON-ish output (unquoted keys, single-item objects/arrays on one line)
JSON.simplified = v => {
return JSON.stringify(v, null, '\t')
.replace(/(\n\t*)"([a-z][a-z0-9_]*)"/ig, '$1$2')
.replace(/(\n[\sa-z0-9_:]+)\{\n\t*(.*)\n\t+\}/ig, '$1{$2}')
.replace(/(\n[\sa-z0-9_:]+)\[\n\t*(.*)\n\t+\]/ig, '$1[$2]');
};
JSON.parseSimplified = t => {
return JSON.parse(t
.replace(/(\n[\sa-z0-9_:]+)([\[\{])([^\n])/ig, '$1$2\n$3')
.replace(/(\n\t*)([a-z][a-z0-9_]*):/ig, '$1"$2":')
@geraintluff
geraintluff / csv-writer.h
Last active January 25, 2022 13:56
Example of outputting CSV from C++, and picking it up with Python (Matplotlib)
#ifndef UTIL_CSV_WRITER
#define UTIL_CSV_WRITER
/* Optional helper class for outputting CSV data. Use like:
CsvWriter csv("out"); // no .csv suffix
// write individual values
csv.write("foo", "bar");
csv.line();
@geraintluff
geraintluff / unit-range-reciprocal.hpp
Last active January 8, 2025 20:39
A map from [0-1] to an arbitrary range, specified using low/mid/high values. Pulled out from some Signalsmith Audio internal code.
class UnitRangeMapReciprocal {
double vMin, vTopFactor, vBottomFactor;
public:
UnitRangeMapReciprocal() : UnitRangeMapReciprocal(0, 0.5, 1) {}
UnitRangeMapReciprocal(double min, double mid, double max) {
vMin = min;
double k = (mid - min)/(max - mid);
vTopFactor = max*k - min;
vBottomFactor = k - 1;
}
{
"https://twitter.com/every_peano/status/590740112625008641": {
"exact": 0
},
"https://twitter.com/every_peano/status/1058469466714132480": {
"relative": "https://twitter.com/every_peano/status/592203098388566016",
"N": 30900
},
"https://twitter.com/every_peano/status/1057001290117595139": {
"relative": "https://twitter.com/every_peano/status/1058469466714132480",
@geraintluff
geraintluff / ffmpeg-install.sh
Created March 30, 2016 10:55 — forked from clayton/ffmpeg-install.sh
Install FFMPEG on OS X with HomeBrew to convert Mp4 to WebM
# Installation
brew install ffmpeg --with-vpx --with-vorbis --with-libvorbis --with-vpx --with-vorbis --with-theora --with-libogg --with-libvorbis --with-gpl --with-version3 --with-nonfree --with-postproc --with-libaacplus --with-libass --with-libcelt --with-libfaac --with-libfdk-aac --with-libfreetype --with-libmp3lame --with-libopencore-amrnb --with-libopencore-amrwb --with-libopenjpeg --with-openssl --with-libopus --with-libschroedinger --with-libspeex --with-libtheora --with-libvo-aacenc --with-libvorbis --with-libvpx --with-libx264 --with-libxvid
# Easy Peasy
ffmpeg -i video.mp4 video.webm
@geraintluff
geraintluff / base64-web-safe.js
Created October 9, 2015 15:36
Convert base64 to and from web-safe variant
// Convert from normal to web-safe, strip trailing "="s
function webSafe64(base64) {
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
// Convert from web-safe to normal, add trailing "="s
function normal64(base64) {
return base64.replace(/\-/g, '+').replace(/_/g, '/') + '=='.substring(0, (3*base64.length)%4);
}
@geraintluff
geraintluff / README.md
Last active August 29, 2015 14:22
Your Unix machine is awaited in Valhalla - witness!

The shutdown messages on the Mac/Linux command line are baked into the /sbin/shutdown executable.

This replaces the messages with suitable Mad Max: Fury Road quotes. You can make your own, but since you're patching a binary file the replacements MUST, I repeat MUST be exactly the same number of characters as the original, or your computer will not ride eternal, shiny and chrome.

The two versions below are for a Ubuntu Linux variant, and Mac OS X Yosemite (both versions Work For Me on at least one machine).