Skip to content

Instantly share code, notes, and snippets.

View geraintluff's full-sized avatar

Geraint geraintluff

View GitHub Profile
@geraintluff
geraintluff / tanh-saturator.h
Last active April 10, 2026 11:55
C++ ADAA tanh() saturator
// Tanh() saturator with ADAA (1st-order)
// (c) 2026 Signalsmith Audio
// 0BSD Licence, or you can re-distribution with any license you like, just replace this line
#pragma once
#include <cmath>
template<typename Sample>
class TanhSaturator {
// Accuracy limit to avoid divide-by-(almost)-zero
@geraintluff
geraintluff / pcg32.hpp
Last active April 9, 2026 18:02
32-bit PCG random numbers, in C++
// PCG-32 implementation: https://pcg-random.org/
// (c) 2026 Signalsmith Audio
// 0BSD Licence, or you can re-distribution with any license you like, just replace this line
#include <random>
struct Pcg32 {
uint64_t state;
static constexpr uint64_t multiply = 6364136223846793005ULL;
uint64_t add;
@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