UNPKG

@ferrite-audio/core

Version:

Core WASM audio processing engine for Ferrite Audio

121 lines 3.9 kB
/** * High-level wrapper for the WASM NoiseReducer */ import init, { NoiseReducer, getWasmMemory } from './wasm_audio_ferrite.js'; export class FerriteCore { constructor(config) { this.reducer = null; this.initialized = false; this.config = config; } /** * Initialize the WASM module */ async initialize(wasmPath) { if (this.initialized) return; // Initialize WASM - handle both browser and Node.js if (wasmPath) { await init(wasmPath); } else if (typeof window !== 'undefined') { // Browser - use relative path await init(); } else { // Node.js - will need buffer throw new Error('In Node.js, provide wasmPath or buffer to initialize()'); } // Create noise reducer this.reducer = new NoiseReducer(this.config.sampleRate); // Apply configuration if (this.config.enableGate !== undefined) { this.reducer.set_gate_enabled(this.config.enableGate); } if (this.config.gateThreshold !== undefined) { this.reducer.set_gate_threshold(this.config.gateThreshold); } if (this.config.enableSpectral !== undefined) { this.reducer.set_spectral_enabled(this.config.enableSpectral); } if (this.config.reductionAmount !== undefined) { this.reducer.set_reduction_amount(this.config.reductionAmount); } if (this.config.wienerFilter !== undefined) { this.reducer.set_wiener_filter_mode(this.config.wienerFilter); } this.initialized = true; } /** * Process audio samples */ process(input) { if (!this.reducer) { throw new Error('FerriteCore not initialized'); } return this.reducer.process(input); } /** * Process audio in-place using zero-copy API */ processInPlace(buffer) { if (!this.reducer) { throw new Error('FerriteCore not initialized'); } const memory = getWasmMemory(); if (!memory || !this.reducer.alloc_buffer) { // Fallback to regular processing const result = this.reducer.process(buffer); buffer.set(result); return; } // Zero-copy processing using processPtr const ptr = this.reducer.alloc_buffer(buffer.length); const wasmBuffer = new Float32Array(memory.buffer, ptr, buffer.length); wasmBuffer.set(buffer); this.reducer.processPtr(ptr, buffer.length); buffer.set(wasmBuffer); } /** * Learn noise profile from samples */ learnNoise(samples) { if (!this.reducer) { throw new Error('FerriteCore not initialized'); } this.reducer.learn_noise(samples); } /** * Update configuration */ updateConfig(config) { if (!this.reducer) return; if (config.enableGate !== undefined) { this.reducer.set_gate_enabled(config.enableGate); } if (config.gateThreshold !== undefined) { this.reducer.set_gate_threshold(config.gateThreshold); } if (config.enableSpectral !== undefined) { this.reducer.set_spectral_enabled(config.enableSpectral); } if (config.reductionAmount !== undefined) { this.reducer.set_reduction_amount(config.reductionAmount); } if (config.wienerFilter !== undefined) { this.reducer.set_wiener_filter_mode(config.wienerFilter); } } /** * Clean up resources */ dispose() { if (this.reducer) { this.reducer.free(); this.reducer = null; } this.initialized = false; } } //# sourceMappingURL=ferrite-core.js.map