|
50 | 50 | that.opts = opts |
51 | 51 | that.selector = opts.selector |
52 | 52 | that.fps = opts.fps || 60 |
53 | | - |
54 | 53 | that.onError = opts.onError || noop |
55 | 54 | that.onMouseover = opts.onMouseover || noop |
56 | 55 | that.onMouseout = opts.onMouseout || noop |
| 56 | + that.onUpdate = opts.onUpdate || noop |
57 | 57 | that.buflen = opts.buflen || 1024 |
58 | 58 | that.buf = new Uint8Array(that.buflen) |
59 | 59 |
|
60 | 60 | supportAudio = that.initRender() |
61 | 61 | .setupStream() |
62 | | - |
63 | 62 | supportAudio && supportAudio.loop() |
64 | 63 |
|
65 | 64 | return that |
|
75 | 74 | then = +new Date |
76 | 75 |
|
77 | 76 | return function loop() { |
78 | | - |
79 | 77 | rAF(loop) |
80 | | - |
81 | 78 | var now = +new Date, |
82 | 79 | delta = now - then |
83 | | - |
84 | 80 | if (delta > interval) { |
85 | 81 | then = now - (delta % interval) |
86 | 82 | that.updateRender() |
|
94 | 90 | */ |
95 | 91 | fn.initRender = function() { |
96 | 92 | var that = this |
| 93 | + |
97 | 94 | that.wrap = d3.select(that.selector) |
98 | 95 | that.width = parseFloat(that.opts.width || that.wrap.style("width")) |
99 | 96 | that.height = parseFloat(that.opts.height || that.wrap.style("height")) |
100 | 97 |
|
101 | 98 | that.xScale = d3.scale.linear() |
102 | 99 | .domain([0, that.buflen]) |
103 | 100 | .rangeRound([0, that.width]) |
104 | | - |
105 | 101 | that.yScale = d3.scale.linear() |
106 | 102 | .domain([0, 255]) |
107 | 103 | .rangeRound([that.height, 0]) |
|
114 | 110 | .on({ |
115 | 111 | mouseover: function(e) { |
116 | 112 | that.onMouseover.call(that, e) |
117 | | - } |
118 | | - |
119 | | - , |
| 113 | + }, |
120 | 114 | mouseout: function(e) { |
121 | 115 | that.onMouseout.call(that, e) |
122 | 116 | } |
|
128 | 122 | .data(that.buf) |
129 | 123 | .enter() |
130 | 124 | .append("line") |
131 | | - |
132 | 125 | return that |
133 | 126 | } |
134 | 127 |
|
|
138 | 131 | */ |
139 | 132 | fn.setupStream = function() { |
140 | 133 | var that = this, |
141 | | - nav = navigator |
142 | | - |
143 | | - , audio = WIN.AudioContext || WIN.webkitAudioContext |
144 | | - |
145 | | - , userMedia = nav.getUserMedia || nav.webkitGetUserMedia || nav.mozGetUserMedia |
| 134 | + nav = navigator, |
| 135 | + audio = WIN.AudioContext || WIN.webkitAudioContext, |
| 136 | + userMedia = nav.getUserMedia || nav.webkitGetUserMedia || nav.mozGetUserMedia |
146 | 137 |
|
147 | 138 | // First detect `audio` / `getUserMedia` support. |
148 | 139 | if (!audio || !userMedia) return that.onError() |
149 | 140 |
|
150 | | - audio = new audio() |
151 | | - |
152 | 141 | // Create audio analyser. |
| 142 | + audio = new audio() |
153 | 143 | that.analyser = audio.createAnalyser() |
154 | 144 | that.analyser.fftSize = that.buflen |
155 | 145 |
|
|
162 | 152 | } catch (e) { |
163 | 153 | return that.onError() |
164 | 154 | } |
165 | | - |
166 | 155 | return that |
167 | 156 | } |
168 | 157 |
|
|
173 | 162 | fn.updateRender = function() { |
174 | 163 | var that = this |
175 | 164 | if (!that.analyser) return |
176 | | - |
177 | 165 | that.analyser.getByteTimeDomainData(that.buf) |
178 | | - |
179 | 166 | that.waveforms && that.waveforms.data(that.buf) |
180 | 167 | .attr({ |
181 | 168 | "x1": function(d, i) { |
|
191 | 178 | return that.yScale(d) |
192 | 179 | } |
193 | 180 | }) |
| 181 | + that.onUpdate(that, that.buf) |
194 | 182 | return that |
195 | 183 | } |
196 | 184 |
|
|
0 commit comments