Skip to content

Commit f99b46d

Browse files
author
portableCoder
committedMar 12, 2024
add customSound option
1 parent 013f536 commit f99b46d

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed
 

‎package-lock.json

Lines changed: 2 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Geiger.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ import {
88
useState,
99
} from "react";
1010

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+
}
1127
function playGeigerClickSound(audioContext: AudioContext, amplitude: number) {
1228
const volume = Math.max(0.5, amplitude);
1329
const duration = 0.001;
@@ -56,13 +72,15 @@ const Geiger: FC<{
5672
renderTimeThreshold?: number;
5773
phaseOption?: PhaseOption;
5874
enabled?: boolean;
75+
customSound?: string;
5976
children: ReactNode;
6077
}> = ({
6178
profilerId = "geiger",
6279
renderTimeThreshold = 50,
6380
phaseOption = "both",
6481
enabled = true,
6582
children,
83+
customSound,
6684
}) => {
6785
const [audioContext, setAudioContext] = useState<AudioContext | null>(null);
6886

@@ -77,7 +95,13 @@ const Geiger: FC<{
7795
1,
7896
(actualDuration - renderTimeThreshold) / (renderTimeThreshold * 2)
7997
);
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);
81105
}
82106
},
83107
[audioContext, phaseOption, renderTimeThreshold]

0 commit comments

Comments
 (0)
Please sign in to comment.