The invocation would be something like: setattribute(NoteVal, 'content', txtNote(midi_val, 0)); where NoteVal is a Text object and midi_val is some 0-127 value.
The code is:
Code: Select all
decl tVal, trNotes, trSharps, i, posn;
// two separate arrays to represent C, C#, D, D#, E, F, F#, G, G#, A, A#, B
trNotes = {0x43, 0x43, 0x44, 0x44, 0x45, 0x46, 0x46, 0x47, 0x47, 0x41, 0x41, 0x42};
trSharps = {0x00, 0x23, 0x00, 0x23, 0x00, 0x00, 0x23, 0x00, 0x23, 0x00, 0x23, 0x00};
i = 0;
// offset = 0 is a base way to go from C-2 to G8
// +12 for starting at C-1, +21 for starting at A-1, +24 for starting at C0, +36 for starting at C1
if (offset < 0) return ('ERR');
posn = midiVal + offset;
tVal[i++] = trNotes[posn % 12];
tVal[i] = trSharps[posn % 12];
if (tVal[i] != 0) // if we have a sharp symbol, move to next space
i++;
if (posn < 24) // 24 is c0 w. no offset. 0x2D is a '-'
tVal[i++] = 0x2D;
if (posn > 143) { // 144 is when you need a tens place. 0x31 is a '1'
tVal[i++] = 0x31;
posn = posn - 144 + 24; // 24 is for the un-offset original 2 octaves
}
tVal[i] = 0x30 + abs(2 - floor(posn / 12));
return(arraytostring(tVal));