saebekassebil / teoria

Javascript taught Music Theory
http://saebekassebil.github.io/teoria
MIT License
1.31k stars 114 forks source link

Simpler value() function #125

Open tobx opened 5 years ago

tobx commented 5 years ago

Hey, I love your project and try to understand how things work (just for fun).

I think the Interval.prototype.value() function can be a lot simplified. An octave has 7 key steps, a fifths has 4. So why not simplify the function like this:

let steps = vector.sum(vector.mul(this.coord, [7, 4]));
return steps + (steps < 0 ? -1 : 1);

I tried it with a small test and it seems to work:

['P1', 'M3', 'm13', 'P15', 'd12', 'AA3', 'AA-1', 'M-14', 'm-6', 'P-15', 'd-12', 'AA-10'].forEach((name) => {
    let interval = teoria.interval(name);
    let steps = interval.coord[0] * 7 + interval.coord[1] * 4;
    let value = steps + (steps < 0 ? -1 : 1);
    console.log(name, value === interval.value());
});

I hope I have not overlooked something...