Skip to content

Commit dc78691

Browse files
author
“LaloMrtnz”
committed
Improved formatting.
1 parent 54f1eff commit dc78691

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

.DS_Store

0 Bytes
Binary file not shown.

Kaleido.playground/Contents.swift

+16-12
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import UIKit
22
import AVFoundation
33
import PlaygroundSupport
44
/*:
5-
# Kaleido
5+
# Kaleido♦️🔵🔶
66
## The music-lovin' kaleidoscope.
77

8-
Kaleido uses **CAReplicatorLayers**, masks, **CoreAnimation** and **AVFoundation** to create a kaleidoscope visualizer that reacts to the beat of your favorite tracks.
8+
Kaleido uses `CAReplicatorLayers`, masks, `CoreAnimation` and `AVFoundation` to create a kaleidoscope visualizer that reacts to the beat of your favorite tracks.
99

1010
----
1111

12-
### Setup
12+
### Setup 🔧
1313
*/
1414
let view = UIView(frame: CGRect(x: 0, y: 0, width: 600, height: 400))
1515
view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
@@ -19,7 +19,8 @@ let main = UIView()
1919
main.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
2020
main.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
2121

22-
//: The **KaleidoLayer** is the base of the visualizer, it masks our *main* view and transforms it, creating the basic kaleidoscopic shape.
22+
//: The `KaleidoLayer` is the base of the visualizer, it masks our *main* view and transforms it, creating the basic kaleidoscopic shape.\
23+
//: ![KaleidoLayer](kaleidoLayer.png "Kaleido Layer Explained")
2324
let kaleido = KaleidoLayer(referenceLayer: main.layer)
2425

2526
//: **CAReplicatorLayers** are used to duplicate the main KaleidoLayer and make the grid.
@@ -41,7 +42,7 @@ view.layer.addSublayer(grid)
4142
/*:
4243
----
4344

44-
### Adding Elements
45+
### Adding Elements 🚥
4546
Different views are added to the *main Kaleido Layer* and animated using Core Animation to create the Kaleidoscope effect.
4647
*/
4748

@@ -130,23 +131,23 @@ main.addSubview(elm1)
130131
/*:
131132
----
132133

133-
### Responding to Audio Player
134-
We use **AVAudioPlayer** to play the song Kaleido will react to.
135-
Based on the *peakPower* and *averagePower* meters, we change some of the moving elements' background colors.
134+
### Responding to Audio Player 🔊
135+
We use `AVAudioPlayer` to play the song Kaleido will react to.
136+
Based on the `peakPower` and `averagePower` meters, we change some of the moving elements' background colors.
136137

137138
*/
138139
var player: AVAudioPlayer?
139140
/*:
140141
* experiment:
141-
Change the **song** value to play different tracks. You can chose from:
142+
Change the `song` value to play different tracks. You can chose from:
142143

143144
1. "moose"
144145
2. "fractal"
145-
3. "rock"
146+
3. Add your own tracks and add the file name.
146147

147148
Can you see how the colors react to the beat of your music?
148149
*/
149-
let song = "rock"
150+
let song = "moose"
150151

151152
guard let songURL = Bundle.main.url(forResource: song, withExtension: "mp3") else {
152153
fatalError("audio file is not in bundle.")
@@ -162,6 +163,7 @@ do {
162163
player.prepareToPlay()
163164
player.play()
164165

166+
//: The `Timer` helps us to run a function any given amount of time (in this case, every 0.5 seconds) as long as the `AudioPlayer` is playing the song.
165167
Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { (_) in
166168

167169
if !player.isPlaying {
@@ -176,8 +178,10 @@ do {
176178
let linearPeak = pow(10.0, logPeak/20.0)
177179
let linearAverage = pow(10.0, logAverage/20.0)
178180

179-
181+
//: If the peak power of the track at a given moment of time is above 55%, then we detect a beat (maybe a kickdrum or a snare sounding).🥁
180182
if linearPeak >= 0.55 {
183+
184+
//: The background **color hue** for these animated elements changes everytime an accent beat is detected.
181185
elm4.backgroundColor = UIColor(hue: CGFloat(Float(arc4random()) / Float(UINT32_MAX)), saturation: 0.7, brightness: 0.7, alpha: 1.0)
182186
elm3.backgroundColor = UIColor(hue: CGFloat(Float(arc4random()) / Float(UINT32_MAX)), saturation: 0.7, brightness: 0.7, alpha: 1.0)
183187
elm6.backgroundColor = UIColor(hue: CGFloat(Float(arc4random()) / Float(UINT32_MAX)), saturation: 0.7, brightness: 0.7, alpha: 1.0)
2.54 KB
Loading

Kaleido.playground/Sources/KaleidoLayer.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
22
import UIKit
33

4-
/// A layer that masks and transforms a layer to turn it into a square kaleidoscopic view.
4+
/// A layer that masks and applys several transforms to a layer, and turns it into a square kaleidoscopic view.
55
public class KaleidoLayer: CAReplicatorLayer {
66

77
public init(referenceLayer: CALayer) {

0 commit comments

Comments
 (0)