musictheory

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 28, 2017 License: MIT Imports: 5 Imported by: 11

README

Music Theory

Build Status GoDoc

Explorations in music theory.

Usage

package main

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

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).(mt.Pitch).Name(mt.AscNames)          // G4
	root.Transpose(A4).(mt.Pitch).Name(mt.AscNames)          // F#4
	root.Transpose(P5.Negate()).(mt.Pitch).Name(mt.AscNames) // F3

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

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

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

楽しみます!

TODO

  • Chords and inversions
  • ...
  • Write a tool or two that uses this stuff...

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 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 (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) Transposer

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) Transposer

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 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) Transpose

func (p Pitch) Transpose(i Interval) Transposer

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 []Transposer

Scale is a series of Transposers

func NewScale

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

NewScale returns a Scale built using a set of intervals

func (Scale) Transpose

func (s Scale) Transpose(i Interval) Transposer

Transpose transposes a scale by the specified Interval

type Transposer

type Transposer interface {
	Transpose(Interval) Transposer
}

Transposer is something that shifts by an Interval

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL