-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.js
214 lines (176 loc) · 4.79 KB
/
sound.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const Automerge = require('automerge')
const _ = require('lodash')
// blank scene
let scene = {
nodes: {},
arcs: {}
}
// make a local automerge doc from our scene
let doc1 = Automerge.from(scene)
// given a series of deltas...
let deltas = [
[
{
"op": "newnode",
"path": "noise_0",
"kind": "noise",
"orient": [
0.3121451653567321,
0.369889483526838,
0.14650496286711281,
0.8627186456637955
],
"pos": [
0.0605223497200336,
1.8,
0.0405112532755187
]
},
[
{
"op": "newnode",
"path": "noise_0.out",
"history": false,
"index": 0,
"kind": "outlet"
}
]
],
[
{
"op": "newnode",
"path": "speaker_1",
"category": "abstraction",
"kind": "speaker",
"orient": [
0,
-0.258819,
0,
0.965926
],
"pos": [
0.75,
1,
0
]
},
[
{
"op": "newnode",
"path": "speaker_1.input",
"index": 0,
"kind": "inlet"
}
],
// use this to test a delnode
// [
// {
// "op": "delnode",
// "path": "speaker_1.input",
// "index": 0,
// "kind": "inlet"
// }
// ]
],
{
"op": "connect",
"paths": [
"noise_0.out",
"speaker_1.input"
]
}
]
let disconnectDelta = {
"op": "disconnect",
"paths": [
"noise_0.out",
"speaker_1.input"
]
}
// ...apply that delta to the scene using automerge
function applyDelta(delta){
switch(delta.op){
case 'propchange':
break;
case 'repath':
break;
case 'newnode':
// get the path, check if its a parent-most node or if it is a child node
let path = delta.path
let fullPath = path.split('.')
// is the node a parent?
if(fullPath.length == 1){
// add parent node to scene
doc1 = Automerge.change(doc1, doc => {
// create the node
doc.nodes[path] = {}
// remove the op and path props so that we just add the _props to the node in the doc
delete delta.op
delete delta.path
doc.nodes[path]['_props'] = delta
// otherObject[delta.path] = delta._props
})
} else {
delete delta.op
path = delta.path
delete delta.path
doc1 = Automerge.change(doc1, doc => {
_.set(doc1.nodes, path, {_props: delta});
})
}
break
case 'delnode':
doc1 = Automerge.change(doc1, doc => {
_.unset(doc1.nodes, delta.path);
})
break
// connection delta
case 'connect':
arc = delta.paths
src = arc[0]
dest = arc[1]
doc1 = Automerge.change(doc1, doc1 => {
// does the src have an arc?
if(doc1.arcs[src]){
// add connection to the dest
doc1.arcs[src].push(dest)
} else {
// add src obj and add connection the dest
doc1.arcs[src] = [dest]
}
})
break
// disconnection delta
case 'disconnect':
src = delta.paths[0]
unpatch = doc1.arcs[src].indexOf(delta.paths[1])
doc1 = Automerge.change(doc1, doc1 => {
// remove arc
doc1.arcs[src].splice(unpatch, 1)
// if src has no more connections:
if(doc1.arcs[src].length == 0){
delete doc1.arcs[src]
}
})
break
default: console.log('delta not defined in switchcase', delta.op)
}
}
let handleDelta = function(d) {
if ( d instanceof Array) {
for (var i = 0; i < d.length; i++) {
handleDelta(d[i]);
}
}
else applyDelta(d);
}
handleDelta(deltas)
console.log(doc1)
handleDelta(disconnectDelta)
console.log(doc1)
module.exports = {
// deltas,
doc1,
scene
}
// console.log(test)