1414 * limitations under the License.
1515 */
1616
17- import AVFoundation
17+ @ preconcurrency import AVFoundation
1818import LiveKit
1919
20- public class AudioProcessor : ObservableObject , AudioRenderer {
20+ @MainActor
21+ public final class AudioProcessor : ObservableObject , AudioRenderer {
2122 public let isCentered : Bool
2223 public let smoothingFactor : Float
2324
24- // Normalized to 0.0-1.0 range.
25- @Published public var bands : [ Float ]
25+ /// Normalized to 0.0-1.0 range.
26+ @Published public private ( set ) var bands : [ Float ]
2627
27- private let _processor : AudioVisualizeProcessor
28- private weak var _track : AudioTrack ?
28+ private let processor : AudioVisualizeProcessor
29+ private weak var track : AudioTrack ?
2930
3031 public init ( track: AudioTrack ? ,
3132 bandCount: Int ,
3233 isCentered: Bool = true ,
33- smoothingFactor: Float = 0.3 )
34+ smoothingFactor: Float = 0.25 )
3435 {
3536 self . isCentered = isCentered
3637 self . smoothingFactor = smoothingFactor
3738 bands = Array ( repeating: 0.0 , count: bandCount)
3839
39- _processor = AudioVisualizeProcessor ( bandsCount: bandCount)
40- _track = track
41- _track? . add ( audioRenderer: self )
40+ processor = AudioVisualizeProcessor ( bandsCount: bandCount)
41+
42+ self . track = track
43+ track? . add ( audioRenderer: self )
4244 }
4345
4446 deinit {
45- _track ? . remove ( audioRenderer: self )
47+ track ? . remove ( audioRenderer: self )
4648 }
4749
48- public func render( pcmBuffer: AVAudioPCMBuffer ) {
49- let newBands = _processor. process ( pcmBuffer: pcmBuffer)
50- guard var newBands else { return }
51-
52- // If centering is enabled, rearrange the normalized bands
53- if isCentered {
54- newBands. sort ( by: > )
55- newBands = centerBands ( newBands)
56- }
50+ public nonisolated func render( pcmBuffer: AVAudioPCMBuffer ) {
51+ Task {
52+ let newBands = await processor. process ( pcmBuffer: pcmBuffer)
53+ guard var newBands else { return }
5754
58- DispatchQueue . main. async { [ weak self] in
59- guard let self else { return }
55+ // If centering is enabled, rearrange the normalized bands
56+ if isCentered {
57+ newBands. sort ( by: > )
58+ newBands = Self . centerBands ( newBands)
59+ }
6060
61- self . bands = zip ( self . bands, newBands) . map { old, new in
62- self . _smoothTransition ( from: old, to: new, factor: self . smoothingFactor)
61+ await MainActor . run { [ newBands] in
62+ bands = zip ( bands, newBands) . map { old, new in
63+ Self . smoothTransition ( from: old, to: new, factor: smoothingFactor)
64+ }
6365 }
6466 }
6567 }
6668
6769 // MARK: - Private
6870
6971 /// Centers the sorted bands by placing higher values in the middle.
70- @inline ( __always) private func centerBands( _ sortedBands: [ Float ] ) -> [ Float ] {
72+ @inline ( __always) private nonisolated static func centerBands( _ sortedBands: [ Float ] ) -> [ Float ] {
7173 var centeredBands = [ Float] ( repeating: 0 , count: sortedBands. count)
7274 var leftIndex = sortedBands. count / 2
7375 var rightIndex = leftIndex
@@ -88,17 +90,17 @@ public class AudioProcessor: ObservableObject, AudioRenderer {
8890 }
8991
9092 /// Applies an easing function to smooth the transition.
91- @inline ( __always) private func _smoothTransition ( from oldValue: Float , to newValue: Float , factor: Float ) -> Float {
93+ @inline ( __always) private nonisolated static func smoothTransition ( from oldValue: Float , to newValue: Float , factor: Float ) -> Float {
9294 // Calculate the delta change between the old and new value
9395 let delta = newValue - oldValue
9496 // Apply an ease-in-out cubic easing curve
95- let easedFactor = _easeInOutCubic ( t: factor)
97+ let easedFactor = easeInOutCubic ( t: factor)
9698 // Calculate and return the smoothed value
9799 return oldValue + delta * easedFactor
98100 }
99101
100102 /// Easing function: ease-in-out cubic
101- @inline ( __always) private func _easeInOutCubic ( t: Float ) -> Float {
103+ @inline ( __always) private nonisolated static func easeInOutCubic ( t: Float ) -> Float {
102104 t < 0.5 ? 4 * t * t * t : 1 - pow( - 2 * t + 2 , 3 ) / 2
103105 }
104106}
0 commit comments