Skip to content

Commit 86a8634

Browse files
committed
Move parts of visualization code into components
1 parent 027fea7 commit 86a8634

File tree

1 file changed

+2
-57
lines changed

1 file changed

+2
-57
lines changed

Sources/LiveKit/Convenience/AudioProcessing.swift

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ public class AudioVisualizeProcessor {
107107
public let minDB: Float
108108
public let maxDB: Float
109109
public let bandsCount: Int
110-
public let isCentered: Bool
111-
public let smoothingFactor: Float
112110

113111
private var bands: [Float]?
114112

@@ -121,17 +119,13 @@ public class AudioVisualizeProcessor {
121119
maxFrequency: Float = 8000,
122120
minDB: Float = -32.0,
123121
maxDB: Float = 32.0,
124-
bandsCount: Int = 100,
125-
isCentered: Bool = false,
126-
smoothingFactor: Float = 0.3) // Smoothing factor for smoother transitions
122+
bandsCount: Int = 100)
127123
{
128124
self.minFrequency = minFrequency
129125
self.maxFrequency = maxFrequency
130126
self.minDB = minDB
131127
self.maxDB = maxDB
132128
self.bandsCount = bandsCount
133-
self.isCentered = isCentered
134-
self.smoothingFactor = smoothingFactor
135129

136130
processor = FFTProcessor(bufferSize: Self.bufferSize)
137131
bands = [Float](repeating: 0.0, count: bandsCount)
@@ -160,58 +154,9 @@ public class AudioVisualizeProcessor {
160154
let headroom = maxDB - minDB
161155

162156
// Normalize magnitudes (already in decibels)
163-
var normalizedBands = bands.magnitudes.map { magnitude in
157+
return bands.magnitudes.map { magnitude in
164158
let adjustedMagnitude = max(0, magnitude + abs(minDB))
165159
return min(1.0, adjustedMagnitude / headroom)
166160
}
167-
168-
// If centering is enabled, rearrange the normalized bands
169-
if isCentered {
170-
normalizedBands.sort(by: >)
171-
normalizedBands = centerBands(normalizedBands)
172-
}
173-
174-
// Smooth transition using an easing function
175-
self.bands = zip(self.bands ?? [], normalizedBands).map { old, new in
176-
_smoothTransition(from: old, to: new, factor: smoothingFactor)
177-
}
178-
179-
return self.bands
180-
}
181-
182-
/// Centers the sorted bands by placing higher values in the middle.
183-
private func centerBands(_ sortedBands: [Float]) -> [Float] {
184-
var centeredBands = [Float](repeating: 0, count: sortedBands.count)
185-
var leftIndex = sortedBands.count / 2
186-
var rightIndex = leftIndex
187-
188-
for (index, value) in sortedBands.enumerated() {
189-
if index % 2 == 0 {
190-
// Place value to the right
191-
centeredBands[rightIndex] = value
192-
rightIndex += 1
193-
} else {
194-
// Place value to the left
195-
leftIndex -= 1
196-
centeredBands[leftIndex] = value
197-
}
198-
}
199-
200-
return centeredBands
201-
}
202-
203-
/// Applies an easing function to smooth the transition.
204-
private func _smoothTransition(from oldValue: Float, to newValue: Float, factor: Float) -> Float {
205-
// Calculate the delta change between the old and new value
206-
let delta = newValue - oldValue
207-
// Apply an ease-in-out cubic easing curve
208-
let easedFactor = _easeInOutCubic(t: factor)
209-
// Calculate and return the smoothed value
210-
return oldValue + delta * easedFactor
211-
}
212-
213-
/// Easing function: ease-in-out cubic
214-
private func _easeInOutCubic(t: Float) -> Float {
215-
t < 0.5 ? 4 * t * t * t : 1 - pow(-2 * t + 2, 3) / 2
216161
}
217162
}

0 commit comments

Comments
 (0)