Skip to content

Commit 7951de1

Browse files
author
yuji
committed
Add: handle & formatting
1 parent a92d9e9 commit 7951de1

File tree

5 files changed

+197
-219
lines changed

5 files changed

+197
-219
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Use these options like:
8989
| onError | `Function` | empty function | On error handle, maybe not supports `getUserMedia/Stream API` |
9090
| onMouseover | `Function` | empty function | On mouseover handle. |
9191
| onMouseout | `NumFunctionber` | empty function | On mouseout handle. |
92+
| onUpdate | `NumFunctionber` | empty function | On update handle, the way to sync audio buffer. |
9293
| buflen | `Number` | 1024 | The audio stream buffer length. |
9394

9495
## Development

demo/js/audio_waveform.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/audio_waveform.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,15 @@
5050
that.opts = opts
5151
that.selector = opts.selector
5252
that.fps = opts.fps || 60
53-
5453
that.onError = opts.onError || noop
5554
that.onMouseover = opts.onMouseover || noop
5655
that.onMouseout = opts.onMouseout || noop
56+
that.onUpdate = opts.onUpdate || noop
5757
that.buflen = opts.buflen || 1024
5858
that.buf = new Uint8Array(that.buflen)
5959

6060
supportAudio = that.initRender()
6161
.setupStream()
62-
6362
supportAudio && supportAudio.loop()
6463

6564
return that
@@ -75,12 +74,9 @@
7574
then = +new Date
7675

7776
return function loop() {
78-
7977
rAF(loop)
80-
8178
var now = +new Date,
8279
delta = now - then
83-
8480
if (delta > interval) {
8581
then = now - (delta % interval)
8682
that.updateRender()
@@ -94,14 +90,14 @@
9490
*/
9591
fn.initRender = function() {
9692
var that = this
93+
9794
that.wrap = d3.select(that.selector)
9895
that.width = parseFloat(that.opts.width || that.wrap.style("width"))
9996
that.height = parseFloat(that.opts.height || that.wrap.style("height"))
10097

10198
that.xScale = d3.scale.linear()
10299
.domain([0, that.buflen])
103100
.rangeRound([0, that.width])
104-
105101
that.yScale = d3.scale.linear()
106102
.domain([0, 255])
107103
.rangeRound([that.height, 0])
@@ -114,9 +110,7 @@
114110
.on({
115111
mouseover: function(e) {
116112
that.onMouseover.call(that, e)
117-
}
118-
119-
,
113+
},
120114
mouseout: function(e) {
121115
that.onMouseout.call(that, e)
122116
}
@@ -128,7 +122,6 @@
128122
.data(that.buf)
129123
.enter()
130124
.append("line")
131-
132125
return that
133126
}
134127

@@ -138,18 +131,15 @@
138131
*/
139132
fn.setupStream = function() {
140133
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
146137

147138
// First detect `audio` / `getUserMedia` support.
148139
if (!audio || !userMedia) return that.onError()
149140

150-
audio = new audio()
151-
152141
// Create audio analyser.
142+
audio = new audio()
153143
that.analyser = audio.createAnalyser()
154144
that.analyser.fftSize = that.buflen
155145

@@ -162,7 +152,6 @@
162152
} catch (e) {
163153
return that.onError()
164154
}
165-
166155
return that
167156
}
168157

@@ -173,9 +162,7 @@
173162
fn.updateRender = function() {
174163
var that = this
175164
if (!that.analyser) return
176-
177165
that.analyser.getByteTimeDomainData(that.buf)
178-
179166
that.waveforms && that.waveforms.data(that.buf)
180167
.attr({
181168
"x1": function(d, i) {
@@ -191,6 +178,7 @@
191178
return that.yScale(d)
192179
}
193180
})
181+
that.onUpdate(that, that.buf)
194182
return that
195183
}
196184

dist/audio_waveform.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)