You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.0 KiB
JavaScript

3 years ago
const ui = document.getElementById("ui");
const bpm = document.getElementById("bpm");
bpm.addEventListener("change", (e) => {
Tone.Transport.bpm.value = e.target.value;
});
const newChannel = document.getElementById("new-channel");
const insert = document.getElementById("insert");
insert.addEventListener("click", (e) => {
3 years ago
input = newChannel.querySelector("input[type=text]");
const newSequence = input.value;
3 years ago
if (newSequence) {
createChannel(rebuildArray(newSequence));
3 years ago
input.value = "";
3 years ago
}
});
document.getElementById("start")?.addEventListener("click", async () => {
await Tone.start();
console.log("Audio is ready");
Tone.Transport.bpm.value = bpm.value;
newChannel.classList.remove("hidden");
3 years ago
createChannel(["C4", null, null, "F4", null, null, null]);
createChannel(["C3", "F3"]);
3 years ago
});
let createSequence = function (sequence) {
const synth = new Tone.Synth();
const seq = new Tone.Sequence((time, note) => {
synth.triggerAttackRelease(note, 0.1, time);
}, sequence).start(0);
Tone.Transport.start();
return synth;
};
let createChannel = function (sequence) {
const channel = new Tone.Channel().toDestination();
const synth = new Tone.Synth();
let seq = new Tone.Sequence((time, note) => {
synth.triggerAttackRelease(note, 0.1, time);
}, sequence).start(0);
Tone.Transport.start();
synth.connect(channel);
let container = document.createElement("div");
container.classList.add("channel");
let control = document.createElement("input");
control.setAttribute("type", "range");
control.value = 0;
control.setAttribute("min", -48);
control.setAttribute("max", 6);
control.addEventListener("change", (e) => {
channel.volume.value = e.target.value;
});
let notation = document.createElement("input");
notation.value = simplifyArray(JSON.stringify(sequence));
3 years ago
notation.classList.add("notation");
notation.addEventListener("change", (e) => {
try {
let newSeq = rebuildArray(e.target.value);
3 years ago
seq.dispose();
seq = new Tone.Sequence((time, note) => {
synth.triggerAttackRelease(note, 0.1, time);
}, newSeq).start(0);
} catch (e) {
console.warn(e);
}
});
container.appendChild(notation);
container.appendChild(control);
ui.prepend(container);
};
function simplifyArray(literal) {
// Sorry for this
3 years ago
return literal
.replaceAll('"', "")
.replaceAll("'", "")
.replaceAll(",", ", ")
.replaceAll("null", "-");
}
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));
3 years ago
getAllIndexes(array, "-").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;
}