- Notifications
You must be signed in to change notification settings - Fork114
Javascript taught Music Theory
License
saebekassebil/teoria
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Teoria.js is a lightweight and fast JavaScript libraryfor music theory, both Jazz and Classical. It aims at providing an intuitiveprogramming interface for music software (such as Sheet Readers,Sheet Writers, MIDI Players etc.).
A note object (
teoria.Note
), which understands alterations, octaves,key number, frequency and etc. and Helmholtz notationA chord object (
teoria.Chord
), which understands everythingfrom simple major/minor chords to advanced Jazz chords (Ab#5b9, F(#11) and such)A scale object (
teoria.Scale
), The scale object is a powerful presentation ofa scale, which supports quite a few handy methods. A scale can either beconstructed from the predefined scales, which by default contains the 7 modes(Ionian, Dorian, Phrygian etc.) a major and minor pentatonic and the harmonicchromatic scale or from an arbitrary array of intervals. The scale objectalso supports solfège, which makes it perfect for tutorials on sight-reading.An interval object (
teoria.Interval
), which makes it easy to find theinterval between two notes, or find a note that is a given interval from a note.There's also support for counting the interval span in semitones and inverting theinterval.
$ npm install teoria
Can be used with both Node and Browserify/webpack/etc.
Include the bundled build file,teoria.js
from this repository, directly:
<scriptsrc="path/to/teoria.js"></script>
This is just a short introduction to what teoria-code looks like,for a technical library reference, look further down this document.
// Create notes:vara4=teoria.note('a4');// Scientific notationvarg5=teoria.note("g''");// Helmholtz notationvarc3=teoria.note.fromKey(28);// From a piano key number// Find and create notes based on intervalsteoria.interval(a4,g5);// Returns a Interval object representing a minor seventhteoria.interval(a4,'M6');// Returns a Note representing F#5a4.interval('m3');// Returns a Note representing C#4a4.interval(g5);// Returns a Interval object representing a minor seventha4.interval(teoria.note('bb5')).invert();// Returns a Interval representing a major seventh// Create scales, based on notes.a4.scale('mixolydian').simple();// Returns: ["a", "b", "c#", "d", "e", "f#", "g"]a4.scale('aeolian').simple();// Returns: ["a", "b", "c", "d", "e", "f", "g"]g5.scale('ionian').simple();// Returns: ["g", "a", "b", "c", "d", "e", "f#"]g5.scale('dorian');// Returns a Scale object// Create chords with the powerful chord parsera4.chord('sus2').name;// Returns the name of the chord: 'Asus2'c3.chord('m').name;// Returns 'Cm'teoria.chord('Ab#5b9');// Returns a Chord object, representing a Ab#5b9 chordg5.chord('dim');// Returns a Chord object, representing a Gdim chord// Calculate note frequencies or find the note corresponding to a frequencyteoria.note.fromFrequency(467);// Returns: {'note':{...},'cents':3.102831} -> A4# a little out of tune.a4.fq();// Outputs 440g5.fq();// Outputs 783.9908719634985// teoria allows for crazy chaining:teoria.note('a')// Create a note, A3.scale('lydian')// Create a lydian scale with that note as root (A lydian).interval('M2')// Transpose the whole scale a major second up (B lydian).get('third')// Get the third note of the scale (D#4).chord('maj9')// Create a maj9 chord with that note as root (D#maj9).toString();// Make a string representation: 'D#maj9'
name - The name argument is the note name as a string. The note can bothbe expressed in scientific and Helmholtz notation.Some examples of valid note names:Eb4
,C#,,
,C4
,d#''
,Ab2
coord - If the first argument isn't a string, but a coord array,it will instantiate aNote
instance.
duration - The duration argument is an optionalobject
argument.The object has two also optional parameters:
value
- Anumber
corresponding to the value of the duration, such that:1 = whole
,2 = half (minim)
,4 = quarter
,8 = eight
dots
- The number of dots attached to the note. Defaults to0
.
A static method that returns an instance of Note set to the noteat the given piano key, where A0 is key number 1.SeeWikipedia's piano key articlefor more information.
A static method returns an object containing two elements:
note - ANote
which corresponds to the closest note with the given frequency
cents - A number value of how many cents the note is out of tune
- Returns an instance of Note set to the corresponding MIDI note value.
note - A number ranging from 0-127 representing a MIDI note value
- Returns an instance of Note representing the note name
note - The name argument is the note name as a string. The note can bothbe expressed in scientific and Helmholtz notation.Some examples of valid note names:Eb4
,C#,,
,C4
,d#''
,Ab2
- The name of the note, in lowercase letter (only the name, not theaccidental signs)
- The numeric value of the octave of the note
- The duration object as described in the constructor for Note
- Returns the string symbolic of the accidental sign (
x
,#
,b
orbb
)
- Returns the numeric value (mostly used internally) of the sign:
x = 2, # = 1, b = -1, bb = -2
- Returns the piano key number. E.g. A4 would return 49
whitenotes - If this parameter is set totrue
only the white keys willbe counted when finding the key number. This is mostly for internal use.
- Returns a number ranging from 0-127 representing a MIDI note value
- Calculates and returns the frequency of the note.
concertPitch - If supplied this number will be used instead of the normalconcert pitch which is 440hz. This is useful for some classical music.
- Returns the pitch class (index) of the note.
This allows for easy enharmonic checking:
teoria.note('e').chroma() === teoria.note('fb').chroma();
The chroma number is ranging from pitch class C which is 0 to 11 which is B
- Returns an instance of Scale, with the tonic/root set to this note.
scaleName - The name of the scale to be returned.'minor'
,'chromatic'
,'ionian'
and others are valid scale names.
- A sugar function for calling teoria.interval(note, interval);
Look at the documentation forteoria.interval
- Like the
#interval
method, but changesthis
note, instead of returning a new
- Returns an instance of Chord, with root note set to this note
name - The name attribute is the last part of the chord symbol.Examples:'m7'
,'#5b9'
,'major'
. If the name parameterisn't set, a standard major chord will be returned.
- Returns the note name formatted in Helmholtz notation.
Example:teoria.note('A5').helmholtz() -> "a''"
- Returns the note name formatted in scientific notation.
Example:teoria.note("ab'").scientific() -> "Ab4"
- Returns all notes that are enharmonic with the note
oneAccidental - Boolean, if set to true, only enharmonic notes with oneaccidental is returned. E.g. results such as 'eb' and 'c#' but not 'ebb' and 'cx'
teoria.note('c').enharmonics().toString();// -> 'dbb, b#'teoria.note('c').enharmonics(true).toString();// -> 'b#'
- Returns the duration of the note, given a tempo (in bpm) and a beat unit(the lower numeral of the time signature)
- Returns the solfege step in the given scale context
scale - An instance ofScale
, which is the context of the solfege step measuring
showOctaves - A boolean. If set to true, a "Helmholtz-like" notation will beused if there's bigger intervals than an octave
- Returns the duration name.
Examples:teoria.note('A', 8).durationName() -> 'eighth'
,teoria.note('C', 16).durationName() -> 'sixteenth'
- Returns this note's degree in a given scale (Scale). For example a
D
in a C major scale will return2
as it is the second degree of that scale.If however the noteisn't a part of the scale, the degree returned will be0
, meaning that the degree doesn't exist. This allows this method to be botha scale degree index finderand an "isNoteInScale" method.
scale - An instance ofScale
which is the context of the degree measuring
- Usability function for returning the note as a string
dontShow - If set totrue
the octave will not be included in the returned string.
- A chord class with a lot of functionality to alter and analyze the chord.
root - ANote
instance which is to be the root of the chord
chord - A string containing the chord symbol. This can be anything fromsimple chords, to super-advanced jazz chords thanks to the detailed androbust chord parser engine. Example values:'m'
,'m7'
,'#5b9'
,'9sus4
and'#11b5#9'
- A simple function for getting the notes, no matter the octave, in a chord
name - A string containing the full chord symbol, with note name. Examples:'Ab7'
,'F#(#11b5)'
note - Instead of supplying a string containing the full chord symbol,one can pass aNote
object instead. The note will be considered root inthe new chord object
octave - If the first argument of the function is a chord name (typeof "string"
),then the second argument is an optional octave number (typeof "number"
) of the root.
symbol - A string containing the chord symbol (excluding the note name)
- Holds the full chord symbol, inclusive the root name.
- Holds the
Note
that is the root of the chord.
- Returns an array of
Note
s that the chord consists of.
- Returns an
Array
of only the notes' names, not the fullNote
objects.
- Returns the bass note of the chord (The note voiced the lowest)
- Works both as a setter and getter. If no parameter is supplied thecurrent voicing is returned as an array of
Interval
s
voicing - An optional array of intervals in simple-formatthat represents the current voicing of the chord.
Here's an example:
varbbmaj=teoria.chord('Bbmaj7');// Default voicing:bbmaj.voicing();// #-> ['P1', 'M3', 'P5', 'M7'];bbmaj.notes();// #-> ['bb', 'd', 'f', 'a'];// New voicingbbmaj.voicing(['P1','P5','M7','M10']);bbmaj.notes();// #-> ['bb', 'f', 'a', 'd'];
NB: Note that above returned results are pseudo-results, as they will bereturned wrapped inInterval
andNote
objects.
- Returns a string which holds the quality of the chord,
'major'
,'minor'
,'augmented'
,'diminished'
,'half-diminished'
,'dominant'
orundefined
- Returns the note at a given interval in the chord, if it exists.
interval - A string name of an interval, for example'third'
,'fifth'
,'ninth'
.
- Returns the naïvely chosen dominant which is a perfect fifth away.
additional - Additional chord extension, for example:'b9'
or'#5'
- Returns the naïvely chosen subdominant which is a perfect fourth away.
additional - Like the dominant's.
- Returns the parallel chord for major and minor triads
additional - Like the dominant's
- Returns the type of the chord:
'dyad'
,'triad'
,'trichord'
,'tetrad'
or'unknown'
.
- Returns the same chord, a
interval
away
- Like the
#interval
method, except it'sthis
chord that gets changed instead ofreturning a new chord.
- Simple usability function which is an alias for Chord.name
- The teoria representation of a scale, with a given tonic.
tonic - ANote
which is to be the tonic of the scale
scale - Can either be a name of a scale (string), or an array ofabsolute intervals that defines the scale. The scales supported by default are:
- major
- minor
- ionian (Alias for major)
- dorian
- phrygian
- lydian
- mixolydian
- aeolian (Alias for minor)
- locrian
- majorpentatonic
- minorpentatonic
- chromatic
- harmonicchromatic (Alias for chromatic)
- blues
- doubleharmonic
- flamenco
- harmonicminor
- melodicminor
- wholetone
- Sugar function for constructing a new
Scale
object
- An array of all the scale ID's that comes with teoria
- The name of the scale (if available). Type
string
orundefined
- The
Note
which is the scale's tonic
- Returns an array of
Note
s which is the scale's notes
- Returns an
Array
of only the notes' names, not the fullNote
objects.
- Returns the type of the scale, depending on the number of notes.A scale of length x gives y:
- 2 gives 'ditonic'
- 3 gives 'tritonic'
- 4 gives 'tetratonic'
- 5 gives 'pentatonic'
- 6 gives 'hexatonic',
- 7 gives 'heptatonic',
- 8 gives 'octatonic'
- Returns the note at the given scale index
index - Can be a number referring to the scale step, or the name (string) of thescale step. E.g. 'first', 'second', 'fourth', 'seventh'.
- Returns the solfege name of the given scale step
index Same asScale#get
showOctaves - A boolean meaning the same asshowOctaves
inNote#solfege
- A sugar function for the
#from
and#between
methods of the same namespace andfor creatingInterval
objects.
- A sugar method for the
Interval.toCoord
function
- A sugar method for the
Interval.from
function
- Like above, but with a
Interval
instead of a string representation ofthe interval
- A sugar method for the
Interval.between
function
- A representation of a music interval
- Returns a
Interval
representing the interval expressed in string form.
- Returns a note which is a given interval away from a root note.
from - TheNote
which is the root of the measuring
to - AInterval
- Returns an interval object which represents the interval between two notes.
from andto are twoNote
s which are the notes that theinterval is measured from. For example if 'a' and 'c' are given, the resultinginterval object would represent a minor third.
Interval.between(teoria.note("a"),teoria.note("c'"))->teoria.interval('m3')
- Returns the inversion of the interval provided
simpleInterval - An interval represented in simple string form. Examples:
- 'm3' = minor third
- 'P4' = perfect fourth
- 'A4' = augmented fifth
- 'd7' = diminished seventh
- 'M6' = major sixth.
'm' = minor
,'M' = major
,'A' = augmented
and'd' = diminished
The number may be prefixed with a-
to signify that its direction is down. E.g.:
m-3
means a descending minor third, andP-5
means a descending perfect fifth.
- The interval representation of the interval
- The interval number (A ninth = 9, A seventh = 7, fifteenth = 15)
- The value of the interval - That is a ninth = 9, but a downwards ninth is = -9
- Returns thesimpleInterval representation of the interval. E.g.
'P5'
,'M3'
,'A9'
, etc.
- Returns the name of the simple interval (not compound)
- Returns the type of array, either
'perfect'
(1, 4, 5, 8) or'minor'
(2, 3, 6, 7)
- The quality of the interval (
'dd'
,'d'
'm'
,'P'
,'M'
,'A'
or'AA'
)
verbose is set to a truish value, then long quality names are returned:'doubly diminished'
,'diminished'
,'minor'
, etc.
- The direction of the interval
dir - If supplied, then the interval's direction is to thenewDirection
which is either'up'
or'down'
- Returns the
number
of semitones the interval span.
- Returns the simple part of the interval as a Interval. Example:
ignoreDirection - An optional boolean that, if set totrue
, returns the"direction-agnostic" interval. That is the interval with a positive number.
teoria.interval('M17').simple();// #-> 'M3'teoria.interval('m23').simple();// #-> 'm2'teoria.interval('P5').simple();// #-> 'P5'teoria.interval('P-4').simple();// #-> 'P-4'// With ignoreDirection = trueteoria.interval('M3').simple(true);// #->'M3'teoria.interval('m-10').simple(true);// #-> 'm3'
NB: Note that above returned results are pseudo-results, as they will bereturned wrapped inInterval
objects.
- Returns the number of compound intervals
- Returns a boolean value, showing if the interval is a compound interval
- Adds the
interval
to this interval, and returns aInterval
representing the result of the addition
- Returns true if the supplied
interval
is equal to this interval
- Returns true if the supplied
interval
is greater than this interval
- Returns true if the supplied
interval
is smaller than this interval
- Returns the inverted interval as a
Interval
- Returns the relative to default, value of the quality.E.g. a teoria.interval('M6'), will have a relative quality value of 1, as all theintervals defaults to minor and perfect respectively.
About
Javascript taught Music Theory
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.