Skip to content

Commit d6132af

Browse files
committed
upgrade the single page version
1 parent 702f99f commit d6132af

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

audio_visualizer_single_page_version.html

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@
6666
this.infoUpdateId = null, //to sotore the setTimeout ID and clear the interval
6767
this.animationId = null,
6868
this.status = 0, //flag for sound is playing 1 or stopped 0
69-
this.forceStop = false
69+
this.forceStop = false,
70+
this.allCapsReachBottom = false
7071
};
7172
Visualizer.prototype = {
7273
ini: function() {
@@ -179,8 +180,10 @@
179180
audioBufferSouceNode.stop = audioBufferSouceNode.noteOff //in old browsers use noteOn method
180181
};
181182
//stop the previous sound if any
182-
if (this.source !== null) {
183+
if (this.animationId !== null) {
183184
cancelAnimationFrame(this.animationId);
185+
}
186+
if (this.source !== null) {
184187
this.source.stop(0);
185188
}
186189
audioBufferSouceNode.start(0);
@@ -214,13 +217,24 @@
214217
var drawMeter = function() {
215218
var array = new Uint8Array(analyser.frequencyBinCount);
216219
analyser.getByteFrequencyData(array);
220+
if (that.status === 0) {
221+
//fix when some sounds end the value still not back to zero
222+
for (var i = array.length - 1; i >= 0; i--) {
223+
array[i] = 0;
224+
};
225+
allCapsReachBottom = true;
226+
for (var i = capYPositionArray.length - 1; i >= 0; i--) {
227+
allCapsReachBottom = allCapsReachBottom && (capYPositionArray[i] === 0);
228+
};
229+
if (allCapsReachBottom) {
230+
cancelAnimationFrame(that.animationId);//since the sound is top and animation finished, stop the requestAnimation to prevent potential memory leak,THIS IS VERY IMPORTANT!
231+
return;
232+
};
233+
};
217234
var step = Math.round(array.length / meterNum); //sample limited data from the total array
218235
ctx.clearRect(0, 0, cwidth, cheight);
219236
for (var i = 0; i < meterNum; i++) {
220237
var value = array[i * step];
221-
if (that.status === 0) {
222-
value = 0; //fix when some sounds end the value still not back to zero
223-
};
224238
if (capYPositionArray.length < Math.round(meterNum)) {
225239
capYPositionArray.push(value);
226240
};
@@ -235,7 +249,7 @@
235249
ctx.fillStyle = gradient; //set the filllStyle to gradient for a better look
236250
ctx.fillRect(i * 12 /*meterWidth+gap*/ , cheight - value + capHeight, meterWidth, cheight); //the meter
237251
}
238-
this.animationId = requestAnimationFrame(drawMeter);
252+
that.animationId = requestAnimationFrame(drawMeter);
239253
}
240254
this.animationId = requestAnimationFrame(drawMeter);
241255
},
@@ -246,7 +260,6 @@
246260
return;
247261
};
248262
this.status = 0;
249-
console.log('audio ended');
250263
var text = 'HTML5 Audio API showcase | An Audio Viusalizer';
251264
document.getElementById('fileWrapper').style.opacity = 1;
252265
document.getElementById('info').innerHTML = text;

0 commit comments

Comments
 (0)