Skip to content

Commit 924f4ad

Browse files
committed
swiftformatting
1 parent c4460b6 commit 924f4ad

File tree

10 files changed

+62
-92
lines changed

10 files changed

+62
-92
lines changed

Demo/WaveformDemo/ContentView.swift

+9-12
Original file line numberDiff line numberDiff line change
@@ -21,58 +21,55 @@ func clamp(_ x: Double, _ inf: Double, _ sup: Double) -> Double {
2121
}
2222

2323
struct MinimapView: View {
24-
2524
@Binding var start: Double
2625
@Binding var length: Double
27-
26+
2827
@GestureState var initialStart: Double?
2928
@GestureState var initialLength: Double?
30-
29+
3130
let indicatorSize = 10.0
32-
31+
3332
var body: some View {
3433
GeometryReader { gp in
3534
RoundedRectangle(cornerRadius: indicatorSize)
3635
.frame(width: length * gp.size.width)
3736
.offset(x: start * gp.size.width)
3837
.opacity(0.5)
3938
.gesture(DragGesture()
40-
.updating($initialStart) { drag, state, _ in
39+
.updating($initialStart) { _, state, _ in
4140
if state == nil {
4241
state = start
4342
}
4443
}
4544
.onChanged { drag in
4645
if let initialStart = initialStart {
47-
start = clamp(initialStart + drag.translation.width / gp.size.width, 0, 1-length)
46+
start = clamp(initialStart + drag.translation.width / gp.size.width, 0, 1 - length)
4847
}
4948
}
5049
)
51-
50+
5251
RoundedRectangle(cornerRadius: indicatorSize)
5352
.foregroundColor(.white)
5453
.frame(width: indicatorSize).opacity(0.3)
55-
.offset(x: (start+length) * gp.size.width)
54+
.offset(x: (start + length) * gp.size.width)
5655
.padding(indicatorSize)
5756
.gesture(DragGesture()
58-
.updating($initialLength) { drag, state, _ in
57+
.updating($initialLength) { _, state, _ in
5958
if state == nil {
6059
state = length
6160
}
6261
}
6362
.onChanged { drag in
6463
if let initialLength = initialLength {
65-
length = clamp(initialLength + drag.translation.width / gp.size.width, 0, 1-start)
64+
length = clamp(initialLength + drag.translation.width / gp.size.width, 0, 1 - start)
6665
}
6766
}
68-
6967
)
7068
}
7169
}
7270
}
7371

7472
struct ContentView: View {
75-
7673
@StateObject var model = WaveformDemoModel(file: getFile())
7774

7875
@State var start = 0.0

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import PackageDescription
55
let package = Package(
66
name: "Waveform",
77
platforms: [.macOS(.v11), .iOS(.v14)],
8-
products: [ .library(name: "Waveform", targets: ["Waveform"])],
8+
products: [.library(name: "Waveform", targets: ["Waveform"])],
99
targets: [
1010
.target(name: "Waveform", dependencies: []),
1111
.testTarget(name: "WaveformTests", dependencies: ["Waveform"], resources: [.copy("beat.aiff")]),

Sources/Waveform/Helpers.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ import Metal
33

44
func binMin(samples: [Float], binSize: Int) -> [Float] {
55
var out: [Float] = []
6-
for bin in 0..<samples.count/binSize {
7-
out.append(samples[(bin*binSize) ..< ((bin+1)*binSize)].min()!)
6+
for bin in 0 ..< samples.count / binSize {
7+
out.append(samples[(bin * binSize) ..< ((bin + 1) * binSize)].min()!)
88
}
99
return out
1010
}
1111

1212
func binMax(samples: [Float], binSize: Int) -> [Float] {
1313
var out: [Float] = []
14-
for bin in 0..<samples.count/binSize {
15-
out.append(samples[(bin*binSize) ..< ((bin+1)*binSize)].max()!)
14+
for bin in 0 ..< samples.count / binSize {
15+
out.append(samples[(bin * binSize) ..< ((bin + 1) * binSize)].max()!)
1616
}
1717
return out
1818
}
1919

2020
extension MTLDevice {
2121
func makeBuffer(_ values: [Float]) -> MTLBuffer? {
22-
self.makeBuffer(bytes: values, length: MemoryLayout<Float>.size * values.count)
22+
makeBuffer(bytes: values, length: MemoryLayout<Float>.size * values.count)
2323
}
2424
}

Sources/Waveform/Renderer.swift

+13-24
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@ import MetalKit
55
let MaxBuffers = 3
66

77
public struct Constants {
8-
public init() {
9-
10-
}
8+
public init() {}
119
}
1210

1311
class Renderer: NSObject, MTKViewDelegate {
14-
1512
var device: MTLDevice!
1613
var queue: MTLCommandQueue!
1714
var pipeline: MTLRenderPipelineState!
1815
var source = ""
1916
public var constants = Constants()
2017

2118
private let inflightSemaphore = DispatchSemaphore(value: MaxBuffers)
22-
19+
2320
var minBuffers: [MTLBuffer] = []
2421
var maxBuffers: [MTLBuffer] = []
2522

@@ -30,9 +27,9 @@ class Renderer: NSObject, MTKViewDelegate {
3027
init(device: MTLDevice) {
3128
self.device = device
3229
queue = device.makeCommandQueue()
33-
30+
3431
let library = try! device.makeDefaultLibrary(bundle: Bundle.module)
35-
32+
3633
let rpd = MTLRenderPipelineDescriptor()
3734
rpd.vertexFunction = library.makeFunction(name: "waveform_vert")
3835
rpd.fragmentFunction = library.makeFunction(name: "waveform_frag")
@@ -46,37 +43,34 @@ class Renderer: NSObject, MTKViewDelegate {
4643
colorAttachment.destinationAlphaBlendFactor = .oneMinusSourceAlpha
4744

4845
pipeline = try! device.makeRenderPipelineState(descriptor: rpd)
49-
46+
5047
super.init()
5148
}
5249

53-
func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
50+
func mtkView(_: MTKView, drawableSizeWillChange _: CGSize) {}
5451

55-
}
56-
5752
func selectBuffers(width: CGFloat) -> (MTLBuffer, MTLBuffer) {
58-
5953
var level = 0
6054
for (minBuffer, maxBuffer) in zip(minBuffers, maxBuffers) {
6155
if CGFloat(minBuffer.length / MemoryLayout<Float>.size) < width {
6256
return (minBuffer, maxBuffer)
6357
}
6458
level += 1
6559
}
66-
60+
6761
return (minBuffers.last!, maxBuffers.last!)
6862
}
69-
63+
7064
func encode(to commandBuffer: MTLCommandBuffer,
7165
pass: MTLRenderPassDescriptor,
72-
width: CGFloat) {
73-
66+
width: CGFloat)
67+
{
7468
pass.colorAttachments[0].clearColor = MTLClearColorMake(0, 0, 0, 0)
7569

7670
let highestResolutionCount = Float(lastSamples.samples.count)
7771
let startFactor = Float(start) / highestResolutionCount
7872
let lengthFactor = Float(length) / highestResolutionCount
79-
73+
8074
let (minBuffer, maxBuffer) = selectBuffers(width: width / CGFloat(lengthFactor))
8175
let enc = commandBuffer.makeRenderCommandEncoder(descriptor: pass)!
8276
enc.setRenderPipelineState(pipeline)
@@ -93,11 +87,9 @@ class Renderer: NSObject, MTKViewDelegate {
9387
enc.setFragmentBytes(c, length: MemoryLayout<Constants>.size, index: 3)
9488
enc.drawPrimitives(type: .triangleStrip, vertexStart: 0, vertexCount: 4)
9589
enc.endEncoding()
96-
9790
}
9891

9992
func draw(in view: MTKView) {
100-
10193
let size = view.frame.size
10294
let w = Float(size.width)
10395
let h = Float(size.height)
@@ -117,15 +109,12 @@ class Renderer: NSObject, MTKViewDelegate {
117109
semaphore.signal()
118110
}
119111

120-
121112
if let renderPassDescriptor = view.currentRenderPassDescriptor, let currentDrawable = view.currentDrawable {
122-
123113
encode(to: commandBuffer, pass: renderPassDescriptor, width: size.width)
124114

125115
commandBuffer.present(currentDrawable)
126116
}
127117
commandBuffer.commit()
128-
129118
}
130119

131120
func set(samples: SampleBuffer, start: Int, length: Int) {
@@ -137,10 +126,10 @@ class Renderer: NSObject, MTKViewDelegate {
137126
lastSamples = samples
138127
minBuffers.removeAll()
139128
maxBuffers.removeAll()
140-
129+
141130
var minSamples = samples.samples
142131
var maxSamples = samples.samples
143-
132+
144133
var s = samples.samples.count
145134
while s > 2 {
146135
minBuffers.append(device.makeBuffer(minSamples)!)

Sources/Waveform/SampleBuffer.swift

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import Foundation
32

43
/// Immutable data for samples so we can quickly compare to see if we should recompute.

Sources/Waveform/Waveform.metal

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#include <metal_stdlib>
32
using namespace metal;
43

Sources/Waveform/Waveform.swift

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import AVFoundation
2-
import SwiftUI
32
import MetalKit
3+
import SwiftUI
44

55
#if os(macOS)
6-
public struct Waveform : NSViewRepresentable {
7-
6+
public struct Waveform: NSViewRepresentable {
87
var samples: SampleBuffer
98
var start: Int
109
var length: Int
@@ -15,7 +14,7 @@ public struct Waveform : NSViewRepresentable {
1514
self.constants = constants
1615
self.start = start
1716
if length > 0 {
18-
self.length = min(length, (samples.samples.count - start))
17+
self.length = min(length, samples.samples.count - start)
1918
} else {
2019
self.length = samples.samples.count - start
2120
}
@@ -53,8 +52,7 @@ public struct Waveform : NSViewRepresentable {
5352
}
5453
}
5554
#else
56-
public struct Waveform : UIViewRepresentable {
57-
55+
public struct Waveform: UIViewRepresentable {
5856
var samples: SampleBuffer
5957
var start: Int
6058
var length: Int
@@ -101,6 +99,5 @@ public struct Waveform : UIViewRepresentable {
10199
length: length)
102100
uiView.setNeedsDisplay()
103101
}
104-
105102
}
106103
#endif
+16-22
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
2-
1+
import CoreGraphics
32
import Foundation
43
import Metal
5-
import CoreGraphics
64

75
func createImage(data: UnsafeMutablePointer<UInt8>, w: Int, h: Int) -> CGImage {
8-
96
let dataSize = 4 * w * h
107

11-
let provider = CGDataProvider(dataInfo: nil, data: data, size: dataSize, releaseData: {_,_,_ in })!
8+
let provider = CGDataProvider(dataInfo: nil, data: data, size: dataSize, releaseData: { _, _, _ in })!
129

1310
let colorSpace = CGColorSpaceCreateDeviceRGB()
1411

1512
let image = CGImage(width: w,
1613
height: h,
1714
bitsPerComponent: 8,
1815
bitsPerPixel: 32,
19-
bytesPerRow: w*4,
16+
bytesPerRow: w * 4,
2017
space: colorSpace,
2118
bitmapInfo: .init(rawValue: CGImageAlphaInfo.noneSkipLast.rawValue),
2219
provider: provider,
@@ -25,49 +22,46 @@ func createImage(data: UnsafeMutablePointer<UInt8>, w: Int, h: Int) -> CGImage {
2522
intent: .defaultIntent)!
2623

2724
return image
28-
2925
}
3026

3127
extension MTLTexture {
3228
var cgImage: CGImage {
33-
34-
let dataSize = width*height*4
29+
let dataSize = width * height * 4
3530
let ptr = UnsafeMutablePointer<UInt8>.allocate(capacity: dataSize)
36-
31+
3732
switch pixelFormat {
3833
case .bgra8Unorm:
39-
getBytes(ptr, bytesPerRow: width*4, from: MTLRegionMake2D(0, 0, width, height), mipmapLevel: 0)
40-
for i in 0..<(width*height) {
41-
swap(&ptr[4*i], &ptr[4*i+2])
34+
getBytes(ptr, bytesPerRow: width * 4, from: MTLRegionMake2D(0, 0, width, height), mipmapLevel: 0)
35+
for i in 0 ..< (width * height) {
36+
swap(&ptr[4 * i], &ptr[4 * i + 2])
4237
}
4338
default:
4439
fatalError()
4540
}
46-
41+
4742
return createImage(data: ptr, w: width, h: height)
4843
}
49-
44+
5045
var isBlack: Bool {
51-
52-
let dataSize = width*height*4
46+
let dataSize = width * height * 4
5347
let ptr = UnsafeMutablePointer<UInt8>.allocate(capacity: dataSize)
5448
defer {
5549
ptr.deallocate()
5650
}
57-
51+
5852
switch pixelFormat {
5953
case .bgra8Unorm:
60-
getBytes(ptr, bytesPerRow: width*4, from: MTLRegionMake2D(0, 0, width, height), mipmapLevel: 0)
54+
getBytes(ptr, bytesPerRow: width * 4, from: MTLRegionMake2D(0, 0, width, height), mipmapLevel: 0)
6155
default:
6256
fatalError()
6357
}
64-
65-
for x in 0..<dataSize {
58+
59+
for x in 0 ..< dataSize {
6660
if ptr[x] != 0 {
6761
return false
6862
}
6963
}
70-
64+
7165
return true
7266
}
7367
}

0 commit comments

Comments
 (0)