Skip to content

Instantly share code, notes, and snippets.

View postspectacular's full-sized avatar
🎉
Celebrating 24 years of building open source tools

Karsten Schmidt postspectacular

🎉
Celebrating 24 years of building open source tools
View GitHub Profile
@postspectacular
postspectacular / 004.js
Last active March 7, 2024 06:32
#HowToThing #004 Creating text-based plots to debug & visualize sequential data in a REPL-driven workflow. Here we're using https://thi.ng/dsp signal generators, but any numeric array or iterable will work...
// $ node
// Welcome to Node.js v20.5.1.
// Type ".help" for more information.
const { barChartHStr, lineChartStr } = await import("@thi.ng/text-canvas");
const { adsr, osc, modOsc, rect, saw, sin, tri } = await import("@thi.ng/dsp");
// display line plot of 100 samples from an amplitude-modulate sine oscillator
console.log(lineChartStr(20, modOsc(sin, 0.01, 0, osc(sin, 0.2)).take(100)));
// ╭╮ ╭╮ ╭╮ ╭╮
@postspectacular
postspectacular / foreach.ts
Created August 10, 2023 21:52
Performance comparison between different for-loop variations and payload sizes
import { FORMAT_MD, suite } from "@thi.ng/bench";
import { range } from "@thi.ng/transducers";
const testForIndexFwd = (src: number[]) => {
let m = 0;
for (let i = 0, n = src.length; i < n; i++) m += src[i];
return m;
};
const testForIndexRev = (src: number[]) => {
@postspectacular
postspectacular / theme-swatches.ts
Last active July 10, 2023 10:57
Marblemania color theme swatch generator (context: https://mastodon.thi.ng/@toxi/110689378884928332)
import { sortByCachedKey } from "@thi.ng/arrays";
import { analog, lch, srgb, swatchesH } from "@thi.ng/color";
import { asRGB, type LCHTheme, type RGBTheme } from "@thi.ng/color-palettes";
import { compareNumAsc } from "@thi.ng/compare";
import { serialize } from "@thi.ng/hiccup";
import { svg } from "@thi.ng/hiccup-svg";
import { SYSTEM } from "@thi.ng/random";
import { map, mean, pluck, range } from "@thi.ng/transducers";
import { comparator3 } from "@thi.ng/vectors";
import { writeFileSync } from "fs";
export type FxParamType =
| "number"
| "bigint"
| "boolean"
| "color"
| "string"
| "select";
interface FxParamBigintOpts {
min?: number | bigint;
@postspectacular
postspectacular / lottery.ts
Created April 17, 2023 07:02
Bubblemania lottery script
import { parseCSVSimple } from "@thi.ng/csv";
import { readText } from "@thi.ng/file-io";
import { XsAdd, pickRandom } from "@thi.ng/random";
import { split } from "@thi.ng/strings";
import {
comp,
distinct,
map,
mapcat,
push,
@postspectacular
postspectacular / rectops.ts
Created November 22, 2022 14:27
(Non-exhaustive) selection/overview of rectangle operations provided by https://thi.ng/geom
rect(100)
// Rect { pos: [ 0, 0 ], attribs: undefined, size: [ 100, 100 ] }
rect([100, 200], 100)
// Rect { pos: [ 100, 200 ], attribs: undefined, size: [ 100, 100 ] }
rect([100, 200], [10, 20])
// Rect { pos: [ 100, 200 ], attribs: undefined, size: [ 10, 20 ] }
rectFromCentroid([100, 200], 100)
@postspectacular
postspectacular / sequencer.ts
Last active November 16, 2022 17:22
Generative audio sequencer & WAV file export using thi.ng/dsp
/*
* Copyright (c) 2022 Karsten Schmidt
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
@postspectacular
postspectacular / lottery.ts
Last active October 25, 2022 13:30
NFT giveaway lottery code
import { int, parseCSVSimple } from "@thi.ng/csv";
import { readText } from "@thi.ng/file-io";
import { pickRandom } from "@thi.ng/random";
import { split } from "@thi.ng/strings";
import {
comp,
distinct,
mapcat,
push,
repeat,
@postspectacular
postspectacular / benchmark.js
Last active October 18, 2022 10:43
JS ImageData update benchmarks showing perf gains (2-4x) from using u32 memory views over standard u8 accesses
import { suite } from "@thi.ng/bench";
const w = 640;
const h = 480;
const idata = new ImageData(w, h);
// exposed u8clampedarray
const u8 = idata.data;
// rewrap same memory as u32
const u32 = new Uint32Array(u8.buffer);
@postspectacular
postspectacular / build.sh
Created October 4, 2022 11:00
Zig command to build a WASM library, can't figure out equivalent using `build.zig` - help?
#!/bin/sh
zig build-lib -O ReleaseSmall -target wasm32-freestanding -dynamic --strip \
--pkg-begin wasmapi ../../node_modules/@thi.ng/wasm-api/include/wasmapi.zig --pkg-end \
--pkg-begin dom ../../node_modules/@thi.ng/wasm-api-dom/include/dom.zig --pkg-end \
src/main.zig
# optional post processing & optimization
wasm-opt main.wasm -o main.wasm -O3