Skip to content

Commit 707c315

Browse files
committed
chore: applying PR feedback
1 parent 6997466 commit 707c315

File tree

5 files changed

+57
-62
lines changed

5 files changed

+57
-62
lines changed
Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,15 @@
11
import React from "react";
2-
import { buttonSoundConfig } from "../constants.ts";
3-
import { playSound } from "../util/playSound.ts";
42

53
import { Box } from "./Box.tsx";
6-
7-
let audioContext: AudioContext | null = null;
4+
import { playNote } from "../util/playNote.ts";
5+
import { config } from "../constants.ts";
86

97
export function App() {
10-
const handleOnClick = (freq: number) =>
11-
playSound(
12-
(audioContext ??= new AudioContext()),
13-
freq,
14-
buttonSoundConfig.duration,
15-
buttonSoundConfig.volume,
16-
);
17-
188
return (
199
<div style={{ display: "flex", gap: 10 }}>
20-
<Box
21-
color="red"
22-
handleClick={() => handleOnClick(buttonSoundConfig.frequencies.red)}
23-
/>
24-
<Box
25-
color={
26-
// cobalt blue
27-
"#0050B5"
28-
}
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)}
40-
/>
10+
{config.boxes.map((box) => (
11+
<Box color={box.color} onClick={() => playNote(box.frequency)} />
12+
))}
4113
</div>
4214
);
4315
}

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

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

1111
export function Box({
1212
color = "lightblue",
1313
height = 100,
1414
width = 100,
1515
margin,
16-
handleClick,
16+
onClick,
1717
}: Props) {
1818
return (
1919
<button
2020
style={{
2121
backgroundColor: color,
22-
height: height,
23-
width: width,
22+
height,
23+
width,
2424
borderRadius: 20, // rounded corners
25-
margin: margin, // spaces between boxes
25+
margin, // spaces between boxes
2626
cursor: "pointer",
2727
border: 0,
2828
}}
29-
onClick={handleClick}
29+
onClick={onClick}
3030
/>
3131
);
3232
}
Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
export const buttonSoundConfig = {
2-
duration: 300,
3-
volume: 0.1,
4-
frequencies: {
5-
red: 261.63,
6-
cobaltBlue: 329.63,
7-
green: 392.0,
8-
yellow: 523.25,
9-
},
1+
export const config = {
2+
soundDurationMs: 300,
3+
volumePct: 0.1,
4+
buttons: [
5+
{
6+
color: "red",
7+
frequency: 261.63,
8+
},
9+
{
10+
color: "#0050B5", // cobalt blue
11+
frequency: 329.63,
12+
},
13+
{
14+
color: "green",
15+
frequency: 392.0,
16+
},
17+
{
18+
color: "yellow",
19+
frequency: 523.25,
20+
},
21+
],
1022
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { config } from "../constants.ts";
2+
import { playSound } from "./playSound.ts";
3+
4+
export const playNote = (freq: number) =>
5+
playSound(freq, config.soundDurationMs, config.volumePct);
Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,51 @@
1+
let audioContext: AudioContext | null = null;
2+
13
/**
24
* Plays a sound using the Web Audio API.
35
*
46
* @param context - The audio context to use for generating the sound.
57
* @param frequency - The frequency of the sound in Hertz
6-
* @param duration - The duration of the sound in milliseconds.
8+
* @param durationMs - The duration of the sound in milliseconds.
79
* @param volume - The volume of the sound, typically between 0 and 1.
810
*/
911
export const playSound = (
10-
context: AudioContext,
1112
frequency: number,
12-
duration: number,
13-
volume: number,
13+
durationMs: number,
14+
volumePct: number,
1415
) => {
1516
if (frequency <= 0) {
1617
throw new Error("Frequency must be a positive number.");
1718
}
1819

19-
if (duration <= 0) {
20+
if (durationMs <= 0) {
2021
throw new Error("Duration must be a positive number.");
2122
}
2223

23-
if (volume < 0 || volume > 1) {
24+
if (volumePct < 0 || volumePct > 1) {
2425
throw new Error("Volume must be between 0 and 1.");
2526
}
2627

27-
const oscillatorNode = context.createOscillator();
28-
oscillatorNode.frequency.setValueAtTime(frequency, context.currentTime);
28+
audioContext ??= new AudioContext();
29+
30+
const oscillatorNode = audioContext.createOscillator();
31+
oscillatorNode.frequency.setValueAtTime(frequency, audioContext.currentTime);
2932

30-
const gainNode = context.createGain();
31-
gainNode.gain.setValueAtTime(volume, context.currentTime);
33+
const gainNode = audioContext.createGain();
34+
gainNode.gain.setValueAtTime(volumePct, audioContext.currentTime);
3235

3336
// Creates the smooth fades-in / fade-out sound effect (Avoids the popping sounds)
34-
gainNode.gain.linearRampToValueAtTime(volume, context.currentTime + 0.01);
37+
gainNode.gain.linearRampToValueAtTime(
38+
volumePct,
39+
audioContext.currentTime + 0.01,
40+
);
3541
gainNode.gain.linearRampToValueAtTime(
3642
0,
37-
context.currentTime + duration / 1000 - 0.01,
43+
audioContext.currentTime + durationMs / 1000 - 0.01,
3844
);
3945

4046
oscillatorNode.connect(gainNode);
41-
gainNode.connect(context.destination);
47+
gainNode.connect(audioContext.destination);
4248

4349
oscillatorNode.start();
44-
oscillatorNode.stop(context.currentTime + duration / 1000);
50+
oscillatorNode.stop(audioContext.currentTime + durationMs / 1000);
4551
};

0 commit comments

Comments
 (0)