Fix EstimatedWaveform amplitudes (#1939)

* Fix range

* Add data range & cleanup

* Fix preview tests
This commit is contained in:
Alfonso Grillo
2023-10-23 16:42:56 +02:00
committed by GitHub
parent 68442d5cd9
commit 30e6d823c5
6 changed files with 30 additions and 24 deletions
@@ -17,29 +17,32 @@
import SwiftUI
struct EstimatedWaveform: Equatable, Hashable {
static let dataRange: ClosedRange<UInt16> = 0...1024
let data: [UInt16]
}
extension EstimatedWaveform {
func normalisedData(keepSamplesCount: Int) -> [Float] {
guard keepSamplesCount > 0 else {
/// Maps the `data` array to Float values in the range 0...1 respecting the `Self.dataRange` limits.
/// Up to `maxSamplesCount` will be returned in the output array
func normalisedData(maxSamplesCount: Int) -> [Float] {
guard maxSamplesCount > 0 else {
return []
}
// Filter the data to keep only the expected number of samples
let originalCount = Double(data.count)
let expectedCount = Double(keepSamplesCount)
var filteredData: [UInt16] = []
if expectedCount < originalCount {
for index in 0..<keepSamplesCount {
let targetIndex = (Double(index) * (originalCount / expectedCount)).rounded()
filteredData.append(UInt16(data[Int(targetIndex)]))
}
let result: [UInt16]
if data.count > maxSamplesCount {
result = (0..<maxSamplesCount)
.map { index in
let targetIndex = Int((Double(index) * (Double(data.count) / Double(maxSamplesCount))).rounded())
return UInt16(data[targetIndex])
}
} else {
filteredData = data
result = data
}
// Normalize the sample
let max = max(1.0, filteredData.max().flatMap { Float($0) } ?? 1.0)
return filteredData.map { Float($0) / max }
// Normalize the sample in the allowed range
return result.map { Float($0) / Float(Self.dataRange.upperBound) }
}
}
@@ -79,7 +82,7 @@ struct EstimatedWaveformView: View {
if normalizedWaveformData.count == count {
return
}
normalizedWaveformData = waveform.normalisedData(keepSamplesCount: count)
normalizedWaveformData = waveform.normalisedData(maxSamplesCount: count)
}
}
@@ -118,7 +121,7 @@ private struct WaveformShape: Shape {
}
}
struct WaveformView_Previews: PreviewProvider, TestablePreview {
struct EstimatedWaveformView_Previews: PreviewProvider, TestablePreview {
static var previews: some View {
// Wrap the WaveformView in a VStack otherwise the preview test will fail (because of Prefire / GeometryReader)
VStack {
@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:1433dd84c473727cd6ecda8a37ad12e73e9c8a137503a22586d66328a9798d84
size 59769
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:2fe33a68b975f2483b8879f813f69c6b8565ea92cc512e1b469361dca5b30334
size 69043
oid sha256:1a14094723fbe08e8ebf64f6a7f567e57e8835533fcb3c2f03f806acded2af54
size 65817
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:5d32a87762fc4db710b3c698d5447dd9357a1f6cebe269df986a6bd5928aba4d
size 64543
oid sha256:f08e9a60a5e93a67714b869c77e16abd6252e96515aec9951c2fac8a206950e1
size 61376
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:2f9bd66934c88ce2d6bb0b392389b38b9ef24ebff2f78e7a53c9f1618c0f40fe
size 73049
oid sha256:cced4619918ea4c66bddbc0c3b957b6239c2b11a4b40b41c08a0f4d84da1d944
size 70256
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:90039cc5805d8f0bbfbb9d48e52831dff02f86e38c573c6a616fa80ce94f22e8
size 69596
oid sha256:6086d159d868c2e0e9d80e19453c24fca783384864b9e6660c785cd4fd7c0986
size 66757