Skip to content

Instantly share code, notes, and snippets.

@jscalo
Last active December 18, 2020 18:14
Show Gist options
  • Save jscalo/9c16d5012b2e303b6971a005345f99c4 to your computer and use it in GitHub Desktop.
Save jscalo/9c16d5012b2e303b6971a005345f99c4 to your computer and use it in GitHub Desktop.
private func updateLines(samples: [Float], yMidline: CGFloat) {
let maxSampleMagnitude = max(dataProvider.summarySampleMax, -(dataProvider.summarySampleMin), Float.leastNonzeroMagnitude)
let yScalingFactor = bounds.height / 2 / CGFloat(maxSampleMagnitude)
var xPos: CGFloat = 0
let cnt = samples.count
var idx = 0
let visibleDur = dataProvider.duration / Double(viewPort.zoom)
let startTime = (Double(viewPort.startingXUnit) / Double(viewPort.xAxisUnits)) * dataProvider.duration
while idx < cnt && xPos < bounds.maxX && idx < layers.count {
let time = startTime + Double(xPos) / Double(viewPort.visibleWidth) * visibleDur
let sample = dataProvider.sample(at: time)
let layer = layers[idx]
layer.isHidden = false
let y = CGFloat(sample) * yScalingFactor
layer.frame = CGRect(x: xPos, y: yMidline - y, width: 1, height: 2 * y)
xPos += 1.0 / viewPort.screenScale
idx += 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment