Skip to content

Instantly share code, notes, and snippets.

@Joehoel
Created April 24, 2024 10:50
Show Gist options
  • Save Joehoel/1d4d3d7bd70037175c51d0b0c489b88c to your computer and use it in GitHub Desktop.
Save Joehoel/1d4d3d7bd70037175c51d0b0c489b88c to your computer and use it in GitHub Desktop.
Audio Processor
import { FFT } from 'fft-js'; // This is a placeholder, ensure to use an appropriate FFT library
import * as fs from 'fs';
import { createReadStream, createWriteStream } from 'fs';
import { EventEmitter } from 'events';
const TwoPi = Math.PI * 2;
const FourPi = Math.PI * 4;
const FSam = 44100;
const nOLA = 2;
const nFFT = 2048;
const nFFT2 = nFFT / 2;
const nTap = nFFT2;
const nZero = nFFT2;
const nSam = nFFT - nZero;
const nShift = nSam / nOLA;
const Freq = [125, 187, 250, 375, 500, 750, 1000, 1500, 2000, 3000, 4000, 6000, 8000, 12000, 16000];
const Smearing: number[] = [0.4, 0.5, 0.9, 1.2, 1.5, 1.8, 2.0, 2.3, 3.1, 3.9];
type RealArray = Float64Array;
interface Threshold { hearingLossDb: number[]; }
class AudioProcessor {
private fft: FFT;
private buffer: Int16Array;
private windowFunction: RealArray;
constructor() {
this.fft = new FFT(nFFT);
this.buffer = new Int16Array(nFFT);
this.windowFunction = this.blackmanWindow();
}
private blackmanWindow(): RealArray {
let window = new Float64Array(nFFT);
for (let i = 0; i < nFFT; i++) {
window[i] = 0.42 - 0.5 * Math.cos(TwoPi * i / nFFT) + 0.08 * Math.cos(FourPi * i / nFFT);
}
return window;
}
public lowPassTimeSeries(fc: number): RealArray {
let fir = new Float64Array(nTap);
let sum = 0;
for (let i = 0; i < nTap; i++) {
let x = fc * TwoPi * (i - nTap / 2);
if (x === 0) {
fir[i] = fc * TwoPi;
} else {
fir[i] = Math.sin(x) / x; // sinc(x)
}
fir[i] *= this.windowFunction[i];
sum += fir[i];
}
for (let i = 0; i < nTap; i++) {
fir[i] /= sum;
}
return fir;
}
public hearingLossFilter(yFir: RealArray, threshold: Threshold, ear: number): void {
// Implement the filter logic based on hearing thresholds
}
public smearingFilter(wFft: RealArray, b: number): void {
// Implement smearing filter logic
}
}
// Usage of the class for audio processing
const audioProcessor = new AudioProcessor();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment