Skip to content

Commit 064314d

Browse files
committed
feat: implementing sounds on click + fixed popping sounds from the playSound
1 parent 68c8772 commit 064314d

File tree

4 files changed

+57
-30
lines changed

4 files changed

+57
-30
lines changed
Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,43 @@
11
import React from "react";
22
import { buttonSoundConfig } from "../constants.ts";
3-
import playSound from "../util/playSound.ts";
3+
import { playSound } from "../util/playSound.ts";
44

55
import { Box } from "./Box.tsx";
66

77
let audioContext: AudioContext | null = null;
88

99
export function App() {
10+
const handleOnClick = (freq: number) =>
11+
playSound(
12+
(audioContext ??= new AudioContext()),
13+
freq,
14+
buttonSoundConfig.duration,
15+
buttonSoundConfig.volume,
16+
);
17+
1018
return (
11-
<div>
12-
{buttonSoundConfig.frequencies.map((freq, index) => (
13-
<button
14-
key={index}
15-
onClick={() =>
16-
playSound(
17-
(audioContext ??= new AudioContext()),
18-
freq,
19-
buttonSoundConfig.duration,
20-
buttonSoundConfig.volume,
21-
)
22-
}
23-
>
24-
{freq}
25-
</button>
26-
))}
27-
<Box color="red" />
19+
<div style={{display: "flex", gap: 10}}>
20+
<Box
21+
color="red"
22+
handleClick={() => handleOnClick(buttonSoundConfig.frequencies.red)}
23+
/>
2824
<Box
2925
color={
3026
// cobalt blue
3127
"#0050B5"
3228
}
29+
handleClick={() =>
30+
handleOnClick(buttonSoundConfig.frequencies.cobaltBlue)
31+
}
32+
/>
33+
<Box
34+
color="green"
35+
handleClick={() => handleOnClick(buttonSoundConfig.frequencies.green)}
36+
/>
37+
<Box
38+
color="yellow"
39+
handleClick={() => handleOnClick(buttonSoundConfig.frequencies.yellow)}
3340
/>
34-
<Box color="green" />
35-
<Box color="yellow" />
3641
</div>
3742
);
3843
}

workspaces/simon-game/src/app/components/Box.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,28 @@ type Props = {
55
height?: number;
66
width?: number;
77
margin?: number;
8+
handleClick: () => void;
89
};
910

10-
export function Box(props: Props) {
11+
export function Box({
12+
color = "lightblue",
13+
height = 100,
14+
width = 100,
15+
margin,
16+
handleClick,
17+
}: Props) {
1118
return (
12-
<div
19+
<button
1320
style={{
14-
backgroundColor: props.color ?? "lightblue",
15-
height: props.height ?? 100,
16-
width: props.width ?? 100,
21+
backgroundColor: color,
22+
height: height,
23+
width: width,
1724
borderRadius: 20, // rounded corners
18-
margin: props.margin, // spaces between boxes
25+
margin: margin, // spaces between boxes
26+
cursor: "pointer",
27+
border: 0,
1928
}}
29+
onClick={handleClick}
2030
/>
2131
);
2232
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
export const buttonSoundConfig = {
22
duration: 300,
33
volume: 0.1,
4-
frequencies: [261.63, 329.63, 392.0, 523.25],
4+
frequencies: {
5+
red: 261.63,
6+
cobaltBlue: 329.63,
7+
green: 392.0,
8+
yellow: 523.25,
9+
},
510
};

workspaces/simon-game/src/app/util/playSound.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
* @param duration - The duration of the sound in milliseconds.
77
* @param volume - The volume of the sound, typically between 0 and 1.
88
*/
9-
export default function playSound(
9+
export const playSound = (
1010
context: AudioContext,
1111
frequency: number,
1212
duration: number,
1313
volume: number,
14-
) {
14+
) => {
1515
if (frequency <= 0) {
1616
throw new Error("Frequency must be a positive number.");
1717
}
@@ -30,9 +30,16 @@ export default function playSound(
3030
const gainNode = context.createGain();
3131
gainNode.gain.setValueAtTime(volume, context.currentTime);
3232

33+
// Creates the smooth fades-in / fade-out sound effect (Avoids the popping sounds)
34+
gainNode.gain.linearRampToValueAtTime(volume, context.currentTime + 0.01);
35+
gainNode.gain.linearRampToValueAtTime(
36+
0,
37+
context.currentTime + duration / 1000 - 0.01,
38+
);
39+
3340
oscillatorNode.connect(gainNode);
3441
gainNode.connect(context.destination);
3542

3643
oscillatorNode.start();
3744
oscillatorNode.stop(context.currentTime + duration / 1000);
38-
}
45+
};

0 commit comments

Comments
 (0)