This repository was archived by the owner on Apr 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlink-param.js
113 lines (94 loc) · 2.5 KB
/
link-param.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var Observ = require('observ')
var ObservStruct = require('observ-struct')
var Prop = require('observ-default')
var Param = require('audio-slot-param')
var ParamProxy = require('audio-slot-param/proxy')
var Transform = require('audio-slot-param/transform')
module.exports = LinkParam
function LinkParam (context) {
var obs = ObservStruct({
param: Observ(),
minValue: Param(context, 0),
maxValue: Param(context, 1),
mode: Prop('linear'),
quantize: Prop(0)
})
obs.value = ParamProxy(context, 0)
obs._type = 'LinkParam'
obs.context = context
var updating = false
var releaseParams = null
var onDestroy = []
// transform: value * (maxValue - minValue) + minValue
var outputValue = Transform(context, [
{ param: obs.mode },
{ param: obs.value, transform: applyInterpolation },
{ param: Transform(context,
[ { param: obs.maxValue },
{ param: obs.minValue, transform: subtract }
]), transform: multiply
},
{ param: obs.minValue, transform: add },
{ param: obs.quantize, transform: quantize }
])
obs.onSchedule = outputValue.onSchedule
obs.getValueAt = outputValue.getValueAt
obs.getValue = function () {
return outputValue.getValueAt(context.audio.currentTime)
}
if (context.paramLookup) {
releaseParams = context.paramLookup(handleUpdate)
}
if (context.active) {
onDestroy.push(context.active(handleUpdate))
}
obs.param(handleUpdate)
obs.destroy = function () {
while (onDestroy.length) {
onDestroy.pop()()
}
releaseParams && releaseParams()
releaseParams = null
obs.value.destroy()
}
return obs
// scoped
function updateNow () {
if (!context.active || context.active()) {
var param = context.paramLookup.get(obs.param())
obs.value.setTarget(param)
} else {
obs.value.setTarget(null)
}
updating = false
}
function handleUpdate () {
if (!updating) {
updating = true
setImmediate(updateNow)
}
}
function applyInterpolation (mode, value) {
if (mode === 'exp') {
if (obs.minValue() < obs.maxValue()) {
return value * value
} else {
var i = 1 - value
return 1 - (i * i)
}
} else { // linear
return value
}
}
}
function quantize (value, grid) {
if (grid) {
return Math.round(value * grid) / grid
} else {
return value
}
}
// transform operations
function add (a, b) { return a + b }
function subtract (a, b) { return a - b }
function multiply (a, b) { return a * b }