|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect, useMemo, useRef, useState } from 'react'; |
| 4 | +import { type VariantProps, cva } from 'class-variance-authority'; |
| 5 | +import { |
| 6 | + type AnimationPlaybackControlsWithThen, |
| 7 | + type ValueAnimationTransition, |
| 8 | + animate, |
| 9 | + useMotionValue, |
| 10 | + useMotionValueEvent, |
| 11 | +} from 'motion/react'; |
| 12 | +import { |
| 13 | + type AgentState, |
| 14 | + type TrackReference, |
| 15 | + type TrackReferenceOrPlaceholder, |
| 16 | + // useMultibandTrackVolume, |
| 17 | + useTrackVolume, |
| 18 | +} from '@livekit/components-react'; |
| 19 | +import { cn } from '@/lib/utils'; |
| 20 | +import { OscilliscopeShaders, type OscilliscopeShadersProps } from './oscilliscope-shaders'; |
| 21 | + |
| 22 | +const DEFAULT_SPEED = 5; |
| 23 | +const DEFAULT_AMPLITUDE = 0.025; |
| 24 | +const DEFAULT_FREQUENCY = 10; |
| 25 | +const DEFAULT_TRANSITION: ValueAnimationTransition = { duration: 0.2, ease: 'easeOut' }; |
| 26 | + |
| 27 | +export const audioShaderVisualizerVariants = cva(['aspect-square'], { |
| 28 | + variants: { |
| 29 | + size: { |
| 30 | + icon: 'h-[24px] gap-[2px]', |
| 31 | + sm: 'h-[56px] gap-[4px]', |
| 32 | + md: 'h-[112px] gap-[8px]', |
| 33 | + lg: 'h-[224px] gap-[16px]', |
| 34 | + xl: 'h-[448px] gap-[32px]', |
| 35 | + }, |
| 36 | + }, |
| 37 | + defaultVariants: { |
| 38 | + size: 'md', |
| 39 | + }, |
| 40 | +}); |
| 41 | + |
| 42 | +interface AudioOscilloscopeVisualizerProps { |
| 43 | + state?: AgentState; |
| 44 | + audioTrack: TrackReferenceOrPlaceholder; |
| 45 | + className?: string; |
| 46 | +} |
| 47 | + |
| 48 | +export function AudioOscilloscopeVisualizer({ |
| 49 | + size = 'lg', |
| 50 | + state = 'speaking', |
| 51 | + audioTrack, |
| 52 | + className, |
| 53 | +}: AudioOscilloscopeVisualizerProps & |
| 54 | + OscilliscopeShadersProps & |
| 55 | + VariantProps<typeof audioShaderVisualizerVariants>) { |
| 56 | + const [speed, setSpeed] = useState(DEFAULT_SPEED); |
| 57 | + const [amplitude, setAmplitude] = useState(DEFAULT_AMPLITUDE); |
| 58 | + const [frequency, setFrequency] = useState(DEFAULT_FREQUENCY); |
| 59 | + |
| 60 | + const amplitudeValue = useMotionValue(DEFAULT_AMPLITUDE); |
| 61 | + const frequencyValue = useMotionValue(DEFAULT_FREQUENCY); |
| 62 | + |
| 63 | + const amplitudeControlsRef = useRef<AnimationPlaybackControlsWithThen | null>(null); |
| 64 | + const frequencyControlsRef = useRef<AnimationPlaybackControlsWithThen | null>(null); |
| 65 | + |
| 66 | + useMotionValueEvent(amplitudeValue, 'change', (value) => setAmplitude(value)); |
| 67 | + useMotionValueEvent(frequencyValue, 'change', (value) => setFrequency(value)); |
| 68 | + |
| 69 | + const volume = useTrackVolume(audioTrack as TrackReference, { |
| 70 | + fftSize: 512, |
| 71 | + smoothingTimeConstant: 0.55, |
| 72 | + }); |
| 73 | + |
| 74 | + useEffect(() => { |
| 75 | + switch (state) { |
| 76 | + case 'connecting': |
| 77 | + setSpeed(DEFAULT_SPEED); |
| 78 | + amplitudeControlsRef.current = animate(amplitudeValue, 0.1, DEFAULT_TRANSITION); |
| 79 | + frequencyControlsRef.current = animate(frequencyValue, [50, 60], { |
| 80 | + duration: 0.33, |
| 81 | + repeat: Infinity, |
| 82 | + repeatType: 'mirror', |
| 83 | + }); |
| 84 | + return; |
| 85 | + case 'initializing': |
| 86 | + setSpeed(DEFAULT_SPEED); |
| 87 | + amplitudeControlsRef.current = animate(amplitudeValue, 0.05, DEFAULT_TRANSITION); |
| 88 | + frequencyControlsRef.current = animate(frequencyValue, [40, 30], { |
| 89 | + duration: 0.66, |
| 90 | + repeat: Infinity, |
| 91 | + repeatType: 'mirror', |
| 92 | + }); |
| 93 | + return; |
| 94 | + case 'listening': |
| 95 | + setSpeed(DEFAULT_SPEED); |
| 96 | + amplitudeControlsRef.current = animate(amplitudeValue, 0.025, DEFAULT_TRANSITION); |
| 97 | + frequencyControlsRef.current = animate(frequencyValue, [30, 10], { |
| 98 | + duration: 1.5, |
| 99 | + repeat: Infinity, |
| 100 | + repeatType: 'mirror', |
| 101 | + }); |
| 102 | + return; |
| 103 | + case 'thinking': |
| 104 | + setSpeed(DEFAULT_SPEED); |
| 105 | + amplitudeControlsRef.current = animate(amplitudeValue, 0.025, DEFAULT_TRANSITION); |
| 106 | + frequencyControlsRef.current = animate(frequencyValue, [30, 50], { |
| 107 | + duration: 0.5, |
| 108 | + repeat: Infinity, |
| 109 | + repeatType: 'mirror', |
| 110 | + }); |
| 111 | + return; |
| 112 | + default: |
| 113 | + setSpeed(DEFAULT_SPEED); |
| 114 | + amplitudeControlsRef.current = animate(amplitudeValue, 0.025, DEFAULT_TRANSITION); |
| 115 | + frequencyControlsRef.current = animate(frequencyValue, 10, DEFAULT_TRANSITION); |
| 116 | + return; |
| 117 | + } |
| 118 | + }, [state, amplitudeValue, frequencyValue]); |
| 119 | + |
| 120 | + useEffect(() => { |
| 121 | + if (state === 'speaking' && volume > 0) { |
| 122 | + amplitudeControlsRef.current?.stop(); |
| 123 | + frequencyControlsRef.current?.stop(); |
| 124 | + |
| 125 | + amplitudeValue.set(0.02 + 0.4 * volume); |
| 126 | + frequencyValue.set(20 + 60 * volume); |
| 127 | + } |
| 128 | + }, [state, volume, amplitudeValue, frequencyValue]); |
| 129 | + |
| 130 | + return ( |
| 131 | + <OscilliscopeShaders |
| 132 | + speed={speed} |
| 133 | + amplitude={amplitude} |
| 134 | + frequency={frequency} |
| 135 | + lineWidth={0.005} |
| 136 | + smoothing={0} |
| 137 | + className={cn( |
| 138 | + audioShaderVisualizerVariants({ size }), |
| 139 | + '[mask-image:linear-gradient(90deg,rgba(0,0,0,0)_0%,rgba(0,0,0,1)_20%,rgba(0,0,0,1)_80%,rgba(0,0,0,0)_100%)]', |
| 140 | + 'overflow-hidden rounded-full', |
| 141 | + className |
| 142 | + )} |
| 143 | + /> |
| 144 | + ); |
| 145 | +} |
0 commit comments