-
-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathContentView.swift
More file actions
281 lines (253 loc) · 10.6 KB
/
ContentView.swift
File metadata and controls
281 lines (253 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//
// ContentView.swift
// iOS Example
//
// Created by William Entriken on Mar 27, 2024.
//
import FDWaveformView
import SwiftUI
// UIViewRepresentable wrapper for FDWaveformView
struct SwiftUIFDWaveformView: UIViewRepresentable {
@Binding var audioURL: URL?
var dataSource: FDWaveformDataSource?
@Binding var canScrub: Bool
@Binding var canStretch: Bool
@Binding var canScroll: Bool
@Binding var progress: Int?
@Binding var wavesColor: Color?
@Binding var backgroundColor: Color?
@Binding var usesLogarithmicScale: Bool
@Binding var zoomFactor: Double // 1.0 = full, 2.0 = 2x zoom, etc.
let delegate = FDWDelegate()
func makeUIView(context: Context) -> FDWaveformView {
let waveformView = FDWaveformView()
waveformView.delegate = delegate
return waveformView
}
func updateUIView(_ uiView: FDWaveformView, context: Context) {
if let dataSource = dataSource {
uiView.audioURL = nil
uiView.dataSource = dataSource
} else {
uiView.dataSource = nil
uiView.audioURL = audioURL
}
uiView.doesAllowScrubbing = canScrub
uiView.doesAllowStretch = canStretch
uiView.doesAllowScroll = canScroll
uiView.usesLogarithmicScale = usesLogarithmicScale
if let progress {
uiView.highlightedSamples = 0..<progress
}
if let wavesColor {
uiView.wavesColor = UIColor(wavesColor)
}
if let backgroundColor {
uiView.backgroundColor = UIColor(backgroundColor)
}
// Apply zoom factor - zoom into the center
let totalSamples = uiView.totalSamples
if totalSamples > 0 && zoomFactor > 1.0 {
let visibleSamples = Int(Double(totalSamples) / zoomFactor)
let center = totalSamples / 2
let start = max(0, center - visibleSamples / 2)
let end = min(totalSamples, start + visibleSamples)
uiView.zoomSamples = start..<end
} else if totalSamples > 0 {
uiView.zoomSamples = 0..<totalSamples
}
}
}
struct ContentView: View {
@State private var audioURL: URL?
@State private var dataSource: FDWaveformDataSource?
@State private var isScrubActive = false
@State private var isStretchActive = false
@State private var isScrollActive = false
@State private var progress: Int?
@State private var wavesColor: Color?
@State private var backgroundColor: Color?
@State private var usesLogarithmicScale = true
@State private var zoomFactor: Double = 1.0
var body: some View {
NavigationView {
VStack {
HStack {
Button("Load AAC") {
dataSource = nil
audioURL = nil
if let url = Bundle.main.url(
forResource: "TchaikovskyExample2", withExtension: "m4a")
{
audioURL = url
}
}
.buttonStyle(.borderedProminent)
Button("Load MP3") {
dataSource = nil
audioURL = nil
if let url = Bundle.main.url(
forResource: "TchaikovskyExample2", withExtension: "mp3")
{
audioURL = url
}
}
.buttonStyle(.borderedProminent)
}
HStack {
Button("Sine Wave") {
audioURL = nil
// Generate a 5-second sine wave at 440Hz
dataSource = SineWaveSource(
frequency: 440.0,
duration: 5.0,
sampleRate: 44100.0,
modulationPeriod: 1.0
)
}
.buttonStyle(.borderedProminent)
}
HStack {
Button("Randomize Progress") {
// Random progress between 0 and 10000
progress = Int.random(in: 0...10000)
}
.buttonStyle(.bordered)
Button("Randomize Colors") {
// Generate random colors
wavesColor = Color(
red: Double.random(in: 0...1),
green: Double.random(in: 0...1),
blue: Double.random(in: 0...1)
)
backgroundColor = Color(
red: Double.random(in: 0...1),
green: Double.random(in: 0...1),
blue: Double.random(in: 0...1)
)
}
.buttonStyle(.bordered)
}
HStack {
Button("Zoom In") {
zoomFactor = min(zoomFactor * 2.0, 64.0)
}
.buttonStyle(.bordered)
Button("Zoom Out") {
zoomFactor = max(zoomFactor / 2.0, 1.0)
}
.buttonStyle(.bordered)
}
HStack {
Button("Logarithmic") {
usesLogarithmicScale = true
}
.buttonStyle(.bordered)
.disabled(usesLogarithmicScale)
Button("Linear") {
usesLogarithmicScale = false
}
.buttonStyle(.bordered)
.disabled(!usesLogarithmicScale)
}
Button("Run Performance Profiling") {
/*
NSLog("RUNNING PERFORMANCE PROFILE")
let alert = UIAlertController(title: "Start Profiling", message:"Profiling will begin, please don't touch anything. This will take less than 30 seconds.", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default)
alert.addAction(action)
present(alert, animated: true)
self.profileResult = ""
// Delay execution of my block for 1 seconds.
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(1) * Int64(NSEC_PER_SEC)) / Double(NSEC_PER_SEC), execute: {
self.profileResult.append("AAC:")
self.doLoadAAC()
})
// Delay execution of my block for 5 seconds.
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(5) * Int64(NSEC_PER_SEC)) / Double(NSEC_PER_SEC), execute: {
self.profileResult.append(" MP3:")
self.doLoadMP3()
})
/*
// Delay execution of my block for 9 seconds.
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(9) * Int64(NSEC_PER_SEC)) / Double(NSEC_PER_SEC), execute: {
self.profileResult.append(" OGG:")
self.doLoadOGG()
})
*/
// Delay execution of my block for 9 seconds.
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(9) * Int64(NSEC_PER_SEC)) / Double(NSEC_PER_SEC), execute: {
let alert = UIAlertController(title: "PLEASE POST TO github.com/fulldecent/FDWaveformView/wiki", message: self.profileResult, preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default)
alert.addAction(action)
self.present(alert, animated: true)
})
*/
}
.buttonStyle(.borderedProminent)
HStack {
Toggle("Scrub", isOn: $isScrubActive)
Toggle("Stretch", isOn: $isStretchActive)
Toggle("Scroll", isOn: $isScrollActive)
}
.padding()
SwiftUIFDWaveformView(
audioURL: $audioURL,
dataSource: dataSource,
canScrub: $isScrubActive,
canStretch: $isStretchActive,
canScroll: $isScrollActive,
progress: $progress,
wavesColor: $wavesColor,
backgroundColor: $backgroundColor,
usesLogarithmicScale: $usesLogarithmicScale,
zoomFactor: $zoomFactor
)
.onAppear {
if let url = Bundle.main.url(forResource: "Submarine", withExtension: "aiff") {
audioURL = url
}
}
.frame(height: 200)
}
.padding()
}
}
}
#Preview {
ContentView()
}
class FDWDelegate: NSObject, FDWaveformViewDelegate {
fileprivate var startRendering = Date()
fileprivate var endRendering = Date()
fileprivate var startLoading = Date()
fileprivate var endLoading = Date()
fileprivate var profileResult = ""
func waveformViewWillRender(_ waveformView: FDWaveformView) {
startRendering = Date()
}
func waveformViewDidRender(_ waveformView: FDWaveformView) {
endRendering = Date()
NSLog(
"FDWaveformView rendering done, took %0.3f seconds",
endRendering.timeIntervalSince(startRendering))
profileResult.append(
String(format: " render %0.3f ", endRendering.timeIntervalSince(startRendering)))
UIView.animate(
withDuration: 0.25,
animations: { () -> Void in
waveformView.alpha = 1.0
})
}
func waveformViewWillLoad(_ waveformView: FDWaveformView) {
startLoading = Date()
}
func waveformViewDidLoad(_ waveformView: FDWaveformView) {
endLoading = Date()
NSLog(
"FDWaveformView loading done, took %0.3f seconds",
endLoading.timeIntervalSince(startLoading))
profileResult.append(
String(format: " load %0.3f ", endLoading.timeIntervalSince(startLoading)))
}
}