@@ -8,6 +8,22 @@ import {
8
8
useState ,
9
9
} from "react" ;
10
10
11
+ function playGeigerSoundFile (
12
+ audioContext : AudioContext ,
13
+ src : string ,
14
+ amplitude : number
15
+ ) {
16
+ const audioElement = new Audio ( src ) ;
17
+ const audioSource = audioContext . createMediaElementSource ( audioElement ) ;
18
+ audioSource . connect ( audioContext . destination ) ;
19
+ audioElement . volume = amplitude ;
20
+ // Limit audio to 1s
21
+ audioElement . play ( ) ;
22
+ setTimeout ( ( ) => {
23
+ audioElement . pause ( ) ;
24
+ audioElement . currentTime = 0 ;
25
+ } , 1000 ) ;
26
+ }
11
27
function playGeigerClickSound ( audioContext : AudioContext , amplitude : number ) {
12
28
const volume = Math . max ( 0.5 , amplitude ) ;
13
29
const duration = 0.001 ;
@@ -56,13 +72,15 @@ const Geiger: FC<{
56
72
renderTimeThreshold ?: number ;
57
73
phaseOption ?: PhaseOption ;
58
74
enabled ?: boolean ;
75
+ customSound ?: string ;
59
76
children : ReactNode ;
60
77
} > = ( {
61
78
profilerId = "geiger" ,
62
79
renderTimeThreshold = 50 ,
63
80
phaseOption = "both" ,
64
81
enabled = true ,
65
82
children,
83
+ customSound,
66
84
} ) => {
67
85
const [ audioContext , setAudioContext ] = useState < AudioContext | null > ( null ) ;
68
86
@@ -77,7 +95,13 @@ const Geiger: FC<{
77
95
1 ,
78
96
( actualDuration - renderTimeThreshold ) / ( renderTimeThreshold * 2 )
79
97
) ;
80
- playGeigerClickSound ( audioContext , amplitude ) ;
98
+
99
+ if ( customSound && typeof customSound == "string" ) {
100
+ if ( customSound === "" ) {
101
+ console . warn ( "The sound file path is empty" ) ;
102
+ }
103
+ playGeigerSoundFile ( audioContext , customSound , amplitude ) ;
104
+ } else playGeigerClickSound ( audioContext , amplitude ) ;
81
105
}
82
106
} ,
83
107
[ audioContext , phaseOption , renderTimeThreshold ]
0 commit comments