This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
163 lines (152 loc) · 4.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const audioClips = [{
"url": "https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3",
"id": "Heater-1",
"keyCode": 81,
"keyTrigger": "Q",
},
{
"url": "https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3",
"id": "Heater-2",
"keyCode": 87,
"keyTrigger": "W",
},
{
"url": "https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3",
"id": "Heater-3",
"keyCode": 69,
"keyTrigger": "E",
},
{
"url": "https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3",
"id": "Heater-4",
"keyCode": 65,
"keyTrigger": "A",
},
{
"url": "https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3",
"id": "Clap",
"keyCode": 83,
"keyTrigger": "S",
},
{
"url": "https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3",
"id": "Open-HH",
"keyCode": 68,
"keyTrigger": "D",
},
{
"url": "https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3",
"id": "Kick-n'-Hat",
"keyCode": 90,
"keyTrigger": "Z",
},
{
"url": "https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3",
"id": "Kick",
"keyCode": 88,
"keyTrigger": "X",
},
{
"url": "https://s3.amazonaws.com/freecodecamp/drums/Cev_H2.mp3",
"id": "Closed-HH",
"keyCode": 67,
"keyTrigger": "C",
},
];
function Pad(props) {
const [blink, setBlink] = React.useState(false);
React.useEffect(() => {
document.addEventListener("keydown", handleKeyPress)
return () => (
document.removeEventListener("keydown", handleKeyPress)
)
})
const handleKeyPress = (e) => {
if (props.power && e.keyCode == props.padData.keyCode) {
playSound();
}
}
const playSound = () => {
if (props.power) {
const audioTag = document.getElementById(props.padData.keyTrigger);
setBlink(true);
setTimeout(() => { setBlink(false) }, 100);
audioTag.volume = props.level;
audioTag.play();
props.setInfo(props.padData.id)
}
}
return (
<div
class={`btn btn-warning p-4 m-2 ${blink && 'btn-danger'}`}
onClick={playSound}
>
{props.padData.keyTrigger}
<audio
class="clip"
id={props.padData.keyTrigger}
src={props.padData.url}
/>
</div>
)
}
function VolumeSlider(props) {
return (
<input
type="range"
class="form-range w-50"
id="slider"
min="0"
max="1"
step="0.1"
value={props.level}
onChange={(e) => {
if (props.power) {
props.setLevel(e.target.value);
props.setInfo(e.target.value);
}
}}
/>
)
}
function Display(props) {
return <h1 class="text-light">{props.info}</h1>
}
function App() {
const [power, setPower] = React.useState(false);
const [level, setLevel] = React.useState(0.5);
const [info, setInfo] = React.useState('Power off');
const togglePower = () => {
setPower(!power);
if (power == false) {
setInfo('Power on')
} else {
setLevel(0.5);
setInfo('Power off');
}
}
return (
<div class="container bg-info bg-gradient min-vh-100" id="drum-machine">
<div className="col"></div>
<h1 class="text-center text-white">Drum Machine</h1>
<div class="form-check form-switch" id="power">
<input class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckChecked" onClick={togglePower} />
<label class="form-check-label fw-bold" for="flexSwitchCheckChecked">Power</label>
</div>
<div class="container">
{audioClips.map((clip) => (
<Pad key={clip.id} padData={clip} level={level} setInfo={setInfo} power={power} />
))}
</div>
<div class="container" id="volume-controller">
<h1>Volume</h1>
<VolumeSlider level={level} setLevel={setLevel} setInfo={setInfo} power={power} />
</div>
<div class="container bg-success text-center w-50" id="display">
<Display info={info} power={power} />
</div>
</div>
)
};
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);