-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
239 lines (221 loc) · 6.91 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
function App() {
const [powerOn, setPowerOn] = React.useState(false);
const [bankOn, setBankOn] = React.useState(false);
const [volume, setVolume] = React.useState(50);
const [currentlyDisplayedButtonMessage, setCurrentlyDisplayedButtonMessage] =
React.useState(null);
return (
<div className="app">
<Buttons
powerOn={powerOn}
bankOn={bankOn}
volume={volume}
setCurrentlyDisplayedButtonMessage={setCurrentlyDisplayedButtonMessage}
/>
<ControlPanel
powerOn={powerOn}
setPowerOn={setPowerOn}
bankOn={bankOn}
setBankOn={setBankOn}
setVolume={setVolume}
currentlyDisplayedButtonMessage={currentlyDisplayedButtonMessage}
setCurrentlyDisplayedButtonMessage={setCurrentlyDisplayedButtonMessage}
/>
</div>
);
}
function Buttons({
powerOn,
bankOn,
volume,
setCurrentlyDisplayedButtonMessage,
}) {
const buttonClass = `sound-button ${powerOn ? "" : "power-off"}`;
const sounds = {
"Ominous drums": new Audio(
"https://assets.mixkit.co/sfx/preview/mixkit-ominous-drums-227.mp3"
),
"Ominous drums appear": new Audio(
"https://assets.mixkit.co/sfx/preview/mixkit-ominous-drums-appear-228.mp3"
),
"Drum hit with eco": new Audio(
"https://assets.mixkit.co/sfx/preview/mixkit-drum-hit-with-eco-544.mp3"
),
"Mystery trailer drum": new Audio(
"https://assets.mixkit.co/sfx/preview/mixkit-cinematic-mystery-trailer-drum-hit-546.mp3"
),
"Synthetic power bass": new Audio(
"https://assets.mixkit.co/sfx/preview/mixkit-synthetic-power-bass-transition-2296.mp3"
),
"Futuristic bass hit": new Audio(
"https://assets.mixkit.co/sfx/preview/mixkit-futuristic-bass-hit-2303.mp3"
),
"Bass guitar": new Audio(
"https://assets.mixkit.co/sfx/preview/mixkit-bass-guitar-single-note-2331.mp3"
),
"Bell sound": new Audio(
"https://assets.mixkit.co/sfx/preview/mixkit-modern-classic-door-bell-sound-113.mp3"
),
"Siren tone": new Audio(
"https://assets.mixkit.co/sfx/preview/mixkit-siren-tone-1649.mp3"
),
};
const soundsBankOn = {
"Classic flute": new Audio(
"https://assets.mixkit.co/sfx/preview/mixkit-flute-alert-2307.mp3"
),
"Musical flute": new Audio(
"https://assets.mixkit.co/sfx/preview/mixkit-musical-flute-alert-2308.mp3"
),
"Indian flute": new Audio(
"https://assets.mixkit.co/sfx/preview/mixkit-possitive-indian-flute-2312.mp3"
),
"Long flute": new Audio(
"https://assets.mixkit.co/sfx/preview/mixkit-melodical-flute-music-notification-2310.mp3"
),
Trumpet: new Audio(
"https://assets.mixkit.co/sfx/preview/mixkit-trumpets-and-strings-off-beat-2286.mp3"
),
"Drum bass hit": new Audio(
"https://assets.mixkit.co/sfx/preview/mixkit-drum-bass-hit-2294.mp3"
),
Cymball: new Audio(
"https://assets.mixkit.co/sfx/preview/mixkit-relaxing-bell-chime-3109.mp3"
),
Whoosh: new Audio(
"https://assets.mixkit.co/sfx/preview/mixkit-meteor-whoosh-3024.mp3"
),
Click: new Audio(
"https://assets.mixkit.co/sfx/preview/mixkit-on-or-off-light-switch-tap-2585.mp3"
),
};
const handleClick = (e) => {
if (!powerOn) return;
const selectedBankSet = bankOn ? soundsBankOn : sounds;
const clickedButtonIndex = Number(e.target.getAttribute("value"));
const selectedAudioKey = Object.keys(selectedBankSet)[clickedButtonIndex];
const selectedAudioValue =
selectedBankSet[Object.keys(selectedBankSet)[clickedButtonIndex]];
selectedAudioValue.pause();
selectedAudioValue.currentTime = 0;
selectedAudioValue.volume = volume / 100;
selectedAudioValue.play();
setCurrentlyDisplayedButtonMessage(selectedAudioKey);
};
return (
<div className="buttons-container">
<div value="0" onClick={(e) => handleClick(e)} className={buttonClass}>
Q
</div>
<div value="1" onClick={(e) => handleClick(e)} className={buttonClass}>
W
</div>
<div value="2" onClick={(e) => handleClick(e)} className={buttonClass}>
E
</div>
<div value="3" onClick={(e) => handleClick(e)} className={buttonClass}>
A
</div>
<div value="4" onClick={(e) => handleClick(e)} className={buttonClass}>
S
</div>
<div value="5" onClick={(e) => handleClick(e)} className={buttonClass}>
D
</div>
<div value="6" onClick={(e) => handleClick(e)} className={buttonClass}>
Z
</div>
<div value="7" onClick={(e) => handleClick(e)} className={buttonClass}>
X
</div>
<div value="8" onClick={(e) => handleClick(e)} className={buttonClass}>
C
</div>
</div>
);
}
function ControlPanel({
powerOn,
setPowerOn,
bankOn,
setBankOn,
setVolume,
currentlyDisplayedButtonMessage,
setCurrentlyDisplayedButtonMessage,
}) {
const handlePowerChange = () => {
setPowerOn(!powerOn);
setCurrentlyDisplayedButtonMessage(null);
clearTimeout(localStorage.getItem("timeout"));
};
const handleBankChange = () => {
setBankOn(!bankOn);
if (!bankOn) {
setCurrentlyDisplayedButtonMessage("Flute kit");
} else {
setCurrentlyDisplayedButtonMessage("Drum kit");
}
clearTimeout(localStorage.getItem("timeout"));
};
const handleRangeChange = (e) => {
setVolume(Number(e.target.value));
setCurrentlyDisplayedButtonMessage("Volume: " + Number(e.target.value));
clearTimeout(localStorage.getItem("timeout"));
localStorage.setItem(
"timeout",
setTimeout(() => {
setCurrentlyDisplayedButtonMessage(null);
}, 1000)
);
};
const displayedMessage = () => {
if (currentlyDisplayedButtonMessage === null) return;
else {
return <p>{currentlyDisplayedButtonMessage}</p>;
}
};
return (
<div className="control-panel">
<div className="power">
<p>Power</p>
<div className="switch">
<label>
Off
<input onChange={handlePowerChange} type="checkbox" />
<span className="lever"></span>
On
</label>
</div>
</div>
<div className="display">{displayedMessage()}</div>
<form action="#">
<p className="range-field">
<input
disabled={powerOn ? false : true}
type="range"
id="test5"
min="0"
max="100"
onChange={(e) => handleRangeChange(e)}
/>
</p>
</form>
<div className="bank">
<p>Bank</p>
<div className="switch">
<label>
Off
<input
disabled={powerOn ? false : true}
type="checkbox"
onChange={handleBankChange}
/>
<span className="lever"></span>
On
</label>
</div>
</div>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("root"));