Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
 * Available window function types for audio processing
 */
export type WindowFunctionType = "hann" | "hamming" | "blackman" | "rectangular";
 
/**
 * Applies a window function to a buffer of audio samples
 * @param buffer - The audio samples to apply the window function to
 * @param windowType - The type of window function to apply
 */
export function applyWindowFunction(buffer: Float32Array, windowType: WindowFunctionType = "hann"): void {
  const n = buffer.length;
 
  switch (windowType.toLowerCase()) {
    case "hann":
      for (let i = 0; i < n; i++) {
        buffer[i] *= 0.5 * (1 - Math.cos((2 * Math.PI * i) / (n - 1)));
      }
      break;
    case "hamming":
      for (let i = 0; i < n; i++) {
        buffer[i] *= 0.54 - 0.46 * Math.cos((2 * Math.PI * i) / (n - 1));
      }
      break;
    case "blackman":
      for (let i = 0; i < n; i++) {
        buffer[i] *= 0.42 - 0.5 * Math.cos((2 * Math.PI * i) / (n - 1)) + 0.08 * Math.cos((4 * Math.PI * i) / (n - 1));
      }
      break;
    case "rectangular":
      // No window function (rectangular)
      break;
  }
}