From 30e6d823c5db6c89be3d30eaa8c938a38e6930ce Mon Sep 17 00:00:00 2001 From: Alfonso Grillo Date: Mon, 23 Oct 2023 16:42:56 +0200 Subject: [PATCH] Fix EstimatedWaveform amplitudes (#1939) * Fix range * Add data range & cleanup * Fix preview tests --- .../VoiceMessages/EstimatedWaveformView.swift | 35 ++++++++++--------- .../test_estimatedWaveformView.1.png | 3 ++ .../test_voiceMessagePreviewComposer.1.png | 4 +-- .../test_voiceMessageRoomPlaybackView.1.png | 4 +-- ...st_voiceMessageRoomTimelineView.Bubble.png | 4 +-- ...est_voiceMessageRoomTimelineView.Plain.png | 4 +-- 6 files changed, 30 insertions(+), 24 deletions(-) create mode 100644 UnitTests/__Snapshots__/PreviewTests/test_estimatedWaveformView.1.png diff --git a/ElementX/Sources/Services/Timeline/TimelineItems/Items/Messages/VoiceMessages/EstimatedWaveformView.swift b/ElementX/Sources/Services/Timeline/TimelineItems/Items/Messages/VoiceMessages/EstimatedWaveformView.swift index dd4da8c53..4ae87feab 100644 --- a/ElementX/Sources/Services/Timeline/TimelineItems/Items/Messages/VoiceMessages/EstimatedWaveformView.swift +++ b/ElementX/Sources/Services/Timeline/TimelineItems/Items/Messages/VoiceMessages/EstimatedWaveformView.swift @@ -17,29 +17,32 @@ import SwiftUI struct EstimatedWaveform: Equatable, Hashable { + static let dataRange: ClosedRange = 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.. maxSamplesCount { + result = (0..