-
Notifications
You must be signed in to change notification settings - Fork 114
/
index.js
80 lines (61 loc) · 2.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var Note = require('./lib/note');
var Interval = require('./lib/interval');
var Chord = require('./lib/chord');
var Scale = require('./lib/scale');
var teoria;
// never thought I would write this, but: Legacy support
function intervalConstructor(from, to) {
// Construct a Interval object from string representation
if (typeof from === 'string')
return Interval.toCoord(from);
if (typeof to === 'string' && from instanceof Note)
return Interval.from(from, Interval.toCoord(to));
if (to instanceof Interval && from instanceof Note)
return Interval.from(from, to);
if (to instanceof Note && from instanceof Note)
return Interval.between(from, to);
throw new Error('Invalid parameters');
}
intervalConstructor.toCoord = Interval.toCoord;
intervalConstructor.from = Interval.from;
intervalConstructor.between = Interval.between;
intervalConstructor.invert = Interval.invert;
function noteConstructor(name, duration) {
if (typeof name === 'string')
return Note.fromString(name, duration);
else
return new Note(name, duration);
}
noteConstructor.fromString = Note.fromString;
noteConstructor.fromKey = Note.fromKey;
noteConstructor.fromFrequency = Note.fromFrequency;
noteConstructor.fromMIDI = Note.fromMIDI;
function chordConstructor(name, symbol) {
if (typeof name === 'string') {
var root, octave;
root = name.match(/^([a-h])(x|#|bb|b?)/i);
if (root && root[0]) {
octave = typeof symbol === 'number' ? symbol.toString(10) : '4';
return new Chord(Note.fromString(root[0].toLowerCase() + octave),
name.substr(root[0].length));
}
} else if (name instanceof Note)
return new Chord(name, symbol);
throw new Error('Invalid Chord. Couldn\'t find note name');
}
function scaleConstructor(tonic, scale) {
tonic = (tonic instanceof Note) ? tonic : teoria.note(tonic);
return new Scale(tonic, scale);
}
teoria = {
note: noteConstructor,
chord: chordConstructor,
interval: intervalConstructor,
scale: scaleConstructor,
Note: Note,
Chord: Chord,
Scale: Scale,
Interval: Interval
};
require('./lib/sugar')(teoria);
exports = module.exports = teoria;