-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSpectrum.tsx
327 lines (300 loc) · 10.7 KB
/
Spectrum.tsx
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/** @jsxImportSource @emotion/react */
import { css } from "@emotion/react";
import { ColorPaletteProp, LinearProgress, Stack, Typography } from "@mui/joy";
import { AnimationControls, motion } from "framer-motion";
import { ComponentProps, useEffect, useRef, useState } from "react";
import { useDebounceValue, useInterval, useResizeObserver } from "usehooks-ts";
import { maxArray } from "../utils";
import AmplitudeIndicators from "./AmplitudeIndicators";
import { PiMicrophoneSlash } from "react-icons/pi";
export type SpectrumVariantProp = "soft" | "outlined";
export type SpectrumProps = {
/** The width of the bars */
step?: number;
/** The gap between the bars */
gap?: number;
/** Whether to interpolate the color of the bars depending on the value */
interpolate?: boolean;
/** Whether to display ghost bars */
ghost?: boolean;
/** How long the ghost bars should last, set to null to never clear. */
ghostDuration?: number | null;
/** The minimum frequency to display */
minFrequency?: number;
/** The maximum frequency to display */
maxFrequency?: number;
/** The variant of the bars. Cf. MUI Joy */
variant?: SpectrumVariantProp;
/** The color of the bars. Cf. MUI Joy */
color?: ColorPaletteProp;
/** Callback when the audio input is ready */
onLoad?: () => void;
/** Callback when an error occurs */
onError?: () => void;
} & ComponentProps<typeof motion.div>;
export default function Spectrum({
step = 8,
gap = 4,
interpolate = false,
ghost = false,
ghostDuration = null,
minFrequency = 0,
maxFrequency = 200,
variant = "soft",
color = "primary",
onLoad,
onError,
...props
}: SpectrumProps) {
const [loading, setLoading] = useState(true);
const [error, setError] = useState(false);
const [bars, setBars] = useState<number[]>([]);
const [ghostBars, setGhostBars] = useState<number[]>([]);
const ref = useRef<HTMLDivElement>(null);
const { width: observedWidth, height: observedHeight } = useResizeObserver({
ref,
});
// Debounce the width, height and bar count to avoid unnecessary re-renders on resize
const [width, setWidth] = useDebounceValue(0, 200);
const [height, setHeight] = useDebounceValue(0, 200);
const [barCount, setBarCount] = useDebounceValue(0, 200);
// Clear ghost bars after the specified duration
useInterval(() => {
if (ghost) setGhostBars(Array.from({ length: barCount }, () => 0));
}, ghostDuration);
// Reset the ghost bars when the ghost prop becomes false
useEffect(() => {
if (!ghost) {
setGhostBars([]);
}
}, [ghost]);
// Compute the width and height of each bar
useEffect(() => {
setWidth(observedWidth || 400);
setHeight(observedHeight || 80);
}, [observedWidth, observedHeight]);
// Compute the amount of bars to display
useEffect(() => {
const count = Math.floor((width - gap) / (step + gap)) + 1;
setBarCount(count);
}, [width, gap, step]);
// Update the bars based on the audio input
useEffect(() => {
let audioContext: AudioContext;
let animationFrameId: number;
let lifeCycleCompleted = false;
navigator.mediaDevices
.getUserMedia({ audio: true })
.then((stream) => {
audioContext = new window.AudioContext();
const analyser = audioContext.createAnalyser();
analyser.fftSize = 2048;
const bufferLength = analyser.frequencyBinCount;
const dataArray = new Uint8Array(bufferLength);
if (lifeCycleCompleted) {
return;
}
// Connect the audio input to the analyser
const source = audioContext.createMediaStreamSource(stream);
source.connect(analyser);
/**
* Update the bars based on the audio input. This function is called recursively using
* requestAnimationFrame.
*/
const updateBars = () => {
if (lifeCycleCompleted) {
return;
}
animationFrameId = requestAnimationFrame(updateBars);
analyser.getByteFrequencyData(dataArray);
const newBars = Array.from({ length: barCount }, (_, index) => {
const start = Math.floor(
minFrequency + (index / barCount) * (maxFrequency - minFrequency)
);
const end = Math.floor(
minFrequency +
((index + 1) / barCount) * (maxFrequency - minFrequency)
);
return (
dataArray
.slice(start, end)
.reduce((acc, value) => acc + value, 0) /
(end - start)
);
});
// Update the bars
setBars(newBars);
if (ghost)
setGhostBars((prevGhostBars) =>
prevGhostBars.length === newBars.length
? maxArray(prevGhostBars, newBars)
: newBars
);
};
// Start the animation loop
updateBars();
})
.catch(() => {
setError(true);
onError?.();
})
.finally(() => {
// Stop loading when the audio input is ready
setLoading(false);
onLoad?.();
});
return () => {
// Clean up the audio context and the animation frame
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
}
audioContext?.close();
lifeCycleCompleted = true;
};
}, [barCount, minFrequency, maxFrequency, ghost]);
/** Since the bar width is used in both the loading and the loaded state, we compute it here */
const barWidth = (width - (barCount - 1) * gap) / barCount;
return (
<motion.div
ref={ref}
aria-label="Audio spectrum"
css={css`
position: relative;
display: flex;
border-radius: 1.375rem;
width: min(27rem, 100%);
height: min(7rem, 100%);
overflow: hidden;
`}
{...props}
>
{loading || error ? (
<motion.div
layoutId="content"
key={loading ? "loading" : "error"}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
css={css`
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
`}
>
{error ? (
<Stack direction="row" gap={2} alignItems="center">
<PiMicrophoneSlash size="1.25rem" style={{ flexShrink: 0 }} />
<Typography level="body-sm" textColor="text.secondary">
It seems like we couldn't access your microphone.
</Typography>
</Stack>
) : (
<LinearProgress
color={color}
variant="soft"
thickness={barWidth}
sx={{
width: "100%",
}}
/>
)}
</motion.div>
) : (
<>
{[ghostBars, bars].map((amplitudes, index) => {
const spectrumType = index === 0 ? "ghost" : "bars";
const computedGap =
spectrumType === "bars" && variant === "outlined" && ghost
? gap + 2
: gap;
return (
<AmplitudeIndicators
layoutId="content"
key={spectrumType}
amplitudes={amplitudes}
amplitudeProps={(amplitude) => {
const barHeight = height * (amplitude / 255);
const outlined = variant === "outlined";
let backgroundColor: string;
let borderColor: string;
if (spectrumType === "ghost") {
backgroundColor = `color-mix(in srgb, transparent 40%, var(--joy-palette-${color}-${variant}Bg))`;
borderColor = `color-mix(in srgb, transparent 40%, var(--joy-palette-${color}-outlinedBorder))`;
} else {
const amplitudePercentage = 100 * (barHeight / height);
backgroundColor = `color-mix(in srgb, var(--joy-palette-${color}-${variant}Color) ${
interpolate
? amplitudePercentage
: amplitude > 0
? 100
: 0
}%, var(--joy-palette-${color}-${variant}Bg, transparent))`;
borderColor = `color-mix(in srgb, var(--joy-palette-${color}-outlinedBorder) ${
interpolate
? amplitudePercentage
: amplitude > 0
? 100
: 0
}%, var(--joy-palette-${color}-${variant}Bg, transparent))`;
}
const computedBarWidth =
spectrumType === "bars" && variant === "outlined" && ghost
? barWidth - 2
: barWidth;
const computedBarHeight =
spectrumType === "bars" && variant === "outlined" && ghost
? barHeight - 2
: barHeight;
return {
animate: (spectrumType === "ghost"
? {
height: `${computedBarHeight}px`,
}
: {}) as AnimationControls,
style: {
...(interpolate ? { backgroundColor, borderColor } : {}),
...(spectrumType === "bars"
? {
height: `${computedBarHeight}px`,
}
: {}),
width: `${computedBarWidth}px`,
minHeight: `${computedBarWidth}px`,
maxHeight: `${height}px`,
},
css: css`
${interpolate
? ""
: `background-color: ${backgroundColor}; border-color: ${borderColor};`}
border-width: ${outlined ? "1px" : "0"};
border-style: solid;
border-radius: 0.4rem;
transition: ${interpolate
? "none"
: "background-color 0.2s ease"};
`,
};
}}
gap={computedGap}
style={
spectrumType === "ghost"
? {
position: "absolute",
top: 0,
left: 0,
zIndex: 0,
}
: {
zIndex: 1,
}
}
/>
);
})}
</>
)}
</motion.div>
);
}