From 9620d3750c54b21c3da0460cea117b57fbeb9eac Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Mon, 28 Jul 2025 15:36:21 -0400 Subject: get volume control working --- player.mjs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'player.mjs') diff --git a/player.mjs b/player.mjs index 988d5d6..c8707b2 100644 --- a/player.mjs +++ b/player.mjs @@ -12,8 +12,6 @@ const tromboneWave = new PeriodicWave(audioCtx, { imag: Trombone.imag }); -const globalGain = new GainNode(audioCtx, { gain: 0.06 }) - export default class extends HTMLElement { static name = 'x-player' static register() { @@ -21,10 +19,12 @@ export default class extends HTMLElement { customElements.define(this.name, this); } + // A4 + aFreq = 440; + // oscillatornodes notes = []; getOffsets = () => []; - detuneParams = []; constructor() { super(); @@ -32,14 +32,18 @@ export default class extends HTMLElement { } connectedCallback() { + this.globalGain = audioCtx.createGain(); + this.globalGain.connect(audioCtx.destination); + this.globalGain.gain.setValueAtTime(this.volume, audioCtx.currentTime); + this.querySelector('.volume').onchange = e => { console.debug('vol changed', e, this.volume); - globalGain.setValueAtTime(0.1, audioCtx.currentTime); - globalGain.value = this.volume; + this.globalGain.gain.setValueAtTime(this.volume, audioCtx.currentTime); } } get volume() { + console.debug('Player#volume', this.querySelector('.volume').value) return Number(this.querySelector('.volume').value); } @@ -74,13 +78,11 @@ export default class extends HTMLElement { for (let i = 0; i < this.offsets.length; i++) { if (!this.notes[i]) { const note = new OscillatorNode(audioCtx, { - frequency: 440, // a above middle c (c4) - // detune: o, - // type: 'sine', + frequency: this.aFreq, type: 'custom', periodicWave: tromboneWave, }); - note.connect(globalGain).connect(audioCtx.destination); + note.connect(this.globalGain); note.start(); this.notes[i] = note; } -- cgit v1.3