MusicTheory adds a bunch of basic enums and structs that you can define pretty much any music related data. Most importants are Pitch, Key, Scale and Chord.
All data types conforms Codable, CustomStringConvertable. Pitch, and Accident structs are RawPresentable with Int as well as ExpressibleByIntegerLiteral that you can represent them directly with Ints.
Pitch and Key
All keys can be defined with Key struct.
It has a KeyType where you can set the base key like C, D, A, G, and an Accidental where it can be .natural, .flat, sharp or more specific like .sharps(amount: 3).
You can create Pitches with a Key and octave.
Also, you can create Pitches with MIDI note number. rawValue of a pitch is its MIDI note number.
Pitch, Key, Accidental structs are equatable, + and - custom operators defined for making calculations easier.
Also, there are other helper functions or properties like frequency of a note.
You can define them with directly string representations as well.
let dFlat = Key(type: d, accidental: .flat)
let c4 = Pitch(key: Key(type: .c), octave: 4)
let aSharp: Key = "a#" // Key(type: .a, accidental: .sharp)
let gFlat3: Pitch = "gb3" // or "g♭3" or "Gb3" is Pitch(key: (type: .g, accidental: .flat), octave: 3)
Interval
Intervals are halfsteps between pitches.
They are IntegerLiteral and you can make add/substract them between themselves, notes or note types.
You can build up a custom interval with its quality, degree and semitone properties.
You can build scales or chords from intervals.
Minor, major, perfect, augmented and diminished intervals up to 2 octaves are predefined.
ScaleType and Scale
ScaleType enum defines a lot of readymade scales.
Also, you can create a custom scale type by ScaleType.custom(intervals: [Interval], description: String)
Scale defines a scale with a scale type and root key.
You can generate notes of scale in an octave range.
Also you can generate HarmonicField of a scale.
Harmonic field is all possible triad, tetrad or extended chords in a scale.
let c = Key(type: .c)
let maj: ScaleType = .major
let cMaj = Scale(type: maj, key: c)
ChordType and Chord
ChordType is a struct with ChordParts which are building blocks of chords.
You can define any chord existing with ChordType.
Thirds, fifths, sixths, sevenths and extensions are parts of the ChordType.
Each of them also structs which conforms ChordPart protocol.
Chord defines chords with type and a root key.
You can generate notes of chord in any octave range.
MusicTheory
A music theory library with
Key
,Pitch
,Interval
,Scale
andChord
representations in swift enums.Requirements
Install
CocoaPods
Swift Package Manager
Usage
MusicTheory
adds a bunch of basic enums and structs that you can define pretty much any music related data. Most importants arePitch
,Key
,Scale
andChord
.All data types conforms
Codable
,CustomStringConvertable
.Pitch
, andAccident
structs areRawPresentable
withInt
as well asExpressibleByIntegerLiteral
that you can represent them directly withInt
s.Pitch
andKey
Key
struct.KeyType
where you can set the base key like C, D, A, G, and anAccidental
where it can be.natural
,.flat
,sharp
or more specific like.sharps(amount: 3)
.Pitch
es with aKey
and octave.Pitch
es with MIDI note number.rawValue
of a pitch is its MIDI note number.Pitch
,Key
,Accidental
structs are equatable,+
and-
custom operators defined for making calculations easier.Interval
IntegerLiteral
and you can make add/substract them between themselves, notes or note types.ScaleType
andScale
ScaleType
enum defines a lot of readymade scales.ScaleType.custom(intervals: [Interval], description: String)
Scale
defines a scale with a scale type and root key.HarmonicField
of a scale.ChordType
andChord
ChordType
is a struct withChordPart
s which are building blocks of chords.ChordType
.ChordType
.ChordPart
protocol.Chord
defines chords with type and a root key.ChordProgression
enum.Tempo
andTimeSignature
NoteValue
of each beat.NoteValue
.HarmonicFunctions
Playgrounds
Documentation
Full documentation is here
Unit Tests
You can find unit tests in
MusicTheoryTests
target.Press
⌘+U
for running tests.AppStore
This library battle tested in my apps for iOS, macOS, watchOS and tvOS, check them out!
KeyBud (iOS, watchOS, tvOS, macOS)
FretBud (iOS, watchOS, tvOS)
ChordBud (iOS)
ArpBud (iOS)
ScaleBud (iOS, AUv3, M1)
StepBud (iOS, AUv3, M1)
RhythmBud (iOS, AUv3, M1)
ArpBud 2 (iOS, AUv3, M1)
ChordBud 2 (iOS, AUv3, M1)
LoopBud (iOS, AUv3, M1)
Euclid Goes to Party (iOS, AUv3, M1)
SnakeBud (iOS, AUv3, M1)
MelodyBud (iOS, AUv3, M1)
ScaleBud 2 (iOS, AUv3, M1)
ShiftBud (iOS, AUv3, M1)
PolyBud (iOS, AUv3, M1)
PatternBud (iOS, AUv3, M1)
MIDI Motion (iOS, watchOS)
Textquencer (iOS, AUv3, M1)
In Theory (iOS, AUv3, M1)
BrainBud (iOS, AUv3, M1)
Binarhythmic (iOS, AUv3, M1)