Tone.js/test/helper/compare/Spectrum.ts
Yotam Mann aaf880c925
Using web-test-runner for tests, updating import paths (#1242)
* WIP moving tests to web-test-runner

* updating thresholds

* Adding file extensions

* Testing integrations

* linting

* fixing dep

* moving back to root dir

* prettier all of the files

* updating eslint rules to use with prettier

* remove import package

* moving tsignore around

* removing unneeded ignores

* all tests run on puppeteer, no need for testing guards

* linting

* import type syntax

* cleaning up

* Update package.json
2024-05-03 14:31:14 -04:00

26 lines
615 B
TypeScript

import { TestAudioBuffer } from "./TestAudioBuffer.js";
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
.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));
}
});
return spectrogram;
}