Skip to content

Commit b35628e

Browse files
author
Danilo Campana Fuchs
committed
chore: Format code
1 parent 2e1a141 commit b35628e

13 files changed

+139
-139
lines changed

ForceTouchPlayer/AppDelegate.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,25 @@ import SwiftUI
33

44
@NSApplicationMain
55
class AppDelegate: NSObject, NSApplicationDelegate {
6-
76
var window: NSWindow!
87

9-
10-
func applicationDidFinishLaunching(_ aNotification: Notification) {
8+
func applicationDidFinishLaunching(_: Notification) {
119
// Create the SwiftUI view that provides the window contents.
1210
let contentView = ContentView(songsRepository: SongsRepository())
1311

14-
// Create the window and set the content view.
12+
// Create the window and set the content view.
1513
window = NSWindow(
1614
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
1715
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
18-
backing: .buffered, defer: false)
16+
backing: .buffered, defer: false
17+
)
1918
window.center()
2019
window.setFrameAutosaveName("Main Window")
2120
window.contentView = NSHostingView(rootView: contentView)
2221
window.makeKeyAndOrderFront(nil)
2322
}
2423

25-
func applicationWillTerminate(_ aNotification: Notification) {
24+
func applicationWillTerminate(_: Notification) {
2625
// Insert code here to tear down your application
2726
}
28-
29-
3027
}
31-

ForceTouchPlayer/ContentView.swift

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,24 @@ var currentNote: Note?
99
var currentNoteEndTime: Date?
1010

1111
struct ContentView: View {
12-
1312
let songsList: [Song]
14-
13+
1514
private let timer = Timer.publish(every: timerInterval, tolerance: timerInterval, on: .main, in: .common).autoconnect()
16-
15+
1716
@State private var timerEnabled = false
18-
19-
@State private var currentNoteIndex: Int = 0;
20-
17+
18+
@State private var currentNoteIndex: Int = 0
19+
2120
@State private var tempo = 144.0
2221
@State private var currentSongIndex: Int = 0
23-
22+
2423
init(songsRepository: SongsRepository) {
25-
self.songsList = songsRepository.listSongs()
24+
songsList = songsRepository.listSongs()
2625
}
27-
26+
2827
var body: some View {
28+
let tempoStr = String(format: "%d", Int(self.tempo)).padding(toLength: 5, withPad: " ", startingAt: 0)
2929

30-
let tempoStr = String(format: "%.1f", self.tempo).padding(toLength: 5, withPad: " ", startingAt: 0);
31-
3230
return HStack {
3331
Spacer()
3432
.frame(width: 64.0)
@@ -41,7 +39,7 @@ struct ContentView: View {
4139
}
4240
HStack {
4341
Text("Tempo: \(tempoStr) BPM").frame(width: 150, alignment: .leading)
44-
Slider(value: $tempo, in: 40...300, step: 8)
42+
Slider(value: $tempo, in: 40 ... 300, step: 8)
4543
}
4644
Spacer()
4745
Text("Don't take your finger off of the trackpad!")
@@ -60,111 +58,111 @@ struct ContentView: View {
6058
self.playTick(time: time)
6159
}
6260
}
63-
64-
func handlePickerChange(_ index: Int) {
65-
self.tempo = songsList[currentSongIndex].defaultTempo
66-
self.stop()
61+
62+
func handlePickerChange(_: Int) {
63+
tempo = songsList[currentSongIndex].defaultTempo
64+
stop()
6765
}
68-
66+
6967
func toggleTimer() {
70-
if (self.timerEnabled) {
71-
self.stop()
68+
if timerEnabled {
69+
stop()
7270
} else {
73-
self.start()
71+
start()
7472
}
7573
}
76-
74+
7775
func start() {
78-
self.timerEnabled = true
79-
self.enqueueInitialNote()
76+
timerEnabled = true
77+
enqueueInitialNote()
8078
}
81-
79+
8280
func stop() {
83-
self.timerEnabled = false
84-
self.currentNoteIndex = 0
81+
timerEnabled = false
82+
currentNoteIndex = 0
8583
currentNote = nil
8684
currentNoteEndTime = nil
8785
skippedTicksCount = 0
8886
}
89-
87+
9088
func playTick(time: Date) {
9189
guard timerEnabled else { return }
9290
guard let currentNote = currentNote else { return }
9391
guard let currentNoteEndTime = currentNoteEndTime else { return }
94-
95-
if (time > currentNoteEndTime) {
96-
self.enqueueNextNote()
92+
93+
if time > currentNoteEndTime {
94+
enqueueNextNote()
9795
}
98-
99-
self.playTickOfTone(frequency: currentNote.frequency)
96+
97+
playTickOfTone(frequency: currentNote.frequency)
10098
}
101-
99+
102100
func enqueueInitialNote() {
103-
self.currentNoteIndex = 0
104-
self.enqueueNote()
101+
currentNoteIndex = 0
102+
enqueueNote()
105103
}
106-
104+
107105
func enqueueNextNote() {
108-
self.currentNoteIndex += 1
109-
self.enqueueNote()
106+
currentNoteIndex += 1
107+
enqueueNote()
110108
}
111-
109+
112110
func enqueueNote() {
113-
guard let note = self.loadNote(index: self.currentNoteIndex) else {
111+
guard let note = loadNote(index: currentNoteIndex) else {
114112
// End of song
115-
self.stop()
113+
stop()
116114
return
117115
}
118-
116+
119117
// Time it takes to play a note with 4 beats
120118
// (1min / tempo (bpm))
121-
let baseDuration = (60000.0) / tempo
122-
119+
let baseDuration = 60000.0 / tempo
120+
123121
let noteDuration = baseDuration * note.value
124-
122+
125123
skippedTicksCount = 0
126-
currentNote = note;
124+
currentNote = note
127125
currentNoteEndTime = Date().addingTimeInterval(noteDuration / 1000.0)
128126
}
129-
127+
130128
func loadNote(index: Int) -> Note? {
131-
let currentSong = self.songsList[currentSongIndex]
132-
133-
if (index >= currentSong.notes.endIndex) {
129+
let currentSong = songsList[currentSongIndex]
130+
131+
if index >= currentSong.notes.endIndex {
134132
return nil
135133
}
136-
134+
137135
return currentSong.notes[index]
138136
}
139-
137+
140138
/// Software PWM
141139
/// Only performs haptic feedback in fractions of the timer frequency
142140
/// Example: If timer has frequency of 1000 Hz and we want to play at 100Hz,
143141
/// we need to skip 9 clock ticks. This way, we play once every 10 ticks (1000/10 = 100)
144142
func playTickOfTone(frequency: Double) {
145-
if (frequency == 0) {
146-
return;
143+
if frequency == 0 {
144+
return
147145
}
148146
let clockTicksPerToneTick = Int(timerClockHz / frequency)
149147
let clockTicksToSkip = clockTicksPerToneTick - 1
150-
151-
if (skippedTicksCount < clockTicksToSkip) {
148+
149+
if skippedTicksCount < clockTicksToSkip {
152150
skippedTicksCount += 1
153151
return
154152
}
155-
156-
self.performHapticFeedback()
153+
154+
performHapticFeedback()
157155
skippedTicksCount = 0
158156
}
159-
157+
160158
func performHapticFeedback() {
161159
NSHapticFeedbackManager.defaultPerformer.perform(
162160
NSHapticFeedbackManager.FeedbackPattern.generic,
163-
performanceTime: NSHapticFeedbackManager.PerformanceTime.now)
161+
performanceTime: NSHapticFeedbackManager.PerformanceTime.now
162+
)
164163
}
165164
}
166165

167-
168166
struct ContentView_Previews: PreviewProvider {
169167
static var previews: some View {
170168
ContentView(songsRepository: SongsRepository())

ForceTouchPlayer/Extensions.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extension Binding {
88
set: { selection in
99
self.wrappedValue = selection
1010
handler(selection)
11-
})
11+
}
12+
)
1213
}
1314
}

ForceTouchPlayer/Song.swift

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@ import Foundation
22

33
struct Song {
44
let name: String
5-
5+
66
/// Tempo when user first selects the song
77
var defaultTempo: Double = 144.0
8-
8+
99
/// Padding note added between each note in a song
1010
var padding: Note? = Note(frequency: 0, value: 0.25)
11-
11+
1212
/// Raw notes in array-of-tuples form
1313
let rawNotes: [RawNote]
14-
14+
1515
var notes: [Note] {
16-
1716
let parsedNotes = rawNotes.map {
1817
Note(frequency: $0.frequency, value: $0.value)
1918
}
20-
21-
let paddedNotes = parsedNotes.enumerated().flatMap({(index, note) -> [Note] in
22-
if (padding == nil || index == parsedNotes.count - 1) { // Do not pad end of song
19+
20+
let paddedNotes = parsedNotes.enumerated().flatMap { (index, note) -> [Note] in
21+
if padding == nil || index == parsedNotes.count - 1 {
22+
// Do not pad end of song
2323
return [note]
2424
}
2525
return [note, padding!]
26-
})
27-
26+
}
27+
2828
return paddedNotes
2929
}
3030
}
@@ -38,6 +38,7 @@ struct Note {
3838
/// a 1 means a quarter note, 0.5 an eighteenth , 0.25 sixteenth, so on
3939
typealias RawNote = (frequency: Double, value: Double)
4040

41+
// swiftformat:disable all -- Keep spacing
4142
let NOTE_B0 = 31.0
4243
let NOTE_C1 = 33.0
4344
let NOTE_CS1 = 35.0
@@ -100,3 +101,4 @@ let NOTE_A5 = 880.0
100101
let NOTE_AS5 = 932.0
101102
let NOTE_B5 = 988.0
102103
let REST = 0.0
104+
// swiftformat:enable all

ForceTouchPlayer/Songs/CMajorScale.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ let cMajorScale = Song(
2323
(NOTE_D2, 1),
2424
(NOTE_E2, 1),
2525
(NOTE_F2, 1),
26-
26+
2727
(NOTE_G2, 1),
28-
28+
2929
(NOTE_F2, 1),
3030
(NOTE_E2, 1),
3131
(NOTE_D2, 1),

ForceTouchPlayer/Songs/CantinaBand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ let cantinaBand = Song(
6868
(NOTE_AS1, 0.5),
6969
(NOTE_AS1, 0.5),
7070
(NOTE_B1, 1),
71-
(NOTE_G1, 1)
71+
(NOTE_G1, 1),
7272
]
7373
)

0 commit comments

Comments
 (0)