voice-selector.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Derived from vercel/ai-elements (skills/ai-elements/scripts/voice-selector.tsx).
  3. * Copyright 2023 Vercel, Inc. Licensed under Apache-2.0.
  4. * Modified by ZCode: local integration, formatting and adaptations.
  5. * See THIRD-PARTY-NOTICES.md in the repository root for license and provenance.
  6. */
  7. "use client";
  8. import {
  9. VoiceSelector,
  10. VoiceSelectorAccent,
  11. VoiceSelectorAge,
  12. VoiceSelectorBullet,
  13. VoiceSelectorContent,
  14. VoiceSelectorDescription,
  15. VoiceSelectorEmpty,
  16. VoiceSelectorGender,
  17. VoiceSelectorInput,
  18. VoiceSelectorItem,
  19. VoiceSelectorList,
  20. VoiceSelectorName,
  21. VoiceSelectorPreview,
  22. VoiceSelectorTrigger,
  23. } from "@/components/ai-elements/voice-selector";
  24. import { Button } from "@/components/ui/button";
  25. import type { ComponentProps } from "react";
  26. import { memo, useCallback, useRef, useState } from "react";
  27. const voices: {
  28. id: string;
  29. name: string;
  30. description: string;
  31. gender: ComponentProps<typeof VoiceSelectorGender>["value"];
  32. accent: ComponentProps<typeof VoiceSelectorAccent>["value"];
  33. age: string;
  34. previewUrl: string;
  35. }[] = [
  36. {
  37. accent: "american",
  38. age: "20-30",
  39. description: "Energetic, Social Media Creator",
  40. gender: "male",
  41. id: "liam",
  42. name: "Liam",
  43. previewUrl:
  44. "https://ejiidnob33g9ap1r.public.blob.vercel-storage.com/ElevenLabs_2026-01-16T21_16_50_Liam%20-%20Energetic%2C%20Social%20Media%20Creator_pre_sp100_s50_sb75_se0_b_m2.mp3",
  45. },
  46. {
  47. accent: "american",
  48. age: "30-40",
  49. description: "Dominant, Firm",
  50. gender: "male",
  51. id: "adam",
  52. name: "Adam",
  53. previewUrl:
  54. "https://ejiidnob33g9ap1r.public.blob.vercel-storage.com/ElevenLabs_2026-01-16T21_17_00_Adam%20-%20Dominant%2C%20Firm_pre_sp100_s50_sb75_se0_b_m2.mp3",
  55. },
  56. {
  57. accent: "british",
  58. age: "30-40",
  59. description: "Clear, Engaging Educator",
  60. gender: "female",
  61. id: "alice",
  62. name: "Alice",
  63. previewUrl:
  64. "https://ejiidnob33g9ap1r.public.blob.vercel-storage.com/ElevenLabs_2026-01-16T21_17_09_Alice%20-%20Clear%2C%20Engaging%20Educator_pre_sp100_s50_sb75_se0_b_m2.mp3",
  65. },
  66. {
  67. accent: "american",
  68. age: "50-60",
  69. description: "Wise, Mature, Balanced",
  70. gender: "male",
  71. id: "bill",
  72. name: "Bill",
  73. previewUrl:
  74. "https://ejiidnob33g9ap1r.public.blob.vercel-storage.com/ElevenLabs_2026-01-16T21_17_25_Bill%20-%20Wise%2C%20Mature%2C%20Balanced_pre_sp100_s50_sb75_se0_b_m2.mp3",
  75. },
  76. {
  77. accent: "american",
  78. age: "20-30",
  79. description: "Playful, Bright, Warm",
  80. gender: "female",
  81. id: "jessica",
  82. name: "Jessica",
  83. previewUrl:
  84. "https://ejiidnob33g9ap1r.public.blob.vercel-storage.com/ElevenLabs_2026-01-16T21_17_50_Jessica%20-%20Playful%2C%20Bright%2C%20Warm_pre_sp100_s50_sb75_se0_b_m2.mp3",
  85. },
  86. {
  87. accent: "british",
  88. age: "30-40",
  89. description: "Velvety Actress",
  90. gender: "female",
  91. id: "lily",
  92. name: "Lily",
  93. previewUrl:
  94. "https://ejiidnob33g9ap1r.public.blob.vercel-storage.com/ElevenLabs_2026-01-16T21_18_03_Lily%20-%20Velvety%20Actress_pre_sp100_s50_sb75_se0_b_m2.mp3",
  95. },
  96. ];
  97. interface VoiceItemProps {
  98. voice: (typeof voices)[0];
  99. playingVoice: string | null;
  100. loadingVoice: string | null;
  101. onSelect: (id: string) => void;
  102. onPreview: (id: string) => void;
  103. }
  104. const VoiceItem = memo(
  105. ({ voice, playingVoice, loadingVoice, onSelect, onPreview }: VoiceItemProps) => {
  106. const handleSelect = useCallback(() => onSelect(voice.id), [onSelect, voice.id]);
  107. const handlePreview = useCallback(() => onPreview(voice.id), [onPreview, voice.id]);
  108. return (
  109. <VoiceSelectorItem key={voice.id} onSelect={handleSelect} value={voice.id}>
  110. <VoiceSelectorPreview
  111. loading={loadingVoice === voice.id}
  112. onPlay={handlePreview}
  113. playing={playingVoice === voice.id}
  114. />
  115. <VoiceSelectorName>{voice.name}</VoiceSelectorName>
  116. <VoiceSelectorDescription>{voice.description}</VoiceSelectorDescription>
  117. <VoiceSelectorBullet />
  118. <VoiceSelectorAccent value={voice.accent} />
  119. <VoiceSelectorBullet />
  120. <VoiceSelectorAge>{voice.age}</VoiceSelectorAge>
  121. <VoiceSelectorBullet />
  122. <VoiceSelectorGender value={voice.gender} />
  123. </VoiceSelectorItem>
  124. );
  125. },
  126. );
  127. VoiceItem.displayName = "VoiceItem";
  128. const Example = () => {
  129. const [open, setOpen] = useState(false);
  130. const [selectedVoice, setSelectedVoice] = useState<string | null>(null);
  131. const [playingVoice, setPlayingVoice] = useState<string | null>(null);
  132. const [loadingVoice, setLoadingVoice] = useState<string | null>(null);
  133. const audioRef = useRef<HTMLAudioElement | null>(null);
  134. const handleSelect = useCallback((voiceId: string) => {
  135. setSelectedVoice(voiceId);
  136. setOpen(false);
  137. }, []);
  138. const handlePreview = useCallback(
  139. (voiceId: string) => {
  140. const voice = voices.find((v) => v.id === voiceId);
  141. if (!voice) {
  142. return;
  143. }
  144. // If clicking the same voice that's playing, pause it
  145. if (playingVoice === voiceId) {
  146. audioRef.current?.pause();
  147. setPlayingVoice(null);
  148. return;
  149. }
  150. // Stop any currently playing audio
  151. if (audioRef.current) {
  152. audioRef.current.pause();
  153. audioRef.current = null;
  154. }
  155. setLoadingVoice(voiceId);
  156. const audio = new Audio(voice.previewUrl);
  157. audioRef.current = audio;
  158. audio.addEventListener("canplaythrough", () => {
  159. setLoadingVoice(null);
  160. setPlayingVoice(voiceId);
  161. audio.play();
  162. });
  163. audio.addEventListener("ended", () => {
  164. setPlayingVoice(null);
  165. });
  166. audio.addEventListener("error", () => {
  167. setLoadingVoice(null);
  168. setPlayingVoice(null);
  169. });
  170. audio.load();
  171. },
  172. [playingVoice],
  173. );
  174. const selectedVoiceData = voices.find((voice) => voice.id === selectedVoice);
  175. return (
  176. <div className="flex size-full flex-col items-center justify-center">
  177. <VoiceSelector onOpenChange={setOpen} open={open}>
  178. <VoiceSelectorTrigger asChild>
  179. <Button className="w-full max-w-xs" variant="outline">
  180. {selectedVoiceData ? (
  181. <>
  182. <VoiceSelectorName>{selectedVoiceData.name}</VoiceSelectorName>
  183. <VoiceSelectorAccent value={selectedVoiceData.accent} />
  184. <VoiceSelectorBullet />
  185. <VoiceSelectorAge>{selectedVoiceData.age}</VoiceSelectorAge>
  186. <VoiceSelectorBullet />
  187. <VoiceSelectorGender value={selectedVoiceData.gender} />
  188. </>
  189. ) : (
  190. <span className="flex-1 text-left text-sm">Select a voice...</span>
  191. )}
  192. </Button>
  193. </VoiceSelectorTrigger>
  194. <VoiceSelectorContent className="max-w-md">
  195. <VoiceSelectorInput placeholder="Search voices..." />
  196. <VoiceSelectorList>
  197. <VoiceSelectorEmpty>No voices found.</VoiceSelectorEmpty>
  198. {voices.map((voice) => (
  199. <VoiceItem
  200. key={voice.id}
  201. loadingVoice={loadingVoice}
  202. onPreview={handlePreview}
  203. onSelect={handleSelect}
  204. playingVoice={playingVoice}
  205. voice={voice}
  206. />
  207. ))}
  208. </VoiceSelectorList>
  209. </VoiceSelectorContent>
  210. </VoiceSelector>
  211. </div>
  212. );
  213. };
  214. export default Example;