saebekassebil / teoria

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

Position of notes on the staff #102

Closed amitgur closed 8 years ago

amitgur commented 8 years ago

Any way to get note position in the staff ? example E4 on line 2 A4 between lines 2 and 3 etc...

Silverwolf90 commented 8 years ago

This also depends on staff's clef. As this is specific to music notation my guess is that this would be a bit out of scope for teoria.

Although it's rather trivial to do. You just need to take the diatonic integer value of your pitch, subtracted by the pitch of the bottom staff line (E4 in treble clef) and divide that result by 2.

For the staff line of A4 in treble clef:

c d e f g a b
1 2 3 4 5 6 7

(A4 - E4) / 2 = (6 + 7 * 4) - (3 + 7 * 4) / 2 = 1.5
saebekassebil commented 8 years ago

Hey Amit and Cyril.

Indeed I think this might be a bit out of scope for teoria, but could easily be made into a separate module. As @Silverwolf90 notes, the staff position is dependent on the clef you're using.

I'd probably use the "internal" function Note#key(true) which is the "diatonic integer value" of the pitch + the octave value. So basically it's just counting all the white keys and leaving out the blacks.

You'll need a constant or a reference point to where 0 is. If we use the white key method above, that constant would be C4 = 24. Now we can position our notes relative to C4. C4 in our normal treble clef (𝄞) is at staff position 2.5 (counting the lowest line as 0). So our positioning algorithm would be:

var C4 = 24;
var C4pos = 2.5;
var note = teoria.note('D4');

var staffPosition = (note.key(true) - C4) / 2 + C4pos
staffPosition == 3

I hope that makes some kind of sense - although it turned out a bit longer than I expected :)

saebekassebil commented 8 years ago

I just made a module to help out with this problem. It's aptly named note-staff-position.

I hope this solves your problem Amit? Otherwise feel free to re-open :)