forked from hydra-synth/hydra-synth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhydra-synth.js
467 lines (412 loc) · 12.3 KB
/
hydra-synth.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
const Output = require('./src/output.js')
const loop = require('raf-loop')
const Source = require('./src/hydra-source.js')
const Mouse = require('./src/lib/mouse.js')()
const Audio = require('./src/lib/audio.js')
const VidRecorder = require('./src/lib/video-recorder.js')
const ArrayUtils = require('./src/lib/array-utils.js')
const Sandbox = require('./src/eval-sandbox.js')
const Generator = require('./src/generator-factory.js')
// to do: add ability to pass in certain uniforms and transforms
class HydraRenderer {
console.log("HOLA SOLCHA!!! ")
constructor ({
pb = null,
width = 1280,
height = 720,
numSources = 4,
numOutputs = 4,
makeGlobal = true,
autoLoop = true,
detectAudio = true,
enableStreamCapture = true,
canvas,
precision,
extendTransforms = {} // add your own functions on init
} = {}) {
ArrayUtils.init()
this.pb = pb
this.width = width
this.height = height
this.renderAll = false
this.detectAudio = detectAudio
this._initCanvas(canvas)
// object that contains all properties that will be made available on the global context and during local evaluation
this.synth = {
time: 0,
bpm: 30,
width: this.width,
height: this.height,
fps: undefined,
stats: {
fps: 0
},
speed: 1,
mouse: Mouse,
render: this._render.bind(this),
setResolution: this.setResolution.bind(this),
update: (dt) => {},// user defined update function
hush: this.hush.bind(this)
}
if (makeGlobal) window.loadScript = this.loadScript
this.timeSinceLastUpdate = 0
this._time = 0 // for internal use, only to use for deciding when to render frames
// only allow valid precision options
let precisionOptions = ['lowp','mediump','highp']
if(precision && precisionOptions.includes(precision.toLowerCase())) {
this.precision = precision.toLowerCase()
//
// if(!precisionValid){
// console.warn('[hydra-synth warning]\nConstructor was provided an invalid floating point precision value of "' + precision + '". Using default value of "mediump" instead.')
// }
} else {
let isIOS =
(/iPad|iPhone|iPod/.test(navigator.platform) ||
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)) &&
!window.MSStream;
this.precision = isIOS ? 'highp' : 'mediump'
}
this.extendTransforms = extendTransforms
// boolean to store when to save screenshot
this.saveFrame = false
// if stream capture is enabled, this object contains the capture stream
this.captureStream = null
this.generator = undefined
this._initRegl()
this._initOutputs(numOutputs)
this._initSources(numSources)
this._generateGlslTransforms()
this.synth.screencap = () => {
this.saveFrame = true
}
if (enableStreamCapture) {
try {
this.captureStream = this.canvas.captureStream(25)
// to do: enable capture stream of specific sources and outputs
this.synth.vidRecorder = new VidRecorder(this.captureStream)
} catch (e) {
console.warn('[hydra-synth warning]\nnew MediaSource() is not currently supported on iOS.')
console.error(e)
}
}
if(detectAudio) this._initAudio()
if(autoLoop) loop(this.tick.bind(this)).start()
// final argument is properties that the user can set, all others are treated as read-only
this.sandbox = new Sandbox(this.synth, makeGlobal, ['speed', 'update', 'bpm', 'fps'])
}
eval(code) {
this.sandbox.eval(code)
}
getScreenImage(callback) {
this.imageCallback = callback
this.saveFrame = true
}
hush() {
this.s.forEach((source) => {
source.clear()
})
this.o.forEach((output) => {
this.synth.solid(0, 0, 0, 0).out(output)
})
this.synth.render(this.o[0])
this.synth.update = (dt) => {}
}
loadScript(url = "") {
const p = new Promise((res, rej) => {
var script = document.createElement("script");
script.onload = function () {
console.log(`loaded script ${url}`);
res();
};
script.onerror = (err) => {
console.log(`error loading script ${url}`, "log-error");
res()
};
script.src = url;
document.head.appendChild(script);
});
return p;
}
setResolution(width, height) {
// console.log(width, height)
this.canvas.width = width
this.canvas.height = height
this.width = width // is this necessary?
this.height = height // ?
this.sandbox.set('width', width)
this.sandbox.set('height', height)
console.log(this.width)
this.o.forEach((output) => {
output.resize(width, height)
})
this.s.forEach((source) => {
source.resize(width, height)
})
this.regl._refresh()
console.log(this.canvas.width)
}
canvasToImage (callback) {
const a = document.createElement('a')
a.style.display = 'none'
let d = new Date()
a.download = `hydra-${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}-${d.getHours()}.${d.getMinutes()}.${d.getSeconds()}.png`
document.body.appendChild(a)
var self = this
this.canvas.toBlob( (blob) => {
if(self.imageCallback){
self.imageCallback(blob)
delete self.imageCallback
} else {
a.href = URL.createObjectURL(blob)
console.log(a.href)
a.click()
}
}, 'image/png')
setTimeout(() => {
document.body.removeChild(a);
window.URL.revokeObjectURL(a.href);
}, 300);
}
_initAudio () {
const that = this
this.synth.a = new Audio({
numBins: 4,
// changeListener: ({audio}) => {
// that.a = audio.bins.map((_, index) =>
// (scale = 1, offset = 0) => () => (audio.fft[index] * scale + offset)
// )
//
// if (that.makeGlobal) {
// that.a.forEach((a, index) => {
// const aname = `a${index}`
// window[aname] = a
// })
// }
// }
})
}
// create main output canvas and add to screen
_initCanvas (canvas) {
if (canvas) {
this.canvas = canvas
this.width = canvas.width
this.height = canvas.height
} else {
this.canvas = document.createElement('canvas')
this.canvas.width = this.width
this.canvas.height = this.height
this.canvas.style.width = '100%'
this.canvas.style.height = '100%'
this.canvas.style.imageRendering = 'pixelated'
document.body.appendChild(this.canvas)
}
}
_initRegl () {
this.regl = require('regl')({
// profile: true,
canvas: this.canvas,
pixelRatio: 1//,
// extensions: [
// 'oes_texture_half_float',
// 'oes_texture_half_float_linear'
// ],
// optionalExtensions: [
// 'oes_texture_float',
// 'oes_texture_float_linear'
//]
})
// This clears the color buffer to black and the depth buffer to 1
this.regl.clear({
color: [0, 0, 0, 1]
})
this.renderAll = this.regl({
frag: `
precision ${this.precision} float;
varying vec2 uv;
uniform sampler2D tex0;
uniform sampler2D tex1;
uniform sampler2D tex2;
uniform sampler2D tex3;
void main () {
vec2 st = vec2(1.0 - uv.x, uv.y);
st*= vec2(2);
vec2 q = floor(st).xy*(vec2(2.0, 1.0));
int quad = int(q.x) + int(q.y);
st.x += step(1., mod(st.y,2.0));
st.y += step(1., mod(st.x,2.0));
st = fract(st);
if(quad==0){
gl_FragColor = texture2D(tex0, st);
} else if(quad==1){
gl_FragColor = texture2D(tex1, st);
} else if (quad==2){
gl_FragColor = texture2D(tex2, st);
} else {
gl_FragColor = texture2D(tex3, st);
}
}
`,
vert: `
precision ${this.precision} float;
attribute vec2 position;
varying vec2 uv;
void main () {
uv = position;
gl_Position = vec4(1.0 - 2.0 * position, 0, 1);
}`,
attributes: {
position: [
[-2, 0],
[0, -2],
[2, 2]
]
},
uniforms: {
tex0: this.regl.prop('tex0'),
tex1: this.regl.prop('tex1'),
tex2: this.regl.prop('tex2'),
tex3: this.regl.prop('tex3')
},
count: 3,
depth: { enable: false }
})
this.renderFbo = this.regl({
frag: `
precision ${this.precision} float;
varying vec2 uv;
uniform vec2 resolution;
uniform sampler2D tex0;
void main () {
gl_FragColor = texture2D(tex0, vec2(1.0 - uv.x, uv.y));
}
`,
vert: `
precision ${this.precision} float;
attribute vec2 position;
varying vec2 uv;
void main () {
uv = position;
gl_Position = vec4(1.0 - 2.0 * position, 0, 1);
}`,
attributes: {
position: [
[-2, 0],
[0, -2],
[2, 2]
]
},
uniforms: {
tex0: this.regl.prop('tex0'),
resolution: this.regl.prop('resolution')
},
count: 3,
depth: { enable: false }
})
}
_initOutputs (numOutputs) {
const self = this
this.o = (Array(numOutputs)).fill().map((el, index) => {
var o = new Output({
regl: this.regl,
width: this.width,
height: this.height,
precision: this.precision,
label: `o${index}`
})
// o.render()
o.id = index
self.synth['o'+index] = o
return o
})
// set default output
this.output = this.o[0]
}
_initSources (numSources) {
this.s = []
for(var i = 0; i < numSources; i++) {
this.createSource(i)
}
}
createSource (i) {
let s = new Source({regl: this.regl, pb: this.pb, width: this.width, height: this.height, label: `s${i}`})
this.synth['s' + this.s.length] = s
this.s.push(s)
return s
}
_generateGlslTransforms () {
var self = this
this.generator = new Generator({
defaultOutput: this.o[0],
defaultUniforms: this.o[0].uniforms,
extendTransforms: this.extendTransforms,
changeListener: ({type, method, synth}) => {
if (type === 'add') {
self.synth[method] = synth.generators[method]
if(self.sandbox) self.sandbox.add(method)
} else if (type === 'remove') {
// what to do here? dangerously deleting window methods
//delete window[method]
}
// }
}
})
this.synth.setFunction = this.generator.setFunction.bind(this.generator)
}
_render (output) {
if (output) {
this.output = output
this.isRenderingAll = false
} else {
this.isRenderingAll = true
}
}
// dt in ms
tick (dt, uniforms) {
this.sandbox.tick()
if(this.detectAudio === true) this.synth.a.tick()
// let updateInterval = 1000/this.synth.fps // ms
this.sandbox.set('time', this.synth.time += dt * 0.001 * this.synth.speed)
this.timeSinceLastUpdate += dt
if(!this.synth.fps || this.timeSinceLastUpdate >= 1000/this.synth.fps) {
// console.log(1000/this.timeSinceLastUpdate)
this.synth.stats.fps = Math.ceil(1000/this.timeSinceLastUpdate)
if(this.synth.update) {
try { this.synth.update(this.timeSinceLastUpdate) } catch (e) { console.log(e) }
}
// console.log(this.synth.speed, this.synth.time)
for (let i = 0; i < this.s.length; i++) {
this.s[i].tick(this.synth.time)
}
// console.log(this.canvas.width, this.canvas.height)
for (let i = 0; i < this.o.length; i++) {
this.o[i].tick({
time: this.synth.time,
mouse: this.synth.mouse,
bpm: this.synth.bpm,
resolution: [this.canvas.width, this.canvas.height]
})
}
if (this.isRenderingAll) {
this.renderAll({
tex0: this.o[0].getCurrent(),
tex1: this.o[1].getCurrent(),
tex2: this.o[2].getCurrent(),
tex3: this.o[3].getCurrent(),
resolution: [this.canvas.width, this.canvas.height]
})
} else {
this.renderFbo({
tex0: this.output.getCurrent(),
resolution: [this.canvas.width, this.canvas.height]
})
}
this.timeSinceLastUpdate = 0
}
if(this.saveFrame === true) {
this.canvasToImage()
this.saveFrame = false
}
// this.regl.poll()
}
}
module.exports = HydraRenderer