|
| 1 | +<script setup lang="ts"> |
| 2 | +import { onBeforeUnmount, ref, useAttrs, watch } from "vue"; |
| 3 | +import { AudioRecorderError } from "./audioRecorder"; |
| 4 | +import type { AudioRecorderState } from "./audioRecorder"; |
| 5 | +
|
| 6 | +defineOptions({ inheritAttrs: false }); |
| 7 | +
|
| 8 | +const attrs = useAttrs(); |
| 9 | +const canvasRef = ref<HTMLCanvasElement | null>(null); |
| 10 | +const recorderState = ref<AudioRecorderState>("idle"); |
| 11 | +const mediaRecorderRef = ref<MediaRecorder | null>(null); |
| 12 | +const streamRef = ref<MediaStream | null>(null); |
| 13 | +const analyserRef = ref<AnalyserNode | null>(null); |
| 14 | +const audioContextRef = ref<AudioContext | null>(null); |
| 15 | +const animationIdRef = ref<number | null>(null); |
| 16 | +const audioChunksRef = ref<Blob[]>([]); |
| 17 | +const amplitudeHistoryRef = ref<number[]>([]); |
| 18 | +const scrollOffsetRef = ref(0); |
| 19 | +const smoothedAmplitudeRef = ref(0); |
| 20 | +const fadeOpacityRef = ref(0); |
| 21 | +
|
| 22 | +function cleanup() { |
| 23 | + if (animationIdRef.value !== null) { |
| 24 | + cancelAnimationFrame(animationIdRef.value); |
| 25 | + animationIdRef.value = null; |
| 26 | + } |
| 27 | +
|
| 28 | + const recorder = mediaRecorderRef.value; |
| 29 | + if (recorder && recorder.state !== "inactive") { |
| 30 | + try { |
| 31 | + recorder.stop(); |
| 32 | + } catch { |
| 33 | + // ignore cleanup stop failures |
| 34 | + } |
| 35 | + } |
| 36 | +
|
| 37 | + if (streamRef.value) { |
| 38 | + streamRef.value.getTracks().forEach((track) => track.stop()); |
| 39 | + streamRef.value = null; |
| 40 | + } |
| 41 | +
|
| 42 | + const audioContext = audioContextRef.value; |
| 43 | + if (audioContext && audioContext.state !== "closed") { |
| 44 | + audioContext.close().catch(() => { |
| 45 | + // ignore close errors |
| 46 | + }); |
| 47 | + } |
| 48 | +
|
| 49 | + mediaRecorderRef.value = null; |
| 50 | + analyserRef.value = null; |
| 51 | + audioContextRef.value = null; |
| 52 | + audioChunksRef.value = []; |
| 53 | + amplitudeHistoryRef.value = []; |
| 54 | + scrollOffsetRef.value = 0; |
| 55 | + smoothedAmplitudeRef.value = 0; |
| 56 | + fadeOpacityRef.value = 0; |
| 57 | +} |
| 58 | +
|
| 59 | +async function start() { |
| 60 | + if (recorderState.value !== "idle") { |
| 61 | + throw new AudioRecorderError("Recorder is already active"); |
| 62 | + } |
| 63 | +
|
| 64 | + try { |
| 65 | + const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); |
| 66 | + streamRef.value = stream; |
| 67 | +
|
| 68 | + const audioContext = new AudioContext(); |
| 69 | + audioContextRef.value = audioContext; |
| 70 | + const source = audioContext.createMediaStreamSource(stream); |
| 71 | + const analyser = audioContext.createAnalyser(); |
| 72 | + analyser.fftSize = 2048; |
| 73 | + source.connect(analyser); |
| 74 | + analyserRef.value = analyser; |
| 75 | +
|
| 76 | + const mimeType = MediaRecorder.isTypeSupported("audio/webm;codecs=opus") |
| 77 | + ? "audio/webm;codecs=opus" |
| 78 | + : MediaRecorder.isTypeSupported("audio/webm") |
| 79 | + ? "audio/webm" |
| 80 | + : MediaRecorder.isTypeSupported("audio/mp4") |
| 81 | + ? "audio/mp4" |
| 82 | + : ""; |
| 83 | +
|
| 84 | + const options = mimeType ? { mimeType } : {}; |
| 85 | + const recorder = new MediaRecorder(stream, options); |
| 86 | + mediaRecorderRef.value = recorder; |
| 87 | + audioChunksRef.value = []; |
| 88 | +
|
| 89 | + recorder.ondataavailable = (event) => { |
| 90 | + if (event.data.size > 0) { |
| 91 | + audioChunksRef.value.push(event.data); |
| 92 | + } |
| 93 | + }; |
| 94 | +
|
| 95 | + recorder.start(100); |
| 96 | + recorderState.value = "recording"; |
| 97 | + } catch (error) { |
| 98 | + cleanup(); |
| 99 | + if (error instanceof Error && error.name === "NotAllowedError") { |
| 100 | + throw new AudioRecorderError("Microphone permission denied"); |
| 101 | + } |
| 102 | + if (error instanceof Error && error.name === "NotFoundError") { |
| 103 | + throw new AudioRecorderError("No microphone found"); |
| 104 | + } |
| 105 | + throw new AudioRecorderError( |
| 106 | + error instanceof Error ? error.message : "Failed to start recording", |
| 107 | + ); |
| 108 | + } |
| 109 | +} |
| 110 | +
|
| 111 | +function stop() { |
| 112 | + return new Promise<Blob>((resolve, reject) => { |
| 113 | + const recorder = mediaRecorderRef.value; |
| 114 | + if (!recorder || recorderState.value !== "recording") { |
| 115 | + reject(new AudioRecorderError("No active recording")); |
| 116 | + return; |
| 117 | + } |
| 118 | +
|
| 119 | + recorderState.value = "processing"; |
| 120 | + recorder.onstop = () => { |
| 121 | + const audioBlob = new Blob(audioChunksRef.value, { |
| 122 | + type: recorder.mimeType || "audio/webm", |
| 123 | + }); |
| 124 | + cleanup(); |
| 125 | + recorderState.value = "idle"; |
| 126 | + resolve(audioBlob); |
| 127 | + }; |
| 128 | + recorder.onerror = () => { |
| 129 | + cleanup(); |
| 130 | + recorderState.value = "idle"; |
| 131 | + reject(new AudioRecorderError("Recording failed")); |
| 132 | + }; |
| 133 | + recorder.stop(); |
| 134 | + }); |
| 135 | +} |
| 136 | +
|
| 137 | +function calculateAmplitude(dataArray: Uint8Array) { |
| 138 | + let sum = 0; |
| 139 | + for (let index = 0; index < dataArray.length; index += 1) { |
| 140 | + const sample = (dataArray[index] ?? 128) / 128 - 1; |
| 141 | + sum += sample * sample; |
| 142 | + } |
| 143 | + return Math.sqrt(sum / dataArray.length); |
| 144 | +} |
| 145 | +
|
| 146 | +function drawFrame() { |
| 147 | + const canvas = canvasRef.value; |
| 148 | + if (!canvas) { |
| 149 | + return; |
| 150 | + } |
| 151 | +
|
| 152 | + const context = canvas.getContext("2d"); |
| 153 | + if (!context) { |
| 154 | + return; |
| 155 | + } |
| 156 | +
|
| 157 | + const rect = canvas.getBoundingClientRect(); |
| 158 | + const dpr = window.devicePixelRatio || 1; |
| 159 | + if ( |
| 160 | + canvas.width !== rect.width * dpr || |
| 161 | + canvas.height !== rect.height * dpr |
| 162 | + ) { |
| 163 | + canvas.width = rect.width * dpr; |
| 164 | + canvas.height = rect.height * dpr; |
| 165 | + context.scale(dpr, dpr); |
| 166 | + } |
| 167 | +
|
| 168 | + const barWidth = 2; |
| 169 | + const barGap = 1; |
| 170 | + const barSpacing = barWidth + barGap; |
| 171 | + const maxBars = Math.floor(rect.width / barSpacing) + 2; |
| 172 | +
|
| 173 | + if (analyserRef.value && recorderState.value === "recording") { |
| 174 | + if (amplitudeHistoryRef.value.length === 0) { |
| 175 | + amplitudeHistoryRef.value = Array.from({ length: maxBars }, () => 0); |
| 176 | + } |
| 177 | +
|
| 178 | + if (fadeOpacityRef.value < 1) { |
| 179 | + fadeOpacityRef.value = Math.min(1, fadeOpacityRef.value + 0.03); |
| 180 | + } |
| 181 | +
|
| 182 | + scrollOffsetRef.value += 1 / 3; |
| 183 | + const bufferLength = analyserRef.value.fftSize; |
| 184 | + const dataArray = new Uint8Array(bufferLength); |
| 185 | + analyserRef.value.getByteTimeDomainData(dataArray); |
| 186 | + const rawAmplitude = calculateAmplitude(dataArray); |
| 187 | + const attackSpeed = 0.12; |
| 188 | + const decaySpeed = 0.08; |
| 189 | + const speed = |
| 190 | + rawAmplitude > smoothedAmplitudeRef.value ? attackSpeed : decaySpeed; |
| 191 | + smoothedAmplitudeRef.value += |
| 192 | + (rawAmplitude - smoothedAmplitudeRef.value) * speed; |
| 193 | +
|
| 194 | + if (scrollOffsetRef.value >= barSpacing) { |
| 195 | + scrollOffsetRef.value -= barSpacing; |
| 196 | + amplitudeHistoryRef.value.push(smoothedAmplitudeRef.value); |
| 197 | + if (amplitudeHistoryRef.value.length > maxBars) { |
| 198 | + amplitudeHistoryRef.value = amplitudeHistoryRef.value.slice(-maxBars); |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | +
|
| 203 | + context.clearRect(0, 0, rect.width, rect.height); |
| 204 | + const computedStyle = window.getComputedStyle(canvas); |
| 205 | + context.fillStyle = computedStyle.color; |
| 206 | + context.globalAlpha = fadeOpacityRef.value; |
| 207 | +
|
| 208 | + const centerY = rect.height / 2; |
| 209 | + const maxAmplitude = rect.height / 2 - 2; |
| 210 | + const edgeFadeWidth = 12; |
| 211 | +
|
| 212 | + for (let index = 0; index < amplitudeHistoryRef.value.length; index += 1) { |
| 213 | + const amplitude = amplitudeHistoryRef.value[index] ?? 0; |
| 214 | + const scaledAmplitude = Math.min(amplitude * 4, 1); |
| 215 | + const barHeight = Math.max(2, scaledAmplitude * maxAmplitude * 2); |
| 216 | + const x = |
| 217 | + rect.width - |
| 218 | + (amplitudeHistoryRef.value.length - index) * barSpacing - |
| 219 | + scrollOffsetRef.value; |
| 220 | + const y = centerY - barHeight / 2; |
| 221 | + if (x + barWidth <= 0 || x >= rect.width) { |
| 222 | + continue; |
| 223 | + } |
| 224 | +
|
| 225 | + let edgeOpacity = 1; |
| 226 | + if (x < edgeFadeWidth) { |
| 227 | + edgeOpacity = Math.max(0, x / edgeFadeWidth); |
| 228 | + } else if (x > rect.width - edgeFadeWidth) { |
| 229 | + edgeOpacity = Math.max(0, (rect.width - x) / edgeFadeWidth); |
| 230 | + } |
| 231 | +
|
| 232 | + context.globalAlpha = fadeOpacityRef.value * edgeOpacity; |
| 233 | + context.fillRect(x, y, barWidth, barHeight); |
| 234 | + } |
| 235 | +
|
| 236 | + animationIdRef.value = requestAnimationFrame(drawFrame); |
| 237 | +} |
| 238 | +
|
| 239 | +watch( |
| 240 | + recorderState, |
| 241 | + () => { |
| 242 | + if (animationIdRef.value !== null) { |
| 243 | + cancelAnimationFrame(animationIdRef.value); |
| 244 | + animationIdRef.value = null; |
| 245 | + } |
| 246 | + animationIdRef.value = requestAnimationFrame(drawFrame); |
| 247 | + }, |
| 248 | + { immediate: true }, |
| 249 | +); |
| 250 | +
|
| 251 | +onBeforeUnmount(() => { |
| 252 | + cleanup(); |
| 253 | +}); |
| 254 | +
|
| 255 | +defineExpose({ |
| 256 | + get state() { |
| 257 | + return recorderState.value; |
| 258 | + }, |
| 259 | + start, |
| 260 | + stop, |
| 261 | + dispose: cleanup, |
| 262 | +}); |
| 263 | +</script> |
| 264 | + |
| 265 | +<template> |
| 266 | + <div data-copilotkit class="cpk:w-full cpk:px-5 cpk:py-3" v-bind="attrs"> |
| 267 | + <canvas ref="canvasRef" class="cpk:block cpk:h-[26px] cpk:w-full" /> |
| 268 | + </div> |
| 269 | +</template> |
0 commit comments