tonaljs / tonal

A music theory library for Javascript
https://tonaljs.github.io/tonal/docs
3.84k stars 220 forks source link

Is there a way to get possible keys based a set of notes? #198

Closed nieldlr closed 4 years ago

nieldlr commented 4 years ago

Hi,

totally in love with this library! Thanks so much.

I'm not sure if I'm missing this, but I'd like to have the possibility provide a set of notes and tell me what possible keys work with these notes? Is that possible at the moment? Sorry if this is an obvious one!

danigb commented 4 years ago

Hi,

glad you like it! :-)

I think there's no obvious or easy way to do it. I guess some approach is to generate the major scale for every key you want to test (this is the first decision to make: are flats and sharps allowed as key root?). Then with the @tonaljs/pcset utilities, find if a collection of notes matches the set.

Something like this (but iterating for all desired keys to check):

const cMajor = Scale.get('C major').notes;
const isCMajorKey = PcSet.isSubsetOf(cMajor);
isCMajorKey(['C', 'D', 'E') // => true
isCMajorKey(['C', 'D', 'Eb') // => false

Hope it helps

nieldlr commented 4 years ago

I think that helps yes! Got a bit sidetracked at the moment, but will try this in the future. Thanks @danigb!