Skip to content

Commit

Permalink
smoothing 2
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie committed Sep 29, 2024
1 parent 0a8f253 commit 8e3dbe9
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions Sources/LiveKit/Convenience/AudioProcessing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public class AudioVisualizeProcessor {
maxDB: Float = 32.0,
bandsCount: Int = 100,
isCentered: Bool = false,
smoothingFactor: Float = 0.1) // Smoothing factor for smoother transitions
smoothingFactor: Float = 0.2) // Smoothing factor for smoother transitions
{
self.minFrequency = minFrequency
self.maxFrequency = maxFrequency
Expand Down Expand Up @@ -173,9 +173,9 @@ public class AudioVisualizeProcessor {
normalizedBands = centerBands(normalizedBands)
}

// Smooth transition between old and new bands
// Smooth transition using an easing function
self.bands = zip(self.bands ?? [], normalizedBands).map { old, new in
old * (1.0 - smoothingFactor) + new * smoothingFactor
_smoothTransition(from: old, to: new, factor: smoothingFactor)
}
}

Expand All @@ -199,4 +199,19 @@ public class AudioVisualizeProcessor {

return centeredBands
}

/// Applies an easing function to smooth the transition.
private func _smoothTransition(from oldValue: Float, to newValue: Float, factor: Float) -> Float {
// Calculate the delta change between the old and new value
let delta = newValue - oldValue
// Apply an ease-in-out cubic easing curve
let easedFactor = _easeInOutCubic(t: factor)
// Calculate and return the smoothed value
return oldValue + delta * easedFactor
}

/// Easing function: ease-in-out cubic
private func _easeInOutCubic(t: Float) -> Float {
t < 0.5 ? 4 * t * t * t : 1 - pow(-2 * t + 2, 3) / 2
}
}

0 comments on commit 8e3dbe9

Please sign in to comment.