Created
November 21, 2015 04:38
-
-
Save notthetup/2d6f9e2a1fd08946bba3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_renderWaveform: function(buffer){ | |
var samples = buffer.getChannelData(0); | |
var container = this.$$('paper-item-body'); | |
var canvas = this.$$('#waveform'); | |
var width = container.offsetWidth; | |
var height = Math.min(container.offsetHeight, 76); | |
var ratio = this._getBackingScale(canvas); | |
canvas.width = Math.floor(width * ratio); | |
canvas.height = Math.floor(height * ratio); | |
canvas.style.width = Math.floor(width) + 'px'; | |
canvas.style.height = Math.floor(height) + 'px'; | |
canvas.getContext('2d').setTransform(ratio, 0, 0, ratio, 0, 0); | |
var samplesPerPixel = Math.floor(samples.length/width); | |
var ctx = canvas.getContext('2d'); | |
ctx.clearRect(0, 0, width, height); | |
ctx.strokeStyle = this.waveformColor || 'rgba(0,0,0,0.4)'; | |
ctx.translate(0, height/2); | |
for (var pixelIndex = 0;pixelIndex < width; pixelIndex++){ | |
var sumSq = 0; | |
var sampleCount = 0; | |
var dataIndex = pixelIndex*samplesPerPixel; | |
for (var sampleIndex = 0; sampleIndex < samplesPerPixel; sampleIndex++){ | |
var thisSampleIndex = dataIndex + sampleIndex; | |
if (thisSampleIndex < samples.length){ | |
sumSq += samples[thisSampleIndex] * samples[thisSampleIndex]; | |
sampleCount++; | |
} | |
} | |
var x = pixelIndex; | |
var y = Math.sqrt(sumSq/sampleCount) * (height/2); | |
ctx.beginPath(); | |
ctx.moveTo(x, -y); | |
ctx.lineTo(x, y); | |
ctx.stroke(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment