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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import type { Visualizer } from "../Visual/Visualizer";
 
export const __DEBUG__ = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test";
export const OFFSCREEN_CANVAS_SUPPORTED = "OffscreenCanvas" in globalThis;
 
const TIME_TOLERANCE = 0.000001;
 
export enum defaults {
  timelineHeight = 32,
  timelinePlacement = "top",
}
 
type LogLevel = "log" | "warn" | "error" | "info";
 
export const logger =
  (level: LogLevel = "log") =>
  (...args: any[]) => {
    if (__DEBUG__) {
      // eslint-disable-next-line no-console
      console[level](...args);
    }
  };
 
export const log = logger("log");
export const warn = logger("warn");
export const error = logger("error");
export const info = logger("info");
 
export const clamp = (value: number, min: number, max: number) => {
  return Math.max(min, Math.min(max, value));
};
 
export const toPrecision = (value: number, precision = 2) => {
  const multiplier = 10 ** precision;
 
  return Math.round(value * multiplier) / multiplier;
};
 
export const filterData = (audioBuffer: AudioBuffer | null, channel?: number) => {
  if (!audioBuffer) return new Float32Array(0);
 
  return audioBuffer.getChannelData(channel ?? 0);
};
 
export const isInRange = (value: number, min: number, max: number) => {
  return value >= min && value <= max;
};
 
export const findLast = <T = any>(array: T[], predicate: (item: T) => boolean): T | undefined => {
  for (let i = array.length - 1; i >= 0; i--) {
    if (predicate(array[i])) {
      return array[i];
    }
  }
};
 
export const debounce = (
  fn: (...args: any[]) => any,
  timeout: number,
  { leading = false }: { leading?: boolean } = {},
) => {
  let timer: number | undefined;
 
  return ((...args: any[]) => {
    if (timer) {
      clearTimeout(timer);
    }
 
    if (leading) {
      fn(...args);
    }
 
    timer = setTimeout(() => fn(...args), timeout) as any;
  }) as typeof fn;
};
 
export const repeat = (str: string, times: number) =>
  Array.from({ length: times })
    .map(() => str)
    .join("");
 
export const roundToStep = (value: number, step: number, roundFunction: "floor" | "ceil" | "round" = "round") => {
  switch (roundFunction) {
    case "floor":
      return Math.floor(value / step) * step;
    case "ceil":
      return Math.ceil(value / step) * step;
    case "round":
      return Math.round(value / step) * step;
  }
};
 
export const minmax = (array: ArrayLike<number>) => {
  const arraySize = array.length;
 
  if (arraySize > 0) {
    let max;
    let min;
    let i = 0;
 
    max = min = array[0];
 
    while (i < arraySize) {
      const value = array[i];
 
      if (value > max) max = value;
      else if (value < min) min = value;
 
      i++;
    }
 
    return [min, max];
  }
  return [Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY];
};
 
export const averageMinMax = (data: Float32Array) => {
  const [min, max] = minmax(data);
 
  return [clamp(min, -1, 1), clamp(max, -1, 1)];
};
 
export const average = (array: ArrayLike<number>) => {
  const arraySize = array.length;
 
  if (arraySize > 0) {
    let sum = 0;
 
    for (let i = 0; i < arraySize; i++) {
      sum += array[i];
    }
 
    return sum / arraySize;
  }
  return 0;
};
 
export const measure = (message: string, callback: () => void) => {
  let start = 0;
 
  if (__DEBUG__) {
    start = performance.now();
  }
 
  callback();
 
  if (__DEBUG__) {
    info(`[MEASURE]: ${message} took ${performance.now() - start}ms`);
  }
};
 
export const chunk6 = <T>(array: ArrayLike<T>, size: number) => {
  const chunked_arr = [];
 
  for (let i = 0; i < array.length; i++) {
    const last = chunked_arr[chunked_arr.length - 1];
 
    if (!last || last.length === size) {
      chunked_arr.push([array[i]]);
    } else {
      last.push(array[i]);
    }
  }
 
  return chunked_arr;
};
 
export const bufferAllocator = () => {
  const buffers = new Map<number, Float32Array>();
 
  const allocate = (size: number) => {
    if (buffers.has(size)) return buffers.get(size)!;
 
    const buffer = new Float32Array(size);
 
    buffers.set(size, buffer);
 
    return buffer;
  };
 
  return { allocate };
};
 
export const getOffsetLeft = (element: HTMLElement) => {
  return element.getBoundingClientRect().left;
};
 
export const getOffsetTop = (element: HTMLElement) => {
  return element.getBoundingClientRect().top;
};
 
export const getCursorPositionX = (e: MouseEvent, offsetElement: HTMLElement) => {
  return e.clientX - getOffsetLeft(offsetElement);
};
 
export const getCursorPositionY = (e: MouseEvent, offsetElement: HTMLElement) => {
  return e.clientY - getOffsetTop(offsetElement);
};
 
export const pixelsToTime = (pixels: number, zoomedWidth: number, duration: number) => {
  return (pixels / zoomedWidth) * duration;
};
 
export const getCursorTime = (e: MouseEvent, visualizer: Visualizer, duration: number) => {
  const { zoomedWidth, container } = visualizer;
  const cursorPosition = getCursorPositionX(e, container) + visualizer.getScrollLeftPx();
  const time = pixelsToTime(cursorPosition, zoomedWidth, duration);
 
  return time;
};
 
export const isTimeSimilar = (a: number, b: number) => Math.abs(a - b) < TIME_TOLERANCE;
export const isTimeRelativelySimilar = (a: number, b: number, observedDuration: number) =>
  isTimeSimilar(a / observedDuration, b / observedDuration);
 
/**
 * A constant representing the thickness of the scrollbar's handle in pixels.
 * This value is calculated dynamically by creating a temporary DOM element
 * with a scrollable area and comparing its offset width to its client width.
 * Useful for making precise layout adjustments that depend on the width of the scrollbar.
 *
 * Note: The calculation is performed immediately when the variable is defined
 * and retains its value for the duration of runtime.
 *
 * @constant {number}
 */
export const BROWSER_SCROLLBAR_WIDTH = ((): number => {
  const scrollDiv = document.createElement("div");
  scrollDiv.style.width = "100px";
  scrollDiv.style.height = "100px";
  scrollDiv.style.overflow = "scroll";
  scrollDiv.style.position = "absolute";
  scrollDiv.style.top = "-9999px";
  document.body.appendChild(scrollDiv);
  const scrollSize = scrollDiv.offsetWidth - scrollDiv.clientWidth;
  document.body.removeChild(scrollDiv);
  return scrollSize;
})();