@@ -18,20 +18,20 @@ import AVFoundation
18
18
import LiveKit
19
19
import SwiftUI
20
20
21
- class AudioProcessor : ObservableObject , AudioRenderer {
22
- private weak var _track : AudioTrack ?
23
- private let isCentered : Bool
21
+ public class AudioProcessor : ObservableObject , AudioRenderer {
22
+ public let isCentered : Bool
24
23
public let smoothingFactor : Float
25
24
26
25
// Normalized to 0.0-1.0 range.
27
- @Published var bands : [ Float ]
26
+ @Published public var bands : [ Float ]
28
27
29
28
private let _processor : AudioVisualizeProcessor
29
+ private weak var _track : AudioTrack ?
30
30
31
- init ( track: AudioTrack ? ,
32
- bandCount: Int ,
33
- isCentered: Bool = true ,
34
- smoothingFactor: Float = 0.3 )
31
+ public init ( track: AudioTrack ? ,
32
+ bandCount: Int ,
33
+ isCentered: Bool = true ,
34
+ smoothingFactor: Float = 0.3 )
35
35
{
36
36
self . isCentered = isCentered
37
37
self . smoothingFactor = smoothingFactor
@@ -46,7 +46,7 @@ class AudioProcessor: ObservableObject, AudioRenderer {
46
46
_track? . remove ( audioRenderer: self )
47
47
}
48
48
49
- func render( pcmBuffer: AVAudioPCMBuffer ) {
49
+ public func render( pcmBuffer: AVAudioPCMBuffer ) {
50
50
let newBands = _processor. process ( pcmBuffer: pcmBuffer)
51
51
guard var newBands else { return }
52
52
@@ -134,50 +134,54 @@ class AudioProcessor: ObservableObject, AudioRenderer {
134
134
/// ```
135
135
/// BarAudioVisualizer(audioTrack: audioTrack, barColor: .blue, barCount: 10)
136
136
/// ```
137
- struct BarAudioVisualizer : View {
137
+ public struct BarAudioVisualizer : View {
138
138
public let barCount : Int
139
139
public let barColor : Color
140
140
public let barCornerRadius : CGFloat
141
141
public let barSpacingFactor : CGFloat
142
+ public let barMinOpacity : Double
142
143
public let isCentered : Bool
143
144
144
145
public let audioTrack : AudioTrack
145
146
146
147
@StateObject private var audioProcessor : AudioProcessor
147
148
148
- init ( audioTrack: AudioTrack ,
149
- barColor: Color = . white,
150
- barCount: Int = 7 ,
151
- barCornerRadius: CGFloat = 100 ,
152
- barSpacingFactor: CGFloat = 0.015 ,
153
- isCentered: Bool = true )
149
+ public init ( audioTrack: AudioTrack ,
150
+ barColor: Color = . white,
151
+ barCount: Int = 7 ,
152
+ barCornerRadius: CGFloat = 100 ,
153
+ barSpacingFactor: CGFloat = 0.015 ,
154
+ barMinOpacity: CGFloat = 0.35 ,
155
+ isCentered: Bool = true )
154
156
{
155
157
self . audioTrack = audioTrack
156
158
self . barColor = barColor
157
159
self . barCount = barCount
158
160
self . barCornerRadius = barCornerRadius
159
161
self . barSpacingFactor = barSpacingFactor
162
+ self . barMinOpacity = Double ( barMinOpacity)
160
163
self . isCentered = isCentered
161
164
162
165
_audioProcessor = StateObject ( wrappedValue: AudioProcessor ( track: audioTrack,
163
166
bandCount: barCount,
164
167
isCentered: isCentered) )
165
168
}
166
169
167
- var body : some View {
170
+ public var body : some View {
168
171
GeometryReader { geometry in
172
+ let barMinHeight = ( geometry. size. width - geometry. size. width * barSpacingFactor * CGFloat( barCount + 2 ) ) / CGFloat( barCount)
169
173
HStack ( alignment: . center, spacing: geometry. size. width * barSpacingFactor) {
170
174
ForEach ( 0 ..< audioProcessor. bands. count, id: \. self) { index in
171
175
VStack {
172
176
Spacer ( )
173
- RoundedRectangle ( cornerRadius: barCornerRadius )
174
- . fill ( barColor. opacity ( Double ( audioProcessor. bands [ index] ) ) )
175
- . frame ( height: CGFloat ( audioProcessor. bands [ index] ) * geometry . size . height )
177
+ RoundedRectangle ( cornerRadius: barMinHeight )
178
+ . fill ( barColor. opacity ( ( 1.0 - barMinOpacity ) * Double( audioProcessor. bands [ index] ) + barMinOpacity ) )
179
+ . frame ( height: ( geometry . size . height - barMinHeight ) * CGFloat( audioProcessor. bands [ index] ) + barMinHeight )
176
180
Spacer ( )
177
181
}
178
182
}
179
183
}
184
+ . padding ( geometry. size. width * barSpacingFactor)
180
185
}
181
- . padding ( )
182
186
}
183
187
}
0 commit comments