This repository has been archived by the owner on Apr 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
example.js
77 lines (59 loc) · 1.94 KB
/
example.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
var createAudioNode = require('./index')
var extendTransform = require('audio-param-transform')
var audioContext = new AudioContext()
var filter = audioContext.createBiquadFilter()
var gain = audioContext.createGain()
extendTransform(filter.frequency, audioContext)
extendTransform(gain.gain, audioContext)
filter.connect(gain)
var decimalFrequencyParam = filter.frequency.transform(valueToFreq)
var gainBoostParam = gain.gain.transform(valueToGain)
var customNode = createAudioNode(filter, gain, {
amount: {
min: 0,
max: 1,
defaultValue: 0.5,
targets: [ decimalFrequencyParam, gainBoostParam ]
}
})
function valueToFreq(defaultValue, value){
var min = Math.log(100)/Math.log(10)
, max = Math.log(20000)/Math.log(10)
, range = max-min
return Math.pow(10, value * range + min)
}
function valueToGain(defaultValue, value){
var gain = (value) + 1
return gain
}
var oscillator = audioContext.createOscillator()
oscillator.type = 'sawtooth'
oscillator.frequency.value = 400
oscillator.start(0)
oscillator.connect(customNode)
customNode.connect(audioContext.destination)
var slider = document.createElement('input')
slider.type = 'range'
slider.min = 0
slider.max = 1
slider.step = 0.00001
slider.value = customNode.amount.value
document.body.appendChild(slider)
slider.onchange = function(){
customNode.amount.value = parseFloat(this.value)
}
addButton('setValueAtTime (0.8 at t+0)', function(){
customNode.amount.setValueAtTime(0.8, audioContext.currentTime)
})
addButton('linearRampToValueAtTime (0.2 at t+1)', function(){
customNode.amount.linearRampToValueAtTime(0.2, audioContext.currentTime + 1)
})
addButton('exponentialRampToValueAtTime (0.9 at t+1)', function(){
customNode.amount.exponentialRampToValueAtTime(0.9, audioContext.currentTime + 1)
})
function addButton(name, func){
var button = document.createElement('button')
button.onclick = func
button.textContent = name
document.body.appendChild(button)
}