2024-05-03 14:16:02 +00:00
|
|
|
import { TestAudioBuffer } from "./TestAudioBuffer.js";
|
2024-05-01 19:55:52 +00:00
|
|
|
import windowing from "fft-windowing";
|
|
|
|
import ft from "fourier-transform";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a spectrogram of the buffer
|
|
|
|
*/
|
|
|
|
export function analyze(
|
|
|
|
buffer: TestAudioBuffer,
|
|
|
|
fftSize = 256,
|
|
|
|
hopSize = 128
|
|
|
|
) {
|
|
|
|
const spectrogram: number[][] = [];
|
|
|
|
buffer
|
2024-05-03 14:16:02 +00:00
|
|
|
.toMono()
|
|
|
|
.toArray()
|
|
|
|
.forEach((channel) => {
|
|
|
|
for (
|
|
|
|
let index = 0;
|
|
|
|
index < channel.length - fftSize;
|
|
|
|
index += hopSize
|
|
|
|
) {
|
|
|
|
const segment = windowing.blackman_harris(
|
|
|
|
channel.slice(index, index + fftSize)
|
|
|
|
);
|
|
|
|
spectrogram.push(ft(segment));
|
|
|
|
}
|
|
|
|
});
|
2024-05-01 19:55:52 +00:00
|
|
|
return spectrogram;
|
|
|
|
}
|