From 4a375051616d1ccb826f00040874c2cd75902ed8 Mon Sep 17 00:00:00 2001 From: Fen Dweller Date: Wed, 7 Aug 2019 16:21:45 -0400 Subject: [PATCH] Add volume control for loops --- audio.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/audio.js b/audio.js index 5a109e3..8112b1a 100644 --- a/audio.js +++ b/audio.js @@ -1,5 +1,6 @@ let playing = []; let looping = {}; +let loopGains = {}; let waiting = {}; @@ -45,8 +46,8 @@ function playSfx(name) { src.start(0); } -function playLoop(name) { - if (audioDict[name] == undefined) { +function playLoop(name, volume=1) { + if (audioDict[name] === undefined) { if (waiting[name]) { waiting[name].push({ @@ -59,6 +60,8 @@ function playLoop(name) { console.error(name + " is not loaded yet, dingus"); return; } + + loopGains[name].gain.value = volume; // if already playing, just keep going if (looping[name] && !looping[name].done) { @@ -68,7 +71,8 @@ function playLoop(name) { let src = audioContext.createBufferSource(); src.buffer = audioDict[name]; - src.connect(gainControl); + src.connect(loopGains[name]); + loopGains[name].connect(gainControl); looping[name] = src; @@ -132,6 +136,8 @@ function loadAudio(name, flush=false) { return; } + loopGains[name] = audioContext.createGain(); + waiting[name] = []; // is the audio already stored locally?