Swiftpack.co - Package - cemolcay/MusicTheory

MusicTheory Build Status

A music theory library with Key, Pitch, Interval, Scale and Chord representations in swift enums.

Requirements

  • Swift 4.0+
  • iOS 8.0+
  • macOS 10.9+
  • tvOS 9.0+
  • watchOS 2.0+

Install

CocoaPods

pod 'MusicTheorySwift'

Swift Package Manager

let package = Package(
  name: ...
  dependencies: [
    .package(url: "https://github.com/cemolcay/MusicTheory.git")
  ],
  targets: ...
)

Usage

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.
  • You can generate inversions of any chord.
let m13 = ChordType(
  third: .minor,
  seventh: .dominant,
  extensions: [
    ChordExtensionType(type: .thirteenth)
  ])
let cm13 = Chord(type: m13, key: Key(type: .c))
  • You can generate chord progressions with ChordProgression enum.
  • For any scale, in any harmonic field, for any inversion.
let progression = ChordProgression.i_ii_vi_iv
let cSharpHarmonicMinorTriadsProgression = progression.chords(
  for: cSharpHarmonicMinor,
  harmonicField: .triad,
  inversion: 0)

Tempo and TimeSignature

  • Tempo is a helper struct to define timings in your music app.
  • TimeSignature is number of beats in per measure and NoteValue of each beat.
  • You can calculate notes duration in any tempo by ther NoteValue.
  • Note value defines the note's duration in a beat. It could be whole note, half note, quarter note, 8th, 16th or 32nd note.

Documentation

Full documentation are 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)
StepBud (iOS, AUv3)
RhythmBud (iOS, AUv3)

Github

link
Stars: 200

Dependencies

Used By

Total: 0

Releases

Added Swift Package Manager Support - 2019-06-17 08:43:38

Update Swift 5 - 2019-04-30 07:46:44

Add CustomChordType - 2019-04-09 00:57:09

Represents a custom chord type for the chords that can not be represented by the current data structures. Can be initialized with the custom intervals.

Data structure changes and new chord functionality - 2018-12-09 13:11:41

  • Generate roman numerics for chords
  • Make ScaleType and ChordProgression struct instead of enum

Added Chord Progressions - 2018-08-24 16:24:50

You can create chord progressions now. It supports custom ones with Codable protocol, that you can store and use later.

let progression = ChordProgression.i_ii_vi_iv
let cSharpHarmonicMinorTriadsProgression = progression.chords(
  for: cSharpHarmonicMinor,
  harmonicField: .triad,
  inversion: 0)

ExpressibleByStringLiteral Support - 2018-08-10 06:39:35

You can create Pitches and Keys from string now.

let p: Pitch = "c#2"
let k: Key = "a#"

v1.0.0 - 2018-06-26 14:54:01

Breaking Changes

  • Note and NoteType structs removed.
  • You need to use Key instead of NoteType with a KeyType and Accidental.
  • You need to use Pitch instead of Note with a Key and octave (Int).
  • Scale and Chord structs notes(..) functions renamed pitches(...) and they are returning [Pitch] instead of [Note].
  • Scale and Chord structs noteTypes property renamed keys and it returns [Key] instead of [NoteType].
  • New Accidental struct added. You can define accidents like sharp, flat, doubleSharp or more spesific like .sharps(amount: 3).
  • Interval struct rewritten. Now it is a struct instead of an enum. But all important intervals as in old version added as static variables and it probably won't break anything.

1.0.0 Pre-Release - 2018-06-22 14:04:40

Breaking Changes

  • Note and NoteType structs removed.
  • You need to use Key instead of NoteType with a KeyType and Accidental.
  • You need to use Pitch instead of Note with a Key and octave (Int).
  • Scale and Chord structs notes(..) functions renamed pitches(...) and they are returning [Pitch] instead of [Note].
  • Scale and Chord structs noteTypes property renamed keys and it returns [Key] instead of [NoteType].
  • New Accidental struct added. You can define accidents like sharp, flat, doubleSharp or more spesific like .sharps(amount: 3).

Swift 4.1 - 2018-05-12 16:13:49

Migrate Swift 4.0 with Codable protocol - 2017-10-31 05:38:08

  • Migrate from Swift 3.2 to Swift 4.0
  • All structs and enums conform Codable.
  • NoteValueType and NoteModifier enums are Double type now, unnecessary props removed.

Completely new Chords - 2017-10-23 23:19:16

  • Chords rewritten
  • Now you can define all chords existing.
  • Added extended chords for generating harmonic fields.

Swift 4.0 Support - 2017-10-03 05:32:02

  • Harmonic Fields! Now you can generate triad or tetrad chords for each Scale.
  • Chord inversions added.
  • Add Equatable protocol to Interval, Scale, ScaleType, Chord, ChordType
  • Could initilize ChrodTypes and ScaleTypes from intervals.
  • Updated documentation.
  • Updated unit tests.

Added TimeSignature, Tempo and NoteValue data structures - 2017-07-17 00:13:07

Now you can calculate note durations with newly added Tempo, TimeSignature and NoteValue data structures. For example in a 4/4 time signature, 120BPM tempo, a quarter note would be 0.5 seconds. Also you can calculate hertz of a note for LFO speed.

Added new scales and chords - 2017-03-01 16:10:09

  • All scales and chords added from https://www.apassion4jazz.net/keys.html.
  • Dataset.swift file deleted.
  • ScaleType.all and ChrodType.all static properties moved to their enums from deleted Dataset.swift file.
  • Implemented ExpressibleByIntegerLiteral protocol in Interval enum

Shared library - 2017-02-09 19:45:11

Ready to SwiftPackageManager with shared swift library

  • iOS 8.0+
  • tvOS 9.0+
  • macOS 10.9+
  • watchOS 2.0+

Classified new version - 2017-01-07 17:46:52

let cmin = Scale.minor(key: .c)

updated with

let min = ScaleType.min // enum
let cmin = Scale(type: min, key: .c) // struct

style for all Note, Scale and Chord types. We have NoteType, ScaleType and ChordType enums, key and octave agnostic. We have Note, Scale and Chord structs with keys and octaves as well as some handy operation functions.

Initial Release - 2017-01-02 22:46:09

Core functions with Note and basic Intervals, Scales and Chords.