-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9403552
commit e4398be
Showing
18 changed files
with
162 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useAudioVisualizer } from '@lobehub/tts'; | ||
import { useTheme } from 'antd-style'; | ||
import { RefObject, memo } from 'react'; | ||
|
||
export interface VisualizerProps { | ||
borderRadius?: number; | ||
color?: string; | ||
count?: number; | ||
gap?: number; | ||
maxHeight?: number; | ||
minHeight?: number; | ||
width?: number; | ||
} | ||
|
||
const Visualizer = memo<VisualizerProps & { audioRef: RefObject<HTMLAudioElement> }>( | ||
({ audioRef, count = 4, width = 48, color, ...barStyle }) => { | ||
const maxHeight = barStyle?.maxHeight || width * 3; | ||
const minHeight = barStyle?.minHeight || width; | ||
const borderRadius = barStyle?.borderRadius || width / 2; | ||
const theme = useTheme(); | ||
const bars = useAudioVisualizer(audioRef, { count }); | ||
|
||
return bars.map((bar, index) => ( | ||
<div | ||
key={index} | ||
style={{ | ||
background: color || theme.colorPrimary, | ||
borderRadius, | ||
height: minHeight + (bar / 255) * (maxHeight - minHeight), | ||
transition: 'height 50ms cubic-bezier(.2,-0.5,.8,1.5)', | ||
width, | ||
}} | ||
/> | ||
)); | ||
}, | ||
); | ||
|
||
export default Visualizer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,49 @@ | ||
import { useTheme } from 'antd-style'; | ||
import React, { RefObject, memo } from 'react'; | ||
import { Icon } from '@lobehub/ui'; | ||
import { Loader2 } from 'lucide-react'; | ||
import { CSSProperties, RefObject, memo } from 'react'; | ||
import { ErrorBoundary } from 'react-error-boundary'; | ||
import { Flexbox } from 'react-layout-kit'; | ||
|
||
import { useAudioVisualizer } from '@/hooks/useAudioVisualizer'; | ||
import Visualizer, { VisualizerProps } from '@/AudioVisualizer/Visualizer'; | ||
|
||
export interface AudioVisualizerProps { | ||
audioRef: RefObject<HTMLAudioElement>; | ||
barStyle?: { | ||
borderRadius?: number; | ||
count?: number; | ||
gap?: number; | ||
maxHeight?: number; | ||
minHeight?: number; | ||
width?: number; | ||
}; | ||
barStyle?: VisualizerProps; | ||
className?: string; | ||
color?: string; | ||
isLoading?: boolean; | ||
style?: CSSProperties; | ||
} | ||
|
||
const AudioVisualizer = memo<AudioVisualizerProps>(({ color, audioRef, barStyle }) => { | ||
const { count, width, gap } = { count: 4, gap: 4, width: 48, ...barStyle }; | ||
const maxHeight = barStyle?.maxHeight || width * 3; | ||
const minHeight = barStyle?.minHeight || width; | ||
const borderRadius = barStyle?.borderRadius || width / 2; | ||
const theme = useTheme(); | ||
const bars = useAudioVisualizer(audioRef, { count }); | ||
return ( | ||
<Flexbox align={'center'} gap={gap} horizontal style={{ height: maxHeight }}> | ||
{bars.map((bar, index) => ( | ||
<div | ||
key={index} | ||
style={{ | ||
background: color || theme.colorPrimary, | ||
borderRadius, | ||
height: minHeight + (bar / 255) * (maxHeight - minHeight), | ||
transition: 'height 50ms cubic-bezier(.2,-0.5,.8,1.5)', | ||
width, | ||
}} | ||
/> | ||
))} | ||
</Flexbox> | ||
); | ||
}); | ||
const AudioVisualizer = memo<AudioVisualizerProps>( | ||
({ audioRef, isLoading, barStyle, style, className }) => { | ||
const { count, width, gap } = { count: 4, gap: 4, width: 48, ...barStyle }; | ||
const maxHeight = barStyle?.maxHeight || width * 3; | ||
const containerStyle: CSSProperties = { | ||
fontSize: 24, | ||
height: maxHeight, | ||
minWidth: (width + gap) * count, | ||
...style, | ||
}; | ||
return ( | ||
<ErrorBoundary fallback={<div className={className} style={containerStyle}></div>}> | ||
<Flexbox | ||
align={'center'} | ||
className={className} | ||
gap={gap} | ||
horizontal | ||
justify={'center'} | ||
style={containerStyle} | ||
> | ||
{isLoading ? ( | ||
<Icon icon={Loader2} spin /> | ||
) : ( | ||
<Visualizer audioRef={audioRef} {...barStyle} /> | ||
)} | ||
</Flexbox> | ||
</ErrorBoundary> | ||
); | ||
}, | ||
); | ||
|
||
export default AudioVisualizer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.