1
\$\begingroup\$

I'm trying to program a musical stave and have come against the problem of where to place a note, given its index.

The index of a note is defined from a model created from the table in the following: Piano Note Frequencies

For example, in the model, C4 has an index of 40 and D#5 an index of 55.

Here is an image of what the stave currently looks like: Stave

The text in red shows the indices the stave has available to put notes (disregard the horizontal position). Only a few indices are shown, however they extend much further. For example, placing a note on the stave at index 0, it appears at A4 on the stave.

I'm having difficulty translating from an index in the note model, to an index on the musical stave.

At first, I've done a (knowingly) naive implementation. Index 0 in the stave corresponds to A4, or index 49 in the note model. Given an index from the model, calculate how many half steps away the index is and use this as the index in the stave. For example, C5 is index 52 and gives a stave index of 3. Looking at the above image, it's clear this is wrong for two reasons.

Firstly, we didn't account for a note and its sharp laying on the same index. Secondly, we didn't account that B4 and C5 have one half step between them, but are in different places on the stave.

I've been scratching my head over this for a while. What are the rules to follow for translating from a note index to a position on the stave?

EDIT: Each note in the note model knows whether it is sharp or not, its pitch (e.g. C, D, E, etc), and octave.

\$\endgroup\$
1
\$\begingroup\$

Your indexing system for the staff cannot be used to describe all notes. The main reason is that you have 7 unique lines on the staff (A, B, C, D, E, F, G) but there are 12 unique notes (A, A#, B, C, C#, D, D#, E, F, F#, G, G#).

You need another flag to translate back and forth. My first thought would be a flag called IsSharpFlat that returns -1, 0, 1.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.