Skip to content

Commit 108c12f

Browse files
committed
fix jittery behavior in SwiftUI live view in analogy to 858c157
by moving new sample count tracking inside of WaveformDrawer
1 parent 2dd12fd commit 108c12f

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

DSWaveformImage/DSWaveformImage/Common/WaveformImageDrawer.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public class WaveformImageDrawer: ObservableObject {
1515
/// Makes sure we always look at the same samples while animating
1616
private var lastOffset: Int = 0
1717

18+
/// Keep track of how many samples we are adding each draw cycle
19+
private var lastSampleCount: Int = 0
20+
1821
#if compiler(>=5.5) && canImport(_Concurrency)
1922
/// Async analyzes the provided audio and renders a UIImage of the waveform data calculated by the analyzer.
2023
public func waveformImage(fromAudioAt audioAssetURL: URL,
@@ -69,12 +72,18 @@ extension WaveformImageDrawer {
6972
///
7073
/// Samples need to be normalized within interval `(0...1)`.
7174
/// Ensure context size & scale match with the configuration's size & scale.
72-
func draw(waveform samples: [Float], newSampleCount: Int, on context: CGContext, with configuration: Waveform.Configuration) {
75+
func draw(waveform samples: [Float], on context: CGContext, with configuration: Waveform.Configuration) {
7376
guard samples.count > 0 || shouldDrawSilencePadding else {
7477
return
7578
}
7679

7780
let samplesNeeded = Int(configuration.size.width * configuration.scale)
81+
82+
let newSampleCount: Int = lastSampleCount > samples.count
83+
? samples.count // this implies that we have reset drawing an are starting over
84+
: samples.count - lastSampleCount
85+
86+
lastSampleCount = samples.count
7887

7988
// Reset the cumulative lastOffset when new drawing begins
8089
if samples.count == newSampleCount {

DSWaveformImage/DSWaveformImage/SwiftUI/WaveformLiveCanvas.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ public struct WaveformLiveCanvas: View {
99
@Binding public var shouldDrawSilencePadding: Bool
1010

1111
@StateObject private var waveformDrawer = WaveformImageDrawer()
12-
@State private var lastNewSampleCount: Int = 0
1312

1413
public init(
1514
samples: Binding<[Float]>,
@@ -19,21 +18,17 @@ public struct WaveformLiveCanvas: View {
1918
_samples = samples
2019
_configuration = configuration
2120
_shouldDrawSilencePadding = shouldDrawSilencePadding
22-
lastNewSampleCount = samples.count
2321
}
2422

2523
public var body: some View {
2624
Canvas(rendersAsynchronously: true) { context, size in
2725
context.withCGContext { cgContext in
28-
waveformDrawer.draw(waveform: samples, newSampleCount: lastNewSampleCount, on: cgContext, with: configuration.with(size: size))
26+
waveformDrawer.draw(waveform: samples, on: cgContext, with: configuration.with(size: size))
2927
}
3028
}
3129
.onAppear {
3230
waveformDrawer.shouldDrawSilencePadding = shouldDrawSilencePadding
3331
}
34-
.onChange(of: samples) { [samples] newValue in
35-
lastNewSampleCount = max(0, newValue.count - samples.count)
36-
}
3732
.onChange(of: shouldDrawSilencePadding) { newValue in
3833
waveformDrawer.shouldDrawSilencePadding = newValue
3934
}

DSWaveformImage/DSWaveformImage/UIKit/WaveformLiveView.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ public class WaveformLiveView: UIView {
7373
class WaveformLiveLayer: CALayer {
7474
@NSManaged var samples: [Float]
7575

76-
private var lastNewSampleCount: Int = 0
77-
7876
var configuration = WaveformLiveView.defaultConfiguration {
7977
didSet { contentsScale = configuration.scale }
8078
}
@@ -99,19 +97,15 @@ class WaveformLiveLayer: CALayer {
9997
super.draw(in: context)
10098

10199
UIGraphicsPushContext(context)
102-
waveformDrawer.draw(waveform: samples, newSampleCount: lastNewSampleCount, on: context, with: configuration.with(size: bounds.size))
100+
waveformDrawer.draw(waveform: samples, on: context, with: configuration.with(size: bounds.size))
103101
UIGraphicsPopContext()
104-
105-
lastNewSampleCount = 0
106102
}
107103

108104
func add(_ newSamples: [Float]) {
109-
lastNewSampleCount += newSamples.count
110105
samples += newSamples
111106
}
112107

113108
func reset() {
114-
lastNewSampleCount = 0
115109
samples = []
116110
}
117111
}

0 commit comments

Comments
 (0)