mirror of
https://github.com/boxgaming/qbjs.git
synced 2024-09-20 04:24:45 +00:00
Merge pull request #44 from grymmjack/sound-qb-keyword
Added support for decay and gain
This commit is contained in:
commit
97406a891d
1 changed files with 12 additions and 5 deletions
17
qb.js
17
qb.js
|
@ -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))) {
|
if (!(freq == 0 || (freq >= 32 && freq <= 32767))) {
|
||||||
throw new Error("Frequency invalid - valid: 0 (delay), 32 to 32767");
|
throw new Error("Frequency invalid - valid: 0 (delay), 32 to 32767");
|
||||||
}
|
}
|
||||||
|
@ -2829,12 +2832,16 @@ var QB = new function() {
|
||||||
} else {
|
} else {
|
||||||
var context = new AudioContext();
|
var context = new AudioContext();
|
||||||
var oscillator = context.createOscillator();
|
var oscillator = context.createOscillator();
|
||||||
|
var gainNode = context.createGain();
|
||||||
oscillator.type = shape;
|
oscillator.type = shape;
|
||||||
oscillator.frequency.value = freq;
|
oscillator.frequency.value = freq;
|
||||||
oscillator.connect(context.destination);
|
oscillator.connect(gainNode);
|
||||||
|
gainNode.connect(context.destination)
|
||||||
|
gainNode.gain.value = gain;
|
||||||
oscillator.start();
|
oscillator.start();
|
||||||
setTimeout(await function () {
|
setTimeout(await async function () {
|
||||||
oscillator.stop();
|
gainNode.gain.setTargetAtTime(0, context.currentTime, decay);
|
||||||
|
oscillator.stop(duration + decay + 1);
|
||||||
}, duration);
|
}, duration);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue