forked from kapetan/browser-beep
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (38 loc) · 1.14 KB
/
index.js
File metadata and controls
48 lines (38 loc) · 1.14 KB
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
var FREQUENCY = 440
var INTERVAL = 250
var RAMP_VALUE = 0.00001
var RAMP_DURATION = 1
module.exports = function (options) {
if (!options) options = {}
var context = options.context || new window.AudioContext()
var frequency = options.frequency || FREQUENCY
var interval = options.interval || INTERVAL
var play = function () {
var currentTime = context.currentTime
var osc = context.createOscillator()
var gain = context.createGain()
osc.connect(gain)
gain.connect(context.destination)
gain.gain.setValueAtTime(gain.gain.value, currentTime)
gain.gain.exponentialRampToValueAtTime(RAMP_VALUE, currentTime + RAMP_DURATION)
osc.onended = function () {
gain.disconnect(context.destination)
osc.disconnect(gain)
}
osc.type = 'sine'
osc.frequency.value = frequency
osc.start(currentTime)
osc.stop(currentTime + RAMP_DURATION)
}
var beep = function (times) {
if (!times) times = 1
;(function loop (i) {
play()
if (++i < times) setTimeout(loop, interval, i)
})(0)
}
beep.destroy = function () {
if (!options.context) context.close()
}
return beep
}