-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathAudioWaveformView.swift
293 lines (230 loc) · 10 KB
/
AudioWaveformView.swift
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
282
283
284
285
286
287
288
289
290
291
292
293
//
// AudioWaveformView.swift
// TelegramMac
//
// Created by keepcoder on 04/12/2016.
// Copyright © 2016 Telegram. All rights reserved.
//
import Cocoa
import TGUIKit
import TelegramMedia
fileprivate class AudioWaveformContainerView : SimpleLayer {
var color:NSColor = .accent {
didSet {
self.setNeedsDisplay()
}
}
var waveform:AudioWaveform? {
didSet {
self.setNeedsDisplay()
}
}
var peakHeight:CGFloat = 12
override init(frame frameRect: NSRect) {
super.init()
self.frame = frameRect
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(layer: Any) {
super.init()
}
override func draw(in ctx: CGContext) {
let sampleWidth:CGFloat = 2
let halfSampleWidth:CGFloat = 1
let distance:CGFloat = 1
let size = frame.size
ctx.setFillColor(color.cgColor)
if let waveform = waveform {
waveform.samples.withUnsafeBytes { (samples: UnsafePointer<UInt16>) -> Void in
let peakHeight: CGFloat = 12.0
let maxReadSamples = waveform.samples.count / 2
var maxSample: UInt16 = 0
for i in 0 ..< maxReadSamples {
let sample = samples[i]
if maxSample < sample {
maxSample = sample
}
}
let invScale = 1.0 / max(1.0, CGFloat(maxSample))
let numSamples = Int(floor(size.width / (sampleWidth + distance)))
let adjustedSamplesMemory = malloc(numSamples * 2)!
let adjustedSamples = adjustedSamplesMemory.assumingMemoryBound(to: UInt16.self)
defer {
free(adjustedSamplesMemory)
}
memset(adjustedSamplesMemory, 0, numSamples * 2)
for i in 0 ..< maxReadSamples {
let index = i * numSamples / maxReadSamples
let sample = samples[i]
if adjustedSamples[index] < sample {
adjustedSamples[index] = sample
}
}
for i in 0 ..< numSamples {
let offset = CGFloat(i) * (sampleWidth + distance)
let peakSample = adjustedSamples[i]
var sampleHeight = CGFloat(peakSample) * peakHeight * invScale
if abs(sampleHeight) > peakHeight {
sampleHeight = peakHeight
}
let adjustedSampleHeight = sampleHeight - sampleWidth
if adjustedSampleHeight.isLessThanOrEqualTo(sampleWidth) {
// ctx.fillEllipse(in: CGRect(x: offset, y: size.height - sampleWidth, width: sampleWidth, height: sampleHeight))
ctx.fill(CGRect(x: offset, y: size.height - halfSampleWidth, width: sampleWidth, height: halfSampleWidth))
} else {
let adjustedRect = CGRect(x: offset, y: size.height - adjustedSampleHeight, width: sampleWidth, height: adjustedSampleHeight)
ctx.fill(adjustedRect)
ctx.fillEllipse(in: CGRect(x: adjustedRect.minX, y: adjustedRect.minY - halfSampleWidth, width: sampleWidth, height: sampleWidth))
ctx.fillEllipse(in: CGRect(x: adjustedRect.minX, y: adjustedRect.maxY - halfSampleWidth, width: sampleWidth, height: sampleHeight))
}
}
}
} else {
ctx.fill(NSMakeRect(halfSampleWidth, size.height - sampleWidth, size.width - sampleWidth, sampleWidth))
ctx.fillEllipse(in: NSMakeRect(0.0, size.height - sampleWidth, sampleWidth, sampleWidth))
ctx.fillEllipse(in: NSMakeRect(size.width - sampleWidth, size.height - sampleWidth, sampleWidth, sampleWidth))
}
}
}
class AudioWaveformView: View {
private let foregroundView:AudioWaveformContainerView
private let backgroundView:AudioWaveformContainerView
let foregroundClipingView:View
var peakHeight:CGFloat = 12 {
didSet {
foregroundView.peakHeight = peakHeight
backgroundView.peakHeight = peakHeight
}
}
var waveform:AudioWaveform? {
didSet {
foregroundView.waveform = waveform
backgroundView.waveform = waveform
}
}
required init(frame frameRect: NSRect) {
foregroundView = AudioWaveformContainerView(frame: NSMakeRect(0,0,frameRect.width,frameRect.height))
backgroundView = AudioWaveformContainerView(frame: NSMakeRect(0,0,frameRect.width,frameRect.height))
foregroundClipingView = View(frame: NSMakeRect(0,0,frameRect.width,frameRect.height))
super.init(frame: frameRect)
self.layer?.addSublayer(backgroundView)
foregroundClipingView.layer?.addSublayer(foregroundView)
addSubview(foregroundClipingView)
}
override func mouseDragged(with event: NSEvent) {
super.mouseDragged(with: event)
}
override func setFrameSize(_ newSize: NSSize) {
super.setFrameSize(newSize)
}
override func layout() {
super.layout()
foregroundView.frame = self.bounds
backgroundView.frame = self.bounds
}
func set(foregroundColor:NSColor, backgroundColor:NSColor) {
self.foregroundView.color = foregroundColor
self.backgroundView.color = backgroundColor
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
// Drawing code here.
}
}
private struct ContentParticle {
var position: CGPoint
var direction: CGPoint
var velocity: CGFloat
var alpha: CGFloat
var lifetime: Double
var beginTime: Double
init(position: CGPoint, direction: CGPoint, velocity: CGFloat, alpha: CGFloat, lifetime: Double, beginTime: Double) {
self.position = position
self.direction = direction
self.velocity = velocity
self.alpha = alpha
self.lifetime = lifetime
self.beginTime = beginTime
}
}
private class DrawingLayer : SimpleLayer {
var color: NSColor = .black
var particles: [ContentParticle] = [] {
didSet {
setNeedsDisplay()
}
}
override func draw(in context: CGContext) {
super.draw(in: context)
context.setFillColor(self.color.cgColor)
for particle in self.particles {
let size: CGFloat = 1.4
context.setAlpha(particle.alpha * 1.0)
context.fillEllipse(in: CGRect(origin: CGPoint(x: particle.position.x - size / 2.0, y: particle.position.y - size / 2.0), size: CGSize(width: size, height: size)))
}
}
}
class SparksView: View {
private var particles: [ContentParticle] = []
private var color: NSColor = .black
private let drawingLayer = DrawingLayer()
required init(frame: CGRect) {
super.init(frame: frame)
drawingLayer.frame = bounds
self.layer?.addSublayer(drawingLayer)
}
required public init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private var presentationSampleHeight: CGFloat = 0.0
private var sampleHeight: CGFloat = 0.0
func update(position: CGPoint, sampleHeight: CGFloat, color: NSColor) {
self.color = color
self.drawingLayer.color = color
self.sampleHeight = sampleHeight
self.presentationSampleHeight = self.presentationSampleHeight * 0.9 + self.sampleHeight * 0.1
let v = CGPoint(x: 1.0, y: 0.0)
let c = CGPoint(x: position.x - 4.0, y: position.y + 1.0 - self.presentationSampleHeight * CGFloat(arc4random_uniform(100)) / 100.0)
let timestamp = CACurrentMediaTime()
let dt: CGFloat = 1.0 / 60.0
var removeIndices: [Int] = []
for i in 0 ..< self.particles.count {
let currentTime = timestamp - self.particles[i].beginTime
if currentTime > self.particles[i].lifetime {
removeIndices.append(i)
} else {
let input: CGFloat = CGFloat(currentTime / self.particles[i].lifetime)
let decelerated: CGFloat = (1.0 - (1.0 - input) * (1.0 - input))
self.particles[i].alpha = 1.0 - decelerated
var p = self.particles[i].position
let d = self.particles[i].direction
let v = self.particles[i].velocity
p = CGPoint(x: p.x + d.x * v * dt, y: p.y + d.y * v * dt)
self.particles[i].position = p
}
}
for i in removeIndices.reversed() {
self.particles.remove(at: i)
}
let newParticleCount = 3
for _ in 0 ..< newParticleCount {
let degrees: CGFloat = CGFloat(arc4random_uniform(100)) - 65.0
let angle: CGFloat = degrees * CGFloat.pi / 180.0
let direction = CGPoint(x: v.x * cos(angle) - v.y * sin(angle), y: v.x * sin(angle) + v.y * cos(angle))
let velocity = (80.0 + (CGFloat(arc4random()) / CGFloat(UINT32_MAX)) * 4.0) * 0.5
let lifetime = Double(0.65 + CGFloat(arc4random_uniform(100)) * 0.01)
let particle = ContentParticle(position: c, direction: direction, velocity: velocity, alpha: 1.0, lifetime: lifetime, beginTime: timestamp)
self.particles.append(particle)
}
drawingLayer.particles = particles
}
override func layout() {
super.layout()
drawingLayer.frame = bounds
}
}