Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions src/arpeggio.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {chord as getChord} from './chords';
import {PERFECT_OCTAVE} from './intervals';
import { chord as getChord } from './chords';
import { PERFECT_OCTAVE } from './intervals';

const DIRECTION_DOWN = 'down';
const DIRECTION_UP = 'up';
Expand All @@ -13,19 +13,19 @@ const DIRECTION_UP = 'up';
*
* @returns {array}
*/
export function arpeggio(chord, length, direction) {
chord = getChord(chord);
if (direction === DIRECTION_DOWN) {
chord = chord.reverse();
}
let notes = [];
for (let i = 0; i < length; i++) {
let note = chord[i % chord.length];
let octave = Math.floor(i / chord.length) * PERFECT_OCTAVE;
if (direction === DIRECTION_DOWN) {
octave = -octave;
}
notes.push(note + octave);
}
return notes;
}
exports.arpeggio = (chord, length, direction) => {
chord = getChord(chord);
if (direction === DIRECTION_DOWN) {
chord = chord.reverse();
}
let notes = [];
for (let i = 0; i < length; i++) {
let note = chord[i % chord.length];
let octave = Math.floor(i / chord.length) * PERFECT_OCTAVE;
if (direction === DIRECTION_DOWN) {
octave = -octave;
}
notes.push(note + octave);
}
return notes;
};
64 changes: 32 additions & 32 deletions src/chords.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//prettier-ignore
import {PERFECT_UNISON, MAJOR_THIRD, PERFECT_FIFTH, MINOR_THIRD,
AUGMENTED_FIFTH, DIMINISHED_FIFTH, PERFECT_OCTAVE} from './intervals';
import {note} from './notes';
import { note } from './notes';

/* Chord qualities */
const MAJOR_TRIAD = [PERFECT_UNISON, MAJOR_THIRD, PERFECT_FIFTH];
Expand All @@ -11,27 +12,26 @@ const DIMINISHED_TRIAD = [PERFECT_UNISON, MINOR_THIRD, DIMINISHED_FIFTH];
/* Chord inversions */
const ROOT_POSITION = [PERFECT_UNISON, PERFECT_UNISON, PERFECT_UNISON];
const FIRST_INVERSION = [PERFECT_OCTAVE, PERFECT_UNISON, PERFECT_UNISON];
const SECOND_INVERSION = [PERFECT_OCTAVE, PERFECT_OCTAVE,
PERFECT_UNISON];
const SECOND_INVERSION = [PERFECT_OCTAVE, PERFECT_OCTAVE, PERFECT_UNISON];

const CHORD_REGEX = /^([cdefgab])#?([0-9]|)(M|m|maj|min|dim|aug|)$/i;

const DEFAULT_OCTAVE = 4;

var Qualities = {
'' : MAJOR_TRIAD,
'M' : MAJOR_TRIAD,
'maj' : MAJOR_TRIAD,
'm' : MINOR_TRIAD,
'min' : MINOR_TRIAD,
'dim' : DIMINISHED_TRIAD,
'aug' : AUGMENTED_TRIAD
'': MAJOR_TRIAD,
M: MAJOR_TRIAD,
maj: MAJOR_TRIAD,
m: MINOR_TRIAD,
min: MINOR_TRIAD,
dim: DIMINISHED_TRIAD,
aug: AUGMENTED_TRIAD,
};

var Inversions = {
0 : ROOT_POSITION,
1 : FIRST_INVERSION,
2 : SECOND_INVERSION
0: ROOT_POSITION,
1: FIRST_INVERSION,
2: SECOND_INVERSION,
};

