simplify array string

master
km0 3 years ago
parent f9fc5a2634
commit 6618976efd

@ -13,8 +13,8 @@
<button id="start">Start</button> <button id="start">Start</button>
<label for="bpm">BPM</label> <label for="bpm">BPM</label>
<input type="number" name="bpm" id="bpm" min="1" max="200" value="120" /> <input type="number" name="bpm" id="bpm" min="1" max="200" value="120" />
<div class="new-channel"> <div class="new-channel hidden" id="new-channel">
<input type="text" class="notation" id="new-channel" /> <input type="text" class="notation" />
<div class="insert"> <div class="insert">
<button id="insert">Add</button> <button id="insert">Add</button>
</div> </div>

@ -8,10 +8,9 @@ bpm.addEventListener("change", (e) => {
const newChannel = document.getElementById("new-channel"); const newChannel = document.getElementById("new-channel");
const insert = document.getElementById("insert"); const insert = document.getElementById("insert");
insert.addEventListener("click", (e) => { insert.addEventListener("click", (e) => {
const newSequence = newChannel.value; const newSequence = newChannel.querySelector("input[type=text]").value;
if (newSequence) { if (newSequence) {
createChannel(JSON.parse(newSequence)); createChannel(rebuildArray(newSequence));
newChannel.value = ""; newChannel.value = "";
} }
}); });
@ -21,10 +20,13 @@ document.getElementById("start")?.addEventListener("click", async () => {
console.log("Audio is ready"); console.log("Audio is ready");
Tone.Transport.bpm.value = bpm.value; Tone.Transport.bpm.value = bpm.value;
createChannel(["C3", "F3"]);
createChannel(["C3", ["G3", ["F3", "A#3"]], "C3", ["D#3", "D3"], "F3"]); newChannel.classList.remove("hidden");
createChannel([["C3", ["F3", "G3", "A#3"]], "C3", ["D#3", "D3"], "F3"]);
createChannel(["C4", null, null, "F4", null, null, null]); createChannel(["C4", null, null, "F4", null, null, null]);
createChannel([["C3", ["F3", "G3", "A#3"]], "C3", ["D#3", "D3"], "F3"]);
createChannel(["C3", ["G3", ["F3", "A#3"]], "C3", ["D#3", "D3"], "F3"]);
createChannel(["C3", "F3"]);
}); });
let createSequence = function (sequence) { let createSequence = function (sequence) {
@ -61,11 +63,11 @@ let createChannel = function (sequence) {
}); });
let notation = document.createElement("input"); let notation = document.createElement("input");
notation.value = JSON.stringify(sequence); notation.value = simplifyArray(JSON.stringify(sequence));
notation.classList.add("notation"); notation.classList.add("notation");
notation.addEventListener("change", (e) => { notation.addEventListener("change", (e) => {
try { try {
let newSeq = JSON.parse(e.target.value); let newSeq = rebuildArray(e.target.value);
seq.dispose(); seq.dispose();
seq = new Tone.Sequence((time, note) => { seq = new Tone.Sequence((time, note) => {
synth.triggerAttackRelease(note, 0.1, time); synth.triggerAttackRelease(note, 0.1, time);
@ -79,3 +81,34 @@ let createChannel = function (sequence) {
container.appendChild(control); container.appendChild(control);
ui.prepend(container); ui.prepend(container);
}; };
function simplifyArray(literal) {
// Sorry for this
return literal.replaceAll('"', "").replaceAll("'", "").replaceAll(",", ", ");
}
function rebuildArray(literal) {
// And also for this
let rebuild = literal
.replaceAll(/\s+/gi, "")
.replaceAll("[", '["')
.replaceAll(",", '","')
.replaceAll("]", '"]')
.replaceAll('"[', "[")
.replaceAll(']"', "]");
let array = JSON.parse(rebuild);
try {
getAllIndexes(array, "null").forEach((index) => (array[index] = null));
} catch (e) {
console.log(e);
}
return array;
}
function getAllIndexes(arr, val) {
var indexes = [],
i;
for (i = 0; i < arr.length; i++) if (arr[i] === val) indexes.push(i);
return indexes;
}

@ -9,14 +9,21 @@
margin: 64px 0; margin: 64px 0;
} }
input, label, button, .insert { .hidden {
display: none;
}
input,
label,
button,
.insert {
font-size: 1.5rem; font-size: 1.5rem;
font-family: monospace; font-family: monospace;
margin: 0 8px; margin: 0 8px;
} }
input[type='range'], .insert { input[type="range"],
.insert {
width: 200px; width: 200px;
} }
@ -24,12 +31,12 @@ input[type='range'], .insert {
margin: 0; margin: 0;
} }
button{ button {
border: 1px solid currentColor; border: 1px solid currentColor;
} }
button#insert { button#insert {
margin-right: 140px; margin-right: 140px;
} }
#bpm { #bpm {

Loading…
Cancel
Save