README

Music Theory

Build Status GoDoc

Explorations in music theory.

Usage

package main

import (
    mt "github.com/brettbuddin/musictheory"
    "github.com/brettbuddin/musictheory/intervals"
)

func main() {
    root := mt.NewPitch(mt.C, mt.Natural, 4)

    root.Name(mt.AscNames) // C4
    root.Freq()            // 261.625565 (Hz)
    root.MIDI()            // 72

    P5 := mt.Perfect(5)   // Perfect 5th
    A4 := mt.Augmented(4) // Augmented 4th

    root.Transpose(P5).Name(mt.AscNames)          // G4
    root.Transpose(A4).Name(mt.AscNames)          // F#4
    root.Transpose(P5.Negate()).Name(mt.AscNames) // F3

    mt.NewScale(root, intervals.Dorian, 1)
    // [C4, D4, Eb4, F4, G4, A4, Bb4]

    mt.NewScale(root, intervals.Mixolydian, 2)
    // [C4, D4, E4, F4, G4, A4, Bb4, C5, D5, E5, F5, G5, A5, Bb5]

    rootPosition := mt.NewChord(root, intervals.DiminishedSeventh)
    // [C4, Eb4, Gb4, A4]

    firstInversion := rootPosition.Invert(1)
    // [C5, Eb4, Gb4, A4]

    secondInversion := rootPosition.Invert(2)
    // [C5, Eb5, Gb4, A4]

    note := mt.NewNote(root, mt.D16) // C4 sixteenth note
    note.Time(mt.D4, 120)            // 125ms (quarter note getting the beat at 120 BPM)
}

楽しみます!

Documentation

Index

Constants

View Source
const (
	DoubleFlat  = -2
	Flat        = -1
	Natural     = 0
	Sharp       = 1
	DoubleSharp = 2
)

Modifiers

View Source
const (
	C int = iota + 1
	D
	E
	F
	G
	A
	B
)

Note naturals

Variables

View Source
var (
	D1   = Duration{1, 0, false}   // Whole
	D2   = Duration{2, 0, false}   // Half
	D4   = Duration{4, 0, false}   // Quarter
	D8   = Duration{8, 0, false}   // Eighth
	D16  = Duration{16, 0, false}  // Sixteenth
	D32  = Duration{32, 0, false}  // Thirty Second
	D64  = Duration{64, 0, false}  // Sixty Fourth
	D128 = Duration{128, 0, false} // Hundred Twenty Eighth
)

Durations

Functions

func AscNames

func AscNames(i int) int

AscNames maps an modifier to a correspending diatonic as sharps

func DescNames

func DescNames(i int) int

DescNames maps an modifier to a correspending diatonic as flats

Types

type Chord

type Chord []Pitch

Chord is a series of Pitches intended to be played at the same time

func NewChord

func NewChord(root Pitch, intervals []Interval) Chord

NewChord returns a new Chord with a specific set of intervals

func (Chord) Invert

func (c Chord) Invert(degree int) Chord

Invert performs a chord inversion of some degree

func (Chord) Transpose

func (c Chord) Transpose(i Interval) Chord

Transpose transposes the Chord

type Duration

type Duration struct {
	Value   int
	Dots    int
	Triplet bool
}

Duration represents a note's duration

func Dotted

func Dotted(d Duration, dots int) Duration

Dotted makes a dotted duration

func Triplet

func Triplet(d Duration) Duration

Triplet makes a triplet duration

func (Duration) String

func (d Duration) String() string

func (Duration) Time

func (d Duration) Time(unit Duration, bpm int) time.Duration

Time returns the time in nanoseconds the note's duration lasts. Calculated based on what unit gets the beat and what the BPM is.

type Interval

type Interval struct {
	Octaves   int
	Diatonic  int
	Chromatic int
}

Interval represents an interval in 12-tone equal temperament

func Augmented

func Augmented(step int) Interval

Augmented interval

func Diminished

func Diminished(step int) Interval

Diminished interval

func DoublyAugmented

func DoublyAugmented(step int) Interval

DoublyAugmented interval

func DoublyDiminished

func DoublyDiminished(step int) Interval

