mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 16:10:20 +00:00
Fix EstimatedWaveform amplitudes (#1939)
* Fix range * Add data range & cleanup * Fix preview tests
This commit is contained in:
+19
-16
@@ -17,29 +17,32 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct EstimatedWaveform: Equatable, Hashable {
|
struct EstimatedWaveform: Equatable, Hashable {
|
||||||
|
static let dataRange: ClosedRange<UInt16> = 0...1024
|
||||||
let data: [UInt16]
|
let data: [UInt16]
|
||||||
}
|
}
|
||||||
|
|
||||||
extension EstimatedWaveform {
|
extension EstimatedWaveform {
|
||||||
func normalisedData(keepSamplesCount: Int) -> [Float] {
|
/// Maps the `data` array to Float values in the range 0...1 respecting the `Self.dataRange` limits.
|
||||||
guard keepSamplesCount > 0 else {
|
/// Up to `maxSamplesCount` will be returned in the output array
|
||||||
|
func normalisedData(maxSamplesCount: Int) -> [Float] {
|
||||||
|
guard maxSamplesCount > 0 else {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter the data to keep only the expected number of samples
|
// Filter the data to keep only the expected number of samples
|
||||||
let originalCount = Double(data.count)
|
let result: [UInt16]
|
||||||
let expectedCount = Double(keepSamplesCount)
|
if data.count > maxSamplesCount {
|
||||||
var filteredData: [UInt16] = []
|
result = (0..<maxSamplesCount)
|
||||||
if expectedCount < originalCount {
|
.map { index in
|
||||||
for index in 0..<keepSamplesCount {
|
let targetIndex = Int((Double(index) * (Double(data.count) / Double(maxSamplesCount))).rounded())
|
||||||
let targetIndex = (Double(index) * (originalCount / expectedCount)).rounded()
|
return UInt16(data[targetIndex])
|
||||||
filteredData.append(UInt16(data[Int(targetIndex)]))
|
}
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
filteredData = data
|
result = data
|
||||||
}
|
}
|
||||||
// Normalize the sample
|
|
||||||
let max = max(1.0, filteredData.max().flatMap { Float($0) } ?? 1.0)
|
// Normalize the sample in the allowed range
|
||||||
return filteredData.map { Float($0) / max }
|
return result.map { Float($0) / Float(Self.dataRange.upperBound) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,7 +82,7 @@ struct EstimatedWaveformView: View {
|
|||||||
if normalizedWaveformData.count == count {
|
if normalizedWaveformData.count == count {
|
||||||
return
|
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 {
|
static var previews: some View {
|
||||||
// Wrap the WaveformView in a VStack otherwise the preview test will fail (because of Prefire / GeometryReader)
|
// Wrap the WaveformView in a VStack otherwise the preview test will fail (because of Prefire / GeometryReader)
|
||||||
VStack {
|
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
|
version https://git-lfs.github.com/spec/v1
|
||||||
oid sha256:2fe33a68b975f2483b8879f813f69c6b8565ea92cc512e1b469361dca5b30334
|
oid sha256:1a14094723fbe08e8ebf64f6a7f567e57e8835533fcb3c2f03f806acded2af54
|
||||||
size 69043
|
size 65817
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
version https://git-lfs.github.com/spec/v1
|
version https://git-lfs.github.com/spec/v1
|
||||||
oid sha256:5d32a87762fc4db710b3c698d5447dd9357a1f6cebe269df986a6bd5928aba4d
|
oid sha256:f08e9a60a5e93a67714b869c77e16abd6252e96515aec9951c2fac8a206950e1
|
||||||
size 64543
|
size 61376
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
version https://git-lfs.github.com/spec/v1
|
version https://git-lfs.github.com/spec/v1
|
||||||
oid sha256:2f9bd66934c88ce2d6bb0b392389b38b9ef24ebff2f78e7a53c9f1618c0f40fe
|
oid sha256:cced4619918ea4c66bddbc0c3b957b6239c2b11a4b40b41c08a0f4d84da1d944
|
||||||
size 73049
|
size 70256
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
version https://git-lfs.github.com/spec/v1
|
version https://git-lfs.github.com/spec/v1
|
||||||
oid sha256:90039cc5805d8f0bbfbb9d48e52831dff02f86e38c573c6a616fa80ce94f22e8
|
oid sha256:6086d159d868c2e0e9d80e19453c24fca783384864b9e6660c785cd4fd7c0986
|
||||||
size 69596
|
size 66757
|
||||||
|
|||||||
Reference in New Issue
Block a user