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
|
import { useEffect, useRef, useState } from "react";
import WaveSurfer from "wavesurfer.js";
interface AudioWaveformProps {
audioUrl?: string;
audioBuffer?: AudioBuffer;
height?: number;
className?: string;
}
function extractPeaks({
buffer,
length = 512,
}: {
buffer: AudioBuffer;
length?: number;
}): number[][] {
const channels = buffer.numberOfChannels;
const peaks: number[][] = [];
for (let c = 0; c < channels; c++) {
const data = buffer.getChannelData(c);
const step = Math.floor(data.length / length);
const channelPeaks: number[] = [];
for (let i = 0; i < length; i++) {
const start = i * step;
const end = Math.min(start + step, data.length);
let max = 0;
for (let j = start; j < end; j++) {
const abs = Math.abs(data[j]);
if (abs > max) max = abs;
}
channelPeaks.push(max);
}
peaks.push(channelPeaks);
}
return peaks;
}
export function AudioWaveform({
audioUrl,
audioBuffer,
height = 32,
className = "",
}: AudioWaveformProps) {
const waveformRef = useRef<HTMLDivElement>(null);
const wavesurfer = useRef<WaveSurfer | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(false);
useEffect(() => {
let mounted = true;
const ws = wavesurfer.current;
const initWaveSurfer = async () => {
if (!waveformRef.current || (!audioUrl && !audioBuffer)) return;
try {
if (ws) {
wavesurfer.current = null;
}
const newWaveSurfer = WaveSurfer.create({
container: waveformRef.current,
waveColor: "rgba(255, 255, 255, 0.6)",
progressColor: "rgba(255, 255, 255, 0.9)",
cursorColor: "transparent",
barWidth: 2,
barGap: 1,
height,
normalize: true,
interact: false,
});
if (mounted) {
wavesurfer.current = newWaveSurfer;
} else {
try {
newWaveSurfer.destroy();
} catch {}
return;
}
newWaveSurfer.on("ready", () => {
if (mounted) {
setIsLoading(false);
setError(false);
}
});
newWaveSurfer.on("error", (err) => {
if (mounted) {
console.error("WaveSurfer error:", err);
setError(true);
setIsLoading(false);
}
});
if (audioBuffer) {
const peaks = extractPeaks({ buffer: audioBuffer });
newWaveSurfer.load("", peaks, audioBuffer.duration);
} else if (audioUrl) {
await newWaveSurfer.load(audioUrl);
}
} catch (err) {
if (mounted) {
console.error("Failed to initialize WaveSurfer:", err);
setError(true);
setIsLoading(false);
}
}
};
if (ws) {
const wsToDestroy = ws;
wavesurfer.current = null;
requestAnimationFrame(() => {
try {
wsToDestroy.destroy();
} catch {}
if (mounted) {
initWaveSurfer();
}
});
} else {
initWaveSurfer();
}
return () => {
mounted = false;
const wsToDestroy = wavesurfer.current;
wavesurfer.current = null;
if (wsToDestroy) {
requestAnimationFrame(() => {
try {
wsToDestroy.destroy();
} catch {}
});
}
};
}, [audioUrl, audioBuffer, height]);
if (error) {
return (
<div
className={`flex items-center justify-center ${className}`}
style={{ height }}
>
<span className="text-foreground/60 text-xs">Audio unavailable</span>
</div>
);
}
return (
<div className={`relative ${className}`}>
{isLoading && (
<div className="absolute inset-0 flex items-center justify-center">
<span className="text-foreground/60 text-xs">Loading...</span>
</div>
)}
<div
ref={waveformRef}
className={`w-full ${isLoading ? "opacity-0" : "opacity-100"}`}
style={{ height }}
/>
</div>
);
}
export default AudioWaveform;
|