From 7faf4a4e856aeb920b64477785a4aab9ff574968 Mon Sep 17 00:00:00 2001 From: grymmjack Date: Wed, 31 May 2023 16:52:11 -0400 Subject: [PATCH 1/2] Added support for decay and gain --- qb.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/qb.js b/qb.js index 5b29654..e5af79f 100644 --- a/qb.js +++ b/qb.js @@ -2815,8 +2815,11 @@ var QB = new function() { } }; - this.sub_Sound = async function(freq, duration, shape) { + + this.sub_Sound = async function(freq, duration, shape, decay, gain) { if (shape == undefined) { shape = "square"; } + if (decay == undefined) { decay = 0.0; } + if (gain == undefined) { gain = 1.0; } if (!(freq == 0 || (freq >= 32 && freq <= 32767))) { throw new Error("Frequency invalid - valid: 0 (delay), 32 to 32767"); } @@ -2829,12 +2832,16 @@ var QB = new function() { } else { var context = new AudioContext(); var oscillator = context.createOscillator(); + var gainNode = context.createGain(); oscillator.type = shape; oscillator.frequency.value = freq; - oscillator.connect(context.destination); + oscillator.connect(gainNode); + gainNode.connect(context.destination) + gainNode.gain.value = gain; oscillator.start(); - setTimeout(await function () { - oscillator.stop(); + setTimeout(await async function () { + gainNode.gain.setTargetAtTime(0, context.currentTime, decay); + oscillator.stop(duration + decay + 1); }, duration); } }; From a4ddafb5c8c4057f1783409a02124b9abb22749a Mon Sep 17 00:00:00 2001 From: grymmjack Date: Wed, 31 May 2023 17:01:56 -0400 Subject: [PATCH 2/2] Added type validation - when fail use default --- qb.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/qb.js b/qb.js index e5af79f..eceac3a 100644 --- a/qb.js +++ b/qb.js @@ -2817,9 +2817,9 @@ var QB = new function() { this.sub_Sound = async function(freq, duration, shape, decay, gain) { - if (shape == undefined) { shape = "square"; } - if (decay == undefined) { decay = 0.0; } - if (gain == undefined) { gain = 1.0; } + if (shape == undefined || (typeof shape != 'string')) { shape = "square"; } + if (decay == undefined || (typeof decay != 'number')) { decay = 0.0; } + if (gain == undefined || (typeof gain != 'number')) { gain = 1.0; } if (!(freq == 0 || (freq >= 32 && freq <= 32767))) { throw new Error("Frequency invalid - valid: 0 (delay), 32 to 32767"); }