Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Example/WaveformScrubberExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct ContentView: View {
Section("Audio") {
WaveformScrubber(
config: scrubberConfig,
drawer: BarDrawer(config: .init(barWidth: 2, spacing: 2), upsampleStrategy: .smooth),
drawer: BarDrawer(config: .init(barWidth: 2, spacing: 2, minBarHeight: 4, cornerRadius: 2), upsampleStrategy: .smooth),
url: audioURL,
progress: $progress
) { info in
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ A "Drawer" defines the geometric shape of the waveform. You can choose from seve
The classic bar graph style.
```swift
WaveformScrubber(
drawer: BarDrawer(config: .init(barWidth: 3, spacing: 5)),
drawer: BarDrawer(config: .init(barWidth: 4, spacing: 5, minBarHeight: 4, cornerRadius: 2)),
url: audioURL,
progress: $progress
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,31 @@ import SwiftUI

extension BarDrawer {
public struct Config: Sendable {
public let barWidth: CGFloat
public let spacing: CGFloat
/// Width of each bar.
public var barWidth: CGFloat
/// Spacing between bars.
public var spacing: CGFloat
/// Minimum height for each bar (to ensure visibility).
public var minBarHeight: CGFloat
/// Corner radius for rounding bar edges.
public var cornerRadius: CGFloat

/// Creates a new configuration for the `BarDrawer`'s appearance.
/// - Parameters:
/// - spacing: The distance between each waveform bar. Defaults to `2`.
/// - barWidth: The width of each waveform bar. Defaults to `2`.
/// - minBarHeight: Minimum height for each bar (to ensure visibility). Defaults to `1`.
/// - cornerRadius: Corner radius for rounding bar edges. Defaults to `0`.
public init(
barWidth: CGFloat = 2,
spacing: CGFloat = 2
spacing: CGFloat = 2,
minBarHeight: CGFloat = 1,
cornerRadius: CGFloat = 0
) {
self.barWidth = barWidth
self.spacing = spacing
self.minBarHeight = minBarHeight
self.cornerRadius = cornerRadius
}
}
}
10 changes: 7 additions & 3 deletions Sources/WaveformScrubber/Domain/Shapes/BarDrawer/BarDrawer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,20 @@ public struct BarDrawer: WaveformDrawing {

for (index, sample) in samples.enumerated() {
let x = CGFloat(index) * totalBarWidth
// Ensure height is at least 1 to be visible
let height = max(1, CGFloat(sample) * rect.height)
let rawHeight = CGFloat(sample) * rect.height
// Ensure height is at least minBarHeight to be visible
let height = max(config.minBarHeight, rawHeight)

let barRect = CGRect(
x: x,
y: midY - height / 2,
width: config.barWidth,
height: height
)
path.addRect(barRect)
path.addRoundedRect(
in: barRect,
cornerSize: CGSize(width: config.cornerRadius, height: config.cornerRadius)
)
}
}
}
Expand Down