keyboard has been hooked up

This commit is contained in:
Emanuel Rodriguez 2023-10-07 02:41:24 -07:00
parent 7431d06f79
commit b9acf85e29
1 changed files with 38 additions and 6 deletions

View File

@ -24,9 +24,9 @@ class Synth {
this.audioContext = new AudioContext();
this.gain = this.audioContext.createGain();
this.oscillators = [
this.createOscillator("sawtooth", 440, 0),
this.createOscillator("sawtooth", 440, 0),
this.createOscillator("sawtooth", 440, 0),
this.createOscillator("sawtooth", 261.63, 0),
this.createOscillator("sawtooth", 261.63, 0),
this.createOscillator("sawtooth", 261.63, 0),
];
this.gain.connect(this.audioContext.destination);
}
@ -86,11 +86,14 @@ function updateFrequency(
event,
synth,
oscContainer,
voiceIndex,
note,
octaveShift,
detuneAmount
) {
let baseFreq = oscContainer.baseFreq;
let currentFreq = baseFreq;
if (octaveShift) {
if (octaveShift === "up") {
currentFreq = baseFreq * 2;
@ -101,6 +104,21 @@ function updateFrequency(
}
}
if (note) {
let noteInHz = noteToHz(note);
// if the note in hz is not in the octave the oscillator is currently at
// I will need to transform it down to the correct octave.
currentFreq = noteInHz;
oscContainer.baseFreq = noteInHz;
oscContainer.currentOctave = 0;
let octaveDisplay = document.getElementById(
"octavedisplay" + (voiceIndex + 1)
);
octaveDisplay.value = 0;
console.log(noteInHz);
console.log("current octave for voice: " + oscContainer.currentOctave);
}
if (detuneAmount) {
currentFreq = currentFreq + detuneAmount;
}
@ -126,7 +144,7 @@ function setupOctaveControls(voiceIndex, synth) {
const osc = synth.oscillators[voiceIndex];
osc.currentOctave--;
octaveDisplay.value = osc.currentOctave;
updateFrequency(event, synth, osc, "down", voiceIndex);
updateFrequency(event, synth, osc, voiceIndex, null, "down", null);
});
// Set up event listener for the octave up button for the current voice
@ -136,7 +154,7 @@ function setupOctaveControls(voiceIndex, synth) {
const osc = synth.oscillators[voiceIndex];
osc.currentOctave++;
octaveDisplay.value = osc.currentOctave;
updateFrequency(event, synth, osc, "up", voiceIndex);
updateFrequency(event, synth, osc, voiceIndex, null, "up", null);
});
}
@ -195,6 +213,20 @@ window.onload = function () {
}
});
// handle note changes
document.querySelectorAll("input[name='notechoice']").forEach((rb) => {
rb.addEventListener("change", (event) => {
let noteSelected = document.querySelector(
"input[name='notechoice']:checked"
).value;
for (let i = 0; i < synth.oscillators.length; i++) {
let osc = synth.oscillators[i];
updateFrequency(event, synth, osc, i, noteSelected, null, null);
}
});
});
// Loop through each voice and set up its octave control buttons
for (let i = 0; i < synth.oscillators.length; i++) {
setupOctaveControls(i, synth); // Call setupOctaveControls for each voice
@ -206,6 +238,6 @@ window.onload = function () {
let osc = synth.oscillators[0];
let detune = parseFloat(detuneSliderVoice1.value);
console.log(detune);
updateFrequency(event, synth, osc, null, detune);
updateFrequency(event, synth, osc, 0, null, null, detune);
});
};