Skip to content

Commit dedd05e

Browse files
committed
v
1 parent 4a9e08e commit dedd05e

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

editor/index.js

+55-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ function strip(s) {
3535
}
3636

3737
let g_context;
38+
let g_gainNode;
3839
let g_byteBeat;
3940
let g_filter;
41+
let g_localSettings;
4042
const g_analyzers = [];
4143
let g_splitter;
4244
let g_merger;
@@ -90,9 +92,11 @@ function reconnect() {
9092
const lastNode = connectFor2Channels();
9193
if (g_filter) {
9294
lastNode.connect(g_filter);
93-
g_filter.connect(g_context.destination);
95+
g_filter.connect(g_gainNode);
96+
g_gainNode.connect(g_context.destination);
9497
} else {
95-
lastNode.connect(g_context.destination);
98+
lastNode.connect(g_gainNode);
99+
g_gainNode.connect(g_context.destination);
96100
}
97101
g_context.resume();
98102
}
@@ -131,12 +135,36 @@ const setVisualizer = ndx => {
131135
setSelectOption(visualTypeElem, ndx);
132136
};
133137

138+
try {
139+
g_localSettings = JSON.parse(localStorage.getItem('localSettings'));
140+
} catch {
141+
}
142+
143+
{
144+
if (!g_localSettings || typeof g_localSettings !== 'object') {
145+
g_localSettings = {};
146+
}
147+
const defaultSettings = {
148+
volume: 100,
149+
};
150+
for (const [key, value] of Object.entries(defaultSettings)) {
151+
if (typeof g_localSettings[key] != typeof value) {
152+
g_localSettings[key] = value;
153+
}
154+
}
155+
}
156+
157+
function saveSettings() {
158+
localStorage.setItem('localSettings', JSON.stringify(g_localSettings));
159+
}
160+
134161
async function main() {
135162
canvas = $('visualization');
136163
controls = $('controls');
137164

138165
g_context = new AudioContext();
139166
g_context.resume(); // needed for safari
167+
g_gainNode = new GainNode(g_context);
140168
await ByteBeatNode.setup(g_context);
141169
g_byteBeat = new ByteBeatNode(g_context);
142170

@@ -204,6 +232,22 @@ async function main() {
204232
return select;
205233
}
206234

235+
function addVerticalRange(options, props) {
236+
const fn = props.onChange;
237+
const valueElem = el('div', { textContent: options.value ?? 0 });
238+
return el('div', {className: 'vertical-range', tabIndex: 0}, [
239+
valueElem,
240+
el('div', {className: 'vertical-range-holder'}, [
241+
el('input', { ...options, type: 'range', onInput: (e) => {
242+
valueElem.textContent = e.target.value;
243+
if (fn) {
244+
fn(e);
245+
}
246+
},}),
247+
]),
248+
])
249+
}
250+
207251
beatTypeElem = addSelection(s_beatTypes, 0, {
208252
onChange(event) {
209253
g_byteBeat.setType(event.target.selectedIndex);
@@ -229,6 +273,15 @@ async function main() {
229273
});
230274
controls.appendChild(sampleRateElem);
231275

276+
const volumeElem = addVerticalRange({min: 1, max: 100, step: 1, value: g_localSettings.volume }, {
277+
onChange(event) {
278+
g_gainNode.gain.value = event.target.value / 100;
279+
g_localSettings.volume = parseInt(event.target.value);
280+
saveSettings();
281+
},
282+
});
283+
controls.appendChild(volumeElem);
284+
232285
if (g_slow) {
233286
g_visualizers = [
234287
{name: 'none', visualizer: new NullVisualizer() },

html5bytebeat.html

+32-1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,37 @@
159159
background-color: rgb(0, 0, 66);
160160
display: block;
161161
}
162+
.vertical-range {
163+
display: inline;
164+
position: relative;
165+
text-align: center;
166+
}
167+
.vertical-range>* {
168+
display: inline;
169+
}
170+
.vertical-range input {
171+
position: absolute;
172+
writing-mode: vertical-lr;
173+
direction: rtl;
174+
appearance: slider-vertical;
175+
vertical-align: bottom;
176+
}
177+
.vertical-range>* {
178+
display: inline;
179+
}
180+
.vertical-range .vertical-range-holder {
181+
display: none;
182+
width: 20px;
183+
height: 20px;
184+
position: absolute;
185+
left: calc(50% - 10px);
186+
top: 100%;
187+
}
188+
.vertical-range:focus-within .vertical-range-holder {
189+
display: inline-flex !important;
190+
justify-content: center;
191+
}
192+
162193
.status {
163194
color: #0F0;
164195
}
@@ -194,7 +225,7 @@
194225
outline: 0; /* this removes browser-side outline */
195226
}
196227

197-
button, select, .buttonstyle, #showSongs {
228+
button, select, .buttonstyle, #showSongs, .vertical-range {
198229
padding: 8px 12px 8px 12px;
199230
white-space: pre;
200231
border: none;

0 commit comments

Comments
 (0)