|
| 1 | +import { useSpeechRecognitionEvent } from "expo-speech-recognition"; |
| 2 | +import { Image, StyleSheet, View } from "react-native"; |
| 3 | +import Animated, { |
| 4 | + Extrapolation, |
| 5 | + interpolate, |
| 6 | + useAnimatedStyle, |
| 7 | + useSharedValue, |
| 8 | + withTiming, |
| 9 | + Easing, |
| 10 | + withSpring, |
| 11 | + withDelay, |
| 12 | + withSequence, |
| 13 | +} from "react-native-reanimated"; |
| 14 | +const avatar = require("../assets/avatar.png"); |
| 15 | + |
| 16 | +const minScale = 1; |
| 17 | +const maxScale = 1.5; |
| 18 | + |
| 19 | +/** |
| 20 | + * This is an example component that uses the `volumechange` event to animate the volume metering of a user's voice. |
| 21 | + */ |
| 22 | +export function VolumeMeteringAvatar() { |
| 23 | + const haloScale = useSharedValue(minScale); |
| 24 | + const pulseScale = useSharedValue(minScale); |
| 25 | + const pulseOpacity = useSharedValue(0); |
| 26 | + |
| 27 | + const haloAnimatedStyle = useAnimatedStyle(() => ({ |
| 28 | + transform: [{ scale: haloScale.value }], |
| 29 | + })); |
| 30 | + |
| 31 | + const pulseAnimatedStyle = useAnimatedStyle(() => ({ |
| 32 | + opacity: pulseOpacity.value, |
| 33 | + transform: [{ scale: pulseScale.value }], |
| 34 | + })); |
| 35 | + |
| 36 | + const reset = () => { |
| 37 | + haloScale.value = minScale; |
| 38 | + pulseScale.value = minScale; |
| 39 | + pulseOpacity.value = 0; |
| 40 | + }; |
| 41 | + |
| 42 | + useSpeechRecognitionEvent("start", reset); |
| 43 | + useSpeechRecognitionEvent("end", reset); |
| 44 | + |
| 45 | + useSpeechRecognitionEvent("volumechange", (event) => { |
| 46 | + // Don't animate anything if the volume is too low |
| 47 | + if (event.rmsdB <= 1) { |
| 48 | + return; |
| 49 | + } |
| 50 | + const newScale = interpolate( |
| 51 | + event.rmsdB, |
| 52 | + [-2, 10], // The rmsDB range is between -2 and 10 |
| 53 | + [minScale, maxScale], |
| 54 | + Extrapolation.CLAMP, |
| 55 | + ); |
| 56 | + |
| 57 | + // Animate the halo scaling |
| 58 | + haloScale.value = withSequence( |
| 59 | + withSpring(newScale, { |
| 60 | + damping: 15, |
| 61 | + stiffness: 150, |
| 62 | + }), |
| 63 | + withTiming(minScale, { |
| 64 | + duration: 1000, |
| 65 | + easing: Easing.linear, |
| 66 | + }), |
| 67 | + ); |
| 68 | + |
| 69 | + // Animate the pulse (scale and fade out) |
| 70 | + if (pulseScale.value < newScale) { |
| 71 | + pulseScale.value = withSequence( |
| 72 | + withTiming(maxScale, { |
| 73 | + duration: 1000, |
| 74 | + easing: Easing.out(Easing.quad), |
| 75 | + }), |
| 76 | + withTiming(minScale, { |
| 77 | + duration: 400, |
| 78 | + easing: Easing.linear, |
| 79 | + }), |
| 80 | + ); |
| 81 | + pulseOpacity.value = withSequence( |
| 82 | + withTiming(1, { duration: 800 }), |
| 83 | + withDelay(300, withTiming(0, { duration: 200 })), |
| 84 | + ); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + return ( |
| 89 | + <View style={styles.container}> |
| 90 | + <View style={styles.pulseContainer}> |
| 91 | + <Animated.View style={[styles.pulse, pulseAnimatedStyle]} /> |
| 92 | + </View> |
| 93 | + <View style={styles.pulseContainer}> |
| 94 | + <Animated.View style={[styles.halo, haloAnimatedStyle]} /> |
| 95 | + </View> |
| 96 | + <View style={[styles.centered]}> |
| 97 | + <Image source={avatar} style={styles.avatar} /> |
| 98 | + </View> |
| 99 | + </View> |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +const styles = StyleSheet.create({ |
| 104 | + container: { |
| 105 | + position: "relative", |
| 106 | + marginVertical: 20, |
| 107 | + }, |
| 108 | + pulseContainer: { |
| 109 | + position: "absolute", |
| 110 | + top: 0, |
| 111 | + bottom: 0, |
| 112 | + left: 0, |
| 113 | + right: 0, |
| 114 | + justifyContent: "center", |
| 115 | + alignItems: "center", |
| 116 | + }, |
| 117 | + pulse: { |
| 118 | + borderWidth: 1, |
| 119 | + borderColor: "#539bf5", |
| 120 | + width: 96, |
| 121 | + height: 96, |
| 122 | + borderRadius: 96, |
| 123 | + }, |
| 124 | + halo: { |
| 125 | + backgroundColor: "#6b7280", |
| 126 | + width: 96, |
| 127 | + height: 96, |
| 128 | + borderRadius: 96, |
| 129 | + }, |
| 130 | + centered: { |
| 131 | + justifyContent: "center", |
| 132 | + alignItems: "center", |
| 133 | + }, |
| 134 | + avatar: { |
| 135 | + width: 96, |
| 136 | + height: 96, |
| 137 | + borderRadius: 96, |
| 138 | + overflow: "hidden", |
| 139 | + }, |
| 140 | +}); |
0 commit comments