@ferrite-audio/web-utils
Version:
Web Audio utilities for Ferrite Audio packages
174 lines • 6.61 kB
TypeScript
/**
* Audio utility functions for Web Audio API and WASM integration
*
* This module provides browser-specific audio utilities and acts as a bridge
* between the Web Audio API and our Rust/WASM processing core.
* Heavy DSP processing is delegated to the WASM module.
*/
import type { ProcessingError } from './types';
import { ProcessingErrorType } from './types';
/** Default sample rate if none specified */
export declare const DEFAULT_SAMPLE_RATE = 48000;
/** Default FFT size for spectral processing */
export declare const DEFAULT_FFT_SIZE = 512;
/**
* Get or create the global audio context
* Handles user gesture requirements for Chrome/Safari
*/
export declare function getAudioContext(): AudioContext;
/**
* Resume audio context if suspended (requires user interaction)
*/
export declare function resumeAudioContext(): Promise<void>;
/**
* Create an offline audio context for non-realtime processing
*/
export declare function createOfflineContext(channels: number, length: number, sampleRate: number): OfflineAudioContext;
/**
* Create an AudioBuffer from Float32Array data
* This is browser-specific and can't be done in WASM
*/
export declare function createAudioBuffer(data: Float32Array | number[], sampleRate?: number, channels?: number): AudioBuffer;
/**
* Extract Float32Array from AudioBuffer for WASM processing
*/
export declare function extractChannelData(buffer: AudioBuffer, channel?: number): Float32Array;
/**
* Extract and interleave all channels for WASM processing
*/
export declare function extractInterleavedData(buffer: AudioBuffer): Float32Array;
/**
* Clone an AudioBuffer
*/
export declare function cloneAudioBuffer(source: AudioBuffer): AudioBuffer;
/**
* Get user audio stream with proper constraints
*/
export declare function getUserAudioStream(constraints?: MediaStreamConstraints): Promise<MediaStream>;
/**
* Create a MediaStreamSource node
*/
export declare function createStreamSource(stream: MediaStream, context?: AudioContext): MediaStreamAudioSourceNode;
/**
* Create a ScriptProcessor node for real-time processing
* Note: ScriptProcessor is deprecated but more widely supported than AudioWorklet
*/
export declare function createProcessor(inputChannels?: number, outputChannels?: number, bufferSize?: number, context?: AudioContext): ScriptProcessorNode;
/**
* Stop all tracks in a media stream
*/
export declare function stopStream(stream: MediaStream): void;
/**
* Check if AudioWorklet is supported
*/
export declare function isAudioWorkletSupported(): boolean;
/**
* Load AudioWorklet processor module
*/
export declare function loadAudioWorklet(moduleUrl: string, context?: AudioContext): Promise<void>;
/**
* Create AudioWorklet node
*/
export declare function createAudioWorkletNode(processorName: string, options?: AudioWorkletNodeOptions, context?: AudioContext): AudioWorkletNode;
/**
* Load audio file as AudioBuffer
*/
export declare function loadAudioFile(file: File | Blob): Promise<AudioBuffer>;
/**
* Load audio from URL
*/
export declare function loadAudioFromURL(url: string): Promise<AudioBuffer>;
/**
* Convert AudioBuffer to WAV blob for download
* This is browser-specific functionality
*/
export declare function audioBufferToWav(buffer: AudioBuffer): Blob;
/**
* Download AudioBuffer as WAV file
*/
export declare function downloadAudioBuffer(buffer: AudioBuffer, filename?: string): void;
/**
* Prepare audio data for WASM processing
* Ensures data is in the correct format and within valid ranges
*/
export declare function prepareForWasm(data: Float32Array): Float32Array;
/**
* Create shared memory for WASM if supported
*/
export declare function createSharedBuffer(size: number): SharedArrayBuffer | ArrayBuffer;
/**
* Create a processing error
*/
export declare function createProcessingError(type: ProcessingErrorType, message: string, details?: Record<string, any>): ProcessingError;
/**
* Detect browser audio capabilities
*/
export declare function detectCapabilities(): {
audioContext: boolean;
getUserMedia: boolean;
audioWorklet: boolean;
sharedArrayBuffer: boolean;
offlineContext: boolean;
mediaRecorder: boolean;
};
/**
* Check if running in a secure context (required for some APIs)
*/
export declare function isSecureContext(): boolean;
/**
* Format time in seconds to MM:SS
*/
export declare function formatTime(seconds: number): string;
/**
* Get audio buffer info as string (for debugging)
*/
export declare function bufferToString(buffer: AudioBuffer): string;
/**
* Estimate processing latency based on buffer size and sample rate
*/
export declare function estimateLatency(bufferSize: number, sampleRate: number): number;
/**
* Check if Web Workers are supported
*/
export declare function isWorkerSupported(): boolean;
/**
* Create a processing worker
*/
export declare function createProcessingWorker(workerUrl: string): Worker | null;
/**
* Transfer audio data to worker efficiently
*/
export declare function transferToWorker(worker: Worker, data: Float32Array, transfer?: boolean): void;
export declare const AudioUtils: {
getAudioContext: typeof getAudioContext;
resumeAudioContext: typeof resumeAudioContext;
createOfflineContext: typeof createOfflineContext;
createAudioBuffer: typeof createAudioBuffer;
extractChannelData: typeof extractChannelData;
extractInterleavedData: typeof extractInterleavedData;
cloneAudioBuffer: typeof cloneAudioBuffer;
getUserAudioStream: typeof getUserAudioStream;
createStreamSource: typeof createStreamSource;
createProcessor: typeof createProcessor;
stopStream: typeof stopStream;
isAudioWorkletSupported: typeof isAudioWorkletSupported;
loadAudioWorklet: typeof loadAudioWorklet;
createAudioWorkletNode: typeof createAudioWorkletNode;
loadAudioFile: typeof loadAudioFile;
loadAudioFromURL: typeof loadAudioFromURL;
audioBufferToWav: typeof audioBufferToWav;
downloadAudioBuffer: typeof downloadAudioBuffer;
prepareForWasm: typeof prepareForWasm;
createSharedBuffer: typeof createSharedBuffer;
createProcessingError: typeof createProcessingError;
detectCapabilities: typeof detectCapabilities;
isSecureContext: typeof isSecureContext;
isWorkerSupported: typeof isWorkerSupported;
createProcessingWorker: typeof createProcessingWorker;
transferToWorker: typeof transferToWorker;
formatTime: typeof formatTime;
bufferToString: typeof bufferToString;
estimateLatency: typeof estimateLatency;
};
export default AudioUtils;
//# sourceMappingURL=audio-utils.d.ts.map