forked from mmckegg/adsr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
96 lines (77 loc) · 2.4 KB
/
test.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var audioContext = new AudioContext()
var ADSR = require('./index')
var attack = addSlider('attack', 0, 0.0001, 0, 10)
var decay = addSlider('decay', 0, 0.0001, 0, 10)
var sustain = addSlider('sustain', 1, 0.0001, 0, 2)
var release = addSlider('release', 0, 0.0001, 0, 10)
var startValue = addSlider('startValue', 0, 0.0001, 0, 2)
var endValue = addSlider('endValue', 0, 0.0001, 0, 2)
function getAdsr(){
var adsr = ADSR(audioContext)
adsr.attack = parseFloat(attack.value)
adsr.decay = parseFloat(decay.value)
adsr.sustain = parseFloat(sustain.value)
adsr.release = parseFloat(release.value)
adsr.startValue.value = parseFloat(startValue.value)
adsr.endValue.value = parseFloat(endValue.value)
return adsr
}
addButton('trigger 1s', function(){
var adsr = getAdsr()
var osc = audioContext.createOscillator()
var gain = audioContext.createGain()
osc.connect(gain)
gain.connect(audioContext.destination)
gain.gain.value = 0
adsr.connect(gain.gain)
osc.start(audioContext.currentTime)
adsr.start(audioContext.currentTime)
var endTime = adsr.stop(audioContext.currentTime+1)
osc.stop(endTime)
})
var releaseHold = null
addButton('trigger hold', function(){
var adsr = getAdsr()
var osc = audioContext.createOscillator()
var gain = audioContext.createGain()
osc.connect(gain)
gain.connect(audioContext.destination)
gain.gain.value = 0
adsr.connect(gain.gain)
osc.start(audioContext.currentTime)
adsr.start(audioContext.currentTime)
releaseHold = function(){
var endTime = adsr.stop(audioContext.currentTime)
osc.stop(endTime)
}
}, function(){
releaseHold()
})
function addButton(name, down, up){
var button = document.createElement('button')
button.onmousedown = down
button.onmouseup = up
button.textContent = name
document.body.appendChild(button)
}
function addSlider(property, defaultValue, step, min, max){
var container = document.createElement('div')
container.appendChild(document.createTextNode(property))
var label = document.createTextNode(defaultValue)
var slider = document.createElement('input')
slider.type = 'range'
slider.min = min
slider.max = max
slider.value = defaultValue
slider.style.width = '300px'
if (step){
slider.step = step
}
slider.onchange = function(){
label.data = this.value
}
container.appendChild(slider)
container.appendChild(label)
document.body.appendChild(container)
return slider
}