adds a detune slider

This commit is contained in:
Emanuel Rodriguez 2024-02-08 23:18:43 -08:00
parent dc1bc9df2d
commit 1ac8df3a3f
2 changed files with 20 additions and 4 deletions

View File

@ -1,5 +1,5 @@
class Synth {
constructor(audioContext, waveType = "sine", frequency = 400) {
constructor(audioContext, waveType = "triangle", frequency = 100) {
this.audioContext = audioContext;
this.oscWaveType = waveType;
this.oscFrequency = frequency;
@ -17,6 +17,15 @@ class Synth {
this.osc.type = this.oscWaveType;
}
setOscFrequency(freq) {
this.oscFrequency = freq;
this.osc.frequency.setValueAtTime(freq, this.audioContext.currentTime);
}
setOscDetune(amount) {
this.osc.detune.setValueAtTime(amount, this.audioContext.currentTime);
}
startOsc() {
this.osc.start();
this.isPlaying = true;
@ -29,7 +38,6 @@ class Synth {
this.osc.stop();
this.osc = this.audioContext.createOscillator();
console.log(this.osc);
this.osc.type = currentType;
this.osc.frequency.setValueAtTime(currentFreq, this.audioContext.currentTime);
this.osc.connect(this.audioContext.destination);
@ -37,7 +45,6 @@ class Synth {
}
toggleOsc() {
if (this.isPlaying === false) {
this.startOsc();
} else {
@ -53,4 +60,12 @@ let synth = new Synth(globalAudioContext);
window.onload = function() {
console.log("hello world!");
console.log(synth);
var detune = document.getElementById("detune");
detune.addEventListener("input", () => {
synth.setOscDetune(parseFloat(detune.value))
})
}

View File

@ -11,5 +11,6 @@
<body class="bg-gray-900 p-10 mb-5">
<script src="single-voice-synth.js"></script>
<div class="flex justify-between"></div>
<div class="flex justify-between"></div>
<input type="range" min="-40" max="40" value="0" class="slider" id="detune">
</body>