Skip to content

Instantly share code, notes, and snippets.

// Second-order IIR filter with arbitrary coefficient configurations
class IIRFilter {
constructor(a0, a1, a2, b1, b2) {
this.recalcCoeffs(a0, a1, a2, b1, b2);
}
recalcCoeffs(a0 = 0, a1 = 0, a2 = 0, b1 = 0, b2 = 0) {
this.a0 = a0;
this.a1 = a1;
this.a2 = a2;
@TF3RDL
TF3RDL / Sb-sDFT.js
Last active January 15, 2024 07:45
Variable-Q sliding DFT algorithm
/**
* Single file implementation of variable-Q sliding DFT (VQ-sDFT)
*
* The frequency bands data is formatted like:
* {lo: lowerBound,
* ctr: center,
* hi: higherBound}
*
* where lo and hi are used for calculating the necessary bandwidth for variable-Q/constant-Q transform spectrum analysis and ctr for center frequency. This is generated using functions like generateFreqBands()
*
@TF3RDL
TF3RDL / cameraShake.js
Created May 26, 2023 08:35
Trigger-based shake effect from musicvid.org
class TriggerBasedShake {
constructor(
returnSpeed = 0.1,
displacementThreshold = 4,
waveDuration = Math.PI/8,
movementAmount = 1,
maxShakeIntensity = 1,
maxShakeDisplacement = 4,
minShakeScalar = 0.9,
maxShakeScalar = 1.6,
@TF3RDL
TF3RDL / analog-style-analyzer.js
Last active February 29, 2024 23:12
Implementation of the sliding windowed infinite Fourier transform with functionality to cascade sDFTs serially as well as the analog-style spectrum analyzer implementation
/**
* Single file implementation of analog-style spectrum analyzer using cascaded biquad bandpass filter bank (works best on truly-logarithmic frequency scale and constant-Q resolution)
*
* The frequency bands data is formatted like:
* {lo: lowerBound,
* ctr: center,
* hi: higherBound}
*
* where lo and hi are used for calculating the necessary bandwidth for variable-Q transform spectrum visualizations and ctr for center frequency. This is generated using functions like generateFreqBands
* Note: This one uses its own biquad filter routine for calculating biquad bandpass filter instead of WebAudio API provided BiquadFilterNode, so it allows visualization of data that isn't WebAudio and no OfflineAudioContext is needed
@TF3RDL
TF3RDL / audio-analyzer.js
Last active September 8, 2024 15:42
A collection of audio analysis algorithms and some helper functions
/**
* A collection of audio analysis algorithms and some helper functions
*/
// p5.js map function
function map(x, min, max, targetMin, targetMax) {
return (x - min) / (max - min) * (targetMax - targetMin) + targetMin;
}
function clamp(x, min, max) {
@TF3RDL
TF3RDL / cqt.js
Last active June 16, 2023 22:02
Constant-Q transform using Goertzel algorithm
/**
* Constant-Q Transform (CQT) calculated using Goertzel algorithm
*
* This by itself doesn't need Web Audio API in order to work but it is necessary for real-time visualizations
*
* Real-time usage:
* analyserNode.getFloatTimeDomainData(dataArray);
* const spectrum = cqt(dataArray, freqBands, audioCtx.sampleRate, bandwidthOffset, windowFunction);
*
* Note: the implementation of this CQT is slow compared to FFT