|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useCallback, 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 | +function useAnimatedValue<T>(initialValue: T) { |
| 28 | + const [value, setValue] = useState(initialValue); |
| 29 | + const motionValue = useMotionValue(initialValue); |
| 30 | + const controlsRef = useRef<AnimationPlaybackControlsWithThen | null>(null); |
| 31 | + useMotionValueEvent(motionValue, 'change', (value) => setValue(value as T)); |
| 32 | + |
| 33 | + const animateFn = useCallback( |
| 34 | + (targetValue: T | T[], transition: ValueAnimationTransition) => { |
| 35 | + controlsRef.current = animate(motionValue, targetValue, transition); |
| 36 | + }, |
| 37 | + [motionValue] |
| 38 | + ); |
| 39 | + |
| 40 | + return { value, controls: controlsRef, animate: animateFn }; |
| 41 | +} |
| 42 | + |
| 43 | +export const audioShaderVisualizerVariants = cva(['aspect-square'], { |
| 44 | + variants: { |
| 45 | + size: { |
| 46 | + icon: 'h-[24px] gap-[2px]', |
| 47 | + sm: 'h-[56px] gap-[4px]', |
| 48 | + md: 'h-[112px] gap-[8px]', |
| 49 | + lg: 'h-[224px] gap-[16px]', |
| 50 | + xl: 'h-[448px] gap-[32px]', |
| 51 | + }, |
| 52 | + }, |
| 53 | + defaultVariants: { |
| 54 | + size: 'md', |
| 55 | + }, |
| 56 | +}); |
| 57 | + |
| 58 | +interface AudioOscilloscopeVisualizerProps { |
| 59 | + speed?: number; |
| 60 | + state?: AgentState; |
| 61 | + audioTrack: TrackReferenceOrPlaceholder; |
| 62 | + className?: string; |
| 63 | +} |
| 64 | + |
| 65 | +export function AudioOscilloscopeVisualizer({ |
| 66 | + size = 'lg', |
| 67 | + state = 'speaking', |
| 68 | + speed = DEFAULT_SPEED, |
| 69 | + audioTrack, |
| 70 | + className, |
| 71 | +}: AudioOscilloscopeVisualizerProps & |
| 72 | + OscilliscopeShadersProps & |
| 73 | + VariantProps<typeof audioShaderVisualizerVariants>) { |
| 74 | + const { value: amplitude, animate: animateAmplitude } = useAnimatedValue(DEFAULT_AMPLITUDE); |
| 75 | + const { value: frequency, animate: animateFrequency } = useAnimatedValue(DEFAULT_FREQUENCY); |
| 76 | + const { value: opacity, animate: animateOpacity } = useAnimatedValue(1.0); |
| 77 | + |
| 78 | + const volume = useTrackVolume(audioTrack as TrackReference, { |
| 79 | + fftSize: 512, |
| 80 | + smoothingTimeConstant: 0.55, |
| 81 | + }); |
| 82 | + |
| 83 | + useEffect(() => { |
| 84 | + switch (state) { |
| 85 | + case 'disconnected': |
| 86 | + animateAmplitude(0, DEFAULT_TRANSITION); |
| 87 | + animateFrequency(0, DEFAULT_TRANSITION); |
| 88 | + animateOpacity(1.0, DEFAULT_TRANSITION); |
| 89 | + return; |
| 90 | + case 'listening': |
| 91 | + case 'connecting': |
| 92 | + animateAmplitude(DEFAULT_AMPLITUDE, DEFAULT_TRANSITION); |
| 93 | + animateFrequency(DEFAULT_FREQUENCY, DEFAULT_TRANSITION); |
| 94 | + animateOpacity([1.0, 0.2], { |
| 95 | + duration: 0.75, |
| 96 | + repeat: Infinity, |
| 97 | + repeatType: 'mirror', |
| 98 | + }); |
| 99 | + return; |
| 100 | + case 'thinking': |
| 101 | + case 'initializing': |
| 102 | + animateAmplitude(DEFAULT_AMPLITUDE, DEFAULT_TRANSITION); |
| 103 | + animateFrequency(DEFAULT_FREQUENCY, DEFAULT_TRANSITION); |
| 104 | + animateOpacity([1.0, 0.2], { |
| 105 | + duration: 0.2, |
| 106 | + repeat: Infinity, |
| 107 | + repeatType: 'mirror', |
| 108 | + }); |
| 109 | + return; |
| 110 | + case 'speaking': |
| 111 | + default: |
| 112 | + animateAmplitude(DEFAULT_AMPLITUDE, DEFAULT_TRANSITION); |
| 113 | + animateFrequency(DEFAULT_FREQUENCY, DEFAULT_TRANSITION); |
| 114 | + animateOpacity(1.0, DEFAULT_TRANSITION); |
| 115 | + return; |
| 116 | + } |
| 117 | + }, [state, animateAmplitude, animateFrequency, animateOpacity]); |
| 118 | + |
| 119 | + useEffect(() => { |
| 120 | + if (state === 'speaking' && volume > 0) { |
| 121 | + animateAmplitude(0.02 + 0.4 * volume, { duration: 0 }); |
| 122 | + animateFrequency(20 + 60 * volume, { duration: 0 }); |
| 123 | + } |
| 124 | + }, [state, volume, animateAmplitude, animateFrequency]); |
| 125 | + |
| 126 | + return ( |
| 127 | + <OscilliscopeShaders |
| 128 | + speed={speed} |
| 129 | + amplitude={amplitude} |
| 130 | + frequency={frequency} |
| 131 | + lineWidth={0.005} |
| 132 | + smoothing={0.001} |
| 133 | + style={{ opacity }} |
| 134 | + className={cn( |
| 135 | + audioShaderVisualizerVariants({ size }), |
| 136 | + '[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%)]', |
| 137 | + 'overflow-hidden rounded-full', |
| 138 | + className |
| 139 | + )} |
| 140 | + /> |
| 141 | + ); |
| 142 | +} |
0 commit comments