actual featured page
This commit is contained in:
@@ -11,18 +11,18 @@
|
||||
*/
|
||||
|
||||
/** PCM output format constants */
|
||||
const SAMPLE_RATE = 44100
|
||||
const CHANNELS = 1
|
||||
const BYTES_PER_SAMPLE = 2 // s16le
|
||||
const SAMPLE_RATE = 44100;
|
||||
const CHANNELS = 1;
|
||||
const BYTES_PER_SAMPLE = 2; // s16le
|
||||
|
||||
/** How many samples to buffer (~1 second) */
|
||||
const RING_BUFFER_SAMPLES = SAMPLE_RATE
|
||||
const RING_BUFFER_SAMPLES = SAMPLE_RATE;
|
||||
|
||||
export interface AudioStreamReaderOptions {
|
||||
/** Audio URL or file path to decode */
|
||||
url: string
|
||||
/** Sample rate (default: 44100) */
|
||||
sampleRate?: number
|
||||
/** Audio URL or file path to decode */
|
||||
url: string;
|
||||
/** Sample rate (default: 44100) */
|
||||
sampleRate?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -30,202 +30,232 @@ export interface AudioStreamReaderOptions {
|
||||
* Each start() increments this; the read loop checks it to know
|
||||
* if it's been superseded and should bail out.
|
||||
*/
|
||||
let globalGeneration = 0
|
||||
let globalGeneration = 0;
|
||||
|
||||
export class AudioStreamReader {
|
||||
private proc: ReturnType<typeof Bun.spawn> | null = null
|
||||
private ringBuffer: Float64Array
|
||||
private writePos = 0
|
||||
private totalSamplesWritten = 0
|
||||
private _running = false
|
||||
private generation = 0
|
||||
readonly url: string
|
||||
private sampleRate: number
|
||||
private proc: ReturnType<typeof Bun.spawn> | null = null;
|
||||
private ringBuffer: Float64Array;
|
||||
private writePos = 0;
|
||||
private totalSamplesWritten = 0;
|
||||
private _running = false;
|
||||
private generation = 0;
|
||||
readonly url: string;
|
||||
private sampleRate: number;
|
||||
|
||||
constructor(options: AudioStreamReaderOptions) {
|
||||
this.url = options.url
|
||||
this.sampleRate = options.sampleRate ?? SAMPLE_RATE
|
||||
this.ringBuffer = new Float64Array(RING_BUFFER_SAMPLES)
|
||||
}
|
||||
constructor(options: AudioStreamReaderOptions) {
|
||||
this.url = options.url;
|
||||
this.sampleRate = options.sampleRate ?? SAMPLE_RATE;
|
||||
this.ringBuffer = new Float64Array(RING_BUFFER_SAMPLES);
|
||||
}
|
||||
|
||||
/** Whether the reader is actively reading samples. */
|
||||
get running(): boolean {
|
||||
return this._running
|
||||
}
|
||||
/** Whether the reader is actively reading samples. */
|
||||
get running(): boolean {
|
||||
return this._running;
|
||||
}
|
||||
|
||||
/** Total number of samples written since start(). */
|
||||
get samplesWritten(): number {
|
||||
return this.totalSamplesWritten
|
||||
}
|
||||
/** Total number of samples written since start(). */
|
||||
get samplesWritten(): number {
|
||||
return this.totalSamplesWritten;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the ffmpeg decode process and begin reading PCM data.
|
||||
*
|
||||
* If already running, the previous process is killed first.
|
||||
* Uses a generation counter to guarantee that only one read loop
|
||||
* is ever active — stale loops from killed processes bail out
|
||||
* immediately.
|
||||
*
|
||||
* @param startPosition Seek position in seconds (default: 0).
|
||||
* @param speed Playback speed multiplier (default: 1). Applies ffmpeg
|
||||
* atempo filter so visualization stays in sync with audio.
|
||||
*/
|
||||
start(startPosition = 0, speed = 1): void {
|
||||
// Always kill the previous process first — no early return on _running
|
||||
this.killProcess()
|
||||
/**
|
||||
* Start the ffmpeg decode process and begin reading PCM data.
|
||||
*
|
||||
* If already running, the previous process is killed first.
|
||||
* Uses a generation counter to guarantee that only one read loop
|
||||
* is ever active — stale loops from killed processes bail out
|
||||
* immediately.
|
||||
*
|
||||
* @param startPosition Seek position in seconds (default: 0).
|
||||
* @param speed Playback speed multiplier (default: 1). Applies ffmpeg
|
||||
* atempo filter so visualization stays in sync with audio.
|
||||
*/
|
||||
start(startPosition = 0, speed = 1): void {
|
||||
// Always kill the previous process first — no early return on _running
|
||||
this.killProcess();
|
||||
|
||||
if (!Bun.which("ffmpeg")) {
|
||||
throw new Error("ffmpeg not found — required for audio visualization")
|
||||
}
|
||||
if (!Bun.which("ffmpeg")) {
|
||||
throw new Error("ffmpeg not found — required for audio visualization");
|
||||
}
|
||||
|
||||
// Increment generation so any lingering read loop from a previous
|
||||
// start() will see a mismatch and exit.
|
||||
this.generation = ++globalGeneration
|
||||
// Increment generation so any lingering read loop from a previous
|
||||
// start() will see a mismatch and exit.
|
||||
this.generation = ++globalGeneration;
|
||||
|
||||
const args = [
|
||||
"ffmpeg",
|
||||
"-loglevel", "quiet",
|
||||
"-reconnect", "1",
|
||||
"-reconnect_streamed", "1",
|
||||
"-reconnect_delay_max", "5",
|
||||
]
|
||||
const args = [
|
||||
"ffmpeg",
|
||||
"-loglevel",
|
||||
"quiet",
|
||||
// Read input at native frame rate so decoded PCM stays in sync with
|
||||
// real-time playback. Without -re, ffmpeg greedily decodes the whole
|
||||
// file as fast as possible: the ring buffer fills with audio seconds
|
||||
// ahead of the player (laggy bars), then the process exits when it
|
||||
// hits EOF (bars freeze ~10s in).
|
||||
"-re",
|
||||
"-reconnect",
|
||||
"1",
|
||||
"-reconnect_streamed",
|
||||
"1",
|
||||
"-reconnect_delay_max",
|
||||
"5",
|
||||
];
|
||||
|
||||
// Seek before input for network efficiency
|
||||
if (startPosition > 0) {
|
||||
args.push("-ss", String(startPosition))
|
||||
}
|
||||
// Seek before input for network efficiency
|
||||
if (startPosition > 0) {
|
||||
args.push("-ss", String(startPosition));
|
||||
}
|
||||
|
||||
args.push("-i", this.url)
|
||||
args.push("-i", this.url);
|
||||
|
||||
// Apply speed via atempo filter if not 1x.
|
||||
// ffmpeg atempo only supports 0.5–100.0; chain multiple for extremes.
|
||||
if (speed !== 1 && speed > 0) {
|
||||
args.push("-af", buildAtempoChain(speed))
|
||||
}
|
||||
// Apply speed via atempo filter if not 1x.
|
||||
// ffmpeg atempo only supports 0.5–100.0; chain multiple for extremes.
|
||||
if (speed !== 1 && speed > 0) {
|
||||
args.push("-af", buildAtempoChain(speed));
|
||||
}
|
||||
|
||||
args.push(
|
||||
"-ac", String(CHANNELS),
|
||||
"-ar", String(this.sampleRate),
|
||||
"-f", "s16le",
|
||||
"-acodec", "pcm_s16le",
|
||||
"-",
|
||||
)
|
||||
args.push(
|
||||
"-ac",
|
||||
String(CHANNELS),
|
||||
"-ar",
|
||||
String(this.sampleRate),
|
||||
"-f",
|
||||
"s16le",
|
||||
"-acodec",
|
||||
"pcm_s16le",
|
||||
"-",
|
||||
);
|
||||
|
||||
this.proc = Bun.spawn(args, {
|
||||
stdout: "pipe",
|
||||
stderr: "ignore",
|
||||
stdin: "ignore",
|
||||
})
|
||||
this.proc = Bun.spawn(args, {
|
||||
stdout: "pipe",
|
||||
stderr: "ignore",
|
||||
stdin: "ignore",
|
||||
});
|
||||
|
||||
this._running = true
|
||||
this.writePos = 0
|
||||
this.totalSamplesWritten = 0
|
||||
this._running = true;
|
||||
this.writePos = 0;
|
||||
this.totalSamplesWritten = 0;
|
||||
|
||||
// Capture generation for this run
|
||||
const myGeneration = this.generation
|
||||
// Capture generation for this run
|
||||
const myGeneration = this.generation;
|
||||
|
||||
// Start async reading loop
|
||||
this.readLoop(myGeneration)
|
||||
// Start async reading loop
|
||||
this.readLoop(myGeneration);
|
||||
|
||||
// Detect process exit
|
||||
this.proc.exited.then(() => {
|
||||
// Only clear _running if this is still the current generation
|
||||
if (this.generation === myGeneration) {
|
||||
this._running = false
|
||||
}
|
||||
}).catch(() => {
|
||||
if (this.generation === myGeneration) {
|
||||
this._running = false
|
||||
}
|
||||
})
|
||||
}
|
||||
// Detect process exit
|
||||
this.proc.exited
|
||||
.then(() => {
|
||||
// Only clear _running if this is still the current generation
|
||||
if (this.generation === myGeneration) {
|
||||
this._running = false;
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
if (this.generation === myGeneration) {
|
||||
this._running = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Read available samples into the provided buffer.
|
||||
* Returns the number of samples actually copied.
|
||||
*
|
||||
* @param out - Float64Array to fill with samples (scaled ~+/-32768 for cavacore).
|
||||
* @returns Number of samples written to `out`.
|
||||
*/
|
||||
read(out: Float64Array): number {
|
||||
const available = Math.min(out.length, this.totalSamplesWritten, this.ringBuffer.length)
|
||||
if (available <= 0) return 0
|
||||
/**
|
||||
* Read available samples into the provided buffer.
|
||||
* Returns the number of samples actually copied.
|
||||
*
|
||||
* @param out - Float64Array to fill with samples (scaled ~+/-32768 for cavacore).
|
||||
* @returns Number of samples written to `out`.
|
||||
*/
|
||||
read(out: Float64Array): number {
|
||||
const available = Math.min(
|
||||
out.length,
|
||||
this.totalSamplesWritten,
|
||||
this.ringBuffer.length,
|
||||
);
|
||||
if (available <= 0) return 0;
|
||||
|
||||
// Read the most recent `available` samples from the ring buffer
|
||||
const readStart = (this.writePos - available + this.ringBuffer.length) % this.ringBuffer.length
|
||||
// Read the most recent `available` samples from the ring buffer
|
||||
const readStart =
|
||||
(this.writePos - available + this.ringBuffer.length) %
|
||||
this.ringBuffer.length;
|
||||
|
||||
if (readStart + available <= this.ringBuffer.length) {
|
||||
out.set(this.ringBuffer.subarray(readStart, readStart + available))
|
||||
} else {
|
||||
const firstChunk = this.ringBuffer.length - readStart
|
||||
out.set(this.ringBuffer.subarray(readStart, this.ringBuffer.length))
|
||||
out.set(this.ringBuffer.subarray(0, available - firstChunk), firstChunk)
|
||||
}
|
||||
if (readStart + available <= this.ringBuffer.length) {
|
||||
out.set(this.ringBuffer.subarray(readStart, readStart + available));
|
||||
} else {
|
||||
const firstChunk = this.ringBuffer.length - readStart;
|
||||
out.set(this.ringBuffer.subarray(readStart, this.ringBuffer.length));
|
||||
out.set(this.ringBuffer.subarray(0, available - firstChunk), firstChunk);
|
||||
}
|
||||
|
||||
return available
|
||||
}
|
||||
return available;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the ffmpeg process and clean up.
|
||||
* Safe to call multiple times. Guarantees the read loop exits.
|
||||
*/
|
||||
stop(): void {
|
||||
// Bump generation to invalidate any running read loop
|
||||
this.generation = ++globalGeneration
|
||||
this._running = false
|
||||
this.killProcess()
|
||||
this.writePos = 0
|
||||
this.totalSamplesWritten = 0
|
||||
}
|
||||
/**
|
||||
* Stop the ffmpeg process and clean up.
|
||||
* Safe to call multiple times. Guarantees the read loop exits.
|
||||
*/
|
||||
stop(): void {
|
||||
// Bump generation to invalidate any running read loop
|
||||
this.generation = ++globalGeneration;
|
||||
this._running = false;
|
||||
this.killProcess();
|
||||
this.writePos = 0;
|
||||
this.totalSamplesWritten = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restart the reader at a new position and/or speed.
|
||||
*/
|
||||
restart(startPosition = 0, speed = 1): void {
|
||||
this.start(startPosition, speed)
|
||||
}
|
||||
/**
|
||||
* Restart the reader at a new position and/or speed.
|
||||
*/
|
||||
restart(startPosition = 0, speed = 1): void {
|
||||
this.start(startPosition, speed);
|
||||
}
|
||||
|
||||
/** Kill the ffmpeg process without touching generation/state. */
|
||||
private killProcess(): void {
|
||||
if (this.proc) {
|
||||
try { this.proc.kill() } catch { /* ignore */ }
|
||||
this.proc = null
|
||||
}
|
||||
}
|
||||
/** Kill the ffmpeg process without touching generation/state. */
|
||||
private killProcess(): void {
|
||||
if (this.proc) {
|
||||
try {
|
||||
this.proc.kill();
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
this.proc = null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Internal: continuously reads stdout from ffmpeg and fills the ring buffer. */
|
||||
private async readLoop(myGeneration: number): Promise<void> {
|
||||
const stdout = this.proc?.stdout
|
||||
if (!stdout || typeof stdout === "number") return
|
||||
/** Internal: continuously reads stdout from ffmpeg and fills the ring buffer. */
|
||||
private async readLoop(myGeneration: number): Promise<void> {
|
||||
const stdout = this.proc?.stdout;
|
||||
if (!stdout || typeof stdout === "number") return;
|
||||
|
||||
const reader = (stdout as ReadableStream<Uint8Array>).getReader()
|
||||
try {
|
||||
while (this.generation === myGeneration) {
|
||||
const { done, value } = await reader.read()
|
||||
if (done || this.generation !== myGeneration) break
|
||||
if (!value || value.byteLength === 0) continue
|
||||
const reader = (stdout as ReadableStream<Uint8Array>).getReader();
|
||||
try {
|
||||
while (this.generation === myGeneration) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done || this.generation !== myGeneration) break;
|
||||
if (!value || value.byteLength === 0) continue;
|
||||
|
||||
const sampleCount = Math.floor(value.byteLength / BYTES_PER_SAMPLE)
|
||||
if (sampleCount === 0) continue
|
||||
const sampleCount = Math.floor(value.byteLength / BYTES_PER_SAMPLE);
|
||||
if (sampleCount === 0) continue;
|
||||
|
||||
const int16View = new Int16Array(
|
||||
value.buffer,
|
||||
value.byteOffset,
|
||||
sampleCount,
|
||||
)
|
||||
const int16View = new Int16Array(
|
||||
value.buffer,
|
||||
value.byteOffset,
|
||||
sampleCount,
|
||||
);
|
||||
|
||||
for (let i = 0; i < sampleCount; i++) {
|
||||
this.ringBuffer[this.writePos] = int16View[i]
|
||||
this.writePos = (this.writePos + 1) % this.ringBuffer.length
|
||||
this.totalSamplesWritten++
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Stream ended or process killed — expected during stop()
|
||||
} finally {
|
||||
try { reader.releaseLock() } catch { /* ignore */ }
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < sampleCount; i++) {
|
||||
this.ringBuffer[this.writePos] = int16View[i];
|
||||
this.writePos = (this.writePos + 1) % this.ringBuffer.length;
|
||||
this.totalSamplesWritten++;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Stream ended or process killed — expected during stop()
|
||||
} finally {
|
||||
try {
|
||||
reader.releaseLock();
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -234,18 +264,18 @@ export class AudioStreamReader {
|
||||
* multiple filters for extreme values (e.g. 0.25 = atempo=0.5,atempo=0.5).
|
||||
*/
|
||||
function buildAtempoChain(speed: number): string {
|
||||
const parts: string[] = []
|
||||
let remaining = Math.max(0.25, Math.min(4, speed))
|
||||
const parts: string[] = [];
|
||||
let remaining = Math.max(0.25, Math.min(4, speed));
|
||||
|
||||
while (remaining > 100) {
|
||||
parts.push("atempo=100.0")
|
||||
remaining /= 100
|
||||
}
|
||||
while (remaining < 0.5) {
|
||||
parts.push("atempo=0.5")
|
||||
remaining /= 0.5
|
||||
}
|
||||
parts.push(`atempo=${remaining}`)
|
||||
while (remaining > 100) {
|
||||
parts.push("atempo=100.0");
|
||||
remaining /= 100;
|
||||
}
|
||||
while (remaining < 0.5) {
|
||||
parts.push("atempo=0.5");
|
||||
remaining /= 0.5;
|
||||
}
|
||||
parts.push(`atempo=${remaining}`);
|
||||
|
||||
return parts.join(",")
|
||||
return parts.join(",");
|
||||
}
|
||||
|
||||
@@ -16,170 +16,178 @@
|
||||
* ```
|
||||
*/
|
||||
|
||||
import { dlopen, FFIType, ptr } from "bun:ffi"
|
||||
import { existsSync } from "fs"
|
||||
import { join, dirname } from "path"
|
||||
import { dlopen, FFIType, ptr } from "bun:ffi";
|
||||
import { existsSync } from "fs";
|
||||
import { join, dirname } from "path";
|
||||
|
||||
// ── Types ────────────────────────────────────────────────────────────
|
||||
|
||||
export interface CavaCoreConfig {
|
||||
/** Number of frequency bars (default: 32) */
|
||||
bars?: number
|
||||
/** Audio sample rate in Hz (default: 44100) */
|
||||
sampleRate?: number
|
||||
/** Number of audio channels (default: 1 = mono) */
|
||||
channels?: number
|
||||
/** Automatic sensitivity: 1 = enabled, 0 = disabled (default: 1) */
|
||||
autosens?: number
|
||||
/** Noise reduction factor 0.0–1.0 (default: 0.77) */
|
||||
noiseReduction?: number
|
||||
/** Low frequency cutoff in Hz (default: 50) */
|
||||
lowCutOff?: number
|
||||
/** High frequency cutoff in Hz (default: 10000) */
|
||||
highCutOff?: number
|
||||
/** Number of frequency bars (default: 32) */
|
||||
bars?: number;
|
||||
/** Audio sample rate in Hz (default: 44100) */
|
||||
sampleRate?: number;
|
||||
/** Number of audio channels (default: 1 = mono) */
|
||||
channels?: number;
|
||||
/** Automatic sensitivity: 1 = enabled, 0 = disabled (default: 1) */
|
||||
autosens?: number;
|
||||
/** Noise reduction factor 0.0–1.0 (default: 0.77) */
|
||||
noiseReduction?: number;
|
||||
/** Low frequency cutoff in Hz (default: 50) */
|
||||
lowCutOff?: number;
|
||||
/** High frequency cutoff in Hz (default: 10000) */
|
||||
highCutOff?: number;
|
||||
/** Output scaling mode: 0 = linear (default), 1 = decibel */
|
||||
scalingMode?: number;
|
||||
}
|
||||
|
||||
const DEFAULTS: Required<CavaCoreConfig> = {
|
||||
bars: 32,
|
||||
sampleRate: 44100,
|
||||
channels: 1,
|
||||
autosens: 1,
|
||||
noiseReduction: 0.77,
|
||||
lowCutOff: 50,
|
||||
highCutOff: 10000,
|
||||
}
|
||||
bars: 32,
|
||||
sampleRate: 44100,
|
||||
channels: 1,
|
||||
autosens: 1,
|
||||
noiseReduction: 0.77,
|
||||
lowCutOff: 50,
|
||||
highCutOff: 10000,
|
||||
scalingMode: 0,
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
type CavaLib = { symbols: Record<string, (...args: any[]) => any>; close(): void }
|
||||
type CavaLib = {
|
||||
symbols: Record<string, (...args: any[]) => any>;
|
||||
close(): void;
|
||||
};
|
||||
|
||||
// ── Library resolution ───────────────────────────────────────────────
|
||||
|
||||
function findLibrary(): string | null {
|
||||
const platform = process.platform
|
||||
const libName = platform === "darwin"
|
||||
? "libcavacore.dylib"
|
||||
: platform === "win32"
|
||||
? "cavacore.dll"
|
||||
: "libcavacore.so"
|
||||
const platform = process.platform;
|
||||
const libName =
|
||||
platform === "darwin"
|
||||
? "libcavacore.dylib"
|
||||
: platform === "win32"
|
||||
? "cavacore.dll"
|
||||
: "libcavacore.so";
|
||||
|
||||
// Candidate paths, in priority order:
|
||||
// 1. src/native/ (development)
|
||||
// 2. Same directory as the running executable (dist bundle)
|
||||
// 3. dist/ relative to cwd
|
||||
const candidates = [
|
||||
join(import.meta.dir, "..", "native", libName),
|
||||
join(dirname(process.execPath), libName),
|
||||
join(process.cwd(), "dist", libName),
|
||||
]
|
||||
// Candidate paths, in priority order:
|
||||
// 1. src/native/ (development)
|
||||
// 2. Same directory as the running executable (dist bundle)
|
||||
// 3. dist/ relative to cwd
|
||||
const candidates = [
|
||||
join(import.meta.dir, "..", "native", libName),
|
||||
join(dirname(process.execPath), libName),
|
||||
join(process.cwd(), "dist", libName),
|
||||
];
|
||||
|
||||
for (const candidate of candidates) {
|
||||
if (existsSync(candidate)) return candidate
|
||||
}
|
||||
for (const candidate of candidates) {
|
||||
if (existsSync(candidate)) return candidate;
|
||||
}
|
||||
|
||||
return null
|
||||
return null;
|
||||
}
|
||||
|
||||
// ── CavaCore class ───────────────────────────────────────────────────
|
||||
|
||||
export class CavaCore {
|
||||
private lib: CavaLib
|
||||
private plan: ReturnType<CavaLib["symbols"]["cava_init"]> | null = null
|
||||
private inputBuffer: Float64Array | null = null
|
||||
private outputBuffer: Float64Array | null = null
|
||||
private _bars = 0
|
||||
private _channels = 1
|
||||
private _destroyed = false
|
||||
private lib: CavaLib;
|
||||
private plan: ReturnType<CavaLib["symbols"]["cava_init"]> | null = null;
|
||||
private inputBuffer: Float64Array | null = null;
|
||||
private outputBuffer: Float64Array | null = null;
|
||||
private _bars = 0;
|
||||
private _channels = 1;
|
||||
private _destroyed = false;
|
||||
|
||||
/** Use loadCavaCore() instead of constructing directly. */
|
||||
constructor(lib: CavaLib) {
|
||||
this.lib = lib
|
||||
}
|
||||
/** Use loadCavaCore() instead of constructing directly. */
|
||||
constructor(lib: CavaLib) {
|
||||
this.lib = lib;
|
||||
}
|
||||
|
||||
/** Number of frequency bars configured. */
|
||||
get bars(): number {
|
||||
return this._bars
|
||||
}
|
||||
/** Number of frequency bars configured. */
|
||||
get bars(): number {
|
||||
return this._bars;
|
||||
}
|
||||
|
||||
/** Whether this instance has been initialized (and not yet destroyed). */
|
||||
get isReady(): boolean {
|
||||
return this.plan !== null && !this._destroyed
|
||||
}
|
||||
/** Whether this instance has been initialized (and not yet destroyed). */
|
||||
get isReady(): boolean {
|
||||
return this.plan !== null && !this._destroyed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the cavacore engine with the given configuration.
|
||||
* Must be called before execute(). Can be called again after destroy()
|
||||
* to reinitialize with different parameters.
|
||||
*/
|
||||
init(config: CavaCoreConfig = {}): void {
|
||||
if (this.plan) {
|
||||
this.destroy()
|
||||
}
|
||||
/**
|
||||
* Initialize the cavacore engine with the given configuration.
|
||||
* Must be called before execute(). Can be called again after destroy()
|
||||
* to reinitialize with different parameters.
|
||||
*/
|
||||
init(config: CavaCoreConfig = {}): void {
|
||||
if (this.plan) {
|
||||
this.destroy();
|
||||
}
|
||||
|
||||
const cfg = { ...DEFAULTS, ...config }
|
||||
this._bars = cfg.bars
|
||||
this._channels = cfg.channels
|
||||
const cfg = { ...DEFAULTS, ...config };
|
||||
this._bars = cfg.bars;
|
||||
this._channels = cfg.channels;
|
||||
|
||||
this.plan = this.lib.symbols.cava_init(
|
||||
cfg.bars,
|
||||
cfg.sampleRate,
|
||||
cfg.channels,
|
||||
cfg.autosens,
|
||||
cfg.noiseReduction,
|
||||
cfg.lowCutOff,
|
||||
cfg.highCutOff,
|
||||
)
|
||||
this.plan = this.lib.symbols.cava_init(
|
||||
cfg.bars,
|
||||
cfg.sampleRate,
|
||||
cfg.channels,
|
||||
cfg.autosens,
|
||||
cfg.noiseReduction,
|
||||
cfg.lowCutOff,
|
||||
cfg.highCutOff,
|
||||
cfg.scalingMode,
|
||||
);
|
||||
|
||||
if (!this.plan) {
|
||||
throw new Error("cava_init returned null — initialization failed")
|
||||
}
|
||||
if (!this.plan) {
|
||||
throw new Error("cava_init returned null — initialization failed");
|
||||
}
|
||||
|
||||
// Pre-allocate output buffer (bars * channels)
|
||||
this.outputBuffer = new Float64Array(cfg.bars * cfg.channels)
|
||||
this._destroyed = false
|
||||
}
|
||||
// Pre-allocate output buffer (bars * channels)
|
||||
this.outputBuffer = new Float64Array(cfg.bars * cfg.channels);
|
||||
this._destroyed = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Feed PCM samples into cavacore and get frequency bar values back.
|
||||
*
|
||||
* @param samples - Float64Array of PCM samples (scaled ~±32768).
|
||||
* The array length determines the number of samples processed.
|
||||
* @returns Float64Array of bar values (0.0–1.0 range, length = bars * channels).
|
||||
* Returns the same buffer reference each call (overwritten in place).
|
||||
*/
|
||||
execute(samples: Float64Array): Float64Array {
|
||||
if (!this.plan || !this.outputBuffer) {
|
||||
throw new Error("CavaCore not initialized — call init() first")
|
||||
}
|
||||
/**
|
||||
* Feed PCM samples into cavacore and get frequency bar values back.
|
||||
*
|
||||
* @param samples - Float64Array of PCM samples (scaled ~±32768).
|
||||
* The array length determines the number of samples processed.
|
||||
* @returns Float64Array of bar values (0.0–1.0 range, length = bars * channels).
|
||||
* Returns the same buffer reference each call (overwritten in place).
|
||||
*/
|
||||
execute(samples: Float64Array): Float64Array {
|
||||
if (!this.plan || !this.outputBuffer) {
|
||||
throw new Error("CavaCore not initialized — call init() first");
|
||||
}
|
||||
|
||||
// Reuse input buffer if same size, otherwise allocate new
|
||||
if (!this.inputBuffer || this.inputBuffer.length !== samples.length) {
|
||||
this.inputBuffer = new Float64Array(samples.length)
|
||||
}
|
||||
this.inputBuffer.set(samples)
|
||||
// Reuse input buffer if same size, otherwise allocate new
|
||||
if (!this.inputBuffer || this.inputBuffer.length !== samples.length) {
|
||||
this.inputBuffer = new Float64Array(samples.length);
|
||||
}
|
||||
this.inputBuffer.set(samples);
|
||||
|
||||
this.lib.symbols.cava_execute(
|
||||
ptr(this.inputBuffer),
|
||||
samples.length,
|
||||
ptr(this.outputBuffer),
|
||||
this.plan,
|
||||
)
|
||||
this.lib.symbols.cava_execute(
|
||||
ptr(this.inputBuffer),
|
||||
samples.length,
|
||||
ptr(this.outputBuffer),
|
||||
this.plan,
|
||||
);
|
||||
|
||||
return this.outputBuffer
|
||||
}
|
||||
return this.outputBuffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Release all native resources. Safe to call multiple times.
|
||||
* After calling destroy(), init() can be called again to reuse the instance.
|
||||
*/
|
||||
destroy(): void {
|
||||
if (this.plan && !this._destroyed) {
|
||||
this.lib.symbols.cava_destroy(this.plan)
|
||||
this.plan = null
|
||||
this._destroyed = true
|
||||
}
|
||||
this.inputBuffer = null
|
||||
this.outputBuffer = null
|
||||
}
|
||||
/**
|
||||
* Release all native resources. Safe to call multiple times.
|
||||
* After calling destroy(), init() can be called again to reuse the instance.
|
||||
*/
|
||||
destroy(): void {
|
||||
if (this.plan && !this._destroyed) {
|
||||
this.lib.symbols.cava_destroy(this.plan);
|
||||
this.plan = null;
|
||||
this._destroyed = true;
|
||||
}
|
||||
this.inputBuffer = null;
|
||||
this.outputBuffer = null;
|
||||
}
|
||||
}
|
||||
|
||||
// ── Factory ──────────────────────────────────────────────────────────
|
||||
@@ -190,41 +198,42 @@ export class CavaCore {
|
||||
* to the static waveform display.
|
||||
*/
|
||||
export function loadCavaCore(): CavaCore | null {
|
||||
try {
|
||||
const libPath = findLibrary()
|
||||
if (!libPath) return null
|
||||
try {
|
||||
const libPath = findLibrary();
|
||||
if (!libPath) return null;
|
||||
|
||||
const lib = dlopen(libPath, {
|
||||
cava_init: {
|
||||
args: [
|
||||
FFIType.i32, // bars
|
||||
FFIType.u32, // rate
|
||||
FFIType.i32, // channels
|
||||
FFIType.i32, // autosens
|
||||
FFIType.double, // noise_reduction
|
||||
FFIType.i32, // low_cut_off
|
||||
FFIType.i32, // high_cut_off
|
||||
],
|
||||
returns: FFIType.ptr,
|
||||
},
|
||||
cava_execute: {
|
||||
args: [
|
||||
FFIType.ptr, // cava_in (double*)
|
||||
FFIType.i32, // samples
|
||||
FFIType.ptr, // cava_out (double*)
|
||||
FFIType.ptr, // plan
|
||||
],
|
||||
returns: FFIType.void,
|
||||
},
|
||||
cava_destroy: {
|
||||
args: [FFIType.ptr], // plan
|
||||
returns: FFIType.void,
|
||||
},
|
||||
})
|
||||
const lib = dlopen(libPath, {
|
||||
cava_init: {
|
||||
args: [
|
||||
FFIType.i32, // bars
|
||||
FFIType.u32, // rate
|
||||
FFIType.i32, // channels
|
||||
FFIType.i32, // autosens
|
||||
FFIType.double, // noise_reduction
|
||||
FFIType.i32, // low_cut_off
|
||||
FFIType.i32, // high_cut_off
|
||||
FFIType.i32, // scaling_mode
|
||||
],
|
||||
returns: FFIType.ptr,
|
||||
},
|
||||
cava_execute: {
|
||||
args: [
|
||||
FFIType.ptr, // cava_in (double*)
|
||||
FFIType.i32, // samples
|
||||
FFIType.ptr, // cava_out (double*)
|
||||
FFIType.ptr, // plan
|
||||
],
|
||||
returns: FFIType.void,
|
||||
},
|
||||
cava_destroy: {
|
||||
args: [FFIType.ptr], // plan
|
||||
returns: FFIType.void,
|
||||
},
|
||||
});
|
||||
|
||||
return new CavaCore(lib as CavaLib)
|
||||
} catch {
|
||||
// Library load failed — missing dylib, wrong arch, etc.
|
||||
return null
|
||||
}
|
||||
return new CavaCore(lib as CavaLib);
|
||||
} catch {
|
||||
// Library load failed — missing dylib, wrong arch, etc.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user