Skip to content

Instantly share code, notes, and snippets.

@jscalo
Last active December 18, 2020 18:14
Show Gist options
  • Select an option

  • Save jscalo/9c16d5012b2e303b6971a005345f99c4 to your computer and use it in GitHub Desktop.

Select an option

Save jscalo/9c16d5012b2e303b6971a005345f99c4 to your computer and use it in GitHub Desktop.

Revisions

  1. jscalo renamed this gist Dec 18, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. jscalo created this gist Dec 18, 2020.
    23 changes: 23 additions & 0 deletions waveform.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    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
    }
    }