Skip to content

Commit eb3171b

Browse files
committed
Merge pull request #16 from vadymmarkov/feature/playgrounds
Feature: playgrounds
2 parents d8a7d7f + f824c64 commit eb3171b

File tree

5 files changed

+122
-21
lines changed

5 files changed

+122
-21
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

+27-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
![Cache](https://github.com/vadymmarkov/Pitchy/blob/master/Resources/PitchyPresentation.png)
1+
![Pitchy](https://github.com/vadymmarkov/Pitchy/blob/master/Resources/PitchyPresentation.png)
22

33
[![CI Status](http://img.shields.io/travis/vadymmarkov/Pitchy.svg?style=flat)](https://travis-ci.org/vadymmarkov/Pitchy)
44
[![Version](https://img.shields.io/cocoapods/v/Pitchy.svg?style=flat)](http://cocoadocs.org/docsets/Pitchy)
@@ -24,14 +24,14 @@
2424

2525
## Description
2626

27-
<img src="https://github.com/vadymmarkov/Pitchy/blob/master/Resources/PitchyIcon.png" alt="Cache Icon" align="right" />
27+
<img src="https://github.com/vadymmarkov/Pitchy/blob/master/Resources/PitchyIcon.png" alt="Pitchy Icon" align="right" />
2828
**Pitchy** provides a simple way to get a music pitch from a frequency. Other
2929
than that it has a bunch of useful data structures, calculators and helper
3030
functions to work with notes, octaves and acoustic waves.
3131

3232
From [Wikipedia](https://en.wikipedia.org/wiki/Pitch_(music)):
3333
> Pitch is a perceptual property of sounds that allows their ordering on a
34-
> frequency-related scale,[1] or more commonly, pitch is the quality that makes
34+
> frequency-related scale, or more commonly, pitch is the quality that makes
3535
> it possible to judge sounds as "higher" and "lower" in the sense associated
3636
> with musical melodies.
3737
@@ -68,7 +68,7 @@ do {
6868

6969
print(pitchOffsets.closest.note.string) // "A4"
7070

71-
// You could also use acoustic wave of the pitch
71+
// You could also use acoustic wave
7272
print(pitch.wave.wavelength) // 0.7795 meters
7373
} catch {
7474
// Handle errors
@@ -79,24 +79,30 @@ 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 seconds
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+
Note could be created with a corresponding frequency, letter + octave number or
99+
a pitch index.
94100

95101
```swift
96102
do {
97103
// Note(frequency: 261.626)
98104
// Note(letter: .C, octave: 4)
99-
note = try Note(index: -9)
105+
let note = try Note(index: -9)
100106

101107
print(note.index) // -9
102108
print(note.letter) // .C
@@ -126,14 +132,14 @@ do {
126132
let frequency1 = try NoteCalculator.frequency(index: 0) // 440.0 Hz
127133
let letter = try NoteCalculator.letter(index: 0) // .A
128134
let octave = try NoteCalculator.octave(index: 0) // 4
129-
let index = try NoteCalculator.index(frequency: 440.0) // 0
130-
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
131137

132138
// WaveCalculator
133139
let f = try WaveCalculator.frequency(wavelength: 0.7795) // 440.0 Hz
134-
let wl1 = try WaveCalculator.wavelength(frequency: 440.0) // 0.7795
135-
let wl2 = try WaveCalculator.wavelength(period: 0.00227259) // 0.7795
136-
let period = try WaveCalculator.period(wavelength: 0.7795) // 0.00227259
140+
let wl1 = try WaveCalculator.wavelength(frequency: 440.0) // 0.7795 meters
141+
let wl2 = try WaveCalculator.wavelength(period: 0.00227259) // 0.7795 meters
142+
let period = try WaveCalculator.period(wavelength: 0.7795) // 0.00227259 s
137143
} catch {
138144
// Handle errors
139145
}
@@ -142,7 +148,7 @@ do {
142148
### Config
143149

144150
With a help of `Config` it's possible to adjust minimum and maximum frequencies
145-
that are used for validation in all calculations:
151+
that are used for validations in all calculations:
146152

147153
```swift
148154
Config.minimumFrequency = 20.0
@@ -187,7 +193,7 @@ Vadym Markov, markov.vadym@gmail.com
187193

188194
## Contributing
189195

190-
Check the [CONTRIBUTING](https://github.com/hyperoslo/Cache/blob/master/CONTRIBUTING.md)
196+
Check the [CONTRIBUTING](https://github.com/vadymmarkov/Pitchy/blob/master/CONTRIBUTING.md)
191197
file for more info.
192198

193199
## License

0 commit comments

Comments
 (0)