Skip to content

Commit

Permalink
Get a triad chord for a given degree in the scale
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomartins committed Dec 1, 2019
1 parent 0f4bbe8 commit cbd74dd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ Scale.prototype = {
return this.tonic.interval(interval).interval(new Interval([octaves, 0]));
},

getTriadChord: function(index) {
if (this.scale.length !== 7) {
throw new Error('Chord degrees are only supported for diatonic scales');
}
var root = this.get(index);
var third = root.interval(this.get(index + 2)).toString();
var fifth = root.interval(this.get(index + 4)).toString();
var quality;
if (third === 'M3') {
quality = fifth === 'P5' ? 'M' : 'aug';
} else {
quality = fifth === 'P5' ? 'm' : 'dim';
}
return root.chord(quality);
},

solfege: function(index, showOctaves) {
if (index)
return this.get(index).solfege(this, showOctaves);
Expand Down
28 changes: 28 additions & 0 deletions test/scales.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,33 @@ vows.describe('Scales').addBatch({
'-13 is two octaves down': function(topic) {
assert.deepEqual(topic.get(-13), teoria.note('A2'));
}
},

'Chord Degrees': {
'D major': function() {
var scale = teoria.note('D').scale('major');
var expected = ['DM', 'Em', 'F#m', 'GM', 'AM', 'Bm', 'C#dim'];
var actual = scale.notes().map((n, i) => scale.getTriadChord(i+1).name);
assert.deepEqual(actual, expected);
},

'Bb minor': function() {
var scale = teoria.note('Bb').scale('minor');
var expected = ['Bbm', 'Cdim', 'DbM', 'Ebm', 'Fm', 'GbM', 'AbM'];
var actual = scale.notes().map((n, i) => scale.getTriadChord(i + 1).name);
assert.deepEqual(actual, expected);
},

'G mixolydian': function () {
var scale = teoria.note('G').scale('mixolydian');
var expected = ['GM', 'Am', 'Bdim', 'CM', 'Dm', 'Em', 'FM'];
var actual = scale.notes().map((n, i) => scale.getTriadChord(i + 1).name);
assert.deepEqual(actual, expected);
},

'only supports diatonic scales': function() {
var scale = teoria.note('C').scale('minorpentatonic');
assert.throws(() => scale.getTriadChord(1), Error);
}
}
}).export(module);

0 comments on commit cbd74dd

Please sign in to comment.