Created
July 20, 2013 04:45
-
-
Save chiepomme/6043905 to your computer and use it in GitHub Desktop.
This file contains 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
import javafx.scene.canvas.Canvas | |
class WaveView(width:Double, height:Double) : Canvas(width, height){ | |
fun draw(waveData:ByteArray){ | |
val c = getGraphicsContext2D()!! | |
val zoom = 2.0 | |
val height = getHeight().toDouble() | |
val byteSize = 256 | |
val originY = height / 2 | |
val moveToInitialZero = { c.moveTo(0.0, originY) } | |
val lineToInitialZero = { c.lineTo(0.0, originY) } | |
val lineToFinalZero = { c.lineTo((waveData.size-1) * zoom, originY) } | |
val calculateSampleY = { (i:Int) -> waveData[i] * height / byteSize } | |
val drawUpperHalf = { for(i in waveData.indices) c.lineTo(i * zoom, calculateSampleY(i) + originY) } | |
val drawLowerHalf = { for(i in waveData.indices.reversed()) c.lineTo(i * zoom, -calculateSampleY(i) + originY) } | |
moveToInitialZero() | |
drawUpperHalf() | |
lineToFinalZero() | |
drawLowerHalf() | |
lineToInitialZero() | |
c.fill() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment