API Reference
Core Sonifier class methods and return types.
Sonifier
The main class for creating and controlling sonification.
class Sonifier {
constructor(config?: SonifierConfig);
sonify(
data: number[],
method: SonifierMethod,
options?: SonifierOptions,
): Promise<SonifierResult>;
play(audioBuffer: AudioBuffer): Promise<void>;
stop(): void;
getConfig(): Required<SonifierConfig>;
setConfig(config: SonifierConfig): void;
cleanup(): void;
}sonify(data, method, options?)
Converts numeric data into audio.
Parameters
data: number[]— Array of numeric values to sonifymethod: SonifierMethod—'frequency' | 'volume' | 'rhythm' | 'melody'options?: SonifierOptions— Optional playback options
Returns: Promise<SonifierResult>
const result = await sonifier.sonify([10, 20, 30, 40, 50], 'melody', {
autoPlay: true,
});play(audioBuffer)
Plays an AudioBuffer through the Sonifier instance. Starting a new playback automatically stops any previous one.
const result = await sonifier.sonify(data, 'frequency');
await sonifier.play(result.audioBuffer);stop()
Stops the currently playing audio, if any, and resolves the pending play() promise. Does not cancel in-flight audio generation from sonify().
const result = await sonifier.sonify(data, 'melody');
const playback = sonifier.play(result.audioBuffer);
// Later
sonifier.stop();
await playback; // resolves when stoppedgetConfig() / setConfig(config)
Read or update configuration. setConfig merges the provided fields into the current config and re-validates. Unspecified fields keep their existing values.
cleanup()
Releases AudioContext and worker resources. Call when the instance is no longer needed.
Return types
SonifierResult
interface SonifierResult {
audioBuffer: AudioBuffer; // Generated audio buffer
duration: number; // Audio duration in seconds
dataPoints: DataPoint[]; // Mapped data points
}DataPoint
interface DataPoint {
value: number; // Original value
timestamp: number; // Time position in seconds
volume: number; // Volume value
frequency: number; // Frequency value in Hz
note?: string; // Note name for melody: 'C' | 'D' | 'E' | 'F' | 'G' | 'A' | 'B'
}SonifierMethod
type SonifierMethod = 'frequency' | 'volume' | 'rhythm' | 'melody';