DoublyDiminished interval

func Major

func Major(step int) Interval

Major interval

func Minor

func Minor(step int) Interval

Minor interval

func NewInterval

func NewInterval(step, octaves, offset int) Interval

NewInterval builds a new Interval

func Octave

func Octave(step int) Interval

Octave interval

func ParseInterval

func ParseInterval(str string) (Interval, error)

func Perfect

func Perfect(step int) Interval

Perfect interval

func Semitones

func Semitones(step int) Interval

Semitones is an interval using direct semitones

func (Interval) Eq

func (i Interval) Eq(o Interval) bool

Eq determines if another interval is the same

func (Interval) Negate

func (i Interval) Negate() Interval

Negate returns a new, negated Interval

func (Interval) Quality

func (i Interval) Quality() Quality

Quality returns the Quality

func (Interval) Ratio

func (i Interval) Ratio() float64

Ratio returns the interval ratio

func (Interval) Semitones

func (i Interval) Semitones() int

Semitones returns the total number of semitones that make up the interval

func (Interval) String

func (i Interval) String() string

func (Interval) Transpose

func (i Interval) Transpose(o Interval) Interval

Transpose returns a new Interval that has been transposed by the given Interval

type IntervalFunc

type IntervalFunc func(int) Interval

IntervalFunc creates an interval at as specific step/degree

type ModifierStrategy

type ModifierStrategy func(int) int

ModifierStrategy is a function that maps a modifier to a diatonic

type Note

type Note struct {
	Pitch
	Duration
}

Note is a pitch with a duration

func NewNote

func NewNote(pitch Pitch, duration Duration) Note

NewNote creates a new note

func (Note) String

func (n Note) String() string

func (Note) Transpose

func (n Note) Transpose(i Interval) Note

Transpose transposes a note by a given interval

type Pitch

type Pitch struct {
	Interval
}

Pitch represents an absolute pitch in 12-tone equal temperament

func MustParsePitch

func MustParsePitch(str string) Pitch

MustParsePitch parses and returns a Pitch in scientific pitch notation or panics

func NearestPitch

func NearestPitch(f float64) Pitch

NearestPitch returns the closest pitch to an arbitrary frequency

func NewPitch

func NewPitch(diatonic, modifier, octaves int) Pitch

NewPitch builds a new Pitch

func ParsePitch

func ParsePitch(str string) (Pitch, error)

ParsePitch parses and returns a Pitch in scientific pitch notation

func (Pitch) Eq

func (p Pitch) Eq(o Pitch) bool

Eq determines if another pitch is the same

func (Pitch) Freq

func (p Pitch) Freq() float64

Freq returns the absolute frequency of a pitch in Hz

func (Pitch) MIDI

func (p Pitch) MIDI() int

MIDI returns the MIDI note number of the pitch

func (Pitch) Name

func (p Pitch) Name(strategy ModifierStrategy) string

Name returns the name of the pitch using a particular name strategy (either AscNames or DescNames). The result is in scientific pitch notation format.

func (Pitch) String

func (p Pitch) String() string

func (Pitch) Transpose

func (p Pitch) Transpose(i Interval) Pitch

Transpose transposes a pitch by a given interval

type Quality

type Quality struct {
	Type QualityType
	Size int
}

Quality describes the quality of an interval

func (Quality) Eq

func (q Quality) Eq(o Quality) bool

Eq checks two Qualities for equality

func (Quality) Invert

func (q Quality) Invert() Quality

Invert returns a new, inverted Quality

func (Quality) String

func (q Quality) String() string

type QualityType

type QualityType int

QualityType represents the type a Quality can take

const (
	PerfectType QualityType = iota
	MajorType
	MinorType
	AugmentedType
	DiminishedType
)

Quality types

func (QualityType) String

func (q QualityType) String() string

type Scale

type Scale []Pitch

Scale is a series of Pitches

func NewScale

func NewScale(root Pitch, intervals []Interval, octaves int) Scale

NewScale returns a Scale built using a set of intervals

func (Scale) Transpose

func (s Scale) Transpose(i Interval) Scale

Transpose transposes a scale by the specified Interval

Directories

Path Synopsis
cmd