tonaljs / tonal

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

Chords from intervals #37

Closed Mdashdotdashn closed 7 years ago

Mdashdotdashn commented 7 years ago

I'm looking for a way to convert a set of intervals to the chords they make (the inverse of chord.intervals)

Instinctively I've tried tonal.chord.detect('1P 3M 5P') but it didn't work.

It looks like all the information is there but I'm not sure the api allows me to do this. Is there a clever way to do this I didn't see ?

Thanks !

mrjacobbloom commented 7 years ago

you could maybe do something like

function intervalsToType(input) { // input = '1P 3M 5P'
  var input_array = input.split(' '); // ['1P', '3M', '5P']
  var chordInC_array = input_array.map(int => tonal.transpose('C4', int)); // ['C4', 'E4', 'G4']
  var chordInC_string = chordInC_array.join(' '); // 'C4 E4 G4'
  var chord = tonal.chord.parse(chordInC_string); // {tonic: 'C', type: 'M'}
  return chord.type // 'M'
}

I haven't tested that but something along those lines might work

danigb commented 7 years ago

Thanks for the reply @mrjacobbloom :+1:

Just a note: tonal can work with arrays or strings, there's no need of conversion. So your code can be simplified to:

const input  = '1P 3M 5P'
tonal.chord.detect(tonal.harmonize(input, 'C')) // => ['CM', 'Em#5']

Edited:

or, more similar to your code:

const notes = tonal.harmonize(input, 'C');
const chord = tonal.chord.parse(notes);
return chord.type;
mre-ableton commented 7 years ago

Thanks !

danigb commented 7 years ago

Solution for tonal 1.0.0:

const intervals = ["1P", "3M", "5P"];
const chroma = Tonal.PcSet.chroma(intervals);
Tonal.chord.names(chroma); // => ["M", "Maj"]