Skip to content

Commit 190b840

Browse files
author
Danilo Campana Fuchs
committed
🍻 Let's celebrate optimizations!
1 parent bc3ca0f commit 190b840

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

ForceTouchPlayer/ContentView.swift

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ import SwiftUI
33
let timerClockHz = 1000.0
44
let timerInterval = 1.0 / timerClockHz
55

6+
// FIXME: GLOBAL VARIABLESSSSS AAAAAAAA -- This makes the sounds more consistent
7+
var skippedTicksCount = 0
8+
var currentNote: Note?
9+
var currentNoteEndTime: Date?
10+
611
struct ContentView: View {
712

813
let songsList: [Song]
914

1015
private let timer = Timer.publish(every: timerInterval, tolerance: timerInterval, on: .main, in: .common).autoconnect()
1116

1217
@State private var timerEnabled = false
13-
@State private var skippedTicksCount = 0
1418

1519
@State private var currentNoteIndex: Int = 0;
16-
@State private var currentNote: Note?
17-
@State private var currentNoteEndTime: Date?
1820

1921
@State private var tempo = 144.0
2022
@State private var currentSongIndex: Int = 0
@@ -54,7 +56,7 @@ struct ContentView: View {
5456
.frame(minWidth: 480, maxWidth: 600, minHeight: 300, maxHeight: 400)
5557
.onReceive(timer) {
5658
time in
57-
self.playTick()
59+
self.playTick(time: time)
5860
}
5961
}
6062

@@ -79,17 +81,17 @@ struct ContentView: View {
7981
func stop() {
8082
self.timerEnabled = false
8183
self.currentNoteIndex = 0
82-
self.currentNote = nil
83-
self.currentNoteEndTime = nil
84-
self.skippedTicksCount = 0
84+
currentNote = nil
85+
currentNoteEndTime = nil
86+
skippedTicksCount = 0
8587
}
8688

87-
func playTick() {
89+
func playTick(time: Date) {
8890
guard timerEnabled else { return }
89-
guard let currentNote = self.currentNote else { return }
90-
guard let currentNoteEndTime = self.currentNoteEndTime else { return }
91+
guard let currentNote = currentNote else { return }
92+
guard let currentNoteEndTime = currentNoteEndTime else { return }
9193

92-
if (Date() > currentNoteEndTime) {
94+
if (time > currentNoteEndTime) {
9395
self.enqueueNextNote()
9496
}
9597

@@ -119,10 +121,9 @@ struct ContentView: View {
119121

120122
let noteDuration = baseDuration * note.value
121123

122-
self.skippedTicksCount = 0
123-
self.currentNote = note;
124-
let currentNoteEndTime = Date().addingTimeInterval(noteDuration / 1000.0)
125-
self.currentNoteEndTime = currentNoteEndTime
124+
skippedTicksCount = 0
125+
currentNote = note;
126+
currentNoteEndTime = Date().addingTimeInterval(noteDuration / 1000.0)
126127
}
127128

128129
func loadNote(index: Int) -> Note? {
@@ -146,13 +147,13 @@ struct ContentView: View {
146147
let clockTicksPerToneTick = Int(timerClockHz / frequency)
147148
let clockTicksToSkip = clockTicksPerToneTick - 1
148149

149-
if (self.skippedTicksCount < clockTicksToSkip) {
150-
self.skippedTicksCount += 1
150+
if (skippedTicksCount < clockTicksToSkip) {
151+
skippedTicksCount += 1
151152
return
152153
}
153154

154155
self.performHapticFeedback()
155-
self.skippedTicksCount = 0
156+
skippedTicksCount = 0
156157
}
157158

158159
func performHapticFeedback() {

0 commit comments

Comments
 (0)