-
Notifications
You must be signed in to change notification settings - Fork 0
/
winsound.js
48 lines (36 loc) · 1.74 KB
/
winsound.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//* Pre Requisites:
var audioCtx = new(window.AudioContext || window.webkitAudioContext || window.audioContext);
//All arguments are optional:
//duration of the tone in milliseconds. Default is 500
//frequency of the tone in hertz. default is 440
//volume of the tone. Default is 1, off is 0.
//type of tone. Possible values are sine, square, sawtooth, triangle, and custom. Default is sine.
//callback to use on end of tone
function beep(duration, frequency, volume, type, callback) {
var oscillator = audioCtx.createOscillator();
var gainNode = audioCtx.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);
if (volume) { gainNode.gain.value = volume; }
if (frequency) { oscillator.frequency.value = frequency; }
if (type) { oscillator.type = type; }
if (callback) { oscillator.onended = callback; }
oscillator.start(audioCtx.currentTime);
oscillator.stop(audioCtx.currentTime + ((duration || 500) / 1000));
}
const abeep = (duration, frequency, volume, type, callback) =>new Promise((res, rej) =>{
var oscillator = audioCtx.createOscillator();
var gainNode = audioCtx.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);
if (volume) { gainNode.gain.value = volume; }
if (frequency) { oscillator.frequency.value = frequency; }
if (type) { oscillator.type = type; }
if (callback) { oscillator.onended = callback; }
oscillator.start(audioCtx.currentTime);
oscillator.stop(audioCtx.currentTime + ((duration || 500) / 1000));
res(oscillator)
})
//_________________________________
const Beep = (duration, frequency) => beep(duration, frequency);
const asyncBeep = async (duration, frequency) => await beep(duration, frequency);