/**
Expand All @@ -42,29 +42,29 @@ var Inversions = {
*
* @returns {array} Chord
*/
export function chord(chord, inversion = 0) {
if (Array.isArray(chord)) {
return chord;
}
exports.chord = (chord, inversion = 0) => {
if (Array.isArray(chord)) {
return chord;
}

let chordNotes = [];
let [, root, octave, quality] = chord.match(CHORD_REGEX);
let chordNotes = [];
let [, root, octave, quality] = chord.match(CHORD_REGEX);

if (octave === '') {
octave = DEFAULT_OCTAVE;
}
if (octave === '') {
octave = DEFAULT_OCTAVE;
}

root = note(root + octave);
quality = Qualities[quality];
inversion = Inversions[inversion];
root = note(root + octave);
quality = Qualities[quality];
inversion = Inversions[inversion];

quality.forEach(function(value, index) {
chordNotes.push(value + root + inversion[index]);
});
quality.forEach(function (value, index) {
chordNotes.push(value + root + inversion[index]);
});

return chordNotes;
return chordNotes;
};

export function registerChord(quality, intervals) {
Qualities[quality] = intervals;
}
exports.registerChord = (quality, intervals) => {
Qualities[quality] = intervals;
};
15 changes: 8 additions & 7 deletions src/notes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// prettier-ignore
const NOTES = {
'C0' : 0, 'C#0' : 1, 'D0' : 2, 'D#0' : 3, 'E0' : 4, 'F0' : 5, 'F#0' : 6,
'G0' : 7, 'G#0' : 8, 'A0' : 9, 'A#0' : 10, 'B0' : 11, 'C1' : 12, 'C#1' : 13,
Expand All @@ -23,10 +24,10 @@ const NOTES = {
'G10' : 127,
};

export function note(noteString) {
if (typeof noteString === 'number') {
return noteString;
}
noteString = noteString.toUpperCase();
return NOTES[noteString];
}
exports.note = (noteString) => {
if (typeof noteString === 'number') {
return noteString;
}
noteString = noteString.toUpperCase();
return NOTES[noteString];
};
64 changes: 38 additions & 26 deletions src/scales.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import {TONE, SEMITONE, PERFECT_OCTAVE} from './intervals';
import {note as getNote} from './notes';
import { TONE, SEMITONE, PERFECT_OCTAVE } from './intervals';
import { note as getNote } from './notes';

const CHROMATIC_SCALE = [SEMITONE, SEMITONE, SEMITONE, SEMITONE, SEMITONE,
SEMITONE, SEMITONE, SEMITONE, SEMITONE, SEMITONE, SEMITONE, SEMITONE];
const CHROMATIC_SCALE = [
SEMITONE,
SEMITONE,
SEMITONE,
SEMITONE,
SEMITONE,
SEMITONE,
SEMITONE,
SEMITONE,
SEMITONE,
SEMITONE,
SEMITONE,
SEMITONE,
];
const WHOLE_TONE_SCALE = [TONE, TONE, TONE, TONE, TONE, TONE, TONE];

const IONIAN_MODE = [TONE, TONE, SEMITONE, TONE, TONE, TONE, SEMITONE];
Expand All @@ -14,28 +26,28 @@ const AEOLIAN_MODE = [TONE, SEMITONE, TONE, TONE, SEMITONE, TONE, TONE];
const LOCRIAN_MODE = [SEMITONE, TONE, TONE, SEMITONE, TONE, TONE, TONE];

var Scales = {
ionian : IONIAN_MODE,
dorian : DORIAN_MODE,
phrygian : PHRYGIAN_MODE,
lydian : LYDIAN_MODE,
mixolydian : MIXOLYDIAN_MODE,
aeolian : AEOLIAN_MODE,
locrian : LOCRIAN_MODE,
major : IONIAN_MODE,
minor : AEOLIAN_MODE
ionian: IONIAN_MODE,
dorian: DORIAN_MODE,
phrygian: PHRYGIAN_MODE,
lydian: LYDIAN_MODE,
mixolydian: MIXOLYDIAN_MODE,
aeolian: AEOLIAN_MODE,
locrian: LOCRIAN_MODE,
major: IONIAN_MODE,
minor: AEOLIAN_MODE,
};

export function scale(root, scale, length) {
scale = Scales[scale];
let notes = [];
let note = getNote(root);
for (let i = 0; i < length; i++) {
note = note + scale[i % scale.length];
notes.push(note);
}
return notes;
}
exports.scale = (root, scale, length) => {
scale = Scales[scale];
let notes = [];
let note = getNote(root);
for (let i = 0; i < length; i++) {
note = note + scale[i % scale.length];
notes.push(note);
}
return notes;
};

export function registerScale(name, scale) {
Scales[name] = scale;
}
exports.registerScale = (name, scale) => {
Scales[name] = scale;
};