keyboard has been hooked up
This commit is contained in:
parent
7431d06f79
commit
b9acf85e29
44
synth.js
44
synth.js
|
@ -24,9 +24,9 @@ class Synth {
|
||||||
this.audioContext = new AudioContext();
|
this.audioContext = new AudioContext();
|
||||||
this.gain = this.audioContext.createGain();
|
this.gain = this.audioContext.createGain();
|
||||||
this.oscillators = [
|
this.oscillators = [
|
||||||
this.createOscillator("sawtooth", 440, 0),
|
this.createOscillator("sawtooth", 261.63, 0),
|
||||||
this.createOscillator("sawtooth", 440, 0),
|
this.createOscillator("sawtooth", 261.63, 0),
|
||||||
this.createOscillator("sawtooth", 440, 0),
|
this.createOscillator("sawtooth", 261.63, 0),
|
||||||
];
|
];
|
||||||
this.gain.connect(this.audioContext.destination);
|
this.gain.connect(this.audioContext.destination);
|
||||||
}
|
}
|
||||||
|
@ -86,11 +86,14 @@ function updateFrequency(
|
||||||
event,
|
event,
|
||||||
synth,
|
synth,
|
||||||
oscContainer,
|
oscContainer,
|
||||||
|
voiceIndex,
|
||||||
|
note,
|
||||||
octaveShift,
|
octaveShift,
|
||||||
detuneAmount
|
detuneAmount
|
||||||
) {
|
) {
|
||||||
let baseFreq = oscContainer.baseFreq;
|
let baseFreq = oscContainer.baseFreq;
|
||||||
let currentFreq = baseFreq;
|
let currentFreq = baseFreq;
|
||||||
|
|
||||||
if (octaveShift) {
|
if (octaveShift) {
|
||||||
if (octaveShift === "up") {
|
if (octaveShift === "up") {
|
||||||
currentFreq = baseFreq * 2;
|
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) {
|
if (detuneAmount) {
|
||||||
currentFreq = currentFreq + detuneAmount;
|
currentFreq = currentFreq + detuneAmount;
|
||||||
}
|
}
|
||||||
|
@ -126,7 +144,7 @@ function setupOctaveControls(voiceIndex, synth) {
|
||||||
const osc = synth.oscillators[voiceIndex];
|
const osc = synth.oscillators[voiceIndex];
|
||||||
osc.currentOctave--;
|
osc.currentOctave--;
|
||||||
octaveDisplay.value = 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
|
// 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];
|
const osc = synth.oscillators[voiceIndex];
|
||||||
osc.currentOctave++;
|
osc.currentOctave++;
|
||||||
octaveDisplay.value = 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
|
// Loop through each voice and set up its octave control buttons
|
||||||
for (let i = 0; i < synth.oscillators.length; i++) {
|
for (let i = 0; i < synth.oscillators.length; i++) {
|
||||||
setupOctaveControls(i, synth); // Call setupOctaveControls for each voice
|
setupOctaveControls(i, synth); // Call setupOctaveControls for each voice
|
||||||
|
@ -206,6 +238,6 @@ window.onload = function () {
|
||||||
let osc = synth.oscillators[0];
|
let osc = synth.oscillators[0];
|
||||||
let detune = parseFloat(detuneSliderVoice1.value);
|
let detune = parseFloat(detuneSliderVoice1.value);
|
||||||
console.log(detune);
|
console.log(detune);
|
||||||
updateFrequency(event, synth, osc, null, detune);
|
updateFrequency(event, synth, osc, 0, null, null, detune);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue