-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
58 lines (45 loc) · 1.58 KB
/
index.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
var Analyser = require('web-audio-analyser')
var Texture = require('gl-texture2d')
var ndarray = require('ndarray')
module.exports = GLAudioAnalyser
function GLAudioAnalyser(gl, audio, ctx) {
if (!(this instanceof GLAudioAnalyser))
return new GLAudioAnalyser(gl, audio, ctx)
this.gl = gl
this.audio = audio
this.ctx = ctx || new AudioContext
this.waa = Analyser(this.audio, this.ctx)
var size = (this.waa.analyser[0] || this.waa.analyser).frequencyBinCount
this.waveNda = ndarray(new Float32Array(size), [size, 1])
this.waveTex = Texture(gl, this.waveNda, { float: true })
this.waveFlt = this.waveNda.data
this.freqNda = ndarray(new Float32Array(size), [size, 1])
this.freqTex = Texture(gl, this.freqNda, { float: true })
this.freqFlt = this.freqNda.data
}
GLAudioAnalyser.prototype.waveform = function(channel) {
return this.waa.waveform(null, channel)
}
GLAudioAnalyser.prototype.frequencies = function(channel) {
return this.waa.frequencies(null, channel)
}
GLAudioAnalyser.prototype.bindWaveform = function(index) {
var wave = this.waveform()
var waveFlt = this.waveFlt
for (var i = 0; i < wave.length; i++) {
waveFlt[i] = (wave[i] - 128) / 128
}
var retVal = this.waveTex.bind(index)
this.waveTex.setPixels(this.waveNda)
return retVal
}
GLAudioAnalyser.prototype.bindFrequencies = function(index) {
var freq = this.frequencies()
var freqFlt = this.freqFlt
for (var i = 0; i < freq.length; i++) {
freqFlt[i] = freq[i] / 256
}
var retVal = this.freqTex.bind(index)
this.freqTex.setPixels(this.freqNda)
return retVal
}