Skip to content

Commit f824c64

Browse files
committed
Add playground
1 parent 283e90b commit f824c64

File tree

5 files changed

+111
-12
lines changed

5 files changed

+111
-12
lines changed

Pitchy.xcodeproj/project.pbxproj

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
D512C1311C319C82002DD504 /* AcousticWaveSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AcousticWaveSpec.swift; sourceTree = "<group>"; };
7070
D512C1321C319C82002DD504 /* NoteSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NoteSpec.swift; sourceTree = "<group>"; };
7171
D512C1331C319C82002DD504 /* PitchSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PitchSpec.swift; sourceTree = "<group>"; };
72+
D51575A01C330432006F8E75 /* PitchyPlayground-iOS.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = "PitchyPlayground-iOS.playground"; sourceTree = "<group>"; };
7273
D5DF49B51C2EE67800343F13 /* NoteCalculator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NoteCalculator.swift; sourceTree = "<group>"; };
7374
D5DF49B61C2EE67800343F13 /* PitchCalculator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PitchCalculator.swift; sourceTree = "<group>"; };
7475
D5DF49B71C2EE67800343F13 /* WaveCalculator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WaveCalculator.swift; sourceTree = "<group>"; };
@@ -150,6 +151,7 @@
150151
D5DF498E1C2EE43D00343F13 = {
151152
isa = PBXGroup;
152153
children = (
154+
D51575A01C330432006F8E75 /* PitchyPlayground-iOS.playground */,
153155
D5DF4A2F1C2EEAF200343F13 /* Quick.framework */,
154156
D5DF4A301C2EEAF200343F13 /* Nimble.framework */,
155157
D5DF4A2B1C2EEAE400343F13 /* Quick.framework */,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Pitchy
2+
3+
import Pitchy
4+
5+
// MARK: - Pitch
6+
do {
7+
let pitch = try Pitch(frequency: 445.0)
8+
let pitchOffsets = pitch.offsets
9+
10+
print(pitchOffsets.lower.frequency) // 5 Hz
11+
print(pitchOffsets.lower.percentage) // 19.1%
12+
print(pitchOffsets.lower.note.index) // 0
13+
print(pitchOffsets.lower.cents) // 19.56
14+
15+
print(pitchOffsets.higher.frequency) // -21.164 Hz
16+
print(pitchOffsets.higher.percentage) // -80.9%
17+
print(pitchOffsets.higher.note.index) // 1
18+
print(pitchOffsets.higher.cents) // -80.4338
19+
20+
print(pitchOffsets.closest.note.string) // "A4"
21+
22+
print(pitch.wave.wavelength) // 0.7795 meters
23+
} catch {
24+
// Handle errors
25+
}
26+
27+
// MARK: - Acoustic Wave
28+
29+
do {
30+
// AcousticWave(wavelength: 0.7795)
31+
// AcousticWave(period: 0.00227259)
32+
let wave = try AcousticWave(frequency: 440.0)
33+
34+
print(wave.frequency) // 440 Hz
35+
print(wave.wavelength) // 0.7795 meters
36+
print(wave.period) // 0.00227259 s
37+
print(wave.harmonics[0]) // 440 Hz
38+
print(wave.harmonics[1]) // 880 Hz
39+
} catch {
40+
// Handle errors
41+
}
42+
43+
// MARK: - Note
44+
45+
do {
46+
// Note(frequency: 261.626)
47+
// Note(letter: .C, octave: 4)
48+
let note = try Note(index: -9)
49+
50+
print(note.index) // -9
51+
print(note.letter) // .C
52+
print(note.octave) // 4
53+
print(note.frequency) // 261.626 Hz
54+
print(note.string) // "C4"
55+
print(try note.lower().string) // "B3"
56+
print(try note.higher().string) // "C#4"
57+
} catch {
58+
// Handle errors
59+
}
60+
61+
// MARK: - Calculators
62+
63+
do {
64+
// PitchCalculator
65+
let pitchOffsets = try PitchCalculator.offsets(445.0)
66+
let cents = try PitchCalculator.cents(frequency1: 440.0,
67+
frequency2: 440.0) // 19.56
68+
69+
// NoteCalculator
70+
let frequency1 = try NoteCalculator.frequency(index: 0) // 440.0 Hz
71+
let letter = try NoteCalculator.letter(index: 0) // .A
72+
let octave = try NoteCalculator.octave(index: 0) // 4
73+
let index1 = try NoteCalculator.index(frequency: 440.0) // 0
74+
let index2 = try NoteCalculator.index(letter: .A, octave: 4) // 0
75+
76+
// WaveCalculator
77+
let f = try WaveCalculator.frequency(wavelength: 0.7795) // 440.0 Hz
78+
let wl1 = try WaveCalculator.wavelength(frequency: 440.0) // 0.7795 meters
79+
let wl2 = try WaveCalculator.wavelength(period: 0.00227259) // 0.7795 meters
80+
let period = try WaveCalculator.period(wavelength: 0.7795) // 0.00227259 s
81+
} catch {
82+
// Handle errors
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>

README.md

+16-12
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,19 @@ do {
7979
Get an acoustic wave with wavelength, period and harmonics.
8080

8181
```swift
82-
// AcousticWave(wavelength: 0.7795)
83-
// AcousticWave(period: 0.00227259)
84-
let wave = AcousticWave(frequency: 440.0)
85-
86-
print(wave.frequency) // 440 Hz
87-
print(wave.wavelength) // 0.7795 meters
88-
print(wave.period) // 0.00227259 s
89-
print(wave.harmonics[0]) // 440 Hz
90-
print(wave.harmonics[1]) // 880 Hz
82+
do {
83+
// AcousticWave(wavelength: 0.7795)
84+
// AcousticWave(period: 0.00227259)
85+
let wave = try AcousticWave(frequency: 440.0)
86+
87+
print(wave.frequency) // 440 Hz
88+
print(wave.wavelength) // 0.7795 meters
89+
print(wave.period) // 0.00227259 s
90+
print(wave.harmonics[0]) // 440 Hz
91+
print(wave.harmonics[1]) // 880 Hz
92+
} catch {
93+
// Handle errors
94+
}
9195
```
9296

9397
### Note
@@ -98,7 +102,7 @@ a pitch index.
98102
do {
99103
// Note(frequency: 261.626)
100104
// Note(letter: .C, octave: 4)
101-
note = try Note(index: -9)
105+
let note = try Note(index: -9)
102106

103107
print(note.index) // -9
104108
print(note.letter) // .C
@@ -128,8 +132,8 @@ do {
128132
let frequency1 = try NoteCalculator.frequency(index: 0) // 440.0 Hz
129133
let letter = try NoteCalculator.letter(index: 0) // .A
130134
let octave = try NoteCalculator.octave(index: 0) // 4
131-
let index = try NoteCalculator.index(frequency: 440.0) // 0
132-
let index = try NoteCalculator.index(letter: .A, octave: 4) // 0
135+
let index1 = try NoteCalculator.index(frequency: 440.0) // 0
136+
let index2 = try NoteCalculator.index(letter: .A, octave: 4) // 0
133137

134138
// WaveCalculator
135139
let f = try WaveCalculator.frequency(wavelength: 0.7795) // 440.0 Hz

0 commit comments

Comments
 (0)