1
1
Fork 0
mirror of https://github.com/boxgaming/qbjs.git synced 2024-09-19 20:14:58 +00:00

Merge pull request #44 from grymmjack/sound-qb-keyword

Added support for decay and gain
This commit is contained in:
boxgaming 2023-05-31 16:05:14 -05:00 committed by GitHub
commit 97406a891d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

17
qb.js
View file

@ -2815,8 +2815,11 @@ var QB = new function() {
}
};
this.sub_Sound = async function(freq, duration, shape) {
if (shape == undefined) { shape = "square"; }
this.sub_Sound = async function(freq, duration, shape, decay, gain) {
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");
}
@ -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);
}
};