-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.coffee
565 lines (445 loc) · 14.1 KB
/
test.coffee
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
class Alien
grid: null
cellSize: 30
numberOfRows: 10
numberOfColumns: 10
halfColumns: 0
colReflected: 0
ticks: 0
canvas: null
drawingContext: null
canvasWidth: 0
canvasHeight: 0
constructor: () ->
@halfColumns = @numberOfColumns / 2
@canvasWidth = @cellSize * (@numberOfColumns + 2)
@canvasHeight = @cellSize * (@numberOfRows + 2)
@canvas = document.createElement 'canvas'
@drawingContext = @canvas.getContext '2d'
@canvas.height = @canvasWidth
@canvas.width = @canvasHeight
@canvas.aliens = { x: 0, y: 0 }
# console.log "canvas:::", @canvas
clearCanvas: ->
@drawingContext.clearRect 0, 0, @canvasWidth, @canvasHeight
# @drawingContext.fillStyle = "red"
# @drawingContext.fillRect 0,0, @canvasWidth, @canvasHeight
seed: ->
@grid = []
for row in [0...@numberOfRows]
@grid[row] = []
for column in [0...@halfColumns]
seedCell = @createSeedCell column
@grid[row][column] = seedCell
createSeedCell: (probability) ->
chance = Math.random()
cutoff = (probability + 1) / (@halfColumns + 1)
tobe = chance < cutoff
# console.log chance, cutoff, tobe
tobe
generateAlien: () ->
r = (m) => ~~(Math.random() * m + 1)
q = () =>
l = r(11)
g = r(5)
a = []
i = 0
while i < l
j = i * g
a.push( j )
a.unshift(-j) if i
i++
a
{
x: q()
y: q()
}
drawGrid: (pixels) ->
@canvas.aliens = @generateAlien()
r = ~~(Math.random() * 128 + 64)
g = ~~(Math.random() * 128 + 64)
b = ~~(Math.random() * 128 + 64)
colourLine = "rgba(#{r}, #{g}, #{b}, 1)"
r1 = ~~(r - Math.random() * 64)
g1 = ~~(g - Math.random() * 64)
b1 = ~~(b - Math.random() * 64)
colourFill = "rgb(#{r1}, #{g1}, #{b1})"
for row in [0...@numberOfRows]
for column in [0...@numberOfColumns]
@drawCell row, column, colourLine, 10
for row in [0...@numberOfRows]
for column in [0...@numberOfColumns]
@drawCell row, column, colourFill, 0
drawCell: (y, x, fillStyle, strokeWidth) ->
if x >= @halfColumns
colReflected = @numberOfColumns - x - 1
else
colReflected = x
isOn = @grid[y][colReflected]
if isOn
@drawingContext.fillStyle = fillStyle
@drawingContext.fillRect (1+x) * @cellSize - strokeWidth,
(1+y) * @cellSize - strokeWidth,
@cellSize + strokeWidth * 2,
@cellSize + strokeWidth * 2
getAlien: =>
# console.log "getAlien", @canvas
@canvas
step: =>
# if @ticks % 40 == 0
# @change()
# @ticks++
change: ->
@clearCanvas()
@seed()
@drawGrid()
window.Alien = Aliencon = console
class Analyse
waves: null
node: null
analyser: null
audio: null
frameBufferSize: Math.pow 2, 11
channels: 4
bufferSize: 0
signal: null
fft: null
amplitude: 0
spectrum : []
constructor: () ->
@bufferSize = @frameBufferSize / @channels
@signal = new Float32Array(@bufferSize)
@fft = new FFT(@bufferSize, 44100)
@newAudioContext()
# @loadAudio 'music/usee.mp3'
@loadAudio 'https://dl.dropboxusercontent.com/u/729503/Codepen/usee.mp3'
con.log "Analyse constructor", @signal.length
newAudioContext: ->
fok = (f) =>
window[f]? # and typeof window[f] is 'function' # not a function in safari!
@waves = false
if fok('AudioContext')
con.log("AudioContext")
@waves = new AudioContext()
else if (fok('webkitAudioContext'))
con.log("webkitAudioContext")
@waves = new webkitAudioContext()
con.log "waves", @waves
loadAudio: (url) ->
con.log "loadAudio", url, @waves
if @audio
@audio.remove()
if @node
@node.disconnect()
@audio = new Audio()
@audio.crossOrigin = "anonymous"
@audio.preload = 'auto'
# @audio.loop = true
@audio.controls = true
document.body.appendChild @audio
if @waves
con.log("waves created")
@audio.addEventListener( 'canplay', ( e ) => @setupAudioNodes(e) )
else
# @audio.addEventListener('canplay', tryMoz, false)
@audio.src = url
# waves.playbackRate = 1000
setupAudioNodes: (e) ->
con.log "setupAudioNodes"
unless @analyser?
@analyser = @analyser || @waves.createScriptProcessor( @bufferSize, 2, 2 )
@analyser.onaudioprocess = @processAudio
@node = @waves.createMediaElementSource( @audio )
@node.connect(@analyser)
# gainNode = @node.context.createGain()
# @node.connect( gainNode )
# gainNode.connect( @analyser )
# gainNode.gain.value = 0
# filter = @node.context.createBiquadFilter()
# @node.connect( filter )
# filter.connect( @analyser );
# filter.type = 0 # // Low-pass filter. See BiquadFilterNode docs
# filter.frequency.value = 440 # Set cutoff to 440 HZ
# window.addEventListener( 'mousemove', ( e ) =>
# filter.frequency.value = e.clientX * 2
# filter.Q.value = e.clientY / 10
# )
@analyser.connect(@waves.destination)
@audio.play()
# con.log "node", @node
# con.log "analyser", @analyser
processAudio: (e) =>
# con.log "processAudio"
inputArrayL = e.inputBuffer.getChannelData(0)
inputArrayR = e.inputBuffer.getChannelData(1)
outputArrayL = e.outputBuffer.getChannelData(0)
outputArrayR = e.outputBuffer.getChannelData(1)
# n = bufferSize
for i in [0...@bufferSize]
outputArrayL[i] = inputArrayL[i]
outputArrayR[i] = inputArrayR[i]
@signal[i] = (inputArrayL[i] + inputArrayR[i]) / 2
@fft.forward(@signal)
@fftChanged()
fftChanged: ->
specLength = @fft.spectrum.length
magSum = 0
for i in [0...specLength]
magSum += @fft.spectrum[i]
@amplitude = 100 * magSum / specLength
step: =>
specLength = @fft.spectrum.length # should be @signal.length / 2
totalBands = specLength / 32
@spectrum = []
currentBand = -1
for i in [0...specLength]
if i % totalBands == 0
bandTotal = 0
currentBand++
bandTotal += @fft.spectrum[i]
if i % totalBands == totalBands - 1
@spectrum[currentBand] = bandTotal
getAnalysis: ->
{
spectrum: @spectrum,
amplitude: @amplitude,
waveform: @signal
}
window.Analyse = Analyse
//------------------------------------------//
//-------- STUFF FOR AUDIO ANALYSIS --------//
function FourierTransform(bufferSize, sampleRate)
{
this.bufferSize = bufferSize;
this.sampleRate = sampleRate;
this.bandwidth = bufferSize * sampleRate;
this.spectrum = new Float32Array(bufferSize/2);
this.real = new Float32Array(bufferSize);
this.imag = new Float32Array(bufferSize);
this.peakBand = 0;
this.peak = 0;
this.getBandFrequency = function(index)
{
return this.bandwidth * index + this.bandwidth / 2;
};
this.calculateSpectrum = function()
{
var spectrum = this.spectrum,
real = this.real,
imag = this.imag,
bSi = 2 / this.bufferSize,
rval, ival, mag;
this.peak = this.peakBand = 0;
for (var i = 0, N = bufferSize*0.5; i < N; i++)
{
rval = real[i];
ival = imag[i];
mag = bSi * Math.sqrt(rval * rval + ival * ival);
if (mag > this.peak)
{
this.peakBand = i;
this.peak = mag;
}
spectrum[i] = mag;
}
};
}
function FFT(bufferSize, sampleRate)
{
FourierTransform.call(this, bufferSize, sampleRate);
this.reverseTable = new Uint32Array(bufferSize);
var limit = 1;
var bit = bufferSize >> 1;
var i;
while (limit < bufferSize)
{
for (i = 0; i < limit; i++)
this.reverseTable[i + limit] = this.reverseTable[i] + bit;
limit = limit << 1;
bit = bit >> 1;
}
this.sinTable = new Float32Array(bufferSize);
this.cosTable = new Float32Array(bufferSize);
for (i = 0; i < bufferSize; i++)
{
this.sinTable[i] = Math.sin(-Math.PI/i);
this.cosTable[i] = Math.cos(-Math.PI/i);
}
}
FFT.prototype.forward = function(buffer)
{
var bufferSize = this.bufferSize,
cosTable = this.cosTable,
sinTable = this.sinTable,
reverseTable = this.reverseTable,
real = this.real,
imag = this.imag,
spectrum = this.spectrum;
var k = Math.floor(Math.log(bufferSize) / Math.LN2);
if (Math.pow(2, k) !== bufferSize) { throw "Invalid buffer size, must be a power of 2."; }
if (bufferSize !== buffer.length) { throw "Supplied buffer is not the same size as defined FFT. FFT Size: " + bufferSize + " Buffer Size: " + buffer.length; }
var halfSize = 1,
phaseShiftStepReal,
phaseShiftStepImag,
currentPhaseShiftReal,
currentPhaseShiftImag,
off,
tr,
ti,
tmpReal,
i;
for (i = 0; i < bufferSize; i++)
{
real[i] = buffer[reverseTable[i]];
imag[i] = 0;
}
while (halfSize < bufferSize)
{
phaseShiftStepReal = cosTable[halfSize];
phaseShiftStepImag = sinTable[halfSize];
currentPhaseShiftReal = 1;
currentPhaseShiftImag = 0;
for (var fftStep = 0; fftStep < halfSize; fftStep++)
{
i = fftStep;
while (i < bufferSize)
{
off = i + halfSize;
tr = (currentPhaseShiftReal * real[off]) - (currentPhaseShiftImag * imag[off]);
ti = (currentPhaseShiftReal * imag[off]) + (currentPhaseShiftImag * real[off]);
real[off] = real[i] - tr;
imag[off] = imag[i] - ti;
real[i] += tr;
imag[i] += ti;
i += halfSize << 1;
}
tmpReal = currentPhaseShiftReal;
currentPhaseShiftReal = (tmpReal * phaseShiftStepReal) - (currentPhaseShiftImag * phaseShiftStepImag);
currentPhaseShiftImag = (tmpReal * phaseShiftStepImag) + (currentPhaseShiftImag * phaseShiftStepReal);
}
halfSize = halfSize << 1;
}
return this.calculateSpectrum();
};
window.FFT = FFT;con = console
class Music
analyser: null
aliendude: null
canvas: null
pixels: null
canvasWidth: 1000
canvasHeight: 500
centreX: 0
centreY: 0
scale: 1
float: 20
constructor: ->
con.log "Music constructor"
@createCanvas()
@analyser = new Analyse( @pixels, @centreX, @centreY )
@aliendude = new Alien( )
if window.requestAnimationFrame
con.log "native requestAnimationFrame"
else
con.log "creating requestAnimationFrame"
window.requestAnimationFrame = window.webkitRequestAnimationFrame or window.mozRequestAnimationFrame or window.oRequestAnimationFrame or window.msRequestAnimationFrame
# function(callback, element){
# window.setTimeout(callback, 1000 / 60);
# };
@step()
createCanvas: ->
@centreX = @canvasWidth / 2
@centreY = @canvasHeight / 2
@canvas = document.createElement 'canvas'
document.body.appendChild @canvas
@pixels = @canvas.getContext '2d'
@canvas.width = @canvasWidth
@canvas.height = @canvasHeight
clearCanvas: ->
#pixels.globalCompositeOperation = 'source-atop'
@pixels.fillStyle = "rgb(0, 0, 0)"
#@pixels.fillStyle = "rgba(0, 0, 0, 0.4)"
@pixels.fillRect 0, 0, @canvasWidth, @canvasHeight
#@pixels.globalCompositeOperation = 'lighter'
alienScale: 2
alienRotate: 0.01
step: =>
@clearCanvas()
@analyser.step()
@aliendude.step()
anal = @analyser.getAnalysis()
spectrum = anal.spectrum
amplitude = anal.amplitude
waveform = anal.waveform
# draw amplitude
@pixels.fillStyle = "rgb(0, 20, 40)"
@pixels.fillRect( 0, @canvasHeight, @canvasWidth, -amplitude * @canvasHeight)
# draw spectrum
specLength = spectrum.length
bandWidth = @canvasWidth / specLength
padding = 5
for i in [0...specLength]
c = ~~(i / specLength * 55)
r = 100
g = 200 + c
b = 255 - c
level = spectrum[i]
@pixels.fillStyle = "rgba(#{r}, #{g}, #{b}, 0.5)"
@pixels.fillRect( i * bandWidth + padding, @canvasHeight, bandWidth - padding * 2, -level * @centreY)
# draw waveform
waveformLength = waveform.length
bandWidth = @canvasWidth / waveformLength
@pixels.beginPath();
for i in [0...waveformLength]
#@pixels.fillStyle = "rgba(255, 255, 255, 0.5)"
#@pixels.fillRect( i * bandWidth, @centreY, bandWidth, waveform[i] * @centreY)
@pixels.strokeStyle = '#0fe';
@pixels.lineWidth = 1;
x = i * bandWidth
y = @centreY + waveform[i] * @centreY
if i == 0
@pixels.moveTo( x, y )
else
@pixels.lineTo( x,y )
@pixels.stroke();
# draw alien
@float += @alienRotate
#@scale += 0.01
@scale = spectrum[0]
if @scale > @alienScale * 4
@aliendude.change()
@alienScale = @scale
@alienRotate = (Math.random() - 0.5) * 0.1
else
@alienScale *= 0.9
sc = @alienScale * 1 #+ (Math.sin @scale) * 0.5
#sc = (Math.sin @scale) * 0.5
img = @aliendude.getAlien()
@pixels.save()
@pixels.translate( @centreX, @centreY )
@pixels.rotate( @float )
@pixels.scale( sc, sc )
@pixels.translate( -img.width / 2, -img.height / 2 )
# i think this pattern method 'leaks' like a motherfuck
# pat = @pixels.createPattern(img,"repeat");
# @pixels.rect(0,0, @canvasWidth, @canvasHeight )
# @pixels.fillStyle = pat
# @pixels.fill()
for x in img.aliens.x
for y in img.aliens.y
@pixels.drawImage( img, img.width * x, img.height * y)
@pixels.restore()
# attempt at 3d, but shearing of only 2 triangle subdivisions and this will get costly really fast.
# corners = [
# {x:100,y:100,u:0,v:0},
# {x:300,y:50,u:img.width,v:0},
# {x:350,y:300,u:img.width,v:img.height},
# {x:200,y:400,u:0,v:img.height}
# ]
# for c in corners
# @pixels.fillStyle = "#f00"
# @pixels.fillRect( c.x, c.y, 5, 5 )
# textureMap( @pixels, img, corners )
requestAnimationFrame @step
window.Music = Music
new Music()