mirror of
https://github.com/Tonejs/Tone.js
synced 2025-01-15 13:23:58 +00:00
30 lines
634 B
TypeScript
30 lines
634 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;
|
|
}
|