Bin
2025-12-17 262fecaa75b2909ad244f12c3b079ed3ff4ae329
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
import type React from "react";
import { useCallback, useContext } from "react";
import { TimelineContext } from "../../../components/Timeline/Context";
import type { Waveform } from "../Waveform";
import type { WindowFunctionType } from "../Visual/WindowFunctions";
import type { ColorScheme } from "../Visual/ColorMapper";
import type { SpectrogramScale } from "../Analysis/FFTProcessor";
import { SPECTROGRAM_DEFAULTS } from "../Visual/constants";
import type { MutableRefObject } from "react";
 
interface SpectrogramConfigProps {
  waveform: MutableRefObject<Waveform | undefined>;
}
 
export const SpectrogramConfig: React.FC<SpectrogramConfigProps> = ({ waveform }) => {
  const { changeSetting } = useContext(TimelineContext);
 
  const setFftSamples = useCallback(
    (samples: number) => {
      if (!waveform.current) return;
 
      if (samples < SPECTROGRAM_DEFAULTS.FFT_SAMPLES / 8 || samples > SPECTROGRAM_DEFAULTS.FFT_SAMPLES * 4) {
        console.warn(`Invalid FFT samples value: ${samples}`);
        return;
      }
 
      waveform.current.updateSpectrogramConfig({ fftSamples: samples });
      changeSetting?.("spectrogramFftSamples", samples);
    },
    [waveform, changeSetting],
  );
 
  const setMelBands = useCallback(
    (bands: number) => {
      if (!waveform.current) return;
 
      if (bands < 1 || bands > 512) {
        console.warn(`Invalid mel bands value: ${bands}`);
        return;
      }
 
      waveform.current.updateSpectrogramConfig({ melBands: bands });
      changeSetting?.("numberOfMelBands", bands);
    },
    [waveform, changeSetting],
  );
 
  const setWindowingFunction = useCallback(
    (func: WindowFunctionType) => {
      if (!waveform.current) return;
 
      waveform.current.updateSpectrogramConfig({ windowingFunction: func });
      changeSetting?.("spectrogramWindowingFunction", func);
    },
    [waveform, changeSetting],
  );
 
  const setColorScheme = useCallback(
    (scheme: ColorScheme) => {
      if (!waveform.current) return;
 
      waveform.current.updateSpectrogramConfig({ colorScheme: scheme });
      changeSetting?.("spectrogramColorScheme", scheme);
    },
    [waveform, changeSetting],
  );
 
  const setDbRange = useCallback(
    (minDb: number, maxDb: number) => {
      if (!waveform.current) return;
 
      if (minDb >= maxDb) {
        console.warn(`Invalid dB range: min (${minDb}) must be less than max (${maxDb})`);
        return;
      }
 
      waveform.current.updateSpectrogramConfig({ minDb, maxDb });
      changeSetting?.("spectrogramMinDb", minDb);
      changeSetting?.("spectrogramMaxDb", maxDb);
    },
    [waveform, changeSetting],
  );
 
  const setScale = useCallback(
    (scale: SpectrogramScale) => {
      if (!waveform.current) return;
 
      if (!["linear", "log", "mel"].includes(scale)) {
        console.warn(`Invalid spectrogram scale: ${scale}`);
        return;
      }
 
      waveform.current.updateSpectrogramConfig({ scale });
      changeSetting?.("spectrogramScale", scale);
    },
    [waveform, changeSetting],
  );
 
  return (
    <div className="spectrogram-config">
      <h3>Spectrogram Settings</h3>
      <div className="control-group">
        <label>
          Scale
          <select onChange={(e) => setScale(e.target.value as SpectrogramScale)}>
            <option value="linear">Linear</option>
            <option value="log">Logarithmic</option>
            <option value="mel">Mel</option>
          </select>
        </label>
 
        <label>
          Mel Bands
          <input type="number" min="1" max="512" onChange={(e) => setMelBands(Number.parseInt(e.target.value))} />
        </label>
 
        <label>
          FFT Size
          <select onChange={(e) => setFftSamples(Number.parseInt(e.target.value))}>
            <option value="256">256</option>
            <option value="512">512</option>
            <option value="1024">1024</option>
            <option value="2048">2048</option>
            <option value="4096">4096</option>
          </select>
        </label>
 
        <label>
          Window Function
          <select onChange={(e) => setWindowingFunction(e.target.value as WindowFunctionType)}>
            <option value="hann">Hann</option>
            <option value="hamming">Hamming</option>
            <option value="blackman">Blackman</option>
            <option value="rectangular">Rectangular</option>
          </select>
        </label>
 
        <label>
          Color Scheme
          <select onChange={(e) => setColorScheme(e.target.value as ColorScheme)}>
            <option value="inferno">Inferno</option>
            <option value="magma">Magma</option>
            <option value="viridis">Viridis</option>
            <option value="plasma">Plasma</option>
          </select>
        </label>
 
        <div className="db-range">
          <label>
            Min dB
            <input
              type="number"
              step="1"
              onChange={(e) => {
                const minDb = Number.parseInt(e.target.value);
                const maxDbInput =
                  e.currentTarget.parentElement?.parentElement?.querySelector<HTMLInputElement>('input[name="maxDb"]');
                if (maxDbInput) {
                  setDbRange(minDb, Number.parseInt(maxDbInput.value));
                }
              }}
            />
          </label>
 
          <label>
            Max dB
            <input
              type="number"
              step="1"
              name="maxDb"
              onChange={(e) => {
                const maxDb = Number.parseInt(e.target.value);
                const minDbInput =
                  e.currentTarget.parentElement?.parentElement?.querySelector<HTMLInputElement>(
                    'input:not([name="maxDb"])',
                  );
                if (minDbInput) {
                  setDbRange(Number.parseInt(minDbInput.value), maxDb);
                }
              }}
            />
          </label>
        </div>
      </div>
    </div>
  );
};