Skip to content

Commit ac6ca08

Browse files
authored
Merge pull request #32 from Hitlabs/live-draw-right-to-left
Make sample drawing always appear to move from right to left
2 parents 1b90830 + 59e23ba commit ac6ca08

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

DSWaveformImage/DSWaveformImage/WaveformImageDrawer.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,20 @@ extension WaveformImageDrawer {
8282
lastOffset = 0
8383
}
8484

85-
if case .striped = configuration.style, samples.count >= samplesNeeded {
86-
lastOffset = (lastOffset + min(newSampleCount, samples.count - samplesNeeded)) % stripeBucket(configuration)
85+
if case .striped = configuration.style {
86+
if shouldDrawSilencePadding {
87+
lastOffset = (lastOffset + newSampleCount) % stripeBucket(configuration)
88+
} else if samples.count >= samplesNeeded {
89+
lastOffset = (lastOffset + min(newSampleCount, samples.count - samplesNeeded)) % stripeBucket(configuration)
90+
}
8791
}
8892

89-
// move the window, so that its always at the end (moves the graph after it reached the right side)
93+
// move the window, so that its always at the end (appears to move from right to left)
9094
let startSample = max(0, samples.count - samplesNeeded)
9195
let clippedSamples = Array(samples[startSample..<samples.count])
9296
let dampenedSamples = configuration.shouldDampen ? dampen(clippedSamples, with: configuration) : clippedSamples
93-
let paddedSamples = shouldDrawSilencePadding ? dampenedSamples + Array(repeating: 1, count: samplesNeeded - clippedSamples.count) : dampenedSamples
94-
97+
let paddedSamples = shouldDrawSilencePadding ? Array(repeating: 1, count: samplesNeeded - clippedSamples.count) + dampenedSamples : dampenedSamples
98+
9599
draw(on: context, from: paddedSamples, with: configuration)
96100
}
97101
}
@@ -146,7 +150,9 @@ private extension WaveformImageDrawer {
146150
continue
147151
}
148152

149-
let xPos = CGFloat(x - lastOffset) / configuration.scale
153+
let samplesNeeded = Int(configuration.size.width * configuration.scale)
154+
let xOffset = CGFloat(samplesNeeded - samples.count) / configuration.scale // When there's extra space, draw waveform on the right
155+
let xPos = (CGFloat(x - lastOffset) / configuration.scale) + xOffset
150156
let invertedDbSample = 1 - CGFloat(sample) // sample is in dB, linearly normalized to [0, 1] (1 -> -50 dB)
151157
let drawingAmplitude = max(minimumGraphAmplitude, invertedDbSample * drawMappingFactor)
152158
let drawingAmplitudeUp = positionAdjustedGraphCenter - drawingAmplitude

0 commit comments

Comments
 (0)