Skip to content

soundio/music

Repository files navigation

music

A library of functions for analysing and transforming musical information.

.consonance(array)

Takes an array of note numbers and returns a number from 0-1 that rates the consonance of that group of notes.

// Silence is golden
music.consonance([])                    // 1

// A single tone is consonant
music.consonance([80])                  // 1

// A major third is less consonant
music.consonance([64, 68])              // 0.5

// C7♭9 is really quite dissonant
music.consonance([60, 61, 64, 67, 70])  // 0.129

.density(array)

Takes an array of note numbers and rates the density of the group of notes between 0 and 1. The more notes there are closer together, the higher the density.

music.density([45, 56])                 // 0.1666
music.density([45, 50, 56])             // 0.25

.range(array)

Takes an array of note numbers and returns the difference between the minimum and maximum.

.toScale(array)

Collapse an array of notes into 1 octave to make a scale.

Returns a [scale, transpose] pair, where scale is an ascending array of unique numbers starting with 0 and in the range 0-12, and transpose is a number representing the transposition needed to put it in the key of the notes in array.

.findModes(array)

Find modes where all the notes in array can be found.

Returns an array of [mode, transpose] pairs, representing all modes that are supersets of, or an exact match to toScale(array), and their transpose values.

.transpose(array, n)

Takes an array of note numbers and transposes them by n.

.chromaticism(array1, array2)

Returns a number in the range 0-1 representing the ratio of note numbers in array2 that are a chromatic half-step away from note numbers in array1.

// Two of these three notes are a chromatic half-step away
music.chromaticism([61, 65, 68], [60, 64, 70])  // 0.666

.parallelism(array1, array2)

Returns a number in the range 0-1 representing the ratio of note numbers in array2 that can be considered part of a group that has moved chromatically as a block from note numbers in array1.

// Two of these three notes move in parallel
music.chromaticism([61, 65, 68], [59, 63, 70])  // 0.666

.contraryParallelism(array1, array2)

Returns a number in the range 0-1 representing the ratio of note numbers in array2 that can be considered part of a group that has moved chromatically as a block from note numbers in array1, where there is more than one group and they move by different distances.

// All four of these notes belong to a group that moves in parallel,
// with each group moving contrary to the other
music.chromaticism([61, 65, 68, 70], [59, 63, 70, 72])  // 1

Contrary parallelism requires chords of at least four notes. If either array is less than length 4, contraryParallelism returns 0.