@youneed/cli-middleware-oscillator
v0.1.0
Published
A synthetic spectrum source + cava-style bar renderer for @youneed/cli: adds this.oscillator and spectrumBars() for terminal audio visualisers.
Readme
@youneed/cli-middleware-oscillator
A synthetic spectrum source and a cava-style bar renderer for
@youneed/cli. The middleware adds this.oscillator — a
deterministic, dependency-free signal (a sum of sines, bass-biased) whose
sample(time) returns per-band magnitudes (0..1) — and spectrumBars() turns
those magnitudes into multi-row block bars. It's not a real FFT, it's a stylised
visualiser: perfect for a demo, a loading screen, or a music-player UI, and
testable because the same time always yields the same frame.
import { Application, Command, task, text } from "@youneed/cli";
import { oscillator, spectrumBars } from "@youneed/cli-middleware-oscillator";
class Vis extends Command("vis", { middleware: [oscillator({ bands: 32, speed: 1.5 })] }) {
t = 0;
execute() {
// animate: bump `t` on a timer and repaint via a task/render loop
task(this, async () => {
for (let i = 0; i < 300; i++) {
this.t = i / 30;
this.requestUpdate?.();
await new Promise((r) => setTimeout(r, 33));
}
}).run();
}
render() {
return text`${spectrumBars(this.oscillator.sample(this.t), { height: 10 })}`;
}
}
const app = Application({ name: "demo", commands: [Vis] });
app.run(["vis"]);Exports
oscillator(opts?)— middleware. Contributesthis.oscillator, anOscillatorbuilt fromcreateOscillator(opts).createOscillator(opts?)— build a standaloneOscillator(no middleware; handy for tests).spectrumBars(values, opts?)— render an array of0..1magnitudes as vertical block bars, top row first, returned as a multi-line string.
Options
OscillatorOptions—{ bands?, speed? }.bandsis the number of columns (default24);speedis the animation multiplier (default1).Oscillator—{ bands, sample(time) }.sample(time)returns the per-band magnitudes fortimein seconds.SpectrumOptions—{ height?, color? }.heightis the bar height in rows (default8);color(cell, value, row)transforms each non-blank glyph (e.g. to add ANSI colour).
